glory-ui 0.9.0

Pure Rust semantic widgets and windowed lists for Glory
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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
use std::collections::BTreeMap;
use std::rc::Rc;

use glory_core::{Cage, Vary};
use glory_render::{
    ButtonProps, Change, CheckProps, Descriptor, Event, EventKind, FocusMode, ImageProps,
    InputProps, ItemPosition, LinkProps, NodeId, Patch, Props, Purpose, Role, TextKind, TextProps,
};
use glory_style::{Display, FlexDirection, Style};

use crate::app::{Result, Scope, Widget};

type Bind = Box<dyn FnOnce(&Scope, NodeId) -> Result>;
type Prepare = Box<dyn FnOnce(&Scope) -> Result<(Patch, Bind)>>;
type Handler = Box<dyn FnMut(Event)>;
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
enum Slot {
    Classes,
    Style,
    Name,
    Role,
    Focus,
    Item,
    Semantics,
    Text,
    Kind,
    Selectable,
    Label,
    Enabled,
    Value,
    SelectSelection,
    RichDocument,
    Purpose,
    Multiline,
    Checked,
    CheckMode,
    Source,
    Alternative,
    Graphic,
    Target,
}

// A full initial Graphic declaration has no previous live path to interpolate.
// Seed its props before whole-descriptor construction validation; later bound
// writes still go through the canonical live topology guard.
fn seed_graphic(descriptor: &mut Descriptor, patches: &mut BTreeMap<Slot, Patch>) {
    if let (Props::Graphic(props), Some(Patch::Graphic(Change::Set(value)))) =
        (&mut descriptor.props, patches.remove(&Slot::Graphic))
    {
        *props = *value;
    }
}
struct Element {
    descriptor: Option<Descriptor>,
    initial: BTreeMap<Slot, Patch>,
    bindings: BTreeMap<Slot, Prepare>,
    handlers: BTreeMap<EventKind, Handler>,
    children: Vec<Box<dyn Widget>>,
    needs_alt: bool,
    node_ref: Option<Cage<Option<NodeId>>>,
    mark: Option<&'static str>,
}
impl Element {
    fn new(props: Props) -> Self {
        let needs_alt = matches!(props, Props::Image(_));
        Self {
            descriptor: Some(Descriptor::new(props)),
            initial: BTreeMap::new(),
            bindings: BTreeMap::new(),
            handlers: BTreeMap::new(),
            children: Vec::new(),
            needs_alt,
            node_ref: None,
            mark: None,
        }
    }
    fn set<T: Clone + PartialEq + 'static>(
        &mut self,
        slot: Slot,
        value: impl Into<Vary<T>>,
        mut patch: impl FnMut(T) -> Patch + 'static,
    ) {
        self.initial.remove(&slot);
        self.bindings.remove(&slot);
        match value.into() {
            Vary::Value(value) => {
                self.initial.insert(slot, patch(value));
            }
            value => {
                self.bindings.insert(
                    slot,
                    Box::new(move |_| {
                        // Read without subscribing the surrounding widget or
                        // structural observer. The binding registers its own
                        // dependencies when it first runs after construction.
                        let initial = glory_core::untracked(|| value.try_get())?;
                        let initial_patch = patch(initial.clone());
                        let install: Bind = Box::new(move |scope, node| {
                            let session = Rc::downgrade(&scope.session()?);
                            let mut previous = Some(initial);
                            scope.binding(move || {
                                let Some(session) = session.upgrade() else {
                                    return;
                                };
                                let value = match value.try_get() {
                                    Ok(value) => value,
                                    Err(error) => {
                                        session.report(error.into());
                                        return;
                                    }
                                };
                                if previous.as_ref() == Some(&value) {
                                    return;
                                }
                                let update = patch(value.clone());
                                let result = match update {
                                    Patch::RichDocument {
                                        document: Change::Set(value),
                                        ..
                                    } => session.rich_document(node, value),
                                    Patch::Value {
                                        value: Change::Set(value),
                                        ..
                                    } => session.input_value(node, value),
                                    Patch::SelectSelection {
                                        keys: Change::Set(keys),
                                        ..
                                    } => session.select_selection(node, keys),
                                    Patch::Source {
                                        source: Change::Set(value),
                                        ..
                                    } => session.image_source(node, value),
                                    update => session.patch(node, update),
                                };
                                match result {
                                    Ok(()) => previous = Some(value),
                                    Err(error) => session.report(error),
                                }
                            });
                            Ok(())
                        });
                        Ok((initial_patch, install))
                    }),
                );
            }
        }
    }
    fn set_fn<T: Clone + PartialEq + 'static>(
        &mut self,
        slot: Slot,
        mut read: impl FnMut() -> T + 'static,
        mut patch: impl FnMut(T) -> Patch + 'static,
    ) {
        self.initial.remove(&slot);
        self.bindings.insert(
            slot,
            Box::new(move |scope| {
                let initial = scope.owner().compute(&mut read)?;
                let update = patch(initial.clone());
                let install: Bind = Box::new(move |scope, node| {
                    crate::plan::bind_fn(scope, node, read, patch, Some(initial))
                });
                Ok((update, install))
            }),
        );
    }
    fn build(&mut self, scope: &Scope) -> Result {
        if self.needs_alt {
            return Err(glory_render::Error::InvalidProps(
                "image requires alt text or explicit decorative()",
            )
            .into());
        }
        let mut descriptor = self.descriptor.take().ok_or(crate::app::Error::Gone)?;
        if let Props::File(props) = &descriptor.props {
            props.configuration.validate()?;
        }
        let mut initial = std::mem::take(&mut self.initial);
        let mut bindings = Vec::with_capacity(self.bindings.len());
        for (slot, prepare) in std::mem::take(&mut self.bindings) {
            let (patch, install) = prepare(scope)?;
            initial.insert(slot, patch);
            bindings.push(install);
        }
        // Validate the complete initial descriptor and adapter profile before
        // allocating identity or changing the semantic model/host queue.
        seed_graphic(&mut descriptor, &mut initial);
        descriptor.patches(initial.into_values())?;
        let node = scope.create(descriptor)?;
        for bind in bindings {
            bind(scope, node)?;
        }
        for (kind, handler) in std::mem::take(&mut self.handlers) {
            scope.on(node, kind, handler)?;
        }
        for child in self.children.drain(..) {
            scope.fill(node, child)?;
        }
        if let Some(reference) = self.node_ref.take() {
            reference.try_revise(|value| *value = Some(node))?;
            scope.on_cleanup(move || {
                let _ = reference.try_revise(|value| {
                    if *value == Some(node) {
                        *value = None;
                    }
                });
            })?;
        }
        Ok(())
    }
    fn options<T: 'static>(&mut self, value: T) {
        self.descriptor
            .as_mut()
            .expect("unmounted builder")
            .options
            .insert(value);
    }
    fn static_plan(&mut self) -> Result<crate::Plan> {
        if self.needs_alt
            || !self.bindings.is_empty()
            || !self.handlers.is_empty()
            || self.node_ref.is_some()
        {
            return Err(glory_render::Error::InvalidProps(
                "plan requires static declarations; bind events/values on the instance",
            )
            .into());
        }
        let mut descriptor = self.descriptor.take().ok_or(crate::Error::Gone)?;
        if let Props::File(props) = &descriptor.props {
            props.configuration.validate()?;
        }
        let mut initial = std::mem::take(&mut self.initial);
        seed_graphic(&mut descriptor, &mut initial);
        descriptor.patches(initial.into_values())?;
        let mut nodes = vec![glory_render::PlanNode {
            parent: None,
            descriptor,
        }];
        let mut marks = self
            .mark
            .take()
            .map(|name| vec![(name, 0)])
            .unwrap_or_default();
        for mut child in self.children.drain(..) {
            child.static_plan()?.append(0, &mut nodes, &mut marks);
        }
        crate::Plan::from_nodes(nodes, marks)
    }
}

