cranpose-ui 0.1.125

UI primitives for Cranpose
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
use std::{
    cell::{Cell, RefCell},
    hash::{Hash, Hasher},
    rc::Rc,
};

use cranpose_core::NodeId;
use cranpose_foundation::{
    DelegatableNode, FocusNode, FocusState, ModifierNode, ModifierNodeContext, ModifierNodeElement,
    NodeCapabilities, NodeState, impl_focus_node,
};

use crate::{focus_dispatch, render_state::AppContextId};

/// Focus direction for navigation.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum FocusDirection {
    /// Enter focus from outside.
    Enter,
    /// Exit focus to outside.
    Exit,
    /// Move to next focusable.
    Next,
    /// Move to previous focusable.
    Previous,
    /// Move up (2D navigation).
    Up,
    /// Move down (2D navigation).
    Down,
    /// Move left (2D navigation).
    Left,
    /// Move right (2D navigation).
    Right,
}

type FocusChangedCallback = Rc<dyn Fn(FocusState)>;

struct FocusTargetShared {
    focus_state: Cell<FocusState>,
    on_focus_changed: RefCell<Option<FocusChangedCallback>>,
}

impl FocusTargetShared {
    fn new(on_focus_changed: Option<FocusChangedCallback>) -> Self {
        Self {
            focus_state: Cell::new(FocusState::Inactive),
            on_focus_changed: RefCell::new(on_focus_changed),
        }
    }

    fn set_focus_state(&self, state: FocusState) {
        let old_state = self.focus_state.get();
        if old_state != state {
            self.focus_state.set(state);
            let callback = self.on_focus_changed.borrow().clone();
            if let Some(callback) = callback {
                callback(state);
            }
        }
    }
}

impl focus_dispatch::FocusTargetHandle for FocusTargetShared {
    fn set_focus_state(&self, state: FocusState) {
        FocusTargetShared::set_focus_state(self, state);
    }
}

pub struct FocusTargetNode {
    state: NodeState,
    shared: Rc<FocusTargetShared>,
    handle: Rc<dyn focus_dispatch::FocusTargetHandle>,
    registered_node_id: Cell<Option<NodeId>>,
}

impl FocusTargetNode {
    pub fn new() -> Self {
        Self::from_callback(None)
    }

    pub fn with_callback<F>(callback: F) -> Self
    where
        F: Fn(FocusState) + 'static,
    {
        Self::from_callback(Some(Rc::new(callback) as FocusChangedCallback))
    }

    fn from_callback(on_focus_changed: Option<FocusChangedCallback>) -> Self {
        let shared = Rc::new(FocusTargetShared::new(on_focus_changed));
        let handle: Rc<dyn focus_dispatch::FocusTargetHandle> = shared.clone();
        Self {
            state: NodeState::new(),
            shared,
            handle,
            registered_node_id: Cell::new(None),
        }
    }

    pub fn set_focus_state(&self, state: FocusState) {
        self.shared.set_focus_state(state);
    }

    pub fn clear_focus(&self) {
        self.set_focus_state(FocusState::Inactive);
    }

    fn set_callback(&self, callback: Option<FocusChangedCallback>) {
        *self.shared.on_focus_changed.borrow_mut() = callback;
    }
}

impl Default for FocusTargetNode {
    fn default() -> Self {
        Self::new()
    }
}

impl DelegatableNode for FocusTargetNode {
    fn node_state(&self) -> &NodeState {
        &self.state
    }
}

impl ModifierNode for FocusTargetNode {
    fn on_attach(&mut self, context: &mut dyn ModifierNodeContext) {
        self.state.set_attached(true);
        if let Some(node_id) = context.node_id() {
            self.registered_node_id.set(Some(node_id));
            focus_dispatch::register_focus_target(node_id, Rc::clone(&self.handle));
        }
    }

    fn on_detach(&mut self) {
        self.state.set_attached(false);
        if let Some(node_id) = self.registered_node_id.take() {
            focus_dispatch::unregister_focus_target(node_id, &self.handle);
        }
        self.clear_focus();
    }

    impl_focus_node!();
}

