diff --git a/plugins/window-state/src/cmd.rs b/plugins/window-state/src/cmd.rs new file mode 100644 index 00000000..65690db4 --- /dev/null +++ b/plugins/window-state/src/cmd.rs @@ -0,0 +1,28 @@ +use crate::{AppHandleExt, StateFlags, WindowExt}; +use tauri::{command, AppHandle, Manager, Runtime}; + +#[command] +pub async fn save_window_state( + app: AppHandle, + flags: u32, +) -> std::result::Result<(), String> { + let flags = StateFlags::from_bits(flags) + .ok_or_else(|| format!("Invalid state flags bits: {}", flags))?; + app.save_window_state(flags).map_err(|e| e.to_string())?; + Ok(()) +} + +#[command] +pub async fn restore_state( + app: AppHandle, + label: String, + flags: u32, +) -> std::result::Result<(), String> { + let flags = StateFlags::from_bits(flags) + .ok_or_else(|| format!("Invalid state flags bits: {}", flags))?; + app.get_window(&label) + .ok_or_else(|| format!("Couldn't find window with label: {}", label))? + .restore_state(flags) + .map_err(|e| e.to_string())?; + Ok(()) +} diff --git a/plugins/window-state/src/lib.rs b/plugins/window-state/src/lib.rs index 34470b1d..2a23f35d 100644 --- a/plugins/window-state/src/lib.rs +++ b/plugins/window-state/src/lib.rs @@ -6,8 +6,8 @@ use bitflags::bitflags; use serde::{Deserialize, Serialize}; use tauri::{ plugin::{Builder as PluginBuilder, TauriPlugin}, - AppHandle, LogicalSize, Manager, Monitor, PhysicalPosition, PhysicalSize, RunEvent, Runtime, - Window, WindowEvent, + LogicalSize, Manager, Monitor, PhysicalPosition, PhysicalSize, RunEvent, Runtime, Window, + WindowEvent, }; use std::{ @@ -17,6 +17,8 @@ use std::{ sync::{Arc, Mutex}, }; +mod cmd; + pub const STATE_FILENAME: &str = ".window-state"; #[derive(Debug, thiserror::Error)] @@ -248,32 +250,6 @@ impl WindowExtInternal for Window { } } -#[tauri::command] -async fn js_save_window_state( - app: AppHandle, - flags: u32, -) -> std::result::Result<(), String> { - let flags = StateFlags::from_bits(flags) - .ok_or_else(|| format!("Invalid state flags bits: {}", flags))?; - app.save_window_state(flags).map_err(|e| e.to_string())?; - Ok(()) -} - -#[tauri::command] -async fn js_restore_state( - app: AppHandle, - label: String, - flags: u32, -) -> std::result::Result<(), String> { - let flags = StateFlags::from_bits(flags) - .ok_or_else(|| format!("Invalid state flags bits: {}", flags))?; - app.get_window(&label) - .ok_or_else(|| format!("Couldn't find window with label: {}", label))? - .restore_state(flags) - .map_err(|e| e.to_string())?; - Ok(()) -} - #[derive(Default)] pub struct Builder { denylist: HashSet, @@ -305,8 +281,8 @@ impl Builder { let flags = self.state_flags; PluginBuilder::new("window-state") .invoke_handler(tauri::generate_handler![ - js_save_window_state, - js_restore_state + cmd::save_window_state, + cmd::restore_state ]) .setup(|app| { let cache: Arc>> = if let Some(app_dir) =