dwm: Use functions to update labels

To keep consistency, use functions to update all labels. Since the labels have
tokens that have to be replaced, if this needed to be changed, or if the label
updating code needed to be changed, it could easily be done in one place without
having to scour the code for all places where the label is updated. It improves
refactorability.

This decouples the token replacement of labels from other functions and keeps
that code contained to the update label functions.

Since functions like update_title_label and update_floating_label already exist,
it makes sense to stick with this pattern of using functions to update the
labels. For updating the labels in different ways, overloaded functions can
simply be used.
This commit is contained in:
Mihir Lad 2020-07-23 16:43:46 -04:00
parent 4533cf5003
commit b6c2dc0dee
2 changed files with 31 additions and 7 deletions

View File

@ -166,6 +166,13 @@ namespace modules {
*/ */
void update_tag_labels(); void update_tag_labels();
/**
* Update the title label with the specified title
*
* @param title The title to use to update the label
*/
void update_title_label(const string& title);
/** /**
* Get the window title of the currently focused client from dwm and update * Get the window title of the currently focused client from dwm and update
* the title label * the title label
@ -178,6 +185,13 @@ namespace modules {
*/ */
void update_floating_label(); void update_floating_label();
/**
* Update the layout label with the specified symbol
*
* @param symbol The symbol to use to update the label
*/
void update_layout_label(const string& symbol);
/** /**
* Translate the tag's tag states to a state_t enum value * Translate the tag's tag states to a state_t enum value
* *

View File

@ -86,16 +86,19 @@ namespace modules {
if (m_layout_label) { if (m_layout_label) {
auto layouts = m_ipc->get_layouts(); auto layouts = m_ipc->get_layouts();
m_layouts = m_ipc->get_layouts(); m_layouts = m_ipc->get_layouts();
// First layout is treated as default by dwm // First layout is treated as default by dwm
m_default_layout = &m_layouts->at(0); m_default_layout = &m_layouts->at(0);
m_current_layout = find_layout(m_bar_mon->layout.address.cur); m_current_layout = find_layout(m_bar_mon->layout.address.cur);
m_secondary_layout = find_layout(m_secondary_layout_symbol); m_secondary_layout = find_layout(m_secondary_layout_symbol);
if (m_secondary_layout == nullptr) { if (m_secondary_layout == nullptr) {
throw module_error("Secondary layout symbol does not exist"); throw module_error("Secondary layout symbol does not exist");
} }
// Initialize layout symbol // Initialize layout symbol
m_layout_label->replace_token("%symbol%", m_bar_mon->layout.symbol.cur); update_layout_label(m_bar_mon->layout.symbol.cur);
// This event is only needed to update the layout label // This event is only needed to update the layout label
m_ipc->on_layout_change = [this](const dwmipc::LayoutChangeEvent& ev) { on_layout_change(ev); }; m_ipc->on_layout_change = [this](const dwmipc::LayoutChangeEvent& ev) { on_layout_change(ev); };
m_ipc->subscribe(dwmipc::Event::LAYOUT_CHANGE); m_ipc->subscribe(dwmipc::Event::LAYOUT_CHANGE);
@ -359,6 +362,11 @@ namespace modules {
} }
} }
void dwm_module::update_title_label(const string& title) {
m_title_label->reset_tokens();
m_title_label->replace_token("%title%", title);
}
void dwm_module::update_title_label() { void dwm_module::update_title_label() {
std::string new_title; std::string new_title;
if (m_focused_client_id != 0) { if (m_focused_client_id != 0) {
@ -371,8 +379,7 @@ namespace modules {
throw module_error(err.what()); throw module_error(err.what());
} }
} }
m_title_label->reset_tokens(); update_title_label(new_title);
m_title_label->replace_token("%title%", new_title);
} }
void dwm_module::update_floating_label() { void dwm_module::update_floating_label() {
@ -390,6 +397,11 @@ namespace modules {
} }
} }
void dwm_module::update_layout_label(const string& symbol) {
m_layout_label->reset_tokens();
m_layout_label->replace_token("%symbol%", symbol);
}
auto dwm_module::reconnect_dwm() -> bool { auto dwm_module::reconnect_dwm() -> bool {
try { try {
if (!m_ipc->is_main_socket_connected()) { if (!m_ipc->is_main_socket_connected()) {
@ -412,8 +424,7 @@ namespace modules {
void dwm_module::on_layout_change(const dwmipc::LayoutChangeEvent& ev) { void dwm_module::on_layout_change(const dwmipc::LayoutChangeEvent& ev) {
if (ev.monitor_num == m_bar_mon->num) { if (ev.monitor_num == m_bar_mon->num) {
m_current_layout = find_layout(ev.new_address); m_current_layout = find_layout(ev.new_address);
m_layout_label->reset_tokens(); update_layout_label(ev.new_symbol);
m_layout_label->replace_token("%symbol%", ev.new_symbol);
} }
} }
@ -433,8 +444,7 @@ namespace modules {
void dwm_module::on_focused_title_change(const dwmipc::FocusedTitleChangeEvent& ev) { void dwm_module::on_focused_title_change(const dwmipc::FocusedTitleChangeEvent& ev) {
if (ev.monitor_num == m_bar_mon->num && ev.client_window_id == m_focused_client_id) { if (ev.monitor_num == m_bar_mon->num && ev.client_window_id == m_focused_client_id) {
m_title_label->reset_tokens(); update_title_label(ev.new_name);
m_title_label->replace_token("%title%", ev.new_name);
} }
} }