impl FocusNode for FocusTargetNode {
    fn focus_state(&self) -> FocusState {
        self.shared.focus_state.get()
    }

    fn on_focus_changed(&mut self, _context: &mut dyn ModifierNodeContext, state: FocusState) {
        self.set_focus_state(state);
    }
}

#[derive(Clone)]
pub struct FocusTargetElement {
    on_focus_changed: Option<FocusChangedCallback>,
}

impl FocusTargetElement {
    pub fn new() -> Self {
        Self {
            on_focus_changed: None,
        }
    }

    pub fn with_callback<F>(callback: F) -> Self
    where
        F: Fn(FocusState) + 'static,
    {
        Self {
            on_focus_changed: Some(Rc::new(callback)),
        }
    }
}

impl Default for FocusTargetElement {
    fn default() -> Self {
        Self::new()
    }
}

impl std::fmt::Debug for FocusTargetElement {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("FocusTargetElement")
            .field("has_callback", &self.on_focus_changed.is_some())
            .finish()
    }
}

impl PartialEq for FocusTargetElement {
    fn eq(&self, other: &Self) -> bool {
        self.on_focus_changed.is_some() == other.on_focus_changed.is_some()
    }
}

impl Hash for FocusTargetElement {
    fn hash<H: Hasher>(&self, state: &mut H) {
        "focus_target".hash(state);
        self.on_focus_changed.is_some().hash(state);
    }
}

impl ModifierNodeElement for FocusTargetElement {
    type Node = FocusTargetNode;

    fn create(&self) -> Self::Node {
        if let Some(callback) = &self.on_focus_changed {
            FocusTargetNode::with_callback({
                let callback = callback.clone();
                move |state| callback(state)
            })
        } else {
            FocusTargetNode::new()
        }
    }

    fn update(&self, node: &mut Self::Node) {
        node.set_callback(self.on_focus_changed.clone());
    }

    fn inspector_name(&self) -> &'static str {
        "focusTarget"
    }

    fn capabilities(&self) -> NodeCapabilities {
        NodeCapabilities::FOCUS
    }

    fn always_update(&self) -> bool {
        true
    }
}

/// Why [`FocusRequester::request_focus`] could not move focus.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum FocusRequestError {
    /// The requester has never been attached to a node via
    /// [`Modifier::focus_requester`](super::Modifier::focus_requester), or the
    /// node it was attached to has since left composition.
    NotAttached,
    /// The requester's node is attached, but nothing on it — no
    /// [`Modifier::focus_target`](super::Modifier::focus_target), no
    /// [`Modifier::on_focus_changed`](super::Modifier::on_focus_changed), no
    /// text field — is registered to receive focus.
    NoFocusTarget,
}

impl std::fmt::Display for FocusRequestError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            FocusRequestError::NotAttached => {
                write!(f, "FocusRequester is not attached to a node in composition")
            }
            FocusRequestError::NoFocusTarget => write!(
                f,
                "FocusRequester's node has no focus target to move focus onto"
            ),
        }
    }
}

impl std::error::Error for FocusRequestError {}

#[derive(Clone, Copy)]
struct FocusRequesterBinding {
    app_context: AppContextId,
    node_id: NodeId,
}

/// A handle an app holds to move focus onto a node imperatively.
///
/// `remember` one, hang it on a node with
/// [`Modifier::focus_requester`](super::Modifier::focus_requester) next to a
/// [`Modifier::focus_target`](super::Modifier::focus_target) (or
/// [`Modifier::on_focus_changed`](super::Modifier::on_focus_changed)), and
/// call [`request_focus`](Self::request_focus) — from a click handler, an
/// effect that runs once a screen appears, wherever the app decides focus
/// should move.
///
/// ```ignore
/// let requester = remember(FocusRequester::new).with(Clone::clone);
/// // ... in the composition:
/// Modifier::empty().focus_requester(&requester).focus_target()
/// // ... later, imperatively:
/// requester.request_focus().ok();
/// ```
#[derive(Clone, Default)]
pub struct FocusRequester {
    binding: Rc<Cell<Option<FocusRequesterBinding>>>,
}

