← 稳定版文档 · dev 普通文档 · WASM 交互画廊 →

Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

常用功能:不止是组件

组件解决“长什么样”,功能解决“怎么做成一件事”。这一章把应用里反复出现的几类需求串起来: 主题、快捷键、通知、配置、复制、命令面板和手势。每节都给出最小 API 用法,并在交互文档里配了可运行页面。

线上入口:交互组件文档 → 顶部 tab「常用功能」→ 侧栏选择页面。

交互文档中的常用功能分类

主题与颜色

亮/暗切换不要在组件里写死颜色:

#![allow(unused)]
fn main() {
use rgpui::{Theme, ThemeMode};

Theme::change(ThemeMode::Dark, Some(window), cx);

let primary = cx.theme().primary;
let background = cx.theme().background;
}

组件统一读 cx.theme(),切换后整页一起变化。需要更多预设主题时开启 bundled-themes feature; 完整主题系统见 组件库 的「主题访问」一节。

交互页:主题切换 Theme。

快捷键与动作

快捷键不是直接调函数,而是三段式:定义 Action → bind_keys 绑定 → 视图用 on_action 监听。 按钮也可以通过 dispatch_action 触发同一个动作,保证键盘和鼠标走同一条路径。

#![allow(unused)]
fn main() {
use rgpui::{Action, KeyBinding};

#[derive(Action, PartialEq, Eq, Clone, Copy)]
struct DocsShortcut;

// 构造页面时注册键绑定
cx.bind_keys([KeyBinding::new("cmd-k", DocsShortcut, None)]);

// 监听放在 render 的元素上(构造期调 cx.on_action 会触发绘制阶段断言)
div()
    .id("shortcut-root")
    .track_focus(&self.focus)
    .on_action(cx.listener(|this: &mut MyView, _: &DocsShortcut, _, cx| {
        this.hits += 1;
        cx.notify();
    }));

cx.dispatch_action(&DocsShortcut); // 按钮里复用同一个动作
}

track_focus 的区域要先拿到焦点;交互页里点击该区域即可聚焦后再按快捷键。

更多原理见 快捷键系统。交互页:快捷键与动作 Keymap。

通知与吐司

按用途分三层:

场景用什么
应用内消息中心、未读计数、已读/清空NotificationCenterState / NotificationCenter / NotificationBell
轻量、自动消失的浮层提示Toast / ToastStack
系统级通知条,带操作按钮menu::Notification / NotificationList
#![allow(unused)]
fn main() {
use rgpui::components::{
    NotificationCenter, NotificationCenterState, NotificationItem, NotificationVariant,
};

let state = cx.new(NotificationCenterState::new);
state.update(cx, |state, cx| {
    state.add(
        NotificationItem::new("build", "构建完成")
            .message("全部页面编译通过")
            .variant(NotificationVariant::Success),
        cx,
    );
});
NotificationCenter::new(state).max_visible(4);
}

交互页:通知中心 Notification。

配置持久化

设置项用 ConfigStore 读写 JSON,配置类型只要实现 serde 的 Serialize / Deserialize / Default:

#![allow(unused)]
fn main() {
use rgpui::config_store::ConfigStore;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Default, Clone)]
struct Settings {
    nickname: String,
    compact: bool,
}

let mut store = ConfigStore::new("my-app");
let settings: Settings = store.load().unwrap_or_default();
store.save(&settings)?;
println!("{}", store.config_file().display());
}

写入后可以用 on_change 同步其它视图。交互文档写入系统临时目录,避免污染真实应用配置。 交互页:配置持久化 ConfigStore。

一键复制

文档站所有代码块右上角的复制按钮都是 components::Clipboard,不是自己画的图标:

#![allow(unused)]
fn main() {
use rgpui::components::Clipboard;

Clipboard::new("copy-snippet")
    .value("Switch::new(\"live\").checked(true)")
    .tooltip("复制代码")
    .aria_label("复制代码");
}

值需要运行时计算时用 .value_fn(|window, cx| ...);复制成功后图标短暂变成对勾。 交互页:一键复制 Clipboard。

命令面板与搜索

命令条目用 Command 描述名称、描述、图标、快捷键和 on_select;面板用 CommandPalette 承载搜索、排序和键盘导航。 只需要筛选一组数据时,用 SearchPanelState 更轻。

#![allow(unused)]
fn main() {
use rgpui::components::{Command, CommandPalette};

let commands = vec![
    Command::new("docs.new", "新建文档")
        .description("创建一个空白文档")
        .shortcut("Cmd-N")
        .on_select(|_, cx| { /* 执行命令 */ }),
];
let palette = cx.new(|cx| CommandPalette::new(window, cx, commands));
}

交互页:命令面板 Command;搜索面板见交互文档「数据与输入 / 搜索面板」。

手势识别

轻点、双击、长按、滑动和拖动用 GestureDetector 状态机,不要在视图里自己记按下时间和位移:

#![allow(unused)]
fn main() {
use rgpui::mouse_gestures::GestureDetector;

let mut detector = GestureDetector::new();
let events = detector.on_mouse_down(event.position);
let events = detector.on_mouse_move(event.position);
let events = detector.on_mouse_up(event.position);
}

交互页:手势识别 Gesture。

其它已有页面

对话框、上传、搜索、Markdown、图表、日期/颜色、Toast 等功能在交互文档里也都有独立页面, 分布在「输入与导航」「数据与输入」「布局与内容」「进阶组件」几个分类。本章只收 “跨组件的工作流”,不重复列组件;按意图找组件仍以 组件速查 为准。