Skip to content

armgit / Wake

Public Imported from GitHub
0

All your AI agent sessions in one place — browse, search, resume. Rust + GPUI.

Rust
26.7 KB
Raw Blame
//! Wake 界面尺度规范——六档字阶。
//! 规则:所有 UI 字号必须引用本模块常量,禁止裸 px 数字与 rem 工具类
//! (text_sm 等按 rem=14px 折算会产生 12.25px 这类幽灵值)。
//! 颜色只用三级:foreground(主文字)/muted_foreground(辅助)/primary(强调)。
//!
//! 尺寸随界面缩放(`ui_zoom`):常量写成 `Zpx`,表达式里的字面量写 `zpx(…)`。
//! 裸 `px` 只留给不该跟着放大的东西:1px 发丝线、窗口尺寸、测试。
use std::collections::HashSet;
use std::ops::{Add, Mul, Neg, Sub};

use gpui::{
    div, px, AbsoluteLength, AnyElement, App, DefiniteLength, Div, FontWeight, Global, Hsla,
    InteractiveElement as _, IntoElement, Length, MouseButton, ParentElement as _, Pixels,
    SharedString, Styled, Window, WindowId,
};
use gpui_component::{
    button::{Button, ButtonVariant},
    dialog::{AlertDialog, Dialog, DialogButtonProps},
    h_flex, ActiveTheme as _, Root, Sizable as _, WindowExt as _,
};

// ---------------- 界面缩放 ----------------

/// 设计稿上的像素值(100% 档的读数),取用时乘当前缩放倍数。尺寸常量一律定义成它;
/// 传给 gpui 的样式方法时经下面的 From 自动换算,参与算术时直接落成 `Pixels`
#[derive(Clone, Copy, Debug)]
pub struct Zpx(pub f32);

/// 表达式里的设计稿像素:`zpx(12.)` 就是当前档位下的 12px
pub fn zpx(value: f32) -> Pixels {
    px(value * crate::ui_zoom::factor())
}

impl Zpx {
    /// 当前档位下的实际像素
    pub fn get(self) -> Pixels {
        zpx(self.0)
    }
}

impl From<Zpx> for Pixels {
    fn from(value: Zpx) -> Self {
        value.get()
    }
}

impl From<Zpx> for AbsoluteLength {
    fn from(value: Zpx) -> Self {
        value.get().into()
    }
}

impl From<Zpx> for DefiniteLength {
    fn from(value: Zpx) -> Self {
        value.get().into()
    }
}

impl From<Zpx> for Length {
    fn from(value: Zpx) -> Self {
        value.get().into()
    }
}

impl From<Zpx> for gpui_component::Size {
    fn from(value: Zpx) -> Self {
        value.get().into()
    }
}

impl From<Zpx> for gpui_component::button::ButtonRounded {
    fn from(value: Zpx) -> Self {
        value.get().into()
    }
}

impl Mul<f32> for Zpx {
    type Output = Pixels;
    fn mul(self, rhs: f32) -> Pixels {
        self.get() * rhs
    }
}

impl Neg for Zpx {
    type Output = Pixels;
    fn neg(self) -> Pixels {
        -self.get()
    }
}

impl<T: Into<Pixels>> Add<T> for Zpx {
    type Output = Pixels;
    fn add(self, rhs: T) -> Pixels {
        self.get() + rhs.into()
    }
}

impl<T: Into<Pixels>> Sub<T> for Zpx {
    type Output = Pixels;
    fn sub(self, rhs: T) -> Pixels {
        self.get() - rhs.into()
    }
}

impl Add<Zpx> for Pixels {
    type Output = Pixels;
    fn add(self, rhs: Zpx) -> Pixels {
        self + rhs.get()
    }
}

impl Sub<Zpx> for Pixels {
    type Output = Pixels;
    fn sub(self, rhs: Zpx) -> Pixels {
        self - rhs.get()
    }
}