impl FocusRequester {
    pub fn new() -> Self {
        Self::default()
    }

    /// Moves focus onto the node this requester is attached to.
    ///
    /// See [`FocusRequestError`] for the two predictable ways this can fail
    /// instead of moving focus.
    pub fn request_focus(&self) -> Result<(), FocusRequestError> {
        let Some(binding) = self.binding.get() else {
            return Err(FocusRequestError::NotAttached);
        };
        match focus_dispatch::request_focus_for(binding.app_context, binding.node_id) {
            Some(true) => Ok(()),
            Some(false) => Err(FocusRequestError::NoFocusTarget),
            None => Err(FocusRequestError::NotAttached),
        }
    }

    /// The node this requester is attached to, if attached.
    pub fn node_id(&self) -> Option<NodeId> {
        self.binding.get().map(|binding| binding.node_id)
    }

    fn bind(&self, binding: Option<FocusRequesterBinding>) {
        self.binding.set(binding);
    }

    fn binding_here(node_id: Option<NodeId>) -> Option<FocusRequesterBinding> {
        Some(FocusRequesterBinding {
            app_context: crate::render_state::current_app_context_id_opt()?,
            node_id: node_id?,
        })
    }
}

impl std::fmt::Debug for FocusRequester {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("FocusRequester")
            .field("node_id", &self.node_id())
            .finish()
    }
}

pub struct FocusRequesterNode {
    state: NodeState,
    requester: FocusRequester,
}

impl FocusRequesterNode {
    pub(crate) fn new(requester: FocusRequester) -> Self {
        Self {
            state: NodeState::new(),
            requester,
        }
    }
}

impl DelegatableNode for FocusRequesterNode {
    fn node_state(&self) -> &NodeState {
        &self.state
    }
}

impl ModifierNode for FocusRequesterNode {
    fn on_attach(&mut self, context: &mut dyn ModifierNodeContext) {
        self.state.set_attached(true);
        self.requester
            .bind(FocusRequester::binding_here(context.node_id()));
    }

    fn on_detach(&mut self) {
        self.state.set_attached(false);
        self.requester.bind(None);
    }
}

/// Modifier element for [`FocusRequester`].
#[derive(Clone)]
pub struct FocusRequesterElement {
    requester: FocusRequester,
}

impl FocusRequesterElement {
    pub(crate) fn new(requester: FocusRequester) -> Self {
        Self { requester }
    }
}

impl std::fmt::Debug for FocusRequesterElement {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str("FocusRequesterElement")
    }
}

impl PartialEq for FocusRequesterElement {
    fn eq(&self, other: &Self) -> bool {
        Rc::ptr_eq(&self.requester.binding, &other.requester.binding)
    }
}

impl Eq for FocusRequesterElement {}

impl Hash for FocusRequesterElement {
    fn hash<H: Hasher>(&self, state: &mut H) {
        Rc::as_ptr(&self.requester.binding).hash(state);
    }
}

impl ModifierNodeElement for FocusRequesterElement {
    type Node = FocusRequesterNode;

    fn create(&self) -> Self::Node {
        FocusRequesterNode::new(self.requester.clone())
    }

    fn update(&self, node: &mut Self::Node) {
        if !Rc::ptr_eq(&node.requester.binding, &self.requester.binding) {
            let bound = node.requester.binding.get();
            node.requester.bind(None);
            node.requester = self.requester.clone();
            node.requester.bind(bound);
        }
    }

    fn inspector_name(&self) -> &'static str {
        "focusRequester"
    }

    fn capabilities(&self) -> NodeCapabilities {
        NodeCapabilities::NONE
    }
}

#[cfg(test)]
mod tests {
    use cranpose_foundation::{BasicModifierNodeContext, ModifierNodeChain};

    use super::*;