pub struct Children(Vec<Box<dyn Widget>>);
/// A movable widget fragment without a semantic wrapper node.
pub struct Fragment(Children);
pub fn fragment(children: impl IntoChildren) -> Fragment {
    Fragment(children.into_children())
}
impl Widget for Fragment {
    fn is_empty(&self) -> bool {
        self.0.0.is_empty()
    }
    fn compose(&mut self, _: &glory_core::Scope) -> Result<Option<Box<dyn Widget>>> {
        if self.0.0.len() == 1 {
            Ok(self.0.0.pop())
        } else {
            Ok(None)
        }
    }
    fn build(&mut self, scope: &Scope) -> Result {
        for child in std::mem::take(&mut self.0.0) {
            child.show_in(scope)?;
        }
        Ok(())
    }
}
impl Children {
    #[doc(hidden)]
    pub fn into_widgets(self) -> Vec<Box<dyn Widget>> {
        self.0
    }
}
impl IntoChildren for Children {
    fn into_children(self) -> Children {
        self
    }
}
pub trait IntoChildren {
    fn into_children(self) -> Children;
}
impl<W: Widget> IntoChildren for W {
    fn into_children(self) -> Children {
        Children(vec![Box::new(self)])
    }
}
impl<W: Widget> IntoChildren for Vec<W> {
    fn into_children(self) -> Children {
        Children(
            self.into_iter()
                .map(|value| Box::new(value) as Box<dyn Widget>)
                .collect(),
        )
    }
}
impl IntoChildren for () {
    fn into_children(self) -> Children {
        Children(Vec::new())
    }
}
macro_rules! children_tuple {
    ($($name:ident),+) => {
        impl<$($name: Widget),+> IntoChildren for ($($name,)+) {
            #[allow(non_snake_case)]
            fn into_children(self) -> Children { let ($($name,)+) = self; Children(vec![$(Box::new($name)),+]) }
        }
    };
}
children_tuple!(A, B);
children_tuple!(A, B, C);
children_tuple!(A, B, C, D);
children_tuple!(A, B, C, D, E);
children_tuple!(A, B, C, D, E, F);
children_tuple!(A, B, C, D, E, F, G);
children_tuple!(A, B, C, D, E, F, G, H);
children_tuple!(A, B, C, D, E, F, G, H, I);
children_tuple!(A, B, C, D, E, F, G, H, I, J);
children_tuple!(A, B, C, D, E, F, G, H, I, J, K);
children_tuple!(A, B, C, D, E, F, G, H, I, J, K, L);