/// 产品展示名(About)——medium
pub const FONT_DISPLAY: Zpx = Zpx(28.);
/// 上下文大标题(中栏头部)——semibold
pub const FONT_TITLE: Zpx = Zpx(22.);
/// 区块标题(详情页会话标题)——semibold
pub const FONT_HEADING: Zpx = Zpx(16.);
/// 界面正文(导航行/列表标题/按钮/输入);列表标题用 medium
pub const FONT_BODY: Zpx = Zpx(14.);
/// 辅助说明(列表副行/元信息/占位/空态提示/侧栏子级行)
pub const FONT_CAPTION: Zpx = Zpx(12.);
/// 标签(分组头/计数/快捷键徽标/状态栏);组头 semibold + 大写
pub const FONT_LABEL: Zpx = Zpx(11.);

// ---- 对话区附档(详情逐消息渲染,经用户逐轮校准,与六档并存)----

/// 用户气泡正文(比 FONT_BODY 收半档,气泡内更紧凑)
pub const FONT_MSG_USER: Zpx = Zpx(13.5);
/// 助手平铺正文
pub const FONT_MSG_BODY: Zpx = Zpx(13.);
/// thinking 摘要行(斜体)
pub const FONT_MSG_THINKING: Zpx = Zpx(11.5);

// ---------------- 平台文案 ----------------
// 同一处 UI 在不同平台叫不同名字/键(Finder vs File Explorer vs 文件管理器、
// ⌘ vs Ctrl),文案走这里的函数,别在 render 里散落字面量。名词与 wake-core
// terminal 的平台实现对应:macos=Finder,windows=资源管理器官方英文名
// File Explorer。这里用 `#[cfg]` 而非别处惯用的 `cfg!` 表达式:展开物只是
// `concat!` 出的字面量与同形函数体,没有会在另一平台上失配的类型。
//
// 两个宏的形状相同:给一个平台名词 → `concat!` 派生若干**整句** key(整句
// 即英文文案本身),并直接 emit 取用函数。中间不留 const:那层只是把字面量
// 从宏内递到宏外,而 `t()` 要到运行时才知道译文,常量存不下它。
//
// 整句而非「动词 + {名词}」是有意的——zh-Hans 包里三条 "Show in …" 各自带着
// 平台正确的中文产品名(访达 / 文件资源管理器 / 文件管理器),名词替换式的
// 设计产不出这个。

/// 文件管理器的平台名(macos=Finder,windows=官方英文名 File Explorer)
macro_rules! file_manager_copy {
    ($app:literal) => {
        /// 详情页 / 设置页的「在文件管理器中显示」
        pub fn show_in_fm() -> &'static str {
            crate::i18n::t(concat!("Show in ", $app))
        }
        /// 更多菜单里的同一动作(措辞不同,是两条 key)
        pub fn reveal_in_fm() -> &'static str {
            crate::i18n::t(concat!("Reveal in ", $app))
        }
    };
}
#[cfg(target_os = "macos")]
file_manager_copy!("Finder");
#[cfg(target_os = "windows")]
file_manager_copy!("File Explorer");
#[cfg(not(any(target_os = "macos", target_os = "windows")))]
file_manager_copy!("File Manager");

