helix-kanban 0.2.25

A terminal-based kanban board with file-based storage, multi-project support, Helix-style keybindings, and built-in MCP server for AI integration
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
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
use crate::app::{App, Mode};
use crate::input::Command;
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};

/// 处理键盘输入
/// 返回 false 表示应该退出应用
pub fn handle_key_input(app: &mut App, key: KeyEvent) -> bool {
    // 如果显示欢迎对话框,任意按键都关闭它
    if app.show_welcome_dialog {
        app.show_welcome_dialog = false;
        return true;
    }

    match app.mode {
        Mode::Normal => handle_normal_mode(app, key),
        // Mode::Command => handle_command_mode(app, key), // 已注释
        Mode::TaskSelect => handle_task_select_mode(app, key),
        Mode::Dialog => handle_dialog_mode(app, key),
        Mode::Help => handle_help_mode(app, key),
        Mode::SpaceMenu => handle_space_menu_mode(app, key),
        Mode::Preview => handle_preview_mode(app, key),
    }
}

/// 处理正常模式的按键
fn handle_normal_mode(app: &mut App, key: KeyEvent) -> bool {
    // 特殊处理帮助键
    if let KeyCode::Char('?') = key.code {
        app.mode = Mode::Help;
        app.key_buffer.clear();
        return true;
    }

    // 特殊处理空格键 - 显示命令菜单
    if let KeyCode::Char(' ') = key.code {
        // 空格键总是清空缓冲区并显示菜单
        app.key_buffer.clear();
        app.mode = Mode::SpaceMenu;
        app.menu_state = Some(crate::app::MenuState::Main);
        app.menu_selected_index = Some(0); // 初始化选中第一项
        return true;
    }

    // 尝试匹配命令(使用当前缓冲区和新按键)
    if let Some(cmd) = match_key_sequence(&app.key_buffer, key) {
        app.key_buffer.clear();

        // 特殊处理退出命令
        if cmd == Command::Quit {
            return false;
        }

        execute_command(app, cmd);
        return true;
    }

    // 如果没有匹配到命令,清空缓冲区
    // 因为现在所有多键序列都通过 SpaceMenu 处理,不需要缓冲未匹配的按键
    app.key_buffer.clear();

    true
}

// /// 处理命令模式的按键 - 已注释
// fn handle_command_mode(app: &mut App, key: KeyEvent) -> bool {
//     match key.code {
//         KeyCode::Esc => {
//             app.mode = Mode::Normal;
//             app.command_input.clear();
//             app.completion_selected_index = None;
//         }
//         KeyCode::Tab => {
//             // 下一个补全项
//             let matches = app.command_registry.find_matches(&app.command_input);
//             if !matches.is_empty() {
//                 let current = app.completion_selected_index.unwrap_or(0);
//                 let next = if current + 1 >= matches.len() { 0 } else { current + 1 };
//                 app.completion_selected_index = Some(next);
//             }
//         }
//         KeyCode::BackTab => {
//             // 上一个补全项
//             let matches = app.command_registry.find_matches(&app.command_input);
//             if !matches.is_empty() {
//                 let current = app.completion_selected_index.unwrap_or(0);
//                 let prev = if current == 0 { matches.len() - 1 } else { current - 1 };
//                 app.completion_selected_index = Some(prev);
//             }
//         }
//         KeyCode::Enter => {
//             // 执行命令
//             let should_continue = execute_text_command(app, &app.command_input.clone());
//             app.command_input.clear();
//             app.mode = Mode::Normal;
//             app.completion_selected_index = None;
//             return should_continue;
//         }
//         KeyCode::Backspace => {
//             app.command_input.pop();
//         }
//         KeyCode::Char(c) => {
//             app.command_input.push(c);
//         }
//         _ => {}
//     }
//     true
// }

/// 处理任务选择模式的按键
fn handle_task_select_mode(app: &mut App, key: KeyEvent) -> bool {
    match key.code {
        KeyCode::Esc => {
            app.mode = Mode::Normal;
        }
         KeyCode::Down => {
            execute_command(app, Command::TaskDown);
        }
         KeyCode::Up => {
            execute_command(app, Command::TaskUp);
        }
        _ => {}
    }
    true
}

/// 处理对话框模式的按键
fn handle_dialog_mode(app: &mut App, key: KeyEvent) -> bool {
    use crate::ui::dialogs::DialogType;
    use crate::ui::text_input::InputAction;

    if let Some(dialog) = &mut app.dialog {
        match dialog {
            DialogType::Input { textarea, .. } => {
                // 使用 HelixTextArea 处理按键
                match textarea.handle_key(key) {
                    InputAction::Submit => {
                        // 提交内容
                        let content = textarea.get_content();
                        let dialog_clone = app.dialog.take().unwrap();
                        handle_dialog_submit(app, dialog_clone, content);
                        app.mode = Mode::Normal;
                        // app.ime_state.exit_dialog();  // 已禁用输入法自动切换
                        return true;
                    }
                    InputAction::Cancel => {
                        // 取消对话框
                        app.dialog = None;
                        app.mode = Mode::Normal;
                        // app.ime_state.exit_dialog();  // 已禁用输入法自动切换
                        return true;
                    }
                    InputAction::Continue => {
                        // 继续编辑
                        return true;
                    }
                }
            }
            DialogType::Select {
                items,
                selected,
                filter,
                ..
            } => {
                match key.code {
                    KeyCode::Esc => {
                        app.dialog = None;
                        app.mode = Mode::Normal;
                        // 退出对话框,保存用户输入法并切换回英文(已禁用)
                        // app.ime_state.exit_dialog();
                    }
                    KeyCode::Enter => {
                        // 选择当前项
                        let filtered_items: Vec<_> = if filter.is_empty() {
                            items.clone()
                        } else {
                            items
                                .iter()
                                .filter(|item| item.to_lowercase().contains(&filter.to_lowercase()))
                                .cloned()
                                .collect()
                        };

                        if *selected < filtered_items.len() {
                            let selected_item = filtered_items[*selected].clone();
                            let dialog_clone = app.dialog.take().unwrap();
                            app.mode = Mode::Normal;
                            // app.ime_state.exit_dialog();  // 已禁用输入法自动切换
                            handle_dialog_submit(app, dialog_clone, selected_item);
                            return true;
                        }
                    }
                    KeyCode::Up => {
                        // 向上移动选择
                        if *selected > 0 {
                            *selected -= 1;
                        }
                    }
                    KeyCode::Down => {
                        // 向下移动选择
                        let filtered_count = if filter.is_empty() {
                            items.len()
                        } else {
                            items
                                .iter()
                                .filter(|item| item.to_lowercase().contains(&filter.to_lowercase()))
                                .count()
                        };

                        if filtered_count > 0 && *selected < filtered_count - 1 {
                            *selected += 1;
                        }
                    }
                    KeyCode::Backspace => {
                        filter.pop();
                        *selected = 0;
                    }
                    KeyCode::Char(c) => {
                        filter.push(c);
                        *selected = 0;
                    }
                    _ => {}
                }
            }
            DialogType::Confirm { yes_selected, .. } => {
                match key.code {
                    KeyCode::Esc | KeyCode::Char('n') => {
                        app.dialog = None;
                        app.mode = Mode::Normal;
                        // 退出对话框,保存用户输入法并切换回英文(已禁用)
                        // app.ime_state.exit_dialog();
                    }
                    KeyCode::Enter => {
                        let confirmed = *yes_selected;
                        let dialog_clone = app.dialog.take().unwrap();
                        app.mode = Mode::Normal;
                        // app.ime_state.exit_dialog();  // 已禁用输入法自动切换
                        if confirmed {
                            handle_dialog_submit(app, dialog_clone, String::new());
                        }
                        return true;
                    }
                    KeyCode::Left | KeyCode::Char('h') => {
                        *yes_selected = false; // 左边是"否"
                    }
                    KeyCode::Right | KeyCode::Char('l') => {
                        *yes_selected = true; // 右边是"是"
                    }
                    KeyCode::Char('y') => {
                        *yes_selected = true;
                        // 直接确认
                        let dialog_clone = app.dialog.take().unwrap();
                        app.mode = Mode::Normal;
                        // app.ime_state.exit_dialog();  // 已禁用输入法自动切换
                        handle_dialog_submit(app, dialog_clone, String::new());
                        return true;
                    }
                    _ => {}
                }
            }
        }
    }

    true
}

/// 调试日志辅助函数
fn log_debug(msg: String) {
    use std::fs::OpenOptions;
    use std::io::Write;

    if let Ok(mut file) = OpenOptions::new()
        .create(true)
        .append(true)
        .open("/tmp/kanban_debug.log")
    {
        let _ = writeln!(
            file,
            "[{}] {}",
            chrono::Local::now().format("%H:%M:%S"),
            msg
        );
    }
    // 移除 eprintln! 避免干扰 TUI 界面
}

