Breadcrumbbar.cpp 4.6 KB

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