// 系统回收站的平台名词(macOS 与 freedesktop 都叫 Trash,Windows 是
// Recycle Bin)与由它派生的删除文案。名词只写一次、整句由 concat!
// 拼出:各处分别手写 cfg! 链的话,没有任何东西能拦住它们说不同的词
// ——与 wake-core 各平台 trash_existing 的失败文案也得是同一个词。
//
// 确认框内容活在 **dialog builder 闭包**里、对话框开着时每帧重跑,`t()` 是
// 一次哈希查表,和从前读常量一样不分配。
macro_rules! trash_copy {
    ($noun:literal, $body:literal) => {
        /// 删除确认框主按钮 / 更多菜单项
        pub fn move_to_trash() -> &'static str {
            crate::i18n::t(concat!("Move to ", $noun))
        }
        // 删除成功的通知。单/复数两支都返回 **key** 而非译文:调用点用
        // `i18n::tp` 按条数选支,选支这一步必须发生在查表之前
        pub fn session_trashed_key() -> &'static str {
            concat!("Session moved to ", $noun)
        }
        pub fn sessions_trashed_key() -> &'static str {
            concat!("{} sessions moved to ", $noun)
        }
        /// 确认框正文里嵌的名词(冠词随名词变,故与 $noun 分开)
        pub fn trash_body() -> &'static str {
            crate::i18n::t($body)
        }
        /// 批量清理确认的恢复方式与空间回收条件。
        pub fn cleanup_trash_hint() -> &'static str {
            crate::i18n::t(concat!(
                "You can restore files from ",
                $body,
                ". Space is freed after emptying it."
            ))
        }
        /// 删除确认框正文(下面接着是文件路径)。找回的方式要说准:从废纸篓放回原处,
        /// 扫描认出原样回来的文件就重新列出(`Store::release_put_back`)
        pub fn trash_confirm_body() -> &'static str {
            crate::i18n::t(concat!(
                "This file will be moved to ",
                $body,
                ". Put it back from ",
                $body,
                " and the session shows up again."
            ))
        }
    };
}
#[cfg(target_os = "windows")]
trash_copy!("Recycle Bin", "the Recycle Bin");
#[cfg(not(target_os = "windows"))]
trash_copy!("Trash", "Trash");

/// ⌘K 面板的唯一绑定串——main.rs 的 bind_keys 与下方徽标同源,改键只动这里
pub const SEARCH_KEYSTROKE: &str = "secondary-k";

/// 快捷键徽标,从真实绑定串派生("⌘K"/"Ctrl+K"——secondary→平台键与显示形制都由
/// gpui/Kbd 持有,这里零平台知识)。绑定串是写死的常量,解析不了是代码错
pub fn key_hint(keystroke: &str) -> String {
    let key = gpui::Keystroke::parse(keystroke).expect("binding keystrokes parse");
    gpui_component::kbd::Kbd::format(&key)
}

/// 搜索快捷键徽标
pub fn search_key_hint() -> &'static str {
    use std::sync::OnceLock;
    static HINT: OnceLock<String> = OnceLock::new();
    HINT.get_or_init(|| key_hint(SEARCH_KEYSTROKE))
}

// ---------------- 间距与结构 ----------------
// 间距刻度:4px 网格。新代码一律引用常量或显式 zpx();
// 注意 gpui 的 rem 间距类有幽灵值(rem=14px 下 p_2p5=8.75px、p_3=10.5px,
// 均不在网格上)——对齐敏感处禁止使用,存量逐步迁移。

pub const SPACE_XS: Zpx = Zpx(4.);
pub const SPACE_SM: Zpx = Zpx(8.);
pub const SPACE_MD: Zpx = Zpx(12.);
pub const SPACE_LG: Zpx = Zpx(16.);
pub const SPACE_XL: Zpx = Zpx(20.);
pub const SPACE_XXL: Zpx = Zpx(24.);

/// 图标 / 品牌图与紧随其后的文字之间的间距——全应用只此一个值(侧栏行与组头、列表
/// 元信息行、详情与阅读面头部及元信息行、⌘K 结果、Insights 榜单、Open In 菜单、状态行、
/// 设置页与清理页的行)。用户 2026-09-22 定统一:原先 4 / 6 / 7 / 8 / 12 各处不一。
/// 不在 4px 网格上是有意的——品牌 PNG 没有留白,8px 显得空、4px 又贴;lucide 线条图标
/// 自带约 1.5px 留白,6px 几何间距视觉上约 7.5px。卡片级的 24px 头像不在此列
pub const ICON_TEXT_GAP: Zpx = Zpx(6.);

