Skip to content

A cross-platform desktop All-in-One assistant for Claude Code, Codex, OpenCode,DeepSeek Harness and more. Only official website: ccswitch.io

11.3 KB
Raw Blame
//! Linux 专用的主窗口恢复补丁。
//!
//! 解决 Tauri 2.x 在部分 Linux 发行版(尤其是 Wayland / 某些 WebKitGTK
//! 版本)上启动或从托盘/隐藏状态恢复后「点不到」的问题:
//!
//! - **失效模式 A**(Tauri #10746 / wry #637):webview 在 `show()` 后
//!   没有获得 keyboard focus,导致首次点击被 X11/Wayland 用作
//!   click-to-activate 而非传给 webview。
//! - **失效模式 B**:GTK surface 与 WebKitWebView 的 input region 尺寸
//!   协商在 `visible:false` → `show()` 的路径上失败,整窗永远不响应
//!   点击,只有重新 `size_allocate` 才能恢复。
//! - **失效模式 C**(#7405 / #2736 / #7499):Tao 0.34 的 Wayland `WlHeader`
//!   把 `HeaderBar` 包进 `EventBox::set_above_child(true)`,EventBox 的
//!   GdkWindow 叠在按钮上面,点击被吞掉。网页内容仍可点,原生标题栏按钮全死。
//!   上游修复是 [tao#1218](https://github.com/tauri-apps/tao/pull/1218)
//!   (去掉自定义 CSD,随 tao 0.36 / Tauri 2.12 发布)。在那之前,本模块
//!   把 `above_child` 改回 `false`,让按钮重新接到指针事件。
//!
//! 本模块导出 [`nudge_main_window`]。序列是 fire-and-forget,零抽搐:
//! 1. 激活时立即 `set_focus`;仅当前 runner 的首轮可在 realize 后重试一次,
//!    排队请求不会延迟抢焦点;
//! 2. 装饰对账:仅在与设置不一致时 `set_decorations`;
//! 3. 修补 Wayland CSD `EventBox.above_child`(最大化窗口也走这条,不改几何);
//! 4. 请求 GTK 重新分配 WebView 的布局,刷新输入区而不改变顶层窗口尺寸。
//!
//! 所有「让主窗口出现在用户面前」的路径(正常启动、deeplink、
//! single_instance、托盘 show_main、lightweight 退出/重建)都应在现有
//! `set_focus()` 之后追加一次调用。

use crate::sync_utils::MutexExt;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Mutex;
use std::time::Duration;

use tauri::WebviewWindow;

static IS_NUDGING: AtomicBool = AtomicBool::new(false);

/// 只保留最新一次 nudge 请求。轻量模式销毁重建窗口时,旧任务仍在跑,
/// 新窗口的请求必须覆盖排队,而不是被互斥直接丢掉。
static PENDING_NUDGE: LatestSlot<PendingNudge> = LatestSlot::new();

/// 在 webview realize 之后的延迟,等 GTK 主循环把 realize 事件处理完。
const REALIZE_WAIT: Duration = Duration::from_millis(200);

struct PendingNudge {
    window: WebviewWindow,
    reason: &'static str,
}

/// 覆盖式槽:并发写入只保留最新值,供当前 nudge 任务结束后取出。
struct LatestSlot<T> {
    inner: Mutex<Option<T>>,
}

impl<T> LatestSlot<T> {
    const fn new() -> Self {
        Self {
            inner: Mutex::new(None),
        }
    }

    fn store(&self, value: T) {
        *self.lock() = Some(value);
    }

    fn take(&self) -> Option<T> {
        self.lock().take()
    }

    fn has(&self) -> bool {
        self.lock().is_some()
    }

    fn lock(&self) -> std::sync::MutexGuard<'_, Option<T>> {
        self.inner.lock_recover()
    }
}

fn try_acquire_nudge() -> bool {
    IS_NUDGING
        .compare_exchange(false, true, Ordering::SeqCst, Ordering::SeqCst)
        .is_ok()
}

fn decorations_need_restore(current: bool, desired: bool) -> bool {
    current != desired
}

fn is_window_alive(window: &WebviewWindow) -> bool {
    window.is_visible().is_ok()
}

