Breadcrumbbar.cpp 5.8 KB

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