/// 侧栏容器水平内边距(行的 hover/选中胶囊左右各留这么多)。
pub const SIDEBAR_EDGE: Zpx = Zpx(10.);

// ---- 侧栏中轴:LEAD_AXIS = 26.75 ----
// traffic light 实测左缘 20、直径 13.5,中心即 26.75。侧栏每一行的行首元素
// (导航图标、品牌图、文件夹图标、组头的折叠 chevron)的**中心**都压在这条竖线上。
// 注意:中心对齐与左缘对齐是同一个自由度,只能满足一个——选了中心,
// 图标左缘就会落在 17.75,比红灯左缘还靠左 2.25,这是预期而非错位。
// 压轴的内边距一律经 `axis_inset(行首元素宽, 容器边距)` 从这条轴推出(侧栏行、组头、底部工具条、
// 设置窗导航),标题的首字宽是现量的(`sidebar_title`)。容易漏的是容器边距那一项:容器的
// 内边距改了,传给 axis_inset 的必须是同一个常量,不然行首元素整列离轴。

/// 侧栏中轴(100% 档)。界面缩放时 traffic light 跟着它挪(`ui_zoom::traffic_lights`)
pub const LEAD_AXIS: Zpx = Zpx(26.75);

/// 行首槽位:取最大前导元素(品牌图 18px)的尺寸;槽位内**居中**,
/// 于是 14/15px 的小图标中心也落在轴上。
pub const LEAD_BOX: Zpx = Zpx(18.);
/// 侧栏导航行的线条图标边长(品牌图占满 18px 槽位,线条图标比实心品牌图视觉轻,小一档
/// 才平衡);组头的折叠 chevron 同一档,描边粗细才一致
pub const NAV_ICON: Zpx = Zpx(15.);
/// 宽 `lead` 的行首元素中心压轴所需的左内边距:轴 − 半宽 − 容器自己的内边距 `edge`
pub const fn axis_inset(lead: Zpx, edge: Zpx) -> Zpx {
    Zpx(LEAD_AXIS.0 - lead.0 / 2. - edge.0)
}
/// 宽 `width` 的行首槽位,内容居中:比槽位小的图标中心也落在槽位中心上。槽位宽与传给
/// `axis_inset` 的 `lead` 是一对——侧栏行、设置窗导航、底部状态行的行首都经它
pub fn lead_slot(width: impl Into<Pixels>, child: impl IntoElement) -> Div {
    h_flex()
        .w(width.into())
        .flex_shrink_0()
        .justify_center()
        .child(child)
}
/// 行左内边距 = 26.75(轴) − 9(槽位半宽) − 10(SIDEBAR_EDGE,容器那一半)
pub const LEAD_INSET: Zpx = axis_inset(LEAD_BOX, SIDEBAR_EDGE);
/// 分组项(agent/项目)相对轴的右缩进,表达从属。
/// 压轴的只有主导航行与组头;分组项一律偏这么多。
pub const SUB_INDENT: Zpx = Zpx(12.);
// 组头(Agents / Projects)没有自己的内边距常量:NAV_ICON 的 chevron 按自己的框居中压轴
//(见 workbench.rs group_header);靠首字母字形宽度反推的 GROUP_HEAD_INSET 已删(2026-09-22)。
/// 侧栏标题(主窗的 "Wake" / "Memory",设置窗的 "Settings"):Heading semibold,首字的字形中心
/// 压在中轴上。首字按标题实际继承的字体现量(`layout_width` 走 gpui 的逐帧排版缓存,字号与字重
/// 就是这个 div 自己的),标题换页、换语言照样对得上——原先的 TITLE_INSET = 9 按 "Wake" 的 W 写死,
/// Memory 页与设置窗标题都偏(2026-10-11 用户报设置窗的 S 没压在红灯中心线上)。量的是字宽一半:
/// SF Pro 首字的左承距比右承距略大,字形实际中心再偏右约 0.15,在光栅化步长以内。div 从侧栏左缘
/// 起排,调用方只补纵向与右侧的内边距和颜色
pub fn sidebar_title(title: &'static str, window: &Window) -> Div {
    const WEIGHT: FontWeight = FontWeight::SEMIBOLD;
    let inset = title.chars().next().map_or(Pixels::ZERO, |first| {
        let mut font = window.text_style().font();
        font.weight = WEIGHT;
        let text = window.text_system();
        let width = text.layout_width(text.resolve_font(&font), FONT_HEADING.get(), first);
        (LEAD_AXIS.get() - width / 2.).max(Pixels::ZERO)
    });
    div()
        .pl(inset)
        .text_size(FONT_HEADING)
        .font_weight(WEIGHT)
        .child(title)
}
/// 侧栏主导航行高(固定区:All Sessions/Starred;搜索框同高)
pub const ROW_HEIGHT: Zpx = Zpx(32.);
/// 侧栏子级行高(分组展开项:agent/项目)——比主导航低一级,
/// 配 FONT_CAPTION 形成侧栏纵向层级(macOS 原生侧栏惯例)
pub const ROW_HEIGHT_SUB: Zpx = Zpx(26.);

