fenestra 0.7.0

A pure-Rust native GUI framework with web-grade aesthetics: OKLCH themes, soft shadows, flexbox/grid, and headless PNG rendering
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
//! The flagship example: a SaaS-style dashboard with sidebar navigation, a
//! working search input, a light/dark theme toggle, stat cards, a table, a
//! form, and a modal.
//!
//! `cargo run --example dashboard` opens it in a window.
//! `cargo run --example dashboard -- shot` renders both themes headlessly
//! to `gallery/dashboard_{light,dark}.png` (no display needed).

use fenestra::prelude::*;
use fenestra::shell::render_app;

struct Dashboard {
    dark: bool,
    nav: usize,
    search: String,
    show_modal: bool,
    report_name: String,
    plan: usize,
    notifications: bool,
    form_name: String,
}

#[derive(Clone)]
enum Msg {
    SetDark(bool),
    SetNav(usize),
    Search(String),
    OpenModal,
    CloseModal,
    ReportName(String),
    SetPlan(usize),
    SetNotifications(bool),
    FormName(String),
}

impl Default for Dashboard {
    fn default() -> Self {
        Self {
            dark: false,
            nav: 0,
            search: String::new(),
            show_modal: false,
            report_name: String::new(),
            plan: 1,
            notifications: true,
            form_name: "Acme Inc.".into(),
        }
    }
}

impl App for Dashboard {
    type Msg = Msg;

    fn update(&mut self, msg: Msg) {
        match msg {
            Msg::SetDark(d) => self.dark = d,
            Msg::SetNav(i) => self.nav = i,
            Msg::Search(s) => self.search = s,
            Msg::OpenModal => self.show_modal = true,
            Msg::CloseModal => self.show_modal = false,
            Msg::ReportName(s) => self.report_name = s,
            Msg::SetPlan(i) => self.plan = i,
            Msg::SetNotifications(b) => self.notifications = b,
            Msg::FormName(s) => self.form_name = s,
        }
    }

    fn theme(&self) -> Theme {
        if self.dark {
            Theme::dark()
        } else {
            Theme::light()
        }
    }

    fn view(&self) -> Element<Msg> {
        let theme = self.theme();
        row()
            .w_full()
            .h_full()
            .bg(theme.bg)
            .children([sidebar(&theme, self.nav), main_column(&theme, self)])
    }
}

fn sidebar(theme: &Theme, active: usize) -> Element<Msg> {
    let nav_item = |i: usize, label: &str| {
        let is_active = i == active;
        let mut item = row()
            .items_center()
            .gap(SP2)
            .px(SP3)
            .h(34.0)
            .rounded(R_MD)
            .shrink0()
            .cursor(Cursor::Pointer)
            .focusable(true)
            .on_click(Msg::SetNav(i))
            .transition(Transition::colors())
            .children([
                div().w(5.0).h(5.0).rounded_full().bg(if is_active {
                    theme.accent
                } else {
                    theme.neutrals.step(7)
                }),
                text(label)
                    .size(TextSize::Sm)
                    .weight(if is_active {
                        Weight::Medium
                    } else {
                        Weight::Regular
                    })
                    .color(if is_active {
                        theme.accent_text
                    } else {
                        theme.text_muted
                    }),
            ]);
        if is_active {
            item = item.bg(theme.accent_bg);
        } else {
            let hover = theme.neutrals.step(3);
            item = item.hover(move |s| s.bg(hover));
        }
        item
    };

    col()
        .w(228.0)
        .h_full()
        .p(SP3)
        .gap(SP1)
        .bg(theme.surface)
        .children([
            // Brand.
            row()
                .items_center()
                .gap(SP2)
                .px(SP3)
                .h(48.0)
                .shrink0()
                .children([
                    div().w(18.0).h(18.0).rounded(5.0).bg(theme.accent),
                    text("fenestra").weight(Weight::Semibold),
                ]),
            div().h(SP2),
        ])
        .children([
            nav_item(0, "Overview"),
            nav_item(1, "Reports"),
            nav_item(2, "Customers"),
            nav_item(3, "Billing"),
            nav_item(4, "Settings"),
        ])
        .children([
            spacer(),
            divider(),
            row().items_center().gap(SP2).p(SP2).shrink0().children([
                avatar("RH"),
                col().gap(0.0).children([
                    text("Richard Huang")
                        .size(TextSize::Sm)
                        .weight(Weight::Medium),
                    text("richard@acme.dev")
                        .size(TextSize::Xs)
                        .color(theme.text_subtle),
                ]),
            ]),
        ])
}

