actions: Add utility functions to produce action tags
This commit is contained in:
parent
b2ba21c75d
commit
816b73a95f
@ -4,6 +4,7 @@
|
||||
|
||||
#include "common.hpp"
|
||||
#include "components/types.hpp"
|
||||
#include "modules/meta/input_handler.hpp"
|
||||
|
||||
POLYBAR_NS
|
||||
|
||||
@ -48,7 +49,9 @@ class builder {
|
||||
void underline_close();
|
||||
void control(controltag tag);
|
||||
void cmd(mousebtn index, string action);
|
||||
void cmd(mousebtn btn, const modules::input_handler& handler, string action);
|
||||
void cmd(mousebtn index, string action, const label_t& label);
|
||||
void cmd(mousebtn btn, const modules::input_handler& handler, string action, const label_t& label);
|
||||
void cmd_close();
|
||||
|
||||
protected:
|
||||
|
@ -9,11 +9,11 @@ namespace modules {
|
||||
public:
|
||||
virtual ~input_handler() {}
|
||||
/**
|
||||
* Handle command
|
||||
* Handle action
|
||||
*
|
||||
* \returns true if the command is supported and false otherwise
|
||||
* \returns true if the action is supported and false otherwise
|
||||
*/
|
||||
virtual bool input(string&& cmd) = 0;
|
||||
virtual bool input(string&& action) = 0;
|
||||
|
||||
/**
|
||||
* The name of this input handler
|
||||
|
35
include/utils/actions.hpp
Normal file
35
include/utils/actions.hpp
Normal file
@ -0,0 +1,35 @@
|
||||
#pragma once
|
||||
|
||||
#include "common.hpp"
|
||||
#include "modules/meta/input_handler.hpp"
|
||||
|
||||
POLYBAR_NS
|
||||
|
||||
namespace actions_util {
|
||||
/**
|
||||
* Specifies how an action is routed
|
||||
*
|
||||
* A route consists of an input handler where the action should be delivered
|
||||
* and the action itself.
|
||||
*
|
||||
* TODO maybe remove if redundant at the end
|
||||
*/
|
||||
struct route {
|
||||
bool valid;
|
||||
string handler_name;
|
||||
string action;
|
||||
|
||||
explicit route();
|
||||
explicit route(string handler_name, string action);
|
||||
explicit route(const modules::input_handler& handler, string action);
|
||||
|
||||
/**
|
||||
* Constructs the full action string for this route
|
||||
*/
|
||||
string get_action_string() const;
|
||||
};
|
||||
|
||||
string get_action_string(const modules::input_handler& handler, string action);
|
||||
} // namespace actions_util
|
||||
|
||||
POLYBAR_NS_END
|
@ -6,6 +6,7 @@
|
||||
#include "utils/color.hpp"
|
||||
#include "utils/string.hpp"
|
||||
#include "utils/time.hpp"
|
||||
#include "utils/actions.hpp"
|
||||
POLYBAR_NS
|
||||
|
||||
builder::builder(const bar_settings& bar) : m_bar(bar) {
|
||||
@ -436,6 +437,13 @@ void builder::cmd(mousebtn index, string action) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Open an action tag for the given input handler and action
|
||||
*/
|
||||
void builder::cmd(mousebtn btn, const modules::input_handler& handler, string action) {
|
||||
cmd(btn, actions_util::get_action_string(handler, action));
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrap label in command block
|
||||
*/
|
||||
@ -447,6 +455,14 @@ void builder::cmd(mousebtn index, string action, const label_t& label) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Wrap label in module action
|
||||
*/
|
||||
void builder::cmd(mousebtn btn, const modules::input_handler& handler, string action, const label_t& label) {
|
||||
cmd(btn, actions_util::get_action_string(handler, action), label);
|
||||
}
|
||||
|
||||
/**
|
||||
* Close command tag
|
||||
*/
|
||||
|
26
src/utils/actions.cpp
Normal file
26
src/utils/actions.cpp
Normal file
@ -0,0 +1,26 @@
|
||||
#include "utils/actions.hpp"
|
||||
|
||||
#include "common.hpp"
|
||||
|
||||
POLYBAR_NS
|
||||
|
||||
namespace actions_util {
|
||||
route::route() : valid(false), handler_name(""), action("") {}
|
||||
route::route(string handler_name, string action) : valid(true), handler_name(handler_name), action(action) {}
|
||||
route::route(const modules::input_handler& handler, string action)
|
||||
: valid(true), handler_name(handler.input_handler_name()), action(action) {}
|
||||
|
||||
string route::get_action_string() const {
|
||||
if (!this->valid) {
|
||||
return "";
|
||||
}
|
||||
|
||||
return "#" + this->handler_name + "#" + this->action;
|
||||
}
|
||||
|
||||
string get_action_string(const modules::input_handler& handler, string action) {
|
||||
return route(handler, action).get_action_string();
|
||||
}
|
||||
} // namespace actions_util
|
||||
|
||||
POLYBAR_NS_END
|
Loading…
Reference in New Issue
Block a user