/// 侧栏搜索入口与局部搜索输入共用的外壳;文字输入本身不再叠加组件默认边框。
pub fn search_field_frame(cx: &App) -> Div {
    let theme = cx.theme();
    h_flex()
        .h(ROW_HEIGHT)
        .px(SIDEBAR_EDGE)
        .gap(SPACE_SM)
        .rounded(theme.radius)
        .bg(theme.secondary)
        .text_size(FONT_CAPTION)
        .text_color(theme.muted_foreground)
        .hover(|s| {
            s.bg(theme.secondary_hover)
                .text_colored(theme.foreground, FONT_CAPTION)
        })
}

// ---------------- 圆角 ----------------
// 四档见 DESIGN.md:面板 12 = `theme.radius_lg`,列表与侧栏选择 8 = `theme.radius`
// (这两档走主题 token),其余三档无 token,在此定名以免散成魔法数字。
/// 按钮圆角。**用户钉死 6px,勿改**
pub const RADIUS_BUTTON: Zpx = Zpx(6.);

/// Page and dialog actions use fixed pixels; the library's rem-based Small
/// preset becomes 21px at Wake's 14px rem size and crowds mixed-size footers.
/// Normalize the inner label/icon/gap too: Button renders those in a nested
/// element, so setting the outer text_size alone does not override its size preset.
pub fn action_button(button: Button) -> Button {
    button
        .small()
        .h(BUTTON_ACTION_H)
        .px(SPACE_MD)
        .text_size(FONT_CAPTION)
        .rounded(RADIUS_BUTTON)
}

/// 快捷键标签
pub const RADIUS_KBD: Zpx = Zpx(5.);
/// 小胶囊 badge(项目名 / model / source / 各处计数共用)
pub const RADIUS_BADGE: Zpx = Zpx(4.);
/// 对话正文内联图片缩略图。
pub const IMAGE_THUMB: Zpx = Zpx(104.);
pub const RADIUS_IMAGE: Zpx = Zpx(10.);
pub const IMAGE_SCRIM: f32 = 0.62;
/// 数据可视化小色块(热力图、分布柱、图例),统一保持方格读数感。
pub const RADIUS_CELL: Zpx = Zpx(2.);

// ---------------- 组件度量派生 ----------------

/// gpui-component `Size::Small` 按钮的水平内边距(px_3 @ rem14 = 10.5)。
/// location 表单的"内容轴"对齐从它派生——组件升级或 rem 基准变更先核此值
pub const BUTTON_SM_PX: Zpx = Zpx(10.5);
/// gpui-component `Size::Small` 按钮高度(h_6 = 24),手排胶囊钮取齐用
pub const BUTTON_SM_H: Zpx = Zpx(24.);
/// 页面与确认框操作按钮的高度,避免与组件的 rem 尺寸混用。
pub const BUTTON_ACTION_H: Zpx = Zpx(32.);

