You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Mac平台Tauri应用移除Dock图标并后台运行的问题

解决Mac平台Tauri应用关闭窗口后移除Dock图标并后台运行的问题

问题概述

需要在Mac上用Tauri实现:关闭窗口时移除Dock图标,同时让应用保持后台运行。目前已实现阻止应用退出,但不清楚如何在窗口关闭后移除Dock图标,尝试的两种方案存在问题:

  • 在setup中设置ActivationPolicy::Accessory会默认隐藏Dock图标,不符合“关闭窗口后才隐藏”的需求
  • 直接调用Cocoa的set_activation_policy_可以隐藏图标,但导致应用无法通过Spotlight搜索打开

解决方案

结合Tauri官方API监听窗口事件,切换激活策略,既能满足关闭窗口后隐藏Dock图标的需求,又不会破坏Spotlight搜索功能:

完整实现代码

// Prevents additional console window on Windows in release, DO NOT REMOVE!!
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]

#[tauri::command]
fn greet(name: &str) -> String {
    format!("Hello ->, {}!", name)
}

fn main() {
    tauri::Builder::default()
        .setup(|app| {
            // 初始保持正常激活策略,显示Dock图标
            #[cfg(target_os = "macos")]
            app.set_activation_policy(tauri::ActivationPolicy::Regular)
                .expect("Failed to set activation policy");
            Ok(())
        })
        .on_window_event(|event| {
            #[cfg(target_os = "macos")]
            if let tauri::WindowEvent::CloseRequested { api, .. } = event.event() {
                // 阻止窗口关闭触发应用退出
                api.prevent_close();
                // 隐藏窗口
                event.window().hide().expect("Failed to hide window");
                // 切换激活策略为Accessory,移除Dock图标
                let app_handle = event.window().app_handle();
                app_handle.set_activation_policy(tauri::ActivationPolicy::Accessory)
                    .expect("Failed to set accessory activation policy");
            }
        })
        .build(tauri::generate_context!())
        .expect("error while building tauri application")
        .run(|_app_handle, event| match event {
            tauri::RunEvent::ExitRequested { api, .. } => {
                // 阻止应用退出,保持后台运行
                api.prevent_exit();
            }
            _ => {}
        })
}

可选:添加恢复窗口的入口

如果需要让用户重新打开窗口(比如通过菜单栏),可以补充菜单配置,切换回正常激活策略并显示窗口:

// 在Builder链中插入菜单配置
.menu(tauri::Menu::new().add_submenu(tauri::Submenu::new(
    "应用",
    tauri::Menu::new()
        .add_item(tauri::MenuItem::with_id("show_window", "显示窗口", None, None)),
)))
.on_menu_event(|event| {
    #[cfg(target_os = "macos")]
    if event.menu_item_id() == "show_window" {
        // 切换回正常激活策略,重新显示Dock图标
        event.app_handle().set_activation_policy(tauri::ActivationPolicy::Regular)
            .expect("Failed to set regular activation policy");
        // 显示并聚焦主窗口
        if let Some(window) = event.app_handle().get_window("main") {
            window.show().expect("Failed to show window");
            window.set_focus().expect("Failed to focus window");
        }
    }
})

方案说明

  • 使用Tauri官方的set_activation_policyAPI,避免直接调用Cocoa API导致的Spotlight搜索失效问题
  • 通过WindowEvent::CloseRequested精准监听窗口关闭动作,先隐藏窗口再切换激活策略,符合需求逻辑
  • 保留ExitRequested的阻止逻辑,确保应用在后台持续运行

内容的提问来源于stack exchange,提问作者Nane

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 03:41:20