Co-located native views (NativeView)
Updated: 2026-11-20. When an application needs a platform-specific control or surface that Bern's standard component set does not expose (such as AppKit NSLevelIndicator / NSDatePicker, WinUI 3 NumberBox / CalendarView, Android AndroidView(RatingBar / CalendarView), GTK 4 LevelBar / Calendar, or a custom GPUI / Web canvas), NativeView embeds co-located native source files right beside the .rs UI file that declares it.
Unlike runtime bridge escape hatches, NativeView keeps all semantics, localized strings, CLI entries and fallback behavior in the Rust declaration:
- Co-located files: Backend paths (
swiftui,winui3,compose,gtk,gpui,web) resolve relative to the declaring.rsfile. Directory scanning (sources = "pkg/src") skips*.gtk.rs,*.gpui.rsand*.native.rsautomatically. - No runtime internals exposed: Each backend generates narrow, strongly typed
Props,ActionsandThemestructs for the view instead of exposingBernRuntimeinternals. - Mandatory CLI entries: Every
NativeViewmust declare at least oneNativeAction,NativeNumber,NativeBoolorNativeInputchild. All six backends register those entries automatically under<component>.<view>.<entry>soui inspect,ui activate,ui set-number,ui set-bool,ui editandui submitwork without piercing native widget trees. - Default full-width layout: Without an explicit
width:prop,NativeViewfills the container's available width across all six backends so native panels align cleanly insideColumnlayouts. Passwidth:for a fixed width. - Localization and linting: Pass user-visible text via
NativeText(name:, content: tr! { ... })or entrylabel:s. The compiler also runs a lightweight lint over co-located native files and warns on obvious hardcoded display strings (suppress individual lines with// bern-ignore-l10n). - Progressive cross-platform coverage: Provide
Fallback { ... }for targets without a native file. Targets that have neither a matching backend file nor aFallbackfail atbern check/bern build.
Declaration structure
NativeView(
id: meter,
label: tr! { "zh-Hans" => "原生电平仪表", "en" => "Native Level Meter" },
swiftui: "meter.swiftui.swift",
winui3: "meter.winui3.cpp",
compose: "meter.compose.kt",
gtk: "meter.gtk.rs",
gpui: "meter.gpui.rs",
web: "meter.web.js",
) {
NativeText(name: heading, content: tr! { "zh-Hans" => "原生电平与步进器", "en" => "Native Meter & Stepper" })
NativeDraft(name: hover, value: draft.hover_level)
NativeNumber(id: level, label: tr! { "zh-Hans" => "电平", "en" => "Level" }, current: app.level, min: 0, max: 10, step: 1) => app.set_level(app.level)
NativeBool(id: boosted, label: tr! { "zh-Hans" => "增强模式", "en" => "Boost Mode" }, checked: app.boosted) => app.set_boosted(app.boosted)
NativeAction(id: reset, label: tr! { "zh-Hans" => "重置", "en" => "Reset" }) => app.reset()
Fallback {
Slider(id: level_fallback, label: tr! { "zh-Hans" => "电平", "en" => "Level" }, current: app.level, min: 0, max: 10) => app.set_level(app.level)
}
}Generated types per backend
For each NativeView, the backend exposes:
- Every
NativeValue(name:, value:)andNativeText(name:, content:)as a read-only field inProps. - Every
NativeDraft(name:, value:)as a read-only field inPropsand aset_<name>/set<Name>callback inActions. - Every entry (
NativeAction,NativeNumber,NativeBool,NativeInput) with its localized<id>_label/<id>LabelinProps, current value (level,boosted,date, plusmin/max/ optionalstepforNativeNumber), and its typed callback inActions(reset,set_level/setLevel,set_boosted/setBoosted,set_date/setDate). - A read-only
Themewithdark/is_darkand semantic colors (accent,surface,text_primary/textPrimary,text_secondary/textSecondary,bordermapped tocontrol.border).
| Backend | File suffix | Entry point & naming | Props read model |
Build integration |
|---|---|---|---|---|
| SwiftUI | .swift |
struct <IdPascal>NativeView: View { let props: <IdPascal>NativeProps; let actions: <IdPascal>NativeActions; let theme: BernNativeTheme } (camelCase fields) |
Direct values (SwiftUI tracks @Observable reads automatically) |
Passed directly by original path to swiftc (or rewritten into Sources/ for swiftui-16) |
| WinUI 3 / Islands | .cpp / .inl |
inline xaml::UIElement build(Props const& props, Actions const& actions, Theme const& theme) inside namespace native_<sym> (snake_case fields) |
Reactive getters std::function<T()> read inside Reactive::Run(...) |
#include "<abs_path>" inside app_ui.inl (compiled for both bern::winui with BERN_WINUI=1 and bern::islands with BERN_WINUI=0) |
| Compose | .kt |
@Composable fun NativeContent(props: Props, actions: Actions, theme: Theme) in <pkg>.native_<sym> (camelCase fields) |
Direct immutable data class values on each recomposition |
Emitted into <pkg>/native_<sym>/NativeContent.kt and preserved in Gradle Mirror |
| GTK 4 | .gtk.rs |
pub fn build(props: Props, actions: Actions, theme: Theme) -> impl IsA<gtk::Widget> inside mod native_<sym> (snake_case fields) |
Reactive getters Rc<dyn Fn() -> T> read inside bern_gtk::reactive::effect(...) |
#[path = "<abs_path>"] mod impl_; inside generated app/src/ui.rs |
| GPUI | .gpui.rs |
pub fn render(props: Props, actions: Actions, theme: Theme) -> impl IntoElement inside mod native_<sym> (snake_case fields) |
Direct snapshot values per render frame | #[path = "<abs_path>"] mod impl_; inside generated app/src/ui.rs |
| Web | .js |
export function mount({ host, props, actions, theme, effect, onCleanup }) (camelCase fields) |
Reactive getters () => val read inside the passed effect(...); dispose in onCleanup(...) |
Emitted as ES module native/<sym>.js, statically imported by app.js and included in bern package |
Scalar & draft type mapping
| Rust type | SwiftUI | WinUI 3 / Islands | Compose | GTK 4 / GPUI | Web |
|---|---|---|---|---|---|
bool |
Bool |
bool |
Boolean |
bool |
boolean |
i64 |
Int64 |
int64_t |
Long |
i64 |
number |
f64 |
Double |
double |
Double |
f64 |
number |
String |
String |
winrt::hstring |
String |
String |
string |
Option<f64> |
Double? |
std::optional<double> |
Double? |
Option<f64> |
number | null |
Platform notes
- Apple (
swiftui): One.swiftfile serves macOS, iOS, tvOS and watchOS; branch platform-specific controls with#if os(macOS) ... #else ... #endif. - Windows (
winui3):app_ui.inlcompiles once inbern::winui(BERN_WINUI == 1) and once inbern::islands(BERN_WINUI == 0). Use the sharedxaml::/controls::/media::aliases and guard WinUI 3-only controls (such ascontrols::NumberBox) with#if BERN_WINUI. Capture WinRT handles by value inReactive::Run([=]() mutable { ... }). - Android (
compose): Use standard Compose Material 3 composables or embed classic Android widgets throughandroidx.compose.ui.viewinterop.AndroidView. Omitpackagedeclarations in*.compose.kt; the generator hoists yourimportlines under the per-view subpackage. - Linux (
gtk):bern-gtkre-exportsgtk,adw,gioandglib. Readpropsinsidebern_gtk::reactive::effect(...)and guard widget signal callbacks against re-entrant sync updates. - Web (
web): Use theeffectandonCleanupcallbacks passed intomount(...)instead of re-importing./bern.jsdirectly. Browser-native ESM URL imports work directly without a bundler.
Verification (examples/native-controls)
Complete six-backend showcase in examples/native-controls (meter and calendar views). Verified on 2026-11-19 / 2026-11-20:
- macOS (SwiftUI + AppKit): Built and run on real macOS (
NSLevelIndicator+Gauge+Stepper+NSDatePicker); full CLI end-to-end verification (ui inspect,set-number,set-bool,edit,set-language,activate) passed. - Windows 11 (WinUI 3): Built and run on real Windows 11 (
ProgressBar+NumberBox+ToggleSwitch+CalendarView); full-width layout and full CLI end-to-end verification passed. - Windows 10 VM (XAML Islands): Run on Windows 10 Pro 22H2 (
ProgressBar+-/+stepper +ToggleSwitch+CalendarView); full-width layout and full CLI end-to-end verification passed. - Ubuntu Linux VM (GTK 4 / libadwaita): Built and run on GNOME Wayland (
gtk::LevelBar+gtk::SpinButton+gtk::ToggleButton+gtk::Calendar); full-width layout and full CLI end-to-end verification passed. - iOS 26.5 Simulator (iPhone 17 Pro): Built and run (
Gauge+ProgressView+Stepper+ graphicalDatePicker); full-width layout and full CLI end-to-end verification passed. - Android Emulator (Pixel 9 Pro XL): Built APK, installed and launched (
CircularProgressIndicator+AndroidView(RatingBar)+FilterChip+AndroidView(CalendarView)); full-width rendering verified visually. Remote Android CLI overadb forwardwas not separately verified. - GPUI (macOS): Built and run offscreen (
meter.gpui.rssegmented meter +calendar.gpui.rscalendar grid); full-width rendering and CLI operations (ui inspect,set-number,set-bool) verified. - Web: Passed
bern check, unit tests and code generation; not yet run interactively in a browser.