From 46e36c0e688071142935d7b8356f1dbce32e4985 Mon Sep 17 00:00:00 2001 From: Erik Reider <35975961+ErikReider@users.noreply.github.com> Date: Sat, 28 Oct 2023 18:30:50 +0200 Subject: [PATCH] Simplified the privacy_item hiding/showing logic --- include/modules/privacy/privacy_item.hpp | 4 ++- src/modules/privacy/privacy.cpp | 2 ++ src/modules/privacy/privacy_item.cpp | 43 +++++++++++++----------- 3 files changed, 29 insertions(+), 20 deletions(-) diff --git a/include/modules/privacy/privacy_item.hpp b/include/modules/privacy/privacy_item.hpp index 56d17acf..321cae5c 100644 --- a/include/modules/privacy/privacy_item.hpp +++ b/include/modules/privacy/privacy_item.hpp @@ -29,7 +29,6 @@ class PrivacyItem : public Gtk::Revealer { enum util::PipewireBackend::PrivacyNodeType privacy_type; std::mutex mutex_; - sigc::connection signal_conn; bool init = false; bool in_use = false; @@ -41,6 +40,9 @@ class PrivacyItem : public Gtk::Revealer { Gtk::Box box_; Gtk::Image icon_; + + void on_child_revealed_changed(); + void on_map_changed(); }; } // namespace waybar::modules::privacy diff --git a/src/modules/privacy/privacy.cpp b/src/modules/privacy/privacy.cpp index f1f92838..06fb8259 100644 --- a/src/modules/privacy/privacy.cpp +++ b/src/modules/privacy/privacy.cpp @@ -125,6 +125,8 @@ auto Privacy::update() -> void { if (is_visible) { event_box_.set_visible(true); } else { + // Hides the widget when all of the privacy_item revealers animations + // have finished animating visibility_conn = Glib::signal_timeout().connect( sigc::track_obj( [this] { diff --git a/src/modules/privacy/privacy_item.cpp b/src/modules/privacy/privacy_item.cpp index c859d7e1..7ed8efe1 100644 --- a/src/modules/privacy/privacy_item.cpp +++ b/src/modules/privacy/privacy_item.cpp @@ -29,7 +29,6 @@ PrivacyItem::PrivacyItem(const Json::Value& config_, : Gtk::Revealer(), privacy_type(privacy_type_), mutex_(), - signal_conn(), box_(Gtk::ORIENTATION_HORIZONTAL, 0), icon_() { switch (privacy_type) { @@ -75,6 +74,10 @@ PrivacyItem::PrivacyItem(const Json::Value& config_, } icon_.set_from_icon_name(iconName, Gtk::ICON_SIZE_INVALID); + property_child_revealed().signal_changed().connect( + sigc::mem_fun(*this, &PrivacyItem::on_child_revealed_changed)); + signal_map().connect(sigc::mem_fun(*this, &PrivacyItem::on_map_changed)); + // Don't show by default set_reveal_child(true); set_visible(false); @@ -82,6 +85,18 @@ PrivacyItem::PrivacyItem(const Json::Value& config_, bool PrivacyItem::is_enabled() { return enabled; } +void PrivacyItem::on_child_revealed_changed() { + if (!this->get_child_revealed()) { + set_visible(false); + } +} + +void PrivacyItem::on_map_changed() { + if (this->get_visible()) { + set_reveal_child(true); + } +} + void PrivacyItem::set_in_use(bool in_use) { mutex_.lock(); if (this->in_use == in_use && init) { @@ -90,29 +105,19 @@ void PrivacyItem::set_in_use(bool in_use) { } if (init) { - // Disconnect any previous connection so that it doesn't get activated in - // the future, hiding the module when it should be visible - signal_conn.disconnect(); - this->in_use = in_use; if (this->in_use) { set_visible(true); - signal_conn = Glib::signal_timeout().connect(sigc::track_obj( - [this] { - set_reveal_child(true); - return false; - }, - *this), - 0); + // The `on_map_changed` callback will call `set_reveal_child(true)` + // when the widget is realized so we don't need to call that here. + // This fixes a bug where the revealer wouldn't start the animation + // due to us changing the visibility at the same time. } else { set_reveal_child(false); - signal_conn = Glib::signal_timeout().connect(sigc::track_obj( - [this] { - set_visible(false); - return false; - }, - *this), - get_transition_duration()); + // The `on_child_revealed_changed` callback will call `set_visible(false)` + // when the animation has finished so we don't need to call that here. + // We do this so that the widget gets hidden after the revealer hide animation + // has finished. } } else { set_visible(false);