presentar-core 0.3.4

Core types and traits for Presentar UI framework
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
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
#![allow(clippy::unwrap_used, clippy::disallowed_methods)]
//! Keyboard shortcut management system.
//!
//! This module provides:
//! - Keyboard shortcut registration and handling
//! - Modifier key support (Ctrl, Alt, Shift, Meta)
//! - Context-aware shortcuts (global, focused widget, etc.)
//! - Shortcut conflict detection

use crate::event::Key;
use crate::widget::WidgetId;
use std::collections::HashMap;

/// Modifier keys for keyboard shortcuts.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct Modifiers {
    /// Control key (Cmd on Mac).
    pub ctrl: bool,
    /// Alt key (Option on Mac).
    pub alt: bool,
    /// Shift key.
    pub shift: bool,
    /// Meta key (Windows key, Cmd on Mac).
    pub meta: bool,
}

impl Modifiers {
    /// No modifiers.
    pub const NONE: Self = Self {
        ctrl: false,
        alt: false,
        shift: false,
        meta: false,
    };

    /// Ctrl only.
    pub const CTRL: Self = Self {
        ctrl: true,
        alt: false,
        shift: false,
        meta: false,
    };

    /// Alt only.
    pub const ALT: Self = Self {
        ctrl: false,
        alt: true,
        shift: false,
        meta: false,
    };

    /// Shift only.
    pub const SHIFT: Self = Self {
        ctrl: false,
        alt: false,
        shift: true,
        meta: false,
    };

    /// Meta only.
    pub const META: Self = Self {
        ctrl: false,
        alt: false,
        shift: false,
        meta: true,
    };

    /// Ctrl+Shift.
    pub const CTRL_SHIFT: Self = Self {
        ctrl: true,
        alt: false,
        shift: true,
        meta: false,
    };

    /// Ctrl+Alt.
    pub const CTRL_ALT: Self = Self {
        ctrl: true,
        alt: true,
        shift: false,
        meta: false,
    };

    /// Create custom modifiers.
    pub const fn new(ctrl: bool, alt: bool, shift: bool, meta: bool) -> Self {
        Self {
            ctrl,
            alt,
            shift,
            meta,
        }
    }

    /// Check if any modifier is pressed.
    pub const fn any(&self) -> bool {
        self.ctrl || self.alt || self.shift || self.meta
    }

    /// Check if no modifier is pressed.
    pub const fn none(&self) -> bool {
        !self.any()
    }

    /// Get a display string for the modifiers.
    pub fn display(&self) -> String {
        let mut parts = Vec::new();
        if self.ctrl {
            parts.push("Ctrl");
        }
        if self.alt {
            parts.push("Alt");
        }
        if self.shift {
            parts.push("Shift");
        }
        if self.meta {
            parts.push("Meta");
        }
        parts.join("+")
    }
}

/// A keyboard shortcut (key + modifiers).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Shortcut {
    /// The key.
    pub key: Key,
    /// Modifier keys.
    pub modifiers: Modifiers,
}

impl Shortcut {
    /// Create a new shortcut.
    pub const fn new(key: Key, modifiers: Modifiers) -> Self {
        Self { key, modifiers }
    }

    /// Create a shortcut with no modifiers.
    pub const fn key(key: Key) -> Self {
        Self::new(key, Modifiers::NONE)
    }

    /// Create a shortcut with Ctrl modifier.
    pub const fn ctrl(key: Key) -> Self {
        Self::new(key, Modifiers::CTRL)
    }

    /// Create a shortcut with Alt modifier.
    pub const fn alt(key: Key) -> Self {
        Self::new(key, Modifiers::ALT)
    }

    /// Create a shortcut with Shift modifier.
    pub const fn shift(key: Key) -> Self {
        Self::new(key, Modifiers::SHIFT)
    }

    /// Create a shortcut with Ctrl+Shift modifiers.
    pub const fn ctrl_shift(key: Key) -> Self {
        Self::new(key, Modifiers::CTRL_SHIFT)
    }