fn is_window_visible(window: &WebviewWindow) -> bool {
    window.is_visible().unwrap_or(false)
}

fn should_retry_focus(allow_delayed_focus: bool, currently_focused: bool) -> bool {
    allow_delayed_focus && !currently_focused
}

fn next_delayed_focus_eligibility(
    allow_delayed_focus: bool,
    completed_on_live_window: bool,
) -> bool {
    allow_delayed_focus && !completed_on_live_window
}

struct NudgeGuard;

impl Drop for NudgeGuard {
    fn drop(&mut self) {
        IS_NUDGING.store(false, Ordering::SeqCst);
    }
}

/// 对主窗口执行 Linux 专用的「focus + HeaderBar + surface 重激活」序列。
///
/// 调用是 fire-and-forget:内部 spawn 一个异步任务并立即返回,不阻塞 UI。
/// `reason` 标识触发来源(startup / tray-show-main 等),用于日志排障。
///
/// 同一时刻只跑一条序列。后续请求覆盖排队,当前序列结束后再处理最新请求。
pub(crate) fn nudge_main_window(window: WebviewWindow, reason: &'static str) {
    let _ = window.set_focus();
    PENDING_NUDGE.store(PendingNudge { window, reason });

    if !try_acquire_nudge() {
        log::debug!("Linux: 正在执行主窗口重激活,已排队最新请求 (reason: {reason})");
        return;
    }

    tauri::async_runtime::spawn(async move {
        // 只有紧随本次激活启动的首个存活窗口允许在 realize 后补一次 focus。
        // 同窗排队请求可能已等待上一轮 resize/reconcile 很久,此时用户可能已经
        // 切到别的应用,不能再延迟抢焦点。若旧窗口被 lightweight 模式销毁,
        // 则保留资格给排队的替代窗口,因为它的立即 focus 可能发生在 realize 前。
        let mut allow_delayed_focus = true;
        loop {
            {
                let _guard = NudgeGuard;
                while let Some(PendingNudge { window, reason }) = PENDING_NUDGE.take() {
                    let completed_on_live_window =
                        run_nudge_sequence(window, reason, allow_delayed_focus).await;
                    allow_delayed_focus = next_delayed_focus_eligibility(
                        allow_delayed_focus,
                        completed_on_live_window,
                    );
                }
            }
            // Guard 已释放 IS_NUDGING。若释放窗口期内又有请求入队,重新抢执行权;
            // 抢不到说明另一个 runner 已经接手。
            if !PENDING_NUDGE.has() {
                break;
            }
            if !try_acquire_nudge() {
                break;
            }
        }
    });
}

async fn run_nudge_sequence(
    window: WebviewWindow,
    reason: &'static str,
    allow_delayed_focus: bool,
) -> bool {
    if !is_window_alive(&window) {
        log::debug!("Linux: 窗口已销毁,跳过重激活 (reason: {reason})");
        return false;
    }

    tokio::time::sleep(REALIZE_WAIT).await;

    if !is_window_alive(&window) {
        log::debug!("Linux: 窗口在等待 realize 期间被销毁,跳过重激活 (reason: {reason})");
        return false;
    }

    // 用户已再次藏进托盘:不要 set_focus / 改装饰把窗口 map 回来。
    if !is_window_visible(&window) {
        log::debug!("Linux: 窗口已隐藏,跳过重激活 (reason: {reason})");
        return false;
    }

    if should_retry_focus(allow_delayed_focus, window.is_focused().unwrap_or(false)) {
        let _ = window.set_focus();
    }

    restore_decorations_if_needed(&window);

    // 失效模式 C:Tao 0.34 WlHeader 的 EventBox 叠在 HeaderBar 按钮之上。
    // 不能靠翻转 set_resizable:那只会把 decoration_layout 从
    // "menu:minimize,maximize,close" 改成 "menu:minimize,close" 再改回来,
    // 最大化按钮会闪一下,也不是重绑 subsurface 的 API。
    patch_wayland_header_event_box(&window);

    if let Err(error) = window.with_webview(|webview| {
        use gtk::prelude::WidgetExt;
        webview.inner().queue_resize();
    }) {
        log::warn!("Linux: 无法刷新主 WebView 的输入区: {error}");
    }

    if is_window_alive(&window) {
        log::info!(
            "Linux: 已对主窗口执行 focus + HeaderBar 控制按钮与 surface 重激活 (reason: {reason})"
        );
        true
    } else {
        log::debug!("Linux: 窗口在重激活期间被销毁 (reason: {reason})");
        false
    }
}

