BreadcrumbBar.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibGUI/BoxLayout.h>
  27. #include <LibGUI/BreadcrumbBar.h>
  28. #include <LibGUI/Button.h>
  29. #include <LibGfx/Font.h>
  30. namespace GUI {
  31. // FIXME: Move this somewhere else
  32. class UnuncheckableButton : public GUI::Button {
  33. C_OBJECT(UnuncheckableButton);
  34. public:
  35. virtual ~UnuncheckableButton() override { }
  36. virtual bool is_uncheckable() const override { return false; }
  37. private:
  38. UnuncheckableButton() { }
  39. };
  40. BreadcrumbBar::BreadcrumbBar()
  41. {
  42. auto& layout = set_layout<HorizontalBoxLayout>();
  43. layout.set_spacing(0);
  44. }
  45. BreadcrumbBar::~BreadcrumbBar()
  46. {
  47. }
  48. void BreadcrumbBar::clear_segments()
  49. {
  50. m_segments.clear();
  51. remove_all_children();
  52. }
  53. void BreadcrumbBar::append_segment(const String& text, const Gfx::Bitmap* icon, const String& data)
  54. {
  55. auto& button = add<UnuncheckableButton>();
  56. button.set_button_style(Gfx::ButtonStyle::CoolBar);
  57. button.set_text(text);
  58. button.set_icon(icon);
  59. button.set_focus_policy(FocusPolicy::TabFocus);
  60. button.set_checkable(true);
  61. button.set_exclusive(true);
  62. button.on_click = [this, index = m_segments.size()](auto) {
  63. if (on_segment_click)
  64. on_segment_click(index);
  65. };
  66. button.set_size_policy(SizePolicy::Fixed, SizePolicy::Fixed);
  67. auto button_text_width = button.font().width(text);
  68. auto icon_width = icon ? icon->width() : 0;
  69. auto icon_padding = icon ? 4 : 0;
  70. button.set_preferred_size(button_text_width + icon_width + icon_padding + 16, 16 + 8);
  71. Segment segment { icon, text, data, button.make_weak_ptr<GUI::Button>() };
  72. m_segments.append(move(segment));
  73. }
  74. void BreadcrumbBar::set_selected_segment(Optional<size_t> index)
  75. {
  76. if (!index.has_value()) {
  77. for_each_child_of_type<GUI::AbstractButton>([&](auto& button) {
  78. button.set_checked(false);
  79. return IterationDecision::Continue;
  80. });
  81. return;
  82. }
  83. auto& segment = m_segments[index.value()];
  84. ASSERT(segment.button);
  85. segment.button->set_checked(true);
  86. }
  87. }