| 名称 | makepad-platform |
| 描述 |
Makepad平台技能
版本: makepad-widgets (开发分支) | 最后更新: 2026-01-19
您是Makepad跨平台开发专家。通过以下方式帮助用户:
- 理解平台:解释支持的平台和后端
- 平台特定代码:帮助处理条件编译和平台API
文档
参考本地文件获取详细文档:
./references/platform-support.md- 平台详情和OsType
重要:文档完整性检查
在回答问题之前,Claude必须:
- 阅读上面列出的相关参考文件
- 如果文件读取失败或文件为空:
- 通知用户:“本地文档不完整,建议运行
/sync-crate-skills makepad --force更新文档” - 仍然基于SKILL.md模式加内置知识回答
- 通知用户:“本地文档不完整,建议运行
- 如果参考文件存在,将其内容纳入答案中
支持平台
| 平台 | 图形后端 | 操作系统模块 |
|---|---|---|
| macOS | Metal | apple/metal_*.rs, apple/cocoa_*.rs |
| iOS | Metal | apple/metal_*.rs, apple/ios_*.rs |
| Windows | D3D11 | mswindows/d3d11_*.rs, mswindows/win32_*.rs |
| Linux | OpenGL | linux/opengl_*.rs, linux/x11*.rs, linux/wayland*.rs |
| Web | WebGL2 | web/*.rs, web_browser/*.rs |
| Android | OpenGL ES | android/*.rs |
| OpenHarmony | OHOS | open_harmony/*.rs |
| OpenXR | VR/AR | open_xr/*.rs |
OsType枚举
pub enum OsType {
Unknown,
Windows,
Macos,
Linux { custom_window_chrome: bool },
Ios,
Android(AndroidParams),
OpenHarmony,
Web(WebParams),
OpenXR,
}
// 在代码中检查平台
fn handle_event(&mut self, cx: &mut Cx, event: &Event) {
match cx.os_type() {
OsType::Macos => { /* macOS特定 */ }
OsType::Windows => { /* Windows特定 */ }
OsType::Web(_) => { /* Web特定 */ }
_ => {}
}
}
平台检测
// 在Cx中
impl Cx {
pub fn os_type(&self) -> OsType;
pub fn gpu_info(&self) -> &GpuInfo;
pub fn xr_capabilities(&self) -> &XrCapabilities;
pub fn cpu_cores(&self) -> usize;
}
条件编译
// 编译时平台检测
#[cfg(target_os = "macos")]
fn macos_only() { }
#[cfg(target_os = "windows")]
fn windows_only() { }
#[cfg(target_os = "linux")]
fn linux_only() { }
#[cfg(target_arch = "wasm32")]
fn web_only() { }
#[cfg(target_os = "android")]
fn android_only() { }
#[cfg(target_os = "ios")]
fn ios_only() { }
平台特定功能
桌面 (macOS/Windows/Linux)
- 窗口管理(调整大小、最小化、最大化)
- 文件对话框
- 系统菜单
- 拖放
- 多显示器
移动 (iOS/Android)
- 触摸输入
- 虚拟键盘
- 屏幕方向
- 应用生命周期(前台/后台)
Web (WebGL2)
- DOM集成
- 浏览器事件
- 本地存储
- HTTP请求
入口点
// 应用入口宏
app_main!(App);
pub struct App {
ui: WidgetRef,
}
impl LiveRegister for App {
fn live_register(cx: &mut Cx) {
// 注册组件
crate::makepad_widgets::live_design(cx);
}
}
impl AppMain for App {
fn handle_event(&mut self, cx: &mut Cx, event: &Event) {
// 处理应用事件
self.ui.handle_event(cx, event, &mut Scope::empty());
}
}
回答问题时的注意事项
- Makepad为每个平台编译为原生代码(无运行时解释器)
- 着色器在构建时为每个图形后端编译
- 平台特定代码位于
platform/src/os/目录 - 使用
cx.os_type()进行运行时平台检测 - 使用
#[cfg(target_os = "...")]进行编译时平台检测