Breadcrumbbar.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. set_layout<HorizontalBoxLayout>(GUI::Margins {}, 0);
  52. }
  53. void Breadcrumbbar::clear_segments()
  54. {
  55. m_segments.clear();
  56. remove_all_children();
  57. m_selected_segment = {};
  58. }
  59. void Breadcrumbbar::append_segment(DeprecatedString text, Gfx::Bitmap const* icon, DeprecatedString data, DeprecatedString tooltip)
  60. {
  61. auto& button = add<BreadcrumbButton>();
  62. button.set_button_style(Gfx::ButtonStyle::Coolbar);
  63. button.set_text(String::from_deprecated_string(text).release_value_but_fixme_should_propagate_errors());
  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. if (on_segment_change && m_selected_segment != index)
  73. on_segment_change(index);
  74. };
  75. button.on_double_click = [this](auto modifiers) {
  76. if (on_doubleclick)
  77. on_doubleclick(modifiers);
  78. };
  79. button.on_focus_change = [this, index = m_segments.size()](auto has_focus, auto) {
  80. if (has_focus && on_segment_change && m_selected_segment != index)
  81. on_segment_change(index);
  82. };
  83. button.on_drop = [this, index = m_segments.size()](auto& drop_event) {
  84. if (on_segment_drop)
  85. on_segment_drop(index, drop_event);
  86. };
  87. button.on_drag_enter = [this, index = m_segments.size()](auto& event) {
  88. if (on_segment_drag_enter)
  89. on_segment_drag_enter(index, event);
  90. };
  91. m_segments.append(Segment {
  92. .icon = icon,
  93. .text = move(text),
  94. .data = move(data),
  95. .width = 0,
  96. .shrunken_width = 0,
  97. .button = button.make_weak_ptr<GUI::Button>(),
  98. });
  99. relayout();
  100. }
  101. void Breadcrumbbar::remove_end_segments(size_t start_segment_index)
  102. {
  103. while (segment_count() > start_segment_index) {
  104. auto segment = m_segments.take_last();
  105. remove_child(*segment.button);
  106. }
  107. if (m_selected_segment.has_value() && *m_selected_segment >= start_segment_index)
  108. m_selected_segment = {};
  109. }
  110. Optional<size_t> Breadcrumbbar::find_segment_with_data(DeprecatedString const& data)
  111. {
  112. for (size_t i = 0; i < segment_count(); ++i) {
  113. if (segment_data(i) == data)
  114. return i;
  115. }
  116. return {};
  117. }
  118. void Breadcrumbbar::set_selected_segment(Optional<size_t> index)
  119. {
  120. if (m_selected_segment == index)
  121. return;
  122. m_selected_segment = index;
  123. if (!index.has_value()) {
  124. for_each_child_of_type<GUI::AbstractButton>([&](auto& button) {
  125. button.set_checked(false);
  126. return IterationDecision::Continue;
  127. });
  128. return;
  129. }
  130. auto& segment = m_segments[index.value()];
  131. VERIFY(segment.button);
  132. segment.button->set_checked(true);
  133. if (on_segment_change)
  134. on_segment_change(index);
  135. relayout();
  136. }
  137. void Breadcrumbbar::doubleclick_event(MouseEvent& event)
  138. {
  139. if (on_doubleclick)
  140. on_doubleclick(event.modifiers());
  141. }
  142. void Breadcrumbbar::resize_event(ResizeEvent&)
  143. {
  144. relayout();
  145. }
  146. void Breadcrumbbar::did_change_font()
  147. {
  148. Widget::did_change_font();
  149. relayout();
  150. }
  151. void Breadcrumbbar::relayout()
  152. {
  153. auto total_width = 0;
  154. for (auto& segment : m_segments) {
  155. VERIFY(segment.button);
  156. auto& button = *segment.button;
  157. // NOTE: We use our own font instead of the button's font here in case we're being notified about
  158. // a system font change, and the button hasn't been notified yet.
  159. auto button_text_width = font().width(segment.text);
  160. auto icon_width = button.icon() ? button.icon()->width() : 0;
  161. auto icon_padding = button.icon() ? 4 : 0;
  162. int const max_button_width = 100;
  163. segment.width = static_cast<int>(ceilf(min(button_text_width + icon_width + icon_padding + 16, max_button_width)));
  164. segment.shrunken_width = icon_width + icon_padding + (button.icon() ? 4 : 16);
  165. button.set_max_size(segment.width, 16 + 8);
  166. button.set_min_size(segment.shrunken_width, 16 + 8);
  167. total_width += segment.width;
  168. }
  169. auto remaining_width = total_width;
  170. for (auto& segment : m_segments) {
  171. if (remaining_width > width() && !segment.button->is_checked()) {
  172. segment.button->set_preferred_width(segment.shrunken_width);
  173. remaining_width -= (segment.width - segment.shrunken_width);
  174. continue;
  175. }
  176. segment.button->set_preferred_width(segment.width);
  177. }
  178. }
  179. }