Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,17 @@ pub(crate) fn show_or_create_main_window(app: &tauri::AppHandle) {
pub(crate) fn hide_main_window(app: &tauri::AppHandle) {
set_main_window_visibility(app, false);
if let Some(window) = app.get_webview_window(MAIN_WINDOW_LABEL) {
let _ = window.hide();
if let Err(err) = window.destroy() {
tracing::warn!(error = %err, "destroy window failed");
}
}
sync_main_window_menu_item(app);

if let Some(tray_state) = app.try_state::<tray::TrayState>() {
tray_state.mark_main_window_hidden();
} else {
sync_main_window_menu_item(app);
}
}

pub(crate) fn toggle_main_window(app: &tauri::AppHandle) {
Expand Down
38 changes: 34 additions & 4 deletions src-tauri/src/tray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,21 @@ impl TrayState {
}

pub(crate) fn sync_main_window_menu_item(&self, app: &AppHandle) {
let visible = app
.get_webview_window(crate::MAIN_WINDOW_LABEL)
.and_then(|window| window.is_visible().ok())
.unwrap_or(false);
let visible = resolve_main_window_menu_visible(
app.get_webview_window(crate::MAIN_WINDOW_LABEL)
.and_then(|window| window.is_visible().ok()),
false,
);
self.set_main_window_menu_item_visibility(visible);
}

pub(crate) fn mark_main_window_hidden(&self) {
// Hide action should always flip menu text to "show", even if visibility query lags.
let visible = resolve_main_window_menu_visible(None, true);
self.set_main_window_menu_item_visibility(visible);
}

fn set_main_window_menu_item_visibility(&self, visible: bool) {
let _ = self
.inner
.show_item
Expand Down Expand Up @@ -450,6 +461,13 @@ fn compact_error(err: &str) -> String {
output
}

fn resolve_main_window_menu_visible(probed_visible: Option<bool>, force_hidden: bool) -> bool {
if force_hidden {
return false;
}
probed_visible.unwrap_or(false)
}

fn main_window_menu_text(visible: bool) -> &'static str {
if visible {
HIDE_MAIN_WINDOW_TEXT
Expand All @@ -474,4 +492,16 @@ mod tests {
assert_eq!(super::main_window_menu_text(false), "显示主窗口");
assert_eq!(super::main_window_menu_text(true), "隐藏主窗口");
}

#[test]
fn forced_hidden_state_overrides_visibility_probe() {
assert!(!super::resolve_main_window_menu_visible(Some(true), true));
assert!(!super::resolve_main_window_menu_visible(None, true));
}

#[test]
fn visibility_probe_fallbacks_to_hidden_when_window_missing() {
assert!(!super::resolve_main_window_menu_visible(None, false));
assert!(super::resolve_main_window_menu_visible(Some(true), false));
}
}