    /// Get a display string for the shortcut.
    pub fn display(&self) -> String {
        let key_name = format!("{:?}", self.key);
        if self.modifiers.none() {
            key_name
        } else {
            format!("{}+{}", self.modifiers.display(), key_name)
        }
    }

    /// Common shortcuts
    pub const COPY: Self = Self::ctrl(Key::C);
    pub const CUT: Self = Self::ctrl(Key::X);
    pub const PASTE: Self = Self::ctrl(Key::V);
    pub const UNDO: Self = Self::ctrl(Key::Z);
    pub const REDO: Self = Self::ctrl_shift(Key::Z);
    pub const SAVE: Self = Self::ctrl(Key::S);
    pub const SELECT_ALL: Self = Self::ctrl(Key::A);
    pub const FIND: Self = Self::ctrl(Key::F);
    pub const ESCAPE: Self = Self::key(Key::Escape);
    pub const ENTER: Self = Self::key(Key::Enter);
    pub const TAB: Self = Self::key(Key::Tab);
    pub const DELETE: Self = Self::key(Key::Delete);
    pub const BACKSPACE: Self = Self::key(Key::Backspace);
}

/// Unique ID for a shortcut binding.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ShortcutId(pub u64);

impl ShortcutId {
    /// Create a new shortcut ID.
    pub const fn new(id: u64) -> Self {
        Self(id)
    }
}

/// Context in which a shortcut is active.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)]
pub enum ShortcutContext {
    /// Global - active everywhere.
    #[default]
    Global,
    /// Only when a specific widget has focus.
    Widget(WidgetId),
    /// Only when a widget type has focus.
    WidgetType(String),
    /// Custom context identified by name.
    Custom(String),
}

/// Priority for shortcut resolution when multiple match.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub enum ShortcutPriority {
    /// Low priority - checked last.
    Low = 0,
    /// Normal priority.
    #[default]
    Normal = 1,
    /// High priority - checked first.
    High = 2,
}

/// Callback for shortcut handling.
pub type ShortcutHandler = Box<dyn FnMut() -> bool + Send>;

/// Registration for a shortcut binding.
struct ShortcutBinding {
    #[allow(dead_code)]
    id: ShortcutId,
    shortcut: Shortcut,
    context: ShortcutContext,
    priority: ShortcutPriority,
    description: String,
    enabled: bool,
}

/// Manager for keyboard shortcuts.
pub struct ShortcutManager {
    /// Next binding ID.
    next_id: u64,
    /// Registered bindings.
    bindings: HashMap<ShortcutId, ShortcutBinding>,
    /// Handlers (separate for mutability).
    handlers: HashMap<ShortcutId, ShortcutHandler>,
    /// Index by shortcut for fast lookup.
    by_shortcut: HashMap<Shortcut, Vec<ShortcutId>>,
    /// Current active contexts.
    active_contexts: Vec<ShortcutContext>,
    /// Current modifier state.
    modifiers: Modifiers,
}

impl ShortcutManager {
    /// Create a new shortcut manager.
    pub fn new() -> Self {
        Self {
            next_id: 0,
            bindings: HashMap::new(),
            handlers: HashMap::new(),
            by_shortcut: HashMap::new(),
            active_contexts: vec![ShortcutContext::Global],
            modifiers: Modifiers::NONE,
        }
    }

    /// Register a shortcut.
    pub fn register(&mut self, shortcut: Shortcut, handler: ShortcutHandler) -> ShortcutId {
        self.register_with_options(
            shortcut,
            handler,
            ShortcutContext::Global,
            ShortcutPriority::Normal,
            "",
        )
    }

    /// Register a shortcut with full options.
    pub fn register_with_options(
        &mut self,
        shortcut: Shortcut,
        handler: ShortcutHandler,
        context: ShortcutContext,
        priority: ShortcutPriority,
        description: &str,
    ) -> ShortcutId {
        let id = ShortcutId::new(self.next_id);
        self.next_id += 1;

        let binding = ShortcutBinding {
            id,
            shortcut,
            context,
            priority,
            description: description.to_string(),
            enabled: true,
        };

        self.bindings.insert(id, binding);
        self.handlers.insert(id, handler);

        self.by_shortcut.entry(shortcut).or_default().push(id);

        id
    }

