home / skills / zhanghandong / makepad-skills / makepad-basics

makepad-basics skill

/skills/makepad-basics

This skill helps you create and modify Makepad apps using live_design patterns, app structure guides, and event handling examples.

npx playbooks add skill zhanghandong/makepad-skills --skill makepad-basics

Review the files below or copy the command above to add this skill to your agents.

Files (3)
SKILL.md
4.2 KB
---
name: makepad-basics
description: |
  CRITICAL: Use for Makepad getting started and app structure. Triggers on:
  makepad, makepad getting started, makepad tutorial, live_design!, app_main!,
  makepad project setup, makepad hello world, "how to create makepad app",
  makepad 入门, 创建 makepad 应用, makepad 教程, makepad 项目结构
---

# Makepad Basics Skill

> **Version:** makepad-widgets (dev branch) | **Last Updated:** 2026-01-19
>
> Check for updates: https://crates.io/crates/makepad-widgets

You are an expert at the Rust `makepad-widgets` crate. Help users by:
- **Writing code**: Generate Rust code following the patterns below
- **Answering questions**: Explain concepts, troubleshoot issues, reference documentation

## Documentation

Refer to the local files for detailed documentation:
- `./references/app-structure.md` - Complete app boilerplate and structure
- `./references/event-handling.md` - Event handling patterns

## 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

## Key Patterns

### 1. Basic App Structure

```rust
use makepad_widgets::*;

live_design! {
    use link::theme::*;
    use link::shaders::*;
    use link::widgets::*;

    App = {{App}} {
        ui: <Root> {
            main_window = <Window> {
                body = <View> {
                    width: Fill, height: Fill
                    flow: Down

                    <Label> { text: "Hello Makepad!" }
                }
            }
        }
    }
}

app_main!(App);

#[derive(Live, LiveHook)]
pub struct App {
    #[live] 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());
    }
}
```

### 2. Cargo.toml Setup

```toml
[package]
name = "my_app"
version = "0.1.0"
edition = "2024"

[dependencies]
makepad-widgets = { git = "https://github.com/makepad/makepad", branch = "dev" }
```

### 3. Handling Button Clicks

```rust
impl AppMain for App {
    fn handle_event(&mut self, cx: &mut Cx, event: &Event) {
        let actions = self.ui.handle_event(cx, event, &mut Scope::empty());

        if self.ui.button(id!(my_button)).clicked(&actions) {
            log!("Button clicked!");
        }
    }
}
```

### 4. Accessing and Modifying Widgets

```rust
// Get widget references
let label = self.ui.label(id!(my_label));
label.set_text("Updated text");

let input = self.ui.text_input(id!(my_input));
let text = input.text();
```

## API Reference Table

| Macro/Type | Description | Example |
|------------|-------------|---------|
| `live_design!` | Defines UI in DSL | `live_design! { App = {{App}} { ... } }` |
| `app_main!` | Entry point macro | `app_main!(App);` |
| `#[derive(Live)]` | Derive live data | `#[derive(Live, LiveHook)]` |
| `WidgetRef` | Reference to UI tree | `#[live] ui: WidgetRef` |
| `Cx` | Context for rendering | `fn handle_event(&mut self, cx: &mut Cx, ...)` |
| `id!()` | Widget ID macro | `self.ui.button(id!(my_button))` |

## Platform Setup

| Platform | Requirements |
|----------|--------------|
| macOS | Works out of the box |
| Windows | Works out of the box |
| Linux | `apt-get install clang libaudio-dev libpulse-dev libx11-dev libxcursor-dev` |
| Web | `cargo install wasm-pack` |

## When Writing Code

1. Always include required imports: `use makepad_widgets::*;`
2. Use `live_design!` macro for all UI definitions
3. Implement `LiveRegister` and `AppMain` traits
4. Use `id!()` macro for widget references
5. Handle events through `handle_event` method

## When Answering Questions

1. Emphasize live design - changes in DSL reflect instantly without recompilation
2. Makepad is GPU-first - all rendering is shader-based
3. Cross-platform: same code runs on Android, iOS, Linux, macOS, Windows, Web
4. Recommend UI Zoo example for widget exploration

Overview

This skill helps you get started with Makepad and the makepad-widgets crate, focusing on app structure, live design, and common patterns for Rust-based UI apps. It provides ready-to-use code patterns, platform setup notes, and guidance for event handling and widget access. Use it to bootstrap projects, troubleshoot common issues, and follow recommended Makepad conventions.

How this skill works

The skill inspects and uses the makepad-widgets patterns: live_design! DSL for UI, app_main! entry point, Live-derived app structs, and the AppMain handle_event flow. It generates Rust snippets that include required imports and trait implementations, explains event handling and widget access (id!() macros), and references platform preparation steps. When local reference docs are available, the skill incorporates them into answers; if not, it will warn and proceed with built-in patterns.

When to use it

  • Creating a new Makepad app or hello-world project
  • Understanding and implementing the live_design! UI DSL
  • Wiring event handling and button/click logic with AppMain
  • Accessing and updating widgets (labels, inputs) via WidgetRef and id!()
  • Preparing cross-platform builds (desktop, web, mobile)

Best practices

  • Always include use makepad_widgets::*; and define UI with live_design!
  • Implement LiveRegister to register live assets and AppMain for event loop handling
  • Use id!() consistently for stable widget references and Scope::empty() for simple event scopes
  • Favor live DSL edits during development — changes reflect in the live system without full recompiles
  • Follow platform-specific package requirements (Linux dev libs, wasm-pack for Web)

Example use cases

  • Generate a minimal Makepad app with main window, a label, and app_main! entry
  • Show button click handling code and logging in handle_event
  • Update a label or read a text_input using self.ui.label(...) and self.ui.text_input(...)
  • Provide platform setup checklist for Linux and Web builds
  • Troubleshoot common compile/runtime errors and suggest fixes based on live design patterns

FAQ

Do I need extra imports or macros to start?

Yes. Always include use makepad_widgets::*;, define UI with live_design!, and use app_main!(App); as the entry point.

How do I handle a button click?

Call self.ui.handle_event(...) inside handle_event, capture returned actions, then check self.ui.button(id!(my_button)).clicked(&actions) to run click logic.