Refactor macOS Dock visibility control to use AppHandle API and clean up related helpers
Closes: #110
This commit is contained in:
5 files changed
+16
-31
No files matched your search
@@ -712,14 +712,15 @@ pub(crate) async fn download_and_stage_portable_app_update(
|
||||
let helper_path = work_dir.join("EasyCLIProxyAPI-updater");
|
||||
fs::copy(¤t_exe, &helper_path)
|
||||
.map_err(|error| format!("准备应用更新助手失败: {error}"))?;
|
||||
let app_parent = current_app
|
||||
let backup_app = current_app
|
||||
.parent()
|
||||
.ok_or_else(|| "macOS 应用程序路径没有父目录".to_string())?;
|
||||
.ok_or_else(|| "macOS 应用程序路径没有父目录".to_string())?
|
||||
.join(".EasyCLIProxyAPI.app.update-backup");
|
||||
let descriptor = MacosUpdateDescriptor {
|
||||
parent_pid: std::process::id(),
|
||||
current_app,
|
||||
staged_app,
|
||||
backup_app: app_parent.join(".EasyCLIProxyAPI.app.update-backup"),
|
||||
backup_app,
|
||||
executable_relative_path,
|
||||
ack_path: work_dir.join("update-started.ack"),
|
||||
work_dir: work_dir.clone(),
|
||||
|
||||
@@ -216,6 +216,8 @@ pub(crate) struct YamlValueChange {
|
||||
value: serde_norway::Value,
|
||||
}
|
||||
|
||||
type MissingNestedYamlGroup = (Vec<String>, Vec<(String, serde_norway::Value)>);
|
||||
|
||||
pub(crate) fn render_yaml_value_changes(
|
||||
content: &str,
|
||||
original: &serde_norway::Value,
|
||||
@@ -243,8 +245,7 @@ pub(crate) fn render_yaml_value_changes(
|
||||
remaining_changes.push(change);
|
||||
}
|
||||
}
|
||||
let mut missing_nested_groups: Vec<(Vec<String>, Vec<(String, serde_norway::Value)>)> =
|
||||
Vec::new();
|
||||
let mut missing_nested_groups: Vec<MissingNestedYamlGroup> = Vec::new();
|
||||
let mut yaml_edit_changes = Vec::new();
|
||||
for change in remaining_changes {
|
||||
if change.path.len() < 2
|
||||
|
||||
@@ -1829,9 +1829,7 @@ pub(crate) fn configure_initial_main_window(
|
||||
.ok_or_else(|| "主窗口不存在".to_string())?;
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
if !set_macos_dock_visible(!start_hidden) {
|
||||
return Err("更新 Dock 图标状态失败".to_string());
|
||||
}
|
||||
set_macos_dock_visible(app_handle, !start_hidden);
|
||||
|
||||
if start_hidden {
|
||||
return window
|
||||
|
||||
@@ -33,7 +33,7 @@ use futures_util::StreamExt;
|
||||
#[cfg(target_os = "macos")]
|
||||
use objc2::MainThreadMarker;
|
||||
#[cfg(target_os = "macos")]
|
||||
use objc2_app_kit::{NSApplication, NSApplicationActivationPolicy, NSEvent};
|
||||
use objc2_app_kit::NSEvent;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sha2::{Digest, Sha256};
|
||||
#[cfg(target_os = "windows")]
|
||||
@@ -1697,12 +1697,10 @@ fn main() {
|
||||
if window.label() == "main" {
|
||||
if let tauri::WindowEvent::CloseRequested { api, .. } = event {
|
||||
api.prevent_close();
|
||||
if !set_macos_dock_visible(false) {
|
||||
return;
|
||||
}
|
||||
set_macos_dock_visible(window.app_handle(), false);
|
||||
if let Err(error) = window.hide() {
|
||||
eprintln!("隐藏主窗口失败: {error}");
|
||||
set_macos_dock_visible(true);
|
||||
set_macos_dock_visible(window.app_handle(), true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+5
-18
@@ -11,21 +11,10 @@ pub(crate) struct MacosTrayClickState {
|
||||
}
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
pub(crate) fn set_macos_dock_visible(visible: bool) -> bool {
|
||||
let Some(mtm) = MainThreadMarker::new() else {
|
||||
eprintln!("更新 Dock 图标状态必须在主线程执行");
|
||||
return false;
|
||||
};
|
||||
let policy = if visible {
|
||||
NSApplicationActivationPolicy::Regular
|
||||
} else {
|
||||
NSApplicationActivationPolicy::Accessory
|
||||
};
|
||||
if !NSApplication::sharedApplication(mtm).setActivationPolicy(policy) {
|
||||
eprintln!("更新 Dock 图标状态失败");
|
||||
return false;
|
||||
pub(crate) fn set_macos_dock_visible(app_handle: &tauri::AppHandle, visible: bool) {
|
||||
if let Err(error) = app_handle.set_dock_visibility(visible) {
|
||||
eprintln!("更新 Dock 图标状态失败: {error}");
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
@@ -33,12 +22,10 @@ pub(crate) fn show_main_window_on_main_thread(app_handle: &tauri::AppHandle) {
|
||||
let Some(window) = app_handle.get_webview_window("main") else {
|
||||
return;
|
||||
};
|
||||
if !set_macos_dock_visible(true) {
|
||||
return;
|
||||
}
|
||||
set_macos_dock_visible(app_handle, true);
|
||||
if let Err(error) = window.show() {
|
||||
eprintln!("显示主窗口失败: {error}");
|
||||
set_macos_dock_visible(false);
|
||||
set_macos_dock_visible(app_handle, false);
|
||||
return;
|
||||
}
|
||||
if window.is_minimized().unwrap_or(false) {
|
||||
|
||||
Reference in new issue
Block a user