Skip to content

About

DrawnUI for Rust is part of drawnui.net: a Skia-drawn UI engine, the same controls and contract as DrawnUI for .NET and React, on the desktop, mobile and the web.

Topics

Resources

Accessibility

Stars

2 stars

Watchers

0 watching

Forks

Repository files navigation

DrawnUI for Rust

DrawnUI for Rust: the Hello app on a desktop window, in a browser and on phones, and Dungeon Run

Build rich, fluid UI by drawing it, from one Rust source, on every screen. Every control is painted by Skia on one hardware-accelerated canvas: layouts, smooth lists, animations, shaders, 3D transforms, gestures, keyboard and screen readers, on Windows, macOS, Linux, iOS, Android and in the browser. No garbage collector, no native widgets to fight: the same pixels everywhere, from a few lines of fluent Rust.

use drawnui::prelude::*;

#[derive(Default)]
struct App { count: i32 }

fn build(_app: &mut App) -> Build<SkiaLayout> {
    SkiaLayout::column().spacing(16).padding(24).children((
        SkiaLabel::new("").font_size(24).text_color(Color::WHITE)
            .observe(|me, app: &App| me.set_text(format!("Count {}", app.count))),
        SkiaButton::new("Tap me").on_tapped(|_me, app: &mut App, _cx| app.count += 1),
    ))
}

fn main() {
    drawnui::run("Counter", || Box::new(Ui::new(App::default(), build).font("Default", "assets/OpenSans-Regular.ttf")));
}

One family

DrawnUI for Rust is the same engine as DrawnUI for .NET (MAUI, Blazor, WPF, OpenTK, WebAssembly) and DrawnUI for React: the same controls, property names, layout and drawing rules. What you learn here works there, and screens move between them.

MIT, everywhere

DrawnUI is MIT licensed in every language and on every platform: desktop, mobile, web and embedded devices. Ship it in commercial products with no attribution badge, no about screen and no per-device royalty.

Why DrawnUI for Rust

  • Complete UI engine, ready to use. Rendering, layouts, gestures, a full set of UI controls and the building blocks to make your own: everything you need to create pixel-perfect apps, out of the box.
  • Same pixels everywhere. Every control is drawn by Skia, so your app looks the same on a phone, a desktop and a web page. No native widgets to fight.
  • Smooth by design. No garbage collector, so there are no collection pauses in the middle of a scroll or an animation. Caches keep finished parts of the screen as pictures or GPU textures and draw them again instead of repainting them.
  • Fast on the web. Your code and Skia live in one WebAssembly module and talk directly, with no JavaScript in between: up to 1.6 times less CPU per frame than CanvasKit driven from JavaScript (see Benchmarks).
  • Fluent and familiar. Builders in the style of DrawnUI's C# fluent API: compose, observe your state, handle a tap in one line.
  • Accessible. Screen readers on every platform, keyboard navigation with a focus ring, values a screen reader reads and changes.
  • AI-ready. Ready-made AI skill teaches Claude Code, Codex, Cursor and other coding agents to build DrawnUI apps the right way, from the first cargo new to the browser and Android builds.
  • Nothing to set up. Add one dependency. Skia comes prebuilt for your platform.

Platforms

Platform Graphics Skia prebuilt for
Windows OpenGL x64, ARM64
macOS Metal, paced by the display Apple silicon, Intel
Linux OpenGL, X11 and Wayland x64, ARM64
iOS Metal, 120 Hz on ProMotion screens devices, simulator (Apple silicon and Intel)
Android Vulkan, OpenGL ES as fallback arm64, armv7, x86_64, x86
Browser WebGL2 (wasm32-unknown-emscripten) all browsers with WebGL2

What you get, all drawn on one canvas

  • Layouts: absolute, column, row, wrap, masonry and grids; layers with any alignment, z-index, clipping.
  • Lists that stay smooth: recycled and virtualized templated lists, items of different heights, load more.
  • Controls: labels and rich text (right-to-left and complex scripts, emoji), buttons, editor with IME, toggles, switch, checkbox, radio buttons, slider, progress, scroll with pull to refresh.
  • Navigation: shell with pages, tabs, popups, modals and toasts, carousels and drawer.
  • Animations: value, range, spring, ping-pong and ripple animators, Lottie, GIF and sprites.
  • Transforms in 2D and 3D: rotate, scale, skew, perspective and depth; taps still land on the right control.
  • Effects: SkSL shaders on any control, shader transitions, backdrop blur, SkMesh with your own vertex and fragment programs.
  • Gestures: tap, pan, fling, long press, hover, context menu, mouse wheel and touchpad.
  • Keyboard: Tab, arrows, Enter and Escape move through every control, list, menu and dialog.
  • Accessibility: UI Automation on Windows, VoiceOver on macOS and iOS, TalkBack on Android, Orca on Linux, an ARIA layer in the browser.
  • Fast: every DrawnUI cache type, images decoded off the frame thread, bitmaps made on background threads, a lost GPU context that comes back on its own.
  • Tested: about 900 headless tests, many of them ports of DrawnUI for .NET's tests with the same numbers.

