ftui-widgets 0.4.0

Widget library built on FrankenTUI render and layout.
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
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
#![forbid(unsafe_code)]

//! Keyboard-driven drag-and-drop support (bd-1csc.4).
//!
//! This module enables drag operations via keyboard for accessibility, complementing
//! the mouse-based drag protocol in [`crate::drag`].
//!
//! # Usage
//!
//! ```ignore
//! use ftui_widgets::keyboard_drag::{KeyboardDragManager, KeyboardDragConfig};
//! use ftui_widgets::drag::DragPayload;
//!
//! let config = KeyboardDragConfig::default();
//! let mut manager = KeyboardDragManager::new(config);
//!
//! // User picks up an item (Space/Enter on a draggable)
//! manager.start_drag(source_id, payload);
//!
//! // User navigates targets (Arrow keys)
//! manager.navigate_targets(Direction::Right, &available_targets);
//!
//! // User drops (Space/Enter) or cancels (Escape)
//! if let Some(result) = manager.complete_drag(drop_target) {
//!     // Handle drop result
//! }
//! ```
//!
//! # Invariants
//!
//! 1. A keyboard drag is either `Inactive`, `Holding`, or `Navigating`:
//!    - `Inactive`: No drag in progress
//!    - `Holding`: Item picked up, awaiting target navigation
//!    - `Navigating`: Actively navigating between drop targets
//! 2. `start_drag` can only be called in `Inactive` mode.
//! 3. `navigate_targets` can only be called in `Holding` or `Navigating` mode.
//! 4. `complete_drag` transitions to `Inactive` regardless of success/failure.
//! 5. `cancel_drag` always transitions to `Inactive`.
//!
//! # Failure Modes
//!
//! | Failure | Cause | Fallback |
//! |---------|-------|----------|
//! | No valid targets | All targets reject payload type | Stay in Holding mode |
//! | Focus loss | Window deactivated | Auto-cancel drag |
//! | Invalid source | Source widget destroyed | Cancel drag gracefully |
//!
//! # Accessibility
//!
//! The module supports screen reader announcements via [`Announcement`] queue:
//! - "Picked up: {item description}"
//! - "Drop target: {target name} ({position})"
//! - "Dropped on: {target name}" or "Drop cancelled"

use crate::drag::DragPayload;
use crate::measure_cache::WidgetId;
use ftui_core::geometry::Rect;
use ftui_render::cell::Cell;
use ftui_render::cell::PackedRgba;
use ftui_render::frame::Frame;
use ftui_style::Style;

// ---------------------------------------------------------------------------
// KeyboardDragMode
// ---------------------------------------------------------------------------

/// Current mode of a keyboard drag operation.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum KeyboardDragMode {
    /// No keyboard drag in progress.
    #[default]
    Inactive,
    /// Item picked up, awaiting target selection.
    Holding,
    /// Actively navigating between drop targets.
    Navigating,
}

impl KeyboardDragMode {
    /// Returns true if a drag is in progress.
    #[must_use]
    pub fn is_active(self) -> bool {
        !matches!(self, Self::Inactive)
    }

    /// Returns the stable string representation.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Inactive => "inactive",
            Self::Holding => "holding",
            Self::Navigating => "navigating",
        }
    }
}

// ---------------------------------------------------------------------------
// Direction
// ---------------------------------------------------------------------------

/// Navigation direction for keyboard drag target selection.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Direction {
    Up,
    Down,
    Left,
    Right,
}

impl Direction {
    /// Returns the opposite direction.
    #[must_use]
    pub const fn opposite(self) -> Self {
        match self {
            Self::Up => Self::Down,
            Self::Down => Self::Up,
            Self::Left => Self::Right,
            Self::Right => Self::Left,
        }
    }

    /// Returns true for vertical directions.
    #[must_use]
    pub const fn is_vertical(self) -> bool {
        matches!(self, Self::Up | Self::Down)
    }
}

// ---------------------------------------------------------------------------
// DropTargetInfo
// ---------------------------------------------------------------------------

/// Information about a potential drop target for keyboard navigation.
#[derive(Debug, Clone)]
pub struct DropTargetInfo {
    /// Unique identifier for the target widget.
    pub id: WidgetId,
    /// Human-readable name for accessibility.
    pub name: String,
    /// Bounding rectangle in screen coordinates.
    pub bounds: Rect,
    /// Accepted drag types (MIME-like patterns).
    pub accepted_types: Vec<String>,
    /// Whether this target is currently enabled.
    pub enabled: bool,
}

impl DropTargetInfo {
    /// Create a new drop target info.
    #[must_use]
    pub fn new(id: WidgetId, name: impl Into<String>, bounds: Rect) -> Self {
        Self {
            id,
            name: name.into(),
            bounds,
            accepted_types: Vec::new(),
            enabled: true,
        }
    }

    /// Add accepted drag types.
    #[must_use]
    pub fn with_accepted_types(mut self, types: Vec<String>) -> Self {
        self.accepted_types = types;
        self
    }

    /// Set enabled state.
    #[must_use]
    pub fn with_enabled(mut self, enabled: bool) -> Self {
        self.enabled = enabled;
        self
    }

