From eefe6e35ac06f1909a0ec5c1a739f34aef68c538 Mon Sep 17 00:00:00 2001 From: FrHun <28605587+frhun@users.noreply.github.com> Date: Wed, 5 Oct 2022 18:19:18 +0200 Subject: [PATCH] LibGUI: Add option to move Toolbar items to overflow menu in groups When items are sent to the overflow menu one by one, it can happen that buttons that are heavily related, and don't make sense without one another (either visually or logically) are separated. This new option enables the developer to choose the "grouping" behavior, of sending all items that are not separated to the overflow menu together, as soon as one of them doesn't have enough space to be displayed. (provided the toolbar is set as collapsible) --- Userland/Libraries/LibGUI/Toolbar.cpp | 11 +++++++++++ Userland/Libraries/LibGUI/Toolbar.h | 3 +++ 2 files changed, 14 insertions(+) diff --git a/Userland/Libraries/LibGUI/Toolbar.cpp b/Userland/Libraries/LibGUI/Toolbar.cpp index 5380af356a..daca0bd235 100644 --- a/Userland/Libraries/LibGUI/Toolbar.cpp +++ b/Userland/Libraries/LibGUI/Toolbar.cpp @@ -27,6 +27,7 @@ Toolbar::Toolbar(Orientation orientation, int button_size) , m_button_size(button_size) { REGISTER_BOOL_PROPERTY("collapsible", is_collapsible, set_collapsible); + REGISTER_BOOL_PROPERTY("grouped", is_grouped, set_grouped); if (m_orientation == Orientation::Horizontal) set_fixed_height(button_size); @@ -202,6 +203,16 @@ ErrorOr Toolbar::update_overflow_menu() return {}; } + if (m_grouped) { + for (size_t i = marginal_index.value(); i > 0; --i) { + auto& item = m_items.at(i); + if (item.type == Item::Type::Separator) + break; + item.widget->set_visible(false); + marginal_index = i; + } + } + if (!m_overflow_action) TRY(create_overflow_objects()); m_overflow_action->set_enabled(true); diff --git a/Userland/Libraries/LibGUI/Toolbar.h b/Userland/Libraries/LibGUI/Toolbar.h index 177578e799..8a9ec2cf1b 100644 --- a/Userland/Libraries/LibGUI/Toolbar.h +++ b/Userland/Libraries/LibGUI/Toolbar.h @@ -27,6 +27,8 @@ public: bool is_collapsible() const { return m_collapsible; } void set_collapsible(bool b) { m_collapsible = b; } + bool is_grouped() const { return m_grouped; } + void set_grouped(bool b) { m_grouped = b; } virtual Optional calculated_preferred_size() const override; virtual Optional calculated_min_size() const override; @@ -58,6 +60,7 @@ private: const Gfx::Orientation m_orientation; int m_button_size { 24 }; bool m_collapsible { false }; + bool m_grouped { false }; }; }