Skip to content

Commit e6d8315

Browse files
authored
fix(desktop): throttle window state persistence (#11746)
1 parent 784a17f commit e6d8315

2 files changed

Lines changed: 42 additions & 7 deletions

File tree

packages/desktop/src-tauri/src/lib.rs

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,18 @@ use tauri_plugin_deep_link::DeepLinkExt;
2323
use tauri_plugin_dialog::{DialogExt, MessageDialogButtons, MessageDialogResult};
2424
use tauri_plugin_shell::process::{CommandChild, CommandEvent};
2525
use tauri_plugin_store::StoreExt;
26-
use tokio::sync::oneshot;
26+
use tauri_plugin_window_state::{AppHandleExt, StateFlags};
27+
use tokio::sync::{mpsc, oneshot};
2728

2829
use crate::window_customizer::PinchZoomDisablePlugin;
2930

3031
const SETTINGS_STORE: &str = "opencode.settings.dat";
3132
const DEFAULT_SERVER_URL_KEY: &str = "defaultServerUrl";
3233

34+
fn window_state_flags() -> StateFlags {
35+
StateFlags::all() - StateFlags::DECORATIONS
36+
}
37+
3338
#[derive(Clone, serde::Serialize, specta::Type)]
3439
struct ServerReadyData {
3540
url: String,
@@ -293,10 +298,7 @@ pub fn run() {
293298
.plugin(tauri_plugin_os::init())
294299
.plugin(
295300
tauri_plugin_window_state::Builder::new()
296-
.with_state_flags(
297-
tauri_plugin_window_state::StateFlags::all()
298-
- tauri_plugin_window_state::StateFlags::DECORATIONS,
299-
)
301+
.with_state_flags(window_state_flags())
300302
.build(),
301303
)
302304
.plugin(tauri_plugin_store::Builder::new().build())
@@ -365,6 +367,8 @@ pub fn run() {
365367

366368
let window = window_builder.build().expect("Failed to create window");
367369

370+
setup_window_state_listener(&app, &window);
371+
368372
#[cfg(windows)]
369373
let _ = window.create_overlay_titlebar();
370374

@@ -560,3 +564,34 @@ async fn spawn_local_server(
560564
}
561565
}
562566
}
567+
568+
fn setup_window_state_listener(app: &tauri::AppHandle, window: &tauri::WebviewWindow) {
569+
let (tx, mut rx) = mpsc::channel::<()>(1);
570+
571+
window.on_window_event(move |event| {
572+
if !matches!(event, WindowEvent::Moved(_) | WindowEvent::Resized(_)) {
573+
return;
574+
}
575+
let _ = tx.try_send(());
576+
});
577+
578+
tauri::async_runtime::spawn({
579+
let app = app.clone();
580+
581+
async move {
582+
let save = || {
583+
let handle = app.clone();
584+
let app = app.clone();
585+
let _ = handle.run_on_main_thread(move || {
586+
let _ = app.save_window_state(window_state_flags());
587+
});
588+
};
589+
590+
while rx.recv().await.is_some() {
591+
tokio::time::sleep(Duration::from_millis(200)).await;
592+
593+
save();
594+
}
595+
}
596+
});
597+
}

packages/desktop/src-tauri/src/window_customizer.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use tauri::{plugin::Plugin, Manager, Runtime, Window};
1+
use tauri::{Manager, Runtime, Window, plugin::Plugin};
22

33
pub struct PinchZoomDisablePlugin;
44

@@ -21,8 +21,8 @@ impl<R: Runtime> Plugin<R> for PinchZoomDisablePlugin {
2121
let _ = webview_window.with_webview(|_webview| {
2222
#[cfg(target_os = "linux")]
2323
unsafe {
24-
use gtk::glib::ObjectExt;
2524
use gtk::GestureZoom;
25+
use gtk::glib::ObjectExt;
2626
use webkit2gtk::glib::gobject_ffi;
2727

2828
if let Some(data) = _webview.inner().data::<GestureZoom>("wk-view-zoom-gesture") {

0 commit comments

Comments
 (0)