rok-ui 0.9.2

shadcn/ui-style component system for GPUI desktop apps, with a React-like developer experience.
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
//! A small notes app built on the BLoC architecture (`docs/guide/architecture.md`).
//!
//! Run with `cargo run --example notes --features bloc`; `cargo test --example notes
//! --features bloc` runs its bloc tests.
//!
//! The three layers, as modules here (as folders in an app made with `cargo rok-ui new
//! --template bloc`):
//!
//! - `data`: the `Note` model and `NotesRepository`, a trait with an in-memory implementation.
//! - `features::notes::bloc`: `NotesBloc` turns `NotesEvent`s into `NotesState`s, using only
//!   the repository.
//! - `features::notes::view` and `main`: pages read the bloc with `cx.bloc`, show its state
//!   with `BlocBuilder` and add events from handlers. Local UI state (a settings switch) stays
//!   in `use_signal`.

/// The data layer.
mod data {
    use std::sync::{Mutex, PoisonError};

    use rok_ui::prelude::SharedString;

    /// A note.
    #[derive(Clone, Debug, PartialEq, Eq)]
    pub struct Note {
        pub id: usize,
        pub title: SharedString,
        pub body: SharedString,
    }

    /// Where notes live. A trait, so tests and other backends can stand in.
    pub trait NotesRepository: Send + Sync {
        /// Every note.
        fn list(&self) -> Vec<Note>;
        /// Store a new note and return it.
        fn add(&self, title: SharedString) -> Note;
        /// Forget a note.
        fn delete(&self, id: usize);
    }

    /// Notes in memory, seeded with a few.
    pub struct InMemoryNotes(Mutex<Vec<Note>>);

    impl InMemoryNotes {
        pub fn seeded() -> Self {
            Self(Mutex::new(vec![
                Note {
                    id: 1,
                    title: "Groceries".into(),
                    body: "Milk, eggs, dates and Arabic coffee.".into(),
                },
                Note {
                    id: 2,
                    title: "Release checklist".into(),
                    body: "Bump versions, update the changelog, tag and publish.".into(),
                },
                Note {
                    id: 3,
                    title: "ملاحظة بالعربية".into(),
                    body: "النص العربي يظهر بالترتيب الصحيح في كل مكان.".into(),
                },
            ]))
        }
    }

    impl NotesRepository for InMemoryNotes {
        fn list(&self) -> Vec<Note> {
            self.0
                .lock()
                .unwrap_or_else(PoisonError::into_inner)
                .clone()
        }

        fn add(&self, title: SharedString) -> Note {
            let mut notes = self.0.lock().unwrap_or_else(PoisonError::into_inner);
            let id = notes.iter().map(|note| note.id).max().unwrap_or(0) + 1;
            let note = Note {
                id,
                title,
                body: SharedString::default(),
            };
            notes.push(note.clone());
            note
        }

        fn delete(&self, id: usize) {
            self.0
                .lock()
                .unwrap_or_else(PoisonError::into_inner)
                .retain(|note| note.id != id);
        }
    }
}

/// Features: business logic and views, one module per feature.
mod features {
    pub mod notes {
        /// Business logic: no GPUI, no components.
        pub mod bloc {
            use std::sync::Arc;

            use rok_ui::{
                bloc::{Bloc, Emitter},
                prelude::SharedString,
            };

            use crate::data::{Note, NotesRepository};

            /// What happened (past tense).
            #[derive(Debug)]
            pub enum NotesEvent {
                NotesRequested,
                NoteAdded { title: SharedString },
                NoteDeleted { id: usize },
            }

            /// How far loading got.
            #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
            pub enum NotesStatus {
                #[default]
                Initial,
                Success,
            }

            /// What the views show.
            #[derive(Clone, Debug, Default, PartialEq, Eq)]
            pub struct NotesState {
                pub status: NotesStatus,
                pub notes: Vec<Note>,
            }

            /// Notes: listing, adding and deleting through the repository.
            pub struct NotesBloc {
                repository: Arc<dyn NotesRepository>,
            }

            impl NotesBloc {
                pub fn new(repository: Arc<dyn NotesRepository>) -> Self {
                    Self { repository }
                }
            }

            impl Bloc for NotesBloc {
                type Event = NotesEvent;
                type State = NotesState;

                fn initial_state(&self) -> NotesState {
                    NotesState::default()
                }

                async fn on(&self, event: NotesEvent, emit: &Emitter<NotesState>) {
                    match event {
                        NotesEvent::NotesRequested => {}
                        NotesEvent::NoteAdded { title } => {
                            if title.trim().is_empty() {
                                return;
                            }
                            self.repository.add(title);
                        }
                        NotesEvent::NoteDeleted { id } => self.repository.delete(id),
                    }
                    emit.emit(NotesState {
                        status: NotesStatus::Success,
                        notes: self.repository.list(),
                    });
                }
            }
        }

        /// The pages.
        pub mod view {
            use rok_ui::{bloc::BlocBuilder, prelude::*, router};

            use super::bloc::{NotesBloc, NotesEvent};