// ---------------- 交互态文字 ----------------

/// gpui 陷阱兜底:`.hover()`/`.active()` 闭包的 text 样式对 base 是**整体替换**
/// (`Style.text` 无 `#[refineable]`,refine 直接覆盖)——闭包里只改文字色会把
/// base 的字号一并丢掉、回退窗口默认 14px。交互闭包内改文字色一律走本方法,
/// 把该元素 base 的字号原样重申;禁止在 hover/active 里裸调 `text_color`
/// (图标-only 元素除外:图标尺寸走 `with_size`,不受 text 替换影响)。
pub trait TextColored: Styled + Sized {
    fn text_colored(self, color: Hsla, size: impl Into<Pixels>) -> Self {
        self.text_color(color).text_size(size.into())
    }
}

impl<T: Styled + Sized> TextColored for T {}

/// Wake 主窗口的透明标题栏高度。28px 详情操作条上下各保留 8px。
pub const WINDOW_TITLEBAR_HEIGHT: Zpx = Zpx(44.);

// ---- 弹窗「点面板外关闭」补丁 ----
//
// gpui-component fd3bc2b 的 Dialog 把遮罩点击监听器挂在一个零高度的包装 div 上,永远
// hover 不到,`overlay_closable` 形同虚设(上游 df1d07b2「Restore the overlay behind an
// open dialog」已修;升级到含修复的版本后删掉本段,两个入口退化成 `window.open_dialog`
// 与 Root 的两层 overlay)。
//
// 原理:在窗口根节点、`Root::render_dialog_layer` 之后再挂一层全窗透明 sentinel。dialog
// 的宿主是 deferred、最后才画:遮罩是普通 hitbox 不挡鼠标,popup 是 occlude——于是鼠标
// 落在 popup 之外时 sentinel 才 hovered,它的 mouse_down 就是「点在面板外」。该不该关由
// 弹窗表态:经 `open_closable_dialog` 打开的弹窗每帧在 builder 里登记本窗口,
// `overlay_layers` 渲染完 dialog 层(builder 就在里面跑)立刻消费这个登记来决定挂不挂
// sentinel——AlertDialog、已关闭的弹窗没有登记,面板外点击不关。不像上游那样豁免标题栏
// 高度:弹窗开着时整窗被它的 occlude 层挡住、标题栏本来就拖不动,而弹窗顶边只在窗口高度
// 1/10 处,豁免带会把「框外上方」整段吞掉(2026-09-02 用户实测)。

/// 本帧登记过「可点面板外关闭」弹窗的窗口,`overlay_layers` 消费即清
#[derive(Default)]
struct ClosableDialogs(HashSet<WindowId>);
impl Global for ClosableDialogs {}

/// 弹窗宽度:设计稿宽度随缩放放大,但不超出窗口——gpui-component 的 Dialog 不会把
/// 自己夹进窗口,宽出来就两边一起出界(150% 档下 640 宽的弹窗就比最小窗口宽)
fn dialog_width(design: f32, window: &Window) -> Pixels {
    zpx(design).min(window.viewport_size().width - SPACE_XXL * 2.)
}

/// 打开可点面板外关闭的普通弹窗(确认类用 `open_alert`);Wake 里不要再裸调
/// `window.open_dialog` / `open_alert_dialog`。`width` 是设计稿宽度:两个入口都按上面的
/// 规则定宽、把组件写死的 16px 内边距换成随缩放的同值,build 里不必再写 `.w()`
pub fn open_closable_dialog<F>(window: &mut Window, cx: &mut App, width: f32, build: F)
where
    F: Fn(Dialog, &mut Window, &mut App) -> Dialog + 'static,
{
    window.open_dialog(cx, move |dialog, window, cx| {
        let id = window.window_handle().window_id();
        cx.default_global::<ClosableDialogs>().0.insert(id);
        let width = dialog_width(width, window);
        build(dialog.p(SPACE_LG), window, cx).w(width)
    });
}

