From b6c2dc0dee92c98d76de7fbadc6cadfda0a64625 Mon Sep 17 00:00:00 2001 From: Mihir Lad Date: Thu, 23 Jul 2020 16:43:46 -0400 Subject: [PATCH] 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. --- include/modules/dwm.hpp | 14 ++++++++++++++ src/modules/dwm.cpp | 24 +++++++++++++++++------- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/include/modules/dwm.hpp b/include/modules/dwm.hpp index ac44bb4b..ab7ec38d 100644 --- a/include/modules/dwm.hpp +++ b/include/modules/dwm.hpp @@ -166,6 +166,13 @@ namespace modules { */ 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 * the title label @@ -178,6 +185,13 @@ namespace modules { */ 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 * diff --git a/src/modules/dwm.cpp b/src/modules/dwm.cpp index 2e23f575..f303a190 100644 --- a/src/modules/dwm.cpp +++ b/src/modules/dwm.cpp @@ -86,16 +86,19 @@ namespace modules { if (m_layout_label) { auto layouts = m_ipc->get_layouts(); m_layouts = m_ipc->get_layouts(); + // First layout is treated as default by dwm m_default_layout = &m_layouts->at(0); m_current_layout = find_layout(m_bar_mon->layout.address.cur); m_secondary_layout = find_layout(m_secondary_layout_symbol); + if (m_secondary_layout == nullptr) { throw module_error("Secondary layout symbol does not exist"); } // 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 m_ipc->on_layout_change = [this](const dwmipc::LayoutChangeEvent& ev) { on_layout_change(ev); }; 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() { std::string new_title; if (m_focused_client_id != 0) { @@ -371,8 +379,7 @@ namespace modules { throw module_error(err.what()); } } - m_title_label->reset_tokens(); - m_title_label->replace_token("%title%", new_title); + update_title_label(new_title); } 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 { try { if (!m_ipc->is_main_socket_connected()) { @@ -412,8 +424,7 @@ namespace modules { void dwm_module::on_layout_change(const dwmipc::LayoutChangeEvent& ev) { if (ev.monitor_num == m_bar_mon->num) { m_current_layout = find_layout(ev.new_address); - m_layout_label->reset_tokens(); - m_layout_label->replace_token("%symbol%", ev.new_symbol); + update_layout_label(ev.new_symbol); } } @@ -433,8 +444,7 @@ namespace modules { 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) { - m_title_label->reset_tokens(); - m_title_label->replace_token("%title%", ev.new_name); + update_title_label(ev.new_name); } }