    /// Check if this target can accept the given payload type.
    #[must_use]
    pub fn can_accept(&self, drag_type: &str) -> bool {
        if !self.enabled {
            return false;
        }
        if self.accepted_types.is_empty() {
            return true; // Accept any if no filter specified
        }
        self.accepted_types.iter().any(|pattern| {
            if pattern == "*" || pattern == "*/*" {
                true
            } else if let Some(prefix) = pattern.strip_suffix("/*") {
                drag_type.starts_with(prefix)
                    && drag_type.as_bytes().get(prefix.len()) == Some(&b'/')
            } else {
                pattern == drag_type
            }
        })
    }

    /// Returns the center point of this target's bounds.
    #[must_use]
    pub fn center(&self) -> (u16, u16) {
        (
            self.bounds.x + self.bounds.width / 2,
            self.bounds.y + self.bounds.height / 2,
        )
    }
}

// ---------------------------------------------------------------------------
// Announcement
// ---------------------------------------------------------------------------

/// Screen reader announcement for accessibility.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Announcement {
    /// The text to announce.
    pub text: String,
    /// Priority level (higher = more important).
    pub priority: AnnouncementPriority,
}

/// Priority level for announcements.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Default)]
pub enum AnnouncementPriority {
    /// Low priority, may be skipped if queue is full.
    Low,
    /// Normal priority.
    #[default]
    Normal,
    /// High priority, interrupts current announcement.
    High,
}

impl Announcement {
    /// Create a normal priority announcement.
    #[must_use]
    pub fn normal(text: impl Into<String>) -> Self {
        Self {
            text: text.into(),
            priority: AnnouncementPriority::Normal,
        }
    }

    /// Create a high priority announcement.
    #[must_use]
    pub fn high(text: impl Into<String>) -> Self {
        Self {
            text: text.into(),
            priority: AnnouncementPriority::High,
        }
    }
}

// ---------------------------------------------------------------------------
// KeyboardDragConfig
// ---------------------------------------------------------------------------

/// Configuration for keyboard drag behavior.
#[derive(Debug, Clone)]
pub struct KeyboardDragConfig {
    /// Keys that activate drag (pick up or drop).
    /// Default: Space, Enter
    pub activate_keys: Vec<ActivateKey>,

    /// Whether Escape cancels the drag.
    pub cancel_on_escape: bool,

    /// Style for highlighting the selected drop target.
    pub target_highlight_style: TargetHighlightStyle,

    /// Style for highlighting invalid drop targets.
    pub invalid_target_style: TargetHighlightStyle,

    /// Whether to wrap around when navigating past the last/first target.
    pub wrap_navigation: bool,

    /// Maximum announcements to queue.
    pub max_announcement_queue: usize,
}

/// Keys that can activate drag operations.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ActivateKey {
    Space,
    Enter,
}

impl Default for KeyboardDragConfig {
    fn default() -> Self {
        Self {
            activate_keys: vec![ActivateKey::Space, ActivateKey::Enter],
            cancel_on_escape: true,
            target_highlight_style: TargetHighlightStyle::default(),
            invalid_target_style: TargetHighlightStyle::invalid_default(),
            wrap_navigation: true,
            max_announcement_queue: 5,
        }
    }
}

// ---------------------------------------------------------------------------
// TargetHighlightStyle
// ---------------------------------------------------------------------------

/// Visual style for highlighting drop targets during keyboard drag.
#[derive(Debug, Clone)]
pub struct TargetHighlightStyle {
    /// Border style (character to use for highlighting).
    pub border_char: char,
    /// Foreground color for the highlight border.
    pub border_fg: PackedRgba,
    /// Background color for the target area.
    pub background: Option<PackedRgba>,
    /// Whether to render a pulsing animation.
    pub animate_pulse: bool,
}

impl Default for TargetHighlightStyle {
    fn default() -> Self {
        Self {
            border_char: 'â–ˆ',
            border_fg: PackedRgba::rgb(100, 180, 255), // Blue highlight
            background: Some(PackedRgba::rgba(100, 180, 255, 40)), // Subtle blue tint
            animate_pulse: true,
        }
    }
}

impl TargetHighlightStyle {
    /// Style for invalid drop targets.
    #[must_use]
    pub fn invalid_default() -> Self {
        Self {
            border_char: 'â–ª',
            border_fg: PackedRgba::rgb(180, 100, 100), // Red highlight
            background: Some(PackedRgba::rgba(180, 100, 100, 20)), // Subtle red tint
            animate_pulse: false,
        }
    }

    /// Create a custom style.
    #[must_use]
    pub fn new(border_char: char, fg: PackedRgba) -> Self {
        Self {
            border_char,
            border_fg: fg,
            background: None,
            animate_pulse: false,
        }
    }

    /// Set background color.
    #[must_use]
    pub fn with_background(mut self, bg: PackedRgba) -> Self {
        self.background = Some(bg);
        self
    }

    /// Enable pulse animation.
    #[must_use]
    pub fn with_pulse(mut self) -> Self {
        self.animate_pulse = true;
        self
    }
}

// ---------------------------------------------------------------------------
// KeyboardDragState
// ---------------------------------------------------------------------------

