From e0688307f32a9a5fcd7fb16a9c4f4d5e02c0b6d3 Mon Sep 17 00:00:00 2001 From: Michael Carlberg Date: Sun, 1 Jan 2017 21:28:28 +0100 Subject: [PATCH] wip(systray): Add module base --- include/modules/meta/factory.hpp | 7 ++++ include/modules/systray.hpp | 40 ++++++++++++++++++ include/x11/tray_manager.hpp | 2 + src/modules/systray.cpp | 71 ++++++++++++++++++++++++++++++++ 4 files changed, 120 insertions(+) create mode 100644 include/modules/systray.hpp create mode 100644 src/modules/systray.cpp diff --git a/include/modules/meta/factory.hpp b/include/modules/meta/factory.hpp index f9a06425..d412b9fe 100644 --- a/include/modules/meta/factory.hpp +++ b/include/modules/meta/factory.hpp @@ -15,6 +15,9 @@ #include "modules/menu.hpp" #include "modules/meta/base.hpp" #include "modules/script.hpp" +#if DEBUG +#include "modules/systray.hpp" +#endif #include "modules/tailscript.hpp" #include "modules/temperature.hpp" #include "modules/text.hpp" @@ -75,6 +78,10 @@ namespace { return new volume_module(bar, move(module_name)); } else if (name == "internal/network") { return new network_module(bar, move(module_name)); +#if DEBUG + } else if (name == "internal/systray") { + return new systray_module(bar, move(module_name)); +#endif } else if (name == "internal/temperature") { return new temperature_module(bar, move(module_name)); } else if (name == "internal/xbacklight") { diff --git a/include/modules/systray.hpp b/include/modules/systray.hpp new file mode 100644 index 00000000..b7e0cf3f --- /dev/null +++ b/include/modules/systray.hpp @@ -0,0 +1,40 @@ +#if DEBUG +#pragma once + +#include "modules/meta/static_module.hpp" +#include "modules/meta/input_handler.hpp" + +POLYBAR_NS + +class connection; + +namespace modules { + /** + * Module used to display information about the + * currently active X window. + */ + class systray_module : public static_module, public input_handler { + public: + explicit systray_module(const bar_settings&, string); + + void update(); + bool build(builder* builder, const string& tag) const; + + protected: + bool input(string&& cmd); + + private: + static constexpr const char* EVENT_TOGGLE{"systray-toggle"}; + + static constexpr const char* TAG_LABEL_TOGGLE{""}; + static constexpr const char* TAG_TRAY_CLIENTS{""}; + + connection& m_connection; + label_t m_label; + + bool m_hidden{false}; + }; +} + +POLYBAR_NS_END +#endif diff --git a/include/x11/tray_manager.hpp b/include/x11/tray_manager.hpp index d8b2c62e..e7a53220 100644 --- a/include/x11/tray_manager.hpp +++ b/include/x11/tray_manager.hpp @@ -25,6 +25,8 @@ #define TRAY_WM_NAME "Polybar tray window" #define TRAY_WM_CLASS "tray\0Polybar" +#define TRAY_PLACEHOLDER "" + POLYBAR_NS namespace chrono = std::chrono; diff --git a/src/modules/systray.cpp b/src/modules/systray.cpp new file mode 100644 index 00000000..1bbfc082 --- /dev/null +++ b/src/modules/systray.cpp @@ -0,0 +1,71 @@ +#if DEBUG +#include "modules/systray.hpp" +#include "drawtypes/label.hpp" +#include "x11/connection.hpp" +#include "x11/tray_manager.hpp" + +#include "modules/meta/base.inl" + +POLYBAR_NS + +namespace modules { + template class module; + + /** + * Construct module + */ + systray_module::systray_module(const bar_settings& bar, string name_) + : static_module(bar, move(name_)), m_connection(connection::make()) { + // Add formats and elements + m_formatter->add(DEFAULT_FORMAT, TAG_LABEL_TOGGLE, {TAG_LABEL_TOGGLE, TAG_TRAY_CLIENTS}); + + if (m_formatter->has(TAG_LABEL_TOGGLE)) { + m_label = load_label(m_conf, name(), TAG_LABEL_TOGGLE); + } + } + + /** + * Update + */ + void systray_module::update() { + if (m_label) { + m_label->reset_tokens(); + m_label->replace_token("%title%", ""); + } + + broadcast(); + } + + /** + * Build output + */ + bool systray_module::build(builder* builder, const string& tag) const { + if (tag == TAG_LABEL_TOGGLE) { + builder->cmd(mousebtn::LEFT, EVENT_TOGGLE); + builder->node(m_label); + builder->cmd_close(); + } else if (tag == TAG_TRAY_CLIENTS && !m_hidden) { + builder->append(TRAY_PLACEHOLDER); + } else { + return false; + } + return true; + } + + /** + * Handle input event + */ + bool systray_module::input(string&& cmd) { + if (cmd.find(EVENT_TOGGLE) != 0) { + return false; + } + + m_hidden = !m_hidden; + broadcast(); + + return true; + } +} + +POLYBAR_NS_END +#endif