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
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
//! Menu bar rendering
use crate::config::{generate_dynamic_items, Menu, MenuConfig, MenuExt, MenuItem, MenuItemExt};
use crate::primitives::display_width::str_width;
use crate::view::theme::Theme;
use crate::view::ui::layout::point_in_rect;
use ratatui::layout::Rect;
use ratatui::style::{Modifier, Style};
use ratatui::text::{Line, Span};
use ratatui::widgets::{Block, Borders, Paragraph};
use ratatui::Frame;
// Re-export context_keys from the shared types module
pub use crate::types::context_keys;
/// Layout information for hit testing menu interactions
///
/// Returned by `MenuRenderer::render()` to enable mouse hit testing
/// without duplicating position calculations.
#[derive(Debug, Clone, Default)]
pub struct MenuLayout {
/// Areas for top-level menu labels: (menu_index, area)
pub menu_areas: Vec<(usize, Rect)>,
/// Areas for dropdown items: (item_index, area)
/// Only populated when a menu is open
pub item_areas: Vec<(usize, Rect)>,
/// Areas for submenu items at each depth: (depth, item_index, area)
pub submenu_areas: Vec<(usize, usize, Rect)>,
/// The full menu bar area
pub bar_area: Rect,
}
/// Hit test result for menu interactions
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MenuHit {
/// Hit a top-level menu label
MenuLabel(usize),
/// Hit a dropdown item (in the main dropdown)
DropdownItem(usize),
/// Hit a submenu item at a given depth
SubmenuItem { depth: usize, index: usize },
/// Hit the menu bar background
BarBackground,
}
impl MenuLayout {
/// Create a new empty layout
pub fn new(bar_area: Rect) -> Self {
Self {
menu_areas: Vec::new(),
item_areas: Vec::new(),
submenu_areas: Vec::new(),
bar_area,
}
}
/// Get the menu index at a given position
pub fn menu_at(&self, x: u16, y: u16) -> Option<usize> {
for (idx, area) in &self.menu_areas {
if point_in_rect(*area, x, y) {
return Some(*idx);
}
}
None
}
/// Get the dropdown item index at a given position
pub fn item_at(&self, x: u16, y: u16) -> Option<usize> {
for (idx, area) in &self.item_areas {
if point_in_rect(*area, x, y) {
return Some(*idx);
}
}
None
}
/// Get the submenu item at a given position
pub fn submenu_item_at(&self, x: u16, y: u16) -> Option<(usize, usize)> {
for (depth, idx, area) in &self.submenu_areas {
if point_in_rect(*area, x, y) {
return Some((*depth, *idx));
}
}
None
}
/// Perform a complete hit test
pub fn hit_test(&self, x: u16, y: u16) -> Option<MenuHit> {
// Check submenu items first (they're on top)
if let Some((depth, idx)) = self.submenu_item_at(x, y) {
return Some(MenuHit::SubmenuItem { depth, index: idx });
}
// Check dropdown items
if let Some(idx) = self.item_at(x, y) {
return Some(MenuHit::DropdownItem(idx));
}
// Check menu labels
if let Some(idx) = self.menu_at(x, y) {
return Some(MenuHit::MenuLabel(idx));
}
// Check bar background
if point_in_rect(self.bar_area, x, y) {
return Some(MenuHit::BarBackground);
}
None
}
}
/// Menu state context - provides named boolean states for menu item conditions
/// Both `when` conditions and `checkbox` states look up values here
#[derive(Debug, Clone, Default)]
pub struct MenuContext {
states: std::collections::HashMap<String, bool>,
}
impl MenuContext {
pub fn new() -> Self {
Self {
states: std::collections::HashMap::new(),
}
}
/// Set a named boolean state
pub fn set(&mut self, name: impl Into<String>, value: bool) -> &mut Self {
self.states.insert(name.into(), value);
self
}
/// Get a named boolean state (defaults to false if not set)
pub fn get(&self, name: &str) -> bool {
self.states.get(name).copied().unwrap_or(false)
}
/// Builder-style setter
pub fn with(mut self, name: impl Into<String>, value: bool) -> Self {
self.set(name, value);
self
}
}
fn is_menu_item_enabled(item: &MenuItem, context: &MenuContext) -> bool {
match item {
MenuItem::Action { when, .. } => {
match when.as_deref() {
Some(condition) => context.get(condition),
None => true, // No condition means always enabled
}
}
_ => true,
}
}
fn is_checkbox_checked(checkbox: &Option<String>, context: &MenuContext) -> bool {
match checkbox.as_deref() {
Some(name) => context.get(name),
None => false,
}
}
/// Menu bar state (tracks which menu is open and which item is highlighted)
#[derive(Debug, Clone, Default)]
pub struct MenuState {
/// Index of the currently open menu (None if menu bar is closed)
pub active_menu: Option<usize>,
/// Index of the highlighted item within the active menu or current submenu
pub highlighted_item: Option<usize>,
/// Path of indices into nested submenus (empty = at top level menu)
/// Each element is the index of the submenu item that was opened
pub submenu_path: Vec<usize>,
/// Runtime menu additions from plugins
pub plugin_menus: Vec<Menu>,
/// Context containing named boolean states for conditions and checkboxes
pub context: MenuContext,
}
impl MenuState {
pub fn new() -> Self {
Self::default()
}
/// Open a menu by index
pub fn open_menu(&mut self, index: usize) {
self.active_menu = Some(index);
self.highlighted_item = Some(0);
self.submenu_path.clear();
}
/// Close the currently open menu (and all submenus)
pub fn close_menu(&mut self) {
self.active_menu = None;
self.highlighted_item = None;
self.submenu_path.clear();
}
/// Navigate to the next menu (right) - only at top level
/// Skips menus that are hidden (where `when` condition evaluates to false)
pub fn next_menu(&mut self, menus: &[Menu]) {
let Some(active) = self.active_menu else {
return;
};
let total = menus.len();
if total == 0 {
return;
}
// Find the next visible menu, wrapping around
for i in 1..=total {
let next_idx = (active + i) % total;
if self.is_menu_visible(&menus[next_idx]) {
self.active_menu = Some(next_idx);
self.highlighted_item = Some(0);
self.submenu_path.clear();
return;
}
}
// No visible menu found, stay on current
}
/// Navigate to the previous menu (left) - only at top level
/// Skips menus that are hidden (where `when` condition evaluates to false)
pub fn prev_menu(&mut self, menus: &[Menu]) {
let Some(active) = self.active_menu else {
return;
};
let total = menus.len();
if total == 0 {
return;
}
// Find the previous visible menu, wrapping around
for i in 1..=total {
let prev_idx = (active + total - i) % total;
if self.is_menu_visible(&menus[prev_idx]) {
self.active_menu = Some(prev_idx);
self.highlighted_item = Some(0);
self.submenu_path.clear();
return;
}
}
// No visible menu found, stay on current
}
/// Check if a menu is visible based on its `when` condition
fn is_menu_visible(&self, menu: &Menu) -> bool {
match &menu.when {
Some(condition) => self.context.get(condition),
None => true, // No condition = always visible
}
}
/// Check if we're currently in a submenu
pub fn in_submenu(&self) -> bool {
!self.submenu_path.is_empty()
}
/// Get the current submenu depth (0 = top level menu)
pub fn submenu_depth(&self) -> usize {
self.submenu_path.len()
}
/// Open a submenu at the current highlighted item
/// Returns true if a submenu was opened, false if the item wasn't a submenu
pub fn open_submenu(&mut self, menus: &[Menu]) -> bool {
let Some(active_idx) = self.active_menu else {
return false;
};
let Some(highlighted) = self.highlighted_item else {
return false;
};
// Get the current menu items
let Some(menu) = menus.get(active_idx) else {
return false;
};
let Some(items) = self.get_current_items_cloned(menu) else {
return false;
};
// Check if highlighted item is a submenu (including DynamicSubmenu which was expanded)
if let Some(item) = items.get(highlighted) {
match item {
MenuItem::Submenu {
items: submenu_items,
..
} if !submenu_items.is_empty() => {
self.submenu_path.push(highlighted);
self.highlighted_item = Some(0);
return true;
}
MenuItem::DynamicSubmenu { source, .. } => {
// Generate items to check if non-empty
let generated = generate_dynamic_items(source);
if !generated.is_empty() {
self.submenu_path.push(highlighted);
self.highlighted_item = Some(0);
return true;
}
}
_ => {}
}
}
false
}
/// Close the current submenu and go back to parent
/// Returns true if a submenu was closed, false if already at top level
pub fn close_submenu(&mut self) -> bool {
if let Some(parent_idx) = self.submenu_path.pop() {
self.highlighted_item = Some(parent_idx);
true
} else {
false
}
}
/// Get the menu items at the current submenu level
pub fn get_current_items<'a>(
&self,
menus: &'a [Menu],
active_idx: usize,
) -> Option<&'a [MenuItem]> {
let menu = menus.get(active_idx)?;
let mut items: &[MenuItem] = &menu.items;
for &idx in &self.submenu_path {
match items.get(idx)? {
MenuItem::Submenu {
items: submenu_items,
..
} => {
items = submenu_items;
}
_ => return None,
}
}
Some(items)
}
/// Get owned vec of current items (for use when Menu is cloned)
/// DynamicSubmenus are expanded to regular Submenus
pub fn get_current_items_cloned(&self, menu: &Menu) -> Option<Vec<MenuItem>> {
// Expand all items (handles DynamicSubmenu -> Submenu)
let mut items: Vec<MenuItem> = menu.items.iter().map(|i| i.expand_dynamic()).collect();
for &idx in &self.submenu_path {
match items.get(idx)?.expand_dynamic() {
MenuItem::Submenu {
items: submenu_items,
..
} => {
items = submenu_items;
}
_ => return None,
}
}
Some(items)
}
/// Navigate to the next item in the current menu/submenu (down)
pub fn next_item(&mut self, menu: &Menu) {
let Some(idx) = self.highlighted_item else {
return;
};
// Get current items (may be in a submenu)
let Some(items) = self.get_current_items_cloned(menu) else {
return;
};
if items.is_empty() {
return;
}
// Skip separators and disabled items
let mut next = (idx + 1) % items.len();
while next != idx && self.should_skip_item(&items[next]) {
next = (next + 1) % items.len();
}
self.highlighted_item = Some(next);
}
/// Navigate to the previous item in the current menu/submenu (up)
pub fn prev_item(&mut self, menu: &Menu) {
let Some(idx) = self.highlighted_item else {
return;
};
// Get current items (may be in a submenu)
let Some(items) = self.get_current_items_cloned(menu) else {
return;
};
if items.is_empty() {
return;
}
// Skip separators and disabled items
let total = items.len();
let mut prev = (idx + total - 1) % total;
while prev != idx && self.should_skip_item(&items[prev]) {
prev = (prev + total - 1) % total;
}
self.highlighted_item = Some(prev);
}
/// Check if a menu item should be skipped during navigation
fn should_skip_item(&self, item: &MenuItem) -> bool {
match item {
MenuItem::Separator { .. } => true,
MenuItem::Action { when, .. } => {
// Skip disabled items (when condition evaluates to false)
match when.as_deref() {
Some(condition) => !self.context.get(condition),
None => false, // No condition means enabled, don't skip
}
}
_ => false,
}
}
/// Get the currently highlighted action (if any)
/// This navigates through the submenu path to find the currently highlighted item
pub fn get_highlighted_action(
&self,
menus: &[Menu],
) -> Option<(String, std::collections::HashMap<String, serde_json::Value>)> {
let active_menu = self.active_menu?;
let highlighted_item = self.highlighted_item?;
// Get the items at the current submenu level, handling DynamicSubmenu
let menu = menus.get(active_menu)?;
let items = self.get_current_items_cloned(menu)?;
let item = items.get(highlighted_item)?;
match item {
MenuItem::Action { action, args, .. } => {
if is_menu_item_enabled(item, &self.context) {
Some((action.clone(), args.clone()))
} else {
None
}
}
_ => None,
}
}
/// Check if the currently highlighted item is a submenu
pub fn is_highlighted_submenu(&self, menus: &[Menu]) -> bool {
let Some(active_menu) = self.active_menu else {
return false;
};
let Some(highlighted_item) = self.highlighted_item else {
return false;
};
// Use get_current_items_cloned to handle DynamicSubmenu
let Some(menu) = menus.get(active_menu) else {
return false;
};
let Some(items) = self.get_current_items_cloned(menu) else {
return false;
};
matches!(
items.get(highlighted_item),
Some(MenuItem::Submenu { .. } | MenuItem::DynamicSubmenu { .. })
)
}
}
/// Renders the menu bar
pub struct MenuRenderer;
impl MenuRenderer {
/// Render the menu bar at the top of the screen
///
/// # Arguments
/// * `frame` - The ratatui frame to render to
/// * `area` - The rectangular area to render the menu bar in
/// * `menu_config` - The menu configuration
/// * `menu_state` - Current menu state (which menu/item is active, and context)
/// * `keybindings` - Keybinding resolver for displaying shortcuts
/// * `theme` - The active theme for colors
/// * `hover_target` - The currently hovered UI element (if any)
///
/// # Returns
/// `MenuLayout` containing hit areas for mouse interaction
pub fn render(
frame: &mut Frame,
area: Rect,
menu_config: &MenuConfig,
menu_state: &MenuState,
keybindings: &crate::input::keybindings::KeybindingResolver,
theme: &Theme,
hover_target: Option<&crate::app::HoverTarget>,
) -> MenuLayout {
let mut layout = MenuLayout::new(area);
// Combine config menus with plugin menus, expanding any DynamicSubmenus
let all_menus: Vec<Menu> = menu_config
.menus
.iter()
.chain(menu_state.plugin_menus.iter())
.cloned()
.map(|mut menu| {
menu.expand_dynamic_items();
menu
})
.collect();
// Track which menus are visible (based on their `when` condition)
let menu_visible: Vec<bool> = all_menus
.iter()
.map(|menu| match &menu.when {
Some(condition) => menu_state.context.get(condition),
None => true, // No condition = always visible
})
.collect();
// Build spans for each menu label and track their areas
let mut spans = Vec::new();
let mut current_x = area.x;
for (idx, menu) in all_menus.iter().enumerate() {
// Skip hidden menus
if !menu_visible[idx] {
continue;
}
let is_active = menu_state.active_menu == Some(idx);
let is_hovered =
matches!(hover_target, Some(crate::app::HoverTarget::MenuBarItem(i)) if *i == idx);
let base_style = if is_active {
Style::default()
.fg(theme.menu_active_fg)
.bg(theme.menu_active_bg)
.add_modifier(Modifier::BOLD)
} else if is_hovered {
Style::default()
.fg(theme.menu_hover_fg)
.bg(theme.menu_hover_bg)
} else {
Style::default().fg(theme.menu_fg).bg(theme.menu_bg)
};
// Calculate label width: " Label " = 1 + label_width + 1
let label_width = str_width(&menu.label) as u16 + 2;
// Track the menu label area for hit testing
layout
.menu_areas
.push((idx, Rect::new(current_x, area.y, label_width, 1)));
// Check for mnemonic character (Alt+letter keybinding)
let mnemonic = keybindings.find_menu_mnemonic(&menu.label);
// Build the label with underlined mnemonic
spans.push(Span::styled(" ", base_style));
if let Some(mnemonic_char) = mnemonic {
// Find the first occurrence of the mnemonic character in the label
let mut found = false;
for c in menu.label.chars() {
if !found && c.to_ascii_lowercase() == mnemonic_char {
// Underline this character
spans.push(Span::styled(
c.to_string(),
base_style.add_modifier(Modifier::UNDERLINED),
));
found = true;
} else {
spans.push(Span::styled(c.to_string(), base_style));
}
}
} else {
// No mnemonic, just render the label normally
spans.push(Span::styled(menu.label.clone(), base_style));
}
spans.push(Span::styled(" ", base_style));
spans.push(Span::raw(" "));
// Move to next position: label_width + 1 for trailing space
current_x += label_width + 1;
}
let line = Line::from(spans);
let paragraph = Paragraph::new(line).style(Style::default().bg(theme.menu_bg));
frame.render_widget(paragraph, area);
// Render dropdown if a menu is active
if let Some(active_idx) = menu_state.active_menu {
if let Some(menu) = all_menus.get(active_idx) {
Self::render_dropdown_chain(
frame,
area,
menu,
menu_state,
active_idx,
&all_menus,
keybindings,
theme,
hover_target,
&mut layout,
);
}
}
layout
}
/// Render a dropdown menu and all its open submenus
#[allow(clippy::too_many_arguments)]
fn render_dropdown_chain(
frame: &mut Frame,
menu_bar_area: Rect,
menu: &Menu,
menu_state: &MenuState,
menu_index: usize,
all_menus: &[Menu],
keybindings: &crate::input::keybindings::KeybindingResolver,
theme: &Theme,
hover_target: Option<&crate::app::HoverTarget>,
layout: &mut MenuLayout,
) {
// Calculate the x position of the top-level dropdown based on menu index
// Skip hidden menus (those with `when` conditions that evaluate to false)
let mut x_offset = 0usize;
for (idx, m) in all_menus.iter().enumerate() {
if idx == menu_index {
break;
}
// Only count visible menus
let is_visible = match &m.when {
Some(condition) => menu_state.context.get(condition),
None => true,
};
if is_visible {
x_offset += str_width(&m.label) + 3; // label + spaces
}
}
let terminal_width = frame.area().width;
let terminal_height = frame.area().height;
// Track dropdown positions for rendering submenus
let mut current_items: &[MenuItem] = &menu.items;
let mut current_x = menu_bar_area.x.saturating_add(x_offset as u16);
let mut current_y = menu_bar_area.y.saturating_add(1);
// Render the main dropdown and collect submenu rendering info
// We'll render depth 0, then 1, etc.
for depth in 0..=menu_state.submenu_path.len() {
let is_deepest = depth == menu_state.submenu_path.len();
let highlighted_item = if is_deepest {
menu_state.highlighted_item
} else {
Some(menu_state.submenu_path[depth])
};
// Render this dropdown level
let dropdown_rect = Self::render_dropdown_level(
frame,
current_items,
highlighted_item,
current_x,
current_y,
terminal_width,
terminal_height,
depth,
&menu_state.submenu_path,
menu_index,
keybindings,
theme,
hover_target,
&menu_state.context,
layout,
);
// If not at the deepest level, navigate into the submenu for next iteration
if !is_deepest {
let submenu_idx = menu_state.submenu_path[depth];
// Handle both Submenu and DynamicSubmenu
let submenu_items = match current_items.get(submenu_idx) {
Some(MenuItem::Submenu { items, .. }) => Some(items.as_slice()),
Some(MenuItem::DynamicSubmenu { .. }) => {
// DynamicSubmenu items will be generated and stored temporarily
// This case shouldn't happen in normal flow since we expand before entering
None
}
_ => None,
};
if let Some(items) = submenu_items {
current_items = items;
// Position submenu to the right of parent, aligned with the highlighted item
current_x = dropdown_rect
.x
.saturating_add(dropdown_rect.width.saturating_sub(1));
current_y = dropdown_rect.y.saturating_add(submenu_idx as u16 + 1); // +1 for border
// Adjust if submenu would go off screen to the right - flip to left side
let next_width = Self::calculate_dropdown_width(items);
if current_x.saturating_add(next_width as u16) > terminal_width {
current_x = dropdown_rect
.x
.saturating_sub(next_width as u16)
.saturating_add(1);
}
} else {
break;
}
}
}
}
/// Calculate the width needed for a dropdown containing the given items
fn calculate_dropdown_width(items: &[MenuItem]) -> usize {
items
.iter()
.map(|item| match item {
MenuItem::Action { label, .. } => str_width(label) + 20,
MenuItem::Submenu { label, .. } => str_width(label) + 20,
MenuItem::DynamicSubmenu { label, .. } => str_width(label) + 20,
MenuItem::Separator { .. } => 20,
MenuItem::Label { info } => str_width(info) + 4,
})
.max()
.unwrap_or(20)
.min(40)
}
/// Render a single dropdown level and return its bounding Rect
#[allow(clippy::too_many_arguments)]
fn render_dropdown_level(
frame: &mut Frame,
items: &[MenuItem],
highlighted_item: Option<usize>,
x: u16,
y: u16,
terminal_width: u16,
terminal_height: u16,
depth: usize,
submenu_path: &[usize],
menu_index: usize,
keybindings: &crate::input::keybindings::KeybindingResolver,
theme: &Theme,
hover_target: Option<&crate::app::HoverTarget>,
context: &MenuContext,
layout: &mut MenuLayout,
) -> Rect {
let max_width = Self::calculate_dropdown_width(items);
let dropdown_height = items.len() + 2; // +2 for borders
let desired_width = max_width as u16;
let desired_height = dropdown_height as u16;
// Bounds check: ensure dropdown fits within the visible area
let adjusted_x = if x.saturating_add(desired_width) > terminal_width {
terminal_width.saturating_sub(desired_width)
} else {
x
};
let available_height = terminal_height.saturating_sub(y);
let height = desired_height.min(available_height);
let available_width = terminal_width.saturating_sub(adjusted_x);
let width = desired_width.min(available_width);
// Only render if we have at least minimal space
if width < 10 || height < 3 {
return Rect {
x: adjusted_x,
y,
width,
height,
};
}
let dropdown_area = Rect {
x: adjusted_x,
y,
width,
height,
};
// Build dropdown content
let mut lines = Vec::new();
let max_items = (height.saturating_sub(2)) as usize;
let items_to_show = items.len().min(max_items);
let content_width = (width as usize).saturating_sub(2);
for (idx, item) in items.iter().enumerate().take(items_to_show) {
let is_highlighted = highlighted_item == Some(idx);
// Check if this item is in the submenu path (has an open child submenu)
let has_open_submenu = depth < submenu_path.len() && submenu_path[depth] == idx;
// For hover target matching at submenu levels
let is_hovered = if depth == 0 {
matches!(
hover_target,
Some(crate::app::HoverTarget::MenuDropdownItem(mi, ii)) if *mi == menu_index && *ii == idx
)
} else {
matches!(
hover_target,
Some(crate::app::HoverTarget::SubmenuItem(d, ii)) if *d == depth && *ii == idx
)
};
let enabled = is_menu_item_enabled(item, context);
// Track item area for hit testing
// Item position: inside border (x+1, y+1+idx), full content width
let item_area = Rect::new(adjusted_x + 1, y + 1 + idx as u16, content_width as u16, 1);
if depth == 0 {
layout.item_areas.push((idx, item_area));
} else {
layout.submenu_areas.push((depth, idx, item_area));
}
let line = match item {
MenuItem::Action {
label,
action,
checkbox,
..
} => {
let style = if !enabled {
Style::default()
.fg(theme.menu_disabled_fg)
.bg(theme.menu_disabled_bg)
} else if is_highlighted {
Style::default()
.fg(theme.menu_highlight_fg)
.bg(theme.menu_highlight_bg)
} else if is_hovered {
Style::default()
.fg(theme.menu_hover_fg)
.bg(theme.menu_hover_bg)
} else {
Style::default()
.fg(theme.menu_dropdown_fg)
.bg(theme.menu_dropdown_bg)
};
let keybinding = keybindings
.find_keybinding_for_action(
action,
crate::input::keybindings::KeyContext::Normal,
)
.unwrap_or_default();
let checkbox_icon = if checkbox.is_some() {
if is_checkbox_checked(checkbox, context) {
"☑ "
} else {
"☐ "
}
} else {
""
};
let checkbox_width = if checkbox.is_some() { 2 } else { 0 };
let label_display_width = str_width(label);
let keybinding_display_width = str_width(&keybinding);
let text = if keybinding.is_empty() {
let padding_needed =
content_width.saturating_sub(checkbox_width + label_display_width + 1);
format!(" {}{}{}", checkbox_icon, label, " ".repeat(padding_needed))
} else {
let padding_needed = content_width.saturating_sub(
checkbox_width + label_display_width + keybinding_display_width + 2,
);
format!(
" {}{}{} {}",
checkbox_icon,
label,
" ".repeat(padding_needed),
keybinding
)
};
Line::from(vec![Span::styled(text, style)])
}
MenuItem::Separator { .. } => {
let separator = "─".repeat(content_width);
Line::from(vec![Span::styled(
format!(" {separator}"),
Style::default()
.fg(theme.menu_separator_fg)
.bg(theme.menu_dropdown_bg),
)])
}
MenuItem::Submenu { label, .. } | MenuItem::DynamicSubmenu { label, .. } => {
// Highlight submenu items that have an open child
let style = if is_highlighted || has_open_submenu {
Style::default()
.fg(theme.menu_highlight_fg)
.bg(theme.menu_highlight_bg)
} else if is_hovered {
Style::default()
.fg(theme.menu_hover_fg)
.bg(theme.menu_hover_bg)
} else {
Style::default()
.fg(theme.menu_dropdown_fg)
.bg(theme.menu_dropdown_bg)
};
// Format: " Label > " - label left-aligned, arrow near the end with padding
// content_width minus: leading space (1) + space before arrow (1) + arrow (1) + trailing space (2)
let label_display_width = str_width(label);
let padding_needed = content_width.saturating_sub(label_display_width + 5);
Line::from(vec![Span::styled(
format!(" {}{} > ", label, " ".repeat(padding_needed)),
style,
)])
}
MenuItem::Label { info } => {
// Disabled info label - always shown in disabled style
let style = Style::default()
.fg(theme.menu_disabled_fg)
.bg(theme.menu_dropdown_bg);
let info_display_width = str_width(info);
let padding_needed = content_width.saturating_sub(info_display_width);
Line::from(vec![Span::styled(
format!(" {}{}", info, " ".repeat(padding_needed)),
style,
)])
}
};
lines.push(line);
}
let block = Block::default()
.borders(Borders::ALL)
.border_style(Style::default().fg(theme.menu_border_fg))
.style(Style::default().bg(theme.menu_dropdown_bg));
let paragraph = Paragraph::new(lines).block(block);
frame.render_widget(paragraph, dropdown_area);
dropdown_area
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::collections::HashMap;
fn create_test_menus() -> Vec<Menu> {
vec![
Menu {
id: None,
label: "File".to_string(),
items: vec![
MenuItem::Action {
label: "New".to_string(),
action: "new_file".to_string(),
args: HashMap::new(),
when: None,
checkbox: None,
},
MenuItem::Separator { separator: true },
MenuItem::Action {
label: "Save".to_string(),
action: "save".to_string(),
args: HashMap::new(),
when: None,
checkbox: None,
},
MenuItem::Action {
label: "Quit".to_string(),
action: "quit".to_string(),
args: HashMap::new(),
when: None,
checkbox: None,
},
],
when: None,
},
Menu {
id: None,
label: "Edit".to_string(),
items: vec![
MenuItem::Action {
label: "Undo".to_string(),
action: "undo".to_string(),
args: HashMap::new(),
when: None,
checkbox: None,
},
MenuItem::Action {
label: "Redo".to_string(),
action: "redo".to_string(),
args: HashMap::new(),
when: None,
checkbox: None,
},
],
when: None,
},
Menu {
id: None,
label: "View".to_string(),
items: vec![MenuItem::Action {
label: "Toggle Explorer".to_string(),
action: "toggle_file_explorer".to_string(),
args: HashMap::new(),
when: None,
checkbox: None,
}],
when: None,
},
]
}
#[test]
fn test_menu_state_default() {
let state = MenuState::new();
assert_eq!(state.active_menu, None);
assert_eq!(state.highlighted_item, None);
assert!(state.plugin_menus.is_empty());
}
#[test]
fn test_menu_state_open_menu() {
let mut state = MenuState::new();
state.open_menu(2);
assert_eq!(state.active_menu, Some(2));
assert_eq!(state.highlighted_item, Some(0));
}
#[test]
fn test_menu_state_close_menu() {
let mut state = MenuState::new();
state.open_menu(1);
state.close_menu();
assert_eq!(state.active_menu, None);
assert_eq!(state.highlighted_item, None);
}
#[test]
fn test_menu_state_next_menu() {
let mut state = MenuState::new();
let menus = create_test_menus();
state.open_menu(0);
state.next_menu(&menus);
assert_eq!(state.active_menu, Some(1));
state.next_menu(&menus);
assert_eq!(state.active_menu, Some(2));
// Wrap around
state.next_menu(&menus);
assert_eq!(state.active_menu, Some(0));
}
#[test]
fn test_menu_state_prev_menu() {
let mut state = MenuState::new();
let menus = create_test_menus();
state.open_menu(0);
// Wrap around backwards
state.prev_menu(&menus);
assert_eq!(state.active_menu, Some(2));
state.prev_menu(&menus);
assert_eq!(state.active_menu, Some(1));
state.prev_menu(&menus);
assert_eq!(state.active_menu, Some(0));
}
#[test]
fn test_menu_state_next_item_skips_separator() {
let mut state = MenuState::new();
let menus = create_test_menus();
state.open_menu(0);
// highlighted_item starts at 0 (New)
assert_eq!(state.highlighted_item, Some(0));
// Next should skip separator and go to Save (index 2)
state.next_item(&menus[0]);
assert_eq!(state.highlighted_item, Some(2));
// Next goes to Quit (index 3)
state.next_item(&menus[0]);
assert_eq!(state.highlighted_item, Some(3));
// Wrap around to New (index 0)
state.next_item(&menus[0]);
assert_eq!(state.highlighted_item, Some(0));
}
#[test]
fn test_menu_state_prev_item_skips_separator() {
let mut state = MenuState::new();
let menus = create_test_menus();
state.open_menu(0);
state.highlighted_item = Some(2); // Start at Save
// Prev should skip separator and go to New (index 0)
state.prev_item(&menus[0]);
assert_eq!(state.highlighted_item, Some(0));
// Wrap around backwards to Quit (index 3)
state.prev_item(&menus[0]);
assert_eq!(state.highlighted_item, Some(3));
}
#[test]
fn test_get_highlighted_action() {
let mut state = MenuState::new();
let menus = create_test_menus();
state.open_menu(0);
state.highlighted_item = Some(2); // Save action
let action = state.get_highlighted_action(&menus);
assert!(action.is_some());
let (action_name, _args) = action.unwrap();
assert_eq!(action_name, "save");
}
#[test]
fn test_menu_item_when_requires_selection() {
let mut state = MenuState::new();
let select_menu = Menu {
id: None,
label: "Edit".to_string(),
items: vec![MenuItem::Action {
label: "Find in Selection".to_string(),
action: "find_in_selection".to_string(),
args: HashMap::new(),
when: Some(context_keys::HAS_SELECTION.to_string()),
checkbox: None,
}],
when: None,
};
state.open_menu(0);
state.highlighted_item = Some(0);
// Without has_selection set, action should be disabled
assert!(state
.get_highlighted_action(std::slice::from_ref(&select_menu))
.is_none());
// With has_selection set to true, action should be enabled
state.context.set(context_keys::HAS_SELECTION, true);
assert!(state.get_highlighted_action(&[select_menu]).is_some());
}
#[test]
fn test_get_highlighted_action_none_when_closed() {
let state = MenuState::new();
let menus = create_test_menus();
assert!(state.get_highlighted_action(&menus).is_none());
}
#[test]
fn test_get_highlighted_action_none_for_separator() {
let mut state = MenuState::new();
let menus = create_test_menus();
state.open_menu(0);
state.highlighted_item = Some(1); // Separator
assert!(state.get_highlighted_action(&menus).is_none());
}
#[test]
fn test_menu_layout_menu_at() {
// Build a layout manually simulating what render would produce
let bar_area = Rect::new(0, 0, 80, 1);
let mut layout = MenuLayout::new(bar_area);
// " File " at x=0, width=6
layout.menu_areas.push((0, Rect::new(0, 0, 6, 1)));
// " Edit " at x=7, width=6
layout.menu_areas.push((1, Rect::new(7, 0, 6, 1)));
// " View " at x=14, width=6
layout.menu_areas.push((2, Rect::new(14, 0, 6, 1)));
// File: x=0-5, y=0
assert_eq!(layout.menu_at(0, 0), Some(0));
assert_eq!(layout.menu_at(3, 0), Some(0));
assert_eq!(layout.menu_at(5, 0), Some(0));
// Space between: x=6
assert_eq!(layout.menu_at(6, 0), None);
// Edit: x=7-12
assert_eq!(layout.menu_at(7, 0), Some(1));
assert_eq!(layout.menu_at(10, 0), Some(1));
assert_eq!(layout.menu_at(12, 0), Some(1));
// Space between: x=13
assert_eq!(layout.menu_at(13, 0), None);
// View: x=14-19
assert_eq!(layout.menu_at(14, 0), Some(2));
assert_eq!(layout.menu_at(17, 0), Some(2));
assert_eq!(layout.menu_at(19, 0), Some(2));
// After View
assert_eq!(layout.menu_at(20, 0), None);
assert_eq!(layout.menu_at(100, 0), None);
// Wrong row returns None
assert_eq!(layout.menu_at(3, 1), None);
}
#[test]
fn test_menu_layout_item_at() {
// Build a layout manually simulating what render would produce
let bar_area = Rect::new(0, 0, 80, 1);
let mut layout = MenuLayout::new(bar_area);
// Dropdown items for File menu at x=1 (inside border), y=2,3,4,5 (inside border)
// Item 0 (New) at y=2
layout.item_areas.push((0, Rect::new(1, 2, 20, 1)));
// Item 1 (Separator) at y=3
layout.item_areas.push((1, Rect::new(1, 3, 20, 1)));
// Item 2 (Save) at y=4
layout.item_areas.push((2, Rect::new(1, 4, 20, 1)));
// Item 3 (Quit) at y=5
layout.item_areas.push((3, Rect::new(1, 5, 20, 1)));
// Menu bar row returns None
assert_eq!(layout.item_at(5, 0), None);
// Border row returns None
assert_eq!(layout.item_at(5, 1), None);
// y=2: New (index 0)
assert_eq!(layout.item_at(5, 2), Some(0));
// y=3: Separator (index 1) - note: layout includes all items, filtering happens elsewhere
assert_eq!(layout.item_at(5, 3), Some(1));
// y=4: Save (index 2)
assert_eq!(layout.item_at(5, 4), Some(2));
// y=5: Quit (index 3)
assert_eq!(layout.item_at(5, 5), Some(3));
// Beyond items
assert_eq!(layout.item_at(5, 6), None);
assert_eq!(layout.item_at(5, 100), None);
}
#[test]
fn test_menu_layout_hit_test() {
let bar_area = Rect::new(0, 0, 80, 1);
let mut layout = MenuLayout::new(bar_area);
// Menu labels
layout.menu_areas.push((0, Rect::new(0, 0, 6, 1)));
// Dropdown items
layout.item_areas.push((0, Rect::new(1, 2, 20, 1)));
layout.item_areas.push((1, Rect::new(1, 3, 20, 1)));
// Submenu items at depth 1
layout.submenu_areas.push((1, 0, Rect::new(22, 3, 15, 1)));
// Hit test menu label
assert_eq!(layout.hit_test(3, 0), Some(MenuHit::MenuLabel(0)));
// Hit test dropdown item
assert_eq!(layout.hit_test(5, 2), Some(MenuHit::DropdownItem(0)));
// Hit test submenu item (should have priority)
assert_eq!(
layout.hit_test(25, 3),
Some(MenuHit::SubmenuItem { depth: 1, index: 0 })
);
// Hit test bar background (inside bar area but not on menu)
assert_eq!(layout.hit_test(50, 0), Some(MenuHit::BarBackground));
// Hit test outside everything
assert_eq!(layout.hit_test(50, 10), None);
}
#[test]
fn test_menu_config_json_parsing() {
let json = r#"{
"menus": [
{
"label": "File",
"items": [
{ "label": "New", "action": "new_file" },
{ "separator": true },
{ "label": "Save", "action": "save" }
]
}
]
}"#;
let config: MenuConfig = serde_json::from_str(json).unwrap();
assert_eq!(config.menus.len(), 1);
assert_eq!(config.menus[0].label, "File");
assert_eq!(config.menus[0].items.len(), 3);
match &config.menus[0].items[0] {
MenuItem::Action { label, action, .. } => {
assert_eq!(label, "New");
assert_eq!(action, "new_file");
}
_ => panic!("Expected Action"),
}
assert!(matches!(
config.menus[0].items[1],
MenuItem::Separator { .. }
));
match &config.menus[0].items[2] {
MenuItem::Action { label, action, .. } => {
assert_eq!(label, "Save");
assert_eq!(action, "save");
}
_ => panic!("Expected Action"),
}
}
#[test]
fn test_menu_item_with_args() {
let json = r#"{
"label": "Go to Line",
"action": "goto_line",
"args": { "line": 42 }
}"#;
let item: MenuItem = serde_json::from_str(json).unwrap();
match item {
MenuItem::Action {
label,
action,
args,
..
} => {
assert_eq!(label, "Go to Line");
assert_eq!(action, "goto_line");
assert_eq!(args.get("line").unwrap().as_i64(), Some(42));
}
_ => panic!("Expected Action with args"),
}
}
#[test]
fn test_empty_menu_config() {
let json = r#"{ "menus": [] }"#;
let config: MenuConfig = serde_json::from_str(json).unwrap();
assert!(config.menus.is_empty());
}
#[test]
fn test_menu_mnemonic_lookup() {
use crate::config::Config;
use crate::input::keybindings::KeybindingResolver;
let config = Config::default();
let resolver = KeybindingResolver::new(&config);
// Check that default Alt+letter bindings are configured
assert_eq!(resolver.find_menu_mnemonic("File"), Some('f'));
assert_eq!(resolver.find_menu_mnemonic("Edit"), Some('e'));
assert_eq!(resolver.find_menu_mnemonic("View"), Some('v'));
assert_eq!(resolver.find_menu_mnemonic("Selection"), Some('s'));
assert_eq!(resolver.find_menu_mnemonic("Go"), Some('g'));
assert_eq!(resolver.find_menu_mnemonic("Help"), Some('h'));
// Case-insensitive matching
assert_eq!(resolver.find_menu_mnemonic("file"), Some('f'));
assert_eq!(resolver.find_menu_mnemonic("FILE"), Some('f'));
// Non-existent menu should return None
assert_eq!(resolver.find_menu_mnemonic("NonExistent"), None);
}
fn create_menu_with_submenus() -> Vec<Menu> {
vec![Menu {
id: None,
label: "View".to_string(),
items: vec![
MenuItem::Action {
label: "Toggle Explorer".to_string(),
action: "toggle_file_explorer".to_string(),
args: HashMap::new(),
when: None,
checkbox: None,
},
MenuItem::Submenu {
label: "Terminal".to_string(),
items: vec![
MenuItem::Action {
label: "Open Terminal".to_string(),
action: "open_terminal".to_string(),
args: HashMap::new(),
when: None,
checkbox: None,
},
MenuItem::Action {
label: "Close Terminal".to_string(),
action: "close_terminal".to_string(),
args: HashMap::new(),
when: None,
checkbox: None,
},
MenuItem::Submenu {
label: "Terminal Settings".to_string(),
items: vec![MenuItem::Action {
label: "Font Size".to_string(),
action: "terminal_font_size".to_string(),
args: HashMap::new(),
when: None,
checkbox: None,
}],
},
],
},
MenuItem::Separator { separator: true },
MenuItem::Action {
label: "Zoom In".to_string(),
action: "zoom_in".to_string(),
args: HashMap::new(),
when: None,
checkbox: None,
},
],
when: None,
}]
}
#[test]
fn test_submenu_open_and_close() {
let mut state = MenuState::new();
let menus = create_menu_with_submenus();
state.open_menu(0);
assert!(state.submenu_path.is_empty());
assert!(!state.in_submenu());
// Move to Terminal submenu item (index 1)
state.highlighted_item = Some(1);
// Open the submenu
assert!(state.open_submenu(&menus));
assert_eq!(state.submenu_path, vec![1]);
assert!(state.in_submenu());
assert_eq!(state.submenu_depth(), 1);
assert_eq!(state.highlighted_item, Some(0)); // Reset to first item
// Close the submenu
assert!(state.close_submenu());
assert!(state.submenu_path.is_empty());
assert!(!state.in_submenu());
assert_eq!(state.highlighted_item, Some(1)); // Restored to parent item
}
#[test]
fn test_nested_submenu() {
let mut state = MenuState::new();
let menus = create_menu_with_submenus();
state.open_menu(0);
state.highlighted_item = Some(1); // Terminal submenu
// Open first level submenu
assert!(state.open_submenu(&menus));
assert_eq!(state.submenu_depth(), 1);
// Move to Terminal Settings (nested submenu at index 2)
state.highlighted_item = Some(2);
// Open second level submenu
assert!(state.open_submenu(&menus));
assert_eq!(state.submenu_path, vec![1, 2]);
assert_eq!(state.submenu_depth(), 2);
assert_eq!(state.highlighted_item, Some(0));
// Close back to first level
assert!(state.close_submenu());
assert_eq!(state.submenu_path, vec![1]);
assert_eq!(state.highlighted_item, Some(2));
// Close back to main menu
assert!(state.close_submenu());
assert!(state.submenu_path.is_empty());
assert_eq!(state.highlighted_item, Some(1));
// Can't close further
assert!(!state.close_submenu());
}
#[test]
fn test_get_highlighted_action_in_submenu() {
let mut state = MenuState::new();
let menus = create_menu_with_submenus();
state.open_menu(0);
state.highlighted_item = Some(1); // Terminal submenu
// On a submenu item, get_highlighted_action should return None
assert!(state.get_highlighted_action(&menus).is_none());
// Open the submenu
state.open_submenu(&menus);
// Now highlighted_item is 0 which is "Open Terminal"
let action = state.get_highlighted_action(&menus);
assert!(action.is_some());
let (action_name, _) = action.unwrap();
assert_eq!(action_name, "open_terminal");
// Navigate to second item
state.highlighted_item = Some(1);
let action = state.get_highlighted_action(&menus);
assert!(action.is_some());
let (action_name, _) = action.unwrap();
assert_eq!(action_name, "close_terminal");
}
#[test]
fn test_get_current_items_at_different_depths() {
let mut state = MenuState::new();
let menus = create_menu_with_submenus();
state.open_menu(0);
// At top level, should get main menu items
let items = state.get_current_items(&menus, 0).unwrap();
assert_eq!(items.len(), 4); // Action, Submenu, Separator, Action
// Open Terminal submenu
state.highlighted_item = Some(1);
state.open_submenu(&menus);
// Now should get Terminal submenu items
let items = state.get_current_items(&menus, 0).unwrap();
assert_eq!(items.len(), 3); // Open, Close, Settings submenu
// Open nested Terminal Settings submenu
state.highlighted_item = Some(2);
state.open_submenu(&menus);
// Now should get Terminal Settings submenu items
let items = state.get_current_items(&menus, 0).unwrap();
assert_eq!(items.len(), 1); // Font Size
}
#[test]
fn test_is_highlighted_submenu() {
let mut state = MenuState::new();
let menus = create_menu_with_submenus();
state.open_menu(0);
state.highlighted_item = Some(0); // Toggle Explorer (action)
assert!(!state.is_highlighted_submenu(&menus));
state.highlighted_item = Some(1); // Terminal (submenu)
assert!(state.is_highlighted_submenu(&menus));
state.highlighted_item = Some(2); // Separator
assert!(!state.is_highlighted_submenu(&menus));
state.highlighted_item = Some(3); // Zoom In (action)
assert!(!state.is_highlighted_submenu(&menus));
}
#[test]
fn test_open_menu_clears_submenu_path() {
let mut state = MenuState::new();
let menus = create_menu_with_submenus();
state.open_menu(0);
state.highlighted_item = Some(1);
state.open_submenu(&menus);
assert!(!state.submenu_path.is_empty());
// Opening a new menu should clear the submenu path
state.open_menu(0);
assert!(state.submenu_path.is_empty());
}
#[test]
fn test_next_prev_menu_clears_submenu_path() {
let mut state = MenuState::new();
let menus = create_menu_with_submenus();
state.open_menu(0);
state.highlighted_item = Some(1);
state.open_submenu(&menus);
assert!(!state.submenu_path.is_empty());
// next_menu should clear submenu path
state.next_menu(&menus);
assert!(state.submenu_path.is_empty());
// Re-open submenu
state.open_menu(0);
state.highlighted_item = Some(1);
state.open_submenu(&menus);
// prev_menu should clear submenu path
state.prev_menu(&menus);
assert!(state.submenu_path.is_empty());
}
#[test]
fn test_navigation_in_submenu() {
let mut state = MenuState::new();
let menus = create_menu_with_submenus();
state.open_menu(0);
state.highlighted_item = Some(1);
state.open_submenu(&menus);
// In Terminal submenu, start at index 0
assert_eq!(state.highlighted_item, Some(0));
// Navigate down
state.next_item(&menus[0]);
assert_eq!(state.highlighted_item, Some(1));
// Navigate down again
state.next_item(&menus[0]);
assert_eq!(state.highlighted_item, Some(2));
// Navigate down wraps to start
state.next_item(&menus[0]);
assert_eq!(state.highlighted_item, Some(0));
// Navigate up wraps to end
state.prev_item(&menus[0]);
assert_eq!(state.highlighted_item, Some(2));
}
/// Helper function to calculate dropdown x offset (mirrors the logic in render_dropdown_chain)
fn calculate_dropdown_x_offset(
all_menus: &[Menu],
menu_index: usize,
context: &MenuContext,
) -> usize {
let mut x_offset = 0usize;
for (idx, m) in all_menus.iter().enumerate() {
if idx == menu_index {
break;
}
// Only count visible menus
let is_visible = match &m.when {
Some(condition) => context.get(condition),
None => true,
};
if is_visible {
x_offset += str_width(&m.label) + 3; // label + spaces
}
}
x_offset
}
#[test]
fn test_dropdown_position_skips_hidden_menus() {
// Create menus: File (always visible), Explorer (conditional), Help (always visible)
let menus = vec![
Menu {
id: None,
label: "File".to_string(), // width 4, total 7 with padding
items: vec![],
when: None,
},
Menu {
id: None,
label: "Explorer".to_string(), // width 8, total 11 with padding
items: vec![],
when: Some("file_explorer_focused".to_string()),
},
Menu {
id: None,
label: "Help".to_string(), // width 4, total 7 with padding
items: vec![],
when: None,
},
];
// When Explorer is hidden, Help dropdown should be at File's width only
let context_hidden = MenuContext::new().with("file_explorer_focused", false);
let x_help_hidden = calculate_dropdown_x_offset(&menus, 2, &context_hidden);
// "File" = 4 chars + 3 spaces = 7
assert_eq!(
x_help_hidden, 7,
"Help dropdown should be at x=7 when Explorer is hidden"
);
// When Explorer is visible, Help dropdown should be at File + Explorer width
let context_visible = MenuContext::new().with("file_explorer_focused", true);
let x_help_visible = calculate_dropdown_x_offset(&menus, 2, &context_visible);
// "File" = 4 chars + 3 spaces = 7, "Explorer" = 8 chars + 3 spaces = 11, total = 18
assert_eq!(
x_help_visible, 18,
"Help dropdown should be at x=18 when Explorer is visible"
);
}
#[test]
fn test_dropdown_position_with_multiple_hidden_menus() {
let menus = vec![
Menu {
id: None,
label: "A".to_string(), // width 1, total 4
items: vec![],
when: None,
},
Menu {
id: None,
label: "B".to_string(), // width 1, total 4
items: vec![],
when: Some("show_b".to_string()),
},
Menu {
id: None,
label: "C".to_string(), // width 1, total 4
items: vec![],
when: Some("show_c".to_string()),
},
Menu {
id: None,
label: "D".to_string(),
items: vec![],
when: None,
},
];
// No conditional menus visible: D should be right after A
let context_none = MenuContext::new();
assert_eq!(calculate_dropdown_x_offset(&menus, 3, &context_none), 4);
// Only B visible: D should be after A + B
let context_b = MenuContext::new().with("show_b", true);
assert_eq!(calculate_dropdown_x_offset(&menus, 3, &context_b), 8);
// Both B and C visible: D should be after A + B + C
let context_both = MenuContext::new().with("show_b", true).with("show_c", true);
assert_eq!(calculate_dropdown_x_offset(&menus, 3, &context_both), 12);
}
}