tui-dispatch-components 0.7.0

Pre-built UI components for tui-dispatch
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
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
use std::any::TypeId;
use std::cell::RefCell;
use std::collections::HashMap;
use std::marker::PhantomData;
use std::rc::Rc;

use crossterm::event::{KeyEvent, KeyModifiers, MouseEvent};
use ratatui::{layout::Rect, Frame};
use tui_dispatch_core::{
    Action as ActionTrait, BindingContext, ComponentId, DefaultBindingContext, EventBus, EventKind,
    EventType, HandlerResponse, RoutedEvent,
};

/// Routed input delivered to interactive components.
#[derive(Debug, Clone)]
pub enum ComponentInput<'a, Ctx> {
    Command {
        name: &'a str,
        ctx: Ctx,
    },
    Key(KeyEvent),
    Mouse(MouseEvent),
    Scroll {
        column: u16,
        row: u16,
        delta: isize,
        modifiers: KeyModifiers,
    },
    Resize(u16, u16),
    Tick,
}

impl<'a, Id, Ctx> From<RoutedEvent<'a, Id, Ctx>> for ComponentInput<'a, Ctx>
where
    Id: ComponentId,
    Ctx: BindingContext,
{
    fn from(event: RoutedEvent<'a, Id, Ctx>) -> Self {
        if let Some(name) = event.command {
            return Self::Command {
                name,
                ctx: event.binding_ctx,
            };
        }

        match event.kind {
            EventKind::Key(key) => Self::Key(key),
            EventKind::Mouse(mouse) => Self::Mouse(mouse),
            EventKind::Scroll {
                column,
                row,
                delta,
                modifiers,
            } => Self::Scroll {
                column,
                row,
                delta,
                modifiers,
            },
            EventKind::Resize(width, height) => Self::Resize(width, height),
            EventKind::Tick => Self::Tick,
        }
    }
}

/// Optional component-local state inspection for debug tooling.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ComponentDebugEntry {
    pub key: String,
    pub value: String,
}

impl ComponentDebugEntry {
    pub fn new(key: impl Into<String>, value: impl Into<String>) -> Self {
        Self {
            key: key.into(),
            value: value.into(),
        }
    }
}

/// Optional debug surface for interactive components.
pub trait ComponentDebugState {
    fn debug_state(&self) -> Vec<ComponentDebugEntry> {
        Vec::new()
    }
}

/// Richer component contract used by [`ComponentHost`].
pub trait InteractiveComponent<A, Ctx = DefaultBindingContext>: ComponentDebugState {
    type Props<'a>
    where
        Self: 'a;

    /// Event types this component should receive when bound through [`ComponentHost`].
    ///
    /// Focused and hovered components still receive routed input through the normal
    /// EventBus routing path. Subscriptions are for events the component wants even
    /// when it is not the focused or hovered target.
    fn subscriptions() -> &'static [EventType] {
        &[EventType::Key]
    }

    #[allow(unused_variables)]
    fn update(
        &mut self,
        input: ComponentInput<'_, Ctx>,
        props: Self::Props<'_>,
    ) -> HandlerResponse<A> {
        HandlerResponse::ignored()
    }

    fn render(&mut self, frame: &mut Frame, area: Rect, props: Self::Props<'_>);
}

/// Factory that builds component props from store state.
pub trait PropsFactory<S, C, A, Ctx>: 'static
where
    C: InteractiveComponent<A, Ctx> + 'static,
{
    fn props<'a>(&self, state: &'a S) -> C::Props<'a>;
}

impl<S, C, A, Ctx, F> PropsFactory<S, C, A, Ctx> for F
where
    C: InteractiveComponent<A, Ctx> + 'static,
    F: for<'a> Fn(&'a S) -> C::Props<'a> + 'static,
{
    fn props<'a>(&self, state: &'a S) -> C::Props<'a> {
        (self)(state)
    }
}

/// Typed handle to a mounted component.
#[derive(Debug, PartialEq, Eq, Hash)]
pub struct Mounted<C> {
    raw: u32,
    _marker: PhantomData<fn() -> C>,
}

impl<C> Mounted<C> {
    fn new(raw: u32) -> Self {
        Self {
            raw,
            _marker: PhantomData,
        }
    }
}

impl<C> Copy for Mounted<C> {}

