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.
This commit is contained in:
Simon Plakolb 2021-05-29 19:40:50 +02:00
parent 0012bcbd74
commit 8fa5d9b838
12 changed files with 49 additions and 47 deletions

View File

@ -142,10 +142,10 @@ std::string AButton::getState(uint8_t value, bool lesser) {
std::string valid_state; std::string valid_state;
for (auto const& state : states) { for (auto const& state : states) {
if ((lesser ? value <= state.second : value >= state.second) && valid_state.empty()) { 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; valid_state = state.first;
} else { } else {
label_->get_style_context()->remove_class(state.first); button_.get_style_context()->remove_class(state.first);
} }
} }
return valid_state; return valid_state;

View File

@ -352,9 +352,9 @@ auto waybar::modules::Battery::update() -> void {
fmt::arg("time", time_remaining_formatted))); fmt::arg("time", time_remaining_formatted)));
} }
if (!old_status_.empty()) { 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; old_status_ = status;
if (!state.empty() && config_["format-" + status + "-" + state].isString()) { if (!state.empty() && config_["format-" + status + "-" + state].isString()) {
format = config_["format-" + status + "-" + state].asString(); format = config_["format-" + status + "-" + state].asString();

View File

@ -189,10 +189,10 @@ auto waybar::modules::Bluetooth::update() -> void {
format_.empty() ? event_box_.hide() : event_box_.show(); format_.empty() ? event_box_.hide() : event_box_.show();
auto update_style_context = [this](const std::string& style_class, bool in_next_state) { 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)) { if (in_next_state && !button_.get_style_context()->has_class(style_class)) {
label_->get_style_context()->add_class(style_class); button_.get_style_context()->add_class(style_class);
} else if (!in_next_state && label_->get_style_context()->has_class(style_class)) { } else if (!in_next_state && button_.get_style_context()->has_class(style_class)) {
label_->get_style_context()->remove_class(style_class); button_.get_style_context()->remove_class(style_class);
} }
}; };
update_style_context("discoverable", cur_controller_.discoverable); update_style_context("discoverable", cur_controller_.discoverable);

View File

@ -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) { for (auto const& c : classes) {
if (c == id_) continue; if (c == id_) continue;
label_->get_style_context()->remove_class(c); button_.get_style_context()->remove_class(c);
} }
for (auto const& c : class_) { 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(); event_box_.show();
} }
} }

View File

