Skip to content

Commit 0ec895c

Browse files
authored
feat(window-state): add filter callback for excluding windows from tracking (#2330)
* feat(window-state): Add glob pattern support to denylist for flexible window management * changefile fix(window-state): Remove unnecessary reference in denylist pattern processing chore(changes): Update window-state and window-state-js versioning for glob pattern support docs(window-state): Update glob pattern denylist documentation for clarity * refactor(window-state): Simplify denylist handling and error management in with_denylist method * feat: add with_denylist_glob * feat(window-state): Add filter callback for excluding windows from tracking * refactor(window-state): Remove unused Glob error variant from Error enum * refactor(window-state): Change filter_callback from Arc<Mutex<FilterCallbackFn>> to Box<FilterCallbackFn> * refactor(window-state): Remove glob dependency from Cargo.toml and Cargo.lock * feat(window-state): Introduce filter callback for dynamic window exclusion * Update .changes/window-state-filter-callback.md
1 parent 406e6f4 commit 0ec895c

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
window-state: patch
3+
window-state-js: patch
4+
---
5+
6+
Add `Builder::with_filter` callback to exclude specific windows from saving their state. This allows for more flexibility by enabling dynamic exclusion of windows based on custom logic.

plugins/window-state/src/lib.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use std::{
2727
mod cmd;
2828

2929
type LabelMapperFn = dyn Fn(&str) -> &str + Send + Sync;
30+
type FilterCallbackFn = dyn Fn(&str) -> bool + Send + Sync;
3031

3132
/// Default filename used to store window state.
3233
///
@@ -320,6 +321,7 @@ impl<R: Runtime> WindowExtInternal for Window<R> {
320321
#[derive(Default)]
321322
pub struct Builder {
322323
denylist: HashSet<String>,
324+
filter_callback: Option<Box<FilterCallbackFn>>,
323325
skip_initial_state: HashSet<String>,
324326
state_flags: StateFlags,
325327
map_label: Option<Box<LabelMapperFn>>,
@@ -344,12 +346,22 @@ impl Builder {
344346
}
345347

346348
/// Sets a list of windows that shouldn't be tracked and managed by this plugin
347-
/// for example splash screen windows.
349+
/// For example, splash screen windows.
348350
pub fn with_denylist(mut self, denylist: &[&str]) -> Self {
349351
self.denylist = denylist.iter().map(|l| l.to_string()).collect();
350352
self
351353
}
352354

355+
/// Sets a filter callback to exclude specific windows from being tracked.
356+
/// Return `true` to save the state, or `false` to skip and not save it.
357+
pub fn with_filter<F>(mut self, filter_callback: F) -> Self
358+
where
359+
F: Fn(&str) -> bool + Send + Sync + 'static,
360+
{
361+
self.filter_callback = Some(Box::new(filter_callback));
362+
self
363+
}
364+
353365
/// Adds the given window label to a list of windows to skip initial state restore.
354366
pub fn skip_initial_state(mut self, label: &str) -> Self {
355367
self.skip_initial_state.insert(label.into());
@@ -413,10 +425,19 @@ impl Builder {
413425
.map(|map| map(window.label()))
414426
.unwrap_or_else(|| window.label());
415427

428+
// Check deny list names
416429
if self.denylist.contains(label) {
417430
return;
418431
}
419432

433+
// Check deny list callback
434+
if let Some(filter_callback) = &self.filter_callback {
435+
// Don't save the state if the callback returns false
436+
if !filter_callback(label) {
437+
return;
438+
}
439+
}
440+
420441
if !self.skip_initial_state.contains(label) {
421442
let _ = window.restore_state(self.state_flags);
422443
}

0 commit comments

Comments
 (0)