    #[test]
    fn focus_target_node_lifecycle() {
        let mut node = FocusTargetNode::new();
        let mut context = BasicModifierNodeContext::new();

        assert_eq!(node.focus_state(), FocusState::Inactive);
        assert!(!node.node_state().is_attached());

        node.on_attach(&mut context);
        assert!(node.node_state().is_attached());

        node.set_focus_state(FocusState::Active);
        assert_eq!(node.focus_state(), FocusState::Active);
        assert!(node.focus_state().is_focused());

        node.on_detach();
        assert!(!node.node_state().is_attached());
        assert_eq!(node.focus_state(), FocusState::Inactive);
    }

    #[test]
    fn focus_target_callback_invoked() {
        use std::cell::RefCell;
        let states = Rc::new(RefCell::new(Vec::new()));
        let states_clone = states.clone();

        let node = FocusTargetNode::with_callback(move |state| {
            states_clone.borrow_mut().push(state);
        });

        node.set_focus_state(FocusState::Active);
        node.set_focus_state(FocusState::ActiveParent);
        node.set_focus_state(FocusState::Inactive);

        let recorded = states.borrow();
        assert_eq!(recorded.len(), 3);
        assert_eq!(recorded[0], FocusState::Active);
        assert_eq!(recorded[1], FocusState::ActiveParent);
        assert_eq!(recorded[2], FocusState::Inactive);
    }

    #[test]
    fn focus_element_creates_node() {
        let element = FocusTargetElement::new();
        let node = element.create();
        assert_eq!(node.focus_state(), FocusState::Inactive);
    }

    #[test]
    fn focus_chain_integration() {
        let element = FocusTargetElement::new();
        let dyn_element = cranpose_foundation::modifier_element(element);

        let mut chain = ModifierNodeChain::new();
        let mut context = BasicModifierNodeContext::new();

        chain.update(vec![dyn_element], &mut context);

        assert_eq!(chain.len(), 1);
        assert!(chain.has_capability(NodeCapabilities::FOCUS));
    }

    #[test]
    fn focus_state_predicates() {
        assert!(FocusState::Active.is_focused());
        assert!(FocusState::Captured.is_focused());
        assert!(!FocusState::Inactive.is_focused());
        assert!(!FocusState::ActiveParent.is_focused());

        assert!(FocusState::Active.has_focus());
        assert!(FocusState::ActiveParent.has_focus());
        assert!(FocusState::Captured.has_focus());
        assert!(!FocusState::Inactive.has_focus());

        assert!(FocusState::Captured.is_captured());
        assert!(!FocusState::Active.is_captured());
    }

    fn attach_at(
        node_id: NodeId,
        elements: Vec<cranpose_foundation::DynModifierElement>,
    ) -> ModifierNodeChain {
        let mut context = BasicModifierNodeContext::new();
        context.set_node_id(Some(node_id));
        let mut chain = ModifierNodeChain::new();
        chain.update(elements, &mut context);
        chain
    }

    #[test]
    fn request_focus_on_a_never_attached_requester_fails_predictably() {
        let requester = FocusRequester::new();
        assert_eq!(
            requester.request_focus(),
            Err(FocusRequestError::NotAttached)
        );
    }

    #[test]
    fn request_focus_on_a_requester_with_no_focus_target_fails_predictably() {
        let _app_context = crate::render_state::app_context_test_scope();
        let requester = FocusRequester::new();

        let _chain = attach_at(
            1,
            vec![cranpose_foundation::modifier_element(
                FocusRequesterElement::new(requester.clone()),
            )],
        );

        assert_eq!(requester.node_id(), Some(1));
        assert_eq!(
            requester.request_focus(),
            Err(FocusRequestError::NoFocusTarget)
        );
    }

    #[test]
    fn a_focus_requester_moves_focus_onto_its_paired_focus_target() {
        let _app_context = crate::render_state::app_context_test_scope();
        let requester = FocusRequester::new();

        let chain = attach_at(
            2,
            vec![
                cranpose_foundation::modifier_element(FocusRequesterElement::new(
                    requester.clone(),
                )),
                cranpose_foundation::modifier_element(FocusTargetElement::new()),
            ],
        );

        assert_eq!(requester.request_focus(), Ok(()));

        let target = chain.node::<FocusTargetNode>(1).expect("focus target node");
        assert_eq!(target.focus_state(), FocusState::Active);
        assert_eq!(focus_dispatch::active_focus_target(), Some(2));
    }

