From 3b57b270af05938167112eb96408ae265a9e6d51 Mon Sep 17 00:00:00 2001 From: Leafd Date: Thu, 9 Oct 2025 15:31:43 -0400 Subject: [PATCH] fix: solve crash on resize #56 --- src-tauri/src/lib.rs | 49 +++++++++++++++++++------------ src-tauri/src/tray.rs | 6 +++- src-tauri/tauri.conf.json | 3 +- src/components/CustomTitlebar.vue | 41 +++++--------------------- 4 files changed, 44 insertions(+), 55 deletions(-) diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index ffd08ee..cc65d94 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -170,31 +170,44 @@ pub fn run() { use objc2::runtime::AnyObject; use objc2_app_kit::NSColor; use objc2::msg_send; + use std::panic; if let Err(e) = window.set_title_bar_style(TitleBarStyle::Transparent) { push_log("error", "backend", format!("Failed to set title bar style: {}", e)); } // Apply macOS-specific window styling with proper error handling - if let Ok(ns_win) = window.ns_window() { - let ns_window = ns_win as *mut AnyObject; - unsafe { - let clear_color = NSColor::clearColor(); - let _: () = msg_send![ns_window, setBackgroundColor: &*clear_color]; - let _: () = msg_send![ns_window, setOpaque: false]; - - let content_view: *mut AnyObject = msg_send![ns_window, contentView]; - let _: () = msg_send![content_view, setWantsLayer: true]; - - let layer: *mut AnyObject = msg_send![content_view, layer]; - let _: () = msg_send![layer, setCornerRadius: 12.0f64]; - let _: () = msg_send![layer, setMasksToBounds: true]; - - let _: () = msg_send![layer, setNeedsDisplayOnBoundsChange: true]; + let styling_result = panic::catch_unwind(panic::AssertUnwindSafe(|| { + if let Ok(ns_win) = window.ns_window() { + let ns_window = ns_win as *mut AnyObject; + unsafe { + let clear_color = NSColor::clearColor(); + let _: () = msg_send![ns_window, setBackgroundColor: &*clear_color]; + let _: () = msg_send![ns_window, setOpaque: false]; + + let content_view: *mut AnyObject = msg_send![ns_window, contentView]; + let _: () = msg_send![content_view, setWantsLayer: true]; + + let layer: *mut AnyObject = msg_send![content_view, layer]; + let _: () = msg_send![layer, setCornerRadius: 12.0f64]; + + } + Ok(()) + } else { + Err("Failed to get NSWindow") + } + })); + + match styling_result { + Ok(Ok(())) => { + push_log("info", "backend", "✅ macOS window styling applied".to_string()); + } + Ok(Err(e)) => { + push_log("error", "backend", format!("Failed to apply window styling: {}", e)); + } + Err(_) => { + push_log("error", "backend", "Panic occurred while applying window styling".to_string()); } - push_log("info", "backend", "✅ macOS window styling applied".to_string()); - } else { - push_log("error", "backend", "Failed to get NSWindow".to_string()); } } } diff --git a/src-tauri/src/tray.rs b/src-tauri/src/tray.rs index 3839dfd..2a5e78d 100644 --- a/src-tauri/src/tray.rs +++ b/src-tauri/src/tray.rs @@ -27,8 +27,12 @@ pub fn setup_tray(app: &AppHandle) -> Result<(), Box> { ])?; + let icon = app.default_window_icon() + .ok_or("No default window icon found")? + .clone(); + let _tray_icon = TrayIconBuilder::new() - .icon(app.default_window_icon().unwrap().clone()) + .icon(icon) .menu(&menu) .show_menu_on_left_click(true) .on_menu_event(|app, event| { diff --git a/src-tauri/tauri.conf.json b/src-tauri/tauri.conf.json index a66b08c..947e8f9 100644 --- a/src-tauri/tauri.conf.json +++ b/src-tauri/tauri.conf.json @@ -25,8 +25,7 @@ "minimizable": true, "maximizable": true, "closable": true, - "hiddenTitle": true, - "transparent": true + "hiddenTitle": true } ], "security": { diff --git a/src/components/CustomTitlebar.vue b/src/components/CustomTitlebar.vue index 8593ce9..07c2952 100644 --- a/src/components/CustomTitlebar.vue +++ b/src/components/CustomTitlebar.vue @@ -25,7 +25,7 @@