fn main_column(theme: &Theme, app: &Dashboard) -> Element<Msg> {
    col()
        .grow()
        .h_full()
        .children([top_bar(theme, app), divider(), content(theme, app)])
}

fn top_bar(_theme: &Theme, app: &Dashboard) -> Element<Msg> {
    row()
        .items_center()
        .gap(SP4)
        .px(SP6)
        .h(64.0)
        .shrink0()
        .children([
            text("Overview").size(TextSize::Lg).weight(Weight::Semibold),
            spacer(),
            Element::from(
                text_input(&app.search)
                    .placeholder("Search…")
                    .width(240.0)
                    .on_input(Msg::Search)
                    .id("search"),
            ),
            Element::from(
                switch(app.dark)
                    .label("Dark")
                    .on_toggle(Msg::SetDark(!app.dark))
                    .id("dark"),
            ),
            Element::from(
                button("New report")
                    .on_click(Msg::OpenModal)
                    .id("new-report"),
            ),
        ])
        .children(if app.show_modal {
            vec![new_report_modal(app)]
        } else {
            vec![]
        })
}

fn new_report_modal(app: &Dashboard) -> Element<Msg> {
    Element::from(
        modal("New report")
            .child(
                text("Reports run nightly and land in your inbox.")
                    .size(TextSize::Sm)
                    .themed(|t: &Theme, s| s.color(t.text_muted)),
            )
            .child(labeled(
                "Report name",
                Element::from(
                    text_input(&app.report_name)
                        .placeholder("Q3 retention…")
                        .width(408.0)
                        .on_input(Msg::ReportName)
                        .id("report-name"),
                ),
            ))
            .child(labeled(
                "Schedule",
                Element::from(
                    select(app.plan, ["Daily", "Weekly", "Monthly"])
                        .width(408.0)
                        .on_change(Msg::SetPlan)
                        .id("schedule"),
                ),
            ))
            .child(
                row().gap(SP3).pt(SP2).children([
                    button("Create").on_click(Msg::CloseModal).id("create"),
                    button("Cancel")
                        .variant(ButtonVariant::Ghost)
                        .on_click(Msg::CloseModal)
                        .id("cancel"),
                ]),
            )
            .on_close(Msg::CloseModal)
            .id("report-modal"),
    )
}

fn labeled<T: Into<Element<Msg>>>(label: &str, control: T) -> Element<Msg> {
    col()
        .gap(SP1)
        .children([text(label)
            .size(TextSize::Sm)
            .weight(Weight::Medium)
            .themed(|t: &Theme, s| s.color(t.text_muted))])
        .child(control)
}

fn content(theme: &Theme, app: &Dashboard) -> Element<Msg> {
    col()
        .grow()
        .p(SP6)
        .gap(SP6)
        .scroll_y()
        .id("content")
        .children([stats_row(), middle_row(theme, app)])
}

fn stats_row() -> Element<Msg> {
    div()
        .grid_cols(vec![Track::Fr(1.0); 4])
        .gap(SP4)
        .shrink0()
        .children([
            Element::from(stat_card("Revenue", "$48,210").delta("+12.5%", Status::Success)),
            Element::from(stat_card("Active users", "8,043").delta("+3.2%", Status::Success)),
            Element::from(stat_card("Churn", "2.4%").delta("+0.3%", Status::Danger)),
            Element::from(stat_card("Avg. session", "4m 32s")),
        ])
}