    /// Unregister a shortcut.
    pub fn unregister(&mut self, id: ShortcutId) -> bool {
        if let Some(binding) = self.bindings.remove(&id) {
            self.handlers.remove(&id);

            if let Some(ids) = self.by_shortcut.get_mut(&binding.shortcut) {
                ids.retain(|&i| i != id);
            }

            true
        } else {
            false
        }
    }

    /// Enable or disable a shortcut.
    pub fn set_enabled(&mut self, id: ShortcutId, enabled: bool) {
        if let Some(binding) = self.bindings.get_mut(&id) {
            binding.enabled = enabled;
        }
    }

    /// Check if a shortcut is enabled.
    pub fn is_enabled(&self, id: ShortcutId) -> bool {
        self.bindings.get(&id).is_some_and(|b| b.enabled)
    }

    /// Set the current modifier state.
    pub fn set_modifiers(&mut self, modifiers: Modifiers) {
        self.modifiers = modifiers;
    }

    /// Get the current modifier state.
    pub fn modifiers(&self) -> Modifiers {
        self.modifiers
    }

    /// Push an active context.
    pub fn push_context(&mut self, context: ShortcutContext) {
        self.active_contexts.push(context);
    }

    /// Pop the most recent context.
    pub fn pop_context(&mut self) -> Option<ShortcutContext> {
        if self.active_contexts.len() > 1 {
            self.active_contexts.pop()
        } else {
            None
        }
    }

    /// Set the focused widget context.
    pub fn set_focused_widget(&mut self, widget_id: Option<WidgetId>) {
        // Remove any existing widget context
        self.active_contexts
            .retain(|c| !matches!(c, ShortcutContext::Widget(_)));

        if let Some(id) = widget_id {
            self.active_contexts.push(ShortcutContext::Widget(id));
        }
    }

    /// Handle a key press and trigger matching shortcuts.
    /// Returns true if a shortcut was triggered.
    pub fn handle_key(&mut self, key: Key) -> bool {
        let shortcut = Shortcut::new(key, self.modifiers);
        self.trigger(shortcut)
    }

    /// Trigger a shortcut directly.
    pub fn trigger(&mut self, shortcut: Shortcut) -> bool {
        let binding_ids = match self.by_shortcut.get(&shortcut) {
            Some(ids) => ids.clone(),
            None => return false,
        };

        // Collect matching bindings with their priorities
        let mut matches: Vec<(ShortcutId, ShortcutPriority)> = binding_ids
            .iter()
            .filter_map(|&id| {
                let binding = self.bindings.get(&id)?;
                if !binding.enabled {
                    return None;
                }
                if self.is_context_active(&binding.context) {
                    Some((id, binding.priority))
                } else {
                    None
                }
            })
            .collect();

        // Sort by priority (highest first)
        matches.sort_by(|a, b| b.1.cmp(&a.1));

        // Try handlers in priority order
        for (id, _) in matches {
            if let Some(handler) = self.handlers.get_mut(&id) {
                if handler() {
                    return true;
                }
            }
        }

        false
    }

    /// Check if a context is currently active.
    fn is_context_active(&self, context: &ShortcutContext) -> bool {
        match context {
            ShortcutContext::Global => true,
            other => self.active_contexts.contains(other),
        }
    }

    /// Get all registered shortcuts.
    pub fn shortcuts(&self) -> impl Iterator<Item = (&Shortcut, &str)> {
        self.bindings
            .values()
            .map(|b| (&b.shortcut, b.description.as_str()))
    }

    /// Get binding count.
    pub fn binding_count(&self) -> usize {
        self.bindings.len()
    }