/// 处理对话框提交
fn handle_dialog_submit(app: &mut App, dialog: crate::ui::dialogs::DialogType, value: String) {
    use crate::ui::dialogs::DialogType;

    match dialog {
        DialogType::Input { title, .. } => {
            log_debug(format!("对话框提交: title='{}', value='{}'", title, value));

            if (title.contains("创建") || title.contains("新建")) && title.contains("项目") {
                // 创建新项目
                if !value.is_empty() {
                    log_debug(format!("调试: 准备创建项目 '{}'", value));

                    // 根据标题判断是本地项目还是全局项目
                    let is_local = title.contains("[L]");
                    let is_global = title.contains("[G]");

                    let result = if is_local {
                        // 创建本地项目
                        log_debug("调试: 创建本地项目".to_string());
                        crate::fs::create_local_project(&value)
                    } else if is_global {
                        // 创建全局项目
                        log_debug("调试: 创建全局项目".to_string());
                        crate::fs::create_project(&value)
                    } else {
                        // 默认创建全局项目(向后兼容)
                        log_debug("调试: 创建默认项目(全局)".to_string());
                        crate::fs::create_project(&value)
                    };

                    match result {
                        Ok(path) => {
                            log_debug(format!("调试: 项目创建成功于 {:?}", path));
                            // 重新加载项目列表
                            match crate::fs::load_all_projects() {
                                Ok(projects) => {
                                    log_debug(format!(
                                        "调试: 重新加载了 {} 个项目",
                                        projects.len()
                                    ));
                                    app.projects = projects;
                                }
                                Err(e) => {
                                    log_debug(format!("调试: 重新加载项目失败: {}", e));
                                }
                            }
                            // 在当前面板打开新项目
                            app.set_focused_project(value);
                        }
                        Err(e) => {
                            log_debug(format!("创建项目失败: {}", e));
                        }
                    }
                } else {
                    log_debug("调试: 项目名称为空".to_string());
                }
            } else if (title.contains("创建") || title.contains("新建")) && title.contains("任务")
            {
                // 创建新任务
                log_debug("调试: 识别为创建任务请求".to_string());
                if !value.is_empty() {
                    create_new_task(app, value);
                } else {
                    log_debug("调试: 任务标题为空".to_string());
                }
            } else if title.contains("编辑任务") {
                // 编辑任务
                if !value.is_empty() {
                    update_task_title(app, value);
                }
            } else if title.contains("编辑标签") {
                // 编辑标签
                update_task_tags(app, value);
            } else if title.contains("重命名项目") {
                // 重命名项目
                if !value.is_empty() {
                    rename_current_project(app, value);
                }
            } else if title.contains("创建新状态") {
                // 创建新状态
                if !value.is_empty()
                    && let Some(project) = app.get_focused_project() {
                        let project_path = project.path.clone();

                        // 使用输入值作为显示名
                        match crate::fs::status::create_status(&project_path, &value, &value) {
                            Ok(_) => {
                                // 重新加载项目
                                if let Err(e) = app.reload_current_project() {
                                    log_debug(format!("重新加载项目失败: {}", e));
                                }
                                app.show_notification(
                                    format!("已创建状态「{}", value),
                                    crate::app::NotificationLevel::Success,
                                );
                            }
                            Err(e) => {
                                app.show_notification(
                                    format!("创建失败: {}", e),
                                    crate::app::NotificationLevel::Error,
                                );
                            }
                        }
                    }
            } else if title.contains("重命名状态") {
                // 重命名状态
                if !value.is_empty()
                    && let Some(project) = app.get_focused_project() {
                        let column = app
                            .selected_column
                            .get(&app.focused_pane)
                            .copied()
                            .unwrap_or(0);
                        if let Some(status) = project.statuses.get(column) {
                            let old_name = status.name.clone();
                            let old_display = status.display.clone();
                            let project_path = project.path.clone();

                            match crate::fs::status::rename_status(
                                &project_path,
                                &old_name,
                                &value,
                                &value,
                            ) {
                                Ok(_) => {
                                    // 重新加载项目
                                    if let Err(e) = app.reload_current_project() {
                                        log_debug(format!("重新加载项目失败: {}", e));
                                    }
                                    app.show_notification(
                                        format!("已将「{}」重命名为「{}", old_display, value),
                                        crate::app::NotificationLevel::Success,
                                    );
                                }
                                Err(e) => {
                                    app.show_notification(
                                        format!("重命名失败: {}", e),
                                        crate::app::NotificationLevel::Error,
                                    );
                                }
                            }
                        }
                    }
            } else if title.contains("编辑显示名") {
                // 编辑状态显示名
                if !value.is_empty()
                    && let Some(project) = app.get_focused_project() {
                        let column = app
                            .selected_column
                            .get(&app.focused_pane)
                            .copied()
                            .unwrap_or(0);
                        if let Some(status) = project.statuses.get(column) {
                            let status_name = status.name.clone();
                            let project_path = project.path.clone();

                            match crate::fs::status::update_status_display(
                                &project_path,
                                &status_name,
                                &value,
                            ) {
                                Ok(_) => {
                                    // 重新加载项目
                                    if let Err(e) = app.reload_current_project() {
                                        log_debug(format!("重新加载项目失败: {}", e));
                                    }
                                    app.show_notification(
                                        format!("已更新显示名为「{}", value),
                                        crate::app::NotificationLevel::Success,
                                    );
                                }
                                Err(e) => {
                                    app.show_notification(
                                        format!("更新失败: {}", e),
                                        crate::app::NotificationLevel::Error,
                                    );
                                }
                            }
                        }
                    }
            }
        }
        DialogType::Select { title, .. } => {
            if title.contains("选择项目")
                || title.contains("打开项目")
                || title.contains("切换项目")
            {
                // 从格式化的字符串中提取项目名
                // 格式: "[G/L] 项目名\n    路径"
                let project_name = value
                    .lines()
                    .next()
                    .unwrap_or(&value)
                    .trim_start_matches("[G] ")
                    .trim_start_matches("[L] ")
                    .trim();

                // 打开选中的项目
                app.set_focused_project(project_name.to_string());
            }
        }
        DialogType::Confirm { action, .. } => {
            match action {
                crate::ui::dialogs::ConfirmAction::HideProject => {
                    // 隐藏项目(软删除)
                    if let Some(project) = app.get_focused_project() {
                        let project_name = project.name.clone();

                        // 添加到隐藏列表
                        if let Err(e) = crate::config::hide_project(&mut app.config, &project_name)
                        {
                            log_debug(format!("隐藏项目失败: {}", e));
                        } else {
                            log_debug(format!("成功隐藏项目: {}", project_name));

                            // 从项目列表中移除
                            app.projects.retain(|p| p.name != project_name);

                            // 清除当前面板的项目引用
                            if let Some(crate::ui::layout::SplitNode::Leaf { project_id, .. }) =
                                app.split_tree.find_pane_mut(app.focused_pane)
                            {
                                *project_id = None;
                            }
                        }
                    }
                }
                crate::ui::dialogs::ConfirmAction::DeleteProject => {
                    // 删除项目文件(硬删除)
                    log_debug("收到 DeleteProject 确认".to_string());
                    if let Some(project) = app.get_focused_project() {
                        let project_name = project.name.clone();
                        let project_path = project.path.clone();

                        log_debug(format!(
                            "准备删除项目: 名称='{}', 路径={:?}",
                            project_name, project_path
                        ));

                        // 使用项目路径直接删除
                        match crate::fs::delete_project_by_path(&project_path) {
                            Err(e) => {
                                log_debug(format!("删除项目失败: {}", e));
                                app.show_notification(
                                    format!("删除项目失败: {}", e),
                                    crate::app::NotificationLevel::Error,
                                );
                            }
                            Ok(_) => {
                                log_debug(format!("成功删除项目: {}", project_name));

                                // 从项目列表中移除
                                app.projects.retain(|p| p.name != project_name);
                                log_debug(format!(
                                    "已从项目列表移除,剩余项目数: {}",
                                    app.projects.len()
                                ));

                                // 清除所有面板中对该项目的引用
                                app.split_tree.clear_project_from_all_panes(&project_name);
                                log_debug("已清除所有面板中的项目引用".to_string());

                                // 显示删除成功通知
                                app.show_notification(
                                    format!("已删除项目: {}", project_name),
                                    crate::app::NotificationLevel::Success,
                                );
                            }
                        }
                    } else {
                        log_debug("无法获取当前聚焦的项目".to_string());
                    }
                }
                crate::ui::dialogs::ConfirmAction::DeleteTask => {
                    // 删除任务
                    if let Some(task) = get_selected_task(app) {
                        // 获取项目路径
                        if let Some(project) = app.get_focused_project() {
                            let project_path = project.path.clone();

                            // 删除任务(包括文件和 tasks.toml 中的元数据)
                            if let Err(e) = crate::fs::delete_task(&project_path, task) {
                                log_debug(format!("删除任务失败: {}", e));
                            } else {
                                // 重新加载当前项目
                                if let Err(e) = app.reload_current_project() {
                                    log_debug(format!("重新加载项目失败: {}", e));
                                }

                                // 调整选中的任务索引
                                let task_idx =
                                    app.selected_task_index.entry(app.focused_pane).or_insert(0);
                                if *task_idx > 0 {
                                    *task_idx -= 1;
                                }
                            }
                        }
                    }
                }
                crate::ui::dialogs::ConfirmAction::DeleteStatus => {
                    // 删除状态
                    if let Some(project) = app.get_focused_project() {
                        let column = app
                            .selected_column
                            .get(&app.focused_pane)
                            .copied()
                            .unwrap_or(0);
                        if let Some(status) = project.statuses.get(column) {
                            let status_name = status.name.clone();
                            let status_display = status.display.clone();
                            let project_path = project.path.clone();

                            match crate::fs::status::delete_status(
                                &project_path,
                                &status_name,
                                None,
                            ) {
                                Ok(_) => {
                                    log_debug(format!("成功删除状态: {}", status_name));

                                    // 重新加载项目
                                    if let Err(e) = app.reload_current_project() {
                                        log_debug(format!("重新加载项目失败: {}", e));
                                    }

                                    // 调整选中列到第一列
                                    app.selected_column.insert(app.focused_pane, 0);

                                    app.show_notification(
                                        format!("已删除状态「{}", status_display),
                                        crate::app::NotificationLevel::Success,
                                    );
                                }
                                Err(e) => {
                                    log_debug(format!("删除状态失败: {}", e));
                                    app.show_notification(
                                        format!("删除失败: {}", e),
                                        crate::app::NotificationLevel::Error,
                                    );
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

/// 匹配键序列到命令
/// buffer: 之前按过的键(不包含当前键)
/// key: 当前正在按的键
pub fn match_key_sequence(buffer: &[char], key: KeyEvent) -> Option<Command> {
    match (buffer, key.code, key.modifiers) {
        // ===== 单键命令(空缓冲区)=====
        // 注意:移除了 'q' 键退出,改用 ':q' 命令或 Space q
        ([], KeyCode::Char('j'), KeyModifiers::NONE) => Some(Command::TaskDown),
        ([], KeyCode::Char('k'), KeyModifiers::NONE) => Some(Command::TaskUp),
        ([], KeyCode::Char('h'), KeyModifiers::NONE) => Some(Command::ColumnLeft),
        ([], KeyCode::Char('l'), KeyModifiers::NONE) => Some(Command::ColumnRight),
        ([], KeyCode::Char('H'), KeyModifiers::SHIFT) => Some(Command::MoveTaskLeft),
        ([], KeyCode::Char('L'), KeyModifiers::SHIFT) => Some(Command::MoveTaskRight),
        ([], KeyCode::Char('J'), KeyModifiers::SHIFT) => Some(Command::MoveTaskDown),
        ([], KeyCode::Char('K'), KeyModifiers::SHIFT) => Some(Command::MoveTaskUp),
        // ([], KeyCode::Char(':'), KeyModifiers::NONE) => Some(Command::EnterCommandMode), // 已注释
        ([], KeyCode::Esc, _) => Some(Command::EnterNormalMode),
        ([], KeyCode::Char('d'), KeyModifiers::NONE) => Some(Command::DeleteTask), // 删除任务
        ([], KeyCode::Char('D'), KeyModifiers::SHIFT) => Some(Command::DeleteTask), // 删除任务
        ([], KeyCode::Char('a'), KeyModifiers::NONE) => Some(Command::NewTask),
        ([], KeyCode::Char('A'), KeyModifiers::SHIFT) => Some(Command::NewTaskInEditor), // 外部编辑器创建任务
        ([], KeyCode::Char('n'), KeyModifiers::NONE) => Some(Command::NewLocalProject),
        ([], KeyCode::Char('N'), KeyModifiers::SHIFT) => Some(Command::NewGlobalProject),
        ([], KeyCode::Char('e'), KeyModifiers::NONE) => Some(Command::EditTask),
        ([], KeyCode::Char('E'), KeyModifiers::SHIFT) => Some(Command::EditTaskInEditor),
        ([], KeyCode::Char('v'), KeyModifiers::NONE) => Some(Command::ViewTask),
        ([], KeyCode::Char('V'), KeyModifiers::SHIFT) => Some(Command::ViewTaskExternal),
        ([], KeyCode::Char('Y'), KeyModifiers::SHIFT) => Some(Command::CopyTask), // 复制任务到剪贴板
        ([], KeyCode::Char('t'), KeyModifiers::NONE) => Some(Command::EditTags),  // 编辑标签

        // 列宽调整
        ([], KeyCode::Char('+'), KeyModifiers::NONE) => Some(Command::IncreaseColumnWidth),
        ([], KeyCode::Char('-'), KeyModifiers::NONE) => Some(Command::DecreaseColumnWidth),
        ([], KeyCode::Char('='), KeyModifiers::NONE) => Some(Command::ResetColumnWidths),
        ([], KeyCode::Char('m'), KeyModifiers::NONE) => Some(Command::ToggleMaximizeColumn),

        ([], KeyCode::Down, _) => Some(Command::TaskDown),
        ([], KeyCode::Up, _) => Some(Command::TaskUp),
        ([], KeyCode::Left, _) => Some(Command::ColumnLeft),
        ([], KeyCode::Right, _) => Some(Command::ColumnRight),

        _ => None,
    }
}

/// 执行命令
fn execute_command(app: &mut App, cmd: Command) {
    use crate::ui::dialogs::DialogType;

    match cmd {
        Command::SplitHorizontal => {
            // 水平分割线 = 上下分屏
            log_debug(format!(
                "执行 SplitHorizontal, 当前焦点: {}",
                app.focused_pane
            ));
            if let Some(pane) = app.split_tree.find_pane_mut(app.focused_pane) {
                let new_pane_id = app.next_pane_id;
                pane.split_vertical(new_pane_id); // split_vertical 创建上下分屏
                app.next_pane_id += 1;
                // 自动对焦新创建的窗口
                app.focused_pane = new_pane_id;
                log_debug(format!(
                    "创建新面板 {}, 新焦点: {}",
                    new_pane_id, app.focused_pane
                ));

                // 保存状态
                let state = crate::state::extract_state(app);
                let _ = crate::state::save_state(&state);
            } else {
                log_debug("找不到当前面板".to_string());
            }
        }
        Command::SplitVertical => {
            // 垂直分割线 = 左右分屏
            log_debug(format!(
                "执行 SplitVertical, 当前焦点: {}",
                app.focused_pane
            ));
            if let Some(pane) = app.split_tree.find_pane_mut(app.focused_pane) {
                let new_pane_id = app.next_pane_id;
                pane.split_horizontal(new_pane_id); // split_horizontal 创建左右分屏
                app.next_pane_id += 1;
                // 自动对焦新创建的窗口
                app.focused_pane = new_pane_id;
                log_debug(format!(
                    "创建新面板 {}, 新焦点: {}",
                    new_pane_id, app.focused_pane
                ));

                // 保存状态
                let state = crate::state::extract_state(app);
                let _ = crate::state::save_state(&state);
            } else {
                log_debug("找不到当前面板".to_string());
            }
        }
        Command::TaskDown => {
            // 获取当前列的任务数量并限制索引
            if let Some(project) = app.get_focused_project() {
                let column = app
                    .selected_column
                    .get(&app.focused_pane)
                    .copied()
                    .unwrap_or(0);

                // 动态获取状态名称
                let status = if let Some(status_name) = app.get_status_name_by_column(column) {
                    status_name
                } else {
                    return;
                };

                let task_count = project.tasks.iter().filter(|t| t.status == status).count();

                if task_count > 0 {
                    let idx = app.selected_task_index.entry(app.focused_pane).or_insert(0);
                    *idx = (*idx + 1).min(task_count - 1);
                }
            }
        }
        Command::TaskUp => {
            let idx = app.selected_task_index.entry(app.focused_pane).or_insert(0);
            *idx = idx.saturating_sub(1);
        }
        Command::ColumnLeft => {
            let col = app.selected_column.entry(app.focused_pane).or_insert(0);
            *col = col.saturating_sub(1);
            // 切换列时重置任务索引到 0
            app.selected_task_index.insert(app.focused_pane, 0);
        }
        Command::ColumnRight => {
            let max_col = app.get_status_count().saturating_sub(1);
            let focused_pane = app.focused_pane;
            let col = app.selected_column.entry(focused_pane).or_insert(0);
            *col = (*col + 1).min(max_col);
            // 切换列时重置任务索引到 0
            app.selected_task_index.insert(focused_pane, 0);
        }
        // Command::EnterCommandMode => {
        //     app.mode = Mode::Command; // 已注释
        // }
        Command::EnterNormalMode | Command::Cancel => {
            app.mode = Mode::Normal;
            app.key_buffer.clear();
            app.command_input.clear(); // 已注释,但仍保留清理逻辑
        }
        Command::NewProject => {
            app.mode = Mode::Dialog;
            // app.ime_state.enter_dialog();  // 进入对话框,恢复用户输入法(已禁用)
            app.dialog = Some(DialogType::Input {
                title: "创建新项目".to_string(),
                prompt: "请输入项目名称:".to_string(),
                textarea: Box::new(crate::ui::text_input::HelixTextArea::new(String::new(), true, false)), // 默认 Insert 模式
            });
        }
        Command::NewLocalProject => {
            app.mode = Mode::Dialog;
            // app.ime_state.enter_dialog();  // 进入对话框,恢复用户输入法(已禁用)
            app.dialog = Some(DialogType::Input {
                title: "创建新本地项目 [L]".to_string(),
                prompt: "请输入项目名称:".to_string(),
                textarea: Box::new(crate::ui::text_input::HelixTextArea::new(String::new(), true, false)), // 默认 Insert 模式
            });
        }
        Command::NewGlobalProject => {
            app.mode = Mode::Dialog;
            // app.ime_state.enter_dialog();  // 进入对话框,恢复用户输入法(已禁用)
            app.dialog = Some(DialogType::Input {
                title: "创建新全局项目 [G]".to_string(),
                prompt: "请输入项目名称:".to_string(),
                textarea: Box::new(crate::ui::text_input::HelixTextArea::new(String::new(), true, false)), // 默认 Insert 模式
            });
        }
        Command::OpenProject => {
            app.mode = Mode::Dialog;
            // app.ime_state.enter_dialog();  // 进入对话框,恢复用户输入法(已禁用)
            // 生成格式化的项目列表:[G/L] 项目名\n    路径
            let project_items: Vec<String> = app
                .projects
                .iter()
                .map(|p| {
                    let type_marker = match p.project_type {
                        crate::models::ProjectType::Global => "[G]",
                        crate::models::ProjectType::Local => "[L]",
                    };
                    let path = match &p.project_type {
                        crate::models::ProjectType::Global => {
                            format!("~/.kanban/projects/{}", p.name)
                        }
                        crate::models::ProjectType::Local => {
                            format!(".kanban/{}", p.name)
                        }
                    };
                    format!("{} {}\n    {}", type_marker, p.name, path)
                })
                .collect();

            app.dialog = Some(DialogType::Select {
                title: "快速切换项目...".to_string(),
                items: project_items,
                selected: 0,
                filter: String::new(),
            });
        }
        Command::RenameProject => {
            // 获取当前项目名
            if let Some(project) = app.get_focused_project() {
                let current_name = project.name.clone();
                app.mode = Mode::Dialog;
                // app.ime_state.enter_dialog();  // 进入对话框,恢复用户输入法(已禁用)
                app.dialog = Some(DialogType::Input {
                    title: "重命名项目".to_string(),
                    prompt: "请输入新的项目名称:".to_string(),
                    textarea: Box::new(crate::ui::text_input::HelixTextArea::new(current_name, true, false)), // 默认 Insert 模式
                });
            }
        }
        Command::HideProject => {
            // 隐藏当前项目(软删除)
            if let Some(project) = app.get_focused_project() {
                let project_name = project.name.clone();
                let project_type = project.project_type;
                let project_path = project.path.clone();

                // 检查是否是当前工作目录的本地项目
                if project_type == crate::models::ProjectType::Local {
                    let current_local_dir = crate::fs::get_local_kanban_dir();
                    // 如果项目路径是当前目录的 .kanban,则不支持软删除
                    if project_path.starts_with(&current_local_dir) {
                        log_debug(
                            "当前目录的本地项目不支持软删除,请使用 D 键删除项目文件".to_string(),
                        );
                        return;
                    }
                }

                // 其他项目(全局项目或其他目录的本地项目):显示确认对话框
                app.mode = Mode::Dialog;
                // app.ime_state.enter_dialog();  // 已禁用输入法自动切换
                app.dialog = Some(DialogType::Confirm {
                    title: "隐藏项目".to_string(),
                    message: format!(
                        "确定要隐藏项目 \"{}\" 吗?\n项目文件不会被删除,下次从该目录启动时会重新加载。",
                        project_name
                    ),
                    yes_selected: true,
                    action: crate::ui::dialogs::ConfirmAction::HideProject,
                });
            }
        }
        Command::DeleteProject => {
            // 删除当前项目(硬删除)
            if let Some(project) = app.get_focused_project() {
                let project_name = project.name.clone();

                // 显示确认对话框
                app.mode = Mode::Dialog;
                // app.ime_state.enter_dialog();  // 进入对话框,恢复用户输入法(已禁用)
                app.dialog = Some(DialogType::Confirm {
                    title: "删除项目文件".to_string(),
                    message: format!(
                        "确定要彻底删除项目 \"{}\" 吗?\n这将永久删除项目的所有文件和任务!此操作不可恢复!",
                        project_name
                    ),
                    yes_selected: false, // 默认选择"否",更安全
                    action: crate::ui::dialogs::ConfirmAction::DeleteProject,
                });
            }
        }
        Command::NewTask => {
            app.mode = Mode::Dialog;
            // app.ime_state.enter_dialog();  // 进入对话框,恢复用户输入法(已禁用)
            app.dialog = Some(DialogType::Input {
                title: "创建新任务".to_string(),
                prompt: "任务标题和内容:".to_string(),
                textarea: Box::new(crate::ui::text_input::HelixTextArea::new(String::new(), true, true)), // 默认 Normal 模式
            });
        }
        Command::NewTaskInEditor => {
            // 用外部编辑器创建新任务
            // 直接在项目中创建任务文件
            use std::io::Write;

            // 获取当前项目
            let (project_path, status) = if let Some(crate::ui::layout::SplitNode::Leaf { project_id, .. }) =
                app.split_tree.find_pane(app.focused_pane)
            {
                if let Some(name) = project_id {
                    if let Some(project) = app.projects.iter().find(|p| p.name == *name) {
                        let column = app.selected_column.get(&app.focused_pane).copied().unwrap_or(0);
                        let status = app.get_status_name_by_column(column).unwrap_or_else(|| "todo".to_string());
                        (project.path.clone(), status)
                    } else {
                        return;
                    }
                } else {
                    return;
                }
            } else {
                return;
            };

            // 获取下一个任务 ID
            if let Ok(next_id) = crate::fs::get_next_task_id(&project_path) {
                let status_dir = project_path.join(&status);
                if !status_dir.exists() {
                    let _ = std::fs::create_dir_all(&status_dir);
                }

                let task_file = status_dir.join(format!("{}.md", next_id));

                // 获取当前列的最大order值
                let max_order = crate::fs::get_max_order_in_status(&project_path, &status).unwrap_or(-1000);
                let new_order = max_order + 1000;

                // 写入 frontmatter 格式的模板内容
                let template = format!(
                    "+++\nid = {}\norder = {}\ncreated = \"{}\"\n+++\n\n# 任务标题\n\n任务描述内容...\n\n## 子任务\n\n- [ ] 子任务 1\n- [ ] 子任务 2\n",
                    next_id,
                    new_order,
                    std::time::SystemTime::now()
                        .duration_since(std::time::UNIX_EPOCH)
                        .unwrap()
                        .as_secs()
                );

                if let Ok(mut file) = std::fs::File::create(&task_file) {
                    let _ = file.write_all(template.as_bytes());
                }

                // 直接打开项目文件
                app.pending_editor_file = Some(task_file.to_string_lossy().to_string());
                app.is_new_task_file = false; // 不是临时文件,是真实的项目文件
            }
        }
        Command::EditTask => {
            // 获取当前选中的任务
            if let Some(task) = get_selected_task(app) {
                let title = task.title.clone();
                app.mode = Mode::Dialog;
                // app.ime_state.enter_dialog();  // 进入对话框,恢复用户输入法(已禁用)
                app.dialog = Some(DialogType::Input {
                    title: "编辑任务".to_string(),
                    prompt: "任务标题和内容:".to_string(),
                    textarea: Box::new(crate::ui::text_input::HelixTextArea::new(title, true, true)), // 默认 Normal 模式
                });
            }
        }
        Command::MoveTaskLeft => {
            move_task_to_status(app, -1);
        }
        Command::MoveTaskRight => {
            move_task_to_status(app, 1);
        }
        Command::MoveTaskUp => {
            move_task_in_column(app, -1);
        }
        Command::MoveTaskDown => {
            move_task_in_column(app, 1);
        }
        Command::FocusNextPane => {
            // 获取所有窗格 ID 并找到下一个
            let all_panes = app.split_tree.collect_pane_ids();
            log_debug(format!(
                "FocusNextPane, 当前焦点: {}, 所有面板: {:?}",
                app.focused_pane, all_panes
            ));
            if all_panes.len() > 1 {
                if let Some(current_idx) = all_panes.iter().position(|&id| id == app.focused_pane) {
                    let next_idx = (current_idx + 1) % all_panes.len();
                    app.focused_pane = all_panes[next_idx];
                    log_debug(format!("切换到面板: {}", app.focused_pane));
                }
            } else {
                log_debug("只有一个面板,无需切换".to_string());
            }
        }
        Command::FocusLeft => {
            log_debug(format!("FocusLeft, 当前焦点: {}", app.focused_pane));
            if let Some(new_pane_id) = app
                .split_tree
                .find_adjacent_pane(app.focused_pane, crate::ui::layout::Direction::Left)
            {
                app.focused_pane = new_pane_id;
                log_debug(format!("移动到左侧面板: {}", new_pane_id));
            } else {
                log_debug("左侧没有面板".to_string());
            }
        }
        Command::FocusRight => {
            log_debug(format!("FocusRight, 当前焦点: {}", app.focused_pane));
            if let Some(new_pane_id) = app
                .split_tree
                .find_adjacent_pane(app.focused_pane, crate::ui::layout::Direction::Right)
            {
                app.focused_pane = new_pane_id;
                log_debug(format!("移动到右侧面板: {}", new_pane_id));
            } else {
                log_debug("右侧没有面板".to_string());
            }
        }
        Command::FocusUp => {
            log_debug(format!("FocusUp, 当前焦点: {}", app.focused_pane));
            if let Some(new_pane_id) = app
                .split_tree
                .find_adjacent_pane(app.focused_pane, crate::ui::layout::Direction::Up)
            {
                app.focused_pane = new_pane_id;
                log_debug(format!("移动到上方面板: {}", new_pane_id));
            } else {
                log_debug("上方没有面板".to_string());
            }
        }
        Command::FocusDown => {
            log_debug(format!("FocusDown, 当前焦点: {}", app.focused_pane));
            if let Some(new_pane_id) = app
                .split_tree
                .find_adjacent_pane(app.focused_pane, crate::ui::layout::Direction::Down)
            {
                app.focused_pane = new_pane_id;
                log_debug(format!("移动到下方面板: {}", new_pane_id));
            } else {
                log_debug("下方没有面板".to_string());
            }
        }
        Command::ClosePane => {
            // 如果当前处于最大化状态,关闭当前窗口并恢复布局
            if app.saved_layout.is_some() {
                log_debug("最大化状态下按 q,关闭窗口并恢复布局".to_string());

                // 先恢复布局
                if let Some(saved) = app.saved_layout.take() {
                    app.split_tree = saved;
                    log_debug("已恢复布局".to_string());
                }

                // 然后关闭当前聚焦的面板
                let current_pane = app.focused_pane;
                log_debug(format!("关闭面板: {}", current_pane));
                if app.split_tree.close_pane(current_pane) {
                    // 关闭成功,重新聚焦到一个有效的面板
                    let all_panes = app.split_tree.collect_pane_ids();
                    if let Some(&first_pane) = all_panes.first() {
                        app.focused_pane = first_pane;
                        log_debug(format!("关闭后聚焦到: {}", first_pane));
                    }

                    // 保存状态
                    let state = crate::state::extract_state(app);
                    let _ = crate::state::save_state(&state);
                } else {
                    log_debug("无法关闭面板".to_string());
                }
            } else {
                // 非最大化状态:尝试关闭当前面板
                log_debug(format!("关闭面板: {}", app.focused_pane));
                let current_pane = app.focused_pane;
                if app.split_tree.close_pane(current_pane) {
                    // 关闭成功,需要重新聚焦到一个有效的面板
                    let all_panes = app.split_tree.collect_pane_ids();
                    if let Some(&first_pane) = all_panes.first() {
                        app.focused_pane = first_pane;
                        log_debug(format!("关闭后聚焦到: {}", first_pane));
                    }

                    // 保存状态
                    let state = crate::state::extract_state(app);
                    let _ = crate::state::save_state(&state);
                } else {
                    // 只有一个面板时,清空该面板的项目
                    log_debug("只有一个面板,清空当前项目".to_string());
                    if let Some(crate::ui::layout::SplitNode::Leaf { project_id, .. }) =
                        app.split_tree.find_pane_mut(app.focused_pane)
                    {
                        *project_id = None;

                        // 保存状态
                        let state = crate::state::extract_state(app);
                        let _ = crate::state::save_state(&state);

                        log_debug("已清空项目".to_string());
                    }
                }
            }
        }
        Command::MaximizePane => {
            // 最大化/恢复当前面板
            app.toggle_maximize();
        }
        Command::EditTaskInEditor => {
            // 用外部编辑器编辑当前选中的任务
            if let Some(task) = get_selected_task(app) {
                // 直接打开项目文件
                app.pending_editor_file = Some(task.file_path.to_string_lossy().to_string());
            }
        }
        Command::ViewTaskExternal => {
            // 用外部工具预览当前选中的任务
            if let Some(task) = get_selected_task(app) {
                // 设置待预览的文件路径
                app.pending_preview_file = Some(task.file_path.to_string_lossy().to_string());
            }
        }
        Command::ViewTask => {
            // TUI 内预览当前选中的任务
            if let Some(task) = get_selected_task(app) {
                // 读取任务文件内容
                if let Ok(content) = std::fs::read_to_string(&task.file_path) {
                    app.preview_content = content;
                    app.preview_scroll = 0;
                    app.mode = Mode::Preview;
                } else {
                    log_debug("读取任务文件失败".to_string());
                }
            }
        }
        Command::DeleteTask => {
            // 删除当前选中的任务
            if let Some(task) = get_selected_task(app) {
                let task_title = task.title.clone();

                // 显示确认对话框
                app.mode = Mode::Dialog;
                // app.ime_state.enter_dialog();  // 进入对话框,恢复用户输入法(已禁用)
                app.dialog = Some(DialogType::Confirm {
                    title: "删除任务".to_string(),
                    message: format!("确定要删除任务 \"{}\" 吗?", task_title),
                    yes_selected: true,
                    action: crate::ui::dialogs::ConfirmAction::DeleteTask,
                });
            }
        }
        Command::CopyTask => {
            // 复制任务到剪贴板
            #[cfg(feature = "clipboard")]
            {
                if let Some(task) = get_selected_task(app) {
                    // 获取任务的完整内容(包含元数据)
                    match crate::fs::task::get_task_full_content(&task) {
                        Ok(content) => {
                            // 复制到剪贴板
                            match arboard::Clipboard::new() {
                                Ok(mut clipboard) => {
                                    if let Err(e) = clipboard.set_text(content) {
                                        app.show_notification(
                                            format!("复制失败: {}", e),
                                            crate::app::NotificationLevel::Error,
                                        );
                                    } else {
                                        app.show_notification(
                                            format!("已复制任务「{}」到剪贴板", task.title),
                                            crate::app::NotificationLevel::Success,
                                        );
                                    }
                                }
                                Err(e) => {
                                    app.show_notification(
                                        format!("无法访问剪贴板: {}", e),
                                        crate::app::NotificationLevel::Error,
                                    );
                                }
                            }
                        }
                        Err(e) => {
                            app.show_notification(
                                format!("读取任务失败: {}", e),
                                crate::app::NotificationLevel::Error,
                            );
                        }
                    }
                }
            }
            #[cfg(not(feature = "clipboard"))]
            {
                app.show_notification(
                    "剪贴板功能未启用".to_string(),
                    crate::app::NotificationLevel::Warning,
                );
            }
        }
        Command::SetTaskPriority(priority) => {
            // 设置任务优先级
            let task_id = if let Some(id) = get_selected_task_id(app) {
                id
            } else {
                return;
            };

            let project_name = if let Some(crate::ui::layout::SplitNode::Leaf { project_id: Some(name), .. }) =
                app.split_tree.find_pane(app.focused_pane)
            {
                name.clone()
            } else {
                return;
            };

            let mut result = Ok(());
            if let Some(project) = app.projects.iter_mut().find(|p| p.name == project_name)
                && let Some(task) = project.tasks.iter_mut().find(|t| t.id == task_id) {
                    let old_priority = task.priority.clone();
                    task.priority = if priority == "none" {
                        None
                    } else {
                        Some(priority.clone())
                    };

                    let project_path = project.path.clone();
                    if let Err(e) = crate::fs::save_task(&project_path, task) {
                        result = Err(e);
                        task.priority = old_priority; // 回滚
                    }
                }

            match result {
                Ok(_) => {
                    app.show_notification(
                        format!("优先级已设置为: {}", if priority == "none" { "" } else { &priority }),
                        crate::app::NotificationLevel::Success,
                    );
                    // 重新加载项目
                    let _ = app.reload_current_project();
                }
                Err(e) => {
                    app.show_notification(
                        format!("保存优先级失败: {}", e),
                        crate::app::NotificationLevel::Error,
                    );
                }
            }
        }
        Command::EditTags => {
            // 编辑任务标签
            if let Some(task) = get_selected_task(app) {
                let current_tags = task.tags.join(", ");
                app.mode = Mode::Dialog;
                // app.ime_state.enter_dialog();  // 进入对话框,恢复用户输入法(已禁用)
                app.dialog = Some(DialogType::Input {
                    title: "编辑标签".to_string(),
                    prompt: "标签(逗号分隔):".to_string(),
                    textarea: Box::new(crate::ui::text_input::HelixTextArea::new(current_tags, true, false)),
                });
            }
        }
        Command::IncreaseColumnWidth => {
            adjust_column_width(app, 5);
        }
        Command::DecreaseColumnWidth => {
            adjust_column_width(app, -5);
        }
        Command::ResetColumnWidths => {
            reset_column_widths(app);
        }
        Command::ToggleMaximizeColumn => {
            toggle_maximize_column(app);
        }
        Command::ReloadCurrentProject => {
            // 重新加载当前项目
            if let Err(e) = app.reload_current_project() {
                log_debug(format!("重新加载当前项目失败: {}", e));
            } else {
                log_debug("重新加载当前项目成功".to_string());
            }
        }
        Command::ReloadAllProjects => {
            // 重新加载所有项目(本地+全局)
            match crate::fs::load_all_projects() {
                Ok(projects) => {
                    app.projects = projects;
                    log_debug(format!(
                        "重新加载所有项目成功,共 {}",
                        app.projects.len()
                    ));
                }
                Err(e) => {
                    log_debug(format!("重新加载所有项目失败: {}", e));
                }
            }
        }
        Command::CopyProjectInfo => {
            // 复制项目信息到剪贴板
            #[cfg(feature = "clipboard")]
            {
                if let Some(project) = app.get_focused_project() {
                    let project_type_label = match project.project_type {
                        crate::models::ProjectType::Global => "[G]",
                        crate::models::ProjectType::Local => "[L]",
                    };

                    let kanban_path = project.path.to_string_lossy();
                    // CLAUDE.md 路径(全局数据目录)
                    let claude_md_path = crate::fs::get_data_dir().join("CLAUDE.md");
                    let claude_md_str = claude_md_path.to_string_lossy().to_string();

                    // 格式化项目信息
                    let project_info = format!(
                        "{} {}\n看板路径: {}\n文档: {}",
                        project_type_label,
                        project.name,
                        kanban_path,
                        claude_md_str
                    );

                    match arboard::Clipboard::new() {
                        Ok(mut clipboard) => {
                            if let Err(e) = clipboard.set_text(project_info) {
                                app.show_notification(
                                    format!("复制失败: {}", e),
                                    crate::app::NotificationLevel::Error,
                                );
                            } else {
                                app.show_notification(
                                    format!("已复制项目「{}」信息到剪贴板", project.name),
                                    crate::app::NotificationLevel::Success,
                                );
                            }
                        }
                        Err(e) => {
                            app.show_notification(
                                format!("无法访问剪贴板: {}", e),
                                crate::app::NotificationLevel::Error,
                            );
                        }
                    }
                }
            }
            #[cfg(not(feature = "clipboard"))]
            {
                app.show_notification(
                    "剪贴板功能未启用".to_string(),
                    crate::app::NotificationLevel::Warning,
                );
            }
        }
        Command::CreateStatus => {
            // 创建新状态
            app.mode = Mode::Dialog;
            // app.ime_state.enter_dialog();  // 已禁用输入法自动切换
            app.dialog = Some(DialogType::Input {
                title: "创建新状态".to_string(),
                prompt: "请输入状态内部名称(英文、数字、下划线):".to_string(),
                textarea: Box::new(crate::ui::text_input::HelixTextArea::new(String::new(), true, false)), // 默认 Insert 模式
            });
        }
        Command::RenameStatus => {
            // 重命名状态 - 收集信息后再修改 app
            let status_info = {
                let project = app.get_focused_project();
                if let Some(project) = project {
                    let column = app
                        .selected_column
                        .get(&app.focused_pane)
                        .copied()
                        .unwrap_or(0);
                    project
                        .statuses
                        .get(column)
                        .map(|s| (s.name.clone(), s.display.clone()))
                } else {
                    None
                }
            };

            if let Some((current_name, current_display)) = status_info {
                app.mode = Mode::Dialog;
                // app.ime_state.enter_dialog();  // 已禁用输入法自动切换
                app.dialog = Some(DialogType::Input {
                    title: format!("重命名状态: {}", current_display),
                    prompt: "请输入新的状态名称(英文、数字、下划线):".to_string(),
                    textarea: Box::new(crate::ui::text_input::HelixTextArea::new(current_name, true, false)), // 默认 Insert 模式
                });
            }
        }
        Command::EditStatusDisplay => {
            // 编辑状态显示名 - 收集信息后再修改 app
            let status_info = {
                let project = app.get_focused_project();
                if let Some(project) = project {
                    let column = app
                        .selected_column
                        .get(&app.focused_pane)
                        .copied()
                        .unwrap_or(0);
                    project
                        .statuses
                        .get(column)
                        .map(|s| (s.name.clone(), s.display.clone()))
                } else {
                    None
                }
            };

            if let Some((status_name, current_display)) = status_info {
                app.mode = Mode::Dialog;
                // app.ime_state.enter_dialog();  // 已禁用输入法自动切换
                app.dialog = Some(DialogType::Input {
                    title: format!("编辑显示名: {}", status_name),
                    prompt: "请输入新的显示名称:".to_string(),
                    textarea: Box::new(crate::ui::text_input::HelixTextArea::new(
                        current_display,
                        true,
                        false,
                    )),
                });
            }
        }
        Command::MoveStatusLeft => {
            // 左移状态列 - 收集信息后再修改 app
            let focused_pane = app.focused_pane;
            let status_info = {
                let project = app.get_focused_project();
                if let Some(project) = project {
                    let column = app.selected_column.get(&focused_pane).copied().unwrap_or(0);
                    project.statuses.get(column).map(|s| {
                        (
                            s.name.clone(),
                            s.display.clone(),
                            project.path.clone(),
                            column,
                        )
                    })
                } else {
                    None
                }
            };

            if let Some((status_name, status_display, project_path, column)) = status_info {
                match crate::fs::status::move_status_order(&project_path, &status_name, -1) {
                    Ok(_) => {
                        // 重新加载项目
                        if let Err(e) = app.reload_current_project() {
                            log_debug(format!("重新加载项目失败: {}", e));
                        }
                        // 更新选中列
                        if column > 0 {
                            app.selected_column.insert(focused_pane, column - 1);
                        }
                        app.show_notification(
                            format!("状态「{}」已左移", status_display),
                            crate::app::NotificationLevel::Success,
                        );
                    }
                    Err(e) => {
                        app.show_notification(
                            format!("移动失败: {}", e),
                            crate::app::NotificationLevel::Error,
                        );
                    }
                }
            }
        }
        Command::MoveStatusRight => {
            // 右移状态列 - 收集信息后再修改 app
            let focused_pane = app.focused_pane;
            let status_info = {
                let project = app.get_focused_project();
                if let Some(project) = project {
                    let column = app.selected_column.get(&focused_pane).copied().unwrap_or(0);
                    let statuses_len = project.statuses.len();
                    project.statuses.get(column).map(|s| {
                        (
                            s.name.clone(),
                            s.display.clone(),
                            project.path.clone(),
                            column,
                            statuses_len,
                        )
                    })
                } else {
                    None
                }
            };

            if let Some((status_name, status_display, project_path, column, statuses_len)) =
                status_info
            {
                match crate::fs::status::move_status_order(&project_path, &status_name, 1) {
                    Ok(_) => {
                        // 重新加载项目
                        if let Err(e) = app.reload_current_project() {
                            log_debug(format!("重新加载项目失败: {}", e));
                        }
                        // 更新选中列
                        if column < statuses_len - 1 {
                            app.selected_column.insert(focused_pane, column + 1);
                        }
                        app.show_notification(
                            format!("状态「{}」已右移", status_display),
                            crate::app::NotificationLevel::Success,
                        );
                    }
                    Err(e) => {
                        app.show_notification(
                            format!("移动失败: {}", e),
                            crate::app::NotificationLevel::Error,
                        );
                    }
                }
            }
        }
        Command::MoveStatusToFirst => {
            // 移动状态列到最左侧
            let focused_pane = app.focused_pane;
            let status_info = {
                let project = app.get_focused_project();
                if let Some(project) = project {
                    let column = app.selected_column.get(&focused_pane).copied().unwrap_or(0);
                    project.statuses.get(column).map(|s| {
                        (
                            s.name.clone(),
                            s.display.clone(),
                            project.path.clone(),
                            column,
                        )
                    })
                } else {
                    None
                }
            };

            if let Some((status_name, status_display, project_path, column)) = status_info {
                if column == 0 {
                    app.show_notification(
                        "状态已在最左侧".to_string(),
                        crate::app::NotificationLevel::Info,
                    );
                    return;
                }

                // 移动到最左侧(索引 0)
                let move_count = -(column as i32);
                match crate::fs::status::move_status_order(&project_path, &status_name, move_count)
                {
                    Ok(_) => {
                        // 重新加载项目
                        if let Err(e) = app.reload_current_project() {
                            log_debug(format!("重新加载项目失败: {}", e));
                        }
                        // 更新选中列到第一列
                        app.selected_column.insert(focused_pane, 0);
                        app.show_notification(
                            format!("状态「{}」已移至最左侧", status_display),
                            crate::app::NotificationLevel::Success,
                        );
                    }
                    Err(e) => {
                        app.show_notification(
                            format!("移动失败: {}", e),
                            crate::app::NotificationLevel::Error,
                        );
                    }
                }
            }
        }
        Command::MoveStatusToLast => {
            // 移动状态列到最右侧
            let focused_pane = app.focused_pane;
            let status_info = {
                let project = app.get_focused_project();
                if let Some(project) = project {
                    let column = app.selected_column.get(&focused_pane).copied().unwrap_or(0);
                    let statuses_len = project.statuses.len();
                    project.statuses.get(column).map(|s| {
                        (
                            s.name.clone(),
                            s.display.clone(),
                            project.path.clone(),
                            column,
                            statuses_len,
                        )
                    })
                } else {
                    None
                }
            };

            if let Some((status_name, status_display, project_path, column, statuses_len)) =
                status_info
            {
                if column == statuses_len - 1 {
                    app.show_notification(
                        "状态已在最右侧".to_string(),
                        crate::app::NotificationLevel::Info,
                    );
                    return;
                }

                // 移动到最右侧
                let move_count = (statuses_len - 1 - column) as i32;
                match crate::fs::status::move_status_order(&project_path, &status_name, move_count)
                {
                    Ok(_) => {
                        // 重新加载项目
                        if let Err(e) = app.reload_current_project() {
                            log_debug(format!("重新加载项目失败: {}", e));
                        }
                        // 更新选中列到最后一列
                        app.selected_column.insert(focused_pane, statuses_len - 1);
                        app.show_notification(
                            format!("状态「{}」已移至最右侧", status_display),
                            crate::app::NotificationLevel::Success,
                        );
                    }
                    Err(e) => {
                        app.show_notification(
                            format!("移动失败: {}", e),
                            crate::app::NotificationLevel::Error,
                        );
                    }
                }
            }
        }
        Command::DeleteStatus => {
            // 删除状态
            if let Some(project) = app.get_focused_project() {
                let column = app
                    .selected_column
                    .get(&app.focused_pane)
                    .copied()
                    .unwrap_or(0);
                if let Some(status) = project.statuses.get(column) {
                    let status_name = status.name.clone();
                    let status_display = status.display.clone();

                    // 检查任务数量
                    let task_count = project
                        .tasks
                        .iter()
                        .filter(|t| t.status == status_name)
                        .count();

                    let message = if task_count > 0 {
                        format!(
                            "确定要删除状态「{}」吗?\n该状态下有 {} 个任务,删除后任务将无法访问。",
                            status_display, task_count
                        )
                    } else {
                        format!("确定要删除状态「{}」吗?", status_display)
                    };

                    app.mode = Mode::Dialog;
                    // app.ime_state.enter_dialog();  // 已禁用输入法自动切换
                    app.dialog = Some(DialogType::Confirm {
                        title: "删除状态".to_string(),
                        message,
                        yes_selected: false, // 默认选择"否",更安全
                        action: crate::ui::dialogs::ConfirmAction::DeleteStatus,
                    });
                }
            }
        }
        // 未实现的命令:静默忽略(不报错,不执行)
        _ => {
            // 不做任何处理,避免报错
        }
    }
}

/// 执行文本命令(从命令模式输入)
/// 返回 false 表示应该退出应用
#[allow(dead_code)]
fn execute_text_command(app: &mut App, cmd_str: &str) -> bool {
    let cmd_str = cmd_str.trim();

    // 查找命令定义
    let cmd_def = app.command_registry.find_exact(cmd_str);

    if let Some(cmd_def) = cmd_def {
        // 根据命令名执行对应操作
        match cmd_def.name {
            "quit" => {
                return false; // 退出应用
            }
            "project-open" => execute_command(app, Command::OpenProject),
            "project-new" => execute_command(app, Command::NewGlobalProject),
            "project-new-local" => execute_command(app, Command::NewLocalProject),
            "project-delete" => execute_command(app, Command::DeleteProject),
            "project-rename" => execute_command(app, Command::RenameProject),
            "task-new" => execute_command(app, Command::NewTask),
            "task-edit" => execute_command(app, Command::EditTask),
            "task-delete" => execute_command(app, Command::DeleteTask),
            "task-view" => execute_command(app, Command::ViewTask),
            "task-view-external" => execute_command(app, Command::ViewTaskExternal),
            "task-edit-external" => execute_command(app, Command::EditTaskInEditor),
            "priority-high" => execute_command(app, Command::SetTaskPriority("high".to_string())),
            "priority-medium" => {
                execute_command(app, Command::SetTaskPriority("medium".to_string()))
            }
            "priority-low" => execute_command(app, Command::SetTaskPriority("low".to_string())),
            "priority-none" => execute_command(app, Command::SetTaskPriority("none".to_string())),
            "split-horizontal" => execute_command(app, Command::SplitHorizontal),
            "split-vertical" => execute_command(app, Command::SplitVertical),
            "close-pane" => execute_command(app, Command::ClosePane),
            "focus-next" => execute_command(app, Command::FocusNextPane),
            "focus-left" => execute_command(app, Command::FocusLeft),
            "focus-right" => execute_command(app, Command::FocusRight),
            "focus-up" => execute_command(app, Command::FocusUp),
            "focus-down" => execute_command(app, Command::FocusDown),
            "reload" => execute_command(app, Command::ReloadCurrentProject),
            "reload-all" => execute_command(app, Command::ReloadAllProjects),
            "help" => {
                app.mode = Mode::Help;
            }
            _ => {
                // 未实现的命令:静默忽略
            }
        }
    } else {
        // 未知命令:静默忽略
    }

    true // 继续运行
}

/// 获取当前选中的任务
fn get_selected_task(app: &App) -> Option<&crate::models::Task> {
    let project = app.get_focused_project()?;
    let column = app
        .selected_column
        .get(&app.focused_pane)
        .copied()
        .unwrap_or(0);
    let task_idx = app
        .selected_task_index
        .get(&app.focused_pane)
        .copied()
        .unwrap_or(0);

    let status = app.get_status_name_by_column(column)?;

    let tasks: Vec<_> = project
        .tasks
        .iter()
        .filter(|t| t.status == status)
        .collect();
    tasks.get(task_idx).copied()
}

/// 获取当前选中的任务(可变)- 返回任务 ID
fn get_selected_task_id(app: &App) -> Option<u32> {
    let column = app
        .selected_column
        .get(&app.focused_pane)
        .copied()
        .unwrap_or(0);
    let task_idx = app
        .selected_task_index
        .get(&app.focused_pane)
        .copied()
        .unwrap_or(0);

    let status = app.get_status_name_by_column(column)?;

    let project = app.get_focused_project()?;
    let tasks: Vec<_> = project
        .tasks
        .iter()
        .filter(|t| t.status == status)
        .collect();
    tasks.get(task_idx).map(|t| t.id)
}

/// 移动任务到相邻状态
fn move_task_to_status(app: &mut App, direction: i32) {
    let column = app
        .selected_column
        .get(&app.focused_pane)
        .copied()
        .unwrap_or(0);
    let status_count = app.get_status_count();
    let new_column = (column as i32 + direction).clamp(0, status_count as i32 - 1) as usize;

    if new_column == column {
        return; // 已经在边界
    }

    // 获取任务 ID
    let task_id = if let Some(id) = get_selected_task_id(app) {
        id
    } else {
        return;
    };

    // 获取项目名称和路径
    let project_name = if let Some(crate::ui::layout::SplitNode::Leaf { project_id: Some(name), .. }) =
        app.split_tree.find_pane(app.focused_pane)
    {
        name.clone()
    } else {
        return;
    };

    // 获取新状态名称
    let new_status = if let Some(status) = app.get_status_name_by_column(new_column) {
        status
    } else {
        return;
    };

    // 找到任务并修改
    if let Some(project) = app.projects.iter_mut().find(|p| p.name == project_name)
        && let Some(task) = project.tasks.iter_mut().find(|t| t.id == task_id) {
            let old_status = task.status.clone();
            task.status = new_status.clone();

            // 移动文件到新的状态目录(使用项目的实际路径)
            let project_path = project.path.clone();

            match crate::fs::move_task(&project_path, task, &new_status) {
                Ok(new_path) => {
                    // 更新任务的文件路径
                    task.file_path = new_path;
                    // 更新界面:移动到新列
                    app.selected_column.insert(app.focused_pane, new_column);
                    app.selected_task_index.insert(app.focused_pane, 0);
                }
                Err(e) => {
                    log_debug(format!("移动任务文件失败: {}", e));
                    task.status = old_status; // 回滚
                }
            }
        }
}

/// 在列内上下移动任务
fn move_task_in_column(app: &mut App, direction: i32) {
    let column = app
        .selected_column
        .get(&app.focused_pane)
        .copied()
        .unwrap_or(0);
    let task_idx = app
        .selected_task_index
        .get(&app.focused_pane)
        .copied()
        .unwrap_or(0);

    let status = if let Some(s) = app.get_status_name_by_column(column) {
        s
    } else {
        return;
    };

    // 获取项目名称和路径
    let (project_name, project_path) =
        if let Some(crate::ui::layout::SplitNode::Leaf { project_id: Some(name), .. }) =
            app.split_tree.find_pane(app.focused_pane)
        {
            if let Some(project) = app.projects.iter().find(|p| &p.name == name) {
                (name.clone(), project.path.clone())
            } else {
                return;
            }
        } else {
            return;
        };

    // 获取当前列的所有任务(已按order排序)
    if let Some(project) = app.projects.iter_mut().find(|p| p.name == project_name) {
        let mut tasks: Vec<&mut crate::models::Task> = project
            .tasks
            .iter_mut()
            .filter(|t| t.status == status)
            .collect();

        if tasks.len() < 2 {
            return; // 不足以移动
        }

        // 按order排序
        tasks.sort_by_key(|t| t.order);

        let new_idx = (task_idx as i32 + direction).clamp(0, tasks.len() as i32 - 1) as usize;

        if new_idx == task_idx {
            return; // 已经在边界
        }

        // 获取当前任务
        let current_task_id = tasks[task_idx].id;

        // 计算新的order值
        let new_order = if new_idx == 0 {
            // 移到最顶部
            tasks[0].order - 1000
        } else if new_idx == tasks.len() - 1 {
            // 移到最底部
            tasks[tasks.len() - 1].order + 1000
        } else {
            // 移到中间:计算上下任务的中间值
            let (order_above, order_below) = if direction > 0 {
                // 向下移动
                (
                    tasks[new_idx].order,
                    tasks
                        .get(new_idx + 1)
                        .map(|t| t.order)
                        .unwrap_or(tasks[new_idx].order + 1000),
                )
            } else {
                // 向上移动
                (
                    tasks
                        .get(new_idx.saturating_sub(1))
                        .map(|t| t.order)
                        .unwrap_or(tasks[new_idx].order - 1000),
                    tasks[new_idx].order,
                )
            };

            // 检查间隙是否足够
            if (order_below - order_above).abs() < 2 {
                // 间隙不够,需要重平衡
                log_debug("Order值间隙不足,执行重平衡".to_string());
                rebalance_order_in_column(&mut tasks);

                // 重新计算new_order
                if direction > 0 {
                    let below_order = tasks
                        .get(new_idx + 1)
                        .map(|t| t.order)
                        .unwrap_or(tasks[new_idx].order + 1000);
                    (tasks[new_idx].order + below_order) / 2
                } else {
                    let above_order = tasks
                        .get(new_idx.saturating_sub(1))
                        .map(|t| t.order)
                        .unwrap_or(tasks[new_idx].order - 1000);
                    (above_order + tasks[new_idx].order) / 2
                }
            } else {
                (order_above + order_below) / 2
            }
        };

        // 更新当前任务的order
        let need_rebalance = tasks
            .iter()
            .any(|t| t.order % 1000 == 0 && t.id != current_task_id);

        if let Some(task) = project.tasks.iter_mut().find(|t| t.id == current_task_id) {
            task.order = new_order;

            // 持久化到文件
            if let Err(e) = crate::fs::save_task(&project_path, task) {
                log_debug(format!("保存任务失败: {}", e));
                return;
            }

            log_debug(format!("任务 {} 的order更新为 {}", task.id, task.order));
        }

        // 如果进行了重平衡,保存所有任务
        if need_rebalance {
            let tasks_to_save: Vec<crate::models::Task> = project
                .tasks
                .iter()
                .filter(|t| t.status == status)
                .cloned()
                .collect();

            for task in tasks_to_save {
                let _ = crate::fs::save_task(&project_path, &task);
            }
        }

        // 更新UI选中索引
        app.selected_task_index.insert(app.focused_pane, new_idx);

        // 重新加载项目以刷新排序
        let _ = app.reload_current_project();
    }
}

/// 创建新任务
fn create_new_task(app: &mut App, input: String) {
    use crate::models::Task;

    log_debug(format!("调试: 准备创建任务,输入内容: '{}'", input));

    // 解析输入:第一行是标题,其余是内容
    let lines: Vec<&str> = input.lines().collect();
    let title = if lines.is_empty() {
        log_debug("调试: 输入为空".to_string());
        return;
    } else {
        lines[0].trim().to_string()
    };

    let content = if lines.len() > 1 {
        lines[1..].join("\n")
    } else {
        String::new()
    };

    log_debug(format!("调试: 标题='{}', 内容长度={}", title, content.len()));

    // 获取当前项目
    let project_name = if let Some(crate::ui::layout::SplitNode::Leaf { project_id, .. }) =
        app.split_tree.find_pane(app.focused_pane)
    {
        if let Some(name) = project_id {
            log_debug(format!("调试: 当前项目 '{}'", name));
            name.clone()
        } else {
            log_debug("调试: 当前面板没有项目".to_string());
            return;
        }
    } else {
        log_debug("调试: 找不到当前面板".to_string());
        return;
    };

    // 获取项目路径(支持本地和全局项目)
    let project_path = if let Some(project) = app.projects.iter().find(|p| p.name == project_name)
    {
        project.path.clone()
    } else {
        log_debug("调试: 在项目列表中找不到项目".to_string());
        return;
    };

    // 获取下一个任务 ID
    if let Ok(next_id) = crate::fs::get_next_task_id(&project_path) {
        log_debug(format!("调试: 下一个任务ID {}", next_id));
        // 获取当前选中的列作为初始状态
        let column = app
            .selected_column
            .get(&app.focused_pane)
            .copied()
            .unwrap_or(0);
        let status = app
            .get_status_name_by_column(column)
            .unwrap_or_else(|| "todo".to_string());
        log_debug(format!("调试: 状态 '{}'", status));

        // 获取当前列的最大order值
        let max_order = crate::fs::get_max_order_in_status(&project_path, &status).unwrap_or(-1000);
        let new_order = max_order + 1000;
        log_debug(format!("调试: 新任务order值 {}", new_order));

        // 创建任务并设置order和content
        let mut task = Task::new(next_id, title.clone(), status.clone());
        task.order = new_order;
        task.content = content;

        // 保存到文件
        match crate::fs::save_task(&project_path, &task) {
            Ok(_) => {
                log_debug("调试: 任务保存成功".to_string());
            }
            Err(e) => {
                log_debug(format!("保存任务失败: {}", e));
                return;
            }
        }

        // 重新加载项目以确保任务列表是最新的
        match crate::fs::load_project(&project_path) {
            Ok(updated_project) => {
                log_debug(format!(
                    "调试: 重新加载项目,共 {} 个任务",
                    updated_project.tasks.len()
                ));
                if let Some(project) = app.projects.iter_mut().find(|p| p.name == project_name) {
                    *project = updated_project;

                    // 找到新任务在当前列的索引(应该是最后一个)
                    let new_task_idx = project
                        .tasks
                        .iter()
                        .filter(|t| t.status == status)
                        .count()
                        .saturating_sub(1);

                    // 自动选中新创建的任务
                    app.selected_task_index
                        .insert(app.focused_pane, new_task_idx);
                    log_debug(format!("调试: 选中任务索引 {}", new_task_idx));
                } else {
                    log_debug(format!(
                        "调试: 在 app.projects 中找不到项目 '{}'",
                        project_name
                    ));
                }
            }
            Err(e) => {
                log_debug(format!("调试: 重新加载项目失败: {}", e));
            }
        }
    } else {
        log_debug("调试: 获取下一个任务ID失败".to_string());
    }
}

/// 重命名当前项目
fn rename_current_project(app: &mut App, new_name: String) {
    // 获取当前项目名
    let old_name = if let Some(crate::ui::layout::SplitNode::Leaf { project_id: Some(name), .. }) =
        app.split_tree.find_pane(app.focused_pane)
    {
        name.clone()
    } else {
        return;
    };

    if old_name == new_name {
        return; // 名称没有变化
    }

    // 找到项目
    if let Some(project) = app.projects.iter().find(|p| p.name == old_name).cloned() {
        let old_path = project.path.clone();
        let new_path = old_path.parent().unwrap().join(&new_name);

        // 重命名目录
        if let Err(e) = std::fs::rename(&old_path, &new_path) {
            log_debug(format!("重命名项目目录失败: {}", e));
            return;
        }

        // 更新配置文件中的项目名
        let config_path = new_path.join(".kanban.toml");
        if let Ok(mut content) = std::fs::read_to_string(&config_path) {
            // 简单替换项目名(第一行)
            let lines: Vec<&str> = content.lines().collect();
            if !lines.is_empty() {
                content = format!("name = \"{}\"\n{}", new_name, lines[1..].join("\n"));
                let _ = std::fs::write(&config_path, content);
            }
        }

        // 重新加载所有项目
        match crate::fs::load_all_projects() {
            Ok(projects) => {
                app.projects = projects;
                // 更新当前面板的项目ID
                app.set_focused_project(new_name);
            }
            Err(e) => {
                log_debug(format!("重新加载项目失败: {}", e));
            }
        }
    }
}

/// 更新任务标题
fn update_task_title(app: &mut App, new_title: String) {
    // 获取任务 ID
    let task_id = if let Some(id) = get_selected_task_id(app) {
        id
    } else {
        return;
    };

    // 获取项目名称
    let project_name = if let Some(crate::ui::layout::SplitNode::Leaf { project_id: Some(name), .. }) =
        app.split_tree.find_pane(app.focused_pane)
    {
        name.clone()
    } else {
        return;
    };

    // 找到任务并更新
    if let Some(project) = app.projects.iter_mut().find(|p| p.name == project_name)
        && let Some(task) = project.tasks.iter_mut().find(|t| t.id == task_id) {
            let old_title = task.title.clone();
            task.title = new_title;

            // 保存到文件(使用项目的实际路径)
            let project_path = project.path.clone();
            if let Err(e) = crate::fs::save_task(&project_path, task) {
                log_debug(format!("保存任务失败: {}", e));
                task.title = old_title; // 回滚
            }
        }
}

/// 更新任务标签
fn update_task_tags(app: &mut App, tags_string: String) {
    // 获取任务 ID
    let task_id = if let Some(id) = get_selected_task_id(app) {
        id
    } else {
        return;
    };

    // 获取项目名称
    let project_name = if let Some(crate::ui::layout::SplitNode::Leaf { project_id: Some(name), .. }) =
        app.split_tree.find_pane(app.focused_pane)
    {
        name.clone()
    } else {
        return;
    };

    // 解析标签(从逗号分隔的字符串)
    let new_tags: Vec<String> = tags_string
        .split(',')
        .map(|s| s.trim().to_string())
        .filter(|s| !s.is_empty())
        .collect();

    // 找到任务并更新
    let mut result = Ok(());
    if let Some(project) = app.projects.iter_mut().find(|p| p.name == project_name)
        && let Some(task) = project.tasks.iter_mut().find(|t| t.id == task_id) {
            let old_tags = task.tags.clone();
            task.tags = new_tags;

            // 保存到文件(使用项目的实际路径)
            let project_path = project.path.clone();
            if let Err(e) = crate::fs::save_task(&project_path, task) {
                result = Err(e);
                task.tags = old_tags; // 回滚
            }
        }

    // 显示通知
    match result {
        Ok(_) => {
            app.show_notification(
                "标签已更新".to_string(),
                crate::app::NotificationLevel::Success,
            );
        }
        Err(e) => {
            app.show_notification(
                format!("保存标签失败: {}", e),
                crate::app::NotificationLevel::Error,
            );
        }
    }
}

/// 处理帮助模式的按键
fn handle_help_mode(app: &mut App, key: KeyEvent) -> bool {
    match key.code {
        KeyCode::Esc | KeyCode::Char('?') => {
            app.mode = Mode::Normal;
        }
        _ => {}
    }
    true
}

/// 处理空格菜单模式的按键
fn handle_space_menu_mode(app: &mut App, key: KeyEvent) -> bool {
    use crate::app::MenuState;

    match key.code {
        KeyCode::Esc => {
            // ESC:子菜单返回主菜单,主菜单退出
            match app.menu_state {
                Some(MenuState::Main) | None => {
                    app.menu_state = None;
                    app.menu_selected_index = None;
                    app.mode = Mode::Normal;
                    app.key_buffer.clear();
                }
                Some(_) => {
                    app.menu_state = Some(MenuState::Main);
                    app.menu_selected_index = Some(0); // 返回主菜单时重置选中索引
                }
            }
        }
        KeyCode::Up => {
            // 向上导航菜单
            navigate_menu_up(app);
        }
        KeyCode::Down => {
            // 向下导航菜单
            navigate_menu_down(app);
        }
        KeyCode::Enter => {
            // 执行选中的命令
            if let Some(idx) = app.menu_selected_index {
                execute_selected_menu_command(app, idx);
                return true;
            }
        }
        KeyCode::Char(c) => {
            match app.menu_state {
                Some(MenuState::Main) => {
                    // 主菜单:切换到子菜单或执行命令
                    match c {
                        'p' => app.menu_state = Some(MenuState::Project),
                        'w' => app.menu_state = Some(MenuState::Window),
                        't' => app.menu_state = Some(MenuState::Task),
                        's' => app.menu_state = Some(MenuState::Status),
                        'f' => {
                            // 快速切换项目
                            app.mode = Mode::Normal;
                            app.menu_state = None;
                            app.key_buffer.clear();
                            execute_command(app, Command::OpenProject);
                        }
                        'r' => {
                            // 重新加载当前项目
                            app.mode = Mode::Normal;
                            app.menu_state = None;
                            app.key_buffer.clear();
                            execute_command(app, Command::ReloadCurrentProject);
                        }
                        'R' => {
                            // 重新加载所有项目
                            app.mode = Mode::Normal;
                            app.menu_state = None;
                            app.key_buffer.clear();
                            execute_command(app, Command::ReloadAllProjects);
                        }
                        'q' => {
                            // 退出应用
                            app.mode = Mode::Normal;
                            app.menu_state = None;
                            app.key_buffer.clear();
                            execute_command(app, Command::Quit);
                            return false;
                        }
                        '?' => {
                            app.mode = Mode::Help;
                            app.menu_state = None;
                            app.key_buffer.clear();
                        }
                        _ => {}
                    }
                }
                Some(MenuState::Project) => {
                    // 项目子菜单:立即执行命令并退出菜单
                    let cmd = match c {
                        'o' => Some(Command::OpenProject),
                        'n' => Some(Command::NewLocalProject),
                        'N' => Some(Command::NewGlobalProject),
                        'd' => Some(Command::HideProject), // 小写d = 软删除(隐藏)
                        'D' => Some(Command::DeleteProject), // 大写D = 硬删除
                        'r' => Some(Command::RenameProject),
                        'i' => Some(Command::CopyProjectInfo), // 复制项目信息
                        _ => None,
                    };
                    if let Some(cmd) = cmd {
                        app.mode = Mode::Normal;
                        app.menu_state = None;
                        app.key_buffer.clear();
                        execute_command(app, cmd);
                    }
                }
                Some(MenuState::Window) => {
                    // 窗口子菜单:立即执行命令并退出菜单
                    let cmd = match c {
                        'w' => Some(Command::FocusNextPane),
                        'v' => Some(Command::SplitVertical),
                        's' => Some(Command::SplitHorizontal),
                        'q' => Some(Command::ClosePane),
                        'h' => Some(Command::FocusLeft),
                        'l' => Some(Command::FocusRight),
                        'k' => Some(Command::FocusUp),
                        'j' => Some(Command::FocusDown),
                        'm' => Some(Command::MaximizePane),
                        _ => None,
                    };
                    if let Some(cmd) = cmd {
                        log_debug(format!("执行窗口命令: {:?}", cmd));
                        app.mode = Mode::Normal;
                        app.menu_state = None;
                        app.key_buffer.clear();
                        execute_command(app, cmd);
                    }
                }
                Some(MenuState::Task) => {
                    // 任务子菜单:立即执行命令并退出菜单
                    let cmd = match c {
                        'a' => Some(Command::NewTask), // 改为 a 键新建任务
                        'e' => Some(Command::EditTask),
                        'E' => Some(Command::EditTaskInEditor),
                        'v' => Some(Command::ViewTask),
                        'V' => Some(Command::ViewTaskExternal),
                        't' => Some(Command::EditTags), // 编辑标签
                        'd' => Some(Command::DeleteTask),
                        'Y' => Some(Command::CopyTask), // 大写 Y 复制任务
                        'h' => Some(Command::SetTaskPriority("high".to_string())),
                        'm' => Some(Command::SetTaskPriority("medium".to_string())),
                        'l' => Some(Command::SetTaskPriority("low".to_string())),
                        'n' => Some(Command::SetTaskPriority("none".to_string())),
                        _ => None,
                    };
                    if let Some(cmd) = cmd {
                        app.mode = Mode::Normal;
                        app.menu_state = None;
                        app.key_buffer.clear();
                        execute_command(app, cmd);
                    }
                }
                Some(MenuState::Status) => {
                    // 状态子菜单:立即执行命令并退出菜单
                    let cmd = match c {
                        'a' => Some(Command::CreateStatus),
                        'r' => Some(Command::RenameStatus),
                        'e' => Some(Command::EditStatusDisplay),
                        'h' => Some(Command::MoveStatusLeft),
                        'l' => Some(Command::MoveStatusRight),
                        'H' => Some(Command::MoveStatusToFirst), // 大写 H 移到最左侧
                        'L' => Some(Command::MoveStatusToLast),  // 大写 L 移到最右侧
                        'd' => Some(Command::DeleteStatus),
                        _ => None,
                    };
                    if let Some(cmd) = cmd {
                        app.mode = Mode::Normal;
                        app.menu_state = None;
                        app.key_buffer.clear();
                        execute_command(app, cmd);
                    }
                }
                None => {}
            }
        }
        _ => {}
    }
    true
}

/// 处理预览模式的按键
fn handle_preview_mode(app: &mut App, key: KeyEvent) -> bool {
    match key.code {
        KeyCode::Esc => {
            app.mode = Mode::Normal;
            app.preview_content.clear();
            app.preview_scroll = 0;
        }
         KeyCode::Down => {
            // 向下滚动
            app.preview_scroll = app.preview_scroll.saturating_add(1);
        }
         KeyCode::Up => {
            // 向上滚动
            app.preview_scroll = app.preview_scroll.saturating_sub(1);
        }
        _ => {}
    }
    true
}

/// 获取当前光标所在行的起始位置
#[allow(dead_code)]
fn get_line_start(text: &str, cursor_pos: usize) -> usize {
    let chars: Vec<char> = text.chars().collect();

    // 从光标位置向前查找换行符
    let mut pos = cursor_pos;
    while pos > 0 {
        if chars[pos - 1] == '\n' {
            return pos;
        }
        pos -= 1;
    }
    0 // 第一行的起始位置
}

/// 获取当前光标所在行的结束位置
#[allow(dead_code)]
fn get_line_end(text: &str, cursor_pos: usize) -> usize {
    let chars: Vec<char> = text.chars().collect();
    let len = chars.len();

    // 从光标位置向后查找换行符
    let mut pos = cursor_pos;
    while pos < len {
        if chars[pos] == '\n' {
            return pos;
        }
        pos += 1;
    }
    len // 最后一行的结束位置
}

/// 垂直移动光标(上下移动行)
/// direction: -1 表示上移,1 表示下移
#[allow(dead_code)]
fn move_cursor_vertical(text: &str, cursor_pos: usize, direction: i32) -> usize {
    let chars: Vec<char> = text.chars().collect();
    let len = chars.len();

    if len == 0 {
        return 0;
    }

    // 获取当前行的起始和结束位置
    let current_line_start = get_line_start(text, cursor_pos);
    let current_line_end = get_line_end(text, cursor_pos);

    // 计算当前列位置
    let column = cursor_pos - current_line_start;

    if direction < 0 {
        // 向上移动
        if current_line_start == 0 {
            // 已经在第一行,移动到行首
            return 0;
        }

        // 找到上一行的起始位置(current_line_start - 1 是上一行的换行符)
        let prev_line_end = current_line_start - 1;
        let prev_line_start = get_line_start(text, prev_line_end.saturating_sub(1));
        let prev_line_len = prev_line_end - prev_line_start;

        // 移动到上一行的相同列位置,或行尾(如果上一行更短)
        prev_line_start + column.min(prev_line_len)
    } else {
        // 向下移动
        if current_line_end >= len {
            // 已经在最后一行,移动到行尾
            return len;
        }

        // 找到下一行的起始位置(current_line_end 是当前行的换行符)
        let next_line_start = current_line_end + 1;
        let next_line_end = get_line_end(text, next_line_start);
        let next_line_len = next_line_end - next_line_start;

        // 移动到下一行的相同列位置,或行尾(如果下一行更短)
        next_line_start + column.min(next_line_len)
    }
}

/// 重平衡列内任务的order值,使其均匀分布
fn rebalance_order_in_column(tasks: &mut [&mut crate::models::Task]) {
    // tasks已按order排序
    for (idx, task) in tasks.iter_mut().enumerate() {
        task.order = (idx as i32) * 1000;
    }
}

/// 调整当前列的宽度
fn adjust_column_width(app: &mut App, delta: i16) {
    let project_name = match get_focused_project_name(app) {
        Some(name) => name,
        None => return,
    };

    let project = match app.get_focused_project() {
        Some(p) => p,
        None => return,
    };

    let column = app
        .selected_column
        .get(&app.focused_pane)
        .copied()
        .unwrap_or(0);
    let num_columns = project.statuses.len();

    // 获取或初始化列宽配置
    let widths = app
        .config
        .column_widths
        .entry(project_name.clone())
        .or_insert_with(|| vec![100 / num_columns as u16; num_columns]);

    if column >= widths.len() {
        return;
    }

    // 计算新宽度(限制在 10%-80% 之间)
    let new_width = (widths[column] as i16 + delta).clamp(10, 80) as u16;
    let old_width = widths[column];
    let diff = new_width as i16 - old_width as i16;

    if diff == 0 {
        return;
    }

    // 调整当前列宽度
    widths[column] = new_width;

    // 从其他列平均分配/回收空间
    let other_count = num_columns - 1;
    if other_count > 0 {
        let per_column_adjust = -(diff as f32 / other_count as f32);
        for (i, width) in widths.iter_mut().enumerate() {
            if i != column {
                let adjusted = (*width as f32 + per_column_adjust).clamp(5.0, 95.0) as u16;
                *width = adjusted;
            }
        }
    }

    // 确保总和为 100%
    normalize_widths(widths);

    // 保存配置
    if let Err(e) = crate::config::save_config(&app.config) {
        log_debug(format!("保存配置失败: {}", e));
    }

    // 记录调整时间
    app.last_column_resize_time = Some(std::time::Instant::now());
}

/// 重置为等宽
fn reset_column_widths(app: &mut App) {
    let project_name = match get_focused_project_name(app) {
        Some(name) => name,
        None => return,
    };

    // 移除配置和最大化状态
    app.config.column_widths.remove(&project_name);
    app.config.maximized_column.remove(&project_name);

    // 保存配置
    if let Err(e) = crate::config::save_config(&app.config) {
        log_debug(format!("保存配置失败: {}", e));
    }
}

/// 切换最大化当前列
fn toggle_maximize_column(app: &mut App) {
    log_debug("调用 toggle_maximize_column".to_string());

    let project_name = match get_focused_project_name(app) {
        Some(name) => name,
        None => {
            log_debug("无法获取项目名称".to_string());
            return;
        }
    };

    let column = app
        .selected_column
        .get(&app.focused_pane)
        .copied()
        .unwrap_or(0);
    log_debug(format!("当前列: {}, 项目: {}", column, project_name));

    // 获取当前最大化状态
    let current_max = app
        .config
        .maximized_column
        .get(&project_name)
        .and_then(|&opt| opt);

    log_debug(format!("当前最大化状态: {:?}", current_max));

    // 切换状态
    if current_max == Some(column) {
        // 已最大化当前列 -> 取消最大化
        log_debug("取消最大化".to_string());
        app.config
            .maximized_column
            .insert(project_name.clone(), None);
    } else {
        // 最大化当前列
        log_debug(format!("最大化列 {}", column));
        app.config
            .maximized_column
            .insert(project_name.clone(), Some(column));
    }

    // 保存配置
    if let Err(e) = crate::config::save_config(&app.config) {
        log_debug(format!("保存配置失败: {}", e));
    }

    // 记录调整时间
    app.last_column_resize_time = Some(std::time::Instant::now());
}

/// 归一化列宽,确保总和为 100%
fn normalize_widths(widths: &mut [u16]) {
    let total: u16 = widths.iter().sum();
    if total == 100 {
        return;
    }

    // 按比例调整
    let scale = 100.0 / total as f32;
    for width in widths.iter_mut() {
        *width = (*width as f32 * scale).round() as u16;
    }

    // 处理舍入误差
    let new_total: u16 = widths.iter().sum();
    if new_total != 100 && !widths.is_empty() {
        let diff = 100i16 - new_total as i16;
        widths[0] = (widths[0] as i16 + diff) as u16;
    }
}

/// 获取当前聚焦项目的名称
fn get_focused_project_name(app: &App) -> Option<String> {
    if let Some(crate::ui::layout::SplitNode::Leaf { project_id, .. }) =
        app.split_tree.find_pane(app.focused_pane)
    {
        project_id.clone()
    } else {
        None
    }
}

/// 获取当前菜单的命令列表(不含空行)
fn get_menu_commands(menu_state: Option<crate::app::MenuState>) -> Vec<char> {
    use crate::app::MenuState;

    match menu_state {
        Some(MenuState::Main) | None => {
            vec!['f', 'p', 'w', 't', 's', 'r', 'R', '?', 'q']
        }
        Some(MenuState::Project) => {
            vec!['o', 'n', 'N', 'd', 'D', 'r', 'i']
        }
        Some(MenuState::Window) => {
            vec!['w', 'v', 's', 'q', 'm', 'h', 'l', 'k', 'j']
        }
        Some(MenuState::Task) => {
            vec!['a', 'e', 'E', 'v', 'V', 't', 'Y', 'd', 'h', 'm', 'l', 'n']
        }
        Some(MenuState::Status) => {
            vec!['a', 'r', 'e', 'h', 'l', 'd']
        }
    }
}

/// 向上导航菜单(跳过空行)
fn navigate_menu_up(app: &mut App) {
    let commands = get_menu_commands(app.menu_state);
    if commands.is_empty() {
        return;
    }

    let current = app.menu_selected_index.unwrap_or(0);
    if current > 0 {
        app.menu_selected_index = Some(current - 1);
    } else {
        // 循环到最后一项
        app.menu_selected_index = Some(commands.len() - 1);
    }
}

/// 向下导航菜单(跳过空行)
fn navigate_menu_down(app: &mut App) {
    let commands = get_menu_commands(app.menu_state);
    if commands.is_empty() {
        return;
    }

    let current = app.menu_selected_index.unwrap_or(0);
    if current < commands.len() - 1 {
        app.menu_selected_index = Some(current + 1);
    } else {
        // 循环到第一项
        app.menu_selected_index = Some(0);
    }
}

/// 执行选中的菜单命令
fn execute_selected_menu_command(app: &mut App, index: usize) {
    use crate::app::MenuState;

    let commands = get_menu_commands(app.menu_state);
    if index >= commands.len() {
        return;
    }

    let c = commands[index];

    // 执行命令(复用现有的字符处理逻辑)
    match app.menu_state {
        Some(MenuState::Main) => match c {
            'p' => {
                app.menu_state = Some(MenuState::Project);
                app.menu_selected_index = Some(0);
            }
            'w' => {
                app.menu_state = Some(MenuState::Window);
                app.menu_selected_index = Some(0);
            }
            't' => {
                app.menu_state = Some(MenuState::Task);
                app.menu_selected_index = Some(0);
            }
            's' => {
                app.menu_state = Some(MenuState::Status);
                app.menu_selected_index = Some(0);
            }
            'f' => {
                app.mode = Mode::Normal;
                app.menu_state = None;
                app.menu_selected_index = None;
                app.key_buffer.clear();
                execute_command(app, Command::OpenProject);
            }
            'r' => {
                app.mode = Mode::Normal;
                app.menu_state = None;
                app.menu_selected_index = None;
                app.key_buffer.clear();
                execute_command(app, Command::ReloadCurrentProject);
            }
            'R' => {
                app.mode = Mode::Normal;
                app.menu_state = None;
                app.menu_selected_index = None;
                app.key_buffer.clear();
                execute_command(app, Command::ReloadAllProjects);
            }
            'q' => {
                app.mode = Mode::Normal;
                app.menu_state = None;
                app.menu_selected_index = None;
                app.key_buffer.clear();
                execute_command(app, Command::Quit);
            }
            '?' => {
                app.mode = Mode::Help;
                app.menu_state = None;
                app.menu_selected_index = None;
                app.key_buffer.clear();
            }
            _ => {}
        },
        Some(MenuState::Project) => {
            let cmd = match c {
                'o' => Some(Command::OpenProject),
                'n' => Some(Command::NewLocalProject),
                'N' => Some(Command::NewGlobalProject),
                'd' => Some(Command::HideProject),
                'D' => Some(Command::DeleteProject),
                'r' => Some(Command::RenameProject),
                'i' => Some(Command::CopyProjectInfo),
                _ => None,
            };
            if let Some(cmd) = cmd {
                app.mode = Mode::Normal;
                app.menu_state = None;
                app.menu_selected_index = None;
                app.key_buffer.clear();
                execute_command(app, cmd);
            }
        }
        Some(MenuState::Window) => {
            let cmd = match c {
                'w' => Some(Command::FocusNextPane),
                'v' => Some(Command::SplitVertical),
                's' => Some(Command::SplitHorizontal),
                'q' => Some(Command::ClosePane),
                'h' => Some(Command::FocusLeft),
                'l' => Some(Command::FocusRight),
                'k' => Some(Command::FocusUp),
                'j' => Some(Command::FocusDown),
                'm' => Some(Command::MaximizePane),
                _ => None,
            };
            if let Some(cmd) = cmd {
                app.mode = Mode::Normal;
                app.menu_state = None;
                app.menu_selected_index = None;
                app.key_buffer.clear();
                execute_command(app, cmd);
            }
        }
        Some(MenuState::Task) => {
            let cmd = match c {
                'a' => Some(Command::NewTask),
                'e' => Some(Command::EditTask),
                'E' => Some(Command::EditTaskInEditor),
                'v' => Some(Command::ViewTask),
                'V' => Some(Command::ViewTaskExternal),
                't' => Some(Command::EditTags),
                'd' => Some(Command::DeleteTask),
                'Y' => Some(Command::CopyTask),
                'h' => Some(Command::SetTaskPriority("high".to_string())),
                'm' => Some(Command::SetTaskPriority("medium".to_string())),
                'l' => Some(Command::SetTaskPriority("low".to_string())),
                'n' => Some(Command::SetTaskPriority("none".to_string())),
                _ => None,
            };
            if let Some(cmd) = cmd {
                app.mode = Mode::Normal;
                app.menu_state = None;
                app.menu_selected_index = None;
                app.key_buffer.clear();
                execute_command(app, cmd);
            }
        }
        Some(MenuState::Status) => {
            let cmd = match c {
                'a' => Some(Command::CreateStatus),
                'r' => Some(Command::RenameStatus),
                'e' => Some(Command::EditStatusDisplay),
                'h' => Some(Command::MoveStatusLeft),
                'l' => Some(Command::MoveStatusRight),
                'd' => Some(Command::DeleteStatus),
                _ => None,
            };
            if let Some(cmd) = cmd {
                app.mode = Mode::Normal;
                app.menu_state = None;
                app.menu_selected_index = None;
                app.key_buffer.clear();
                execute_command(app, cmd);
            }
        }
        None => {}
    }
}