Please star ⭐ if you like it!

Crates

Crate What it is Version
drawnui engine: controls, layouts, caches, gestures, animations, accessibility, desktop, mobile and browser hosts 1.4.3
drawnui-add3d 3D add-on: real 3D models in DrawnUI screens (glTF scenes with skeleton animations, many objects on one game field, shapes made in code); demo at add3d.drawnui.net 1.0.1
drawnui-audio sound add-on: channels that play samples in Impulse Tracker's model (volume, panning, loops, pitch), streams, generators and mixer, on desktop, phones and browser 0.1.0
drawnui-skia-safe Skia for Rust: rust-skia's skia-safe plus SkMesh; drawnui re-exports it as drawnui::skia 0.153.7
drawnui-skia-bindings native Skia under drawnui-skia-safe, downloaded prebuilt from DrawnUi/rust-skia 0.153.7

An app depends on drawnui only; the Skia crates come with it. Use Skia's API through drawnui::skia (the same as skia_safe, SkMesh included) and do not add skia-safe or skia-bindings to an app: a program links one Skia.

Getting started

Fastest way: copy templates/app, rename myapp, build. It is an empty app with an icon, assets, a web page and build scripts for the desktop, the browser and Android.

Or by hand, in a new project (cargo new myapp): cargo add drawnui, or in Cargo.toml:

[dependencies]
# The newest 1.x; `cargo update` moves a project to a newer one.
drawnui = "1.4"

[profile.release]
lto = "thin"
panic = "abort"

The first build downloads Skia for your platform (11 to 25 MB) from DrawnUi/rust-skia's releases. SKIA_BINARIES_URL points the download at a mirror. Do not set FORCE_SKIA_BINARIES_DOWNLOAD: inside a crate it makes skia-bindings compile Skia from source.

.cargo/config.toml, for the browser's link flags and Android's 16 KB pages:

[target.wasm32-unknown-emscripten]
linker = "em++"
rustflags = [
    "-C", "link-arg=-sMAX_WEBGL_VERSION=2",
    "-C", "link-arg=-sMODULARIZE=1",
    "-C", "link-arg=-sEXPORT_NAME=createDrawnUi",
    "-C", "link-arg=-sEXPORTED_RUNTIME_METHODS=GL,HEAPU8,UTF8ToString",
    "-C", "link-arg=-sALLOW_MEMORY_GROWTH=1",
    # A 1 MB stack with the overflow check: layout and paint walk the tree recursively.
    "-C", "link-arg=-sSTACK_SIZE=1048576",
    "-C", "link-arg=-sSTACK_OVERFLOW_CHECK=1",
    "-C", "link-arg=-sERROR_ON_UNDEFINED_SYMBOLS=0",
]

[target.'cfg(target_os = "android")']
rustflags = ["-C", "link-arg=-Wl,-z,max-page-size=16384"]