/// State of an active keyboard drag operation.
#[derive(Debug, Clone)]
pub struct KeyboardDragState {
    /// Widget that initiated the drag.
    pub source_id: WidgetId,
    /// Data being dragged.
    pub payload: DragPayload,
    /// Currently selected drop target index (into available targets list).
    pub selected_target_index: Option<usize>,
    /// Current mode.
    pub mode: KeyboardDragMode,
    /// Animation tick for pulse effect.
    pub animation_tick: u8,
}

impl KeyboardDragState {
    /// Create a new keyboard drag state.
    fn new(source_id: WidgetId, payload: DragPayload) -> Self {
        Self {
            source_id,
            payload,
            selected_target_index: None,
            mode: KeyboardDragMode::Holding,
            animation_tick: 0,
        }
    }

    /// Advance the animation tick.
    pub fn tick_animation(&mut self) {
        self.animation_tick = self.animation_tick.wrapping_add(1);
    }

    /// Get the pulse intensity (0.0 to 1.0) for animation.
    #[must_use]
    pub fn pulse_intensity(&self) -> f32 {
        // Simple sine-based pulse: 0.5 + 0.5 * sin(tick * 0.15)
        let angle = self.animation_tick as f32 * 0.15;
        0.5 + 0.5 * angle.sin()
    }
}

// ---------------------------------------------------------------------------
// KeyboardDragManager
// ---------------------------------------------------------------------------

/// Manager for keyboard-driven drag operations.
#[derive(Debug)]
pub struct KeyboardDragManager {
    /// Configuration.
    config: KeyboardDragConfig,
    /// Current drag state (if any).
    state: Option<KeyboardDragState>,
    /// Announcement queue.
    announcements: Vec<Announcement>,
}

impl KeyboardDragManager {
    /// Create a new keyboard drag manager.
    #[must_use]
    pub fn new(config: KeyboardDragConfig) -> Self {
        Self {
            config,
            state: None,
            announcements: Vec::new(),
        }
    }

    /// Create with default configuration.
    #[must_use]
    pub fn with_defaults() -> Self {
        Self::new(KeyboardDragConfig::default())
    }

    /// Get the current drag mode.
    #[must_use]
    pub fn mode(&self) -> KeyboardDragMode {
        self.state
            .as_ref()
            .map(|s| s.mode)
            .unwrap_or(KeyboardDragMode::Inactive)
    }

    /// Check if a drag is active.
    #[must_use]
    pub fn is_active(&self) -> bool {
        self.state.is_some()
    }

    /// Get the current drag state.
    #[must_use = "use the returned state (if any)"]
    pub fn state(&self) -> Option<&KeyboardDragState> {
        self.state.as_ref()
    }

    /// Get mutable access to the drag state.
    #[must_use = "use the returned state (if any)"]
    pub fn state_mut(&mut self) -> Option<&mut KeyboardDragState> {
        self.state.as_mut()
    }

    /// Start a keyboard drag operation.
    ///
    /// Returns `true` if the drag was started successfully.
    /// Returns `false` if a drag is already in progress.
    pub fn start_drag(&mut self, source_id: WidgetId, payload: DragPayload) -> bool {
        if self.state.is_some() {
            return false;
        }

        let description = payload
            .display_text
            .as_deref()
            .or_else(|| payload.as_text())
            .unwrap_or("item");

        self.queue_announcement(Announcement::high(format!("Picked up: {description}")));

        self.state = Some(KeyboardDragState::new(source_id, payload));
        true
    }