macro_rules! element {
    ($name:ident, $factory:ident, $props:expr) => {
        pub struct $name {
            inner: Element,
        }
        pub fn $factory() -> $name {
            $name {
                inner: Element::new($props),
            }
        }
        impl Default for $name {
            fn default() -> Self {
                $factory()
            }
        }
        impl Widget for $name {
            fn static_plan(&mut self) -> Result<crate::Plan> {
                self.inner.static_plan()
            }
            fn build(&mut self, scope: &Scope) -> Result {
                self.inner.build(scope)
            }
        }
        impl $name {
            /// Name a part of an explicit static plan; not a DOM id or data key.
            pub fn mark(mut self, name: &'static str) -> Self {
                self.inner.mark = Some(name);
                self
            }
            pub fn backend_options<T: 'static>(mut self, value: T) -> Self {
                self.inner.options(value);
                self
            }
        }
    };
}
element!(View, view, Props::View);
element!(Group, group, Props::Group);
element!(Text, text, Props::Text(TextProps::default()));
element!(Button, button, Props::Button(ButtonProps::default()));
element!(Input, input, Props::Input(InputProps::default()));
element!(
    RichEditor,
    rich_editor,
    Props::RichEditor(Default::default())
);
element!(File, file, Props::File(glory_render::FileProps::default()));
element!(
    Select,
    select,
    Props::Select(glory_render::SelectProps::default())
);
element!(Check, check, Props::Check(CheckProps::default()));
element!(Image, image, Props::Image(ImageProps::default()));
element!(
    Graphic,
    graphic,
    Props::Graphic(glory_render::GraphicProps::default())
);
element!(Link, link, Props::Link(LinkProps::default()));