/// 打开确认类弹窗(gpui-component 的 AlertDialog,面板外点击不关),尺寸规则同上
pub fn open_alert<F>(window: &mut Window, cx: &mut App, width: f32, build: F)
where
    F: Fn(AlertDialog, &mut Window, &mut App) -> AlertDialog + 'static,
{
    window.open_alert_dialog(cx, move |dialog, window, cx| {
        let width = dialog_width(width, window);
        build(dialog.p(SPACE_LG), window, cx).width(width)
    });
}

/// 破坏性确认框的按钮:取消 + 点名动作的 danger 主按钮,不留裸 "OK"。`button_props` 会整个
/// 换掉 `AlertDialog::confirm()` 开的取消按钮(confirm 只是把 show_cancel 置真),所以取消在
/// 这里开(用户 2026-10-13 报:隐藏确认框只有 Hide、没有 Cancel)
pub fn danger_buttons(ok: impl Into<SharedString>) -> DialogButtonProps {
    DialogButtonProps::default()
        .show_cancel(true)
        .cancel_text(crate::i18n::t("Cancel"))
        .ok_text(ok)
        .ok_variant(ButtonVariant::Danger)
}

/// 定宽栏(侧栏、会话流、设置侧栏,合计设计宽 `rails`)的缩放倍数:跟着界面缩放放大,
/// 但给窗口其余部分留出它们在 100% 档最小窗口里拿到的宽度(`min_window − rails`)——
/// 窗口最小尺寸只在开窗那一刻定、运行时改不了,档位一高就能把其余部分挤没;不够宽时
/// 等比收窄,最窄退回 100%
pub fn rails_factor(rails: f32, viewport_width: Pixels, min_window: Pixels) -> f32 {
    rails_factor_at(
        rails,
        f32::from(viewport_width),
        f32::from(min_window),
        crate::ui_zoom::factor(),
    )
}

fn rails_factor_at(rails: f32, viewport_width: f32, min_window: f32, factor: f32) -> f32 {
    ((viewport_width - (min_window - rails)) / rails)
        .min(factor)
        .max(1.)
}