    /// Navigate to the next drop target in the given direction.
    ///
    /// Returns the newly selected target info if navigation succeeded.
    #[must_use = "use the returned target (if any)"]
    pub fn navigate_targets<'a>(
        &mut self,
        direction: Direction,
        targets: &'a [DropTargetInfo],
    ) -> Option<&'a DropTargetInfo> {
        let state = self.state.as_mut()?;

        if targets.is_empty() {
            state.selected_target_index = None;
            state.mode = KeyboardDragMode::Holding;
            return None;
        }

        // Filter to valid targets that can accept the payload
        let valid_indices: Vec<usize> = targets
            .iter()
            .enumerate()
            .filter(|(_, t)| t.can_accept(&state.payload.drag_type))
            .map(|(i, _)| i)
            .collect();

        if valid_indices.is_empty() {
            state.selected_target_index = None;
            state.mode = KeyboardDragMode::Holding;
            self.queue_announcement(Announcement::normal("No valid drop targets available"));
            return None;
        }

        // Update mode to navigating
        state.mode = KeyboardDragMode::Navigating;

        // Find current position among valid targets
        let current_valid_idx = state
            .selected_target_index
            .and_then(|idx| valid_indices.iter().position(|&i| i == idx));

        // Calculate next index based on direction and current selection
        let next_valid_idx = match (current_valid_idx, direction) {
            (None, _) => 0, // No selection, start at first
            (Some(idx), Direction::Down | Direction::Right) => {
                if idx + 1 < valid_indices.len() {
                    idx + 1
                } else if self.config.wrap_navigation {
                    0
                } else {
                    idx
                }
            }
            (Some(idx), Direction::Up | Direction::Left) => {
                if idx > 0 {
                    idx - 1
                } else if self.config.wrap_navigation {
                    valid_indices.len() - 1
                } else {
                    idx
                }
            }
        };

        let target_idx = valid_indices[next_valid_idx];
        state.selected_target_index = Some(target_idx);

        let target = &targets[target_idx];
        let position = format!("{} of {}", next_valid_idx + 1, valid_indices.len());
        self.queue_announcement(Announcement::normal(format!(
            "Drop target: {} ({})",
            target.name, position
        )));

        Some(target)
    }

    /// Navigate to a specific target by index.
    pub fn select_target(&mut self, target_index: usize, targets: &[DropTargetInfo]) -> bool {
        let Some(state) = self.state.as_mut() else {
            return false;
        };

        if target_index >= targets.len() {
            state.selected_target_index = None;
            state.mode = KeyboardDragMode::Holding;
            return false;
        }

        let target = &targets[target_index];
        if !target.can_accept(&state.payload.drag_type) {
            state.selected_target_index = None;
            state.mode = KeyboardDragMode::Holding;
            return false;
        }

        state.mode = KeyboardDragMode::Navigating;
        state.selected_target_index = Some(target_index);

        self.queue_announcement(Announcement::normal(format!(
            "Drop target: {}",
            target.name
        )));
        true
    }

    /// Complete the drag operation (drop on selected target).
    ///
    /// Returns `None` if no target is selected or no drag is active.
    /// Returns `Some((payload, target_index))` with the payload and target index.
    #[must_use = "use the returned (payload, target_index) to complete the drop"]
    pub fn complete_drag(&mut self) -> Option<(DragPayload, usize)> {
        let state = self.state.take()?;
        let target_idx = state.selected_target_index?;

        Some((state.payload, target_idx))
    }

    /// Complete the drag with a specific target and get the drop result info.
    #[must_use = "use the drop result (if any) to apply the drop"]
    pub fn drop_on_target(&mut self, targets: &[DropTargetInfo]) -> Option<KeyboardDropResult> {
        let (target_idx, drag_type) = {
            let state = self.state.as_ref()?;
            (
                state.selected_target_index?,
                state.payload.drag_type.clone(),
            )
        };
        let Some(target) = targets.get(target_idx) else {
            self.clear_selected_target();
            self.queue_announcement(Announcement::normal("Selected drop target unavailable"));
            return None;
        };
        if !target.can_accept(&drag_type) {
            self.clear_selected_target();
            self.queue_announcement(Announcement::normal(
                "Selected drop target no longer accepts this item",
            ));
            return None;
        }
        let target_id = target.id;
        let target_name = target.name.clone();
        let state = self.state.take()?;

        self.queue_announcement(Announcement::high(format!("Dropped on: {target_name}")));

        Some(KeyboardDropResult {
            payload: state.payload,
            source_id: state.source_id,
            target_id,
            target_index: target_idx,
        })
    }

    /// Cancel the current drag operation.
    ///
    /// Returns the payload if a drag was active.
    #[must_use = "use the returned payload (if any) to restore state"]
    pub fn cancel_drag(&mut self) -> Option<DragPayload> {
        let state = self.state.take()?;
        self.queue_announcement(Announcement::normal("Drop cancelled"));
        Some(state.payload)
    }

    /// Handle key press during keyboard drag.
    ///
    /// Returns `KeyboardDragAction` indicating what action was triggered.
    pub fn handle_key(&mut self, key: KeyboardDragKey) -> KeyboardDragAction {
        match key {
            KeyboardDragKey::Activate => {
                if self.is_active() {
                    // If navigating with a selected target, complete the drop
                    if let Some(state) = &self.state
                        && state.selected_target_index.is_some()
                    {
                        KeyboardDragAction::Drop
                    } else {
                        // No target selected yet, stay in current state
                        KeyboardDragAction::None
                    }
                } else {
                    // No drag active, signal to pick up
                    KeyboardDragAction::PickUp
                }
            }
            KeyboardDragKey::Cancel => {
                if self.is_active() && self.config.cancel_on_escape {
                    KeyboardDragAction::Cancel
                } else {
                    KeyboardDragAction::None
                }
            }
            KeyboardDragKey::Navigate(dir) => {
                if self.is_active() {
                    KeyboardDragAction::Navigate(dir)
                } else {
                    KeyboardDragAction::None
                }
            }
        }
    }

    /// Advance animation state.
    pub fn tick(&mut self) {
        if let Some(state) = &mut self.state {
            state.tick_animation();
        }
    }

    /// Get and clear pending announcements.
    pub fn drain_announcements(&mut self) -> Vec<Announcement> {
        std::mem::take(&mut self.announcements)
    }

    /// Peek at pending announcements without clearing.
    #[must_use]
    pub fn announcements(&self) -> &[Announcement] {
        &self.announcements
    }

    /// Queue an announcement for screen readers.
    fn queue_announcement(&mut self, announcement: Announcement) {
        if self.config.max_announcement_queue == 0 {
            return;
        }
        if self.announcements.len() >= self.config.max_announcement_queue {
            // Remove lowest priority announcement
            if let Some((pos, lowest_priority)) = self
                .announcements
                .iter()
                .enumerate()
                .min_by_key(|(_, a)| a.priority)
                .map(|(i, a)| (i, a.priority))
            {
                if announcement.priority < lowest_priority {
                    return;
                }
                self.announcements.remove(pos);
            }
        }
        self.announcements.push(announcement);
    }

    fn clear_selected_target(&mut self) {
        if let Some(state) = &mut self.state {
            state.selected_target_index = None;
            state.mode = KeyboardDragMode::Holding;
        }
    }

    /// Render the target highlight overlay.
    pub fn render_highlight(&self, targets: &[DropTargetInfo], frame: &mut Frame) {
        let Some(state) = &self.state else {
            return;
        };
        let Some(target_idx) = state.selected_target_index else {
            return;
        };
        let Some(target) = targets.get(target_idx) else {
            return;
        };

        let style = if target.can_accept(&state.payload.drag_type) {
            &self.config.target_highlight_style
        } else {
            &self.config.invalid_target_style
        };

        let bounds = target.bounds;
        if bounds.is_empty() {
            return;
        }

        // Apply background tint if configured
        if let Some(bg) = style.background {
            // Calculate effective alpha based on pulse
            let alpha = if style.animate_pulse {
                let base_alpha = (bg.0 & 0xFF) as f32 / 255.0;
                let pulsed = base_alpha * (0.5 + 0.5 * state.pulse_intensity());
                (pulsed * 255.0) as u8
            } else {
                (bg.0 & 0xFF) as u8
            };

            let effective_bg = PackedRgba((bg.0 & 0xFFFF_FF00) | alpha as u32);

            // Fill background
            for y in bounds.y..bounds.y.saturating_add(bounds.height) {
                for x in bounds.x..bounds.x.saturating_add(bounds.width) {
                    if let Some(cell) = frame.buffer.get_mut(x, y) {
                        cell.bg = effective_bg;
                    }
                }
            }
        }

        // Draw highlight border
        let fg_style = Style::new().fg(style.border_fg);
        let border_char = style.border_char;

        // Top and bottom borders
        for x in bounds.x..bounds.x.saturating_add(bounds.width) {
            // Top
            let mut cell = Cell::from_char(border_char);
            cell.fg = fg_style.fg.unwrap_or(style.border_fg);
            frame.buffer.set_fast(x, bounds.y, cell);

            // Bottom
            let bottom_y = bounds.y.saturating_add(bounds.height.saturating_sub(1));
            if bounds.height > 1 {
                let mut cell_b = Cell::from_char(border_char);
                cell_b.fg = fg_style.fg.unwrap_or(style.border_fg);
                frame.buffer.set_fast(x, bottom_y, cell_b);
            }
        }

        // Left and right borders (excluding corners)
        for y in
            bounds.y.saturating_add(1)..bounds.y.saturating_add(bounds.height.saturating_sub(1))
        {
            // Left
            let mut cell = Cell::from_char(border_char);
            cell.fg = fg_style.fg.unwrap_or(style.border_fg);
            frame.buffer.set_fast(bounds.x, y, cell);

            // Right
            let right_x = bounds.x.saturating_add(bounds.width.saturating_sub(1));
            if bounds.width > 1 {
                let mut cell_r = Cell::from_char(border_char);
                cell_r.fg = fg_style.fg.unwrap_or(style.border_fg);
                frame.buffer.set_fast(right_x, y, cell_r);
            }
        }
    }
}

