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
use std::sync::Arc;
use std::time::Duration;
use async_trait::async_trait;
use kimun_core::NoteVault;
use kimun_core::error::{FSError, VaultError};
use kimun_core::nfs::VaultPath;
use ratatui::layout::{Constraint, Direction, Layout};
use ratatui::style::Style;
use ratatui::widgets::{Block, Borders, Paragraph};
use crate::app_screen::overlay_host::OverlayHost;
use crate::app_screen::panel_set::PanelSet;
use crate::app_screen::{AppScreen, ScreenKind};
use crate::components::autosave_timer::AutosaveTimer;
use crate::components::backlinks_panel::QueryPanel;
use crate::components::dialogs::ActiveDialog;
use crate::components::event_state::EventState;
use crate::components::events::{AppEvent, AppTx, InputEvent, SaveSource, ScreenEvent, SortTarget};
use crate::components::footer_bar::FooterBar;
use crate::components::note_browser::NoteBrowserModal;
use crate::components::note_browser::file_finder_provider::FileFinderProvider;
use crate::components::note_browser::search_provider::SearchNotesProvider;
use crate::components::overlay::{Overlay, OverlayKind, OverlayMsg};
use crate::components::panel::PanelKind;
use crate::components::saved_searches_modal::SavedSearchesModal;
use crate::components::sidebar::SidebarComponent;
use crate::components::text_editor::TextEditorComponent;
use crate::keys::action_shortcuts::{ActionShortcuts, TextAction};
use crate::keys::key_event_to_combo;
use crate::keys::key_strike::KeyStrike;
use crate::settings::SharedSettings;
use crate::settings::icons::Icons;
use crate::settings::themes::Theme;
use crate::util::single_slot_task::SingleSlotTask;
/// Hard cap on every blocking save path so a stuck disk (NFS hang,
/// fsync stall) cannot freeze quit, navigation, or the next autosave
/// tick. The same value is used both to wait for an in-flight
/// background autosave and to bound our own synchronous save call —
/// the worst-case quit time is therefore one cap plus the
/// observability of `abort()` on a syscall in progress.
const SAVE_TIMEOUT: Duration = Duration::from_secs(5);
pub struct EditorScreen {
vault: Arc<NoteVault>,
settings: SharedSettings,
icons: Icons,
theme: Theme,
/// The persistent panels (sidebar, editor, Query panel) plus their order,
/// visibility, and focus. The host reaches a specific panel through the
/// typed accessors (`panels.editor_mut()`, …) for panel-specific calls.
panels: PanelSet,
path: VaultPath,
footer: FooterBar,
autosave: AutosaveTimer,
/// The active overlay, if any. An open overlay intercepts input ahead of
/// the panels; closing it restores focus to the panel that opened it.
overlays: OverlayHost<PanelKind>,
/// Handle to the most recently spawned background autosave task.
/// `is_in_flight()` is the source of truth for "is a save still in
/// flight"; both successful completion AND panic flip it to false
/// so the next periodic tick can spawn fresh. The synchronous save
/// paths (`open_path` / `on_entry_op` / `on_exit`) await this slot
/// before issuing their own `vault.save_note`, so two concurrent
/// writes for the same path can never collide. Drop aborts the
/// in-flight task so the spawned future cannot outlive the screen.
autosave_task: SingleSlotTask<()>,
}
impl EditorScreen {
pub fn new(vault: Arc<NoteVault>, path: VaultPath, settings: SharedSettings) -> Self {
let s = settings.read().unwrap();
let kb = s.key_bindings.clone();
let theme = s.get_theme();
let kb_map = kb.to_hashmap();
let first_key = |action: &ActionShortcuts| {
kb_map
.get(action)
.and_then(|v| v.first().cloned())
.map(|c| c.to_string())
.unwrap_or_default()
};
let footer = FooterBar::new(
first_key(&ActionShortcuts::OpenSettings),
first_key(&ActionShortcuts::Quit),
first_key(&ActionShortcuts::ToggleSidebar),
first_key(&ActionShortcuts::ToggleQueryPanel),
);
let icons = s.icons();
let sidebar = SidebarComponent::from_settings(vault.clone(), &s);
let backlinks_panel = QueryPanel::new(vault.clone(), kb.clone());
let mut editor = TextEditorComponent::new(kb, &s);
editor.set_vault(vault.clone());
drop(s);
Self {
settings,
icons,
theme,
panels: PanelSet::from_panels(sidebar, editor, backlinks_panel),
vault,
path,
footer,
autosave: AutosaveTimer::new(),
overlays: OverlayHost::new(),
autosave_task: SingleSlotTask::empty(),
}
}
}
/// Encodes raw RGBA pixels as a PNG byte stream.
fn encode_rgba_to_png(width: u32, height: u32, rgba: &[u8]) -> std::io::Result<Vec<u8>> {
let mut buf = Vec::new();
{
let mut encoder = png::Encoder::new(&mut buf, width, height);
encoder.set_color(png::ColorType::Rgba);
encoder.set_depth(png::BitDepth::Eight);
let mut writer = encoder
.write_header()
.map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))?;
writer
.write_image_data(rgba)
.map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))?;
}
Ok(buf)
}
impl EditorScreen {
/// Pulls an image off the system clipboard, encodes it to PNG, saves it as
/// an attachment under the vault's `/assets` directory, and inserts a
/// markdown image link (relative to the current note) at the cursor.
///
/// Returns `true` if a clipboard image was found and the paste was
/// dispatched — even if the encode/save is still in flight. Returns
/// `false` if the clipboard contained no image, so the caller can fall
/// through to a regular text paste.
fn try_paste_image(&mut self, tx: &AppTx) -> bool {
let img = match self.panels.editor_mut().take_clipboard_image() {
Some(i) if !i.rgba.is_empty() && i.width > 0 && i.height > 0 => i,
_ => return false,
};
// arboard contract: rgba length == width * height * 4. A mismatch means
// a misbehaving clipboard provider — refuse rather than encode garbage.
let expected = img
.width
.checked_mul(img.height)
.and_then(|n| n.checked_mul(4));
if expected != Some(img.rgba.len()) {
self.footer
.flash("Clipboard image size mismatch".to_string(), tx);
return true;
}
let asset_path = self.vault.generate_attachment_path("image", "png");
let link_path = asset_path.relative_link_from_note(&self.path);
let markdown = format!("");
let vault = self.vault.clone();
let tx2 = tx.clone();
let width = img.width as u32;
let height = img.height as u32;
let rgba = img.rgba;
tokio::spawn(async move {
// PNG encoding is CPU-bound — keep it off the runtime worker threads.
let png_bytes =
match tokio::task::spawn_blocking(move || encode_rgba_to_png(width, height, &rgba))
.await
{
Ok(Ok(b)) => b,
Ok(Err(e)) => {
tx2.send(AppEvent::DialogError(format!("Image encode failed: {e}")))
.ok();
return;
}
Err(e) => {
tx2.send(AppEvent::DialogError(format!(
"Image encode task failed: {e}"
)))
.ok();
return;
}
};
match vault.save_attachment(&asset_path, &png_bytes).await {
Ok(()) => {
tx2.send(AppEvent::InsertAtCursor(markdown)).ok();
}
Err(e) => {
tx2.send(AppEvent::DialogError(format!("Image save failed: {e}")))
.ok();
}
}
});
true
}
/// Persist a saved search via core. Used by the SaveSearchConfirmed handler
/// and unit tests.
#[cfg(test)]
async fn persist_saved_search(&self, name: &str, query: &str) -> Result<(), VaultError> {
self.vault.save_search(name, query).await
}
async fn follow_link(&mut self, target: String, tx: &AppTx) {
// External URL — hand off to the OS browser/handler.
if kimun_core::note::scan::is_remote_url(&target) {
match open::that_detached(&target) {
Ok(()) => self.footer.flash(format!("Opening {target}"), tx),
Err(e) => self.footer.flash(format!("Cannot open URL: {e}"), tx),
}
return;
}
// Image attachment — resolve the (potentially relative) path against
// the current note's directory, convert to an OS path, hand off to the
// OS default handler. Images are not notes, so skip the note lookup.
if kimun_core::note::scan::target_looks_like_image(&target) {
let parent = self.path.get_parent_path().0;
let resolved = parent.append(&VaultPath::new(target.trim()));
let os_path = self.vault.path_to_pathbuf(&resolved);
match open::that_detached(&os_path) {
Ok(()) => self.footer.flash(format!("Opening {target}"), tx),
Err(e) => self.footer.flash(format!("Cannot open image: {e}"), tx),
}
return;
}
// Note reference — look it up in the vault.
// Strip any `#fragment` suffix before resolving (e.g. `notes/design.md#goals`
// should resolve to `notes/design.md`, not `notes/design.md#goals.md`).
let target_clean = target.split('#').next().unwrap_or(&target).trim_end();
// Resolve the (possibly relative, e.g. `../work/anton.md`) target
// against this note's directory so the existence lookup uses the same
// absolute path the note is stored under. Bare names stay name-lookups.
let path = kimun_core::nfs::VaultPath::note_path_from(target_clean)
.resolve_link_in_note(&self.path);
match self.vault.open_or_search(&path).await {
Ok(results) if results.is_empty() => {
self.present_overlay(Box::new(ActiveDialog::create_note(
path,
self.vault.clone(),
)));
}
Ok(mut results) if results.len() == 1 => {
let (entry, _) = results.remove(0);
self.open_path(entry.path, tx).await;
}
Ok(results) => {
use crate::components::note_browser::link_results_provider::LinkResultsProvider;
let provider = LinkResultsProvider::from_results(results);
let s = self.settings.read().unwrap();
let modal = NoteBrowserModal::new(
format!("Follow: {target}"),
provider,
self.vault.clone(),
s.key_bindings.clone(),
s.icons(),
tx.clone(),
);
drop(s);
self.present_overlay(Box::new(modal));
}
Err(e) => {
self.footer.flash(format!("Link error: {e}"), tx);
}
}
}
pub async fn open_path(&mut self, path: VaultPath, tx: &AppTx) {
if !path.is_note() {
tx.send(AppEvent::OpenScreen(ScreenEvent::OpenBrowse(
self.vault.clone(),
path,
)))
.ok();
return;
}
// Save current note before switching
self.try_save().await;
{
let mut s = self.settings.write().unwrap();
s.add_path_history(&path);
}
let settings_snapshot = self.settings.read().unwrap().clone();
tokio::spawn(async move {
settings_snapshot.save_to_disk().ok();
});
self.path = path.clone();
match self.vault.get_note_text(&self.path).await {
Ok(content) => {
self.panels.editor_mut().set_text(content);
self.panels.editor_mut().set_redraw_tx(tx);
tx.send(AppEvent::Redraw).ok();
if self.panels.is_visible(PanelKind::Query) {
self.panels.query_mut().set_note(path.clone(), tx.clone());
}
}
Err(e) => {
if matches!(e, VaultError::FSError(FSError::VaultPathNotFound { .. })) {
self.present_overlay(Box::new(ActiveDialog::create_note(
self.path.clone(),
self.vault.clone(),
)));
} else {
tracing::error!("Failed to read note {}: {e}", self.path);
let parent = self.path.get_parent_path().0;
tx.send(AppEvent::OpenScreen(ScreenEvent::OpenBrowse(
self.vault.clone(),
parent,
)))
.ok();
}
return;
}
}
// Load the sidebar on first open only; refreshes happen via explicit
// create/rename/delete/move events, not on every note open.
let note_parent = path.get_parent_path().0;
if self.panels.sidebar().is_empty() {
self.navigate_sidebar(note_parent, tx).await;
}
// Abort any existing timer and spawn a fresh one for the new note.
let interval = self.settings.read().unwrap().autosave_interval_secs;
self.autosave.restart(interval, tx.clone());
}
pub async fn navigate_sidebar(&mut self, dir: VaultPath, tx: &AppTx) {
// The sidebar hosts a streamed `SearchList`; (re)building its engine for
// `dir` runs `browse_vault` inside the source and emits rows as they
// arrive (with a redraw on each).
self.panels.sidebar_mut().navigate(dir, tx);
}
async fn try_save(&mut self) {
// Wait out any background autosave so two concurrent `vault.save_note`
// calls cannot race on the same path. Capped at 5s so a wedged
// filesystem (NFS hang, fsync stall, SQLite lock contention) does
// not freeze app-quit indefinitely.
//
// If the timeout fires we MUST abort the prior task and bail
// without issuing our own save: dropping a JoinHandle detaches
// the tokio task rather than cancelling it, so the spawned
// vault.save_note keeps running. Calling our own vault.save_note
// on the same path on top of that is the exact two-writer race
// the in-flight serialisation is meant to prevent. abort() is
// best-effort (will not unwind an in-progress syscall) but it
// stops any further await points in the spawned task. The editor
// stays dirty so the next session retries; the spawned task
// either finishes against the disk on its own or is killed when
// the process exits.
if self.autosave_task.is_in_flight() {
match self.autosave_task.await_with_timeout(SAVE_TIMEOUT).await {
Some(_) => {} // completed (success or panic) — slot already cleared
None => {
// Timeout: abort the spawned task and bail.
self.autosave_task.abort();
return;
}
}
}
if self.panels.editor().is_dirty() {
let text = self.panels.editor().get_text();
// Same cap on our own save so quit cannot hang on a stuck
// disk. A timeout returns Err(_); we skip mark_saved so the
// editor stays dirty for any subsequent retry.
let save = self.vault.save_note(&self.path, &text);
if matches!(tokio::time::timeout(SAVE_TIMEOUT, save).await, Ok(Ok(_))) {
self.panels.editor_mut().mark_saved(text);
}
}
}
/// Fire-and-forget autosave used by the periodic timer. The save runs in
/// a spawned tokio task so the main event loop is never blocked by the
/// filesystem + SQLite write. Completion is reported back as
/// `AppEvent::AutosaveCompleted`, which marks the editor clean iff the
/// editor is still at the revision that was written. `is_in_flight()`
/// on the `SingleSlotTask` slot is the "is a save in flight" signal;
/// it flips to false on both successful completion AND panic, so a
/// single panicked task can never permanently disable autosave.
fn spawn_autosave(&mut self, tx: &AppTx) {
// A previous task that hasn't reported completion yet still holds the
// lock on the file system + SQLite path; let it finish first.
if self.autosave_task.is_in_flight() {
return;
}
if !self.panels.editor().is_dirty() {
return;
}
let text = self.panels.editor().get_text();
let revision = self.panels.editor().content_revision();
let vault = self.vault.clone();
let path = self.path.clone();
let tx = tx.clone();
self.autosave_task.spawn(async move {
let saved_revision = vault.save_note(&path, &text).await.ok().map(|_| revision);
let _ = tx.send(AppEvent::AutosaveCompleted {
path,
saved_revision,
});
});
}
/// The panel to restore focus to when the active overlay closes — the
/// panel that was focused when the overlay opened.
fn opener_focus(&self) -> PanelKind {
self.panels.focused()
}
/// Present `overlay`, recording the currently focused panel as its opener so
/// `dismiss_overlay` can return there on close. The single way the editor
/// opens an overlay — the focus contract lives here, not at each call site.
fn present_overlay(&mut self, overlay: Box<dyn Overlay>) {
let opener = self.opener_focus();
self.overlays.open(overlay, opener);
}
/// Close the active overlay and restore focus to the panel that opened it.
/// The close-side mirror of `present_overlay`. `OverlayHost::close` returns
/// `None` when nothing is open, so this is a no-op then — which is also why
/// a selection that closed the overlay itself (and chose its own focus) is
/// not re-restored by a trailing `CloseOverlay`.
fn dismiss_overlay(&mut self) {
if let Some(opener) = self.overlays.close() {
self.panels.focus(opener);
}
}
async fn on_entry_op(&mut self, from: VaultPath, tx: &AppTx) {
self.dismiss_overlay();
if from == self.path {
self.autosave.stop();
self.try_save().await;
let parent = self.path.get_parent_path().0;
tx.send(AppEvent::OpenScreen(ScreenEvent::OpenBrowse(
self.vault.clone(),
parent,
)))
.ok();
} else if from
.get_parent_path()
.0
.is_like(self.panels.sidebar().current_dir())
{
let dir = self.panels.sidebar().current_dir().clone();
self.navigate_sidebar(dir, tx).await;
}
}
}
impl EditorScreen {
/// The editor owns key input: it is focused and no overlay sits over it.
/// Editor-only shortcuts (file ops, follow-link, find, text styling) and
/// the paste intercept gate on this.
fn editor_active(&self) -> bool {
self.panels.focused() == PanelKind::Editor && !self.overlays.is_open()
}
pub fn focus_editor(&mut self) {
self.panels.focus(PanelKind::Editor);
}
pub fn focus_sidebar(&mut self) {
self.panels.show(PanelKind::Sidebar);
self.panels.focus(PanelKind::Sidebar);
}
/// Move focus to `kind`, revealing it first. Revealing the Query panel
/// loads it for the current note (the heavy side effect that keeps the
/// reveal in the host rather than in `PanelSet`).
fn move_focus_to(&mut self, kind: PanelKind, tx: &AppTx) {
let newly_shown = !self.panels.is_visible(kind);
self.panels.show(kind);
if kind == PanelKind::Query && newly_shown {
self.panels
.query_mut()
.set_note(self.path.clone(), tx.clone());
}
self.panels.focus(kind);
}
/// Move focus one panel left in the current order (revealing it).
fn focus_left(&mut self, tx: &AppTx) {
if let Some(kind) = self.panels.prev_kind() {
self.move_focus_to(kind, tx);
}
}
/// Move focus one panel right in the current order (revealing it).
fn focus_right(&mut self, tx: &AppTx) {
if let Some(kind) = self.panels.next_kind() {
self.move_focus_to(kind, tx);
}
}
fn toggle_sidebar(&mut self) {
if self.panels.is_visible(PanelKind::Sidebar) {
self.panels.hide(PanelKind::Sidebar);
} else {
self.panels.show(PanelKind::Sidebar);
}
}
/// The query the save-current-query action would save, with its
/// saved-search provenance (breadcrumb name) for the dialog's name
/// pre-fill. Sourced from the active note browser if one is open (Ctrl+K
/// modal), otherwise from the Query panel. `None` when there is nothing
/// to save: a blank query, or another overlay is open.
fn save_query_source(&self) -> Option<(String, Option<String>, SaveSource)> {
let (query, provenance, source) = match self.overlays.active_kind() {
Some(OverlayKind::NoteBrowser) => (
self.overlays.active_query().unwrap_or_default().to_string(),
self.overlays
.active_saved_search_provenance()
.map(str::to_string),
SaveSource::NoteBrowser,
),
None => (
self.panels.query().active_query().to_string(),
self.panels.query().saved_search_name().map(str::to_string),
SaveSource::QueryPanel,
),
Some(_) => return None,
};
if query.trim().is_empty() {
None
} else {
Some((query, provenance, source))
}
}
fn apply_saved_search(&mut self, query: String, name: String, tx: &AppTx) {
self.panels.show(PanelKind::Query);
// The virtual backlinks entry's name should not override the
// default "Backlinks" title — but the panel's title logic already
// shows "Backlinks" whenever the active query is `<{note}`, so it's
// safe to always pass the name through.
self.panels
.query_mut()
.apply_query(query, Some(name), tx.clone());
self.panels.focus(PanelKind::Query);
}
fn toggle_backlinks(&mut self, tx: &AppTx) {
if self.panels.is_visible(PanelKind::Query) {
self.panels.hide(PanelKind::Query);
} else {
self.panels.show(PanelKind::Query);
self.panels
.query_mut()
.set_note(self.path.clone(), tx.clone());
self.panels.focus(PanelKind::Query);
}
}
}
#[async_trait]
impl AppScreen for EditorScreen {
fn get_kind(&self) -> ScreenKind {
ScreenKind::Editor
}
async fn on_enter(&mut self, tx: &AppTx) {
self.open_path(self.path.clone(), tx).await;
}
fn handle_input(&mut self, event: &InputEvent, tx: &AppTx) -> EventState {
// Bracketed paste (terminal-level) — fired by Cmd+V on macOS and by
// terminal-paste shortcuts on every platform. Try image first; if the
// clipboard does not hold an image, fall back to the pasted text. The
// payload string may be empty (e.g. clipboard contains image only).
if self.editor_active()
&& let InputEvent::Paste(text) = event
{
if !self.try_paste_image(tx) && !text.is_empty() {
self.panels.editor_mut().paste_text(text, tx);
}
return EventState::Consumed;
}
// Intercept Ctrl+V to handle image paste before the editor consumes it
// for a regular text paste. Falls through if the clipboard is not an image.
if self.editor_active()
&& let InputEvent::Key(key) = event
&& key.modifiers == ratatui::crossterm::event::KeyModifiers::CONTROL
&& key.code == ratatui::crossterm::event::KeyCode::Char('v')
&& self.try_paste_image(tx)
{
return EventState::Consumed;
}
if let InputEvent::Key(key) = event
&& let Some(combo) = key_event_to_combo(key)
{
let is_fkey = matches!(
combo.key,
KeyStrike::F1
| KeyStrike::F2
| KeyStrike::F3
| KeyStrike::F4
| KeyStrike::F5
| KeyStrike::F6
| KeyStrike::F7
| KeyStrike::F8
| KeyStrike::F9
| KeyStrike::F10
| KeyStrike::F11
| KeyStrike::F12
);
if is_fkey
|| ((combo.modifiers.is_ctrl() || combo.modifiers.is_alt())
&& combo.key >= KeyStrike::KeyA
&& combo.key <= KeyStrike::KeyZ)
{
self.footer.flash(combo.to_string(), tx);
}
let action = {
let s = self.settings.read().unwrap();
s.key_bindings.get_action(&combo)
};
match action {
Some(ActionShortcuts::ToggleSidebar) => {
self.toggle_sidebar();
return EventState::Consumed;
}
Some(ActionShortcuts::FocusSidebar) => {
// No-op while an overlay owns input, but still consume the key.
if !self.overlays.is_open() {
self.focus_left(tx);
}
return EventState::Consumed;
}
Some(ActionShortcuts::FocusEditor) => {
if !self.overlays.is_open() {
self.focus_right(tx);
}
return EventState::Consumed;
}
Some(ActionShortcuts::NewJournal) => {
tx.send(AppEvent::OpenJournal).ok();
return EventState::Consumed;
}
Some(ActionShortcuts::SearchNotes) => {
if self.overlays.active_kind() == Some(OverlayKind::NoteBrowser) {
self.dismiss_overlay();
} else if !self.overlays.is_open() {
let s = self.settings.read().unwrap();
let provider = SearchNotesProvider::new(
self.vault.clone(),
s.current_last_paths(),
Some(self.path.clone()),
);
let modal = NoteBrowserModal::new(
"Note Browser",
provider,
self.vault.clone(),
s.key_bindings.clone(),
s.icons(),
tx.clone(),
);
drop(s);
self.present_overlay(Box::new(modal));
}
return EventState::Consumed;
}
Some(ActionShortcuts::OpenNote) => {
if self.overlays.active_kind() == Some(OverlayKind::NoteBrowser) {
self.dismiss_overlay();
} else if !self.overlays.is_open() {
let current_dir = self.path.get_parent_path().0;
let provider = FileFinderProvider::new(self.vault.clone(), current_dir);
let s = self.settings.read().unwrap();
let modal = NoteBrowserModal::new(
"Find Note",
provider,
self.vault.clone(),
s.key_bindings.clone(),
s.icons(),
tx.clone(),
);
drop(s);
self.present_overlay(Box::new(modal));
}
return EventState::Consumed;
}
Some(ActionShortcuts::FileOperations) if self.editor_active() => {
tx.send(AppEvent::ShowFileOpsMenu(self.path.clone())).ok();
return EventState::Consumed;
}
Some(ActionShortcuts::FollowLink) if self.editor_active() => {
use crate::components::text_editor::LinkTarget;
match self.panels.editor_mut().link_at_cursor() {
Some(LinkTarget::Note(target)) => {
tx.send(AppEvent::FollowLink(target)).ok();
}
Some(LinkTarget::Label(name)) => {
tx.send(AppEvent::FollowLabel(name)).ok();
}
None => {}
}
return EventState::Consumed;
}
Some(ActionShortcuts::ToggleQueryPanel) => {
self.toggle_backlinks(tx);
return EventState::Consumed;
}
Some(ActionShortcuts::OpenSavedSearches) => {
if self.overlays.active_kind() == Some(OverlayKind::SavedSearches) {
self.dismiss_overlay();
} else if !self.overlays.is_open() {
let s = self.settings.read().unwrap();
let modal = SavedSearchesModal::new(
self.vault.clone(),
s.key_bindings.clone(),
s.icons(),
tx.clone(),
);
drop(s);
self.present_overlay(Box::new(modal));
}
return EventState::Consumed;
}
Some(ActionShortcuts::OpenSortDialog) => {
if !self.overlays.is_open() {
let target = match self.panels.focused() {
PanelKind::Query => Some(SortTarget::Query),
PanelKind::Sidebar => Some(SortTarget::Sidebar),
_ if self.panels.is_visible(PanelKind::Sidebar) => {
Some(SortTarget::Sidebar)
}
_ => None,
};
if let Some(target) = target {
let dialog = match target {
SortTarget::Sidebar => {
let (f, o) = self.panels.sidebar().current_sort();
ActiveDialog::sort(
target,
f,
o,
self.panels.sidebar().group_dirs(),
)
}
SortTarget::Query => {
let (f, o) = self.panels.query().current_order();
ActiveDialog::sort(target, f, o, false)
}
};
self.present_overlay(Box::new(dialog));
}
}
return EventState::Consumed;
}
Some(ActionShortcuts::SaveCurrentQuery) => {
if let Some((query, provenance, source)) = self.save_query_source() {
// Opening the save dialog replaces the note browser (if
// any); the chained-open guard preserves the original
// opener focus.
self.present_overlay(Box::new(ActiveDialog::save_search(
query,
provenance,
source,
self.vault.clone(),
tx,
)));
}
return EventState::Consumed;
}
Some(ActionShortcuts::SwitchWorkspace) => {
if !self.overlays.is_open() {
let s = self.settings.read().unwrap();
let dialog = ActiveDialog::workspace_switcher(&s);
drop(s);
self.present_overlay(Box::new(dialog));
}
return EventState::Consumed;
}
Some(ActionShortcuts::QuickNote) => {
if !self.overlays.is_open() {
self.present_overlay(Box::new(ActiveDialog::quick_note(
self.vault.clone(),
)));
}
return EventState::Consumed;
}
Some(ActionShortcuts::FindInBuffer) if self.editor_active() => {
self.panels.editor_mut().open_or_advance_search();
return EventState::Consumed;
}
Some(ActionShortcuts::Text(
action @ (TextAction::Bold | TextAction::Italic | TextAction::Strikethrough),
)) if self.editor_active() => {
self.panels.editor_mut().apply_text_action(action);
return EventState::Consumed;
}
_ => {
if is_fkey {
// F1 opens the help modal (only when no other dialog is active).
if combo.key == KeyStrike::F1
&& combo.modifiers.is_empty()
&& !self.overlays.is_open()
{
let s = self.settings.read().unwrap();
let dialog = ActiveDialog::help(&s.key_bindings);
drop(s);
self.present_overlay(Box::new(dialog));
}
// All F-keys (including F1 when a dialog is already open) are consumed
// and never forwarded to the embedded editor.
return EventState::Consumed;
}
}
}
}
// An open overlay intercepts all remaining input ahead of the panels.
if self.overlays.is_open() {
return self.overlays.handle_input(event, tx);
}
if matches!(event, InputEvent::Mouse(_)) {
// `PanelSet` hit-tests the panel columns: a click focuses the
// panel under the cursor (one rule for every panel) and the event
// is forwarded to that panel for its internal behavior.
return self.panels.handle_mouse(event, tx);
}
// Keyboard → the focused panel. The Query panel gets first crack (its
// autocomplete popup may consume Esc); on an unhandled Esc it yields
// focus back to the editor.
if self.panels.focused() == PanelKind::Query
&& let InputEvent::Key(key) = event
{
let state = self.panels.handle_input(event, tx);
if state == EventState::NotConsumed
&& key.code == ratatui::crossterm::event::KeyCode::Esc
{
self.focus_editor();
return EventState::Consumed;
}
return state;
}
self.panels.handle_input(event, tx)
}
fn render(&mut self, f: &mut ratatui::Frame) {
let theme = &self.theme;
f.render_widget(
ratatui::widgets::Block::default().style(theme.base_style()),
f.area(),
);
let rows = Layout::default()
.direction(Direction::Vertical)
.constraints([
Constraint::Length(3),
Constraint::Min(0),
Constraint::Length(3),
])
.split(f.area());
let workspace_label = {
let s = self.settings.read().unwrap();
s.workspace_config
.as_ref()
.map(|wc| format!("{} {}", self.icons.workspace, wc.global.current_workspace))
.unwrap_or_default()
};
let header = Block::default()
.title("Kimün")
.borders(Borders::ALL)
.border_style(Style::default().fg(theme.border.to_ratatui()))
.style(theme.base_style())
.title_style(Style::default().fg(theme.accent.to_ratatui()));
let header_inner = header.inner(rows[0]);
f.render_widget(header, rows[0]);
// Split header inner: note path on left, workspace label on right.
let header_cols = Layout::default()
.direction(Direction::Horizontal)
.constraints([
Constraint::Min(0),
Constraint::Length(workspace_label.len() as u16 + 1),
])
.split(header_inner);
f.render_widget(
Paragraph::new(self.path.to_string())
.style(Style::default().fg(theme.fg_secondary.to_ratatui())),
header_cols[0],
);
f.render_widget(
Paragraph::new(workspace_label)
.alignment(ratatui::layout::Alignment::Right)
.style(Style::default().fg(theme.fg_muted.to_ratatui())),
header_cols[1],
);
// The panels lay themselves out (in config order) and render. No panel
// shows its focused highlight while an overlay sits over them.
self.panels
.render(f, rows[1], theme, !self.overlays.is_open());
// Footer reflects the overlay if one is open, otherwise the focused panel.
let (focus_label, hints) = if let Some(kind) = self.overlays.active_kind() {
(kind.label(), self.overlays.hint_shortcuts())
} else {
(self.panels.focused_label(), self.panels.focused_hints())
};
self.footer
.render(f, rows[2], theme, focus_label, &hints, &self.icons);
// Overlay — rendered last so it appears on top of everything.
self.overlays.render(f, f.area(), &self.theme);
}
async fn handle_app_message(&mut self, msg: AppEvent, tx: &AppTx) {
// Route validation / async-result messages to the active overlay first,
// so an open dialog still receives its events. Show*/CloseOverlay are
// NotConsumed by overlays and fall through to the owned match below.
match self.overlays.handle_app_message(&msg, &self.vault, tx) {
OverlayMsg::Consumed => return,
OverlayMsg::NotConsumed => {}
}
match msg {
AppEvent::ShowFileOpsMenu(path) => {
self.present_overlay(Box::new(ActiveDialog::file_ops_menu(path)));
}
AppEvent::ShowDeleteDialog(path) => {
self.present_overlay(Box::new(ActiveDialog::delete(path, self.vault.clone())));
}
AppEvent::ShowRenameDialog(path) => {
self.present_overlay(Box::new(ActiveDialog::rename(path, self.vault.clone())));
}
AppEvent::ShowMoveDialog(path) => {
self.present_overlay(Box::new(ActiveDialog::move_to(
path,
self.vault.clone(),
tx,
)));
}
AppEvent::CloseOverlay => {
// Dismiss-to-opener. Guarded by is_open() on purpose: a
// selection that wants a specific post-close focus
// (OpenPath -> editor, SavedSearchSelected -> Query panel)
// closes the overlay itself first, so a later/!dialog
// CloseOverlay must not re-restore and clobber that focus.
self.dismiss_overlay();
}
AppEvent::SortChanged {
target,
field,
order,
group_directories,
persist,
} => {
match target {
SortTarget::Sidebar if persist => {
// Update the sidebar's in-session per-context default AND
// apply live. `is_current_journal()` is the single source
// of truth for which context this save targets — reused
// for the on-disk settings write below.
let is_journal = self.panels.sidebar().is_current_journal();
self.panels
.sidebar_mut()
.save_default(field, order, group_directories);
{
let mut s = self.settings.write().unwrap();
if is_journal {
s.journal_sort_field =
crate::settings::SortFieldSetting::from(field);
s.journal_sort_order =
crate::settings::SortOrderSetting::from(order);
} else {
s.default_sort_field =
crate::settings::SortFieldSetting::from(field);
s.default_sort_order =
crate::settings::SortOrderSetting::from(order);
}
s.group_directories = group_directories;
}
let snapshot = self.settings.read().unwrap().clone();
tokio::spawn(async move {
snapshot.save_to_disk().ok();
});
}
SortTarget::Sidebar => {
self.panels
.sidebar_mut()
.apply_sort(field, order, group_directories)
}
// The query panel has no persisted default (the order lives
// in the query string); `persist` is always false here.
SortTarget::Query => self.panels.query_mut().apply_sort(field, order, tx),
}
}
AppEvent::Autosave => {
self.spawn_autosave(tx);
}
AppEvent::AutosaveCompleted {
path,
saved_revision,
} => {
if path == self.path
&& let Some(rev) = saved_revision
{
self.panels.editor_mut().mark_saved_at_revision(rev);
}
// `SingleSlotTask::is_in_flight()` flips to false the
// moment the spawned future returns (success or panic),
// so we don't have to clear the slot manually here —
// the next `spawn_autosave` tick will overwrite it.
// Skip explicit cleanup; was previously racy because a
// stale completion arriving after `try_save` had
// already cleared and respawned could wipe the fresh
// handle.
}
AppEvent::FocusSidebar => {
self.focus_sidebar();
}
AppEvent::OpenJournal => {
// Dismiss any open overlay so the journal note isn't loaded
// behind it (mirrors OpenPath / EntryCreated).
self.dismiss_overlay();
if let Ok((details, _)) = self.vault.journal_entry().await {
let path = details.path;
self.open_path(path.clone(), tx).await;
let note_parent = path.get_parent_path().0;
if note_parent.is_like(self.panels.sidebar().current_dir()) {
let dir = self.panels.sidebar().current_dir().clone();
self.navigate_sidebar(dir, tx).await;
}
}
}
AppEvent::SavedSearchSelected { query, name } => {
// Deliberate non-restoring close: the selection lands in the
// Query panel (set by apply_saved_search), not back on the
// overlay's opener — so close the overlay without dismiss_overlay.
self.overlays.close();
self.apply_saved_search(query, name, tx);
}
AppEvent::FollowLink(target) => {
self.follow_link(target, tx).await;
}
AppEvent::FollowLabel(name) => {
let initial = format!("#{name}");
let s = self.settings.read().unwrap();
let provider = SearchNotesProvider::new(
self.vault.clone(),
s.current_last_paths(),
Some(self.path.clone()),
);
let modal = NoteBrowserModal::with_initial_query(
"Note Browser",
provider,
self.vault.clone(),
s.key_bindings.clone(),
s.icons(),
tx.clone(),
initial,
);
drop(s);
self.present_overlay(Box::new(modal));
}
AppEvent::EntryCreated(path) => {
self.dismiss_overlay();
self.open_path(path.clone(), tx).await;
self.focus_editor();
let note_parent = path.get_parent_path().0;
if note_parent.is_like(self.panels.sidebar().current_dir()) {
let dir = self.panels.sidebar().current_dir().clone();
self.navigate_sidebar(dir, tx).await;
}
}
AppEvent::EntryDeleted(path) => {
self.on_entry_op(path, tx).await;
}
AppEvent::EntryRenamed { from, .. } => {
self.on_entry_op(from, tx).await;
}
AppEvent::EntryMoved { from, .. } => {
self.on_entry_op(from, tx).await;
}
AppEvent::SaveSearchConfirmed {
name,
query,
source,
} => {
// Write in the background; the breadcrumb re-pin waits for
// the success event so the UI never claims an unpersisted
// save (see SavedSearchPersisted below).
let vault = self.vault.clone();
let tx = tx.clone();
tokio::spawn(async move {
match vault.save_search(&name, &query).await {
Ok(()) => {
tx.send(AppEvent::SavedSearchPersisted {
name,
query,
source,
})
.ok();
}
Err(e) => {
tracing::warn!("failed to save search '{}': {}", name, e);
tx.send(AppEvent::SavedSearchSaveFailed { name }).ok();
}
}
});
}
// Re-pin the panel breadcrumb to the saved identity: the edited
// marker drops on an update, the name switches on a save-as-new.
// Only for panel-sourced saves (a note-browser save must not
// steal the panel's provenance, even when the query text
// coincides), and not for the query-as-name fallback (a
// breadcrumb that echoes the query is noise).
AppEvent::SavedSearchPersisted {
name,
query,
source: SaveSource::QueryPanel,
} if name.trim() != query.trim() => {
self.panels.query_mut().repin_saved_search(name, &query);
}
AppEvent::SavedSearchSaveFailed { name } => {
self.footer
.flash(format!("Failed to save search '{name}'"), tx);
}
AppEvent::InsertAtCursor(text) if self.panels.focused() == PanelKind::Editor => {
self.panels.editor_mut().insert_at_cursor(&text, tx);
}
_ => {}
}
}
/// The editor handles every path itself: notes open in the buffer,
/// directories navigate the sidebar. Always consumes.
async fn try_open_path(&mut self, path: VaultPath, tx: &AppTx) -> Option<VaultPath> {
self.dismiss_overlay();
if path.is_note() {
self.open_path(path, tx).await;
self.focus_editor();
} else {
self.navigate_sidebar(path, tx).await;
}
None
}
async fn on_exit(&mut self, _tx: &AppTx) {
self.try_save().await;
}
}
#[cfg(test)]
mod tests {
use super::*;
/// Compile-time test: `PanelKind` and `OverlayKind` are usable here.
#[test]
fn panel_kind_labels_and_overlay_kind_compile() {
assert_eq!(PanelKind::Editor.label(), "EDITOR");
assert_eq!(PanelKind::Sidebar.label(), "SIDEBAR");
assert_eq!(PanelKind::Query.label(), "BACKLINKS");
let _kind = OverlayKind::Dialog;
}
/// One screen over a fresh temp vault. Returns the `TempDir` so the vault
/// directory outlives the test body.
async fn test_screen() -> (
EditorScreen,
Arc<NoteVault>,
SharedSettings,
tempfile::TempDir,
) {
use crate::settings::AppSettings;
use kimun_core::VaultConfig;
use std::sync::RwLock;
let dir = tempfile::TempDir::new().unwrap();
let vault = Arc::new(NoteVault::new(VaultConfig::new(dir.path())).await.unwrap());
let settings: SharedSettings = Arc::new(RwLock::new(AppSettings::default()));
let screen = EditorScreen::new(vault.clone(), VaultPath::root(), settings.clone());
(screen, vault, settings, dir)
}
#[tokio::test]
async fn persist_saved_search_writes_via_core() {
let (screen, _, _, _dir) = test_screen().await;
screen.persist_saved_search("t", "#todo").await.unwrap();
let all = screen.vault.list_saved_searches().await.unwrap();
assert!(all.iter().any(|s| s.name == "t" && s.query == "#todo"));
}
#[tokio::test]
async fn applying_saved_search_sets_panel_query_and_focuses_it() {
let (mut screen, _, _, _dir) = test_screen().await;
let (tx, _rx) = tokio::sync::mpsc::unbounded_channel();
screen.apply_saved_search(
"<{note}".to_string(),
"Backlinks (current note)".to_string(),
&tx,
);
assert!(screen.panels.is_visible(PanelKind::Query));
assert_eq!(screen.panels.query().active_query(), "<{note}");
assert_eq!(screen.panels.focused(), PanelKind::Query);
}
#[tokio::test]
async fn saved_search_persisted_repins_panel_breadcrumb() {
let (mut screen, _, _, _dir) = test_screen().await;
let (tx, _rx) = tokio::sync::mpsc::unbounded_channel();
screen.apply_saved_search("#todo".to_string(), "todo".to_string(), &tx);
screen
.panels
.query_mut()
.set_active_query("#todo and #urgent".to_string());
screen
.handle_app_message(
AppEvent::SavedSearchPersisted {
name: "urgent-todos".to_string(),
query: "#todo and #urgent".to_string(),
source: SaveSource::QueryPanel,
},
&tx,
)
.await;
assert_eq!(
screen.panels.query().saved_search_breadcrumb().as_deref(),
Some("urgent-todos"),
"a persisted panel-sourced save re-pins the breadcrumb"
);
}
#[tokio::test]
async fn persisted_note_browser_save_does_not_repin_even_on_equal_query() {
let (mut screen, _, _, _dir) = test_screen().await;
let (tx, _rx) = tokio::sync::mpsc::unbounded_channel();
screen.apply_saved_search("#todo".to_string(), "todo".to_string(), &tx);
// A note-browser-sourced save whose query text happens to equal the
// panel's live query: source identity, not text equality, decides.
screen
.handle_app_message(
AppEvent::SavedSearchPersisted {
name: "inbox".to_string(),
query: "#todo".to_string(),
source: SaveSource::NoteBrowser,
},
&tx,
)
.await;
assert_eq!(
screen.panels.query().saved_search_breadcrumb().as_deref(),
Some("todo"),
"a note-browser save must not steal the panel's provenance"
);
}
#[tokio::test]
async fn persisted_query_as_name_save_skips_repin() {
let (mut screen, _, _, _dir) = test_screen().await;
let (tx, _rx) = tokio::sync::mpsc::unbounded_channel();
screen.apply_saved_search("#todo".to_string(), "todo".to_string(), &tx);
// The empty-name fallback saved the query under its own text. Pinning
// that as the breadcrumb name would just echo the query (CONTEXT.md:
// the breadcrumb is a distinct provenance tag, not the query).
screen
.handle_app_message(
AppEvent::SavedSearchPersisted {
name: "#todo".to_string(),
query: "#todo".to_string(),
source: SaveSource::QueryPanel,
},
&tx,
)
.await;
assert_eq!(
screen.panels.query().saved_search_breadcrumb().as_deref(),
Some("todo"),
"a query-as-name save leaves the breadcrumb alone"
);
}
#[tokio::test]
async fn save_search_confirmed_persists_then_emits_persisted() {
let (mut screen, vault, _, _dir) = test_screen().await;
let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
screen
.handle_app_message(
AppEvent::SaveSearchConfirmed {
name: "mine".to_string(),
query: "#todo".to_string(),
source: SaveSource::QueryPanel,
},
&tx,
)
.await;
// The write runs in a spawned task; Persisted arrives only on success.
let event = tokio::time::timeout(std::time::Duration::from_secs(5), async {
loop {
match rx.recv().await {
Some(e @ AppEvent::SavedSearchPersisted { .. }) => break e,
Some(_) => continue,
None => panic!("channel closed before SavedSearchPersisted"),
}
}
})
.await
.expect("SavedSearchPersisted within timeout");
let AppEvent::SavedSearchPersisted {
name,
query,
source,
} = event
else {
unreachable!()
};
assert_eq!((name.as_str(), query.as_str()), ("mine", "#todo"));
assert_eq!(source, SaveSource::QueryPanel);
let all = vault.list_saved_searches().await.unwrap();
assert!(all.iter().any(|s| s.name == "mine" && s.query == "#todo"));
}
#[tokio::test]
async fn save_query_source_carries_panel_provenance() {
let (mut screen, _, _, _dir) = test_screen().await;
let (tx, _rx) = tokio::sync::mpsc::unbounded_channel();
screen.apply_saved_search("#todo".to_string(), "todo".to_string(), &tx);
assert_eq!(
screen.save_query_source(),
Some((
"#todo".to_string(),
Some("todo".to_string()),
SaveSource::QueryPanel
)),
"the save dialog opens pre-filled with the breadcrumb provenance"
);
}
// The try_save timeout-abort regression tests (commits 55eb49ed +
// 5e28b796) previously lived here against `await_or_abort`. The
// logic now lives in `SingleSlotTask::await_with_timeout` and is
// covered by `single_slot_task_timeout_returns_none_keeps_handle`
// in `crate::util::single_slot_task`.
#[tokio::test]
async fn saved_search_selected_then_close_overlay_keeps_backlinks_focus() {
use crate::settings::AppSettings;
use kimun_core::VaultConfig;
use std::sync::RwLock;
let dir = tempfile::TempDir::new().unwrap();
let vault = Arc::new(NoteVault::new(VaultConfig::new(dir.path())).await.unwrap());
let settings: SharedSettings = Arc::new(RwLock::new(AppSettings::default()));
let mut screen = EditorScreen::new(vault, VaultPath::root(), settings);
let (tx, _rx) = tokio::sync::mpsc::unbounded_channel();
// Replay the exact sequence the saved-searches modal emits on select.
screen
.handle_app_message(
AppEvent::SavedSearchSelected {
query: "<{note}".to_string(),
name: "Backlinks (current note)".to_string(),
},
&tx,
)
.await;
screen.handle_app_message(AppEvent::CloseOverlay, &tx).await;
assert!(
screen.panels.focused() == PanelKind::Query,
"focus should remain on the Query panel after select + close"
);
assert!(!screen.overlays.is_open(), "overlay should be closed");
}
/// Capture-all guard: while an overlay is open, an opener action
/// (QuickNote) must NOT replace it. Drives a real QuickNote keypress
/// through `handle_input` so the guard in the action arm is exercised
/// end-to-end.
#[tokio::test]
async fn opener_action_does_not_replace_open_overlay() {
use crate::settings::AppSettings;
use kimun_core::VaultConfig;
use ratatui::crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use std::sync::RwLock;
let dir = tempfile::TempDir::new().unwrap();
let vault = Arc::new(NoteVault::new(VaultConfig::new(dir.path())).await.unwrap());
let settings: SharedSettings = Arc::new(RwLock::new(AppSettings::default()));
let mut screen = EditorScreen::new(vault.clone(), VaultPath::root(), settings.clone());
let (tx, _rx) = tokio::sync::mpsc::unbounded_channel();
// Force a SavedSearches overlay open and focus it, as if the user had
// opened it via its action.
{
let s = settings.read().unwrap();
let modal = SavedSearchesModal::new(
vault.clone(),
s.key_bindings.clone(),
s.icons(),
tx.clone(),
);
drop(s);
screen.overlays.open(Box::new(modal), screen.opener_focus());
}
assert_eq!(
screen.overlays.active_kind(),
Some(OverlayKind::SavedSearches),
"precondition: SavedSearches overlay is active"
);
// The QuickNote action key (Ctrl+W by default). Assert the binding
// resolves to QuickNote so this test fails loudly if the default
// rebinds, rather than silently exercising the wrong path.
let quick_note_event = KeyEvent::new(KeyCode::Char('w'), KeyModifiers::CONTROL);
{
let s = settings.read().unwrap();
let combo = key_event_to_combo(&quick_note_event).expect("Ctrl+W maps to a combo");
assert_eq!(
s.key_bindings.get_action(&combo),
Some(ActionShortcuts::QuickNote),
"test assumes Ctrl+W is bound to QuickNote"
);
}
screen.handle_input(&InputEvent::Key(quick_note_event), &tx);
// The guard must have suppressed the open: the SavedSearches overlay
// is still active, NOT replaced by the QuickNote dialog.
assert_eq!(
screen.overlays.active_kind(),
Some(OverlayKind::SavedSearches),
"open overlay must not be replaced by a QuickNote opener action"
);
}
/// Focus actions are inert while an overlay owns input: pressing
/// FocusEditor (Ctrl+L) with a dialog open must not reveal or move the
/// panels underneath, so closing the dialog leaves the layout unchanged.
#[tokio::test]
async fn focus_action_is_noop_while_overlay_open() {
use crate::settings::AppSettings;
use kimun_core::VaultConfig;
use ratatui::crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use std::sync::RwLock;
let dir = tempfile::TempDir::new().unwrap();
let vault = Arc::new(NoteVault::new(VaultConfig::new(dir.path())).await.unwrap());
let settings: SharedSettings = Arc::new(RwLock::new(AppSettings::default()));
let mut screen = EditorScreen::new(vault.clone(), VaultPath::root(), settings.clone());
let (tx, _rx) = tokio::sync::mpsc::unbounded_channel();
// Open a SavedSearches overlay (Query panel starts hidden).
{
let s = settings.read().unwrap();
let modal = SavedSearchesModal::new(
vault.clone(),
s.key_bindings.clone(),
s.icons(),
tx.clone(),
);
drop(s);
screen.overlays.open(Box::new(modal), screen.opener_focus());
}
assert!(!screen.panels.is_visible(PanelKind::Query));
// Ctrl+L (FocusEditor / focus right) must be consumed but do nothing.
let focus_right = KeyEvent::new(KeyCode::Char('l'), KeyModifiers::CONTROL);
{
let s = settings.read().unwrap();
let combo = key_event_to_combo(&focus_right).expect("Ctrl+L maps to a combo");
assert_eq!(
s.key_bindings.get_action(&combo),
Some(ActionShortcuts::FocusEditor),
"test assumes Ctrl+L is bound to FocusEditor"
);
}
screen.handle_input(&InputEvent::Key(focus_right), &tx);
assert!(
!screen.panels.is_visible(PanelKind::Query),
"focus action must not reveal a panel while an overlay is open"
);
assert_eq!(
screen.overlays.active_kind(),
Some(OverlayKind::SavedSearches),
"overlay stays active"
);
}
/// Opening the journal while an overlay is up dismisses the overlay, so the
/// journal note isn't loaded behind it (regression for the OpenJournal arm,
/// which used to skip the restore that OpenPath/EntryCreated do).
#[tokio::test(flavor = "multi_thread")]
async fn open_journal_dismisses_open_overlay() {
let vault = crate::test_support::temp_vault("editor-journal").await;
vault.validate_and_init().await.unwrap();
let settings = std::sync::Arc::new(std::sync::RwLock::new(
crate::settings::AppSettings::default(),
));
let mut screen = EditorScreen::new(vault.clone(), VaultPath::root(), settings.clone());
let (tx, _rx) = tokio::sync::mpsc::unbounded_channel();
{
let s = settings.read().unwrap();
let modal = SavedSearchesModal::new(
vault.clone(),
s.key_bindings.clone(),
s.icons(),
tx.clone(),
);
drop(s);
screen.present_overlay(Box::new(modal));
}
assert!(screen.overlays.is_open(), "precondition: overlay open");
screen.handle_app_message(AppEvent::OpenJournal, &tx).await;
assert!(
!screen.overlays.is_open(),
"OpenJournal must dismiss the overlay before loading the note"
);
}
#[tokio::test]
async fn save_query_from_note_browser_opens_save_dialog() {
use crate::settings::AppSettings;
use kimun_core::VaultConfig;
use ratatui::crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use std::sync::RwLock;
let dir = tempfile::TempDir::new().unwrap();
let vault = Arc::new(NoteVault::new(VaultConfig::new(dir.path())).await.unwrap());
let settings: SharedSettings = Arc::new(RwLock::new(AppSettings::default()));
let mut screen = EditorScreen::new(vault.clone(), VaultPath::root(), settings.clone());
let (tx, _rx) = tokio::sync::mpsc::unbounded_channel();
// Open a note browser carrying a query, as if the user typed "#todo".
{
let s = settings.read().unwrap();
let provider = SearchNotesProvider::new(vault.clone(), s.current_last_paths(), None);
let modal = NoteBrowserModal::with_initial_query(
"Note Browser",
provider,
vault.clone(),
s.key_bindings.clone(),
s.icons(),
tx.clone(),
"#todo",
);
drop(s);
screen.overlays.open(Box::new(modal), screen.opener_focus());
}
assert_eq!(
screen.overlays.active_kind(),
Some(OverlayKind::NoteBrowser),
"precondition: note browser is active with a query"
);
assert_eq!(screen.overlays.active_query(), Some("#todo"));
// Ctrl+D (SaveCurrentQuery) while the note browser is active should
// replace it with the save-search dialog, sourcing the browser's query.
let save_event = KeyEvent::new(KeyCode::Char('d'), KeyModifiers::CONTROL);
{
let s = settings.read().unwrap();
let combo = key_event_to_combo(&save_event).expect("Ctrl+D maps to a combo");
assert_eq!(
s.key_bindings.get_action(&combo),
Some(ActionShortcuts::SaveCurrentQuery),
"test assumes Ctrl+D is bound to SaveCurrentQuery"
);
}
screen.handle_input(&InputEvent::Key(save_event), &tx);
assert_eq!(
screen.overlays.active_kind(),
Some(OverlayKind::Dialog),
"saving from the note browser opens the save-search dialog"
);
}
}
#[cfg(test)]
mod sort_routing_tests {
use super::*;
use crate::app_screen::AppScreen;
use crate::components::events::SortTarget;
use crate::components::file_list::{SortField, SortOrder};
async fn make_editor() -> (
EditorScreen,
AppTx,
tokio::sync::mpsc::UnboundedReceiver<AppEvent>,
) {
let vault = crate::test_support::temp_vault("editor-sort").await;
vault.validate_and_init().await.unwrap();
let settings = std::sync::Arc::new(std::sync::RwLock::new(
crate::settings::AppSettings::default(),
));
let screen = EditorScreen::new(vault, VaultPath::root(), settings);
let (tx, rx) = tokio::sync::mpsc::unbounded_channel();
(screen, tx, rx)
}
#[tokio::test(flavor = "multi_thread")]
async fn sort_save_default_persists_to_settings() {
let (mut screen, tx, _rx) = make_editor().await;
screen
.handle_app_message(
AppEvent::SortChanged {
target: SortTarget::Sidebar,
field: SortField::Title,
order: SortOrder::Descending,
group_directories: true,
persist: true,
},
&tx,
)
.await;
let s = screen.settings.read().unwrap();
assert_eq!(
s.default_sort_field,
crate::settings::SortFieldSetting::Title
);
assert_eq!(
s.default_sort_order,
crate::settings::SortOrderSetting::Descending
);
assert!(s.group_directories);
}
#[tokio::test(flavor = "multi_thread")]
async fn sort_save_default_journal_dir_writes_journal_settings() {
let (mut screen, tx, _rx) = make_editor().await;
// Point the sidebar at the journal directory (current_dir set synchronously).
let journal = screen.vault.journal_path().clone();
screen.panels.sidebar_mut().navigate(journal, &tx);
screen
.handle_app_message(
AppEvent::SortChanged {
target: SortTarget::Sidebar,
field: SortField::Title,
order: SortOrder::Ascending,
group_directories: false,
persist: true,
},
&tx,
)
.await;
let s = screen.settings.read().unwrap();
assert_eq!(
s.journal_sort_field,
crate::settings::SortFieldSetting::Title
);
assert_eq!(
s.journal_sort_order,
crate::settings::SortOrderSetting::Ascending
);
// Default (non-journal) settings must be untouched from their defaults.
assert_eq!(
s.default_sort_field,
crate::settings::SortFieldSetting::Name
);
}
/// A non-persisting SortChanged (a plain dialog toggle) applies live but
/// must NOT write settings.
#[tokio::test(flavor = "multi_thread")]
async fn sort_changed_without_persist_leaves_settings() {
let (mut screen, tx, _rx) = make_editor().await;
screen
.handle_app_message(
AppEvent::SortChanged {
target: SortTarget::Sidebar,
field: SortField::Title,
order: SortOrder::Descending,
group_directories: true,
persist: false,
},
&tx,
)
.await;
let s = screen.settings.read().unwrap();
assert_eq!(
s.default_sort_field,
crate::settings::SortFieldSetting::Name
);
assert_eq!(
s.default_sort_order,
crate::settings::SortOrderSetting::Ascending
);
assert!(!s.group_directories);
}
}