From 06932007a05c4de81f8c1eac6f6130b9ec548168 Mon Sep 17 00:00:00 2001 From: Nolan Prochnau Date: Wed, 7 Jul 2021 15:43:49 -0400 Subject: [PATCH] Universal Visibility Action (#2320 cont'd) (#2426) * Add toggle_visible action * Add set_visible and set_invisible actions * Rename toggle_visible method to match `action_toggle_visible` -> `action_toggle_visibility` Matches with `EVENT_TOGGLE_VISIBILITY` * Update CHANGELOG * Revert #2320 IPC commands IPC commands are no longer necessary now that the actions are implemented. Changed some method permissions as well to reflect this. * Add logging and change action names - `module_toggle` - `module_show` - `module_hide` Delineate common actions to all modules with a `module_` prefix (for future actions too) * Update documentation --- CHANGELOG.md | 6 +++--- doc/user/actions.rst | 13 ++++++++++++ include/components/controller.hpp | 2 -- include/modules/meta/base.hpp | 12 +++++++++-- include/modules/meta/base.inl | 35 ++++++++++++++++++++++++------- include/modules/unsupported.hpp | 1 - src/components/controller.cpp | 30 -------------------------- 7 files changed, 53 insertions(+), 46 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2b701e22..7740fb6f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -76,12 +76,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ([`#316`](https://github.com/polybar/polybar/issues/316)) - Added .ini extension check to the default config search. ([`#2323`](https://github.com/polybar/polybar/issues/2323)) -- IPC commands to change visibility of modules - (`hide.`, `show.`, and `toggle.`) - ([`#2108`](https://github.com/polybar/polybar/issues/2108)) - Config option to hide a certain module (`hidden = false`) ([`#2108`](https://github.com/polybar/polybar/issues/2108)) +- Actions to control visibility of modules + (`module_toggle`, `module_show`, and `module_hide`) + ([`#2108`](https://github.com/polybar/polybar/issues/2108)) - `internal/xworkspaces`: Make the urgent hint persistent ([`#1081`](https://github.com/polybar/polybar/issues/1081)) - `internal/network`: `interface-type` may be used in place of `interface` to diff --git a/doc/user/actions.rst b/doc/user/actions.rst index cb3581de..69dd8fd2 100644 --- a/doc/user/actions.rst +++ b/doc/user/actions.rst @@ -121,6 +121,19 @@ Available Actions The following modules have actions available. Most of them are already used by the module by default for click and scroll events. +All Modules +^^^^^^^^^^^ + +These actions are available to all modules and are prefixed with ``module_``. + +:``module_show``, ``module_hide``: + Shows/Hides a module. The module is still running in the background when + hidden, it is just not drawn. The starting state can be configured with the + `hidden` configuration option. + +:``module_toggle``: + Toggles the visibility of a module. + internal/date ^^^^^^^^^^^^^ diff --git a/include/components/controller.hpp b/include/components/controller.hpp index 2d5cf28b..a11eea02 100644 --- a/include/components/controller.hpp +++ b/include/components/controller.hpp @@ -76,8 +76,6 @@ class controller bool forward_action(const actions_util::action& cmd); bool try_forward_legacy_action(const string& cmd); - void switch_module_visibility(string module_name_raw, int visible); - connection& m_connection; signal_emitter& m_sig; const logger& m_log; diff --git a/include/modules/meta/base.hpp b/include/modules/meta/base.hpp index 984e1610..84722aa5 100644 --- a/include/modules/meta/base.hpp +++ b/include/modules/meta/base.hpp @@ -118,7 +118,6 @@ namespace modules { virtual string name() const = 0; virtual bool running() const = 0; virtual bool visible() const = 0; - virtual void set_visible(bool value) = 0; /** * Handle action, possibly with data attached @@ -145,6 +144,10 @@ namespace modules { module(const bar_settings bar, string name); ~module() noexcept; + static constexpr auto EVENT_MODULE_TOGGLE = "module_toggle"; + static constexpr auto EVENT_MODULE_SHOW = "module_show"; + static constexpr auto EVENT_MODULE_HIDE = "module_hide"; + string type() const override; string name_raw() const override; @@ -152,7 +155,6 @@ namespace modules { bool running() const override; bool visible() const override; - void set_visible(bool value) override; void stop() override; void halt(string error_message) override; @@ -171,6 +173,12 @@ namespace modules { string get_format() const; string get_output(); + void set_visible(bool value); + + void action_module_toggle(); + void action_module_show(); + void action_module_hide(); + protected: signal_emitter& m_sig; const bar_settings m_bar; diff --git a/include/modules/meta/base.inl b/include/modules/meta/base.inl index cd5d8724..c7742744 100644 --- a/include/modules/meta/base.inl +++ b/include/modules/meta/base.inl @@ -26,7 +26,11 @@ namespace modules { , m_builder(make_unique(bar)) , m_formatter(make_unique(m_conf, m_name)) , m_handle_events(m_conf.get(m_name, "handle-events", true)) - , m_visible(!m_conf.get(m_name, "hidden", false)) {} + , m_visible(!m_conf.get(m_name, "hidden", false)) { + m_router->register_action(EVENT_MODULE_TOGGLE, &module::action_module_toggle); + m_router->register_action(EVENT_MODULE_SHOW, &module::action_module_show); + m_router->register_action(EVENT_MODULE_HIDE, &module::action_module_hide); + } template module::~module() noexcept { @@ -67,13 +71,6 @@ namespace modules { return static_cast(m_visible); } - template - void module::set_visible(bool value) { - m_log.info("%s: Visibility changed (state=%s)", m_name, value ? "shown" : "hidden"); - m_visible = value; - broadcast(); - } - template void module::stop() { if (!static_cast(m_enabled)) { @@ -231,6 +228,28 @@ namespace modules { return format->decorate(&*m_builder, m_builder->flush()); } + template + void module::set_visible(bool value) { + m_log.notice("%s: Visibility changed (state=%s)", m_name, value ? "shown" : "hidden"); + m_visible = value; + broadcast(); + } + + template + void module::action_module_toggle() { + set_visible(!m_visible); + } + + template + void module::action_module_show() { + set_visible(true); + } + + template + void module::action_module_hide() { + set_visible(false); + } + // }}} } // namespace modules diff --git a/include/modules/unsupported.hpp b/include/modules/unsupported.hpp index c54b57a4..dda2027e 100644 --- a/include/modules/unsupported.hpp +++ b/include/modules/unsupported.hpp @@ -30,7 +30,6 @@ namespace modules { bool visible() const override { \ return false; \ } \ - void set_visible(bool) override {} \ void start() override {} \ void stop() override {} \ void halt(string) override {} \ diff --git a/src/components/controller.cpp b/src/components/controller.cpp index 0a7b803f..8d11e331 100644 --- a/src/components/controller.cpp +++ b/src/components/controller.cpp @@ -528,26 +528,6 @@ bool controller::forward_action(const actions_util::action& action_triple) { return true; } -void controller::switch_module_visibility(string module_name_raw, int visible) { - for (auto&& mod : m_modules) { - if (mod->name_raw() != module_name_raw) - continue; - - if (visible == 0) { - mod->set_visible(false); - } else if (visible == 1) { - mod->set_visible(true); - } else if (visible == 2) { - mod->set_visible(!mod->visible()); - } - - return; - } - - m_log.err("controller: Module '%s' not found for visibility change (state=%s)", module_name_raw, - visible ? "shown" : "hidden"); -} - /** * Process stored input data */ @@ -839,10 +819,6 @@ bool controller::on(const signals::ipc::command& evt) { return false; } - string hide_module{"hide."}; - string show_module{"show."}; - string toggle_module{"toggle."}; - if (command == "quit") { enqueue(make_quit_evt(false)); } else if (command == "restart") { @@ -853,12 +829,6 @@ bool controller::on(const signals::ipc::command& evt) { m_bar->show(); } else if (command == "toggle") { m_bar->toggle(); - } else if (command.find(hide_module) == 0) { - switch_module_visibility(command.substr(hide_module.length()), 0); - } else if (command.find(show_module) == 0) { - switch_module_visibility(command.substr(show_module.length()), 1); - } else if (command.find(toggle_module) == 0) { - switch_module_visibility(command.substr(toggle_module.length()), 2); } else { m_log.warn("\"%s\" is not a valid ipc command", command); }