fn middle_row(theme: &Theme, app: &Dashboard) -> Element<Msg> {
    div()
        .grid_cols(vec![Track::Fr(5.0), Track::Fr(3.0)])
        .gap(SP4)
        .shrink0()
        .children([deployments_card(theme, app), settings_card(theme, app)])
}

fn deployments_card(theme: &Theme, app: &Dashboard) -> Element<Msg> {
    let rows: Vec<Vec<String>> = [
        ["api-server", "Healthy", "us-east-1", "2m ago"],
        ["worker-pool", "Degraded", "us-east-1", "11m ago"],
        ["edge-cache", "Healthy", "eu-west-2", "1h ago"],
        ["batch-runner", "Healthy", "ap-south-1", "3h ago"],
        ["webhooks", "Healthy", "us-east-1", "6h ago"],
    ]
    .iter()
    .filter(|r| {
        app.search.is_empty()
            || r.iter()
                .any(|c| c.to_lowercase().contains(&app.search.to_lowercase()))
    })
    .map(|r| r.iter().map(|c| (*c).to_owned()).collect())
    .collect();
    let empty = rows.is_empty();

    let mut c = card().gap(SP3).children([
        row().items_center().justify_between().children([
            text("Recent deployments").weight(Weight::Semibold),
            badge("Live", Status::Success),
        ]),
        table(["Service", "Status", "Region", "Updated"], rows),
    ]);
    if empty {
        c = c.child(
            text(format!("No services match \"{}\".", app.search))
                .size(TextSize::Sm)
                .color(theme.text_subtle),
        );
    }
    c
}

fn settings_card(_theme: &Theme, app: &Dashboard) -> Element<Msg> {
    card().gap(SP4).children([
        text("Workspace").weight(Weight::Semibold),
        labeled(
            "Organization",
            Element::from(
                text_input(&app.form_name)
                    .width(260.0)
                    .on_input(Msg::FormName)
                    .id("org"),
            ),
        ),
        labeled(
            "Plan",
            Element::from(
                select(app.plan, ["Starter", "Growth", "Scale"])
                    .width(260.0)
                    .on_change(Msg::SetPlan)
                    .id("plan"),
            ),
        ),
        Element::from(
            switch(app.notifications)
                .label("Email notifications")
                .on_toggle(Msg::SetNotifications(!app.notifications))
                .id("notify"),
        ),
        divider(),
        row().gap(SP3).children([
            button("Save changes").id("save").on_click(Msg::SetNav(0)),
            button("Reset")
                .variant(ButtonVariant::Secondary)
                .id("reset")
                .on_click(Msg::FormName("Acme Inc.".into())),
        ]),
        callout(
            Status::Accent,
            "Changes apply to all 14 members of this workspace.",
        ),
    ])
}

const SIZE: (f64, f64) = (1120.0, 720.0);

fn main() {
    if std::env::args().any(|a| a == "shot") {
        let out = std::path::Path::new("gallery");
        std::fs::create_dir_all(out).expect("create gallery dir");
        for dark in [false, true] {
            let mut app = Dashboard {
                dark,
                ..Dashboard::default()
            };
            let theme = app.theme();
            #[expect(
                clippy::cast_possible_truncation,
                clippy::cast_sign_loss,
                reason = "size"
            )]
            let image = render_app(&mut app, &[], (SIZE.0 as u32, SIZE.1 as u32), &theme);
            let name = if dark {
                "dashboard_dark"
            } else {
                "dashboard_light"
            };
            image
                .save(out.join(format!("{name}.png")))
                .expect("write png");
            println!("wrote gallery/{name}.png");
        }
        return;
    }
    fenestra::run(
        Dashboard::default(),
        WindowOptions::titled("fenestra dashboard").with_size(SIZE.0, SIZE.1),
    );
}