From 8c1ba8358ddfe9a967fdcfe8bf9bef902b2ee8ac Mon Sep 17 00:00:00 2001 From: Patrick Ziegler Date: Tue, 29 Aug 2017 22:25:41 +0200 Subject: [PATCH] feat(menu): Add expand-right option (#658) expand-right defaults to true to preserve the current functionality If set to false, the items in the menu will be added to the left of the toggle label (instead of the right side) Should resolve the issue discussed in #655 --- doc/config.cmake | 2 ++ include/modules/menu.hpp | 2 ++ src/modules/menu.cpp | 40 +++++++++++++++++++++++++++++++++++----- 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/doc/config.cmake b/doc/config.cmake index b5aaa9b5..37930853 100644 --- a/doc/config.cmake +++ b/doc/config.cmake @@ -331,6 +331,8 @@ ramp-foreground = ${colors.foreground-alt} [module/powermenu] type = custom/menu +expand-right = true + format-spacing = 1 label-open =  diff --git a/include/modules/menu.hpp b/include/modules/menu.hpp index 7f62a3a6..a0e1fa3d 100644 --- a/include/modules/menu.hpp +++ b/include/modules/menu.hpp @@ -33,6 +33,8 @@ namespace modules { static constexpr auto EVENT_MENU_OPEN = "menu-open-"; static constexpr auto EVENT_MENU_CLOSE = "menu-close"; + bool m_expand_right{true}; + label_t m_labelopen; label_t m_labelclose; label_t m_labelseparator; diff --git a/src/modules/menu.cpp b/src/modules/menu.cpp index 5935ae5c..f43f4d8e 100644 --- a/src/modules/menu.cpp +++ b/src/modules/menu.cpp @@ -12,9 +12,17 @@ namespace modules { template class module; menu_module::menu_module(const bar_settings& bar, string name_) : static_module(bar, move(name_)) { + m_expand_right = m_conf.get(name(), "expand-right", m_expand_right); + string default_format; - default_format += TAG_LABEL_TOGGLE; - default_format += TAG_MENU; + if(m_expand_right) { + default_format += TAG_LABEL_TOGGLE; + default_format += TAG_MENU; + } else { + default_format += TAG_MENU; + default_format += TAG_LABEL_TOGGLE; + } + m_formatter->add(DEFAULT_FORMAT, default_format, {TAG_LABEL_TOGGLE, TAG_MENU}); @@ -67,6 +75,20 @@ namespace modules { } else if (tag == TAG_MENU && m_level > -1) { auto spacing = m_formatter->get(get_format())->spacing; for (auto&& item : m_levels[m_level]->items) { + /* + * Depending on whether the menu items are to the left or right of the toggle label, the items need to be + * drawn before or after the spacings and the separator + * + * If the menu expands to the left, the separator should be drawn on the right side because otherwise the menu + * would look like this: + * | x | y + */ + if(!m_expand_right) { + builder->cmd(mousebtn::LEFT, item->exec); + builder->node(item->label); + builder->cmd_close(); + } + if (*m_labelseparator) { if (item != m_levels[m_level]->items[0]) { builder->space(spacing); @@ -74,9 +96,17 @@ namespace modules { builder->node(m_labelseparator); builder->space(spacing); } - builder->cmd(mousebtn::LEFT, item->exec); - builder->node(item->label); - builder->cmd_close(); + + /* + * If the menu expands to the right, the separator should be drawn on the left side because otherwise the menu + * would look like this: + * x | y | + */ + if(m_expand_right) { + builder->cmd(mousebtn::LEFT, item->exec); + builder->node(item->label); + builder->cmd_close(); + } } } else { return false;