From e3e099f836ec2f523593427b6008b8c1e7577fc0 Mon Sep 17 00:00:00 2001 From: Alexis Date: Wed, 15 Aug 2018 14:48:08 +0200 Subject: [PATCH] feat(workspaces): icons --- include/modules/workspaces.hpp | 4 +++- resources/config | 9 +++++++++ resources/style.css | 32 ++++++++++++++++++-------------- src/factory.cpp | 2 +- src/modules/battery.cpp | 2 +- src/modules/clock.cpp | 2 +- src/modules/cpu.cpp | 2 +- src/modules/custom.cpp | 4 ++-- src/modules/memory.cpp | 2 +- src/modules/network.cpp | 2 +- src/modules/pulseaudio.cpp | 2 +- src/modules/workspaces.cpp | 18 ++++++++++++++---- 12 files changed, 53 insertions(+), 28 deletions(-) diff --git a/include/modules/workspaces.hpp b/include/modules/workspaces.hpp index 928ee6d..8c1d08e 100644 --- a/include/modules/workspaces.hpp +++ b/include/modules/workspaces.hpp @@ -11,16 +11,18 @@ namespace waybar::modules { class Workspaces : public IModule { public: - Workspaces(waybar::Bar &bar); + Workspaces(waybar::Bar &bar, Json::Value config); auto update() -> void; operator Gtk::Widget &(); private: void _addWorkspace(Json::Value node); + std::string _getIcon(std::string name); Json::Value _getWorkspaces(const std::string data); bool _handleScroll(GdkEventScroll *e); int _getPrevWorkspace(); int _getNextWorkspace(); Bar &_bar; + Json::Value _config; waybar::util::SleeperThread _thread; Gtk::Box _box; util::JsonParser _parser; diff --git a/resources/config b/resources/config index 58aefe6..a86b908 100644 --- a/resources/config +++ b/resources/config @@ -7,6 +7,15 @@ "modules-left": ["workspaces", "custom/spotify"], "modules-right": ["pulseaudio", "network", "cpu", "memory", "battery", "clock"], // Modules configuration + "workspaces": { + "format-icons": { + "1": "", + "2": "", + "3": "", + "4": "", + "5": "" + } + }, "cpu": { "format": "{}% " }, diff --git a/resources/style.css b/resources/style.css index 4fa2226..aecf96b 100644 --- a/resources/style.css +++ b/resources/style.css @@ -11,33 +11,37 @@ window { color: white; } -.workspaces button { - padding: 0 5px; +#workspaces button { + padding: 0 8px; background: transparent; color: white; border-bottom: 3px solid transparent; } -.workspaces button.current { +#workspaces button label { + font-size: 12px; +} + +#workspaces button.current { background: #64727D; border-bottom: 3px solid white; } -.clock, .battery, .cpu, .memory, .network, .pulseaudio, .custom-spotify { +#clock, #battery, #cpu, #memory, #network, #pulseaudio, #custom-spotify { padding: 0 10px; margin: 0 5px; } -.clock { +#clock { background-color: #64727D; } -.battery { +#battery { background-color: #ffffff; color: black; } -.battery.charging { +#battery.charging { color: white; background-color: #26A65B; } @@ -49,7 +53,7 @@ window { } } -.battery.warning { +#battery.warning { background: #f53c3c; color: white; animation-name: blink; @@ -59,30 +63,30 @@ window { animation-direction: alternate; } -.cpu { +#cpu { background: #2ecc71; color: #000000; } -.memory { +#memory { background: #9b59b6; } -.network { +#network { background: #2980b9; } -.pulseaudio { +#pulseaudio { background: #f1c40f; color: black; } -.pulseaudio.muted { +#pulseaudio.muted { background: #90b1b1; color: #2a5c45; } -.custom-spotify { +#custom-spotify { background: #66cc99; color: #2a5c45; } diff --git a/src/factory.cpp b/src/factory.cpp index 1bf0c12..cce540f 100644 --- a/src/factory.cpp +++ b/src/factory.cpp @@ -10,7 +10,7 @@ waybar::IModule *waybar::Factory::makeModule(std::string name) if (name == "battery") return new waybar::modules::Battery(_config[name]); if (name == "workspaces") - return new waybar::modules::Workspaces(_bar); + return new waybar::modules::Workspaces(_bar, _config[name]); if (name == "memory") return new waybar::modules::Memory(_config[name]); if (name == "cpu") diff --git a/src/modules/battery.cpp b/src/modules/battery.cpp index 697c64e..51aeb85 100644 --- a/src/modules/battery.cpp +++ b/src/modules/battery.cpp @@ -23,7 +23,7 @@ waybar::modules::Battery::Battery(Json::Value config) inotify_add_watch(fd, (bat / "uevent").c_str(), IN_ACCESS); // Trigger first value update(); - _label.get_style_context()->add_class("battery"); + _label.set_name("battery"); _thread = [this, fd] { struct inotify_event event; int nbytes = read(fd, &event, sizeof(event)); diff --git a/src/modules/clock.cpp b/src/modules/clock.cpp index 92d2345..d68a5d1 100644 --- a/src/modules/clock.cpp +++ b/src/modules/clock.cpp @@ -3,7 +3,7 @@ waybar::modules::Clock::Clock(Json::Value config) : _config(config) { - _label.get_style_context()->add_class("clock"); + _label.set_name("clock"); _thread = [this] { Glib::signal_idle().connect_once(sigc::mem_fun(*this, &Clock::update)); auto now = waybar::chrono::clock::now(); diff --git a/src/modules/cpu.cpp b/src/modules/cpu.cpp index 76bc804..2cc3db8 100644 --- a/src/modules/cpu.cpp +++ b/src/modules/cpu.cpp @@ -3,7 +3,7 @@ waybar::modules::Cpu::Cpu(Json::Value config) : _config(config) { - _label.get_style_context()->add_class("cpu"); + _label.set_name("cpu"); int interval = _config["interval"] ? _config["inveral"].asInt() : 10; _thread = [this, interval] { Glib::signal_idle().connect_once(sigc::mem_fun(*this, &Cpu::update)); diff --git a/src/modules/custom.cpp b/src/modules/custom.cpp index ee8a9da..47608c6 100644 --- a/src/modules/custom.cpp +++ b/src/modules/custom.cpp @@ -34,10 +34,10 @@ auto waybar::modules::Custom::update() -> void // Hide label if output is empty if (output.empty()) { - _label.get_style_context()->remove_class("custom-" + _name); + _label.set_name(""); _label.hide(); } else { - _label.get_style_context()->add_class("custom-" + _name); + _label.set_name("custom-" + _name); auto format = _config["format"] ? _config["format"].asString() : "{}"; _label.set_text(fmt::format(format, output)); _label.show(); diff --git a/src/modules/memory.cpp b/src/modules/memory.cpp index 19bf5b7..837c4c7 100644 --- a/src/modules/memory.cpp +++ b/src/modules/memory.cpp @@ -3,7 +3,7 @@ waybar::modules::Memory::Memory(Json::Value config) : _config(config) { - _label.get_style_context()->add_class("memory"); + _label.set_name("memory"); int interval = _config["interval"] ? _config["inveral"].asInt() : 30; _thread = [this, interval] { Glib::signal_idle().connect_once(sigc::mem_fun(*this, &Memory::update)); diff --git a/src/modules/network.cpp b/src/modules/network.cpp index 6fea42b..8fc04c1 100644 --- a/src/modules/network.cpp +++ b/src/modules/network.cpp @@ -5,7 +5,7 @@ waybar::modules::Network::Network(Json::Value config) { if (_ifid == 0) throw std::runtime_error("Can't found network interface"); - _label.get_style_context()->add_class("network"); + _label.set_name("network"); int interval = _config["interval"] ? _config["inveral"].asInt() : 30; _thread = [this, interval] { Glib::signal_idle().connect_once(sigc::mem_fun(*this, &Network::update)); diff --git a/src/modules/pulseaudio.cpp b/src/modules/pulseaudio.cpp index 6068c07..3225d6d 100644 --- a/src/modules/pulseaudio.cpp +++ b/src/modules/pulseaudio.cpp @@ -4,7 +4,7 @@ waybar::modules::Pulseaudio::Pulseaudio(Json::Value config) : _config(config), _mainloop(nullptr), _mainloop_api(nullptr), _context(nullptr), _sinkIdx(0), _volume(0), _muted(false) { - _label.get_style_context()->add_class("pulseaudio"); + _label.set_name("pulseaudio"); _mainloop = pa_threaded_mainloop_new(); if (!_mainloop) throw std::runtime_error("pa_mainloop_new() failed."); diff --git a/src/modules/workspaces.cpp b/src/modules/workspaces.cpp index b664a97..b8f6186 100644 --- a/src/modules/workspaces.cpp +++ b/src/modules/workspaces.cpp @@ -1,10 +1,10 @@ #include "modules/workspaces.hpp" #include "ipc/client.hpp" -waybar::modules::Workspaces::Workspaces(Bar &bar) - : _bar(bar), _scrolling(false) +waybar::modules::Workspaces::Workspaces(Bar &bar, Json::Value config) + : _bar(bar), _config(config), _scrolling(false) { - _box.get_style_context()->add_class("workspaces"); + _box.set_name("workspaces"); std::string socketPath = get_socketpath(); _ipcfd = ipc_open_socket(socketPath); _ipcEventfd = ipc_open_socket(socketPath); @@ -67,7 +67,8 @@ auto waybar::modules::Workspaces::update() -> void void waybar::modules::Workspaces::_addWorkspace(Json::Value node) { - auto pair = _buttons.emplace(node["num"].asInt(), node["name"].asString()); + auto pair = _buttons.emplace(node["num"].asInt(), + _getIcon(node["name"].asString())); auto &button = pair.first->second; _box.pack_start(button, false, false, 0); button.set_relief(Gtk::RELIEF_NONE); @@ -90,6 +91,15 @@ void waybar::modules::Workspaces::_addWorkspace(Json::Value node) button.show(); } +std::string waybar::modules::Workspaces::_getIcon(std::string name) +{ + if (_config["format-icons"][name]) + return _config["format-icons"][name].asString(); + if (_config["format-icons"]["default"]) + return _config["format-icons"]["default"].asString(); + return name; +} + bool waybar::modules::Workspaces::_handleScroll(GdkEventScroll *e) { std::lock_guard lock(_mutex);