From 8fa5d9b838111519719ab742acece293bcde68a7 Mon Sep 17 00:00:00 2001 From: Simon Plakolb Date: Sat, 29 May 2021 19:40:50 +0200 Subject: [PATCH] modules: Set style-context on button Fixes issue where the class parameters in style.css would have no effect. The CSS now references the GtkButton instead of the GtkLabel. Removing all style-classes from the custom module GtkButton however removes any properties set via style.css. Thus, the default classes 'flat' and 'text-button' are added on every update of these modules. --- src/AButton.cpp | 4 ++-- src/modules/battery.cpp | 4 ++-- src/modules/bluetooth.cpp | 8 ++++---- src/modules/custom.cpp | 8 +++++--- src/modules/idle_inhibitor.cpp | 6 +++--- src/modules/inhibitor.cpp | 4 ++-- src/modules/mpd/mpd.cpp | 26 +++++++++++++------------- src/modules/network.cpp | 8 ++++---- src/modules/pulseaudio.cpp | 16 ++++++++-------- src/modules/sndio.cpp | 4 ++-- src/modules/sway/language.cpp | 4 ++-- src/modules/temperature.cpp | 4 ++-- 12 files changed, 49 insertions(+), 47 deletions(-) diff --git a/src/AButton.cpp b/src/AButton.cpp index 3b9ad2d..1ef1804 100644 --- a/src/AButton.cpp +++ b/src/AButton.cpp @@ -142,10 +142,10 @@ std::string AButton::getState(uint8_t value, bool lesser) { std::string valid_state; for (auto const& state : states) { if ((lesser ? value <= state.second : value >= state.second) && valid_state.empty()) { - label_->get_style_context()->add_class(state.first); + button_.get_style_context()->add_class(state.first); valid_state = state.first; } else { - label_->get_style_context()->remove_class(state.first); + button_.get_style_context()->remove_class(state.first); } } return valid_state; diff --git a/src/modules/battery.cpp b/src/modules/battery.cpp index 1a37172..6d84946 100644 --- a/src/modules/battery.cpp +++ b/src/modules/battery.cpp @@ -352,9 +352,9 @@ auto waybar::modules::Battery::update() -> void { fmt::arg("time", time_remaining_formatted))); } if (!old_status_.empty()) { - label_->get_style_context()->remove_class(old_status_); + button_.get_style_context()->remove_class(old_status_); } - label_->get_style_context()->add_class(status); + button_.get_style_context()->add_class(status); old_status_ = status; if (!state.empty() && config_["format-" + status + "-" + state].isString()) { format = config_["format-" + status + "-" + state].asString(); diff --git a/src/modules/bluetooth.cpp b/src/modules/bluetooth.cpp index 242774e..d483ac3 100644 --- a/src/modules/bluetooth.cpp +++ b/src/modules/bluetooth.cpp @@ -189,10 +189,10 @@ auto waybar::modules::Bluetooth::update() -> void { format_.empty() ? event_box_.hide() : event_box_.show(); auto update_style_context = [this](const std::string& style_class, bool in_next_state) { - if (in_next_state && !label_->get_style_context()->has_class(style_class)) { - label_->get_style_context()->add_class(style_class); - } else if (!in_next_state && label_->get_style_context()->has_class(style_class)) { - label_->get_style_context()->remove_class(style_class); + if (in_next_state && !button_.get_style_context()->has_class(style_class)) { + button_.get_style_context()->add_class(style_class); + } else if (!in_next_state && button_.get_style_context()->has_class(style_class)) { + button_.get_style_context()->remove_class(style_class); } }; update_style_context("discoverable", cur_controller_.discoverable); diff --git a/src/modules/custom.cpp b/src/modules/custom.cpp index 17950dd..930379b 100644 --- a/src/modules/custom.cpp +++ b/src/modules/custom.cpp @@ -143,14 +143,16 @@ auto waybar::modules::Custom::update() -> void { } } } - auto classes = label_->get_style_context()->list_classes(); + auto classes = button_.get_style_context()->list_classes(); for (auto const& c : classes) { if (c == id_) continue; - label_->get_style_context()->remove_class(c); + button_.get_style_context()->remove_class(c); } for (auto const& c : class_) { - label_->get_style_context()->add_class(c); + button_.get_style_context()->add_class(c); } + button_.get_style_context()->add_class("flat"); + button_.get_style_context()->add_class("text-button"); event_box_.show(); } } diff --git a/src/modules/idle_inhibitor.cpp b/src/modules/idle_inhibitor.cpp index fd5611f..dc373a9 100644 --- a/src/modules/idle_inhibitor.cpp +++ b/src/modules/idle_inhibitor.cpp @@ -44,13 +44,13 @@ waybar::modules::IdleInhibitor::~IdleInhibitor() { auto waybar::modules::IdleInhibitor::update() -> void { // Check status if (status) { - label_->get_style_context()->remove_class("deactivated"); + button_.get_style_context()->remove_class("deactivated"); if (idle_inhibitor_ == nullptr) { idle_inhibitor_ = zwp_idle_inhibit_manager_v1_create_inhibitor( waybar::Client::inst()->idle_inhibit_manager, bar_.surface); } } else { - label_->get_style_context()->remove_class("activated"); + button_.get_style_context()->remove_class("activated"); if (idle_inhibitor_ != nullptr) { zwp_idle_inhibitor_v1_destroy(idle_inhibitor_); idle_inhibitor_ = nullptr; @@ -60,7 +60,7 @@ auto waybar::modules::IdleInhibitor::update() -> void { std::string status_text = status ? "activated" : "deactivated"; label_->set_markup(fmt::format(format_, fmt::arg("status", status_text), fmt::arg("icon", getIcon(0, status_text)))); - label_->get_style_context()->add_class(status_text); + button_.get_style_context()->add_class(status_text); if (tooltipEnabled()) { label_->set_tooltip_markup( status ? fmt::format(config_["tooltip-format-activated"].isString() diff --git a/src/modules/inhibitor.cpp b/src/modules/inhibitor.cpp index 2ab013d..fc85456 100644 --- a/src/modules/inhibitor.cpp +++ b/src/modules/inhibitor.cpp @@ -117,10 +117,10 @@ auto Inhibitor::activated() -> bool { return handle_ != -1; } auto Inhibitor::update() -> void { std::string status_text = activated() ? "activated" : "deactivated"; - label_->get_style_context()->remove_class(activated() ? "deactivated" : "activated"); + button_.get_style_context()->remove_class(activated() ? "deactivated" : "activated"); label_->set_markup(fmt::format(format_, fmt::arg("status", status_text), fmt::arg("icon", getIcon(0, status_text)))); - label_->get_style_context()->add_class(status_text); + button_.get_style_context()->add_class(status_text); if (tooltipEnabled()) { label_->set_tooltip_text(status_text); diff --git a/src/modules/mpd/mpd.cpp b/src/modules/mpd/mpd.cpp index a79f203..ec6ccb3 100644 --- a/src/modules/mpd/mpd.cpp +++ b/src/modules/mpd/mpd.cpp @@ -85,10 +85,10 @@ std::string waybar::modules::MPD::getFilename() const { void waybar::modules::MPD::setLabel() { if (connection_ == nullptr) { - label_->get_style_context()->add_class("disconnected"); - label_->get_style_context()->remove_class("stopped"); - label_->get_style_context()->remove_class("playing"); - label_->get_style_context()->remove_class("paused"); + button_.get_style_context()->add_class("disconnected"); + button_.get_style_context()->remove_class("stopped"); + button_.get_style_context()->remove_class("playing"); + button_.get_style_context()->remove_class("paused"); auto format = config_["format-disconnected"].isString() ? config_["format-disconnected"].asString() @@ -105,7 +105,7 @@ void waybar::modules::MPD::setLabel() { } return; } else { - label_->get_style_context()->remove_class("disconnected"); + button_.get_style_context()->remove_class("disconnected"); } auto format = format_; @@ -118,19 +118,19 @@ void waybar::modules::MPD::setLabel() { if (stopped()) { format = config_["format-stopped"].isString() ? config_["format-stopped"].asString() : "stopped"; - label_->get_style_context()->add_class("stopped"); - label_->get_style_context()->remove_class("playing"); - label_->get_style_context()->remove_class("paused"); + button_.get_style_context()->add_class("stopped"); + button_.get_style_context()->remove_class("playing"); + button_.get_style_context()->remove_class("paused"); } else { - label_->get_style_context()->remove_class("stopped"); + button_.get_style_context()->remove_class("stopped"); if (playing()) { - label_->get_style_context()->add_class("playing"); - label_->get_style_context()->remove_class("paused"); + button_.get_style_context()->add_class("playing"); + button_.get_style_context()->remove_class("paused"); } else if (paused()) { format = config_["format-paused"].isString() ? config_["format-paused"].asString() : config_["format"].asString(); - label_->get_style_context()->add_class("paused"); - label_->get_style_context()->remove_class("playing"); + button_.get_style_context()->add_class("paused"); + button_.get_style_context()->remove_class("playing"); } stateIcon = getStateIcon(); diff --git a/src/modules/network.cpp b/src/modules/network.cpp index c520026..e2467f2 100644 --- a/src/modules/network.cpp +++ b/src/modules/network.cpp @@ -309,8 +309,8 @@ auto waybar::modules::Network::update() -> void { if (!alt_) { auto state = getNetworkState(); - if (!state_.empty() && label_->get_style_context()->has_class(state_)) { - label_->get_style_context()->remove_class(state_); + if (!state_.empty() && button_.get_style_context()->has_class(state_)) { + button_.get_style_context()->remove_class(state_); } if (config_["format-" + state].isString()) { default_format_ = config_["format-" + state].asString(); @@ -322,8 +322,8 @@ auto waybar::modules::Network::update() -> void { if (config_["tooltip-format-" + state].isString()) { tooltip_format = config_["tooltip-format-" + state].asString(); } - if (!label_->get_style_context()->has_class(state)) { - label_->get_style_context()->add_class(state); + if (!button_.get_style_context()->has_class(state)) { + button_.get_style_context()->add_class(state); } format_ = default_format_; state_ = state; diff --git a/src/modules/pulseaudio.cpp b/src/modules/pulseaudio.cpp index 11aaced..f0b7b7d 100644 --- a/src/modules/pulseaudio.cpp +++ b/src/modules/pulseaudio.cpp @@ -240,9 +240,9 @@ auto waybar::modules::Pulseaudio::update() -> void { if (monitor_.find("a2dp_sink") != std::string::npos || // PulseAudio monitor_.find("a2dp-sink") != std::string::npos) { // PipeWire format_name = format_name + "-bluetooth"; - label_->get_style_context()->add_class("bluetooth"); + button_.get_style_context()->add_class("bluetooth"); } else { - label_->get_style_context()->remove_class("bluetooth"); + button_.get_style_context()->remove_class("bluetooth"); } if (muted_) { // Check muted bluetooth format exist, otherwise fallback to default muted format @@ -250,23 +250,23 @@ auto waybar::modules::Pulseaudio::update() -> void { format_name = "format"; } format_name = format_name + "-muted"; - label_->get_style_context()->add_class("muted"); - label_->get_style_context()->add_class("sink-muted"); + button_.get_style_context()->add_class("muted"); + button_.get_style_context()->add_class("sink-muted"); } else { - label_->get_style_context()->remove_class("muted"); - label_->get_style_context()->remove_class("sink-muted"); + button_.get_style_context()->remove_class("muted"); + button_.get_style_context()->remove_class("sink-muted"); } format = config_[format_name].isString() ? config_[format_name].asString() : format; } // TODO: find a better way to split source/sink std::string format_source = "{volume}%"; if (source_muted_) { - label_->get_style_context()->add_class("source-muted"); + button_.get_style_context()->add_class("source-muted"); if (config_["format-source-muted"].isString()) { format_source = config_["format-source-muted"].asString(); } } else { - label_->get_style_context()->remove_class("source-muted"); + button_.get_style_context()->remove_class("source-muted"); if (config_["format-source-muted"].isString()) { format_source = config_["format-source"].asString(); } diff --git a/src/modules/sndio.cpp b/src/modules/sndio.cpp index 57b435e..dfab0d2 100644 --- a/src/modules/sndio.cpp +++ b/src/modules/sndio.cpp @@ -105,9 +105,9 @@ auto Sndio::update() -> void { unsigned int vol = 100. * static_cast(volume_) / static_cast(maxval_); if (volume_ == 0) { - label_->get_style_context()->add_class("muted"); + button_.get_style_context()->add_class("muted"); } else { - label_->get_style_context()->remove_class("muted"); + button_.get_style_context()->remove_class("muted"); } label_->set_markup(fmt::format(format, fmt::arg("volume", vol), fmt::arg("raw_value", volume_))); diff --git a/src/modules/sway/language.cpp b/src/modules/sway/language.cpp index eb41438..f9d81a2 100644 --- a/src/modules/sway/language.cpp +++ b/src/modules/sway/language.cpp @@ -120,9 +120,9 @@ auto Language::update() -> void { } auto Language::set_current_layout(std::string current_layout) -> void { - label_->get_style_context()->remove_class(layout_.short_name); + button_.get_style_context()->remove_class(layout_.short_name); layout_ = layouts_map_[current_layout]; - label_->get_style_context()->add_class(layout_.short_name); + button_.get_style_context()->add_class(layout_.short_name); } auto Language::init_layouts_map(const std::vector& used_layouts) -> void { diff --git a/src/modules/temperature.cpp b/src/modules/temperature.cpp index b6ee7fa..c8e0bf0 100644 --- a/src/modules/temperature.cpp +++ b/src/modules/temperature.cpp @@ -45,9 +45,9 @@ auto waybar::modules::Temperature::update() -> void { auto format = format_; if (critical) { format = config_["format-critical"].isString() ? config_["format-critical"].asString() : format; - label_->get_style_context()->add_class("critical"); + button_.get_style_context()->add_class("critical"); } else { - label_->get_style_context()->remove_class("critical"); + button_.get_style_context()->remove_class("critical"); } if (format.empty()) {