/// 窗口根节点内容之后的三层 overlay:dialog、notification、点面板外关闭的 sentinel,
/// 顺序即契约(sentinel 必须在 dialog 层之后,才能盖在它的 occlude 层上面)
pub fn overlay_layers(window: &mut Window, cx: &mut App) -> Vec<AnyElement> {
    let dialogs = Root::render_dialog_layer(window, cx).map(IntoElement::into_any_element);
    let notifications =
        Root::render_notification_layer(window, cx).map(IntoElement::into_any_element);
    let id = window.window_handle().window_id();
    let sentinel = cx
        .default_global::<ClosableDialogs>()
        .0
        .remove(&id)
        .then(|| {
            div()
                .absolute()
                .inset_0()
                .on_any_mouse_down(|event, window, cx| {
                    if event.button != MouseButton::Left {
                        return;
                    }
                    cx.stop_propagation();
                    window.close_dialog(cx);
                })
                .into_any_element()
        });
    [dialogs, notifications, sentinel]
        .into_iter()
        .flatten()
        .collect()
}

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

    /// 非测试代码里裸 `px(` 的个数(`zpx(`、`.px(` 之类不算);`#[cfg(test)]` 的模块按
    /// 花括号配对跳过
    fn bare_px_count(src: &str) -> usize {
        let mut count = 0;
        let mut depth: Option<i32> = None;
        let mut gated = false;
        for line in src.lines() {
            let trimmed = line.trim_start();
            let delta = line.matches('{').count() as i32 - line.matches('}').count() as i32;
            if let Some(d) = depth.as_mut() {
                *d += delta;
                if *d <= 0 {
                    depth = None;
                }
                continue;
            }
            if trimmed.starts_with("#[cfg(") && trimmed.contains("test") {
                gated = true;
                continue;
            }
            if gated {
                if trimmed.starts_with("#[") {
                    continue;
                }
                gated = false;
                let is_mod = ["mod ", "pub mod ", "pub(crate) mod "]
                    .iter()
                    .any(|head| trimmed.starts_with(head));
                if is_mod && delta > 0 {
                    depth = Some(delta);
                    continue;
                }
            }
            count += line
                .match_indices("px(")
                .filter(|(at, _)| {
                    line[..*at]
                        .chars()
                        .next_back()
                        .is_none_or(|c| !(c.is_alphanumeric() || c == '_' || c == '.'))
                })
                .count();
        }
        count
    }

    /// 裸 `px` 不随界面缩放(issue #51):新增的尺寸要写 `zpx(…)` / `Zpx(…)`。这里按文件
    /// 数一遍,只许减不许增——真该是固定像素的(1px 发丝线、零值、已经按倍数算好的实际
    /// 宽度)就把上限改大,让这个决定在 review 里看得见
    #[test]
    fn bare_pixel_sizes_do_not_creep_back() {
        const FILES: &[(&str, &str, usize)] = &[
            ("workbench.rs", include_str!("workbench.rs"), 18),
            (
                "workbench/memory.rs",
                include_str!("workbench/memory.rs"),
                0,
            ),
            (
                "workbench/skills.rs",
                include_str!("workbench/skills.rs"),
                1,
            ),
            (
                "workbench/cleanup.rs",
                include_str!("workbench/cleanup.rs"),
                1,
            ),
            (
                "workbench/cleanup/filter.rs",
                include_str!("workbench/cleanup/filter.rs"),
                8,
            ),
            (
                "workbench/cleanup/history.rs",
                include_str!("workbench/cleanup/history.rs"),
                1,
            ),
            (
                "workbench/cleanup/rows.rs",
                include_str!("workbench/cleanup/rows.rs"),
                0,
            ),
            ("settings.rs", include_str!("settings.rs"), 2),
            ("ui.rs", include_str!("ui.rs"), 1),
            ("ui_zoom.rs", include_str!("ui_zoom.rs"), 2),
            ("main.rs", include_str!("main.rs"), 0),
            ("theme.rs", include_str!("theme.rs"), 0),
            ("markdown_links.rs", include_str!("markdown_links.rs"), 0),
        ];
        for (name, src, limit) in FILES {
            let count = bare_px_count(src);
            assert!(
                count <= *limit,
                "{name}: {count} bare px() calls (limit {limit}); sizes must use zpx()/Zpx to follow \
                 the zoom level, or raise the limit if this one really is a fixed pixel"
            );
        }
    }

    #[test]
    fn rails_keep_their_width_at_100_percent() {
        // 主窗:侧栏 224 + 会话流 336,最小窗口 940
        for width in [940., 1200., 2560.] {
            assert_eq!(rails_factor_at(560., width, 940., 1.), 1.);
        }
    }

    #[test]
    fn zoomed_rails_grow_but_leave_the_rest_room() {
        // 窗口够宽:按倍数放大
        assert_eq!(rails_factor_at(560., 2560., 940., 1.5), 1.5);
        // 不够宽:收窄到其余部分留够 380
        let factor = rails_factor_at(560., 1100., 940., 1.5);
        assert!((560. * factor - 720.).abs() < 0.01);
        // 最窄退回 100% 档;设置窗侧栏同一条规则
        assert_eq!(rails_factor_at(560., 940., 940., 1.5), 1.);
        assert_eq!(rails_factor_at(180., 720., 720., 1.5), 1.);
    }
}