Skip to content

Commit 5dbb6f8

Browse files
committed
manual window state managemenet
1 parent 62b1c21 commit 5dbb6f8

File tree

6 files changed

+67
-90
lines changed

6 files changed

+67
-90
lines changed

src-tauri/Cargo.lock

-16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src-tauri/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
log = "0.4"
2424
tauri = { version = "2.0.6", features = ["macos-private-api"] }
2525
tauri-plugin-log = "2.0.0-rc"
26-
tauri-plugin-window-state = "2.0.0"
2726
tauri-plugin-dialog = "2"
2827
tauri-plugin-fs = "2"
2928
tauri-plugin-clipboard-manager = "2"

src-tauri/gen/schemas/acl-manifests.json

+1-1
Large diffs are not rendered by default.

src-tauri/gen/schemas/desktop-schema.json

-35
Original file line numberDiff line numberDiff line change
@@ -4867,41 +4867,6 @@
48674867
"description": "Denies the values command without any pre-configured scope.",
48684868
"type": "string",
48694869
"const": "store:deny-values"
4870-
},
4871-
{
4872-
"description": "This permission set configures what kind of\noperations are available from the window state plugin.\n\n#### Granted Permissions\n\nAll operations are enabled by default.\n\n",
4873-
"type": "string",
4874-
"const": "window-state:default"
4875-
},
4876-
{
4877-
"description": "Enables the filename command without any pre-configured scope.",
4878-
"type": "string",
4879-
"const": "window-state:allow-filename"
4880-
},
4881-
{
4882-
"description": "Enables the restore_state command without any pre-configured scope.",
4883-
"type": "string",
4884-
"const": "window-state:allow-restore-state"
4885-
},
4886-
{
4887-
"description": "Enables the save_window_state command without any pre-configured scope.",
4888-
"type": "string",
4889-
"const": "window-state:allow-save-window-state"
4890-
},
4891-
{
4892-
"description": "Denies the filename command without any pre-configured scope.",
4893-
"type": "string",
4894-
"const": "window-state:deny-filename"
4895-
},
4896-
{
4897-
"description": "Denies the restore_state command without any pre-configured scope.",
4898-
"type": "string",
4899-
"const": "window-state:deny-restore-state"
4900-
},
4901-
{
4902-
"description": "Denies the save_window_state command without any pre-configured scope.",
4903-
"type": "string",
4904-
"const": "window-state:deny-save-window-state"
49054870
}
49064871
]
49074872
},

src-tauri/gen/schemas/macOS-schema.json

-35
Original file line numberDiff line numberDiff line change
@@ -4867,41 +4867,6 @@
48674867
"description": "Denies the values command without any pre-configured scope.",
48684868
"type": "string",
48694869
"const": "store:deny-values"
4870-
},
4871-
{
4872-
"description": "This permission set configures what kind of\noperations are available from the window state plugin.\n\n#### Granted Permissions\n\nAll operations are enabled by default.\n\n",
4873-
"type": "string",
4874-
"const": "window-state:default"
4875-
},
4876-
{
4877-
"description": "Enables the filename command without any pre-configured scope.",
4878-
"type": "string",
4879-
"const": "window-state:allow-filename"
4880-
},
4881-
{
4882-
"description": "Enables the restore_state command without any pre-configured scope.",
4883-
"type": "string",
4884-
"const": "window-state:allow-restore-state"
4885-
},
4886-
{
4887-
"description": "Enables the save_window_state command without any pre-configured scope.",
4888-
"type": "string",
4889-
"const": "window-state:allow-save-window-state"
4890-
},
4891-
{
4892-
"description": "Denies the filename command without any pre-configured scope.",
4893-
"type": "string",
4894-
"const": "window-state:deny-filename"
4895-
},
4896-
{
4897-
"description": "Denies the restore_state command without any pre-configured scope.",
4898-
"type": "string",
4899-
"const": "window-state:deny-restore-state"
4900-
},
4901-
{
4902-
"description": "Denies the save_window_state command without any pre-configured scope.",
4903-
"type": "string",
4904-
"const": "window-state:deny-save-window-state"
49054870
}
49064871
]
49074872
},

src-tauri/src/lib.rs

+66-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
use tauri::{Manager, WindowEvent, PhysicalPosition, PhysicalSize, Runtime};
2+
use tauri_plugin_store::{Store, StoreBuilder};
3+
use serde::{Serialize, Deserialize};
4+
use std::path::PathBuf;
5+
6+
#[derive(Debug, Serialize, Deserialize, Clone)]
7+
struct WindowState {
8+
width: f64,
9+
height: f64,
10+
x: i32,
11+
y: i32,
12+
}
13+
114
#[cfg_attr(mobile, tauri::mobile_entry_point)]
215
pub fn run() {
316
#[allow(unused_mut)]
@@ -17,8 +30,47 @@ pub fn run() {
1730

1831
#[cfg(desktop)]
1932
{
20-
app.handle()
21-
.plugin(tauri_plugin_window_state::Builder::default().build())?;
33+
// Initialize store
34+
let path = PathBuf::from("window.dat");
35+
let store = StoreBuilder::new(app.handle(), path)
36+
.build();
37+
38+
let window = app.get_webview_window("main").unwrap();
39+
40+
// Restore window state
41+
if let Ok(store) = store {
42+
if let Some(state) = store.get("window_state") {
43+
if let Ok(window_state) = serde_json::from_value::<WindowState>(state) {
44+
window.set_position(PhysicalPosition::new(
45+
window_state.x,
46+
window_state.y,
47+
))?;
48+
window.set_size(PhysicalSize::new(
49+
window_state.width as u32,
50+
window_state.height as u32,
51+
))?;
52+
}
53+
}
54+
55+
// Save window state when window is moved or resized
56+
let store_clone = store.clone();
57+
let window_clone = window.clone();
58+
window.on_window_event(move |event| {
59+
match event {
60+
WindowEvent::Moved(position) => {
61+
if let Ok(size) = window_clone.outer_size() {
62+
save_window_state(&store_clone, position.x, position.y, size.width as f64, size.height as f64);
63+
}
64+
},
65+
WindowEvent::Resized(size) => {
66+
if let Ok(position) = window_clone.outer_position() {
67+
save_window_state(&store_clone, position.x, position.y, size.width as f64, size.height as f64);
68+
}
69+
},
70+
_ => {}
71+
}
72+
});
73+
}
2274
}
2375

2476
Ok(())
@@ -28,3 +80,15 @@ pub fn run() {
2880
.run(tauri::generate_context!())
2981
.expect("error while running tauri application");
3082
}
83+
84+
fn save_window_state<R: Runtime>(store: &Store<R>, x: i32, y: i32, width: f64, height: f64) {
85+
let window_state = WindowState {
86+
width,
87+
height,
88+
x,
89+
y,
90+
};
91+
92+
let _ = store.set("window_state", serde_json::to_value(window_state).unwrap());
93+
let _ = store.save();
94+
}

0 commit comments

Comments
 (0)