impl<C> Clone for Mounted<C> {
    fn clone(&self) -> Self {
        *self
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum HostLifecycleError<Id> {
    NotMounted(u32),
    StillBound(Id),
    TypeMismatch {
        expected: &'static str,
        actual: &'static str,
    },
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MountedComponentInfo<Id> {
    pub raw: u32,
    pub type_name: &'static str,
    pub bound_id: Option<Id>,
    pub last_area: Option<Rect>,
    pub debug_state: Vec<ComponentDebugEntry>,
}

pub struct ComponentHost<S, A, Id, Ctx> {
    inner: Rc<RefCell<ComponentHostInner<S, A, Id, Ctx>>>,
}

impl<S, A, Id, Ctx> Clone for ComponentHost<S, A, Id, Ctx> {
    fn clone(&self) -> Self {
        Self {
            inner: Rc::clone(&self.inner),
        }
    }
}

impl<S, A, Id, Ctx> Default for ComponentHost<S, A, Id, Ctx>
where
    S: 'static,
    A: 'static,
    Id: ComponentId + 'static,
    Ctx: BindingContext + 'static,
{
    fn default() -> Self {
        Self::new()
    }
}

trait ErasedMounted<S, A, Id, Ctx> {
    fn type_id(&self) -> TypeId;
    fn type_name(&self) -> &'static str;
    fn binding(&self) -> Option<Id>;
    fn set_binding(&mut self, id: Option<Id>);
    fn last_area(&self) -> Option<Rect>;
    fn render_epoch(&self) -> u64;
    fn update(&mut self, input: ComponentInput<'_, Ctx>, state: &S) -> HandlerResponse<A>;
    fn render(&mut self, frame: &mut Frame, area: Rect, state: &S, frame_epoch: u64);
    fn reset_local_state(&mut self);
    fn debug_state(&self) -> Vec<ComponentDebugEntry>;
}

struct MountedEntry<S, A, Id, Ctx, C>
where
    C: InteractiveComponent<A, Ctx> + 'static,
{
    component: C,
    factory: Box<dyn Fn() -> C>,
    props_factory: Box<dyn PropsFactory<S, C, A, Ctx>>,
    bound_id: Option<Id>,
    last_area: Option<Rect>,
    last_render_epoch: u64,
}

impl<S, A, Id, Ctx, C> ErasedMounted<S, A, Id, Ctx> for MountedEntry<S, A, Id, Ctx, C>
where
    S: 'static,
    A: 'static,
    Ctx: 'static,
    C: InteractiveComponent<A, Ctx> + 'static,
    Id: Copy,
{
    fn type_id(&self) -> TypeId {
        TypeId::of::<C>()
    }

    fn type_name(&self) -> &'static str {
        std::any::type_name::<C>()
    }

    fn binding(&self) -> Option<Id> {
        self.bound_id
    }

    fn set_binding(&mut self, id: Option<Id>) {
        self.bound_id = id;
    }

    fn last_area(&self) -> Option<Rect> {
        self.last_area
    }

    fn render_epoch(&self) -> u64 {
        self.last_render_epoch
    }

    fn update(&mut self, input: ComponentInput<'_, Ctx>, state: &S) -> HandlerResponse<A> {
        let props = self.props_factory.props(state);
        self.component.update(input, props)
    }

    fn render(&mut self, frame: &mut Frame, area: Rect, state: &S, frame_epoch: u64) {
        self.last_area = Some(area);
        self.last_render_epoch = frame_epoch;
        let props = self.props_factory.props(state);
        self.component.render(frame, area, props);
    }

    fn reset_local_state(&mut self) {
        self.component = (self.factory)();
    }

    fn debug_state(&self) -> Vec<ComponentDebugEntry> {
        self.component.debug_state()
    }
}

struct ComponentHostInner<S, A, Id, Ctx> {
    next_raw: u32,
    frame_epoch: u64,
    mounted: HashMap<u32, Box<dyn ErasedMounted<S, A, Id, Ctx>>>,
    bindings: HashMap<Id, u32>,
}

impl<S, A, Id, Ctx> ComponentHostInner<S, A, Id, Ctx>
where
    Id: ComponentId,
    Ctx: BindingContext,
{
    fn new() -> Self {
        Self {
            next_raw: 1,
            frame_epoch: 1,
            mounted: HashMap::new(),
            bindings: HashMap::new(),
        }
    }
}

impl<S, A, Id, Ctx> ComponentHost<S, A, Id, Ctx>
where
    S: 'static,
    A: 'static,
    Id: ComponentId + 'static,
    Ctx: BindingContext + 'static,
{
    pub fn new() -> Self {
        Self {
            inner: Rc::new(RefCell::new(ComponentHostInner::new())),
        }
    }

    pub fn mount<C, P>(&self, factory: impl Fn() -> C + 'static, props: P) -> Mounted<C>
    where
        C: InteractiveComponent<A, Ctx> + 'static,
        P: PropsFactory<S, C, A, Ctx> + 'static,
    {
        let mut inner = self.inner.borrow_mut();
        let raw = inner.next_raw;
        inner.next_raw = inner.next_raw.saturating_add(1);
        let component = factory();
        inner.mounted.insert(
            raw,
            Box::new(MountedEntry {
                component,
                factory: Box::new(factory),
                props_factory: Box::new(props),
                bound_id: None,
                last_area: None,
                last_render_epoch: 0,
            }),
        );
        Mounted::new(raw)
    }

    pub fn unmount<C>(&self, mounted: Mounted<C>) -> Result<(), HostLifecycleError<Id>>
    where
        C: InteractiveComponent<A, Ctx> + 'static,
    {
        let mut inner = self.inner.borrow_mut();
        inner.ensure_type::<C>(mounted.raw)?;
        let entry = inner
            .mounted
            .get(&mounted.raw)
            .ok_or(HostLifecycleError::NotMounted(mounted.raw))?;

        if let Some(id) = entry.binding() {
            return Err(HostLifecycleError::StillBound(id));
        }

        inner.mounted.remove(&mounted.raw);
        Ok(())
    }

    pub fn update<C>(
        &self,
        mounted: Mounted<C>,
        input: ComponentInput<'_, Ctx>,
        state: &S,
    ) -> HandlerResponse<A>
    where
        C: InteractiveComponent<A, Ctx> + 'static,
    {
        let mut inner = self.inner.borrow_mut();
        inner.expect_type::<C>(mounted.raw);
        inner
            .mounted
            .get_mut(&mounted.raw)
            .expect("component handle points to an unmounted component")
            .update(input, state)
    }

    pub fn render<C>(&self, mounted: Mounted<C>, frame: &mut Frame, area: Rect, state: &S)
    where
        C: InteractiveComponent<A, Ctx> + 'static,
    {
        let mut inner = self.inner.borrow_mut();
        inner.expect_type::<C>(mounted.raw);
        let frame_epoch = inner.frame_epoch;
        inner
            .mounted
            .get_mut(&mounted.raw)
            .expect("component handle points to an unmounted component")
            .render(frame, area, state, frame_epoch);
    }

    pub fn reset_local_state(&self) {
        let mut inner = self.inner.borrow_mut();
        for entry in inner.mounted.values_mut() {
            entry.reset_local_state();
        }
    }

    pub fn mounted_components(&self) -> Vec<MountedComponentInfo<Id>> {
        let inner = self.inner.borrow();
        let mut info: Vec<_> = inner
            .mounted
            .iter()
            .map(|(raw, entry)| MountedComponentInfo {
                raw: *raw,
                type_name: entry.type_name(),
                bound_id: entry.binding(),
                last_area: entry.last_area(),
                debug_state: entry.debug_state(),
            })
            .collect();
        info.sort_by_key(|entry| entry.raw);
        info
    }
}

impl<S, A, Id, Ctx> ComponentHost<S, A, Id, Ctx>
where
    S: 'static,
    A: 'static + ActionTrait,
    Id: ComponentId + 'static,
    Ctx: BindingContext + 'static,
{
    pub fn bind<C>(&self, bus: &mut EventBus<S, A, Id, Ctx>, id: Id, mounted: Mounted<C>)
    where
        C: InteractiveComponent<A, Ctx> + 'static,
    {
        let (replaced_route, previous_binding) = {
            let mut inner = self.inner.borrow_mut();
            inner.expect_type::<C>(mounted.raw);

            let previous_binding = inner
                .mounted
                .get(&mounted.raw)
                .expect("component handle points to an unmounted component")
                .binding();
            let replaced_route = inner.bindings.get(&id).copied();

            if let Some(previous_raw) = replaced_route {
                if previous_raw != mounted.raw {
                    if let Some(entry) = inner.mounted.get_mut(&previous_raw) {
                        entry.set_binding(None);
                    }
                }
            }

            if let Some(previous_id) = previous_binding {
                if previous_id != id {
                    inner.bindings.remove(&previous_id);
                }
            }

            inner.bindings.insert(id, mounted.raw);
            if let Some(entry) = inner.mounted.get_mut(&mounted.raw) {
                entry.set_binding(Some(id));
            }

            (replaced_route, previous_binding)
        };

        if replaced_route.is_some() {
            bus.unregister(id);
        }
        if let Some(previous_id) = previous_binding {
            if previous_id != id {
                bus.unregister(previous_id);
                bus.context_mut().remove_component_area(previous_id);
            }
        }

        let host = self.clone();
        bus.register(id, move |event, state| {
            host.update(mounted, event.into(), state)
        });
        bus.subscribe_many(id, C::subscriptions());
    }

    pub fn unbind(&self, bus: &mut EventBus<S, A, Id, Ctx>, id: Id) {
        let mut inner = self.inner.borrow_mut();
        if let Some(raw) = inner.bindings.remove(&id) {
            if let Some(entry) = inner.mounted.get_mut(&raw) {
                entry.set_binding(None);
            }
        }
        bus.unregister(id);
        bus.context_mut().remove_component_area(id);
    }

    pub fn sync_areas(&self, bus: &mut EventBus<S, A, Id, Ctx>) {
        let mut areas = Vec::new();
        {
            let mut inner = self.inner.borrow_mut();
            let frame_epoch = inner.frame_epoch;
            for entry in inner.mounted.values() {
                if let Some(id) = entry.binding() {
                    let area = if entry.render_epoch() == frame_epoch {
                        entry.last_area()
                    } else {
                        None
                    };
                    areas.push((id, area));
                }
            }
            inner.frame_epoch = inner.frame_epoch.saturating_add(1);
        }

        let context = bus.context_mut();
        for (id, area) in areas {
            if let Some(area) = area {
                context.set_component_area(id, area);
            } else {
                context.remove_component_area(id);
            }
        }
    }
}

impl<S, A, Id, Ctx> ComponentHostInner<S, A, Id, Ctx>
where
    Id: ComponentId,
    Ctx: BindingContext,
{
    fn ensure_type<C>(&self, raw: u32) -> Result<(), HostLifecycleError<Id>>
    where
        C: 'static,
    {
        let Some(entry) = self.mounted.get(&raw) else {
            return Err(HostLifecycleError::NotMounted(raw));
        };

        if entry.type_id() == TypeId::of::<C>() {
            Ok(())
        } else {
            Err(HostLifecycleError::TypeMismatch {
                expected: std::any::type_name::<C>(),
                actual: entry.type_name(),
            })
        }
    }

    fn expect_type<C>(&self, raw: u32)
    where
        C: 'static,
    {
        if let Err(err) = self.ensure_type::<C>(raw) {
            panic!("invalid mounted component handle: {err:?}");
        }
    }
}

#[cfg(test)]
mod tests {
    use std::cell::Cell;
    use std::rc::Rc;

    use crossterm::event::{
        KeyCode, KeyEvent, KeyEventKind, KeyEventState, MouseButton, MouseEventKind,
    };
    use tui_dispatch_core::testing::{key, RenderHarness};
    use tui_dispatch_core::{
        Action, BindingContext, EventRoutingState, EventType, Keybindings, RouteTarget,
    };

    use super::*;

    #[derive(Clone, Debug, PartialEq, Eq)]
    enum TestAction {
        Emit(String),
    }

    impl Action for TestAction {
        fn name(&self) -> &'static str {
            "emit"
        }
    }

    #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
    enum TestId {
        A,
    }

    impl ComponentId for TestId {
        fn name(&self) -> &'static str {
            match self {
                Self::A => "a",
            }
        }
    }

    #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
    enum TestCtx {
        #[default]
        Default,
        Nav,
    }

    impl BindingContext for TestCtx {
        fn name(&self) -> &'static str {
            match self {
                Self::Default => "default",
                Self::Nav => "nav",
            }
        }

        fn from_name(name: &str) -> Option<Self> {
            match name {
                "default" => Some(Self::Default),
                "nav" => Some(Self::Nav),
                _ => None,
            }
        }

        fn all() -> &'static [Self] {
            &[Self::Default, Self::Nav]
        }
    }

