C+
macOS

A Facet counter on AppKit

This example uses the 0.0.30 Facet application model. The component and its tree are platform-neutral; facet_runtime selects the macOS facade and facet_appkit mounts native AppKit controls. A named window owns the screen, its navigation state, and its lifecycle.

Expected result

Running the compiled binary opens a native 360 × 200 macOS NSWindow titled Counter containing a vertical flex column (padding: 20, gap: 10) with an NSTextField label and an NSButton:

┌──────────────────────────────────────────┐
│ ● ● ●              Counter               │
├──────────────────────────────────────────┤
│                                          │
│   Clicked 3 time(s)      [key: "count"]  │
│                                          │
│   ┌──────────────┐                       │
│   │   Click me   │   [key: "increment"]  │
│   └──────────────┘                       │
│                                          │
└──────────────────────────────────────────┘

Each click invokes Counter::on_click, increments this.clicks with wrapping addition (+%), looks up the mounted "count" label via label::find("count"), and updates the native NSTextField in place without rebuilding the window.

  • Requirements: macOS (Apple Silicon), cpc 0.0.30, Xcode Command Line Tools (xcode-select --install).
  • Build & run:
cpc build
./target/debug/counter

Manifest

[package]
name    = "counter"
version = "0.0.1"
edition = "2026"

[macos]
entry = "src/main.cplus"

[dependencies]
stdlib        = "*"
facet         = "*"
facet_runtime = "*"
flex_layout   = "*"
events        = "*"

[macos.dependencies]
facet_appkit = "*"
objc         = "*"
appkit       = "*"
quartzcore   = "*"
webkit       = "*"

The application entry is selected by the [macos] section; a portable application can instead rely on the default src/main.cplus entry.

Source

import "facet/facet" as core;
import "facet/elements" as ui;
import "facet/component" as component;
import "facet/label" as label;
import "facet/screen" as screen;
import "facet_runtime/runtime" as runtime;
import "stdlib/option" as option;
import "stdlib/status" as status;
import "stdlib/vec" as vec;

struct Counter {
    clicks: i32,
}

impl Counter {
    fn new() -> Counter {
        return Counter { clicks: 0 };
    }

    fn on_click(ref this, sender: *u8) {
        this.clicks = this.clicks +% 1;
        match label::find("count") {
            option::Option[label::Label]::Some(l) => {
                let _updated: label::Label =
                    l.set_text("Clicked ${this.clicks} time(s)");
            }
            option::Option::None => { }
        }
    }
}

impl Counter: component::Component {
    fn build(ref this) -> core::Node {
        return @ui {
            column {
                label("Clicked 0 time(s)", key: "count")
                button("Click me", key: "increment",
                       on_click: this.on_click)
            }.padding(20.0).gap(10.0)
        };
    }
}

impl Counter: component::Lifecycle { }

impl Counter: screen::Screen { }

fn counter_screen() -> screen::ScreenBox {
    return screen::screen_box(Counter::new());
}

fn main() -> i32 {
    let app = runtime::App::new("counter");
    app.window("main", counter_screen,
        chrome: screen::Chrome::new(
            title: "Counter", width: 360.0, height: 200.0));

    if app.run("main") == status::Status::Ok { return 0; }
    return 1;
}

The component owns clicks. The keyed label is found in the mounted tree and updated in place by the handler. Window registration supplies the title and size; screens no longer own chrome and applications no longer call app.screen.

Build

cpc build
./target/debug/counter

Facet uses the same component, screen, named-window, and navigation model with facet_uikit, facet_android, facet_gtk, and facet_winui. Platform-specific dependencies and entries belong in their matching manifest sections.