@ -44,13 +44,13 @@ waybar::modules::IdleInhibitor::~IdleInhibitor() {
auto waybar::modules::IdleInhibitor::update() -> void { auto waybar::modules::IdleInhibitor::update() -> void {
// Check status // Check status
if (status) { if (status) {
label_->get_style_context()->remove_class("deactivated"); button_.get_style_context()->remove_class("deactivated");
if (idle_inhibitor_ == nullptr) { if (idle_inhibitor_ == nullptr) {
idle_inhibitor_ = zwp_idle_inhibit_manager_v1_create_inhibitor( idle_inhibitor_ = zwp_idle_inhibit_manager_v1_create_inhibitor(
waybar::Client::inst()->idle_inhibit_manager, bar_.surface); waybar::Client::inst()->idle_inhibit_manager, bar_.surface);
} }
} else { } else {
label_->get_style_context()->remove_class("activated"); button_.get_style_context()->remove_class("activated");
if (idle_inhibitor_ != nullptr) { if (idle_inhibitor_ != nullptr) {
zwp_idle_inhibitor_v1_destroy(idle_inhibitor_); zwp_idle_inhibitor_v1_destroy(idle_inhibitor_);
idle_inhibitor_ = nullptr; idle_inhibitor_ = nullptr;
@ -60,7 +60,7 @@ auto waybar::modules::IdleInhibitor::update() -> void {
std::string status_text = status ? "activated" : "deactivated"; std::string status_text = status ? "activated" : "deactivated";
label_->set_markup(fmt::format(format_, fmt::arg("status", status_text), label_->set_markup(fmt::format(format_, fmt::arg("status", status_text),
fmt::arg("icon", getIcon(0, 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()) { if (tooltipEnabled()) {
label_->set_tooltip_markup( label_->set_tooltip_markup(
status ? fmt::format(config_["tooltip-format-activated"].isString() status ? fmt::format(config_["tooltip-format-activated"].isString()

View File

@ -117,10 +117,10 @@ auto Inhibitor::activated() -> bool { return handle_ != -1; }
auto Inhibitor::update() -> void { auto Inhibitor::update() -> void {
std::string status_text = activated() ? "activated" : "deactivated"; 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), label_->set_markup(fmt::format(format_, fmt::arg("status", status_text),
fmt::arg("icon", getIcon(0, 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()) { if (tooltipEnabled()) {
label_->set_tooltip_text(status_text); label_->set_tooltip_text(status_text);

View File

@ -85,10 +85,10 @@ std::string waybar::modules::MPD::getFilename() const {
void waybar::modules::MPD::setLabel() { void waybar::modules::MPD::setLabel() {
if (connection_ == nullptr) { if (connection_ == nullptr) {
label_->get_style_context()->add_class("disconnected"); button_.get_style_context()->add_class("disconnected");
label_->get_style_context()->remove_class("stopped"); button_.get_style_context()->remove_class("stopped");
label_->get_style_context()->remove_class("playing"); button_.get_style_context()->remove_class("playing");
label_->get_style_context()->remove_class("paused"); button_.get_style_context()->remove_class("paused");
auto format = config_["format-disconnected"].isString() auto format = config_["format-disconnected"].isString()
? config_["format-disconnected"].asString() ? config_["format-disconnected"].asString()
@ -105,7 +105,7 @@ void waybar::modules::MPD::setLabel() {
} }
return; return;
} else { } else {
label_->get_style_context()->remove_class("disconnected"); button_.get_style_context()->remove_class("disconnected");
} }
auto format = format_; auto format = format_;
@ -118,19 +118,19 @@ void waybar::modules::MPD::setLabel() {
if (stopped()) { if (stopped()) {
format = format =
config_["format-stopped"].isString() ? config_["format-stopped"].asString() : "stopped"; config_["format-stopped"].isString() ? config_["format-stopped"].asString() : "stopped";
label_->get_style_context()->add_class("stopped"); button_.get_style_context()->add_class("stopped");
label_->get_style_context()->remove_class("playing"); button_.get_style_context()->remove_class("playing");
label_->get_style_context()->remove_class("paused"); button_.get_style_context()->remove_class("paused");
} else { } else {
label_->get_style_context()->remove_class("stopped"); button_.get_style_context()->remove_class("stopped");
if (playing()) { if (playing()) {
label_->get_style_context()->add_class("playing"); button_.get_style_context()->add_class("playing");
label_->get_style_context()->remove_class("paused"); button_.get_style_context()->remove_class("paused");
} else if (paused()) { } else if (paused()) {
format = config_["format-paused"].isString() ? config_["format-paused"].asString() format = config_["format-paused"].isString() ? config_["format-paused"].asString()
: config_["format"].asString(); : config_["format"].asString();
label_->get_style_context()->add_class("paused"); button_.get_style_context()->add_class("paused");
label_->get_style_context()->remove_class("playing"); button_.get_style_context()->remove_class("playing");
} }
stateIcon = getStateIcon(); stateIcon = getStateIcon();

View File

@ -309,8 +309,8 @@ auto waybar::modules::Network::update() -> void {
if (!alt_) { if (!alt_) {
auto state = getNetworkState(); auto state = getNetworkState();
if (!state_.empty() && label_->get_style_context()->has_class(state_)) { if (!state_.empty() && button_.get_style_context()->has_class(state_)) {
label_->get_style_context()->remove_class(state_); button_.get_style_context()->remove_class(state_);
} }
if (config_["format-" + state].isString()) { if (config_["format-" + state].isString()) {
default_format_ = config_["format-" + state].asString(); default_format_ = config_["format-" + state].asString();
@ -322,8 +322,8 @@ auto waybar::modules::Network::update() -> void {
if (config_["tooltip-format-" + state].isString()) { if (config_["tooltip-format-" + state].isString()) {
tooltip_format = config_["tooltip-format-" + state].asString(); tooltip_format = config_["tooltip-format-" + state].asString();
} }
if (!label_->get_style_context()->has_class(state)) { if (!button_.get_style_context()->has_class(state)) {
label_->get_style_context()->add_class(state); button_.get_style_context()->add_class(state);
} }
format_ = default_format_; format_ = default_format_;
state_ = state; state_ = state;

View File

@ -240,9 +240,9 @@ auto waybar::modules::Pulseaudio::update() -> void {
if (monitor_.find("a2dp_sink") != std::string::npos || // PulseAudio if (monitor_.find("a2dp_sink") != std::string::npos || // PulseAudio
monitor_.find("a2dp-sink") != std::string::npos) { // PipeWire monitor_.find("a2dp-sink") != std::string::npos) { // PipeWire
format_name = format_name + "-bluetooth"; format_name = format_name + "-bluetooth";
label_->get_style_context()->add_class("bluetooth"); button_.get_style_context()->add_class("bluetooth");
} else { } else {
label_->get_style_context()->remove_class("bluetooth"); button_.get_style_context()->remove_class("bluetooth");
} }
if (muted_) { if (muted_) {
// Check muted bluetooth format exist, otherwise fallback to default muted format // 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";
} }
format_name = format_name + "-muted"; format_name = format_name + "-muted";
label_->get_style_context()->add_class("muted"); button_.get_style_context()->add_class("muted");
label_->get_style_context()->add_class("sink-muted"); button_.get_style_context()->add_class("sink-muted");
} else { } else {
label_->get_style_context()->remove_class("muted"); button_.get_style_context()->remove_class("muted");
label_->get_style_context()->remove_class("sink-muted"); button_.get_style_context()->remove_class("sink-muted");
} }
format = config_[format_name].isString() ? config_[format_name].asString() : format; format = config_[format_name].isString() ? config_[format_name].asString() : format;
} }
// TODO: find a better way to split source/sink // TODO: find a better way to split source/sink
std::string format_source = "{volume}%"; std::string format_source = "{volume}%";
if (source_muted_) { 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()) { if (config_["format-source-muted"].isString()) {
format_source = config_["format-source-muted"].asString(); format_source = config_["format-source-muted"].asString();
} }
} else { } else {
label_->get_style_context()->remove_class("source-muted"); button_.get_style_context()->remove_class("source-muted");
if (config_["format-source-muted"].isString()) { if (config_["format-source-muted"].isString()) {
format_source = config_["format-source"].asString(); format_source = config_["format-source"].asString();
} }

View File

@ -105,9 +105,9 @@ auto Sndio::update() -> void {
unsigned int vol = 100. * static_cast<double>(volume_) / static_cast<double>(maxval_); unsigned int vol = 100. * static_cast<double>(volume_) / static_cast<double>(maxval_);
if (volume_ == 0) { if (volume_ == 0) {
label_->get_style_context()->add_class("muted"); button_.get_style_context()->add_class("muted");
} else { } 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_))); label_->set_markup(fmt::format(format, fmt::arg("volume", vol), fmt::arg("raw_value", volume_)));

View File

@ -120,9 +120,9 @@ auto Language::update() -> void {
} }
auto Language::set_current_layout(std::string current_layout) -> 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]; 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<std::string>& used_layouts) -> void { auto Language::init_layouts_map(const std::vector<std::string>& used_layouts) -> void {

View File

@ -45,9 +45,9 @@ auto waybar::modules::Temperature::update() -> void {
auto format = format_; auto format = format_;
if (critical) { if (critical) {
format = config_["format-critical"].isString() ? config_["format-critical"].asString() : format; 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 { } else {
label_->get_style_context()->remove_class("critical"); button_.get_style_context()->remove_class("critical");
} }
if (format.empty()) { if (format.empty()) {