From a042eea38404294cdd40c180fd8023401fb63e81 Mon Sep 17 00:00:00 2001 From: Robinhuett <5955614+Robinhuett@users.noreply.github.com> Date: Tue, 30 Oct 2018 13:39:30 +0100 Subject: [PATCH 1/2] Add module to show sway binding mode --- include/factory.hpp | 1 + include/modules/sway/mode.hpp | 27 ++++++++++++++++++++++ meson.build | 1 + src/factory.cpp | 3 +++ src/modules/sway/mode.cpp | 43 +++++++++++++++++++++++++++++++++++ 5 files changed, 75 insertions(+) create mode 100644 include/modules/sway/mode.hpp create mode 100644 src/modules/sway/mode.cpp diff --git a/include/factory.hpp b/include/factory.hpp index 2b650677..51aff7cf 100644 --- a/include/factory.hpp +++ b/include/factory.hpp @@ -3,6 +3,7 @@ #include #include "modules/clock.hpp" #ifdef HAVE_SWAY +#include "modules/sway/mode.hpp" #include "modules/sway/workspaces.hpp" #include "modules/sway/window.hpp" #endif diff --git a/include/modules/sway/mode.hpp b/include/modules/sway/mode.hpp new file mode 100644 index 00000000..beb837b6 --- /dev/null +++ b/include/modules/sway/mode.hpp @@ -0,0 +1,27 @@ +#pragma once + +#include +#include "bar.hpp" +#include "client.hpp" +#include "util/chrono.hpp" +#include "util/json.hpp" +#include "ALabel.hpp" +#include "modules/sway/ipc/client.hpp" + +namespace waybar::modules::sway { + +class Mode : public ALabel { + public: + Mode(waybar::Bar&, const Json::Value&); + auto update() -> void; + private: + void worker(); + + Bar& bar_; + waybar::util::SleeperThread thread_; + util::JsonParser parser_; + Ipc ipc_; + std::string mode_; +}; + +} \ No newline at end of file diff --git a/meson.build b/meson.build index a98c8b9b..846580b2 100644 --- a/meson.build +++ b/meson.build @@ -57,6 +57,7 @@ if find_program('sway', required : false).found() add_project_arguments('-DHAVE_SWAY', language: 'cpp') src_files += [ 'src/modules/sway/ipc/client.cpp', + 'src/modules/sway/mode.cpp', 'src/modules/sway/window.cpp', 'src/modules/sway/workspaces.cpp' ] diff --git a/src/factory.cpp b/src/factory.cpp index 479ab79b..4fba8704 100644 --- a/src/factory.cpp +++ b/src/factory.cpp @@ -12,6 +12,9 @@ waybar::IModule* waybar::Factory::makeModule(const std::string &name) const return new waybar::modules::Battery(config_[name]); } #ifdef HAVE_SWAY + if (ref == "sway/mode") { + return new waybar::modules::sway::Mode(bar_, config_[name]); + } if (ref == "sway/workspaces") { return new waybar::modules::sway::Workspaces(bar_, config_[name]); } diff --git a/src/modules/sway/mode.cpp b/src/modules/sway/mode.cpp new file mode 100644 index 00000000..a32f31b8 --- /dev/null +++ b/src/modules/sway/mode.cpp @@ -0,0 +1,43 @@ +#include "modules/sway/mode.hpp" + +waybar::modules::sway::Mode::Mode(Bar& bar, const Json::Value& config) + : ALabel(config, "{}"), bar_(bar) +{ + ipc_.connect(); + ipc_.subscribe("[ \"mode\" ]"); + // Launch worker + worker(); +} + +void waybar::modules::sway::Mode::worker() +{ + thread_ = [this] { + try { + auto res = ipc_.handleEvent(); + auto parsed = parser_.parse(res.payload); + if ((parsed["change"]) != "default" ) { + mode_ = parsed["change"].asString(); + dp.emit(); + } + else if ((parsed["change"]) == "default" ) { + mode_.clear(); + dp.emit(); + } + } catch (const std::exception& e) { + std::cerr << e.what() << std::endl; + } + }; +} + +auto waybar::modules::sway::Mode::update() -> void +{ + if (mode_.empty()) { + label_.set_name(""); + label_.hide(); + } else { + label_.set_name("mode"); + label_.set_text(fmt::format(format_, mode_)); + label_.set_tooltip_text(mode_); + label_.show(); + } +} \ No newline at end of file From 668b7b736cc6eeed4e975860b9e418a8753810cc Mon Sep 17 00:00:00 2001 From: Robinhuett <5955614+Robinhuett@users.noreply.github.com> Date: Tue, 30 Oct 2018 13:44:44 +0100 Subject: [PATCH 2/2] Added default config for sway binding mode --- resources/config | 5 ++++- resources/style.css | 7 ++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/resources/config b/resources/config index 4be4afb6..1709cc28 100644 --- a/resources/config +++ b/resources/config @@ -4,7 +4,7 @@ // "height": 30, // Waybar height // "width": 1280, // Waybar width // Choose the order of the modules - "modules-left": ["sway/workspaces", "custom/spotify"], + "modules-left": ["sway/workspaces", "sway/mode", "custom/spotify"], "modules-center": ["sway/window"], "modules-right": ["pulseaudio", "network", "cpu", "memory", "battery", "battery#bat2", "clock", "tray"], // Modules configuration @@ -23,6 +23,9 @@ // "default": "" // } // }, + "sway/mode": { + "format": "{}" + }, "sway/window": { "max-length": 50 }, diff --git a/resources/style.css b/resources/style.css index 0eb1680d..d104b63c 100644 --- a/resources/style.css +++ b/resources/style.css @@ -23,7 +23,12 @@ window#waybar { border-bottom: 3px solid white; } -#clock, #battery, #cpu, #memory, #network, #pulseaudio, #custom-spotify, #tray { +#mode { + background: #64727D; + border-bottom: 3px solid white; +} + +#clock, #battery, #cpu, #memory, #network, #pulseaudio, #custom-spotify, #tray #mode { padding: 0 10px; margin: 0 5px; }