fn restore_decorations_if_needed(window: &WebviewWindow) {
    let desired = !crate::settings::get_settings().use_app_window_controls;
    match window.is_decorated() {
        Ok(current) if decorations_need_restore(current, desired) => {
            if let Err(e) = window.set_decorations(desired) {
                log::warn!("Linux: 恢复窗口装饰失败: {e}");
            } else {
                log::info!("Linux: 已恢复窗口装饰 decorated={desired}");
            }
        }
        Ok(_) => {}
        Err(e) => log::warn!("Linux: 读取窗口装饰状态失败: {e}"),
    }
}

/// 关掉 Tao 0.34 Wayland `WlHeader` 里 EventBox 的 `above_child`。
///
/// `tao-0.34.6/src/platform_impl/linux/wayland/header.rs` 把 HeaderBar 放进
/// `EventBox` 并 `set_above_child(true)`。GTK 文档:EventBox 的 GdkWindow
/// 会叠在子控件之上,子控件收不到指针事件。X11 上按钮自己的 X window 仍可能
/// 点到;Wayland 子表面堆叠则经常整组按钮假死(tauri#13440 / tao#1218)。
///
/// `connect_resizable_notify` 里的 `set_decoration_layout` 只是按是否可缩放
/// 在 `"menu:minimize,maximize,close"` 和 `"menu:minimize,close"` 之间切换,
/// 不是重绑输入的 API。
///
/// 必须在 GTK 主线程改 widget。`run_on_main_thread` + `gtk_window()` 在主线程
/// 上会走 wry 的 inline `handle_user_message`,不会自己等自己。
fn patch_wayland_header_event_box(window: &WebviewWindow) {
    let window = window.clone();
    if let Err(e) = window.clone().run_on_main_thread(move || {
        apply_wayland_header_event_box_patch(&window);
    }) {
        log::warn!("Linux: 无法在 GTK 主线程修补 HeaderBar EventBox: {e}");
    }
}

fn apply_wayland_header_event_box_patch(window: &WebviewWindow) {
    use gtk::prelude::{Cast, EventBoxExt, GtkWindowExt};

    let gtk_window = match window.gtk_window() {
        Ok(gtk_window) => gtk_window,
        Err(e) => {
            log::warn!("Linux: 获取 gtk_window 失败: {e}");
            return;
        }
    };
    let Some(titlebar) = gtk_window.titlebar() else {
        return;
    };
    let Ok(event_box) = titlebar.downcast::<gtk::EventBox>() else {
        return;
    };
    if event_box.is_above_child() {
        event_box.set_above_child(false);
        log::info!("Linux: 已关闭 Wayland CSD EventBox above_child,标题栏按钮可接收点击");
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn latest_slot_keeps_newest_while_busy() {
        let slot = LatestSlot::new();
        slot.store("destroyed-window");
        slot.store("replacement-window");
        assert!(slot.has());
        assert_eq!(slot.take(), Some("replacement-window"));
        assert!(!slot.has());
        assert_eq!(slot.take(), None);
    }

    #[test]
    fn decorations_are_only_restored_on_mismatch() {
        assert!(!decorations_need_restore(true, true));
        assert!(!decorations_need_restore(false, false));
        assert!(decorations_need_restore(true, false));
        assert!(decorations_need_restore(false, true));
    }

    #[test]
    fn queued_nudges_never_retry_focus() {
        assert!(should_retry_focus(true, false));
        assert!(!should_retry_focus(false, false));
        assert!(!should_retry_focus(true, true));
    }

    #[test]
    fn destroyed_window_preserves_retry_for_replacement() {
        assert!(!next_delayed_focus_eligibility(true, true));
        assert!(next_delayed_focus_eligibility(true, false));
        assert!(!next_delayed_focus_eligibility(false, false));
    }
}