Breadcrumbbar.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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_drop = [this, index = m_segments.size()](auto& drop_event) {
  76. if (on_segment_drop)
  77. on_segment_drop(index, drop_event);
  78. };
  79. button.on_drag_enter = [this, index = m_segments.size()](auto& event) {
  80. if (on_segment_drag_enter)
  81. on_segment_drag_enter(index, event);
  82. };
  83. auto button_text_width = button.font().width(text);
  84. auto icon_width = icon ? icon->width() : 0;
  85. auto icon_padding = icon ? 4 : 0;
  86. const int max_button_width = 100;
  87. auto button_width = min(button_text_width + icon_width + icon_padding + 16, max_button_width);
  88. auto shrunken_width = icon_width + icon_padding + (icon ? 4 : 16);
  89. button.set_fixed_size(button_width, 16 + 8);
  90. Segment segment { icon, text, data, button_width, shrunken_width, button.make_weak_ptr<GUI::Button>() };
  91. m_segments.append(move(segment));
  92. relayout();
  93. }
  94. void Breadcrumbbar::remove_end_segments(size_t start_segment_index)
  95. {
  96. while (segment_count() > start_segment_index) {
  97. auto segment = m_segments.take_last();
  98. remove_child(*segment.button);
  99. }
  100. }
  101. Optional<size_t> Breadcrumbbar::find_segment_with_data(String const& data)
  102. {
  103. for (size_t i = 0; i < segment_count(); ++i) {
  104. if (segment_data(i) == data)
  105. return i;
  106. }
  107. return {};
  108. }
  109. void Breadcrumbbar::set_selected_segment(Optional<size_t> index)
  110. {
  111. if (!index.has_value()) {
  112. for_each_child_of_type<GUI::AbstractButton>([&](auto& button) {
  113. button.set_checked(false);
  114. return IterationDecision::Continue;
  115. });
  116. return;
  117. }
  118. auto& segment = m_segments[index.value()];
  119. VERIFY(segment.button);
  120. segment.button->set_checked(true);
  121. relayout();
  122. }
  123. void Breadcrumbbar::doubleclick_event(MouseEvent& event)
  124. {
  125. if (on_doubleclick)
  126. on_doubleclick(event);
  127. }
  128. void Breadcrumbbar::resize_event(ResizeEvent&)
  129. {
  130. relayout();
  131. }
  132. void Breadcrumbbar::relayout()
  133. {
  134. auto remaining_width = 0;
  135. for (auto& segment : m_segments)
  136. remaining_width += segment.width;
  137. for (auto& segment : m_segments) {
  138. if (remaining_width > width() && !segment.button->is_checked()) {
  139. segment.button->set_fixed_width(segment.shrunken_width);
  140. remaining_width -= (segment.width - segment.shrunken_width);
  141. continue;
  142. }
  143. segment.button->set_fixed_width(segment.width);
  144. }
  145. }
  146. }