// ---------------------------------------------------------------------------
// KeyboardDragKey
// ---------------------------------------------------------------------------

/// Key events relevant to keyboard drag operations.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum KeyboardDragKey {
    /// Activation key (Space or Enter by default).
    Activate,
    /// Cancellation key (Escape by default).
    Cancel,
    /// Navigation key.
    Navigate(Direction),
}

// ---------------------------------------------------------------------------
// KeyboardDragAction
// ---------------------------------------------------------------------------

/// Action resulting from key handling.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum KeyboardDragAction {
    /// No action needed.
    None,
    /// Pick up the focused item to start a drag.
    PickUp,
    /// Navigate to next target in direction.
    Navigate(Direction),
    /// Drop on the selected target.
    Drop,
    /// Cancel the drag operation.
    Cancel,
}

// ---------------------------------------------------------------------------
// KeyboardDropResult
// ---------------------------------------------------------------------------

/// Result of a completed keyboard drag-and-drop operation.
#[derive(Debug, Clone)]
pub struct KeyboardDropResult {
    /// The dropped payload.
    pub payload: DragPayload,
    /// Source widget ID.
    pub source_id: WidgetId,
    /// Target widget ID.
    pub target_id: WidgetId,
    /// Target index in the targets list.
    pub target_index: usize,
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;

    // === KeyboardDragMode tests ===

    #[test]
    fn mode_is_active() {
        assert!(!KeyboardDragMode::Inactive.is_active());
        assert!(KeyboardDragMode::Holding.is_active());
        assert!(KeyboardDragMode::Navigating.is_active());
    }

    #[test]
    fn mode_as_str() {
        assert_eq!(KeyboardDragMode::Inactive.as_str(), "inactive");
        assert_eq!(KeyboardDragMode::Holding.as_str(), "holding");
        assert_eq!(KeyboardDragMode::Navigating.as_str(), "navigating");
    }

    // === Direction tests ===

    #[test]
    fn direction_opposite() {
        assert_eq!(Direction::Up.opposite(), Direction::Down);
        assert_eq!(Direction::Down.opposite(), Direction::Up);
        assert_eq!(Direction::Left.opposite(), Direction::Right);
        assert_eq!(Direction::Right.opposite(), Direction::Left);
    }

