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;
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<R: Runtime> WindowExtInternal for Window<R> {
#[derive(Default)]
pub struct Builder {
denylist: HashSet<String>,
denylist_patterns: Vec<glob::Pattern>,
filter_callback: Option<Arc<Mutex<FilterCallbackFn>>>,
skip_initial_state: HashSet<String>,
state_flags: StateFlags,
map_label: Option<Box<LabelMapperFn>>,
@ -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<Self> {
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<F>(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;
}
}

Loading…
Cancel
Save