From 1368d4a77b4b55b4b58a645983972cd78f17f157 Mon Sep 17 00:00:00 2001 From: thewh1teagle <61390950+thewh1teagle@users.noreply.github.com> Date: Fri, 17 Jan 2025 11:47:45 +0200 Subject: [PATCH] feat(window-state): Add filter callback for excluding windows from tracking --- plugins/window-state/src/lib.rs | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/plugins/window-state/src/lib.rs b/plugins/window-state/src/lib.rs index b23b6bce..8e373d7a 100644 --- a/plugins/window-state/src/lib.rs +++ b/plugins/window-state/src/lib.rs @@ -27,6 +27,7 @@ use std::{ mod cmd; type LabelMapperFn = dyn Fn(&str) -> &str + Send + Sync; +type FilterCallbackFn = dyn Fn(&str) -> bool + Send + Sync; /// Default filename used to store window state. /// @@ -322,7 +323,7 @@ impl WindowExtInternal for Window { #[derive(Default)] pub struct Builder { denylist: HashSet, - denylist_patterns: Vec, + filter_callback: Option>>, skip_initial_state: HashSet, state_flags: StateFlags, map_label: Option>, @@ -353,15 +354,14 @@ impl Builder { self } - /// Sets a list of windows that shouldn't be tracked and managed by this plugin using glob patterns - /// For example, you can denylist windows using wildcards. - pub fn with_denylist_glob(mut self, denylist: &[&str]) -> Result { - let mut denylist_patterns = Vec::new(); - for pattern in denylist { - denylist_patterns.push(glob::Pattern::new(pattern)?); - } - self.denylist_patterns = denylist_patterns; - Ok(self) + /// Sets a filter callback to exclude specific windows from being tracked. + /// Return `true` to save the state, or `false` to skip and not save it. + pub fn with_filter(mut self, filter_callback: F) -> Self + where + F: Fn(&str) -> bool + Send + Sync + 'static, + { + self.filter_callback = Some(Arc::new(Mutex::new(filter_callback))); + self } /// Adds the given window label to a list of windows to skip initial state restore. @@ -432,9 +432,11 @@ impl Builder { return; } - // Check deny list patterns - for pattern in &self.denylist_patterns { - if pattern.matches(label) { + // Check deny list callback + if let Some(filter_callback) = &self.filter_callback { + let filter_callback = filter_callback.lock().unwrap(); + // Don't save the state if the callback returns false + if !filter_callback(label) { return; } }