pub trait Platform: 'static {
Show 81 methods
// Required methods
fn background_executor(&self) -> BackgroundExecutor;
fn foreground_executor(&self) -> ForegroundExecutor;
fn text_system(&self) -> Arc<dyn PlatformTextSystem> ⓘ;
fn run(&self, on_finish_launching: Box<dyn FnOnce() + 'static>);
fn quit(&self);
fn restart(&self, binary_path: Option<PathBuf>);
fn activate(&self, ignoring_other_apps: bool);
fn hide(&self);
fn hide_other_apps(&self);
fn unhide_other_apps(&self);
fn displays(&self) -> Vec<Rc<dyn PlatformDisplay>>;
fn primary_display(&self) -> Option<Rc<dyn PlatformDisplay>>;
fn active_window(&self) -> Option<AnyWindowHandle>;
fn open_window(
&self,
handle: AnyWindowHandle,
options: WindowParams,
) -> Result<Box<dyn PlatformWindow>>;
fn window_appearance(&self) -> WindowAppearance;
fn open_url(&self, url: &str);
fn on_open_urls(&self, callback: Box<dyn FnMut(Vec<String>)>);
fn register_url_scheme(&self, url: &str) -> Task<Result<()>> ⓘ;
fn prompt_for_paths(
&self,
options: PathPromptOptions,
) -> Receiver<Result<Option<Vec<PathBuf>>>> ⓘ;
fn prompt_for_new_path(
&self,
directory: &Path,
suggested_name: Option<&str>,
) -> Receiver<Result<Option<PathBuf>>> ⓘ;
fn can_select_mixed_files_and_dirs(&self) -> bool;
fn reveal_path(&self, path: &Path);
fn open_with_system(&self, path: &Path);
fn on_quit(&self, callback: Box<dyn FnMut()>);
fn on_reopen(&self, callback: Box<dyn FnMut()>);
fn set_menus(&self, menus: Vec<Menu>, keymap: &Keymap);
fn set_dock_menu(&self, menu: Vec<MenuItem>, keymap: &Keymap);
fn on_app_menu_action(&self, callback: Box<dyn FnMut(&dyn Action)>);
fn on_will_open_app_menu(&self, callback: Box<dyn FnMut()>);
fn on_validate_app_menu_command(
&self,
callback: Box<dyn FnMut(&dyn Action) -> bool>,
);
fn thermal_state(&self) -> ThermalState;
fn on_thermal_state_change(&self, callback: Box<dyn FnMut()>);
fn app_path(&self) -> Result<PathBuf>;
fn path_for_auxiliary_executable(&self, name: &str) -> Result<PathBuf>;
fn set_cursor_style(&self, style: CursorStyle);
fn hide_cursor_until_mouse_moves(&self);
fn is_cursor_visible(&self) -> bool;
fn should_auto_hide_scrollbars(&self) -> bool;
fn read_from_clipboard(&self) -> Option<ClipboardItem>;
fn write_to_clipboard(&self, item: ClipboardItem);
fn read_from_primary(&self) -> Option<ClipboardItem>;
fn write_to_primary(&self, item: ClipboardItem);
fn write_credentials(
&self,
url: &str,
username: &str,
password: &[u8],
) -> Task<Result<()>> ⓘ;
fn read_credentials(
&self,
url: &str,
) -> Task<Result<Option<(String, Vec<u8>)>>> ⓘ;
fn delete_credentials(&self, url: &str) -> Task<Result<()>> ⓘ;
fn keyboard_layout(&self) -> Box<dyn PlatformKeyboardLayout>;
fn keyboard_mapper(&self) -> Rc<dyn PlatformKeyboardMapper>;
fn on_keyboard_layout_change(&self, callback: Box<dyn FnMut()>);
// Provided methods
fn window_stack(&self) -> Option<Vec<AnyWindowHandle>> { ... }
fn is_screen_capture_supported(&self) -> bool { ... }
fn screen_capture_sources(
&self,
) -> Receiver<Result<Vec<Rc<dyn ScreenCaptureSource>>>> ⓘ { ... }
fn reduce_motion_enabled(&self) -> bool { ... }
fn button_layout(&self) -> Option<WindowButtonLayout> { ... }
fn get_menus(&self) -> Option<Vec<OwnedMenu>> { ... }
fn perform_dock_menu_action(&self, _action: usize) { ... }
fn add_recent_document(&self, _path: &Path) { ... }
fn update_jump_list(
&self,
_menus: Vec<MenuItem>,
_entries: Vec<SmallVec<[PathBuf; 2]>>,
) -> Task<Vec<SmallVec<[PathBuf; 2]>>> ⓘ { ... }
fn compositor_name(&self) -> &'static str { ... }
fn set_tray(
&self,
_tray: Tray,
_menus: Option<Vec<MenuItem>>,
_keymap: &Keymap,
) { ... }
fn set_tray_icon(&self, _icon: Option<&[u8]>) { ... }
fn set_tray_menu(&self, _menu: Vec<TrayMenuItem>) { ... }
fn set_tray_tooltip(&self, _tooltip: &str) { ... }
fn set_tray_panel_mode(&self, _enabled: bool) { ... }
fn get_tray_icon_bounds(&self) -> Option<Bounds<Pixels>> { ... }
fn on_tray_icon_event(&self, _callback: Box<dyn FnMut(TrayIconEvent)>) { ... }
fn on_tray_menu_action(&self, _callback: Box<dyn FnMut(SharedString)>) { ... }
fn register_global_hotkey(
&self,
_id: u32,
_keystroke: &Keystroke,
) -> Result<()> { ... }
fn unregister_global_hotkey(&self, _id: u32) { ... }
fn on_global_hotkey(&self, _callback: Box<dyn FnMut(u32)>) { ... }
fn show_notification(&self, _title: &str, _body: &str) -> Result<()> { ... }
fn set_auto_launch(&self, _app_id: &str, _enabled: bool) -> Result<()> { ... }
fn is_auto_launch_enabled(&self, _app_id: &str) -> bool { ... }
fn focused_window_info(&self) -> Option<FocusedWindowInfo> { ... }
fn check_permission(&self, _kind: PermissionType) -> PermissionStatus { ... }
fn request_permission(&self, _kind: PermissionType) { ... }
fn on_system_power_event(&self, _callback: Box<dyn FnMut(SystemPowerEvent)>) { ... }
fn on_system_wake(&self, _callback: Box<dyn FnMut()>) { ... }
fn start_power_save_blocker(
&self,
_kind: PowerSaveBlockerKind,
) -> Option<Box<dyn PowerSaveBlocker>> { ... }
fn system_idle_time(&self) -> Option<Duration> { ... }
fn network_status(&self) -> NetworkStatus { ... }
fn os_info(&self) -> OsInfo { ... }
}Expand description
重导出平台抽象层相关类型 跨平台应用抽象层,由各平台 crate(rgpui-windows、rgpui-macos、rgpui-linux、rgpui-web)实现。
提供应用生命周期、窗口管理、系统集成(托盘、快捷键、通知、电源等)的统一接口。
应用通过 [rgpui_platform::application()] 获取实现此 trait 的实例。
Required Methods§
Sourcefn background_executor(&self) -> BackgroundExecutor
fn background_executor(&self) -> BackgroundExecutor
返回后台线程执行器,用于调度异步任务。
Sourcefn foreground_executor(&self) -> ForegroundExecutor
fn foreground_executor(&self) -> ForegroundExecutor
返回主线程执行器,用于调度需要在 UI 线程运行的任务。
Sourcefn text_system(&self) -> Arc<dyn PlatformTextSystem> ⓘ
fn text_system(&self) -> Arc<dyn PlatformTextSystem> ⓘ
返回文本渲染系统实例,负责字体加载、文本布局和渲染。
Sourcefn run(&self, on_finish_launching: Box<dyn FnOnce() + 'static>)
fn run(&self, on_finish_launching: Box<dyn FnOnce() + 'static>)
启动应用主循环,on_finish_launching 在启动完成后回调。
Sourcefn activate(&self, ignoring_other_apps: bool)
fn activate(&self, ignoring_other_apps: bool)
激活应用(将窗口置于前台),ignoring_other_apps 在 macOS 下是否忽略其他应用。
Sourcefn hide_other_apps(&self)
fn hide_other_apps(&self)
隐藏当前应用以外的所有其他应用的窗口。
Sourcefn unhide_other_apps(&self)
fn unhide_other_apps(&self)
取消隐藏所有被 hide_other_apps 隐藏的应用。
Sourcefn displays(&self) -> Vec<Rc<dyn PlatformDisplay>>
fn displays(&self) -> Vec<Rc<dyn PlatformDisplay>>
返回所有可用显示器的列表。
Sourcefn primary_display(&self) -> Option<Rc<dyn PlatformDisplay>>
fn primary_display(&self) -> Option<Rc<dyn PlatformDisplay>>
返回主显示器(包含任务栏/菜单栏的显示器)。
Sourcefn active_window(&self) -> Option<AnyWindowHandle>
fn active_window(&self) -> Option<AnyWindowHandle>
返回当前获得焦点的窗口句柄。
Sourcefn open_window(
&self,
handle: AnyWindowHandle,
options: WindowParams,
) -> Result<Box<dyn PlatformWindow>>
fn open_window( &self, handle: AnyWindowHandle, options: WindowParams, ) -> Result<Box<dyn PlatformWindow>>
根据窗口参数创建平台原生窗口,返回 PlatformWindow 实例。
Sourcefn window_appearance(&self) -> WindowAppearance
fn window_appearance(&self) -> WindowAppearance
返回应用窗口的外观模式(亮色/暗色)。
Sourcefn open_url(&self, url: &str)
fn open_url(&self, url: &str)
在系统默认浏览器中打开 URL。
这是 fire-and-forget 调用:各平台异步启动外部程序,失败只记日志, 调用方无法从此方法得知是否成功(部分平台会回退到门户 API)。
Sourcefn on_open_urls(&self, callback: Box<dyn FnMut(Vec<String>)>)
fn on_open_urls(&self, callback: Box<dyn FnMut(Vec<String>)>)
注册 URL scheme 回调,当应用通过自定义 URL scheme 打开时触发。
各平台从各自的系统入口派发:macOS 由 AppKit 的 application:openURLs: 调用;
Linux 在回调注册时重放本次启动命令行里由桌面入口 %u 传入的 URL;
Windows 当前只保存回调、没有派发点。
Sourcefn register_url_scheme(&self, url: &str) -> Task<Result<()>> ⓘ
fn register_url_scheme(&self, url: &str) -> Task<Result<()>> ⓘ
注册自定义 URL scheme(如 myapp://),使系统将该 scheme 的 URL 分发到本应用。
Sourcefn prompt_for_paths(
&self,
options: PathPromptOptions,
) -> Receiver<Result<Option<Vec<PathBuf>>>> ⓘ
fn prompt_for_paths( &self, options: PathPromptOptions, ) -> Receiver<Result<Option<Vec<PathBuf>>>> ⓘ
打开文件选择对话框,返回用户选择的文件路径列表。
Sourcefn prompt_for_new_path(
&self,
directory: &Path,
suggested_name: Option<&str>,
) -> Receiver<Result<Option<PathBuf>>> ⓘ
fn prompt_for_new_path( &self, directory: &Path, suggested_name: Option<&str>, ) -> Receiver<Result<Option<PathBuf>>> ⓘ
打开文件保存对话框,返回用户指定的保存路径。
Sourcefn can_select_mixed_files_and_dirs(&self) -> bool
fn can_select_mixed_files_and_dirs(&self) -> bool
文件选择对话框是否支持同时选择文件和目录。
Sourcefn reveal_path(&self, path: &Path)
fn reveal_path(&self, path: &Path)
在系统文件管理器中显示(reveal)指定路径。
同 open_url:fire-and-forget,失败只记日志,调用方无从判断结果。
Sourcefn open_with_system(&self, path: &Path)
fn open_with_system(&self, path: &Path)
使用系统默认应用打开指定路径。
设置应用菜单栏(macOS 为全局菜单栏,其他平台为窗口菜单)。
设置 macOS Dock 栏右键菜单。
注册应用菜单操作回调,当用户点击菜单项时触发。
注册菜单即将打开时的回调(可用于动态更新菜单项状态)。
注册菜单命令验证回调,返回 false 可禁用菜单项。
Sourcefn thermal_state(&self) -> ThermalState
fn thermal_state(&self) -> ThermalState
返回系统热状态(正常、警告、临界)。
Sourcefn on_thermal_state_change(&self, callback: Box<dyn FnMut()>)
fn on_thermal_state_change(&self, callback: Box<dyn FnMut()>)
注册系统热状态变化回调。
Sourcefn path_for_auxiliary_executable(&self, name: &str) -> Result<PathBuf>
fn path_for_auxiliary_executable(&self, name: &str) -> Result<PathBuf>
返回辅助可执行文件的路径(如子进程、插件)。
Sourcefn set_cursor_style(&self, style: CursorStyle)
fn set_cursor_style(&self, style: CursorStyle)
设置鼠标光标样式(箭头、手型、文本光标等)。
Sourcefn hide_cursor_until_mouse_moves(&self)
fn hide_cursor_until_mouse_moves(&self)
隐藏鼠标光标,直到用户移动鼠标时自动恢复显示。
Sourcefn is_cursor_visible(&self) -> bool
fn is_cursor_visible(&self) -> bool
返回鼠标光标当前是否可见。
Sourcefn should_auto_hide_scrollbars(&self) -> bool
fn should_auto_hide_scrollbars(&self) -> bool
是否自动隐藏滚动条(鼠标靠近时才显示)。
Sourcefn read_from_clipboard(&self) -> Option<ClipboardItem>
fn read_from_clipboard(&self) -> Option<ClipboardItem>
从系统剪贴板读取内容。
Sourcefn write_to_clipboard(&self, item: ClipboardItem)
fn write_to_clipboard(&self, item: ClipboardItem)
写入内容到系统剪贴板。
Sourcefn read_from_primary(&self) -> Option<ClipboardItem>
fn read_from_primary(&self) -> Option<ClipboardItem>
从 Linux/X11 主选择区(Primary Selection)读取内容。
Sourcefn write_to_primary(&self, item: ClipboardItem)
fn write_to_primary(&self, item: ClipboardItem)
写入内容到 Linux/X11 主选择区。
Sourcefn write_credentials(
&self,
url: &str,
username: &str,
password: &[u8],
) -> Task<Result<()>> ⓘ
fn write_credentials( &self, url: &str, username: &str, password: &[u8], ) -> Task<Result<()>> ⓘ
将凭据(URL、用户名、密码)写入系统密钥链。
Sourcefn read_credentials(&self, url: &str) -> Task<Result<Option<(String, Vec<u8>)>>> ⓘ
fn read_credentials(&self, url: &str) -> Task<Result<Option<(String, Vec<u8>)>>> ⓘ
从系统密钥链读取指定 URL 的凭据。
Sourcefn keyboard_layout(&self) -> Box<dyn PlatformKeyboardLayout>
fn keyboard_layout(&self) -> Box<dyn PlatformKeyboardLayout>
返回当前键盘布局信息。
Sourcefn keyboard_mapper(&self) -> Rc<dyn PlatformKeyboardMapper>
fn keyboard_mapper(&self) -> Rc<dyn PlatformKeyboardMapper>
返回键盘映射器,用于将原始按键事件转换为字符输入。
Sourcefn on_keyboard_layout_change(&self, callback: Box<dyn FnMut()>)
fn on_keyboard_layout_change(&self, callback: Box<dyn FnMut()>)
注册键盘布局变化回调(用户切换输入法/布局时触发)。
Provided Methods§
Sourcefn window_stack(&self) -> Option<Vec<AnyWindowHandle>>
fn window_stack(&self) -> Option<Vec<AnyWindowHandle>>
返回窗口栈(Z-order),从最顶层到最底层。
Sourcefn is_screen_capture_supported(&self) -> bool
fn is_screen_capture_supported(&self) -> bool
当前平台是否支持屏幕捕获功能。
Sourcefn screen_capture_sources(
&self,
) -> Receiver<Result<Vec<Rc<dyn ScreenCaptureSource>>>> ⓘ
fn screen_capture_sources( &self, ) -> Receiver<Result<Vec<Rc<dyn ScreenCaptureSource>>>> ⓘ
获取可用的屏幕捕获源列表(屏幕/窗口),通过 oneshot channel 异步返回。
Sourcefn reduce_motion_enabled(&self) -> bool
fn reduce_motion_enabled(&self) -> bool
返回系统是否开启了「减少动态效果」无障碍设置。
默认 false,即未覆写的平台保持动画开启;核心层由
App::reduce_motion 消费此值。
返回窗口按钮布局配置(如 macOS 红绿灯位置、Windows 按钮顺序)。
获取当前应用菜单的副本。
执行 Dock 菜单中的操作。
Sourcefn add_recent_document(&self, _path: &Path)
fn add_recent_document(&self, _path: &Path)
将路径添加到最近打开文档列表。
Sourcefn update_jump_list(
&self,
_menus: Vec<MenuItem>,
_entries: Vec<SmallVec<[PathBuf; 2]>>,
) -> Task<Vec<SmallVec<[PathBuf; 2]>>> ⓘ
fn update_jump_list( &self, _menus: Vec<MenuItem>, _entries: Vec<SmallVec<[PathBuf; 2]>>, ) -> Task<Vec<SmallVec<[PathBuf; 2]>>> ⓘ
更新 Windows 跳转列表(任务栏右键菜单中的最近文档)。
Sourcefn compositor_name(&self) -> &'static str
fn compositor_name(&self) -> &'static str
返回合成器名称(如 “dwm”、“mutter”),用于诊断。
Sourcefn set_tray(&self, _tray: Tray, _menus: Option<Vec<MenuItem>>, _keymap: &Keymap)
fn set_tray(&self, _tray: Tray, _menus: Option<Vec<MenuItem>>, _keymap: &Keymap)
设置系统托盘图标、菜单和按键绑定。
Sourcefn set_tray_icon(&self, _icon: Option<&[u8]>)
fn set_tray_icon(&self, _icon: Option<&[u8]>)
更新系统托盘图标,None 表示移除图标。
更新系统托盘右键菜单。
Sourcefn set_tray_tooltip(&self, _tooltip: &str)
fn set_tray_tooltip(&self, _tooltip: &str)
设置鼠标悬停在托盘图标上时显示的工具提示文本。
Sourcefn set_tray_panel_mode(&self, _enabled: bool)
fn set_tray_panel_mode(&self, _enabled: bool)
设置托盘面板模式(Windows 下影响图标的显示行为)。
Sourcefn get_tray_icon_bounds(&self) -> Option<Bounds<Pixels>>
fn get_tray_icon_bounds(&self) -> Option<Bounds<Pixels>>
返回系统托盘图标在屏幕上的边界矩形。
Sourcefn on_tray_icon_event(&self, _callback: Box<dyn FnMut(TrayIconEvent)>)
fn on_tray_icon_event(&self, _callback: Box<dyn FnMut(TrayIconEvent)>)
注册托盘图标事件回调(单击、双击、右键等)。
注册托盘菜单项操作回调。
Sourcefn register_global_hotkey(&self, _id: u32, _keystroke: &Keystroke) -> Result<()>
fn register_global_hotkey(&self, _id: u32, _keystroke: &Keystroke) -> Result<()>
注册全局系统快捷键,id 用于标识快捷键,keystroke 定义按键组合。
注意:trait 默认实现返回 Ok(()) 但什么都不做。未覆盖此方法的后端会
「注册成功」却永远收不到按键;Wayland 会如实返回不支持的错误。
Sourcefn unregister_global_hotkey(&self, _id: u32)
fn unregister_global_hotkey(&self, _id: u32)
取消注册全局系统快捷键。
Sourcefn on_global_hotkey(&self, _callback: Box<dyn FnMut(u32)>)
fn on_global_hotkey(&self, _callback: Box<dyn FnMut(u32)>)
注册全局快捷键触发回调,id 对应注册时的标识。
Sourcefn show_notification(&self, _title: &str, _body: &str) -> Result<()>
fn show_notification(&self, _title: &str, _body: &str) -> Result<()>
显示系统通知,返回 Ok(()) 表示通知已发送。
trait 默认实现返回 Ok(()) 但什么都不做(1.4.1 批量修过一批这类
「假实现」)。Linux 无头构建会如实报错;调用方不能只看 Ok 判断送达。
Sourcefn set_auto_launch(&self, _app_id: &str, _enabled: bool) -> Result<()>
fn set_auto_launch(&self, _app_id: &str, _enabled: bool) -> Result<()>
设置开机自启动,app_id 为应用唯一标识。
trait 默认实现返回 Ok(()) 但什么都不做;is_auto_launch_enabled 同理
恒返回 false。未覆盖的后端上不要据此判断自启已生效。
Sourcefn is_auto_launch_enabled(&self, _app_id: &str) -> bool
fn is_auto_launch_enabled(&self, _app_id: &str) -> bool
查询开机自启动是否已启用。
Sourcefn focused_window_info(&self) -> Option<FocusedWindowInfo>
fn focused_window_info(&self) -> Option<FocusedWindowInfo>
返回当前系统中获得焦点的窗口信息(标题、进程名等)。
Wayland 原生会话恒为 None(没有读取其它客户端窗口信息的协议),
调用方无法区分「不支持」与「当前没有焦点窗口」。macOS 当前只提供
app_name / bundle_id / pid,window_title 为空。
Sourcefn check_permission(&self, _kind: PermissionType) -> PermissionStatus
fn check_permission(&self, _kind: PermissionType) -> PermissionStatus
查询指定类别的系统权限状态。
这是权限类别的统一入口:PermissionType 里的每一项都要能问,
不要再为单个类别增设 xxx_status 特例方法 —— 特例只会让某一平台
已经做好的判定逻辑在应用层根本没有调用点。
无法判定(系统没有这个概念、或总线/服务不可达)时返回
PermissionStatus::Unavailable,不要返回 Granted 假装可用。
Sourcefn request_permission(&self, _kind: PermissionType)
fn request_permission(&self, _kind: PermissionType)
请求指定类别的系统权限:有系统授权弹窗的就弹窗,没有的会输出可操作的引导日志。
Sourcefn on_system_power_event(&self, _callback: Box<dyn FnMut(SystemPowerEvent)>)
fn on_system_power_event(&self, _callback: Box<dyn FnMut(SystemPowerEvent)>)
注册系统电源事件回调(系统即将睡眠 / 已从睡眠唤醒)。
Sourcefn on_system_wake(&self, _callback: Box<dyn FnMut()>)
fn on_system_wake(&self, _callback: Box<dyn FnMut()>)
注册系统唤醒时的回调函数。
Sourcefn start_power_save_blocker(
&self,
_kind: PowerSaveBlockerKind,
) -> Option<Box<dyn PowerSaveBlocker>>
fn start_power_save_blocker( &self, _kind: PowerSaveBlockerKind, ) -> Option<Box<dyn PowerSaveBlocker>>
启动电源阻止器:阻止系统休眠或息屏。
返回的句柄持有即生效、Drop 即释放,所以没有配套的
stop_power_save_blocker(id) —— 「返回 ID、再按 ID 停止」的形状会把底层
资源(Linux 上是 logind Inhibit 返回的 fifo fd)泄漏在平台内部,
进程退出前抑制一直挂着,应用忘没忘停都无人知晓。
平台不支持或判定通道不可达时返回 None,不要假装阻止成功。
Sourcefn system_idle_time(&self) -> Option<Duration>
fn system_idle_time(&self) -> Option<Duration>
返回系统空闲时间(自上次用户输入以来的时长)。
Sourcefn network_status(&self) -> NetworkStatus
fn network_status(&self) -> NetworkStatus
返回当前网络连接状态。
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".