            /// The list page: every note, and a field to add one.
            #[component]
            pub fn NotesPage(cx: &mut Cx) -> impl IntoElement {
                let notes = cx.bloc::<NotesBloc>();
                // Once, when the page opens: a state initializer runs on the first render only.
                let requested = notes.clone();
                cx.use_state(move || requested.add(NotesEvent::NotesRequested));
                let title = use_input_state("new-note", cx.window, cx.app, |state| {
                    state.with_placeholder("New note title")
                });
                let add = {
                    let (notes, title) = (notes.clone(), title.clone());
                    move |_: &ClickEvent, _: &mut Window, cx: &mut App| {
                        let text = title.read(cx).text().clone();
                        notes.add(NotesEvent::NoteAdded { title: text });
                        title.update(cx, |state, cx| state.set_text("", cx));
                    }
                };
                Padding::all(px(24.)).child(
                    Column::new()
                        .main_axis_size(MainAxisSize::Min)
                        .cross_axis_alignment(CrossAxisAlignment::Stretch)
                        .spacing(px(12.))
                        .child(
                            Row::new()
                                .spacing(px(8.))
                                .child(Expanded::new().child(Input::new(&title)))
                                .child(
                                    Button::new("add-note")
                                        .icon(IconName::Plus)
                                        .label("Add")
                                        .on_click(add),
                                ),
                        )
                        .child(BlocBuilder::new(&notes, |state, _, _| {
                            Column::new()
                                .main_axis_size(MainAxisSize::Min)
                                .cross_axis_alignment(CrossAxisAlignment::Stretch)
                                .spacing(px(8.))
                                .children(state.notes.iter().map(|note| {
                                    Link::new(("note-link", note.id), format!("/notes/{}", note.id))
                                        .sx(style! { color: foreground })
                                        .child(
                                            Item::new(("note", note.id))
                                                .title(note.title.clone())
                                                .description(if note.body.is_empty() {
                                                    SharedString::from("Empty note")
                                                } else {
                                                    note.body.clone()
                                                }),
                                        )
                                }))
                        })),
                )
            }

            /// One note, looked up by the `:id` route parameter.
            #[component]
            pub fn NotePage(id: usize, cx: &mut Cx) -> impl IntoElement {
                let notes = cx.bloc::<NotesBloc>();
                let state = notes.state();
                let Some(note) = state.notes.iter().find(|note| note.id == id).cloned() else {
                    return Padding::all(px(24.))
                        .child(
                            Empty::new()
                                .icon(IconName::FileText)
                                .title("This note was deleted"),
                        )
                        .into_any_element();
                };
                let delete = move |_: &ClickEvent, _: &mut Window, cx: &mut App| {
                    notes.add(NotesEvent::NoteDeleted { id });
                    router::replace("/", cx);
                };
                Padding::all(px(24.))
                    .child(
                        Column::new()
                            .main_axis_size(MainAxisSize::Min)
                            .cross_axis_alignment(CrossAxisAlignment::Start)
                            .spacing(px(16.))
                            .child(
                                Button::new("back")
                                    .ghost()
                                    .icon(IconName::ArrowLeft.for_direction())
                                    .label("Back")
                                    .on_click(|_, _, cx| router::back(cx)),
                            )
                            .child(H2::new(note.title))
                            .child(P::new(note.body))
                            .child(
                                Button::new("delete-note")
                                    .destructive()
                                    .icon(IconName::Trash)
                                    .label("Delete")
                                    .on_click(delete),
                            ),
                    )
                    .into_any_element()
            }

            /// Settings: purely local UI state, so a `use_signal`, not a bloc.
            #[component]
            pub fn SettingsPage(window: &mut Window, cx: &mut App) -> impl IntoElement {
                let (compact, set_compact) = use_signal(window, cx, || false);
                Padding::all(px(24.)).child(
                    Column::new()
                        .main_axis_size(MainAxisSize::Min)
                        .cross_axis_alignment(CrossAxisAlignment::Start)
                        .spacing(px(16.))
                        .child(H3::new("Settings"))
                        .child(
                            Switch::new("compact")
                                .label("Compact list")
                                .checked(compact.get())
                                .on_change(move |checked, _, _| set_compact.set(*checked)),
                        )
                        .child(Muted::new(if compact.get() {
                            "Compact mode is on (a use_signal owned by this page)."
                        } else {
                            "Compact mode is off."
                        }))
                        .child(
                            Link::new("missing-link", "/does/not/exist")
                                .child("Open a page that does not exist"),
                        ),
                )
            }
        }
    }
}

use std::sync::Arc;

use rok_ui::{
    bloc::{BlocProvider, RepositoryProvider},
    prelude::*,
    router,
};

use data::{InMemoryNotes, NotesRepository};
use features::notes::{
    bloc::NotesBloc,
    view::{NotePage, NotesPage, SettingsPage},
};

