Simplified the privacy_item hiding/showing logic

pull/2612/head
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;
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

View File

@ -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] {

View File

@ -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);