    struct TestState {
        focused: Option<TestId>,
        context: TestCtx,
        label: String,
    }

    fn label_props(state: &TestState) -> &str {
        state.label.as_str()
    }

    impl EventRoutingState<TestId, TestCtx> for TestState {
        fn focused(&self) -> Option<TestId> {
            self.focused
        }

        fn modal(&self) -> Option<TestId> {
            None
        }

        fn binding_context(&self, _id: TestId) -> TestCtx {
            self.context
        }

        fn default_context(&self) -> TestCtx {
            TestCtx::Default
        }
    }

    struct EchoComponent {
        resets: Rc<Cell<usize>>,
    }

    impl ComponentDebugState for EchoComponent {
        fn debug_state(&self) -> Vec<ComponentDebugEntry> {
            vec![ComponentDebugEntry::new(
                "resets",
                self.resets.get().to_string(),
            )]
        }
    }

    impl InteractiveComponent<TestAction, TestCtx> for EchoComponent {
        type Props<'a> = &'a str;

        fn update(
            &mut self,
            input: ComponentInput<'_, TestCtx>,
            props: Self::Props<'_>,
        ) -> HandlerResponse<TestAction> {
            match input {
                ComponentInput::Command { name, .. } => {
                    HandlerResponse::action(TestAction::Emit(name.to_string()))
                }
                ComponentInput::Key(key) if key.code == KeyCode::Char('x') => {
                    HandlerResponse::action(TestAction::Emit("raw".into()))
                }
                ComponentInput::Mouse(mouse)
                    if matches!(mouse.kind, MouseEventKind::Down(MouseButton::Left)) =>
                {
                    HandlerResponse::action(TestAction::Emit("mouse".into())).with_render()
                }
                ComponentInput::Scroll { .. } => {
                    HandlerResponse::action(TestAction::Emit(props.to_string())).with_render()
                }
                ComponentInput::Resize(..) => HandlerResponse::ignored().with_render(),
                ComponentInput::Tick => HandlerResponse::action(TestAction::Emit("tick".into())),
                _ => HandlerResponse::ignored(),
            }
        }

