makepad-platform

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Makepad Platform Skill

Makepad 平台开发技能

Version: makepad-widgets (dev branch) | Last Updated: 2026-01-19
You are an expert at Makepad cross-platform development. Help users by:
  • Understanding platforms: Explain supported platforms and backends
  • Platform-specific code: Help with conditional compilation and platform APIs
版本: makepad-widgets(dev分支)| 最后更新: 2026-01-19
您是Makepad跨平台开发专家。请通过以下方式帮助用户:
  • 理解平台:解释支持的平台和后端
  • 平台特定代码:协助处理条件编译和平台API

Documentation

文档参考

Refer to the local files for detailed documentation:
  • ./references/platform-support.md
    - Platform details and OsType
请参考本地文件获取详细文档:
  • ./references/platform-support.md
    - 平台详情及OsType说明

IMPORTANT: Documentation Completeness Check

重要提示:文档完整性检查

Before answering questions, Claude MUST:
  1. Read the relevant reference file(s) listed above
  2. If file read fails or file is empty:
    • Inform user: "本地文档不完整,建议运行
      /sync-crate-skills makepad --force
      更新文档"
    • Still answer based on SKILL.md patterns + built-in knowledge
  3. If reference file exists, incorporate its content into the answer
在回答问题前,Claude必须:
  1. 阅读上述列出的相关参考文件
  2. 如果文件读取失败或文件为空:
    • 告知用户:"本地文档不完整,建议运行
      /sync-crate-skills makepad --force
      更新文档"
    • 仍需基于SKILL.md模板及内置知识进行回答
  3. 如果参考文件存在,需将其内容融入回答中

Supported Platforms

支持的平台

PlatformGraphics BackendOS Module
macOSMetal
apple/metal_*.rs
,
apple/cocoa_*.rs
iOSMetal
apple/metal_*.rs
,
apple/ios_*.rs
WindowsD3D11
mswindows/d3d11_*.rs
,
mswindows/win32_*.rs
LinuxOpenGL
linux/opengl_*.rs
,
linux/x11*.rs
,
linux/wayland*.rs
WebWebGL2
web/*.rs
,
web_browser/*.rs
AndroidOpenGL ES
android/*.rs
OpenHarmonyOHOS
open_harmony/*.rs
OpenXRVR/AR
open_xr/*.rs
平台图形后端操作系统模块
macOSMetal
apple/metal_*.rs
,
apple/cocoa_*.rs
iOSMetal
apple/metal_*.rs
,
apple/ios_*.rs
WindowsD3D11
mswindows/d3d11_*.rs
,
mswindows/win32_*.rs
LinuxOpenGL
linux/opengl_*.rs
,
linux/x11*.rs
,
linux/wayland*.rs
WebWebGL2
web/*.rs
,
web_browser/*.rs
AndroidOpenGL ES
android/*.rs
OpenHarmonyOHOS
open_harmony/*.rs
OpenXRVR/AR
open_xr/*.rs

OsType Enum

OsType 枚举

rust
pub enum OsType {
    Unknown,
    Windows,
    Macos,
    Linux { custom_window_chrome: bool },
    Ios,
    Android(AndroidParams),
    OpenHarmony,
    Web(WebParams),
    OpenXR,
}

// Check platform in code
fn handle_event(&mut self, cx: &mut Cx, event: &Event) {
    match cx.os_type() {
        OsType::Macos => { /* macOS-specific */ }
        OsType::Windows => { /* Windows-specific */ }
        OsType::Web(_) => { /* Web-specific */ }
        _ => {}
    }
}
rust
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特定逻辑 */ }
        _ => {}
    }
}

Platform Detection

平台检测

rust
// In 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;
}
rust
// 在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;
}

Conditional Compilation

条件编译

rust
// Compile-time platform detection
#[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() { }
rust
// 编译时平台检测
#[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() { }

Platform-Specific Features

平台特定功能

Desktop (macOS/Windows/Linux)

桌面端(macOS/Windows/Linux)

  • Window management (resize, minimize, maximize)
  • File dialogs
  • System menu
  • Drag and drop
  • Multiple monitors
  • 窗口管理(调整大小、最小化、最大化)
  • 文件对话框
  • 系统菜单
  • 拖放功能
  • 多显示器支持

Mobile (iOS/Android)

移动端(iOS/Android)

  • Touch input
  • Virtual keyboard
  • Screen orientation
  • App lifecycle (foreground/background)
  • 触摸输入
  • 虚拟键盘
  • 屏幕方向
  • 应用生命周期(前台/后台)

Web (WebGL2)

Web端(WebGL2)

  • DOM integration
  • Browser events
  • Local storage
  • HTTP requests
  • DOM集成
  • 浏览器事件
  • 本地存储
  • HTTP请求

Entry Point

入口点

rust
// App entry macro
app_main!(App);

pub struct App {
    ui: WidgetRef,
}

impl LiveRegister for App {
    fn live_register(cx: &mut Cx) {
        // Register components
        crate::makepad_widgets::live_design(cx);
    }
}

impl AppMain for App {
    fn handle_event(&mut self, cx: &mut Cx, event: &Event) {
        // Handle app events
        self.ui.handle_event(cx, event, &mut Scope::empty());
    }
}
rust
// 应用入口宏
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());
    }
}

When Answering Questions

回答问题时的注意事项

  1. Makepad compiles to native code for each platform (no runtime interpreter)
  2. Shaders are compiled at build time for each graphics backend
  3. Platform-specific code is in
    platform/src/os/
    directory
  4. Use
    cx.os_type()
    for runtime platform detection
  5. Use
    #[cfg(target_os = "...")]
    for compile-time platform detection
  1. Makepad会为每个平台编译为原生代码(无运行时解释器)
  2. 着色器会在构建时针对每个图形后端进行编译
  3. 平台特定代码位于
    platform/src/os/
    目录下
  4. 使用
    cx.os_type()
    进行运行时平台检测
  5. 使用
    #[cfg(target_os = "...")]
    进行编译时平台检测