    /// Check for shortcut conflicts (same shortcut, same context).
    pub fn find_conflicts(&self) -> Vec<(Shortcut, Vec<ShortcutId>)> {
        let mut conflicts = Vec::new();

        for (shortcut, ids) in &self.by_shortcut {
            if ids.len() < 2 {
                continue;
            }

            // Group by context
            let mut by_context: HashMap<&ShortcutContext, Vec<ShortcutId>> = HashMap::new();
            for &id in ids {
                if let Some(binding) = self.bindings.get(&id) {
                    by_context.entry(&binding.context).or_default().push(id);
                }
            }

            // Find contexts with multiple bindings
            for (_, context_ids) in by_context {
                if context_ids.len() > 1 {
                    conflicts.push((*shortcut, context_ids));
                }
            }
        }

        conflicts
    }

    /// Clear all shortcuts.
    pub fn clear(&mut self) {
        self.bindings.clear();
        self.handlers.clear();
        self.by_shortcut.clear();
    }

    /// Get the description for a shortcut binding.
    pub fn description(&self, id: ShortcutId) -> Option<&str> {
        self.bindings.get(&id).map(|b| b.description.as_str())
    }
}

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

impl std::fmt::Debug for ShortcutManager {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ShortcutManager")
            .field("binding_count", &self.bindings.len())
            .field("active_contexts", &self.active_contexts)
            .field("modifiers", &self.modifiers)
            .finish()
    }
}

/// Builder for creating shortcuts with fluent API.
#[derive(Debug, Clone)]
pub struct ShortcutBuilder {
    key: Key,
    modifiers: Modifiers,
    context: ShortcutContext,
    priority: ShortcutPriority,
    description: String,
}

impl ShortcutBuilder {
    /// Create a new builder.
    pub fn new(key: Key) -> Self {
        Self {
            key,
            modifiers: Modifiers::NONE,
            context: ShortcutContext::Global,
            priority: ShortcutPriority::Normal,
            description: String::new(),
        }
    }

    /// Add Ctrl modifier.
    pub fn ctrl(mut self) -> Self {
        self.modifiers.ctrl = true;
        self
    }

    /// Add Alt modifier.
    pub fn alt(mut self) -> Self {
        self.modifiers.alt = true;
        self
    }

    /// Add Shift modifier.
    pub fn shift(mut self) -> Self {
        self.modifiers.shift = true;
        self
    }

    /// Add Meta modifier.
    pub fn meta(mut self) -> Self {
        self.modifiers.meta = true;
        self
    }

    /// Set context.
    pub fn context(mut self, context: ShortcutContext) -> Self {
        self.context = context;
        self
    }

    /// Set context to a specific widget.
    pub fn for_widget(mut self, widget_id: WidgetId) -> Self {
        self.context = ShortcutContext::Widget(widget_id);
        self
    }

    /// Set priority.
    pub fn priority(mut self, priority: ShortcutPriority) -> Self {
        self.priority = priority;
        self
    }

    /// Set description.
    pub fn description(mut self, desc: &str) -> Self {
        self.description = desc.to_string();
        self
    }

    /// Build the shortcut.
    pub fn build(self) -> Shortcut {
        Shortcut::new(self.key, self.modifiers)
    }