macro_rules! common {
    ($($name:ident),+) => {$ (
        impl $name {
            /// Set the complete whitespace-separated class list (last call wins).
            pub fn class(mut self, value: impl Into<Vary<String>>) -> Self {
                self.inner.set(Slot::Classes, value, |value: String| Patch::Classes(Change::Set(value.split_whitespace().map(str::to_owned).collect()))); self
            }
            /// Compute one property directly in its pure Binding.
            pub fn class_fn(mut self, read: impl FnMut() -> String + 'static) -> Self {
                self.inner.set_fn(Slot::Classes, read, |value: String| Patch::Classes(Change::Set(value.split_whitespace().map(str::to_owned).collect()))); self
            }
            pub fn style_fn(mut self, read: impl FnMut() -> Style + 'static) -> Self {
                self.inner.set_fn(Slot::Style, read, |value| Patch::Style(Change::Set(Box::new(value)))); self
            }
            pub fn style(mut self, value: impl Into<Vary<Style>>) -> Self {
                self.inner.set(Slot::Style, value, |value| Patch::Style(Change::Set(Box::new(value)))); self
            }
            /// Accessible name. Plain inline/paragraph text uses content;
            /// decorative images have no accessible name. A named generic View
            /// becomes an accessible group without changing its layout box.
            pub fn name(mut self, value: impl Into<Vary<String>>) -> Self {
                self.inner.set(Slot::Name, value, |value| Patch::Name(Change::Set(Some(value)))); self
            }
            pub fn focus(mut self, mode: impl Into<Vary<FocusMode>>) -> Self { self.inner.set(Slot::Focus, mode, |value| Patch::Focus(Change::Set(value))); self }
            /// Finite composite-widget state and document-scoped relations.
            pub fn semantics(mut self, value: impl Into<Vary<Option<Box<glory_render::Semantics>>>>) -> Self {
                self.inner.set(Slot::Semantics, value, |value| Patch::Semantics(Change::Set(value))); self
            }
            pub fn on(mut self, kind: EventKind, handler: impl FnMut(Event) + 'static) -> Self { self.inner.handlers.insert(kind, Box::new(handler)); self }
            /// Publish the mounted semantic identity and clear it on retirement.
            pub fn node_ref(mut self, reference: Cage<Option<NodeId>>) -> Self { self.inner.node_ref = Some(reference); self }
        }
    )+};
}
common!(
    View, Text, Button, Input, File, Select, Check, Image, Graphic, Link, RichEditor
);
impl RichEditor {
    pub fn document(mut self, value: impl Into<Vary<Rc<glory_render::RichDocument>>>) -> Self {
        self.inner
            .set(Slot::RichDocument, value, |document| Patch::RichDocument {
                document: Change::Set(document),
                revision: 1,
            });
        self
    }
    pub fn enabled(mut self, value: impl Into<Vary<bool>>) -> Self {
        self.inner.set(Slot::Enabled, value, |value| {
            Patch::Enabled(Change::Set(value))
        });
        self
    }
}
impl Graphic {
    pub fn drawing(mut self, value: impl Into<Vary<glory_render::GraphicProps>>) -> Self {
        self.inner.set(Slot::Graphic, value, |value| {
            Patch::Graphic(Change::Set(Box::new(value)))
        });
        self
    }
}
impl View {
    /// Intrinsic document structure, installed before reactive metadata. Its
    /// kind cannot change on a live node; remount for a different document part.
    pub fn document_part(mut self, part: glory_render::DocumentPart) -> Self {
        self.inner
            .descriptor
            .as_mut()
            .expect("unmounted builder")
            .semantics
            .get_or_insert_with(Default::default)
            .document_part = Some(part);
        self
    }
    /// Structural form kind is installed before reactive metadata bindings.
    pub fn field_part(mut self, part: glory_render::FieldPart) -> Self {
        self.inner
            .descriptor
            .as_mut()
            .expect("unmounted builder")
            .semantics
            .get_or_insert_with(Default::default)
            .field_part = Some(part);
        self
    }
    /// Structural role; controls, text and images retain their own semantics.
    pub fn role(mut self, role: Role) -> Self {
        self.inner.set(Slot::Role, Some(role), |value| {
            Patch::Role(Change::Set(value))
        });
        self
    }
    pub fn item(mut self, value: impl Into<Vary<Option<ItemPosition>>>) -> Self {
        self.inner
            .set(Slot::Item, value, |value| Patch::Item(Change::Set(value)));
        self
    }
}
macro_rules! fill {
    ($($name:ident),+) => {$ (
        impl $name { pub fn fill(mut self, children: impl IntoChildren) -> Self { self.inner.children.extend(children.into_children().0); self } }
    )+};
}
fill!(View, Group, Button, Link);
macro_rules! label {
    ($($name:ident),+) => {$ (
        impl $name {
            pub fn label_fn(mut self, read: impl FnMut() -> String + 'static) -> Self {
                self.inner.set_fn(Slot::Label, read, |value| Patch::Label(Change::Set(value))); self
            }
            pub fn label(mut self, value: impl Into<Vary<String>>) -> Self { self.inner.set(Slot::Label, value, |value| Patch::Label(Change::Set(value))); self } }
    )+};
}
label!(Button, Check, Link);
macro_rules! enabled {
    ($($name:ident),+) => {$ (
        impl $name { pub fn enabled(mut self, value: impl Into<Vary<bool>>) -> Self { self.inner.set(Slot::Enabled, value, |value| Patch::Enabled(Change::Set(value))); self } }
    )+};
}
enabled!(Button, Input, File, Check);
impl File {
    pub fn label(mut self, value: impl Into<String>) -> Self {
        self.inner.set(Slot::Label, value.into(), |value| {
            Patch::Label(Change::Set(value))
        });
        self
    }
    /// Initial finite declaration. Reactive changes use Scope::set_file_configuration,
    /// which allocates from this live node's accepted configuration revision.
    pub fn configuration(mut self, value: glory_render::FileConfig) -> Self {
        let Props::File(props) = &mut self
            .inner
            .descriptor
            .as_mut()
            .expect("unmounted file builder")
            .props
        else {
            unreachable!()
        };
        props.configuration = value;
        self
    }
}
impl Text {
    pub fn content_fn(mut self, read: impl FnMut() -> String + 'static) -> Self {
        self.inner
            .set_fn(Slot::Text, read, |value| Patch::Text(Change::Set(value)));
        self
    }
    pub fn content(mut self, value: impl Into<Vary<String>>) -> Self {
        self.inner
            .set(Slot::Text, value, |value| Patch::Text(Change::Set(value)));
        self
    }
    pub fn kind(mut self, value: TextKind) -> Self {
        self.inner.set(Slot::Kind, value, |value| {
            Patch::TextKind(Change::Set(value))
        });
        self
    }
    pub fn selectable(mut self, value: impl Into<Vary<bool>>) -> Self {
        self.inner.set(Slot::Selectable, value, |value| {
            Patch::Selectable(Change::Set(value))
        });
        self
    }
}
impl Input {
    pub fn value(mut self, value: impl Into<Vary<String>>) -> Self {
        // Static configuration starts at one; bound writes allocate from the
        // live node so explicit Scope patches cannot make this producer regress.
        self.inner.set(Slot::Value, value, |value| Patch::Value {
            value: Change::Set(value),
            revision: 1,
        });
        self
    }
    pub fn purpose(mut self, value: impl Into<Vary<Purpose>>) -> Self {
        self.inner.set(Slot::Purpose, value, |value| {
            Patch::Purpose(Change::Set(value))
        });
        self
    }
    pub fn multiline(mut self, value: bool) -> Self {
        self.inner.set(Slot::Multiline, value, |value| {
            Patch::Multiline(Change::Set(value))
        });
        self
    }
}
impl Check {
    pub fn mode(mut self, value: impl Into<Vary<glory_render::CheckMode>>) -> Self {
        self.inner.set(Slot::CheckMode, value, |value| {
            Patch::CheckMode(Change::Set(value))
        });
        self
    }

    pub fn checked(mut self, value: impl Into<Vary<bool>>) -> Self {
        self.inner.set(Slot::Checked, value, |value| {
            Patch::Checked(Change::Set(value))
        });
        self
    }
}
impl Image {
    pub fn source(mut self, value: impl Into<Vary<String>>) -> Self {
        self.inner.set(Slot::Source, value, |value| Patch::Source {
            source: Change::Set(value),
            revision: 1,
        });
        self
    }
    pub fn responsive(mut self, value: glory_render::ResponsiveImage) -> Self {
        let Props::Image(props) = &mut self
            .inner
            .descriptor
            .as_mut()
            .expect("unmounted builder")
            .props
        else {
            unreachable!()
        };
        props.responsive = value;
        self
    }
    pub fn loading(mut self, value: glory_render::ImageLoading) -> Self {
        let Props::Image(props) = &mut self
            .inner
            .descriptor
            .as_mut()
            .expect("unmounted builder")
            .props
        else {
            unreachable!()
        };
        props.loading = value;
        self
    }
    pub fn alt(mut self, value: impl Into<Vary<String>>) -> Self {
        self.inner.needs_alt = false;
        self.inner.set(Slot::Alternative, value, |value| {
            Patch::Alternative(Change::Set(Some(value)))
        });
        self
    }
    pub fn decorative(mut self) -> Self {
        self.inner.needs_alt = false;
        self.inner.set(Slot::Alternative, None::<String>, |value| {
            Patch::Alternative(Change::Set(value))
        });
        self
    }
}
impl Link {
    pub fn target(mut self, value: impl Into<Vary<String>>) -> Self {
        self.inner.set(Slot::Target, value, |value| {
            Patch::Target(Change::Set(value))
        });
        self
    }
}

pub fn row() -> View {
    view().style(
        Style::new()
            .display(Display::Flex)
            .flex_direction(FlexDirection::Row),
    )
}
pub fn col() -> View {
    view().style(
        Style::new()
            .display(Display::Flex)
            .flex_direction(FlexDirection::Column),
    )
}

/// An owner-scoped widget built by a handwritten Rust closure.
pub struct WidgetFn<F>(F);
/// Build once in the mounted scope; install bindings for reactive updates.
pub fn widget<F: FnMut(&Scope) -> Result + 'static>(build: F) -> WidgetFn<F> {
    WidgetFn(build)
}
impl<F: FnMut(&Scope) -> Result + 'static> Widget for WidgetFn<F> {
    fn build(&mut self, scope: &Scope) -> Result {
        (self.0)(scope)
    }
}

