Toolbar.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. REGISTER_BOOL_PROPERTY("collapsible", is_collapsible, set_collapsible);
  26. if (m_orientation == Orientation::Horizontal)
  27. set_fixed_height(button_size);
  28. else
  29. set_fixed_width(button_size);
  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(" ("sv);
  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. item->widget = TRY(try_add<ToolbarButton>(action));
  87. item->widget->set_fixed_size(m_button_size, m_button_size);
  88. m_items.unchecked_append(move(item));
  89. return *static_cast<Button*>(m_items.last().widget.ptr());
  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. item->widget = 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. Optional<UISize> Toolbar::calculated_preferred_size() const
  117. {
  118. if (m_orientation == Gfx::Orientation::Horizontal)
  119. return { { SpecialDimension::Grow, SpecialDimension::Fit } };
  120. else
  121. return { { SpecialDimension::Fit, SpecialDimension::Grow } };
  122. VERIFY_NOT_REACHED();
  123. }
  124. Optional<UISize> Toolbar::calculated_min_size() const
  125. {
  126. if (m_collapsible) {
  127. if (m_orientation == Gfx::Orientation::Horizontal)
  128. return UISize(m_button_size, SpecialDimension::Shrink);
  129. else
  130. return UISize(SpecialDimension::Shrink, m_button_size);
  131. }
  132. VERIFY(layout());
  133. return { layout()->min_size() };
  134. }
  135. ErrorOr<void> Toolbar::create_overflow_objects()
  136. {
  137. m_overflow_action = Action::create("Overflow Menu", { Mod_Ctrl | Mod_Shift, Key_O }, TRY(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/overflow-menu.png"sv)), [&](auto&) {
  138. m_overflow_menu->popup(m_overflow_button->screen_relative_rect().bottom_left(), {}, m_overflow_button->rect());
  139. });
  140. m_overflow_action->set_status_tip("Show hidden toolbar actions");
  141. m_overflow_action->set_enabled(false);
  142. TRY(layout()->try_add_spacer());
  143. m_overflow_button = TRY(try_add_action(*m_overflow_action));
  144. m_overflow_button->set_visible(false);
  145. return {};
  146. }
  147. ErrorOr<void> Toolbar::update_overflow_menu()
  148. {
  149. if (!m_collapsible)
  150. return {};
  151. Optional<size_t> marginal_index {};
  152. auto position { 0 };
  153. auto is_horizontal { m_orientation == Gfx::Orientation::Horizontal };
  154. auto margin { is_horizontal ? layout()->margins().right() : layout()->margins().bottom() };
  155. auto toolbar_size { is_horizontal ? width() : height() };
  156. for (size_t i = 0; i < m_items.size() - 1; ++i) {
  157. auto& item = m_items.at(i);
  158. auto item_size = is_horizontal ? item.widget->width() : item.widget->height();
  159. if (position + item_size + m_button_size + margin > toolbar_size) {
  160. marginal_index = i;
  161. break;
  162. }
  163. item.widget->set_visible(true);
  164. position += item_size;
  165. }
  166. if (!marginal_index.has_value()) {
  167. if (m_overflow_action) {
  168. m_overflow_action->set_enabled(false);
  169. m_overflow_button->set_visible(false);
  170. }
  171. return {};
  172. }
  173. if (!m_overflow_action)
  174. TRY(create_overflow_objects());
  175. m_overflow_action->set_enabled(true);
  176. m_overflow_button->set_visible(true);
  177. m_overflow_menu = TRY(Menu::try_create());
  178. m_overflow_button->set_menu(m_overflow_menu);
  179. for (size_t i = marginal_index.value(); i < m_items.size(); ++i) {
  180. auto& item = m_items.at(i);
  181. Item* peek_item;
  182. if (i > 0) {
  183. peek_item = &m_items.at(i - 1);
  184. if (peek_item->type == Item::Type::Separator)
  185. peek_item->widget->set_visible(false);
  186. }
  187. if (i < m_items.size() - 1) {
  188. item.widget->set_visible(false);
  189. peek_item = &m_items.at(i + 1);
  190. if (item.action)
  191. TRY(m_overflow_menu->try_add_action(*item.action));
  192. }
  193. if (item.action && peek_item->type == Item::Type::Separator)
  194. TRY(m_overflow_menu->try_add_separator());
  195. }
  196. return {};
  197. }
  198. void Toolbar::resize_event(GUI::ResizeEvent& event)
  199. {
  200. Widget::resize_event(event);
  201. if (auto result = update_overflow_menu(); result.is_error())
  202. warnln("Failed to update overflow menu");
  203. }
  204. }