Toolbar.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/String.h>
  8. #include <AK/StringBuilder.h>
  9. #include <LibCore/EventLoop.h>
  10. #include <LibGUI/Action.h>
  11. #include <LibGUI/ActionGroup.h>
  12. #include <LibGUI/Application.h>
  13. #include <LibGUI/BoxLayout.h>
  14. #include <LibGUI/Button.h>
  15. #include <LibGUI/Painter.h>
  16. #include <LibGUI/SeparatorWidget.h>
  17. #include <LibGUI/Toolbar.h>
  18. #include <LibGfx/Palette.h>
  19. REGISTER_WIDGET(GUI, Toolbar)
  20. namespace GUI {
  21. Toolbar::Toolbar(Orientation orientation, int button_size)
  22. : m_orientation(orientation)
  23. , m_button_size(button_size)
  24. {
  25. if (m_orientation == Orientation::Horizontal) {
  26. set_fixed_height(button_size + 8);
  27. } else {
  28. set_fixed_width(button_size + 8);
  29. }
  30. set_layout<BoxLayout>(orientation);
  31. layout()->set_spacing(0);
  32. layout()->set_margins({ 2, 2, 2, 2 });
  33. }
  34. class ToolbarButton final : public Button {
  35. C_OBJECT(ToolbarButton);
  36. public:
  37. virtual ~ToolbarButton() override = default;
  38. private:
  39. explicit ToolbarButton(Action& action)
  40. {
  41. if (action.group() && action.group()->is_exclusive())
  42. set_exclusive(true);
  43. set_action(action);
  44. set_tooltip(tooltip(action));
  45. set_focus_policy(FocusPolicy::NoFocus);
  46. if (action.icon())
  47. set_icon(action.icon());
  48. else
  49. set_text(action.text());
  50. set_button_style(Gfx::ButtonStyle::Coolbar);
  51. }
  52. String tooltip(Action const& action) const
  53. {
  54. StringBuilder builder;
  55. builder.append(action.text());
  56. if (action.shortcut().is_valid()) {
  57. builder.append(" (");
  58. builder.append(action.shortcut().to_string());
  59. builder.append(")");
  60. }
  61. return builder.to_string();
  62. }
  63. virtual void enter_event(Core::Event& event) override
  64. {
  65. auto* app = Application::the();
  66. if (app && action())
  67. Core::EventLoop::current().post_event(*app, make<ActionEvent>(ActionEvent::Type::ActionEnter, *action()));
  68. return Button::enter_event(event);
  69. }
  70. virtual void leave_event(Core::Event& event) override
  71. {
  72. auto* app = Application::the();
  73. if (app && action())
  74. Core::EventLoop::current().post_event(*app, make<ActionEvent>(ActionEvent::Type::ActionLeave, *action()));
  75. return Button::leave_event(event);
  76. }
  77. };
  78. ErrorOr<NonnullRefPtr<GUI::Button>> Toolbar::try_add_action(Action& action)
  79. {
  80. auto item = TRY(adopt_nonnull_own_or_enomem(new (nothrow) Item));
  81. item->type = Item::Type::Action;
  82. item->action = action;
  83. // NOTE: Grow the m_items capacity before potentially adding a child widget.
  84. // This avoids having to untangle the child widget in case of allocation failure.
  85. TRY(m_items.try_ensure_capacity(m_items.size() + 1));
  86. auto button = TRY(try_add<ToolbarButton>(action));
  87. button->set_fixed_size(m_button_size + 8, m_button_size + 8);
  88. m_items.unchecked_append(move(item));
  89. return button;
  90. }
  91. GUI::Button& Toolbar::add_action(Action& action)
  92. {
  93. auto button = MUST(try_add_action(action));
  94. return *button;
  95. }
  96. ErrorOr<void> Toolbar::try_add_separator()
  97. {
  98. // NOTE: Grow the m_items capacity before potentially adding a child widget.
  99. TRY(m_items.try_ensure_capacity(m_items.size() + 1));
  100. auto item = TRY(adopt_nonnull_own_or_enomem(new (nothrow) Item));
  101. item->type = Item::Type::Separator;
  102. (void)TRY(try_add<SeparatorWidget>(m_orientation == Gfx::Orientation::Horizontal ? Gfx::Orientation::Vertical : Gfx::Orientation::Horizontal));
  103. m_items.unchecked_append(move(item));
  104. return {};
  105. }
  106. void Toolbar::add_separator()
  107. {
  108. MUST(try_add_separator());
  109. }
  110. void Toolbar::paint_event(PaintEvent& event)
  111. {
  112. Painter painter(*this);
  113. painter.add_clip_rect(event.rect());
  114. painter.fill_rect(event.rect(), palette().button());
  115. }
  116. }