1
0
Fork 0
mirror of https://github.com/polybar/polybar.git synced 2024-10-27 05:23:39 -04:00
polybar/include/modules/menu.hpp
Patrick Ziegler 26be83f893
module: Implement action router (#2336)
* module: Implement proof of concept action router

Action implementation inside module becomes much cleaner because each
module just registers action names together with a callback (pointer to
member function) and the action router does the rest.

* Make input function final

This forces all modules to use the action router

* modules: Catch exceptions in action handlers

* Use action router for all modules

* Use action_ prefix for function names

The mpd module's 'stop' action overwrote the base module's stop function
which caused difficult to debug behavior.

To prevent this in the future we now prefix each function that is
responsible for an action with 'action_'

* Cleanup

* actions: Throw exception when re-registering action

Action names are unique inside modules. Unfortunately there is no way to
ensure this statically, the next best thing is to crash the module and
let the user know that this is a bug.

* Formatting

* actions: Ignore data for actions without data

This is the same behavior as before.

* action_router: Write tests
2021-01-04 10:25:52 +01:00

52 lines
1.1 KiB
C++

#pragma once
#include "modules/meta/static_module.hpp"
POLYBAR_NS
namespace modules {
class menu_module : public static_module<menu_module> {
public:
struct menu_tree_item {
string exec;
label_t label;
};
struct menu_tree {
vector<unique_ptr<menu_tree_item>> items;
};
public:
explicit menu_module(const bar_settings&, string);
bool build(builder* builder, const string& tag) const;
void update() {}
static constexpr auto TYPE = "custom/menu";
static constexpr auto EVENT_OPEN = "open";
static constexpr auto EVENT_CLOSE = "close";
static constexpr auto EVENT_EXEC = "exec";
protected:
void action_open(const string& data);
void action_close();
void action_exec(const string& item);
private:
static constexpr auto TAG_LABEL_TOGGLE = "<label-toggle>";
static constexpr auto TAG_MENU = "<menu>";
bool m_expand_right{true};
label_t m_labelopen;
label_t m_labelclose;
label_t m_labelseparator;
vector<unique_ptr<menu_tree>> m_levels;
std::atomic<int> m_level{-1};
};
} // namespace modules
POLYBAR_NS_END