feat(workspaces): icons

This commit is contained in:
Alexis 2018-08-15 14:48:08 +02:00
parent 767d9dd5b4
commit e3e099f836
12 changed files with 53 additions and 28 deletions

View File

@ -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;

View File

@ -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": "{}% "
},

View File

@ -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;
}

View File

@ -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")

View File

@ -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));

View File

@ -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();

View File

@ -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));

View File

@ -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();

View File

@ -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));

View File

@ -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));

View File

@ -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.");

View File

@ -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<std::mutex> lock(_mutex);