1use std::marker::PhantomData;
9
10use crux_core::{
11 App, Command,
12 capability::Operation,
13 macros::effect,
14 render::{RenderOperation, render},
15};
16use facet::Facet;
17use serde::{Deserialize, Serialize, de::DeserializeOwned};
18
19pub use mobiler_ui::{
20 Action, BoxAlign, ButtonStyle, CardStyle, Icon, ImageRatio, ImageShape, InputValue,
21 ProjectColor, Spacing, Tab, TextStyle, Tone, Widget,
22};
23
24#[effect(facet_typegen)]
28#[derive(Debug)]
29pub enum Effect {
30 Render(RenderOperation),
31 PluginNotify(PluginNotify),
33 Plugin(PluginCall),
35}
36
37#[derive(Facet, Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
38pub struct PluginNotify {
39 pub plugin: String,
40 pub op: String,
41 pub input: String,
42}
43impl Operation for PluginNotify {
44 type Output = ();
45}
46
47#[derive(Facet, Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
48pub struct PluginCall {
49 pub plugin: String,
50 pub op: String,
51 pub input: String,
52}
53impl Operation for PluginCall {
54 type Output = PluginResponse;
55}
56
57#[derive(Facet, Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
58pub struct PluginResponse {
59 pub ok: bool,
60 pub output: String,
61}
62
63type Continuation<E> = Box<dyn FnOnce(PluginResponse) -> E + Send>;
64
65pub struct Cx<E> {
68 notifications: Vec<PluginNotify>,
69 requests: Vec<(PluginCall, Continuation<E>)>,
70}
71
72impl<E> Default for Cx<E> {
73 fn default() -> Self {
74 Self { notifications: Vec::new(), requests: Vec::new() }
75 }
76}
77
78impl<E> Cx<E> {
79 pub fn notify(&mut self, plugin: impl Into<String>, op: impl Into<String>, input: impl Into<String>) {
81 self.notifications.push(PluginNotify { plugin: plugin.into(), op: op.into(), input: input.into() });
82 }
83
84 pub fn plugin(
87 &mut self,
88 plugin: impl Into<String>,
89 op: impl Into<String>,
90 input: impl Into<String>,
91 then: impl FnOnce(PluginResponse) -> E + Send + 'static,
92 ) {
93 self.requests
94 .push((PluginCall { plugin: plugin.into(), op: op.into(), input: input.into() }, Box::new(then)));
95 }
96
97 pub fn save(&mut self, data: impl Into<String>) {
99 self.notify("storage", "save", data);
100 }
101
102 pub fn copy(&mut self, text: impl Into<String>) {
104 self.notify("clipboard", "copy", text);
105 }
106
107 pub fn share(&mut self, text: impl Into<String>) {
109 self.notify("share", "text", text);
110 }
111
112 pub fn open_url(&mut self, url: impl Into<String>) {
115 self.notify("browser", "open", url);
116 }
117
118 pub fn toast(&mut self, text: impl Into<String>) {
120 self.notify("toast", "show", text);
121 }
122
123 pub fn haptic(&mut self, style: impl Into<String>) {
126 self.notify("haptics", style, "");
127 }
128
129 pub fn http(
134 &mut self,
135 method: impl Into<String>,
136 url: impl Into<String>,
137 body: Option<String>,
138 then: impl FnOnce(PluginResponse) -> E + Send + 'static,
139 ) {
140 #[derive(Serialize)]
141 struct HttpReq {
142 url: String,
143 body: Option<String>,
144 }
145 let input = serde_json::to_string(&HttpReq { url: url.into(), body })
146 .expect("serialize http request");
147 self.plugin("http", method, input, then);
148 }
149
150 pub fn get(&mut self, url: impl Into<String>, then: impl FnOnce(PluginResponse) -> E + Send + 'static) {
152 self.http("GET", url, None, then);
153 }
154 pub fn post(&mut self, url: impl Into<String>, body: impl Into<String>, then: impl FnOnce(PluginResponse) -> E + Send + 'static) {
156 self.http("POST", url, Some(body.into()), then);
157 }
158 pub fn patch(&mut self, url: impl Into<String>, body: impl Into<String>, then: impl FnOnce(PluginResponse) -> E + Send + 'static) {
160 self.http("PATCH", url, Some(body.into()), then);
161 }
162 pub fn delete(&mut self, url: impl Into<String>, then: impl FnOnce(PluginResponse) -> E + Send + 'static) {
164 self.http("DELETE", url, None, then);
165 }
166
167 pub fn device_model(&mut self, then: impl FnOnce(PluginResponse) -> E + Send + 'static) {
171 self.plugin("device", "model", "", then);
172 }
173
174 pub fn pick_photo(&mut self, then: impl FnOnce(PluginResponse) -> E + Send + 'static) {
179 self.plugin("photo", "pick", "", then);
180 }
181
182 pub fn confirm(
186 &mut self,
187 title: impl Into<String>,
188 message: impl Into<String>,
189 then: impl FnOnce(PluginResponse) -> E + Send + 'static,
190 ) {
191 #[derive(Serialize)]
192 struct Confirm {
193 title: String,
194 message: String,
195 }
196 let input = serde_json::to_string(&Confirm { title: title.into(), message: message.into() })
197 .expect("serialize confirm");
198 self.plugin("dialog", "confirm", input, then);
199 }
200}
201
202pub trait MobilerApp: Default {
207 type Event: Serialize + DeserializeOwned + Send + 'static;
208 type Model: Default;
209
210 fn update(&self, event: Self::Event, model: &mut Self::Model, cx: &mut Cx<Self::Event>);
211
212 fn input(&self, id: &str, value: InputValue, model: &mut Self::Model, cx: &mut Cx<Self::Event>) {
213 let _ = (id, value, model, cx);
214 }
215
216 fn restore(&self, data: &str, model: &mut Self::Model) {
219 let _ = (data, model);
220 }
221
222 fn init(&self, model: &mut Self::Model, cx: &mut Cx<Self::Event>) {
225 let _ = (model, cx);
226 }
227
228 fn view(&self, model: &Self::Model) -> Widget;
229}
230
231pub struct MobilerShell<A>(PhantomData<fn() -> A>);
233
234impl<A> Default for MobilerShell<A> {
235 fn default() -> Self {
236 Self(PhantomData)
237 }
238}
239
240impl<A: MobilerApp> App for MobilerShell<A> {
241 type Event = Action;
242 type Model = A::Model;
243 type ViewModel = Widget;
244 type Effect = Effect;
245
246 fn update(&self, action: Action, model: &mut Self::Model) -> Command<Effect, Action> {
247 let app = A::default();
248 let mut cx = Cx::<A::Event>::default();
249 match action {
250 Action::Fired { token } => {
251 if let Ok(event) = serde_json::from_str::<A::Event>(&token) {
252 app.update(event, model, &mut cx);
253 }
254 }
255 Action::Input { id, value } => app.input(&id, value, model, &mut cx),
256 Action::Restore { data } => app.restore(&data, model),
257 Action::Start => app.init(model, &mut cx),
258 }
259 let mut commands: Vec<Command<Effect, Action>> = Vec::new();
260 for op in cx.notifications {
261 commands.push(Command::notify_shell(op).build());
262 }
263 for (op, then) in cx.requests {
264 commands.push(Command::request_from_shell(op).then_send(move |response: PluginResponse| {
265 Action::Fired { token: serde_json::to_string(&then(response)).expect("serialize event") }
266 }));
267 }
268 commands.push(render());
269 Command::all(commands)
270 }
271
272 fn view(&self, model: &Self::Model) -> Widget {
273 A::default().view(model)
274 }
275}
276
277#[derive(Clone, Debug)]
296pub struct Nav<R> {
297 stack: Vec<R>,
298}
299
300impl<R: Clone + Serialize> Nav<R> {
301 #[must_use]
303 pub fn new(root: R) -> Self {
304 Self { stack: vec![root] }
305 }
306 pub fn push(&mut self, route: R) {
308 self.stack.push(route);
309 }
310 pub fn pop(&mut self) {
312 if self.stack.len() > 1 {
313 self.stack.pop();
314 }
315 }
316 pub fn reset(&mut self, root: R) {
318 self.stack = vec![root];
319 }
320 #[must_use]
322 pub fn current(&self) -> &R {
323 self.stack.last().expect("nav stack is never empty")
324 }
325 #[must_use]
327 pub fn depth(&self) -> u32 {
328 self.stack.len() as u32
329 }
330 #[must_use]
332 pub fn can_go_back(&self) -> bool {
333 self.stack.len() > 1
334 }
335 fn route_key(&self) -> String {
338 serde_json::to_string(self.current()).expect("serialize route")
339 }
340}
341
342fn tok<E: Serialize>(event: E) -> String {
346 serde_json::to_string(&event).expect("serialize event")
347}
348
349#[must_use]
350pub fn styled(content: impl Into<String>, style: TextStyle) -> Widget {
351 Widget::Text { content: content.into(), style }
352}
353#[must_use]
354pub fn text(content: impl Into<String>) -> Widget { styled(content, TextStyle::Body) }
355#[must_use]
356pub fn title(content: impl Into<String>) -> Widget { styled(content, TextStyle::Title) }
357#[must_use]
358pub fn subtitle(content: impl Into<String>) -> Widget { styled(content, TextStyle::Subtitle) }
359#[must_use]
360pub fn caption(content: impl Into<String>) -> Widget { styled(content, TextStyle::Caption) }
361#[must_use]
362pub fn emphasis(content: impl Into<String>) -> Widget { styled(content, TextStyle::Emphasis) }
363
364#[must_use]
365pub fn image(source: impl Into<String>, shape: ImageShape, ratio: ImageRatio) -> Widget {
366 Widget::Image { source: source.into(), shape, ratio }
367}
368#[must_use]
369pub fn badge(label: impl Into<String>, tone: Tone) -> Widget {
370 Widget::Badge { label: label.into(), tone }
371}
372#[must_use]
374pub fn color_dot(color: ProjectColor) -> Widget {
375 Widget::ColorDot { color }
376}
377#[must_use]
378pub fn divider() -> Widget { Widget::Divider }
379#[must_use]
380pub fn spacer(size: Spacing) -> Widget { Widget::Spacer { size } }
381
382#[must_use]
383pub fn row(children: Vec<Widget>) -> Widget { Widget::Row { children } }
384#[must_use]
385pub fn column(children: Vec<Widget>) -> Widget { Widget::Column { children } }
386#[must_use]
387pub fn card(child: Widget, style: CardStyle) -> Widget {
388 Widget::Card { child: Box::new(child), style, on_press: None }
389}
390#[must_use]
392pub fn card_button<E: Serialize>(child: Widget, style: CardStyle, on_press: E) -> Widget {
393 Widget::Card { child: Box::new(child), style, on_press: Some(tok(on_press)) }
394}
395#[must_use]
398pub fn stack(align: BoxAlign, scrim: bool, children: Vec<Widget>) -> Widget {
399 Widget::Box { children, align, scrim }
400}
401#[must_use]
402pub fn grid(children: Vec<Widget>) -> Widget { Widget::Grid { children } }
403
404#[must_use]
405pub fn button<E: Serialize>(label: impl Into<String>, style: ButtonStyle, on_press: E) -> Widget {
406 Widget::Button { label: label.into(), style, on_press: tok(on_press) }
407}
408#[must_use]
409pub fn icon_button<E: Serialize>(icon: Icon, on_press: E) -> Widget {
410 Widget::IconButton { icon, on_press: tok(on_press) }
411}
412#[must_use]
413pub fn chip<E: Serialize>(label: impl Into<String>, selected: bool, on_press: E) -> Widget {
414 Widget::Chip { label: label.into(), selected, on_press: tok(on_press) }
415}
416#[must_use]
417pub fn text_field(id: impl Into<String>, placeholder: impl Into<String>, value: impl Into<String>) -> Widget {
418 Widget::TextField { id: id.into(), placeholder: placeholder.into(), value: value.into() }
419}
420#[must_use]
421pub fn toggle(id: impl Into<String>, label: impl Into<String>, value: bool) -> Widget {
422 Widget::Toggle { id: id.into(), label: label.into(), value }
423}
424#[must_use]
425pub fn checkbox(id: impl Into<String>, label: impl Into<String>, value: bool) -> Widget {
426 Widget::Checkbox { id: id.into(), label: label.into(), value }
427}
428#[must_use]
429pub fn slider(id: impl Into<String>, value: i32, max: i32) -> Widget {
430 Widget::Slider { id: id.into(), value, max }
431}
432#[must_use]
433pub fn stepper<E: Serialize>(value: i32, on_decrement: E, on_increment: E) -> Widget {
434 Widget::Stepper { value, on_decrement: tok(on_decrement), on_increment: tok(on_increment) }
435}
436
437#[must_use]
439pub fn tab<E: Serialize>(label: impl Into<String>, selected: bool, on_select: E) -> Tab {
440 Tab { label: label.into(), selected, on_select: tok(on_select) }
441}
442
443#[must_use]
446pub fn scaffold(title: impl Into<String>, dark_mode: bool, tabs: Vec<Tab>, body: Widget) -> Widget {
447 let title = title.into();
448 Widget::Scaffold { route: title.clone(), title, body: Box::new(body), tabs, back: None, dark_mode, depth: 1 }
450}
451
452#[must_use]
456pub fn scaffold_back<E: Serialize>(title: impl Into<String>, dark_mode: bool, tabs: Vec<Tab>, body: Widget, back: E) -> Widget {
457 let title = title.into();
458 Widget::Scaffold { route: title.clone(), title, body: Box::new(body), tabs, back: Some(tok(back)), dark_mode, depth: 2 }
459}
460
461#[must_use]
466pub fn nav_scaffold<R, E>(
467 title: impl Into<String>,
468 dark_mode: bool,
469 tabs: Vec<Tab>,
470 body: Widget,
471 nav: &Nav<R>,
472 on_back: E,
473) -> Widget
474where
475 R: Clone + Serialize,
476 E: Serialize,
477{
478 Widget::Scaffold {
479 title: title.into(),
480 body: Box::new(body),
481 tabs,
482 back: if nav.can_go_back() { Some(tok(on_back)) } else { None },
483 dark_mode,
484 route: nav.route_key(),
485 depth: nav.depth(),
486 }
487}
488
489#[cfg(test)]
490mod tests {
491 use super::*;
492 use serde::Serialize;
493
494 #[derive(Clone, Copy, Serialize, PartialEq, Debug)]
495 enum Route {
496 Home,
497 Detail(u32),
498 }
499
500 #[derive(Serialize)]
501 enum Ev {
502 Tap,
503 Open(u32),
504 }
505
506 #[test]
509 fn nav_push_pop_depth() {
510 let mut nav = Nav::new(Route::Home);
511 assert_eq!(nav.depth(), 1);
512 assert!(!nav.can_go_back());
513
514 nav.push(Route::Detail(7));
515 assert_eq!(nav.depth(), 2);
516 assert!(nav.can_go_back());
517 assert!(matches!(nav.current(), Route::Detail(7)));
518
519 nav.pop();
520 assert_eq!(nav.depth(), 1);
521 assert!(matches!(nav.current(), Route::Home));
522
523 nav.pop(); assert_eq!(nav.depth(), 1);
525 }
526
527 #[test]
528 fn nav_reset_replaces_stack() {
529 let mut nav = Nav::new(Route::Home);
530 nav.push(Route::Detail(1));
531 nav.push(Route::Detail(2));
532 nav.reset(Route::Detail(9));
533 assert_eq!(nav.depth(), 1);
534 assert!(matches!(nav.current(), Route::Detail(9)));
535 }
536
537 #[test]
538 fn nav_route_key_is_serialization() {
539 let nav = Nav::new(Route::Detail(3));
540 assert_eq!(nav.route_key(), serde_json::to_string(&Route::Detail(3)).unwrap());
541 }
542
543 #[test]
546 fn scaffold_sets_route_depth_and_no_back() {
547 match scaffold("Home", false, vec![], text("x")) {
548 Widget::Scaffold { route, depth, back, dark_mode, .. } => {
549 assert_eq!(route, "Home");
550 assert_eq!(depth, 1);
551 assert!(back.is_none());
552 assert!(!dark_mode);
553 }
554 other => panic!("expected Scaffold, got {other:?}"),
555 }
556 }
557
558 #[test]
559 fn scaffold_back_is_depth_2_with_back() {
560 match scaffold_back("Detail", true, vec![], text("x"), Ev::Tap) {
561 Widget::Scaffold { depth, back, dark_mode, .. } => {
562 assert_eq!(depth, 2);
563 assert_eq!(back, Some(serde_json::to_string(&Ev::Tap).unwrap()));
564 assert!(dark_mode);
565 }
566 other => panic!("expected Scaffold, got {other:?}"),
567 }
568 }
569
570 #[test]
571 fn nav_scaffold_shows_back_only_when_poppable() {
572 let mut nav = Nav::new(Route::Home);
573 match nav_scaffold("T", false, vec![], text("x"), &nav, Ev::Tap) {
575 Widget::Scaffold { back, depth, route, .. } => {
576 assert!(back.is_none());
577 assert_eq!(depth, 1);
578 assert_eq!(route, serde_json::to_string(&Route::Home).unwrap());
579 }
580 other => panic!("expected Scaffold, got {other:?}"),
581 }
582 nav.push(Route::Detail(2));
584 match nav_scaffold("T", false, vec![], text("x"), &nav, Ev::Tap) {
585 Widget::Scaffold { back, depth, .. } => {
586 assert_eq!(back, Some(serde_json::to_string(&Ev::Tap).unwrap()));
587 assert_eq!(depth, 2);
588 }
589 other => panic!("expected Scaffold, got {other:?}"),
590 }
591 }
592
593 #[test]
594 fn buttons_carry_serialized_event_tokens() {
595 match button("Go", ButtonStyle::Filled, Ev::Open(5)) {
596 Widget::Button { label, on_press, .. } => {
597 assert_eq!(label, "Go");
598 assert_eq!(on_press, serde_json::to_string(&Ev::Open(5)).unwrap());
599 }
600 other => panic!("expected Button, got {other:?}"),
601 }
602 match card_button(text("c"), CardStyle::Elevated, Ev::Tap) {
603 Widget::Card { on_press, .. } => {
604 assert_eq!(on_press, Some(serde_json::to_string(&Ev::Tap).unwrap()));
605 }
606 other => panic!("expected Card, got {other:?}"),
607 }
608 match card(text("c"), CardStyle::Elevated) {
610 Widget::Card { on_press, .. } => assert!(on_press.is_none()),
611 other => panic!("expected Card, got {other:?}"),
612 }
613 }
614
615 #[test]
618 fn cx_notify_and_save_enqueue_notifications() {
619 let mut cx = Cx::<Ev>::default();
620 cx.notify("toast", "show", "hi");
621 cx.save("blob");
622 assert_eq!(cx.notifications.len(), 2);
623 assert_eq!(cx.notifications[0], PluginNotify { plugin: "toast".into(), op: "show".into(), input: "hi".into() });
624 assert_eq!(cx.notifications[1], PluginNotify { plugin: "storage".into(), op: "save".into(), input: "blob".into() });
625 assert!(cx.requests.is_empty());
626 }
627
628 #[test]
629 fn cx_http_helpers_build_requests() {
630 let mut cx = Cx::<Ev>::default();
631 cx.get("http://h/x", |_| Ev::Tap);
632 cx.post("http://h/y", "hello", |_| Ev::Tap);
633 cx.patch("http://h/z", "patch", |_| Ev::Tap);
634 cx.delete("http://h/d", |_| Ev::Tap);
635
636 let methods: Vec<&str> = cx.requests.iter().map(|(c, _)| c.op.as_str()).collect();
637 assert_eq!(methods, ["GET", "POST", "PATCH", "DELETE"]);
638 assert!(cx.requests.iter().all(|(c, _)| c.plugin == "http"));
639
640 let get_input: serde_json::Value = serde_json::from_str(&cx.requests[0].0.input).unwrap();
641 assert_eq!(get_input["url"], "http://h/x");
642 assert!(get_input["body"].is_null());
643
644 let post_input: serde_json::Value = serde_json::from_str(&cx.requests[1].0.input).unwrap();
645 assert_eq!(post_input["url"], "http://h/y");
646 assert_eq!(post_input["body"], "hello");
647 }
648}