From 2847f11d3ac9a339c022fb6efa0e4c3a2218ded1 Mon Sep 17 00:00:00 2001 From: thewh1teagle <61390950+thewh1teagle@users.noreply.github.com> Date: Fri, 17 Jan 2025 01:07:30 +0200 Subject: [PATCH] feat(window-state): Add glob pattern support to denylist for flexible window management --- Cargo.lock | 1 + plugins/window-state/Cargo.toml | 1 + plugins/window-state/src/lib.rs | 25 ++++++++++++++++++------- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index be7bddebc0..6fa6415856 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6919,6 +6919,7 @@ name = "tauri-plugin-window-state" version = "2.2.0" dependencies = [ "bitflags 2.7.0", + "glob", "log", "serde", "serde_json", diff --git a/plugins/window-state/Cargo.toml b/plugins/window-state/Cargo.toml index 3ea0daaf44..447ed4c57f 100644 --- a/plugins/window-state/Cargo.toml +++ b/plugins/window-state/Cargo.toml @@ -29,4 +29,5 @@ serde_json = { workspace = true } tauri = { workspace = true } log = { workspace = true } thiserror = { workspace = true } +glob = { workspace = true } bitflags = "2" diff --git a/plugins/window-state/src/lib.rs b/plugins/window-state/src/lib.rs index 8c675d4dab..9f0da1e0ae 100644 --- a/plugins/window-state/src/lib.rs +++ b/plugins/window-state/src/lib.rs @@ -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 = std::result::Result; @@ -319,7 +321,7 @@ impl WindowExtInternal for Window { #[derive(Default)] pub struct Builder { - denylist: HashSet, + denylist: Vec, skip_initial_state: HashSet, state_flags: StateFlags, map_label: Option>, @@ -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 { + 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. @@ -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) {