feat(window-state): Add filter callback for excluding windows from tracking

pull/2330/head
thewh1teagle 6 months ago
parent ff66ed3e82
commit 1368d4a77b
No known key found for this signature in database

@ -27,6 +27,7 @@ use std::{
mod cmd; mod cmd;
type LabelMapperFn = dyn Fn(&str) -> &str + Send + Sync; type LabelMapperFn = dyn Fn(&str) -> &str + Send + Sync;
type FilterCallbackFn = dyn Fn(&str) -> bool + Send + Sync;
/// Default filename used to store window state. /// Default filename used to store window state.
/// ///
@ -322,7 +323,7 @@ impl<R: Runtime> WindowExtInternal for Window<R> {
#[derive(Default)] #[derive(Default)]
pub struct Builder { pub struct Builder {
denylist: HashSet<String>, denylist: HashSet<String>,
denylist_patterns: Vec<glob::Pattern>, filter_callback: Option<Arc<Mutex<FilterCallbackFn>>>,
skip_initial_state: HashSet<String>, skip_initial_state: HashSet<String>,
state_flags: StateFlags, state_flags: StateFlags,
map_label: Option<Box<LabelMapperFn>>, map_label: Option<Box<LabelMapperFn>>,
@ -353,15 +354,14 @@ impl Builder {
self self
} }
/// Sets a list of windows that shouldn't be tracked and managed by this plugin using glob patterns /// Sets a filter callback to exclude specific windows from being tracked.
/// For example, you can denylist windows using wildcards. /// Return `true` to save the state, or `false` to skip and not save it.
pub fn with_denylist_glob(mut self, denylist: &[&str]) -> Result<Self> { pub fn with_filter<F>(mut self, filter_callback: F) -> Self
let mut denylist_patterns = Vec::new(); where
for pattern in denylist { F: Fn(&str) -> bool + Send + Sync + 'static,
denylist_patterns.push(glob::Pattern::new(pattern)?); {
} self.filter_callback = Some(Arc::new(Mutex::new(filter_callback)));
self.denylist_patterns = denylist_patterns; self
Ok(self)
} }
/// Adds the given window label to a list of windows to skip initial state restore. /// Adds the given window label to a list of windows to skip initial state restore.
@ -432,9 +432,11 @@ impl Builder {
return; return;
} }
// Check deny list patterns // Check deny list callback
for pattern in &self.denylist_patterns { if let Some(filter_callback) = &self.filter_callback {
if pattern.matches(label) { let filter_callback = filter_callback.lock().unwrap();
// Don't save the state if the callback returns false
if !filter_callback(label) {
return; return;
} }
} }

Loading…
Cancel
Save