Skip to content

Commit

Permalink
modules: Set style-context on button
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
pinselimo committed Jun 20, 2021
1 parent c5ba09d commit 612a1a8
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 38 deletions.
4 changes: 2 additions & 2 deletions src/AButton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,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;
Expand Down
4 changes: 2 additions & 2 deletions src/modules/battery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -308,9 +308,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();
Expand Down
8 changes: 5 additions & 3 deletions src/modules/custom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,15 @@ 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) {
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();
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/modules/idle_inhibitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
26 changes: 13 additions & 13 deletions src/modules/mpd/mpd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ std::string waybar::modules::MPD::getTag(mpd_tag_type type, unsigned idx) 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()
Expand All @@ -94,7 +94,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_;
Expand All @@ -107,19 +107,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();
Expand Down
8 changes: 4 additions & 4 deletions src/modules/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,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();
Expand All @@ -332,8 +332,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;
Expand Down
16 changes: 8 additions & 8 deletions src/modules/pulseaudio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,34 +213,34 @@ 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
if (format_name != "format" && !config_[format_name + "-muted"].isString()) {
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();
}
Expand Down
4 changes: 2 additions & 2 deletions src/modules/sndio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ auto Sndio::update() -> void {
unsigned int vol = 100. * static_cast<double>(volume_) / static_cast<double>(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,
Expand Down
4 changes: 2 additions & 2 deletions src/modules/temperature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,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");
}
auto max_temp = config_["critical-threshold"].isInt() ? config_["critical-threshold"].asInt() : 0;
label_->set_markup(fmt::format(format,
Expand Down

0 comments on commit 612a1a8

Please sign in to comment.