Tutorial: A native piece
A native piece wraps a platform control behind a shared Rust API. Each supported toolkit needs an implementation that creates, updates, and measures the control. This tutorial creates an optional extension crate; ordinary search fields, combo boxes and pickers are already in core.
Use a native extension when a composition cannot provide the required input, rendering or accessibility behavior. For arrangements of existing controls, follow the composite-piece tutorial. For services with no view, follow the part tutorial.
Create the extension
Generate a crate and its demo app with the toolkits you intend to support:
day new piece day-piece-example-control --toolkits appkit,gtk,qt,uikit,material,winui
The generated package contains a shared frontend, per-toolkit renderer modules, Cargo features,
and the native build metadata required by those toolkits. Its demo supplies a place to exercise
the control independently. Add --local <path-to-day> to use a local framework checkout.
The maintained in-repository example is
day-piece-text-editor.
It owns its renderer modules and native shims. Core controls have a different ownership model:
frontends live in day-pieces, protocol types in day-spec, and renderers in each toolkit.
Do not add toolkit dependencies to day-pieces when implementing a new core control.
Define the protocol
Give the extension a unique kind string, a complete initial properties type, and a sparse patch enum. Initial properties must contain everything needed to create the native view. Each patch represents one subsequent change; unrelated state should remain untouched.
Keep those types independent of toolkit handles. The frontend implements
Piece::build, creates its
native node, and connects reactive values and events. The renderer registration generated by
the scaffold connects that kind to its toolkit implementation.
The frontend must seed initial state before mounting and subscribe to later changes in the piece’s scope. Native user changes update the binding. Programmatic changes must not echo back as fresh user edits: use a guard around native setters where the platform emits change events for both paths. Preserve native IME composition, selection and undo behavior.
Use generated resource accessors for placeholders, accessibility labels and other app-owned text in the demo. See localization.
Implement each renderer
Use the generated renderer modules as the starting point. A renderer creates a native handle, applies patches, and reports measurement appropriate to the view. A growing surface must respect the offered size; a compact control should retain its natural size.
Connect native events to Day’s event trampoline using the node identity supplied at creation. Keep callbacks on the UI thread, suppress setter echoes, and release retained callback state when the view is destroyed. Prefer native accessibility semantics and forward the Day label, identifier, enabled state and focus behavior.
The backend extension contract describes registration, native handles, measurement, lifecycle and container insertion. The Android JNI contract covers Java factories and failure handling. Check the actual generated source before copying a platform-specific snippet.
Declare native contributions
Keep the frontend available without selecting a backend. Toolkit features enable only that
renderer and its dependencies. List supported backends in [package.metadata.day.piece] so
Day’s build command can select the matching feature.
Native contributions belong to the extension that owns them:
- Qt and WinUI shims are compiled by its build script for the selected target.
- Android Java, resources, dependencies and manifest contributions use Day’s Android metadata.
- Apple Swift sources, frameworks and package dependencies use the relevant Apple metadata.
- HarmonyOS ArkTS and native contributions use the HarmonyOS metadata.
See the native contribution reference for the metadata keys and generated build artifacts. Declare permissions through the portable permissions contract where a portable permission exists.
Integrate and verify
Add the extension as a dependency of its demo. Day’s build command enables its declared backend feature. Direct Cargo commands require the corresponding feature explicitly.
Test the shared binding and patch behavior with the mock backend, then run the demo on each supported native toolkit. A mock test cannot prove that a native renderer registered or that IME, accessibility, focus and destruction work correctly.
Use dayscript to assert the control exists, change its value, observe the bound readout, and capture the result. Include a check for placeholder rendering. Exercise both programmatic updates and native user input, then remove and recreate the view to check cleanup. Run Clippy for every locally available SDK; keep unsupported host targets explicit in the validation report.
For a container, also verify child insertion, scrolling, clipping and child lifetime. Core pull-to-refresh demonstrates the behavior, but its implementation is now toolkit-owned rather than an optional extension package.
See testing, the text editor, and the independent webview repository for maintained examples and additional platform contracts.