A native desktop control room.
Build an egui/eframe desktop app around a separately tested Rust model.
Download this project ↓01 The shape of the build
A desktop UI should reflect a model rather than burying every rule inside button handlers. This project keeps a bounded counter in a plain Rust library and connects it to an immediate-mode egui interface through eframe. UI code is rerun when a frame needs painting, so avoid performing slow filesystem or network operations directly in the rendering path. Model tests can run headlessly; opening the actual native window requires a graphical desktop and the platform graphics dependencies.
- 1
Define a Counter whose methods preserve the range 0 through 100.
- 2
Test the model independently of the windowing runtime.
- 3
Implement eframe::App::ui with buttons that call the model.
- 4
Use run_native to open the window on a machine with a desktop session.
02 Get it running
Install Rust stable and extract this project’s ZIP. Run these commands from the extracted desktop-counter folder. The full workbench keeps it under cookbook/desktop-counter.
cargo test --lib
cargo runA native Build Lab / Counter window shows a bounded count, minus/plus buttons, and reset. The count never leaves 0–100.
First builds download dependencies. The included Cargo.lock files record the versions resolved during authoring. Use --locked with Cargo commands when you want to enforce those resolutions.
03 The complete source
Every source file below is also in the download. Start with the manifest, then follow the Rust entry point and any UI or test files.
Cargo.toml9 lines +
[package]
name = "desktop-counter"
version = "0.1.0"
edition = "2024"
[dependencies]
eframe = { version = "0.37", default-features = false, features = ["default_fonts", "glow", "x11", "wayland"] }
[workspace]src/lib.rs34 lines +
#[derive(Default)]
pub struct Counter {
value: u32,
}
impl Counter {
pub fn value(&self) -> u32 {
self.value
}
pub fn increment(&mut self) {
self.value = (self.value + 1).min(100);
}
pub fn decrement(&mut self) {
self.value = self.value.saturating_sub(1);
}
pub fn reset(&mut self) {
self.value = 0;
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn bounds() {
let mut c = Counter::default();
c.decrement();
assert_eq!(c.value(), 0);
for _ in 0..150 {
c.increment();
}
assert_eq!(c.value(), 100);
c.reset();
assert_eq!(c.value(), 0);
}
}src/main.rs35 lines +
use desktop_counter::Counter;
use eframe::egui;
#[derive(Default)]
struct CounterApp {
counter: Counter,
}
impl eframe::App for CounterApp {
fn ui(&mut self, ui: &mut egui::Ui, _frame: &mut eframe::Frame) {
egui::CentralPanel::default().show(ui, |ui| {
ui.heading("Small steps. Real state.");
ui.label("The Build Lab / Rust desktop recipe");
ui.separator();
ui.heading(self.counter.value().to_string());
ui.horizontal(|ui| {
if ui.button("−").clicked() {
self.counter.decrement();
}
if ui.button("+").clicked() {
self.counter.increment();
}
if ui.button("Reset").clicked() {
self.counter.reset();
}
});
ui.label("Range: 0–100. State lasts for this session.");
});
}
}
fn main() -> eframe::Result {
eframe::run_native(
"Build Lab / Counter",
eframe::NativeOptions::default(),
Box::new(|_| Ok(Box::new(CounterApp::default()))),
)
}04 Check the behavior
- □cargo test --lib checks lower and upper bounds and reset.
- □Repeated minus clicks at zero keep the value at zero.
- □The native window responds to resize and closes normally on your desktop.
05 Know what you are building on
Requires a graphical desktop and platform graphics/windowing prerequisites.
The counter is in memory; closing the app resets it.
Move expensive work to a worker and report results back rather than blocking the UI.
Choose one boundary to improve. Write down the new behavior, add a test that demonstrates it, then change the implementation. Keep the original working path intact.