pub mod events {
    pub use glory_render::Event;
    use glory_render::EventKind;
    #[allow(non_upper_case_globals)]
    pub const activate: EventKind = EventKind::Activate;
    #[allow(non_upper_case_globals)]
    pub const value_changed: EventKind = EventKind::ValueChanged;
    #[allow(non_upper_case_globals)]
    pub const checked_changed: EventKind = EventKind::CheckedChanged;
    #[allow(non_upper_case_globals)]
    pub const select_changed: EventKind = EventKind::SelectChanged;
    #[allow(non_upper_case_globals)]
    pub const choice_command: EventKind = EventKind::Choice;
    #[allow(non_upper_case_globals)]
    pub const tags_command: EventKind = EventKind::Tags;
    #[allow(non_upper_case_globals)]
    pub const context_menu: EventKind = EventKind::ContextMenu;
    #[allow(non_upper_case_globals)]
    pub const commit: EventKind = EventKind::Commit;
    #[allow(non_upper_case_globals)]
    pub const focus_changed: EventKind = EventKind::FocusChanged;
    #[allow(non_upper_case_globals)]
    pub const focus_within: EventKind = EventKind::FocusWithin;
    #[allow(non_upper_case_globals)]
    pub const navigate: EventKind = EventKind::Navigate;
    #[allow(non_upper_case_globals)]
    pub const typeahead: EventKind = EventKind::Typeahead;
    #[allow(non_upper_case_globals)]
    pub const menu_command: EventKind = EventKind::Menu;
    #[allow(non_upper_case_globals)]
    pub const pointer_boundary: EventKind = EventKind::Pointer;
    #[allow(non_upper_case_globals)]
    pub const pointer_within: EventKind = EventKind::PointerWithin;
    #[allow(non_upper_case_globals)]
    pub const keyboard: EventKind = EventKind::Keyboard;
    #[allow(non_upper_case_globals)]
    pub const drag: EventKind = EventKind::Drag;
    #[allow(non_upper_case_globals)]
    pub const composition: EventKind = EventKind::Composition;
    #[allow(non_upper_case_globals)]
    pub const scroll: EventKind = EventKind::Scroll;
    #[allow(non_upper_case_globals)]
    pub const resize: EventKind = EventKind::Resize;
}

impl Select {
    /// Static bounded intrinsic options; selection has an independent revision.
    pub fn configuration(mut self, value: glory_render::SelectProps) -> Self {
        self.inner
            .descriptor
            .as_mut()
            .expect("unmounted builder")
            .props = Props::Select(value);
        self
    }
    pub fn selected(mut self, value: impl Into<Vary<Vec<glory_render::SemanticId>>>) -> Self {
        self.inner.set(Slot::SelectSelection, value, |keys| {
            Patch::SelectSelection {
                keys: Change::Set(keys),
                revision: 1,
            }
        });
        self
    }
    pub fn enabled(mut self, value: impl Into<Vary<bool>>) -> Self {
        self.inner
            .set(Slot::Enabled, value, |v| Patch::Enabled(Change::Set(v)));
        self
    }
}