feat: Module Visibility (#2320)

Modules can now also be shown and hidden using ipc commands:

    $ polybar-msg [-p PID] cmd hide.mymodule # Hides module mymodule
    $ polybar-msg [-p PID] cmd show.mymodule # Shows module mymodule
    $ polybar-msg [-p PID] cmd toggle.mymodule # Toggles visibility of mymodule

* Hopefully implement visibility checking

* Implement hide command

* Implement `show` and `toggle` commands

* Refactor and add some logging

* Run style checks and update CHANGELOG

* Get around unused parameter warnings

* Change `set_visible` to return nothing

* Make errors more informative

Co-authored-by: Patrick Ziegler <p.ziegler96@gmail.com>

* Update bar when changing module visibility

- Called in the module to maintain dependence on the signal emitter
- Update CHANGELOG to make changes more verbose

* wrong var

* Update include/modules/unsupported.hpp

Co-authored-by: Patrick Ziegler <p.ziegler96@gmail.com>

Co-authored-by: Patrick Ziegler <p.ziegler96@gmail.com>
This commit is contained in:
Nolan Prochnau 2020-12-27 10:05:26 -05:00 committed by GitHub
parent b49f325e1e
commit 8e46e54cb3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 60 additions and 1 deletions

View File

@ -59,6 +59,9 @@ 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.<name>`, `show.<name>`, and `toggle.<name>`)
([`#2108`](https://github.com/polybar/polybar/issues/2108))
### Changed
- Slight changes to the value ranges the different ramp levels are responsible

View File

@ -77,6 +77,8 @@ tray-padding = 2
cursor-click = pointer
cursor-scroll = ns-resize
enable-ipc = true
[module/xwindow]
type = internal/xwindow
label = %title:0:30:...%

View File

@ -76,6 +76,8 @@ 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;

View File

@ -114,6 +114,8 @@ namespace modules {
virtual string name_raw() const = 0;
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 +147,10 @@ namespace modules {
string name_raw() const;
string name() const;
bool running() const;
bool visible() const;
void set_visible(bool value);
void stop();
void halt(string error_message);
void teardown();
@ -184,6 +190,7 @@ namespace modules {
private:
atomic<bool> m_enabled{true};
atomic<bool> m_visible{true};
atomic<bool> m_changed{true};
string m_cache;
};

View File

@ -56,6 +56,18 @@ namespace modules {
return static_cast<bool>(m_enabled);
}
template <typename Impl>
bool module<Impl>::visible() const {
return static_cast<bool>(m_visible);
}
template <typename Impl>
void module<Impl>::set_visible(bool value) {
m_log.info("%s: Visibility changed (state=%s)", m_name, value ? "shown" : "hidden");
m_visible = value;
broadcast();
}
template <typename Impl>
void module<Impl>::stop() {
if (!static_cast<bool>(m_enabled)) {

View File

@ -27,6 +27,10 @@ namespace modules {
bool running() const { \
return false; \
} \
bool visible() const { \
return false; \
} \
void set_visible(bool) {} \
void start() {} \
void stop() {} \
void halt(string) {} \

View File

@ -530,6 +530,25 @@ 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
*/
@ -600,7 +619,7 @@ bool controller::process_update(bool force) {
}
for (const auto& module : block.second) {
if (!module->running()) {
if (!module->running() || !module->visible()) {
continue;
}
@ -833,6 +852,10 @@ 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") {
@ -843,6 +866,12 @@ 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);
}