    #[test]
    fn direction_is_vertical() {
        assert!(Direction::Up.is_vertical());
        assert!(Direction::Down.is_vertical());
        assert!(!Direction::Left.is_vertical());
        assert!(!Direction::Right.is_vertical());
    }

    // === DropTargetInfo tests ===

    #[test]
    fn drop_target_info_new() {
        let target = DropTargetInfo::new(WidgetId(1), "Test Target", Rect::new(0, 0, 10, 5));
        assert_eq!(target.id, WidgetId(1));
        assert_eq!(target.name, "Test Target");
        assert!(target.enabled);
        assert!(target.accepted_types.is_empty());
    }

    #[test]
    fn drop_target_info_can_accept_any() {
        let target = DropTargetInfo::new(WidgetId(1), "Any", Rect::new(0, 0, 1, 1));
        // No filter means accept any
        assert!(target.can_accept("text/plain"));
        assert!(target.can_accept("application/json"));
    }

    #[test]
    fn drop_target_info_can_accept_filtered() {
        let target = DropTargetInfo::new(WidgetId(1), "Text Only", Rect::new(0, 0, 1, 1))
            .with_accepted_types(vec!["text/plain".to_string()]);
        assert!(target.can_accept("text/plain"));
        assert!(!target.can_accept("application/json"));
    }

    #[test]
    fn drop_target_info_can_accept_wildcard() {
        let target = DropTargetInfo::new(WidgetId(1), "All Text", Rect::new(0, 0, 1, 1))
            .with_accepted_types(vec!["text/*".to_string()]);
        assert!(target.can_accept("text/plain"));
        assert!(target.can_accept("text/html"));
        assert!(!target.can_accept("application/json"));
    }

    #[test]
    fn drop_target_info_disabled() {
        let target =
            DropTargetInfo::new(WidgetId(1), "Disabled", Rect::new(0, 0, 1, 1)).with_enabled(false);
        assert!(!target.can_accept("text/plain"));
    }

    #[test]
    fn drop_target_info_center() {
        let target = DropTargetInfo::new(WidgetId(1), "Test", Rect::new(10, 20, 10, 6));
        assert_eq!(target.center(), (15, 23));
    }

    // === Announcement tests ===

    #[test]
    fn announcement_normal() {
        let a = Announcement::normal("Test message");
        assert_eq!(a.text, "Test message");
        assert_eq!(a.priority, AnnouncementPriority::Normal);
    }

    #[test]
    fn announcement_high() {
        let a = Announcement::high("Important!");
        assert_eq!(a.priority, AnnouncementPriority::High);
    }

    // === KeyboardDragConfig tests ===

    #[test]
    fn config_defaults() {
        let config = KeyboardDragConfig::default();
        assert!(config.cancel_on_escape);
        assert!(config.wrap_navigation);
        assert_eq!(config.activate_keys.len(), 2);
    }

    // === KeyboardDragState tests ===

    #[test]
    fn drag_state_animation() {
        let payload = DragPayload::text("test");
        let mut state = KeyboardDragState::new(WidgetId(1), payload);

        let initial_tick = state.animation_tick;
        state.tick_animation();
        assert_eq!(state.animation_tick, initial_tick.wrapping_add(1));
    }

    #[test]
    fn drag_state_pulse_intensity() {
        let payload = DragPayload::text("test");
        let state = KeyboardDragState::new(WidgetId(1), payload);

        let intensity = state.pulse_intensity();
        assert!((0.0..=1.0).contains(&intensity));
    }

    // === KeyboardDragManager tests ===

    #[test]
    fn manager_start_drag() {
        let mut manager = KeyboardDragManager::with_defaults();
        assert!(!manager.is_active());

        let payload = DragPayload::text("item");
        assert!(manager.start_drag(WidgetId(1), payload));
        assert!(manager.is_active());
        assert_eq!(manager.mode(), KeyboardDragMode::Holding);
    }

    #[test]
    fn manager_double_start_fails() {
        let mut manager = KeyboardDragManager::with_defaults();

        assert!(manager.start_drag(WidgetId(1), DragPayload::text("first")));
        assert!(!manager.start_drag(WidgetId(2), DragPayload::text("second")));
    }

    #[test]
    fn manager_cancel_drag() {
        let mut manager = KeyboardDragManager::with_defaults();
        manager.start_drag(WidgetId(1), DragPayload::text("item"));

        let payload = manager.cancel_drag();
        assert!(payload.is_some());
        assert!(!manager.is_active());
    }

    #[test]
    fn manager_cancel_inactive() {
        let mut manager = KeyboardDragManager::with_defaults();
        assert!(manager.cancel_drag().is_none());
    }

    #[test]
    fn manager_navigate_targets() {
        let mut manager = KeyboardDragManager::with_defaults();
        manager.start_drag(WidgetId(1), DragPayload::text("item"));

        let targets = vec![
            DropTargetInfo::new(WidgetId(10), "Target A", Rect::new(0, 0, 10, 5)),
            DropTargetInfo::new(WidgetId(11), "Target B", Rect::new(20, 0, 10, 5)),
        ];

        let selected = manager.navigate_targets(Direction::Down, &targets);
        assert!(selected.is_some());
        assert_eq!(selected.unwrap().name, "Target A");
        assert_eq!(manager.mode(), KeyboardDragMode::Navigating);
    }

