refactor(menu): Menu module logic

This commit is contained in:
Michael Carlberg 2016-10-11 15:28:14 +02:00
parent fb6e5ea378
commit e5d981faf9
2 changed files with 59 additions and 55 deletions

View File

@ -1034,10 +1034,14 @@ See [the bspwm module](#module-internalbspwm) for details on `label-dimmed`.
; Available tags: ; Available tags:
; <label-toggle> (default) - gets replaced with <label-(open|close)> ; <label-toggle> (default) - gets replaced with <label-(open|close)>
; <menu> (default) ; <menu> (default)
;f-ormat = <label-toggle> <menu> ;format = <label-toggle> <menu>
label-open = Apps label-open = Apps
label-close = x label-close = x
; Optional item separator
; Default: none
label-separator = |
~~~ ~~~

View File

@ -20,54 +20,44 @@ namespace modules {
using static_module::static_module; using static_module::static_module;
void setup() { void setup() {
auto default_format_string = string{TAG_LABEL_TOGGLE} + " " + string{TAG_MENU}; string default_format{TAG_LABEL_TOGGLE + string{" "} + TAG_MENU};
m_formatter->add(DEFAULT_FORMAT, default_format_string, {TAG_LABEL_TOGGLE, TAG_MENU}); m_formatter->add(DEFAULT_FORMAT, default_format, {TAG_LABEL_TOGGLE, TAG_MENU});
if (m_formatter->has(TAG_LABEL_TOGGLE)) { if (m_formatter->has(TAG_LABEL_TOGGLE)) {
m_labelopen = get_config_label(m_conf, name(), "label-open"); m_labelopen = get_config_label(m_conf, name(), "label-open");
m_labelclose = get_optional_config_label(m_conf, name(), "label-close", "x"); m_labelclose = get_optional_config_label(m_conf, name(), "label-close", "x");
} }
if (m_formatter->has(TAG_MENU)) { m_labelseparator = get_optional_config_label(m_conf, name(), "label-separator", "");
int level_n = 0;
if (!m_formatter->has(TAG_MENU))
return;
while (true) { while (true) {
auto level_path = "menu-" + to_string(level_n); string level_param{"menu-" + to_string(m_levels.size())};
if (m_conf.get<string>(name(), level_path + "-0", "") == "") if (m_conf.get<string>(name(), level_param + "-0", "").empty())
break; break;
m_log.trace("%s: Creating menu level %i", name(), m_levels.size());
m_levels.emplace_back(make_unique<menu_tree>()); m_levels.emplace_back(make_unique<menu_tree>());
int item_n = 0;
while (true) { while (true) {
auto item_path = level_path + "-" + to_string(item_n); string item_param{level_param + "-" + to_string(m_levels.back()->items.size())};
if (m_conf.get<string>(name(), item_path, "") == "") if (m_conf.get<string>(name(), item_param, "").empty())
break; break;
m_log.trace("%s: Creating menu level item %i", name(), m_levels.back()->items.size());
auto item = make_unique<menu_tree_item>(); auto item = make_unique<menu_tree_item>();
item->label = get_config_label(m_conf, name(), item_param);
item->label = get_config_label(m_conf, name(), item_path); item->exec = m_conf.get<string>(name(), item_param + "-exec", EVENT_MENU_CLOSE);
item->exec = m_conf.get<string>(name(), item_path + "-exec", EVENT_MENU_CLOSE);
m_levels.back()->items.emplace_back(std::move(item)); m_levels.back()->items.emplace_back(std::move(item));
item_n++;
}
level_n++;
} }
} }
} }
string get_output() {
m_builder->node(module::get_output());
return m_builder->flush();
}
bool build(builder* builder, string tag) { bool build(builder* builder, string tag) {
if (tag == TAG_LABEL_TOGGLE && m_level == -1) { if (tag == TAG_LABEL_TOGGLE && m_level == -1) {
builder->cmd(mousebtn::LEFT, string(EVENT_MENU_OPEN) + "0"); builder->cmd(mousebtn::LEFT, string(EVENT_MENU_OPEN) + "0");
@ -78,17 +68,13 @@ namespace modules {
builder->node(m_labelclose); builder->node(m_labelclose);
builder->cmd_close(true); builder->cmd_close(true);
} else if (tag == TAG_MENU && m_level > -1) { } else if (tag == TAG_MENU && m_level > -1) {
int i = 0; for (auto&& item : m_levels[m_level]->items) {
if (item != m_levels[m_level]->items.front())
for (auto&& m : m_levels[m_level]->items) {
if (i++ > 0)
builder->space(); builder->space();
builder->color_alpha("77"); if (*m_labelseparator)
builder->node("/"); builder->node(m_labelseparator, true);
builder->color_close(true); builder->cmd(mousebtn::LEFT, item->exec);
builder->space(); builder->node(item->label);
builder->cmd(mousebtn::LEFT, m->exec);
builder->node(m->label);
builder->cmd_close(true); builder->cmd_close(true);
} }
} else { } else {
@ -98,6 +84,23 @@ namespace modules {
} }
bool handle_event(string cmd) { bool handle_event(string cmd) {
if (cmd.compare(0, 4, "menu") != 0)
return false;
// broadcast update when leaving leaving the function
auto exit_handler = scope_util::make_exit_handler<>([this]() {
if (!m_threads.empty()) {
m_log.trace("%s: Cleaning up previous broadcast threads", name());
for (auto&& thread : m_threads)
if (thread.joinable())
thread.join();
m_threads.clear();
}
m_log.trace("%s: Dispatching broadcast thread", name());
m_threads.emplace_back(thread(&menu_module::broadcast, this));
});
if (cmd.compare(0, strlen(EVENT_MENU_OPEN), EVENT_MENU_OPEN) == 0) { if (cmd.compare(0, strlen(EVENT_MENU_OPEN), EVENT_MENU_OPEN) == 0) {
auto level = cmd.substr(strlen(EVENT_MENU_OPEN)); auto level = cmd.substr(strlen(EVENT_MENU_OPEN));
@ -105,22 +108,17 @@ namespace modules {
level = "0"; level = "0";
m_level = std::atoi(level.c_str()); m_level = std::atoi(level.c_str());
m_log.info("%s: Opening menu level '%i'", name(), static_cast<int>(m_level));
if (m_level >= (int)m_levels.size()) { if (static_cast<size_t>(m_level) >= m_levels.size()) {
m_log.err("%s: Cannot open unexisting menu level '%d'", name(), level); m_log.warn("%s: Cannot open unexisting menu level '%i'", name(), level);
m_level = -1; m_level = -1;
} }
} else if (cmd == EVENT_MENU_CLOSE) { } else if (cmd == EVENT_MENU_CLOSE) {
m_log.info("%s: Closing menu tree", name());
m_level = -1; m_level = -1;
} else {
m_level = -1;
broadcast();
return false;
} }
broadcast();
return true; return true;
} }
@ -135,11 +133,13 @@ namespace modules {
static constexpr auto EVENT_MENU_OPEN = "menu_open-"; static constexpr auto EVENT_MENU_OPEN = "menu_open-";
static constexpr auto EVENT_MENU_CLOSE = "menu_close"; static constexpr auto EVENT_MENU_CLOSE = "menu_close";
int m_level = -1;
vector<unique_ptr<menu_tree>> m_levels;
label_t m_labelopen; label_t m_labelopen;
label_t m_labelclose; label_t m_labelclose;
label_t m_labelseparator;
vector<unique_ptr<menu_tree>> m_levels;
std::atomic<int> m_level{-1};
}; };
} }