/// The shell, rendered below the providers so it can read the bloc.
#[component]
fn Shell(cx: &mut Cx) -> impl IntoElement {
    let selected = usize::from(router::is_active("/settings", false, cx));
    let title = match router::location(cx).path() {
        "/settings" => "Settings",
        _ => "Notes",
    };
    let count = cx.watch_bloc::<NotesBloc>().notes.len();
    AdaptiveScaffold::new("notes-shell")
        .app_bar(
            AppBar::new()
                .title(title)
                .action(
                    Button::new("go-back")
                        .ghost()
                        .icon_only(IconName::ArrowLeft.for_direction())
                        .tooltip("Back (Alt+Left)")
                        .disabled(!router::can_go_back(cx))
                        .on_click(|_, _, cx| router::back(cx)),
                )
                .action(
                    Button::new("go-forward")
                        .ghost()
                        .icon_only(IconName::ArrowRight.for_direction())
                        .tooltip("Forward (Alt+Right)")
                        .disabled(!router::can_go_forward(cx))
                        .on_click(|_, _, cx| router::forward(cx)),
                ),
        )
        .destination(
            NavigationDestination::new(IconName::FileText, "Notes").badge(count.to_string()),
        )
        .destination(NavigationDestination::new(IconName::Settings, "Settings"))
        .selected_index(selected)
        .on_change(|index, _, cx| {
            router::navigate(if *index == 1 { "/settings" } else { "/" }, cx);
        })
        .child(
            Router::new()
                .route("/", |_, _, _| NotesPage::new())
                .route("/notes/:id", |route, _, _| {
                    NotePage::new(route.param_as::<usize>("id").unwrap_or(0))
                })
                .route("/settings", |_, _, _| SettingsPage::new())
                .redirect("/home", "/")
                .not_found(|route, _, _| {
                    Padding::all(px(24.)).child(
                        Empty::new()
                            .icon(IconName::CircleAlert)
                            .title("Page not found")
                            .description(format!("Nothing lives at {}.", route.path())),
                    )
                }),
        )
}

struct NotesApp;

impl Render for NotesApp {
    fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
        // Repositories, then blocs built from them, then the UI.
        AppRoot::new().child(
            RepositoryProvider::new()
                .provide::<dyn NotesRepository>(Arc::new(InMemoryNotes::seeded()))
                .child(
                    BlocProvider::bloc(|scope| {
                        NotesBloc::new(scope.repository::<dyn NotesRepository>())
                    })
                    .child(Shell::new()),
                ),
        )
    }
}

fn main() {
    Application::new()
        .with_assets(rok_ui::Assets)
        .run(|cx: &mut App| {
            rok_ui::init(cx);
            let bounds = Bounds::centered(None, gpui::size(px(1000.), px(720.)), cx);
            cx.open_window(
                WindowOptions {
                    window_bounds: Some(WindowBounds::Windowed(bounds)),
                    titlebar: Some(gpui::TitlebarOptions {
                        title: Some("rok-ui notes".into()),
                        ..Default::default()
                    }),
                    ..Default::default()
                },
                |_, cx| cx.new(|_| NotesApp),
            )
            .expect("failed to open the window");
            cx.activate(true);
        });
}

/// Events in, states out, with a fake repository.
#[cfg(test)]
mod tests {
    use std::sync::{Arc, Mutex};

    use rok_ui::{bloc::test, prelude::SharedString};

    use crate::{
        data::{Note, NotesRepository},
        features::notes::bloc::{NotesBloc, NotesEvent, NotesStatus},
    };

    /// Remembers what it was asked to do.
    #[derive(Default)]
    struct FakeNotes(Mutex<Vec<Note>>);

    impl NotesRepository for FakeNotes {
        fn list(&self) -> Vec<Note> {
            self.0.lock().unwrap().clone()
        }

        fn add(&self, title: SharedString) -> Note {
            let note = Note {
                id: self.0.lock().unwrap().len() + 1,
                title,
                body: SharedString::default(),
            };
            self.0.lock().unwrap().push(note.clone());
            note
        }

        fn delete(&self, id: usize) {
            self.0.lock().unwrap().retain(|note| note.id != id);
        }
    }

    fn titles(state: &crate::features::notes::bloc::NotesState) -> Vec<&str> {
        state.notes.iter().map(|note| note.title.as_ref()).collect()
    }

    #[test]
    fn adding_and_deleting_notes() {
        let states = test::run(
            NotesBloc::new(Arc::new(FakeNotes::default())),
            [
                NotesEvent::NotesRequested,
                NotesEvent::NoteAdded {
                    title: "Groceries".into(),
                },
                NotesEvent::NoteAdded {
                    title: "Taxes".into(),
                },
                NotesEvent::NoteDeleted { id: 1 },
            ],
        );
        assert_eq!(states[0].status, NotesStatus::Success);
        let titles: Vec<Vec<&str>> = states.iter().map(titles).collect();
        assert_eq!(
            titles,
            [
                vec![],
                vec!["Groceries"],
                vec!["Groceries", "Taxes"],
                vec!["Taxes"]
            ]
        );
    }

    #[test]
    fn blank_titles_are_ignored() {
        let states = test::run(
            NotesBloc::new(Arc::new(FakeNotes::default())),
            [NotesEvent::NoteAdded { title: "  ".into() }],
        );
        assert!(states.is_empty(), "nothing changed, so nothing was emitted");
    }
}