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"
dependencies = [
"bitflags 2.7.0",
"glob",
"log",
"serde",
"serde_json",

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

@ -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>;
@ -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>>,
@ -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.
@ -413,9 +421,12 @@ impl Builder {
.map(|map| map(window.label()))
.unwrap_or_else(|| window.label());
if self.denylist.contains(label) {
for pattern in &self.denylist {
if pattern.matches(label) {
return;
}
}
if !self.skip_initial_state.contains(label) {
let _ = window.restore_state(self.state_flags);

Loading…
Cancel
Save