Fix/signal disconnect (#445)
Co-authored-by: elkowar <5300871+elkowar@users.noreply.github.com>
This commit is contained in:
parent
92e389130f
commit
52e14d8263
1 changed files with 11 additions and 20 deletions
|
@ -11,11 +11,10 @@ use anyhow::{anyhow, Context, Result};
|
|||
use codespan_reporting::diagnostic::Severity;
|
||||
use eww_shared_util::Spanned;
|
||||
use gdk::{ModifierType, NotifyType};
|
||||
use glib::signal::SignalHandlerId;
|
||||
use glib::translate::FromGlib;
|
||||
use gtk::{self, glib, prelude::*, DestDefaults, TargetEntry, TargetList};
|
||||
use itertools::Itertools;
|
||||
use once_cell::sync::Lazy;
|
||||
use std::hash::Hasher;
|
||||
|
||||
use std::{
|
||||
cell::RefCell,
|
||||
|
@ -38,24 +37,16 @@ use yuck::{
|
|||
/// thus not connecting a new handler unless the condition is met.
|
||||
macro_rules! connect_signal_handler {
|
||||
($widget:ident, if $cond:expr, $connect_expr:expr) => {{
|
||||
// static hashmap of widget hashes to signal handler ids.
|
||||
// For each use of connect_signal_handler (which represents a specific widget-type and a specific attribute),
|
||||
// we need to remember the handlers of all actual widget instances that use this field.
|
||||
// We can't go by instance by just using a static, and we can't really go by field/attribute without static -- so we do both.
|
||||
static ID: Lazy<std::sync::Mutex<HashMap<u64, SignalHandlerId>>> = Lazy::new(|| std::sync::Mutex::new(HashMap::new()));
|
||||
let widget_hash = {
|
||||
let mut hasher = std::collections::hash_map::DefaultHasher::new();
|
||||
std::hash::Hash::hash(&$widget, &mut hasher);
|
||||
hasher.finish()
|
||||
};
|
||||
let old = if $cond {
|
||||
let new_id = $connect_expr;
|
||||
ID.lock().unwrap().insert(widget_hash, new_id)
|
||||
} else {
|
||||
ID.lock().unwrap().remove(&widget_hash)
|
||||
};
|
||||
unsafe {
|
||||
let key = ::std::concat!("signal-handler:", ::std::line!());
|
||||
let old = $widget.data::<gtk::glib::SignalHandlerId>(key);
|
||||
|
||||
if let Some(old) = old {
|
||||
$widget.disconnect(old);
|
||||
let a = old.as_ref().as_raw();
|
||||
$widget.disconnect(gtk::glib::SignalHandlerId::from_glib(a));
|
||||
}
|
||||
|
||||
$widget.set_data::<gtk::glib::SignalHandlerId>(key, $connect_expr);
|
||||
}
|
||||
}};
|
||||
($widget:ident, $connect_expr:expr) => {{
|
||||
|
|
Loading…
Add table
Reference in a new issue