    #[test]
    fn manager_navigate_empty_targets() {
        let mut manager = KeyboardDragManager::with_defaults();
        manager.start_drag(WidgetId(1), DragPayload::text("item"));
        manager
            .state_mut()
            .expect("drag active")
            .selected_target_index = Some(3);
        manager.state_mut().expect("drag active").mode = KeyboardDragMode::Navigating;

        let targets: Vec<DropTargetInfo> = vec![];
        let selected = manager.navigate_targets(Direction::Down, &targets);
        assert!(selected.is_none());
        assert_eq!(manager.mode(), KeyboardDragMode::Holding);
        assert!(
            manager
                .state()
                .expect("drag remains active")
                .selected_target_index
                .is_none()
        );
    }

    #[test]
    fn manager_navigate_wrap() {
        let mut manager = KeyboardDragManager::with_defaults();
        manager.start_drag(WidgetId(1), DragPayload::text("item"));

        let targets = vec![
            DropTargetInfo::new(WidgetId(10), "Target A", Rect::new(0, 0, 10, 5)),
            DropTargetInfo::new(WidgetId(11), "Target B", Rect::new(20, 0, 10, 5)),
        ];

        // Navigate to first
        let _ = manager.navigate_targets(Direction::Down, &targets);
        // Navigate to second
        let _ = manager.navigate_targets(Direction::Down, &targets);
        // Navigate past end, should wrap to first
        let selected = manager.navigate_targets(Direction::Down, &targets);

        assert_eq!(selected.unwrap().name, "Target A");
    }

    #[test]
    fn manager_complete_drag() {
        let mut manager = KeyboardDragManager::with_defaults();
        manager.start_drag(WidgetId(1), DragPayload::text("item"));

        let targets = vec![DropTargetInfo::new(
            WidgetId(10),
            "Target A",
            Rect::new(0, 0, 10, 5),
        )];

        let _ = manager.navigate_targets(Direction::Down, &targets);

        let result = manager.complete_drag();
        assert!(result.is_some());
        let (payload, idx) = result.unwrap();
        assert_eq!(payload.as_text(), Some("item"));
        assert_eq!(idx, 0);
        assert!(!manager.is_active());
    }

    #[test]
    fn manager_complete_without_target() {
        let mut manager = KeyboardDragManager::with_defaults();
        manager.start_drag(WidgetId(1), DragPayload::text("item"));

        // No target selected
        let result = manager.complete_drag();
        assert!(result.is_none());
    }

    #[test]
    fn manager_navigate_no_valid_targets_clears_stale_selection() {
        let mut manager = KeyboardDragManager::with_defaults();
        manager.start_drag(WidgetId(1), DragPayload::new("text/plain", vec![]));

        let valid_targets = vec![
            DropTargetInfo::new(WidgetId(10), "Text Target", Rect::new(0, 0, 10, 5))
                .with_accepted_types(vec!["text/plain".to_string()]),
        ];
        let _ = manager.navigate_targets(Direction::Down, &valid_targets);
        assert_eq!(manager.mode(), KeyboardDragMode::Navigating);

        let invalid_targets = vec![
            DropTargetInfo::new(WidgetId(11), "Image Target", Rect::new(20, 0, 10, 5))
                .with_accepted_types(vec!["image/*".to_string()]),
        ];
        let selected = manager.navigate_targets(Direction::Down, &invalid_targets);

        assert!(selected.is_none());
        assert_eq!(manager.mode(), KeyboardDragMode::Holding);
        assert!(
            manager
                .state()
                .expect("drag remains active")
                .selected_target_index
                .is_none()
        );
        assert_eq!(
            manager.handle_key(KeyboardDragKey::Activate),
            KeyboardDragAction::None
        );
    }

    #[test]
    fn manager_handle_key_pickup() {
        let mut manager = KeyboardDragManager::with_defaults();
        let action = manager.handle_key(KeyboardDragKey::Activate);
        assert_eq!(action, KeyboardDragAction::PickUp);
    }

    #[test]
    fn manager_handle_key_drop() {
        let mut manager = KeyboardDragManager::with_defaults();
        manager.start_drag(WidgetId(1), DragPayload::text("item"));

        // Select a target
        manager.state_mut().unwrap().selected_target_index = Some(0);

        let action = manager.handle_key(KeyboardDragKey::Activate);
        assert_eq!(action, KeyboardDragAction::Drop);
    }

    #[test]
    fn manager_handle_key_cancel() {
        let mut manager = KeyboardDragManager::with_defaults();
        manager.start_drag(WidgetId(1), DragPayload::text("item"));

        let action = manager.handle_key(KeyboardDragKey::Cancel);
        assert_eq!(action, KeyboardDragAction::Cancel);
    }

    #[test]
    fn manager_handle_key_navigate() {
        let mut manager = KeyboardDragManager::with_defaults();
        manager.start_drag(WidgetId(1), DragPayload::text("item"));

        let action = manager.handle_key(KeyboardDragKey::Navigate(Direction::Down));
        assert_eq!(action, KeyboardDragAction::Navigate(Direction::Down));
    }

    #[test]
    fn manager_announcements() {
        let mut manager = KeyboardDragManager::with_defaults();
        manager.start_drag(WidgetId(1), DragPayload::text("item"));

        let announcements = manager.drain_announcements();
        assert!(!announcements.is_empty());
        assert!(announcements[0].text.contains("Picked up"));
    }

