Breadcrumbbar.cpp 5.8 KB

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