feat(window-state): Add glob pattern support to denylist for flexible window management

pull/2330/head
thewh1teagle 6 months ago
parent 406e6f484c
commit 2847f11d3a
No known key found for this signature in database

1
Cargo.lock generated

@ -6919,6 +6919,7 @@ name = "tauri-plugin-window-state"
version = "2.2.0" version = "2.2.0"
dependencies = [ dependencies = [
"bitflags 2.7.0", "bitflags 2.7.0",
"glob",
"log", "log",
"serde", "serde",
"serde_json", "serde_json",

@ -29,4 +29,5 @@ serde_json = { workspace = true }
tauri = { workspace = true } tauri = { workspace = true }
log = { workspace = true } log = { workspace = true }
thiserror = { workspace = true } thiserror = { workspace = true }
glob = { workspace = true }
bitflags = "2" bitflags = "2"

@ -41,6 +41,8 @@ pub enum Error {
Tauri(#[from] tauri::Error), Tauri(#[from] tauri::Error),
#[error(transparent)] #[error(transparent)]
SerdeJson(#[from] serde_json::Error), SerdeJson(#[from] serde_json::Error),
#[error(transparent)]
Glob(#[from] glob::PatternError),
} }
pub type Result<T> = std::result::Result<T, Error>; pub type Result<T> = std::result::Result<T, Error>;
@ -319,7 +321,7 @@ impl<R: Runtime> WindowExtInternal for Window<R> {
#[derive(Default)] #[derive(Default)]
pub struct Builder { pub struct Builder {
denylist: HashSet<String>, denylist: Vec<glob::Pattern>,
skip_initial_state: HashSet<String>, skip_initial_state: HashSet<String>,
state_flags: StateFlags, state_flags: StateFlags,
map_label: Option<Box<LabelMapperFn>>, map_label: Option<Box<LabelMapperFn>>,
@ -344,10 +346,16 @@ impl Builder {
} }
/// Sets a list of windows that shouldn't be tracked and managed by this plugin /// Sets a list of windows that shouldn't be tracked and managed by this plugin
/// for example splash screen windows. /// For example, splash screen windows. It also supports glob patterns for flexible window matching.
pub fn with_denylist(mut self, denylist: &[&str]) -> Self { pub fn with_denylist(mut self, denylist: &mut [&str]) -> Result<Self> {
self.denylist = denylist.iter().map(|l| l.to_string()).collect(); denylist.sort();
self
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. /// 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())) .map(|map| map(window.label()))
.unwrap_or_else(|| 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) { if !self.skip_initial_state.contains(label) {

Loading…
Cancel
Save