Skip to content

Build a Cross-Framework Counter

Tutorial2 min read
Edit page

You will build a counter application whose reactive logic lives in a framework-agnostic library crate and whose view layer runs on GPUI. At the end you re-target the same hook to Leptos by changing a single feature flag. The counter is the smallest program that exercises every layer of Quoin: a signal for state, a portable hook written against ReactiveContext, and a framework view that reads the signal.

Prerequisites: Rust stable 1.88 or newer (rustup update stable), and a terminal. No GPUI system libraries are needed for the logic crate; GPUI itself needs the usual Linux windowing headers listed in the installation guide.

  1. Create a Cargo workspace with two crates: a library for the portable logic and a binary for the GPUI view.

    Terminal
    cargo new counter --lib
    cargo new counter-gpui --bin
  2. Add Quoin to both manifests. The library depends on quoin-core (traits only, no framework). The binary depends on quoin with the gpui feature.

    counter/Cargo.toml
    [dependencies]
    quoin-core = "0.1"
    counter-gpui/Cargo.toml
    [dependencies]
    counter = { path = "../counter" }
    quoin = { version = "0.1", features = ["gpui"] }

The hook is an ordinary Rust function generic over ReactiveContext. It knows nothing about GPUI, windows, or rendering.

counter/src/lib.rs
use quoin_core::prelude::*;
use std::rc::Rc;
pub struct Counter<S: Signal<u32>> {
pub count: S,
pub increment: Rc<dyn Fn()>,
}
pub fn use_counter<C: ReactiveContext>(cx: &C) -> Counter<C::Signal<u32>> {
let count = cx.create_signal(0u32);
let increment = {
let count = count.clone();
Rc::new(move || count.update(|c| *c += 1))
};
Counter { count, increment }
}

Key points:

  • cx.create_signal(0u32) allocates reactive state through the context. The returned type is C::Signal<u32> — whatever the active framework provides.
  • count.update(|c| *c += 1) mutates in place and requests a re-render on pull-based adapters.
  • The Signal<T> trait bound keeps the struct usable with any adapter’s signal type.

Now wire the hook into a GPUI component and bootstrap the app with run_app!.

counter-gpui/src/main.rs
use counter::use_counter;
use quoin::prelude::*;
struct CounterView {
counter: counter::Counter<quoin::GpuiSignal<u32>>,
_ctx: GpuiContext,
}
impl Render for CounterView {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let count = self.counter.count.get();
div()
.flex()
.child(format!("Count: {count}"))
.child(
div()
.child("Increment")
.on_mouse_down(MouseButton::Left, cx.listener(|this, _, _, _| {
(this.counter.increment)();
}))
)
}
}
fn main() {
let ctx = GpuiContext::new();
let counter = use_counter(&ctx);
run_app!(CounterView { counter, _ctx: ctx });
}

Run it:

Terminal
cargo run -p counter-gpui

Expected output: a native window showing Count: 0 with an Increment button; each click updates the label.

Change the feature flag and swap the view. The hook crate is untouched.

counter-leptos/Cargo.toml
[dependencies]
counter = { path = "../counter" }
quoin = { version = "0.1", features = ["leptos"] }
counter-leptos/src/lib.rs
use counter::use_counter;
use quoin::prelude::*;
#[component]
pub fn App() -> impl IntoView {
let ctx = LeptosContext::new();
let counter = use_counter(&ctx);
view! {
<div>
<p>"Count: " {move || counter.count.get()}</p>
<button on:click=move |_| (counter.increment)()>"Increment"</button>
</div>
}
}

You can also skip the view layer entirely: the testing guide runs the same hook headlessly with quoin-testkit, and the devtools guide traces every signal mutation it performs.