Breadcrumbbar.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
  4. * Copyright (c) 2022, the SerenityOS developers.
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <LibGUI/BoxLayout.h>
  9. #include <LibGUI/Breadcrumbbar.h>
  10. #include <LibGUI/Button.h>
  11. #include <LibGUI/Painter.h>
  12. #include <LibGfx/Font/Font.h>
  13. #include <LibGfx/Palette.h>
  14. REGISTER_WIDGET(GUI, Breadcrumbbar)
  15. namespace GUI {
  16. class BreadcrumbButton : public Button {
  17. C_OBJECT(BreadcrumbButton);
  18. public:
  19. virtual ~BreadcrumbButton() override = default;
  20. virtual bool is_uncheckable() const override { return false; }
  21. virtual void drop_event(DropEvent& event) override
  22. {
  23. if (on_drop)
  24. on_drop(event);
  25. }
  26. virtual void drag_enter_event(DragEvent& event) override
  27. {
  28. update();
  29. if (on_drag_enter)
  30. on_drag_enter(event);
  31. }
  32. virtual void drag_leave_event(Event&) override
  33. {
  34. update();
  35. }
  36. virtual void paint_event(PaintEvent& event) override
  37. {
  38. Button::paint_event(event);
  39. if (has_pending_drop()) {
  40. Painter painter(*this);
  41. painter.draw_rect(rect(), palette().selection(), true);
  42. }
  43. }
  44. Function<void(DropEvent&)> on_drop;
  45. Function<void(DragEvent&)> on_drag_enter;
  46. private:
  47. BreadcrumbButton() = default;
  48. };
  49. Breadcrumbbar::Breadcrumbbar()
  50. {
  51. auto& layout = set_layout<HorizontalBoxLayout>();
  52. layout.set_spacing(0);
  53. }
  54. void Breadcrumbbar::clear_segments()
  55. {
  56. m_segments.clear();
  57. remove_all_children();
  58. }
  59. void Breadcrumbbar::append_segment(String text, Gfx::Bitmap const* icon, String data, String tooltip)
  60. {
  61. auto& button = add<BreadcrumbButton>();
  62. button.set_button_style(Gfx::ButtonStyle::Coolbar);
  63. button.set_text(text);
  64. button.set_icon(icon);
  65. button.set_tooltip(move(tooltip));
  66. button.set_focus_policy(FocusPolicy::TabFocus);
  67. button.set_checkable(true);
  68. button.set_exclusive(true);
  69. button.on_click = [this, index = m_segments.size()](auto) {
  70. if (on_segment_click)
  71. on_segment_click(index);
  72. };
  73. button.on_focus_change = [this, index = m_segments.size()](auto has_focus, auto) {
  74. if (has_focus && on_segment_click)
  75. on_segment_click(index);
  76. };
  77. button.on_drop = [this, index = m_segments.size()](auto& drop_event) {
  78. if (on_segment_drop)
  79. on_segment_drop(index, drop_event);
  80. };
  81. button.on_drag_enter = [this, index = m_segments.size()](auto& event) {
  82. if (on_segment_drag_enter)
  83. on_segment_drag_enter(index, event);
  84. };
  85. auto button_text_width = button.font().width(text);
  86. auto icon_width = icon ? icon->width() : 0;
  87. auto icon_padding = icon ? 4 : 0;
  88. int const max_button_width = 100;
  89. auto button_width = min(button_text_width + icon_width + icon_padding + 16, max_button_width);
  90. auto shrunken_width = icon_width + icon_padding + (icon ? 4 : 16);
  91. button.set_fixed_size(button_width, 16 + 8);
  92. Segment segment { icon, text, data, button_width, shrunken_width, button.make_weak_ptr<GUI::Button>() };
  93. m_segments.append(move(segment));
  94. relayout();
  95. }
  96. void Breadcrumbbar::remove_end_segments(size_t start_segment_index)
  97. {
  98. while (segment_count() > start_segment_index) {
  99. auto segment = m_segments.take_last();
  100. remove_child(*segment.button);
  101. }
  102. }
  103. Optional<size_t> Breadcrumbbar::find_segment_with_data(String const& data)
  104. {
  105. for (size_t i = 0; i < segment_count(); ++i) {
  106. if (segment_data(i) == data)
  107. return i;
  108. }
  109. return {};
  110. }
  111. void Breadcrumbbar::set_selected_segment(Optional<size_t> index)
  112. {
  113. if (!index.has_value()) {
  114. for_each_child_of_type<GUI::AbstractButton>([&](auto& button) {
  115. button.set_checked(false);
  116. return IterationDecision::Continue;
  117. });
  118. return;
  119. }
  120. auto& segment = m_segments[index.value()];
  121. VERIFY(segment.button);
  122. segment.button->set_checked(true);
  123. relayout();
  124. }
  125. void Breadcrumbbar::doubleclick_event(MouseEvent& event)
  126. {
  127. if (on_doubleclick)
  128. on_doubleclick(event);
  129. }
  130. void Breadcrumbbar::resize_event(ResizeEvent&)
  131. {
  132. relayout();
  133. }
  134. void Breadcrumbbar::relayout()
  135. {
  136. auto remaining_width = 0;
  137. for (auto& segment : m_segments)
  138. remaining_width += segment.width;
  139. for (auto& segment : m_segments) {
  140. if (remaining_width > width() && !segment.button->is_checked()) {
  141. segment.button->set_fixed_width(segment.shrunken_width);
  142. remaining_width -= (segment.width - segment.shrunken_width);
  143. continue;
  144. }
  145. segment.button->set_fixed_width(segment.width);
  146. }
  147. }
  148. }