The web page loads your build and drawnui_host.js from the same drawnui version (the template's web.ps1 / web.sh copy it for you):

<canvas id="canvas" style="width:100vw;height:100dvh;display:block;touch-action:none"></canvas>
<script src="myapp.js"></script>
<script src="drawnui_host.js"></script>
<script>DrawnUi.start({ canvas: document.getElementById("canvas"), create: createDrawnUi });</script>

Per platform:

  • Windows, Linux, macOS: cargo run --release. Assets go next to the executable (the template's build.rs), icon.ico becomes the window and taskbar icon. Linux needs the X11 / Wayland and GL development packages.

  • Browser: rustup target add wasm32-unknown-emscripten, emsdk 6 with its tools on PATH, then the template's web.ps1 / web.sh; serve dist/ over http.

  • Android: cargo-ndk and NDK 26; cargo ndk ... rustc --lib --release --crate-type cdylib makes the activity's library (android_main in the template's src/lib.rs), packed by Gradle with a GameActivity (templates/fiddle has the Gradle project and build scripts). Add the winit and android-activity fixes to your Cargo.toml:

    [patch.crates-io]
    winit = { git = "https://github.com/DrawnUi/winit", rev = "fd3685990f74fb3e8917ee56f3a33690b9471d16" }
    android-activity = { git = "https://github.com/DrawnUi/android-activity", rev = "d45520c015edc340c16770c2266d711327ce01f1" }
  • iOS: dev/ios builds an .app bundle for the simulator, or a signed one for a device. Apps built with the iOS 27 SDK must adopt the scene lifecycle: the app's Info.plist names drawnui's scene delegate (the scripts write it):

    <key>UIApplicationSceneManifest</key>
    <dict>
        <key>UIApplicationSupportsMultipleScenes</key><false/>
        <key>UISceneConfigurations</key>
        <dict>
            <key>UIWindowSceneSessionRoleApplication</key>
            <array>
                <dict>
                    <key>UISceneConfigurationName</key><string>Default Configuration</string>
                    <key>UISceneDelegateClassName</key><string>DrawnUiSceneDelegate</string>
                </dict>
            </array>
        </dict>
    </dict>
  • Hot reload in the browser (feature hot-reload): the app is built as an emscripten side module and swapped into the running engine with its state. Link that module with -Bsymbolic, so it runs its own code and not the previous module's; see hotreload.md.

AI skills

Building with an AI coding agent? Give it the DrawnUI for Rust skill and it writes DrawnUI code the way the engine expects: adding drawnui, fluent builders, controls and layouts, caching, the browser build with its link flags and web page, Android, icons, accessibility.

Benchmarks

The same draw list, N rounded rectangles per frame, 1609x1163 canvas, Chrome on Windows, WebGL2:

Draw calls DrawnUI for Rust (Rust + Skia in one wasm module) CanvasKit 0.42 driven from JS
2,000 59.9 FPS, 2.8 ms CPU 60.0 FPS, 3.7 ms CPU
5,000 60.0 FPS, 5.1 ms CPU 59.8 FPS, 7.4 ms CPU
20,000 59.7 FPS, 12.5 ms CPU 47.8 FPS, 20.6 ms CPU

Windows desktop (OpenGL): 20,000 draw calls at 60 FPS, 10 to 12 ms CPU. MacBook Air M1 (Metal): 20,000 draw calls at a steady 60 FPS, 10.3 ms CPU. SkMesh: 2,000 animated waves (a vertex program each, one draw call each) in 6.1 ms CPU on the M1, and at 60 FPS in 6.4 ms on a Windows desktop.

The web build links only the Skia code it uses:

Build raw brotli
gl 3.93 MB 1.15 MB
gl + text layout (ICU, HarfBuzz) + SVG 7.39 MB 2.27 MB
CanvasKit 0.42 wasm, for scale 7.32 MB 2.28 MB

Android: Vulkan by default

A phone with a Mali-G57 MC2, 1080x2408, a 90 Hz panel, the same release build on each API. On heavy scenes Vulkan draws 21 to 35 % more frames; where both keep up with the panel, it needs 26 to 59 % less CPU per frame.

Scene Vulkan OpenGL ES Vulkan better by
20,000 draw calls 40.2 FPS, 24.8 ms a frame 33.3 FPS, 30.0 ms +21 % FPS
2,000 SkMesh waves 57.2 FPS, 17.5 ms 42.4 FPS, 23.6 ms +35 % FPS
2,000 animated shapes 91.6 FPS, CPU 5.4 ms 91.6 FPS, CPU 7.3 ms -26 % CPU
HelloRust: Recycled cells, flings 91.6 FPS, CPU 1.45 ms 91.6 FPS, CPU 2.71 ms -46 % CPU
HelloRust: Uneven cells, flings 91.6 FPS, CPU 0.94 ms 91.6 FPS, CPU 2.32 ms -59 % CPU
HelloRust: Lottie & GIF 91.6 FPS, CPU 1.94 ms 91.6 FPS, CPU 3.67 ms -47 % CPU
HelloRust: Shaders 91.6 FPS, CPU 1.60 ms 91.6 FPS, CPU 2.75 ms -42 % CPU

OpenGL ES stays available as the fallback and as an option (Ui::gpu_backend).

Building this repository

cargo test -p drawnui -p hellorust                   # headless tests, no window
cargo run --release -p hellorust                     # the demo
cd examples/bench && cargo run --release -- 20000 g0  # 20,000 draw calls
cd examples/bench && cargo run --release -- 2000 mesh # 2,000 animated SkMesh waves
dev/build.ps1 -Example hellorust -WebOnly            # the browser build into target/web/hellorust

The workspace builds against three checkouts next to this repository:

  • ../rust-skia: DrawnUi/rust-skia, branch drawnui-crates (git clone -b drawnui-crates https://github.com/DrawnUi/rust-skia.git). Skia downloads prebuilt, as it does for apps.
  • ../winit and ../android-activity: upstream plus one fix each; the commands are in dev/winit-android and dev/android-activity-null-text.

To work on Skia itself, dev/skia-binaries.ps1 builds it from source once per target and exports the archive every later build unpacks. Our notes on each platform, for anyone bringing a Rust + Skia library to it: solved-win.md, solved-wasm.md, solved-mac.md, solved-ios.md, solved-android.md, solved-linux.md.

Repository layout

  • drawnui/: engine crate; drawnui/web/drawnui_host.js is the browser host.
  • examples/hello/: counter. examples/hellorust/: DrawnUI Hello app. examples/bench/: the benchmark scene. examples/images/: a photo feed. examples/dungeon/: Dungeon Run. examples/popups/: a right-click menu and a dropdown in popup windows.
  • templates/app/: starter app.
  • dev/: build, release and platform scripts.

License

MIT, see LICENSE.

About

DrawnUI for Rust is part of drawnui.net: a Skia-drawn UI engine, the same controls and contract as DrawnUI for .NET and React, on the desktop, mobile and the web.

Topics

Resources

Accessibility

Stars

2 stars

Watchers

0 watching

Forks

Releases

Sponsor this project

Packages

Contributors

Languages