-
Notifications
You must be signed in to change notification settings - Fork 373
Open
Labels
Description
CodeScene considers this plugin to have a frequently changed & high complexity method:
https://codescene.io/projects/33264/jobs/771244/results/files/hotspots?file-name=tauri-plugin-window-state%2Fsrc%2Flib.rs
plugins-workspace/plugins/window-state/src/lib.rs
Lines 194 to 289 in 27b7341
pub fn build<R: Runtime>(self) -> TauriPlugin<R> { | |
PluginBuilder::new("window-state") | |
.setup(|app| { | |
let cache: Arc<Mutex<HashMap<String, WindowMetadata>>> = if let Some(app_dir) = | |
app.path_resolver().app_config_dir() | |
{ | |
let state_path = app_dir.join(STATE_FILENAME); | |
if state_path.exists() { | |
Arc::new(Mutex::new( | |
tauri::api::file::read_binary(state_path) | |
.map_err(Error::TauriApi) | |
.and_then(|state| bincode::deserialize(&state).map_err(Into::into)) | |
.unwrap_or_default(), | |
)) | |
} else { | |
Default::default() | |
} | |
} else { | |
Default::default() | |
}; | |
app.manage(WindowStateCache(cache)); | |
Ok(()) | |
}) | |
.on_webview_ready(move |window| { | |
if self.denylist.contains(window.label()) { | |
return; | |
} | |
if !self.skip_initial_state.contains(window.label()) { | |
let _ = window.restore_state(self.show_mode); | |
} | |
let cache = window.state::<WindowStateCache>(); | |
let cache = cache.0.clone(); | |
let label = window.label().to_string(); | |
let window_clone = window.clone(); | |
window.on_window_event(move |e| match e { | |
WindowEvent::Moved(position) => { | |
let mut c = cache.lock().unwrap(); | |
if let Some(state) = c.get_mut(&label) { | |
let is_maximized = window_clone.is_maximized().unwrap_or(false); | |
state.maximized = is_maximized; | |
if let Some(monitor) = window_clone.current_monitor().unwrap() { | |
state.monitor = | |
monitor.name().map(ToString::to_string).unwrap_or_default(); | |
let monitor_position = monitor.position(); | |
// save only window positions that are inside the current monitor | |
if position.x > monitor_position.x | |
&& position.y > monitor_position.y | |
&& !is_maximized | |
{ | |
state.x = position.x; | |
state.y = position.y; | |
}; | |
}; | |
} | |
} | |
WindowEvent::Resized(size) => { | |
let scale_factor = window_clone | |
.current_monitor() | |
.ok() | |
.map(|m| m.map(|m| m.scale_factor()).unwrap_or(1.)) | |
.unwrap_or(1.); | |
let size = size.to_logical(scale_factor); | |
let mut c = cache.lock().unwrap(); | |
if let Some(state) = c.get_mut(&label) { | |
let is_maximized = window_clone.is_maximized().unwrap_or(false); | |
let is_fullscreen = window_clone.is_fullscreen().unwrap_or(false); | |
state.decorated = window_clone.is_decorated().unwrap_or(true); | |
state.maximized = is_maximized; | |
state.fullscreen = is_fullscreen; | |
// It doesn't make sense to save a window with 0 height or width | |
if size.width > 0. && size.height > 0. && !is_maximized { | |
state.width = size.width; | |
state.height = size.height; | |
} | |
} | |
} | |
WindowEvent::CloseRequested { .. } => { | |
let mut c = cache.lock().unwrap(); | |
if let Some(state) = c.get_mut(&label) { | |
state.visible = window_clone.is_visible().unwrap_or(true); | |
} | |
} | |
_ => {} | |
}); | |
}) | |
.on_event(|app, event| { | |
if let RunEvent::Exit = event { | |
let _ = app.save_window_state(); | |
} | |
}) | |
.build() | |
} |
Metadata
Metadata
Assignees
Labels
Type
Projects
Status
📬Proposal