Use focused_title_change_event to update titles

This fixes issue mihirlad55/polybar-dwm-module#4 where the title would not
update if the title of the currently focused window changed. The
focused_title_change_event is raised exactly when the focused window's
title changes, so this should automatically update the title using the callback
when necessary.
This commit is contained in:
Mihir Lad 2020-07-18 01:46:07 -04:00
parent 136fc3efed
commit 8e5b5258e2
2 changed files with 12 additions and 2 deletions

View File

@ -67,6 +67,7 @@ namespace modules {
shared_ptr<std::vector<dwmipc::Monitor>> m_monitors;
unsigned int m_active_mon_num = 0;
unsigned int m_bar_mon = 0;
unsigned int m_focused_client_id = 0;
std::unordered_map<state_t, label_t> m_state_labels;
vector<tag_t> m_tags;

View File

@ -111,19 +111,28 @@ namespace modules {
return;
}
m_focused_client_id = ev.new_win_id;
m_title_label->reset_tokens();
if (ev.new_win_id != 0) {
auto focused_client = m_ipc->get_client(ev.new_win_id);
if (m_focused_client_id != 0) {
auto focused_client = m_ipc->get_client(m_focused_client_id);
m_title_label->replace_token("%title%", focused_client->name);
} else {
m_title_label->replace_token("%title%", "");
}
};
m_ipc->on_focused_title_change = [this](const dwmipc::FocusedTitleChangeEvent& ev) {
if (ev.monitor_num == m_bar_mon && ev.client_window_id == m_focused_client_id) {
m_title_label->reset_tokens();
m_title_label->replace_token("%title%", ev.new_name);
}
};
m_ipc->subscribe(dwmipc::Event::LAYOUT_CHANGE);
m_ipc->subscribe(dwmipc::Event::CLIENT_FOCUS_CHANGE);
m_ipc->subscribe(dwmipc::Event::TAG_CHANGE);
m_ipc->subscribe(dwmipc::Event::MONITOR_FOCUS_CHANGE);
m_ipc->subscribe(dwmipc::Event::FOCUSED_TITLE_CHANGE);
} catch (const dwmipc::IPCError& err) {
throw module_error(err.what());
}