BreadcrumbBar.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. // FIXME: Should Core::Object have something like "remove_all_children()" perhaps?
  52. auto children = this->children();
  53. for (auto& child : children)
  54. child.remove_from_parent();
  55. }
  56. void BreadcrumbBar::append_segment(const String& text, const Gfx::Bitmap* icon, const String& data)
  57. {
  58. auto& button = add<UnuncheckableButton>();
  59. button.set_button_style(Gfx::ButtonStyle::CoolBar);
  60. button.set_text(text);
  61. button.set_icon(icon);
  62. button.set_checkable(true);
  63. button.set_exclusive(true);
  64. button.on_click = [this, index = m_segments.size()](auto) {
  65. if (on_segment_click)
  66. on_segment_click(index);
  67. };
  68. button.set_size_policy(SizePolicy::Fixed, SizePolicy::Fixed);
  69. auto button_text_width = button.font().width(text);
  70. auto icon_width = icon ? icon->width() : 0;
  71. auto icon_padding = icon ? 4 : 0;
  72. button.set_preferred_size(button_text_width + icon_width + icon_padding + 16, 16 + 8);
  73. Segment segment { icon, text, data, button.make_weak_ptr<GUI::Button>() };
  74. m_segments.append(move(segment));
  75. }
  76. void BreadcrumbBar::set_selected_segment(Optional<size_t> index)
  77. {
  78. if (!index.has_value()) {
  79. for_each_child_of_type<GUI::AbstractButton>([&](auto& button) {
  80. button.set_checked(false);
  81. return IterationDecision::Continue;
  82. });
  83. return;
  84. }
  85. auto& segment = m_segments[index.value()];
  86. ASSERT(segment.button);
  87. segment.button->set_checked(true);
  88. }
  89. }