Simplified the privacy_item hiding/showing logic

This commit is contained in:
Erik Reider 2023-10-28 18:30:50 +02:00
parent 86491e1512
commit 46e36c0e68
3 changed files with 29 additions and 20 deletions

View File

@ -29,7 +29,6 @@ class PrivacyItem : public Gtk::Revealer {
enum util::PipewireBackend::PrivacyNodeType privacy_type; enum util::PipewireBackend::PrivacyNodeType privacy_type;
std::mutex mutex_; std::mutex mutex_;
sigc::connection signal_conn;
bool init = false; bool init = false;
bool in_use = false; bool in_use = false;
@ -41,6 +40,9 @@ class PrivacyItem : public Gtk::Revealer {
Gtk::Box box_; Gtk::Box box_;
Gtk::Image icon_; Gtk::Image icon_;
void on_child_revealed_changed();
void on_map_changed();
}; };
} // namespace waybar::modules::privacy } // namespace waybar::modules::privacy

View File

@ -125,6 +125,8 @@ auto Privacy::update() -> void {
if (is_visible) { if (is_visible) {
event_box_.set_visible(true); event_box_.set_visible(true);
} else { } else {
// Hides the widget when all of the privacy_item revealers animations
// have finished animating
visibility_conn = Glib::signal_timeout().connect( visibility_conn = Glib::signal_timeout().connect(
sigc::track_obj( sigc::track_obj(
[this] { [this] {

View File

@ -29,7 +29,6 @@ PrivacyItem::PrivacyItem(const Json::Value& config_,
: Gtk::Revealer(), : Gtk::Revealer(),
privacy_type(privacy_type_), privacy_type(privacy_type_),
mutex_(), mutex_(),
signal_conn(),
box_(Gtk::ORIENTATION_HORIZONTAL, 0), box_(Gtk::ORIENTATION_HORIZONTAL, 0),
icon_() { icon_() {
switch (privacy_type) { switch (privacy_type) {
@ -75,6 +74,10 @@ PrivacyItem::PrivacyItem(const Json::Value& config_,
} }
icon_.set_from_icon_name(iconName, Gtk::ICON_SIZE_INVALID); 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 // Don't show by default
set_reveal_child(true); set_reveal_child(true);
set_visible(false); set_visible(false);
@ -82,6 +85,18 @@ PrivacyItem::PrivacyItem(const Json::Value& config_,
bool PrivacyItem::is_enabled() { return enabled; } 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) { void PrivacyItem::set_in_use(bool in_use) {
mutex_.lock(); mutex_.lock();
if (this->in_use == in_use && init) { if (this->in_use == in_use && init) {
@ -90,29 +105,19 @@ void PrivacyItem::set_in_use(bool in_use) {
} }
if (init) { 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; this->in_use = in_use;
if (this->in_use) { if (this->in_use) {
set_visible(true); set_visible(true);
signal_conn = Glib::signal_timeout().connect(sigc::track_obj( // The `on_map_changed` callback will call `set_reveal_child(true)`
[this] { // when the widget is realized so we don't need to call that here.
set_reveal_child(true); // This fixes a bug where the revealer wouldn't start the animation
return false; // due to us changing the visibility at the same time.
},
*this),
0);
} else { } else {
set_reveal_child(false); set_reveal_child(false);
signal_conn = Glib::signal_timeout().connect(sigc::track_obj( // The `on_child_revealed_changed` callback will call `set_visible(false)`
[this] { // when the animation has finished so we don't need to call that here.
set_visible(false); // We do this so that the widget gets hidden after the revealer hide animation
return false; // has finished.
},
*this),
get_transition_duration());
} }
} else { } else {
set_visible(false); set_visible(false);