        fn render(&mut self, frame: &mut Frame, area: Rect, props: Self::Props<'_>) {
            use ratatui::widgets::Paragraph;
            frame.render_widget(Paragraph::new(props.to_string()), area);
        }
    }

    struct TickComponent;

    impl ComponentDebugState for TickComponent {}

    impl InteractiveComponent<TestAction, TestCtx> for TickComponent {
        type Props<'a> = &'a str;

        fn subscriptions() -> &'static [EventType] {
            &[EventType::Tick]
        }

        fn update(
            &mut self,
            input: ComponentInput<'_, TestCtx>,
            _props: Self::Props<'_>,
        ) -> HandlerResponse<TestAction> {
            match input {
                ComponentInput::Tick => {
                    HandlerResponse::action(TestAction::Emit("tick-subscription".into()))
                }
                _ => HandlerResponse::ignored(),
            }
        }

        fn render(&mut self, frame: &mut Frame, area: Rect, _props: Self::Props<'_>) {
            use ratatui::widgets::Paragraph;
            frame.render_widget(Paragraph::new("tick"), area);
        }
    }

    fn key_event(code: KeyCode) -> KeyEvent {
        KeyEvent {
            code,
            modifiers: KeyModifiers::NONE,
            kind: KeyEventKind::Press,
            state: KeyEventState::empty(),
        }
    }

    #[test]
    fn mounted_host_reuses_props_and_resets_local_state() {
        let host = ComponentHost::<TestState, TestAction, TestId, TestCtx>::new();
        let resets = Rc::new(Cell::new(0));
        let mounted: Mounted<EchoComponent> = host.mount::<EchoComponent, _>(
            {
                let resets = Rc::clone(&resets);
                move || {
                    resets.set(resets.get() + 1);
                    EchoComponent {
                        resets: Rc::clone(&resets),
                    }
                }
            },
            label_props,
        );

        let mut harness = RenderHarness::new(12, 1);
        let state = TestState {
            focused: Some(TestId::A),
            context: TestCtx::Default,
            label: "hello".into(),
        };
        let output = harness
            .render_to_string_plain(|frame| host.render(mounted, frame, frame.area(), &state));
        assert!(output.contains("hello"));

        let response = host.update(
            mounted,
            ComponentInput::Scroll {
                column: 0,
                row: 0,
                delta: 1,
                modifiers: KeyModifiers::NONE,
            },
            &state,
        );
        assert_eq!(response.actions, vec![TestAction::Emit("hello".into())]);
        assert!(response.needs_render);

        host.reset_local_state();
        let info = host.mounted_components();
        assert_eq!(
            info[0].debug_state[0],
            ComponentDebugEntry::new("resets", "2")
        );
    }

    #[test]
    fn bind_routes_commands_and_syncs_areas() {
        let host = ComponentHost::<TestState, TestAction, TestId, TestCtx>::new();
        let mounted: Mounted<EchoComponent> = host.mount::<EchoComponent, _>(
            || EchoComponent {
                resets: Rc::new(Cell::new(1)),
            },
            label_props,
        );

        let mut bus = EventBus::<TestState, TestAction, TestId, TestCtx>::new();
        host.bind(&mut bus, TestId::A, mounted);

        let mut bindings = Keybindings::new();
        bindings.add(TestCtx::Nav, "next", vec!["j".into()]);
        let state = TestState {
            focused: Some(TestId::A),
            context: TestCtx::Nav,
            label: "bound".into(),
        };

        let outcome = bus.handle_event(&EventKind::Key(key("j")), &state, &bindings);
        assert_eq!(outcome.actions, vec![TestAction::Emit("next".into())]);

        let tick = bus.handle_event(&EventKind::Tick, &state, &bindings);
        assert_eq!(tick.actions, vec![TestAction::Emit("tick".into())]);

        let mut render = RenderHarness::new(10, 1);
        render.render(|frame| host.render(mounted, frame, frame.area(), &state));
        host.sync_areas(&mut bus);
        assert_eq!(
            bus.context().component_areas.get(&TestId::A).copied(),
            Some(Rect::new(0, 0, 10, 1))
        );

        host.sync_areas(&mut bus);
        assert_eq!(bus.context().component_areas.get(&TestId::A).copied(), None);
    }

    #[test]
    fn bind_subscribes_default_key_events() {
        let host = ComponentHost::<TestState, TestAction, TestId, TestCtx>::new();
        let mounted: Mounted<EchoComponent> = host.mount::<EchoComponent, _>(
            || EchoComponent {
                resets: Rc::new(Cell::new(1)),
            },
            label_props,
        );
        let mut bus = EventBus::<TestState, TestAction, TestId, TestCtx>::new();

        host.bind(&mut bus, TestId::A, mounted);

        assert_eq!(bus.get_subscribers(EventType::Key), vec![TestId::A]);

        let mut bindings = Keybindings::new();
        bindings.add(TestCtx::Nav, "next", vec!["j".into()]);
        let state = TestState {
            focused: None,
            context: TestCtx::Nav,
            label: "bound".into(),
        };

        let outcome = bus.handle_event(&EventKind::Key(key("j")), &state, &bindings);
        assert_eq!(outcome.actions, vec![TestAction::Emit("next".into())]);
    }

    #[test]
    fn bind_subscribes_declared_event_types() {
        let host = ComponentHost::<TestState, TestAction, TestId, TestCtx>::new();
        let mounted: Mounted<TickComponent> =
            host.mount::<TickComponent, _>(|| TickComponent, label_props);
        let mut bus = EventBus::<TestState, TestAction, TestId, TestCtx>::new();

        host.bind(&mut bus, TestId::A, mounted);

        assert!(bus.get_subscribers(EventType::Key).is_empty());
        assert_eq!(bus.get_subscribers(EventType::Tick), vec![TestId::A]);

        let state = TestState {
            focused: None,
            context: TestCtx::Default,
            label: "bound".into(),
        };
        let outcome = bus.handle_event(&EventKind::Tick, &state, &Keybindings::new());
        assert_eq!(
            outcome.actions,
            vec![TestAction::Emit("tick-subscription".into())]
        );
    }

    #[test]
    fn unmount_requires_unbinding_first() {
        let host = ComponentHost::<TestState, TestAction, TestId, TestCtx>::new();
        let mounted: Mounted<EchoComponent> = host.mount::<EchoComponent, _>(
            || EchoComponent {
                resets: Rc::new(Cell::new(1)),
            },
            label_props,
        );
        let mut bus = EventBus::<TestState, TestAction, TestId, TestCtx>::new();

        host.bind(&mut bus, TestId::A, mounted);
        assert_eq!(
            host.unmount(mounted),
            Err(HostLifecycleError::StillBound(TestId::A))
        );

        host.unbind(&mut bus, TestId::A);
        assert_eq!(host.unmount(mounted), Ok(()));
    }

    #[test]
    fn routed_event_prefers_command_over_raw_key() {
        let routed = RoutedEvent {
            kind: EventKind::Key(key_event(KeyCode::Char('j'))),
            command: Some("next"),
            binding_ctx: TestCtx::Nav,
            target: RouteTarget::Focused(TestId::A),
            context: &Default::default(),
        };

        match ComponentInput::from(routed) {
            ComponentInput::Command { name, ctx } => {
                assert_eq!(name, "next");
                assert_eq!(ctx, TestCtx::Nav);
            }
            other => panic!("unexpected routed input: {other:?}"),
        }
    }
}