Skip to content

Commit

Permalink
feat(window-state): Add glob pattern support to denylist for flexible…
Browse files Browse the repository at this point in the history
… window management
  • Loading branch information
thewh1teagle committed Jan 16, 2025
1 parent 406e6f4 commit 2847f11
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions plugins/window-state/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ serde_json = { workspace = true }
tauri = { workspace = true }
log = { workspace = true }
thiserror = { workspace = true }
glob = { workspace = true }
bitflags = "2"
25 changes: 18 additions & 7 deletions plugins/window-state/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ pub enum Error {
Tauri(#[from] tauri::Error),
#[error(transparent)]
SerdeJson(#[from] serde_json::Error),
#[error(transparent)]
Glob(#[from] glob::PatternError),
}

pub type Result<T> = std::result::Result<T, Error>;
Expand Down Expand Up @@ -319,7 +321,7 @@ impl<R: Runtime> WindowExtInternal for Window<R> {

#[derive(Default)]
pub struct Builder {
denylist: HashSet<String>,
denylist: Vec<glob::Pattern>,
skip_initial_state: HashSet<String>,
state_flags: StateFlags,
map_label: Option<Box<LabelMapperFn>>,
Expand All @@ -344,10 +346,16 @@ impl Builder {
}

/// Sets a list of windows that shouldn't be tracked and managed by this plugin
/// for example splash screen windows.
pub fn with_denylist(mut self, denylist: &[&str]) -> Self {
self.denylist = denylist.iter().map(|l| l.to_string()).collect();
self
/// For example, splash screen windows. It also supports glob patterns for flexible window matching.
pub fn with_denylist(mut self, denylist: &mut [&str]) -> Result<Self> {
denylist.sort();

let mut denylist_patterns = Vec::new();
for pattern in denylist {
denylist_patterns.push(glob::Pattern::new(&pattern)?);
}
self.denylist = denylist_patterns;
Ok(self)
}

/// Adds the given window label to a list of windows to skip initial state restore.
Expand Down Expand Up @@ -413,8 +421,11 @@ impl Builder {
.map(|map| map(window.label()))
.unwrap_or_else(|| window.label());

if self.denylist.contains(label) {
return;

for pattern in &self.denylist {
if pattern.matches(label) {
return;
}
}

if !self.skip_initial_state.contains(label) {
Expand Down

0 comments on commit 2847f11

Please sign in to comment.