Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(window-state): add Builder::map_label method #1497

Merged
merged 6 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/window-state-map-label.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"window-state": patch
---

Add `map_label` option to transform the window label before using it internally, based on [amrbashir's suggestion](https://github.com/tauri-apps/plugins-workspace/pull/531#pullrequestreview-1566282300).
ferreira-tb marked this conversation as resolved.
Show resolved Hide resolved
34 changes: 28 additions & 6 deletions plugins/window-state/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ impl Default for StateFlags {

struct PluginState {
filename: String,
map_label: Option<Box<dyn (Fn(&str) -> &str) + Send + Sync>>,
}

#[derive(Debug, Deserialize, Serialize, PartialEq)]
Expand Down Expand Up @@ -148,13 +149,20 @@ impl<R: Runtime> WindowExt for WebviewWindow<R> {
}
impl<R: Runtime> WindowExt for Window<R> {
fn restore_state(&self, flags: StateFlags) -> tauri::Result<()> {
let plugin_state = self.app_handle().state::<PluginState>();
let label = plugin_state
.map_label
.as_ref()
.map(|map| map(self.label()))
.unwrap_or_else(|| self.label());

let cache = self.state::<WindowStateCache>();
let mut c = cache.0.lock().unwrap();

let mut should_show = true;

if let Some(state) = c
.get(self.label())
.get(label)
.filter(|state| state != &&WindowState::default())
{
if flags.contains(StateFlags::DECORATIONS) {
Expand Down Expand Up @@ -235,7 +243,7 @@ impl<R: Runtime> WindowExt for Window<R> {
metadata.fullscreen = self.is_fullscreen()?;
}

c.insert(self.label().into(), metadata);
c.insert(label.into(), metadata);
}

if flags.contains(StateFlags::VISIBLE) && should_show {
Expand Down Expand Up @@ -309,6 +317,7 @@ pub struct Builder {
denylist: HashSet<String>,
skip_initial_state: HashSet<String>,
state_flags: StateFlags,
map_label: Option<Box<dyn (Fn(&str) -> &str) + Send + Sync>>,
ferreira-tb marked this conversation as resolved.
Show resolved Hide resolved
filename: Option<String>,
}

Expand Down Expand Up @@ -342,9 +351,15 @@ impl Builder {
self
}

pub fn map_label(mut self, map_fn: fn(&str) -> &str) -> Self {
ferreira-tb marked this conversation as resolved.
Show resolved Hide resolved
ferreira-tb marked this conversation as resolved.
Show resolved Hide resolved
self.map_label = Some(Box::new(map_fn));
self
}

pub fn build<R: Runtime>(self) -> TauriPlugin<R> {
let flags = self.state_flags;
let filename = self.filename.unwrap_or_else(|| DEFAULT_FILENAME.into());
let map_label = self.map_label;

PluginBuilder::new("window-state")
.invoke_handler(tauri::generate_handler![
Expand Down Expand Up @@ -372,21 +387,28 @@ impl Builder {
Default::default()
};
app.manage(WindowStateCache(cache));
app.manage(PluginState { filename });
app.manage(PluginState { filename, map_label });
Ok(())
})
.on_window_ready(move |window| {
if self.denylist.contains(window.label()) {
let plugin_state = window.app_handle().state::<PluginState>();
let label = plugin_state
.map_label
.as_ref()
.map(|map| map(window.label()))
.unwrap_or_else(|| window.label());

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

if !self.skip_initial_state.contains(window.label()) {
if !self.skip_initial_state.contains(label) {
let _ = window.restore_state(self.state_flags);
}

let cache = window.state::<WindowStateCache>();
let cache = cache.0.clone();
let label = window.label().to_string();
let label = label.to_string();
let window_clone = window.clone();
let flags = self.state_flags;

Expand Down
Loading