    #[test]
    fn request_focus_after_the_node_leaves_composition_fails_predictably() {
        let _app_context = crate::render_state::app_context_test_scope();
        let requester = FocusRequester::new();

        let mut context = BasicModifierNodeContext::new();
        context.set_node_id(Some(3));
        let mut chain = ModifierNodeChain::new();
        chain.update(
            vec![
                cranpose_foundation::modifier_element(FocusRequesterElement::new(
                    requester.clone(),
                )),
                cranpose_foundation::modifier_element(FocusTargetElement::new()),
            ],
            &mut context,
        );
        assert!(requester.request_focus().is_ok());

        chain.update(Vec::new(), &mut context);

        assert_eq!(
            requester.request_focus(),
            Err(FocusRequestError::NotAttached)
        );
    }

    #[test]
    fn focus_survives_the_requested_node_being_recomposed() {
        let _app_context = crate::render_state::app_context_test_scope();
        let requester = FocusRequester::new();

        let mut context = BasicModifierNodeContext::new();
        context.set_node_id(Some(4));
        let mut chain = ModifierNodeChain::new();
        let make_elements = || {
            vec![
                cranpose_foundation::modifier_element(FocusRequesterElement::new(
                    requester.clone(),
                )),
                cranpose_foundation::modifier_element(FocusTargetElement::new()),
            ]
        };
        chain.update(make_elements(), &mut context);
        assert_eq!(requester.request_focus(), Ok(()));

        let node_ptr_before = {
            let node = chain.node::<FocusTargetNode>(1).unwrap();
            &*node as *const FocusTargetNode
        };

        chain.update(make_elements(), &mut context);

        let target = chain.node::<FocusTargetNode>(1).unwrap();
        let node_ptr_after = &*target as *const FocusTargetNode;
        assert_eq!(
            node_ptr_before, node_ptr_after,
            "recomposition with a structurally equal modifier must reuse the node"
        );
        assert_eq!(
            target.focus_state(),
            FocusState::Active,
            "recomposing the focused node must not reset its focus state"
        );
        assert_eq!(focus_dispatch::active_focus_target(), Some(4));
    }

    #[test]
    fn requesting_focus_from_inside_on_focus_changed_does_not_double_borrow_or_recurse() {
        let _app_context = crate::render_state::app_context_test_scope();

        let requester_a = FocusRequester::new();
        let requester_b = FocusRequester::new();
        let requester_b_for_callback = requester_b.clone();
        let bounced = Rc::new(Cell::new(false));
        let bounced_for_callback = bounced.clone();

        let chain_a = attach_at(
            10,
            vec![
                cranpose_foundation::modifier_element(FocusRequesterElement::new(
                    requester_a.clone(),
                )),
                cranpose_foundation::modifier_element(FocusTargetElement::with_callback(
                    move |state| {
                        if state == FocusState::Active && !bounced_for_callback.get() {
                            bounced_for_callback.set(true);
                            requester_b_for_callback.request_focus().expect(
                                "a reentrant request_focus must succeed, not double-borrow",
                            );
                        }
                    },
                )),
            ],
        );
        let chain_b = attach_at(
            20,
            vec![
                cranpose_foundation::modifier_element(FocusRequesterElement::new(
                    requester_b.clone(),
                )),
                cranpose_foundation::modifier_element(FocusTargetElement::new()),
            ],
        );

        let result =
            std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| requester_a.request_focus()));
        assert!(result.is_ok(), "a reentrant focus request must not panic");
        assert_eq!(result.unwrap(), Ok(()));
        assert!(bounced.get(), "the reentrant callback never ran");

        let target_a = chain_a.node::<FocusTargetNode>(1).unwrap();
        let target_b = chain_b.node::<FocusTargetNode>(1).unwrap();
        assert_eq!(target_a.focus_state(), FocusState::Inactive);
        assert_eq!(target_b.focus_state(), FocusState::Active);
        assert_eq!(focus_dispatch::active_focus_target(), Some(20));
    }
}