    #[test]
    fn manager_announcement_queue_limit() {
        let config = KeyboardDragConfig {
            max_announcement_queue: 2,
            ..Default::default()
        };
        let mut manager = KeyboardDragManager::new(config);

        // Fill queue
        manager.start_drag(WidgetId(1), DragPayload::text("item1"));
        let _ = manager.cancel_drag();
        manager.start_drag(WidgetId(2), DragPayload::text("item2"));

        // Should have at most 2 announcements
        assert!(manager.announcements().len() <= 2);
    }

    #[test]
    fn manager_announcement_queue_zero_discards_announcements() {
        let config = KeyboardDragConfig {
            max_announcement_queue: 0,
            ..Default::default()
        };
        let mut manager = KeyboardDragManager::new(config);

        assert!(manager.start_drag(WidgetId(1), DragPayload::text("item")));
        let _ = manager.cancel_drag();

        assert!(manager.announcements().is_empty());
    }

    #[test]
    fn manager_lower_priority_announcement_does_not_evict_higher_priority() {
        let config = KeyboardDragConfig {
            max_announcement_queue: 1,
            ..Default::default()
        };
        let mut manager = KeyboardDragManager::new(config);

        assert!(manager.start_drag(WidgetId(1), DragPayload::text("item")));
        let _ = manager.cancel_drag();

        assert_eq!(manager.announcements().len(), 1);
        assert_eq!(
            manager.announcements()[0].priority,
            AnnouncementPriority::High
        );
        assert!(manager.announcements()[0].text.contains("Picked up"));
    }

    // === Target filtering tests ===

    #[test]
    fn manager_navigate_skips_incompatible() {
        let mut manager = KeyboardDragManager::with_defaults();
        manager.start_drag(WidgetId(1), DragPayload::new("text/plain", vec![]));

        let targets = vec![
            DropTargetInfo::new(WidgetId(10), "Text Target", Rect::new(0, 0, 10, 5))
                .with_accepted_types(vec!["text/plain".to_string()]),
            DropTargetInfo::new(WidgetId(11), "Image Target", Rect::new(20, 0, 10, 5))
                .with_accepted_types(vec!["image/*".to_string()]),
            DropTargetInfo::new(WidgetId(12), "Text Target 2", Rect::new(40, 0, 10, 5))
                .with_accepted_types(vec!["text/plain".to_string()]),
        ];

        // First navigation should select Text Target
        let selected = manager.navigate_targets(Direction::Down, &targets);
        assert_eq!(selected.unwrap().name, "Text Target");

        // Second navigation should skip Image Target and select Text Target 2
        let selected = manager.navigate_targets(Direction::Down, &targets);
        assert_eq!(selected.unwrap().name, "Text Target 2");
    }

    // === Integration tests ===

    #[test]
    fn full_keyboard_drag_lifecycle() {
        let mut manager = KeyboardDragManager::with_defaults();

        // 1. Start drag
        assert!(manager.start_drag(WidgetId(1), DragPayload::text("dragged_item")));
        assert_eq!(manager.mode(), KeyboardDragMode::Holding);

        let targets = vec![
            DropTargetInfo::new(WidgetId(10), "Target A", Rect::new(0, 0, 10, 5)),
            DropTargetInfo::new(WidgetId(11), "Target B", Rect::new(0, 10, 10, 5)),
        ];

        // 2. Navigate to target
        let _ = manager.navigate_targets(Direction::Down, &targets);
        assert_eq!(manager.mode(), KeyboardDragMode::Navigating);

        // 3. Navigate to next target
        let _ = manager.navigate_targets(Direction::Down, &targets);

        // 4. Complete drop
        let result = manager.drop_on_target(&targets);
        assert!(result.is_some());
        let result = result.unwrap();
        assert_eq!(result.payload.as_text(), Some("dragged_item"));
        assert_eq!(result.target_id, WidgetId(11));
        assert_eq!(result.target_index, 1);

        // 5. Manager is now inactive
        assert!(!manager.is_active());
    }

    #[test]
    fn manager_drop_on_invalidated_target_keeps_drag_active() {
        let mut manager = KeyboardDragManager::with_defaults();
        assert!(manager.start_drag(WidgetId(1), DragPayload::new("text/plain", vec![])));

        let targets = vec![
            DropTargetInfo::new(WidgetId(10), "Text Target", Rect::new(0, 0, 10, 5))
                .with_accepted_types(vec!["text/plain".to_string()]),
        ];
        let _ = manager.navigate_targets(Direction::Down, &targets);
        assert_eq!(manager.mode(), KeyboardDragMode::Navigating);

        let invalidated_targets = vec![
            DropTargetInfo::new(WidgetId(10), "Image Target", Rect::new(0, 0, 10, 5))
                .with_accepted_types(vec!["image/*".to_string()]),
        ];
        let result = manager.drop_on_target(&invalidated_targets);

        assert!(result.is_none());
        assert!(manager.is_active());
        assert_eq!(manager.mode(), KeyboardDragMode::Holding);
        assert!(
            manager
                .state()
                .expect("drag remains active")
                .selected_target_index
                .is_none()
        );
    }
}