From 17382ef7fa99eef094aa5df12cc243cb77050fb0 Mon Sep 17 00:00:00 2001 From: FTCHD <144691102+FTCHD@users.noreply.github.com> Date: Tue, 12 May 2026 20:31:58 +0300 Subject: [PATCH] run gtk calls on main thread --- src-tauri/common/shortcut.rs | 4 +- src-tauri/common/window.rs | 73 +++++++++++++++++++++++------------- 2 files changed, 49 insertions(+), 28 deletions(-) diff --git a/src-tauri/common/shortcut.rs b/src-tauri/common/shortcut.rs index 265a961..c39e954 100644 --- a/src-tauri/common/shortcut.rs +++ b/src-tauri/common/shortcut.rs @@ -121,7 +121,7 @@ pub fn show_toolbar_window(app_handle: &AppHandle) -> Result<(), tauri::Error> { window.unminimize()?; window.set_focus()?; #[cfg(target_os = "linux")] - focus_window_linux(&window); + focus_window_linux(app_handle, &window); } Ok(()) @@ -138,7 +138,7 @@ fn open_toolbar(app_handle: &AppHandle) -> Result<(), tauri::Error> { window.unminimize()?; window.set_focus()?; #[cfg(target_os = "linux")] - focus_window_linux(&window); + focus_window_linux(app_handle, &window); } } diff --git a/src-tauri/common/window.rs b/src-tauri/common/window.rs index 73bedb9..ecdbd95 100644 --- a/src-tauri/common/window.rs +++ b/src-tauri/common/window.rs @@ -49,11 +49,14 @@ fn centered_logical_position( } #[cfg(target_os = "linux")] -pub(crate) fn focus_window_linux(window: &WebviewWindow) { - use gtk::prelude::*; - if let Ok(gtk_win) = window.gtk_window() { - gtk_win.present_with_time(gtk::current_event_time()); - } +pub(crate) fn focus_window_linux(app_handle: &AppHandle, window: &WebviewWindow) { + let window_clone = window.clone(); + let _ = app_handle.run_on_main_thread(move || { + use gtk::prelude::*; + if let Ok(gtk_win) = window_clone.gtk_window() { + gtk_win.present_with_time(gtk::current_event_time()); + } + }); } #[tauri::command] @@ -67,10 +70,10 @@ pub async fn open_full_window( if let Some(existing) = app_handle.get_webview_window(&name) { existing.set_focus().map_err(|e| e.to_string())?; #[cfg(target_os = "linux")] - focus_window_linux(&existing); + focus_window_linux(&app_handle, &existing); return Ok(()); } - + let os = std::env::consts::OS; let primary_monitor = app_handle @@ -119,16 +122,23 @@ pub async fn open_full_window( window.show().map_err(|e| e.to_string())?; window.set_focus().map_err(|e| e.to_string())?; #[cfg(target_os = "linux")] - focus_window_linux(&window); + focus_window_linux(&app_handle, &window); #[cfg(target_os = "linux")] if let Some((x, y)) = centered_logical_position(&app_handle, width, height) { let _ = window.set_position(tauri::Position::Logical(tauri::LogicalPosition::new(x, y))); } - if os != "windows" && os != "macos" { - window.set_resizable(false).map_err(|e| e.to_string())?; - window.set_resizable(true).map_err(|e| e.to_string())?; + #[cfg(target_os = "linux")] + { + let window_clone = window.clone(); + app_handle.run_on_main_thread(move || { + use gtk::prelude::*; + if let Ok(gtk_win) = window_clone.gtk_window() { + gtk_win.set_resizable(false); + gtk_win.set_resizable(true); + } + }).map_err(|e| e.to_string())?; } Ok(()) @@ -146,7 +156,7 @@ pub async fn open_window( if let Some(existing) = app_handle.get_webview_window(&name) { existing.set_focus().map_err(|e| e.to_string())?; #[cfg(target_os = "linux")] - focus_window_linux(&existing); + focus_window_linux(&app_handle, &existing); return Ok(()); } @@ -220,7 +230,7 @@ pub async fn open_window( if let Some(existing) = app_handle.get_webview_window(&name) { existing.set_focus().map_err(|e| e.to_string())?; #[cfg(target_os = "linux")] - focus_window_linux(&existing); + focus_window_linux(&app_handle, &existing); return Ok(()); } @@ -244,17 +254,21 @@ pub async fn open_window( let window = builder.build().map_err(|e| e.to_string())?; + std::thread::sleep(std::time::Duration::from_millis(750)); + window.set_zoom(1.0).map_err(|e| e.to_string())?; window.show().map_err(|e| e.to_string())?; window.set_focus().map_err(|e| e.to_string())?; - focus_window_linux(&window); - if let Some((x, y)) = centered_logical_position(&app_handle, width, height) { - let _ = window.set_position(tauri::Position::Logical(tauri::LogicalPosition::new(x, y))); - } - - window.set_resizable(false).map_err(|e| e.to_string())?; - window.set_resizable(true).map_err(|e| e.to_string())?; + let window_clone = window.clone(); + app_handle.run_on_main_thread(move || { + use gtk::prelude::*; + if let Ok(gtk_win) = window_clone.gtk_window() { + gtk_win.present_with_time(gtk::current_event_time()); + gtk_win.set_resizable(false); + gtk_win.set_resizable(true); + } + }).map_err(|e| e.to_string())?; Ok(()) } @@ -268,10 +282,10 @@ pub async fn open_small_window( if let Some(existing) = app_handle.get_webview_window(&name) { existing.set_focus().map_err(|e| e.to_string())?; #[cfg(target_os = "linux")] - focus_window_linux(&existing); + focus_window_linux(&app_handle, &existing); return Ok(()); } - + let os = std::env::consts::OS; let mut builder = WebviewWindowBuilder::new(&app_handle, &name, WebviewUrl::App(url.into())) @@ -312,7 +326,7 @@ pub async fn open_small_window( window.show().map_err(|e| e.to_string())?; window.set_focus().map_err(|e| e.to_string())?; #[cfg(target_os = "linux")] - focus_window_linux(&window); + focus_window_linux(&app_handle, &window); window.set_always_on_top(true).map_err(|e| e.to_string())?; #[cfg(target_os = "linux")] @@ -320,9 +334,16 @@ pub async fn open_small_window( let _ = window.set_position(tauri::Position::Logical(tauri::LogicalPosition::new(x, y))); } - if os != "windows" && os != "macos" { - window.set_resizable(true).map_err(|e| e.to_string())?; - window.set_resizable(false).map_err(|e| e.to_string())?; + #[cfg(target_os = "linux")] + { + let window_clone = window.clone(); + app_handle.run_on_main_thread(move || { + use gtk::prelude::*; + if let Ok(gtk_win) = window_clone.gtk_window() { + gtk_win.set_resizable(true); + gtk_win.set_resizable(false); + } + }).map_err(|e| e.to_string())?; } Ok(())