mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 02:27:43 +00:00
LibGUI+WindowServer: Add "visible" state to GUI actions
This patch adds a visibility state to GUI::Action. All actions default to being visible. When invisible, they do not show up in toolbars on menus (and importantly, they don't occupy any space). This can be used to hide/show context-sensitive actions dynamically without rebuilding menus and toolbars. Thanks to Tim Slater for assuming that action visibility was a thing, which gave me a reason to implement it! :^)
This commit is contained in:
parent
df7c0eacd4
commit
49f5767789
13 changed files with 88 additions and 14 deletions
|
@ -228,6 +228,19 @@ void Action::set_enabled(bool enabled)
|
|||
});
|
||||
}
|
||||
|
||||
void Action::set_visible(bool visible)
|
||||
{
|
||||
if (m_visible == visible)
|
||||
return;
|
||||
m_visible = visible;
|
||||
for_each_toolbar_button([visible](auto& button) {
|
||||
button.set_visible(visible);
|
||||
});
|
||||
for_each_menu_item([visible](auto& item) {
|
||||
item.set_visible(visible);
|
||||
});
|
||||
}
|
||||
|
||||
void Action::set_checked(bool checked)
|
||||
{
|
||||
if (m_checked == checked)
|
||||
|
|
|
@ -105,6 +105,9 @@ public:
|
|||
bool is_enabled() const { return m_enabled; }
|
||||
void set_enabled(bool);
|
||||
|
||||
bool is_visible() const { return m_visible; }
|
||||
void set_visible(bool);
|
||||
|
||||
bool is_checkable() const { return m_checkable; }
|
||||
void set_checkable(bool checkable) { m_checkable = checkable; }
|
||||
|
||||
|
@ -146,6 +149,7 @@ private:
|
|||
Shortcut m_shortcut;
|
||||
Shortcut m_alternate_shortcut;
|
||||
bool m_enabled { true };
|
||||
bool m_visible { true };
|
||||
bool m_checkable { false };
|
||||
bool m_checked { false };
|
||||
bool m_swallow_key_event_when_disabled { false };
|
||||
|
|
|
@ -165,6 +165,7 @@ void Button::set_action(Action& action)
|
|||
{
|
||||
m_action = action;
|
||||
action.register_button({}, *this);
|
||||
set_visible(action.is_visible());
|
||||
set_enabled(action.is_enabled());
|
||||
set_checkable(action.is_checkable());
|
||||
if (action.is_checkable())
|
||||
|
|
|
@ -201,14 +201,14 @@ void Menu::realize_menu_item(MenuItem& item, int item_id)
|
|||
bool exclusive = action.group() && action.group()->is_exclusive() && action.is_checkable();
|
||||
bool is_default = (m_current_default_action.ptr() == &action);
|
||||
auto icon = action.icon() ? action.icon()->to_shareable_bitmap() : Gfx::ShareableBitmap();
|
||||
ConnectionToWindowServer::the().async_add_menu_item(m_menu_id, item_id, -1, action.text(), action.is_enabled(), action.is_checkable(), action.is_checkable() ? action.is_checked() : false, is_default, shortcut_text, icon, exclusive);
|
||||
ConnectionToWindowServer::the().async_add_menu_item(m_menu_id, item_id, -1, action.text(), action.is_enabled(), action.is_visible(), action.is_checkable(), action.is_checkable() ? action.is_checked() : false, is_default, shortcut_text, icon, exclusive);
|
||||
break;
|
||||
}
|
||||
case MenuItem::Type::Submenu: {
|
||||
auto& submenu = *item.submenu();
|
||||
submenu.realize_if_needed(m_current_default_action.strong_ref());
|
||||
auto icon = submenu.icon() ? submenu.icon()->to_shareable_bitmap() : Gfx::ShareableBitmap();
|
||||
ConnectionToWindowServer::the().async_add_menu_item(m_menu_id, item_id, submenu.menu_id(), submenu.name(), true, false, false, false, "", icon, false);
|
||||
ConnectionToWindowServer::the().async_add_menu_item(m_menu_id, item_id, submenu.menu_id(), submenu.name(), true, true, false, false, false, "", icon, false);
|
||||
break;
|
||||
}
|
||||
case MenuItem::Type::Invalid:
|
||||
|
|
|
@ -50,6 +50,14 @@ void MenuItem::set_enabled(bool enabled)
|
|||
update_window_server();
|
||||
}
|
||||
|
||||
void MenuItem::set_visible(bool visible)
|
||||
{
|
||||
if (m_visible == visible)
|
||||
return;
|
||||
m_visible = visible;
|
||||
update_window_server();
|
||||
}
|
||||
|
||||
void MenuItem::set_checked(bool checked)
|
||||
{
|
||||
VERIFY(is_checkable());
|
||||
|
@ -75,7 +83,7 @@ void MenuItem::update_window_server()
|
|||
auto& action = *m_action;
|
||||
auto shortcut_text = action.shortcut().is_valid() ? action.shortcut().to_deprecated_string() : DeprecatedString();
|
||||
auto icon = action.icon() ? action.icon()->to_shareable_bitmap() : Gfx::ShareableBitmap();
|
||||
ConnectionToWindowServer::the().async_update_menu_item(m_menu_id, m_identifier, -1, action.text(), action.is_enabled(), action.is_checkable(), action.is_checkable() ? action.is_checked() : false, m_default, shortcut_text, icon);
|
||||
ConnectionToWindowServer::the().async_update_menu_item(m_menu_id, m_identifier, -1, action.text(), action.is_enabled(), action.is_visible(), action.is_checkable(), action.is_checkable() ? action.is_checked() : false, m_default, shortcut_text, icon);
|
||||
}
|
||||
|
||||
void MenuItem::set_menu_id(Badge<Menu>, unsigned int menu_id)
|
||||
|
|
|
@ -45,6 +45,9 @@ public:
|
|||
bool is_enabled() const { return m_enabled; }
|
||||
void set_enabled(bool);
|
||||
|
||||
bool is_visible() const { return m_visible; }
|
||||
void set_visible(bool);
|
||||
|
||||
bool is_default() const { return m_default; }
|
||||
void set_default(bool);
|
||||
|
||||
|
@ -61,6 +64,7 @@ private:
|
|||
int m_menu_id { -1 };
|
||||
unsigned m_identifier { 0 };
|
||||
bool m_enabled { true };
|
||||
bool m_visible { true };
|
||||
bool m_checkable { false };
|
||||
bool m_checked { false };
|
||||
bool m_default { false };
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue