From 816b73a95faf00d28ac2d0385c67d9bd54416429 Mon Sep 17 00:00:00 2001
From: patrick96
Date: Thu, 7 May 2020 01:26:14 +0200
Subject: [PATCH] actions: Add utility functions to produce action tags
---
include/components/builder.hpp | 3 +++
include/modules/meta/input_handler.hpp | 6 ++---
include/utils/actions.hpp | 35 ++++++++++++++++++++++++++
src/components/builder.cpp | 16 ++++++++++++
src/utils/actions.cpp | 26 +++++++++++++++++++
5 files changed, 83 insertions(+), 3 deletions(-)
create mode 100644 include/utils/actions.hpp
create mode 100644 src/utils/actions.cpp
diff --git a/include/components/builder.hpp b/include/components/builder.hpp
index aaad688a..c8e62447 100644
--- a/include/components/builder.hpp
+++ b/include/components/builder.hpp
@@ -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:
diff --git a/include/modules/meta/input_handler.hpp b/include/modules/meta/input_handler.hpp
index 0d740322..6ce01524 100644
--- a/include/modules/meta/input_handler.hpp
+++ b/include/modules/meta/input_handler.hpp
@@ -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
diff --git a/include/utils/actions.hpp b/include/utils/actions.hpp
new file mode 100644
index 00000000..f8530dd6
--- /dev/null
+++ b/include/utils/actions.hpp
@@ -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
diff --git a/src/components/builder.cpp b/src/components/builder.cpp
index cb34970f..0c994f75 100644
--- a/src/components/builder.cpp
+++ b/src/components/builder.cpp
@@ -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
*/
diff --git a/src/utils/actions.cpp b/src/utils/actions.cpp
new file mode 100644
index 00000000..7e7e9273
--- /dev/null
+++ b/src/utils/actions.cpp
@@ -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