    /// Register with a manager.
    pub fn register(self, manager: &mut ShortcutManager, handler: ShortcutHandler) -> ShortcutId {
        let shortcut = Shortcut::new(self.key, self.modifiers);
        manager.register_with_options(
            shortcut,
            handler,
            self.context,
            self.priority,
            &self.description,
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
    use std::sync::Arc;

    // Modifiers tests
    #[test]
    fn test_modifiers_constants() {
        assert!(!Modifiers::NONE.any());
        assert!(Modifiers::NONE.none());

        assert!(Modifiers::CTRL.ctrl);
        assert!(!Modifiers::CTRL.alt);

        assert!(Modifiers::CTRL_SHIFT.ctrl);
        assert!(Modifiers::CTRL_SHIFT.shift);
    }

    #[test]
    fn test_modifiers_new() {
        let mods = Modifiers::new(true, true, false, false);
        assert!(mods.ctrl);
        assert!(mods.alt);
        assert!(!mods.shift);
        assert!(!mods.meta);
    }

    #[test]
    fn test_modifiers_display() {
        assert_eq!(Modifiers::NONE.display(), "");
        assert_eq!(Modifiers::CTRL.display(), "Ctrl");
        assert_eq!(Modifiers::CTRL_SHIFT.display(), "Ctrl+Shift");
    }

    // Shortcut tests
    #[test]
    fn test_shortcut_new() {
        let shortcut = Shortcut::new(Key::A, Modifiers::CTRL);
        assert_eq!(shortcut.key, Key::A);
        assert!(shortcut.modifiers.ctrl);
    }

    #[test]
    fn test_shortcut_constructors() {
        let key_only = Shortcut::key(Key::Escape);
        assert!(key_only.modifiers.none());

        let ctrl = Shortcut::ctrl(Key::S);
        assert!(ctrl.modifiers.ctrl);

        let alt = Shortcut::alt(Key::F4);
        assert!(alt.modifiers.alt);

        let shift = Shortcut::shift(Key::Tab);
        assert!(shift.modifiers.shift);

        let ctrl_shift = Shortcut::ctrl_shift(Key::Z);
        assert!(ctrl_shift.modifiers.ctrl);
        assert!(ctrl_shift.modifiers.shift);
    }

    #[test]
    fn test_shortcut_display() {
        assert_eq!(Shortcut::key(Key::A).display(), "A");
        assert_eq!(Shortcut::ctrl(Key::S).display(), "Ctrl+S");
        assert_eq!(Shortcut::ctrl_shift(Key::Z).display(), "Ctrl+Shift+Z");
    }

    #[test]
    fn test_shortcut_constants() {
        assert_eq!(Shortcut::COPY, Shortcut::ctrl(Key::C));
        assert_eq!(Shortcut::UNDO, Shortcut::ctrl(Key::Z));
        assert_eq!(Shortcut::REDO, Shortcut::ctrl_shift(Key::Z));
    }

    #[test]
    fn test_shortcut_equality() {
        let s1 = Shortcut::ctrl(Key::S);
        let s2 = Shortcut::ctrl(Key::S);
        let s3 = Shortcut::ctrl(Key::A);

        assert_eq!(s1, s2);
        assert_ne!(s1, s3);
    }

    // ShortcutId tests
    #[test]
    fn test_shortcut_id() {
        let id1 = ShortcutId::new(1);
        let id2 = ShortcutId::new(1);
        let id3 = ShortcutId::new(2);

        assert_eq!(id1, id2);
        assert_ne!(id1, id3);
    }

    // ShortcutContext tests
    #[test]
    fn test_shortcut_context_default() {
        assert_eq!(ShortcutContext::default(), ShortcutContext::Global);
    }

    // ShortcutPriority tests
    #[test]
    fn test_shortcut_priority_ordering() {
        assert!(ShortcutPriority::High > ShortcutPriority::Normal);
        assert!(ShortcutPriority::Normal > ShortcutPriority::Low);
    }

    // ShortcutManager tests
    #[test]
    fn test_manager_new() {
        let manager = ShortcutManager::new();
        assert_eq!(manager.binding_count(), 0);
    }

    #[test]
    fn test_manager_register() {
        let mut manager = ShortcutManager::new();

        let id = manager.register(Shortcut::ctrl(Key::S), Box::new(|| true));
        assert_eq!(manager.binding_count(), 1);
        assert!(manager.is_enabled(id));
    }

    #[test]
    fn test_manager_unregister() {
        let mut manager = ShortcutManager::new();

        let id = manager.register(Shortcut::ctrl(Key::S), Box::new(|| true));
        assert_eq!(manager.binding_count(), 1);

        let removed = manager.unregister(id);
        assert!(removed);
        assert_eq!(manager.binding_count(), 0);
    }

    #[test]
    fn test_manager_set_enabled() {
        let mut manager = ShortcutManager::new();

        let id = manager.register(Shortcut::ctrl(Key::S), Box::new(|| true));
        assert!(manager.is_enabled(id));

        manager.set_enabled(id, false);
        assert!(!manager.is_enabled(id));

        manager.set_enabled(id, true);
        assert!(manager.is_enabled(id));
    }

    #[test]
    fn test_manager_handle_key() {
        let triggered = Arc::new(AtomicBool::new(false));
        let triggered_clone = triggered.clone();

        let mut manager = ShortcutManager::new();
        manager.register(
            Shortcut::ctrl(Key::S),
            Box::new(move || {
                triggered_clone.store(true, Ordering::SeqCst);
                true
            }),
        );

        // Without Ctrl, should not trigger
        manager.set_modifiers(Modifiers::NONE);
        let result = manager.handle_key(Key::S);
        assert!(!result);
        assert!(!triggered.load(Ordering::SeqCst));

        // With Ctrl, should trigger
        manager.set_modifiers(Modifiers::CTRL);
        let result = manager.handle_key(Key::S);
        assert!(result);
        assert!(triggered.load(Ordering::SeqCst));
    }

    #[test]
    fn test_manager_trigger() {
        let counter = Arc::new(AtomicUsize::new(0));
        let counter_clone = counter.clone();

        let mut manager = ShortcutManager::new();
        manager.register(
            Shortcut::ctrl(Key::C),
            Box::new(move || {
                counter_clone.fetch_add(1, Ordering::SeqCst);
                true
            }),
        );

        manager.trigger(Shortcut::ctrl(Key::C));
        assert_eq!(counter.load(Ordering::SeqCst), 1);

        manager.trigger(Shortcut::ctrl(Key::C));
        assert_eq!(counter.load(Ordering::SeqCst), 2);
    }

    #[test]
    fn test_manager_disabled_shortcut_not_triggered() {
        let triggered = Arc::new(AtomicBool::new(false));
        let triggered_clone = triggered.clone();

        let mut manager = ShortcutManager::new();
        let id = manager.register(
            Shortcut::ctrl(Key::S),
            Box::new(move || {
                triggered_clone.store(true, Ordering::SeqCst);
                true
            }),
        );

        manager.set_enabled(id, false);
        manager.set_modifiers(Modifiers::CTRL);

        let result = manager.handle_key(Key::S);
        assert!(!result);
        assert!(!triggered.load(Ordering::SeqCst));
    }

    #[test]
    fn test_manager_context() {
        let triggered = Arc::new(AtomicBool::new(false));
        let triggered_clone = triggered.clone();

        let mut manager = ShortcutManager::new();
        manager.register_with_options(
            Shortcut::ctrl(Key::S),
            Box::new(move || {
                triggered_clone.store(true, Ordering::SeqCst);
                true
            }),
            ShortcutContext::Widget(WidgetId::new(1)),
            ShortcutPriority::Normal,
            "",
        );

        // Without widget context, should not trigger
        manager.set_modifiers(Modifiers::CTRL);
        let result = manager.handle_key(Key::S);
        assert!(!result);

        // With widget context, should trigger
        manager.set_focused_widget(Some(WidgetId::new(1)));
        let result = manager.handle_key(Key::S);
        assert!(result);
        assert!(triggered.load(Ordering::SeqCst));
    }

    #[test]
    fn test_manager_priority() {
        let order = Arc::new(std::sync::Mutex::new(Vec::new()));
        let order1 = order.clone();
        let order2 = order.clone();

        let mut manager = ShortcutManager::new();

        manager.register_with_options(
            Shortcut::ctrl(Key::S),
            Box::new(move || {
                order1.lock().unwrap().push("low");
                false // Don't consume, let next handler run
            }),
            ShortcutContext::Global,
            ShortcutPriority::Low,
            "",
        );

        manager.register_with_options(
            Shortcut::ctrl(Key::S),
            Box::new(move || {
                order2.lock().unwrap().push("high");
                false
            }),
            ShortcutContext::Global,
            ShortcutPriority::High,
            "",
        );

        manager.trigger(Shortcut::ctrl(Key::S));

        let order_vec = order.lock().unwrap();
        assert_eq!(*order_vec, vec!["high", "low"]);
    }

    #[test]
    fn test_manager_handler_consumes() {
        let counter = Arc::new(AtomicUsize::new(0));
        let c1 = counter.clone();
        let c2 = counter.clone();

        let mut manager = ShortcutManager::new();

        manager.register_with_options(
            Shortcut::ctrl(Key::S),
            Box::new(move || {
                c1.fetch_add(1, Ordering::SeqCst);
                true // Consume the event
            }),
            ShortcutContext::Global,
            ShortcutPriority::High,
            "",
        );

        manager.register_with_options(
            Shortcut::ctrl(Key::S),
            Box::new(move || {
                c2.fetch_add(1, Ordering::SeqCst);
                true
            }),
            ShortcutContext::Global,
            ShortcutPriority::Low,
            "",
        );

        manager.trigger(Shortcut::ctrl(Key::S));

        // Only high priority handler should have run
        assert_eq!(counter.load(Ordering::SeqCst), 1);
    }

    #[test]
    fn test_manager_push_pop_context() {
        let mut manager = ShortcutManager::new();

        manager.push_context(ShortcutContext::Custom("editor".to_string()));
        manager.push_context(ShortcutContext::Custom("modal".to_string()));

        let popped = manager.pop_context();
        assert_eq!(popped, Some(ShortcutContext::Custom("modal".to_string())));

        let popped = manager.pop_context();
        assert_eq!(popped, Some(ShortcutContext::Custom("editor".to_string())));

        // Can't pop Global context
        let popped = manager.pop_context();
        assert!(popped.is_none());
    }

    #[test]
    fn test_manager_find_conflicts() {
        let mut manager = ShortcutManager::new();

        let id1 = manager.register(Shortcut::ctrl(Key::S), Box::new(|| true));
        let id2 = manager.register(Shortcut::ctrl(Key::S), Box::new(|| true));
        manager.register(Shortcut::ctrl(Key::A), Box::new(|| true)); // No conflict

        let conflicts = manager.find_conflicts();
        assert_eq!(conflicts.len(), 1);
        assert!(conflicts[0].1.contains(&id1));
        assert!(conflicts[0].1.contains(&id2));
    }

    #[test]
    fn test_manager_shortcuts() {
        let mut manager = ShortcutManager::new();

        manager.register_with_options(
            Shortcut::ctrl(Key::S),
            Box::new(|| true),
            ShortcutContext::Global,
            ShortcutPriority::Normal,
            "Save",
        );

        manager.register_with_options(
            Shortcut::ctrl(Key::O),
            Box::new(|| true),
            ShortcutContext::Global,
            ShortcutPriority::Normal,
            "Open",
        );

        let shortcuts: Vec<_> = manager.shortcuts().collect();
        assert_eq!(shortcuts.len(), 2);
    }

    #[test]
    fn test_manager_clear() {
        let mut manager = ShortcutManager::new();
        manager.register(Shortcut::ctrl(Key::S), Box::new(|| true));
        manager.register(Shortcut::ctrl(Key::O), Box::new(|| true));

        manager.clear();
        assert_eq!(manager.binding_count(), 0);
    }

    #[test]
    fn test_manager_description() {
        let mut manager = ShortcutManager::new();

        let id = manager.register_with_options(
            Shortcut::ctrl(Key::S),
            Box::new(|| true),
            ShortcutContext::Global,
            ShortcutPriority::Normal,
            "Save document",
        );

        assert_eq!(manager.description(id), Some("Save document"));
    }

    // ShortcutBuilder tests
    #[test]
    fn test_builder() {
        let shortcut = ShortcutBuilder::new(Key::S).ctrl().shift().build();

        assert_eq!(shortcut.key, Key::S);
        assert!(shortcut.modifiers.ctrl);
        assert!(shortcut.modifiers.shift);
        assert!(!shortcut.modifiers.alt);
    }

    #[test]
    fn test_builder_register() {
        let mut manager = ShortcutManager::new();

        let id = ShortcutBuilder::new(Key::S)
            .ctrl()
            .description("Save")
            .register(&mut manager, Box::new(|| true));

        assert!(manager.is_enabled(id));
        assert_eq!(manager.description(id), Some("Save"));
    }

    #[test]
    fn test_builder_for_widget() {
        let builder = ShortcutBuilder::new(Key::Enter).for_widget(WidgetId::new(42));

        assert_eq!(builder.context, ShortcutContext::Widget(WidgetId::new(42)));
    }
}