clin-rs 0.3.6

Encrypted terminal note-taking app
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
use crate::app::ContextMenu;
use crate::app::{App, EditFocus, ListFocus};
use crate::keybinds::*;
use crossterm::event::*;
use ratatui::prelude::*;
use tui_textarea::*;

pub fn handle_list_keys(app: &mut App, key: KeyEvent) -> bool {
    if let Some(mut palette) = app.command_palette.take() {
        if palette.handle_input(key) {
            if key.code == KeyCode::Enter {
                if let Some(selected_idx) = palette.state.selected() {
                    if let Some(item) = palette.items.get(selected_idx) {
                        let action_id = item.id.clone();
                        let note_id = palette.context_note_id.clone();
                        if let Err(e) =
                            crate::actions::execute_action(&action_id, app, note_id.as_deref())
                        {
                            app.set_temporary_status(&format!("Action failed: {}", e));
                        }
                    }
                }
            }
            return false;
        }
        app.command_palette = Some(palette);
        return false;
    }

    // Handle note create popup if open
    if let Some(mut popup) = app.note_create_popup.take() {
        if key.code == KeyCode::Esc {
            app.note_create_popup = None;
        } else if key.code == KeyCode::Enter {
            app.note_create_popup = Some(popup);
            app.confirm_create_note();
        } else {
            popup.input.input(Input::from(key));
            app.note_create_popup = Some(popup);
        }
        return false;
    }

    // Handle folder popup if open
    if let Some(mut popup) = app.folder_popup.take() {
        if key.code == KeyCode::Esc {
            app.folder_popup = None;
        } else if key.code == KeyCode::Enter {
            app.folder_popup = Some(popup);
            app.confirm_folder_popup();
        } else {
            popup.input.input(Input::from(key));
            app.folder_popup = Some(popup);
        }
        return false;
    }

    // Handle tag popup if open
    if let Some(mut popup) = app.tag_popup.take() {
        if key.code == KeyCode::Esc {
            app.tag_popup = None;
        } else if key.code == KeyCode::Enter {
            app.tag_popup = Some(popup);
            app.confirm_manage_tags();
        } else if key.code == KeyCode::Char('D') && key.modifiers.contains(KeyModifiers::SHIFT) {
            app.tag_popup = Some(popup);
            app.begin_delete_tag();
        } else if key.code == KeyCode::Tab {
            app.tag_popup = Some(popup);
            if app
                .tag_popup
                .as_ref()
                .map_or(false, |p| !p.suggestions.is_empty())
            {
                app.accept_tag_suggestion();
            } else {
                app.cycle_tag_suggestion();
            }
        } else {
            popup.input.input(Input::from(key));
            app.tag_popup = Some(popup);
            app.update_tag_suggestions();
        }
        return false;
    }

    // Handle filter popup if open
    if let Some(mut popup) = app.filter_popup.take() {
        if key.code == KeyCode::Esc {
            app.cancel_filter_tags();
        } else if key.code == KeyCode::Enter {
            app.filter_popup = Some(popup);
            app.confirm_filter_tags();
        } else if key.code == KeyCode::Tab {
            app.filter_popup = Some(popup);
            if app
                .filter_popup
                .as_ref()
                .map_or(false, |p| !p.suggestions.is_empty())
            {
                app.accept_filter_suggestion();
            } else {
                app.cycle_filter_suggestion();
            }
        } else {
            popup.input.input(Input::from(key));
            app.filter_popup = Some(popup);
            app.update_filter_suggestions();
        }
        return false;
    }

    // Handle note rename popup if open
    if let Some(mut popup) = app.note_rename_popup.take() {
        if key.code == KeyCode::Esc {
            app.note_rename_popup = None;
        } else if key.code == KeyCode::Enter {
            app.note_rename_popup = Some(popup);
            app.confirm_rename_note();
        } else {
            popup.input.input(Input::from(key));
            app.note_rename_popup = Some(popup);
        }
        return false;
    }

    // Handle search popup if open
    if let Some(mut popup) = app.search_popup.take() {
        if key.code == KeyCode::Esc {
            app.search_popup = Some(popup);
            app.cancel_search();
        } else if key.code == KeyCode::Enter {
            app.search_popup = Some(popup);
            app.confirm_search();
        } else {
            popup.input.input(Input::from(key));
            app.search_popup = Some(popup);
            app.update_search();
        }
        return false;
    }

    // Handle confirm popup (must be before trash view since both can be open)
    if app.confirm_popup.is_some() {
        if key.code == KeyCode::Left || key.code == KeyCode::Char('h') {
            app.confirm_popup_select_confirm();
        } else if key.code == KeyCode::Right || key.code == KeyCode::Char('l') {
            app.confirm_popup_select_cancel();
        } else if key.code == KeyCode::Tab {
            app.confirm_popup_toggle_button();
        } else if key.code == KeyCode::Enter {
            app.confirm_popup_activate();
        } else if key.code == KeyCode::Esc {
            app.cancel_confirm();
        } else if app.keybinds.matches_list(ListAction::Confirm, &key) {
            app.confirm_action();
        } else if app.keybinds.matches_list(ListAction::Cancel, &key) {
            app.cancel_confirm();
        }
        return false;
    }

    // Handle trash view if open
    if let Some(ref mut trash) = app.trash_view {
        match key.code {
            KeyCode::Up | KeyCode::Char('k') => {
                trash.selected = trash.selected.saturating_sub(1);
            }
            KeyCode::Down | KeyCode::Char('j') => {
                if trash.selected + 1 < trash.items.len() {
                    trash.selected += 1;
                }
            }
            KeyCode::Char('r') | KeyCode::Enter => {
                app.restore_from_trash();
            }
            KeyCode::Char('d') | KeyCode::Delete => {
                app.begin_delete_from_trash();
            }
            KeyCode::Char('E') => {
                app.begin_empty_trash();
            }
            KeyCode::Esc | KeyCode::Char('q') => {
                app.close_trash_view();
            }
            _ => {}
        }
        return false;
    }

    // Handle folder picker if open
    if let Some(mut picker) = app.folder_picker.take() {
        match key.code {
            KeyCode::Up | KeyCode::Char('k') => {
                picker.selected = picker.selected.saturating_sub(1);
                app.folder_picker = Some(picker);
            }
            KeyCode::Down | KeyCode::Char('j') => {
                if picker.selected + 1 < picker.folders.len() {
                    picker.selected += 1;
                }
                app.folder_picker = Some(picker);
            }
            KeyCode::Enter | KeyCode::Char('l') => {
                app.folder_picker = Some(picker);
                app.confirm_move();
            }
            KeyCode::Esc | KeyCode::Char('h') => {
                app.folder_picker = None;
            }
            _ => {
                app.folder_picker = Some(picker);
            }
        }
        return false;
    }

    // Handle template popup if open
    if let Some(mut popup) = app.template_popup.take() {
        match key.code {
            KeyCode::Up | KeyCode::Char('k') => {
                popup.selected = popup.selected.saturating_sub(1);
                app.template_popup = Some(popup);
            }
            KeyCode::Down | KeyCode::Char('j') => {
                if popup.selected + 1 < popup.templates.len() {
                    popup.selected += 1;
                }
                app.template_popup = Some(popup);
            }
            KeyCode::Enter | KeyCode::Char('l') => {
                app.template_popup = Some(popup);
                app.select_template();
            }
            KeyCode::Esc | KeyCode::Char('h') => {
                app.close_template_popup();
            }
            _ => {
                app.template_popup = Some(popup);
            }
        }
        return false;
    }

    // Cycle focus with Tab
    if app.keybinds.matches_list(ListAction::CycleFocus, &key) {
        app.list_focus = match app.list_focus {
            ListFocus::Notes => ListFocus::EncryptionToggle,
            ListFocus::EncryptionToggle => ListFocus::ExternalEditorToggle,
            ListFocus::ExternalEditorToggle => ListFocus::Notes,
        };
        return false;
    }

    // Handle toggle buttons when focused
    if app.list_focus == ListFocus::EncryptionToggle {
        if app.keybinds.matches_list(ListAction::ToggleButton, &key) {
            app.toggle_encryption_mode();
        } else if app.keybinds.matches_list(ListAction::Quit, &key) {
            return true;
        }
        return false;
    }
    if app.list_focus == ListFocus::ExternalEditorToggle {
        if app.keybinds.matches_list(ListAction::ToggleButton, &key) {
            app.toggle_external_editor_mode();
        } else if app.keybinds.matches_list(ListAction::Quit, &key) {
            return true;
        }
        return false;
    }

    // Standard keybind handling
    if app.keybinds.matches_list(ListAction::Quit, &key) {
        return true;
    }
    if app.keybinds.matches_list(ListAction::Help, &key) {
        app.open_help_page();
        return false;
    }
    if app.keybinds.matches_list(ListAction::OpenLocation, &key) {
        app.open_selected_note_location();
        return false;
    }
    if app.keybinds.matches_list(ListAction::Delete, &key) {
        app.begin_delete_selected();
        return false;
    }
    if app.keybinds.matches_list(ListAction::MoveDown, &key) {
        if app.visual_index < app.visual_list.len().saturating_sub(1) {
            app.visual_index += 1;
            app.update_preview();
        }
        return false;
    }
    if app.keybinds.matches_list(ListAction::MoveUp, &key) {
        if app.visual_index > 0 {
            app.visual_index -= 1;
            app.update_preview();
        }
        return false;
    }
    if app.keybinds.matches_list(ListAction::CollapseFolder, &key) {
        app.collapse_selected_folder();
        return false;
    }
    if app.keybinds.matches_list(ListAction::ExpandFolder, &key) {
        app.expand_selected_folder();
        return false;
    }
    if app.keybinds.matches_list(ListAction::Open, &key) {
        app.open_selected();
        return false;
    }
    if app.keybinds.matches_list(ListAction::NewFromTemplate, &key) {
        app.open_template_popup();
        return false;
    }
    if app.keybinds.matches_list(ListAction::CreateFolder, &key) {
        app.begin_create_folder();
        return false;
    }
    if app.keybinds.matches_list(ListAction::CreateNote, &key) {
        app.begin_create_note();
        return false;
    }
    if app.keybinds.matches_list(ListAction::RenameFolder, &key)
        || app.keybinds.matches_list(ListAction::Rename, &key)
    {
        // Context-sensitive rename: folder or note based on selection
        if let Some(item) = app.visual_list.get(app.visual_index) {
            match item {
                crate::app::VisualItem::Folder { .. } => app.begin_rename_folder(),
                crate::app::VisualItem::Note { .. } => app.begin_rename_note(),
                _ => app.set_temporary_status_static("Select a note or folder to rename"),
            }
        }
        return false;
    }
    if app.keybinds.matches_list(ListAction::MoveNote, &key) {
        app.begin_move();
        return false;
    }
    if app.keybinds.matches_list(ListAction::ManageTags, &key) {
        app.begin_manage_tags();
        return false;
    }
    if app.keybinds.matches_list(ListAction::FilterTags, &key) {
        app.begin_filter_tags();
        return false;
    }
    if app
        .keybinds
        .matches_list(ListAction::OpenCommandPalette, &key)
    {
        if let Some(item) = app.visual_list.get(app.visual_index) {
            match item {
                crate::app::VisualItem::Note { id, .. } => {
                    app.command_palette =
                        Some(crate::palette::CommandPalette::new(Some(id.clone())));
                }
                _ => {
                    app.command_palette = Some(crate::palette::CommandPalette::new(None));
                }
            }
        } else {
            app.command_palette = Some(crate::palette::CommandPalette::new(None));
        }
        return false;
    }

    // QoL feature handlers
    if app.keybinds.matches_list(ListAction::Duplicate, &key) {
        app.duplicate_note();
        return false;
    }
    if app.keybinds.matches_list(ListAction::TogglePin, &key) {
        app.toggle_pin();
        return false;
    }
    if app.keybinds.matches_list(ListAction::CycleSort, &key) {
        app.cycle_sort();
        return false;
    }
    if app.keybinds.matches_list(ListAction::Search, &key) {
        app.begin_search();
        return false;
    }
    if app.keybinds.matches_list(ListAction::JumpToTop, &key) {
        app.jump_to_bottom(); // G jumps to bottom (vim convention)
        return false;
    }
    if app.keybinds.matches_list(ListAction::PageUp, &key) {
        app.page_up();
        return false;
    }
    if app.keybinds.matches_list(ListAction::PageDown, &key) {
        app.page_down();
        return false;
    }
    if app.keybinds.matches_list(ListAction::OpenTrash, &key) {
        app.open_trash_view();
        return false;
    }
    if app.keybinds.matches_list(ListAction::TogglePreview, &key) {
        app.toggle_preview();
        return false;
    }

    // Handle vim-style 'g' for gg (jump to top)
    if key.code == KeyCode::Char('g') {
        if app.handle_g_press() {
            return false;
        }
    }

    false
}

pub fn handle_help_keys(app: &mut App, key: KeyEvent) {
    if app.keybinds.matches_help(HelpAction::Close, &key) {
        app.close_help_page();
    } else if app.keybinds.matches_help(HelpAction::ScrollDown, &key) {
        app.help_scroll = app.help_scroll.saturating_add(1);
    } else if app.keybinds.matches_help(HelpAction::ScrollUp, &key) {
        app.help_scroll = app.help_scroll.saturating_sub(1);
    }
}

pub fn handle_edit_keys(app: &mut App, key: KeyEvent, focus: &mut EditFocus) -> bool {
    // Handle context menu if open
    if let Some(mut menu) = app.context_menu.take() {
        match key.code {
            KeyCode::Up => {
                menu.selected = menu.selected.saturating_sub(1);
                app.context_menu = Some(menu);
            }
            KeyCode::Down => {
                if menu.selected < 3 {
                    menu.selected += 1;
                }
                app.context_menu = Some(menu);
            }
            KeyCode::Enter => {
                app.handle_menu_action(menu.selected, focus);
            }
            KeyCode::Esc => {
                app.context_menu = None;
            }
            _ => {
                app.context_menu = Some(menu);
            }
        }
        return false;
    }

    // Quit with save
    if app.keybinds.matches_edit(EditAction::Quit, &key) {
        app.autosave();
        return true;
    }

    // Cycle focus
    if app.keybinds.matches_edit(EditAction::CycleFocus, &key) {
        *focus = match *focus {
            EditFocus::Title => EditFocus::Body,
            EditFocus::Body => EditFocus::EncryptionToggle,
            EditFocus::EncryptionToggle => EditFocus::ExternalEditorToggle,
            EditFocus::ExternalEditorToggle => EditFocus::Title,
        };
        return false;
    }

    // Back to list
    if app.keybinds.matches_edit(EditAction::Back, &key) {
        app.autosave();
        app.back_to_list();
        *focus = EditFocus::Body;
        return false;
    }

    match *focus {
        EditFocus::Title => {
            if key.code == KeyCode::Enter {
                *focus = EditFocus::Body;
                return false;
            }

            if handle_os_shortcuts(&app.keybinds, &mut app.title_editor, key) {
                return false;
            }

            if app.title_editor.input(Input::from(key)) && app.title_editor.lines().len() > 1 {
                let normalized = get_title_text(&app.title_editor).replace(['\r', '\n'], " ");
                app.title_editor = make_title_editor(&normalized);
            }
        }
        EditFocus::Body => {
            if handle_os_shortcuts(&app.keybinds, &mut app.editor, key) {
                return false;
            }
            app.editor.input(Input::from(key));
        }
        EditFocus::EncryptionToggle => {
            if app.keybinds.matches_edit(EditAction::ToggleButton, &key) {
                app.toggle_encryption_mode();
            }
        }
        EditFocus::ExternalEditorToggle => {
            if app.keybinds.matches_edit(EditAction::ToggleButton, &key) {
                app.toggle_external_editor_mode();
            }
        }
    }

    false
}

pub fn handle_list_mouse(app: &mut App, mouse_event: MouseEvent, terminal_area: Rect) {
    if app.confirm_popup.is_some() {
        if mouse_event.kind == MouseEventKind::Down(MouseButton::Left) {
            let popup_area = crate::ui::centered_rect(50, 30, terminal_area);
            let click_x = mouse_event.column;
            let click_y = mouse_event.row;

            if click_x >= popup_area.x
                && click_x < popup_area.x + popup_area.width
                && click_y >= popup_area.y
                && click_y < popup_area.y + popup_area.height
            {
                let mid_x = popup_area.x + popup_area.width / 2;
                if click_x < mid_x {
                    app.confirm_action();
                } else {
                    app.cancel_confirm();
                }
            }
        }
        return;
    }

    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Length(3),
            Constraint::Min(5),
            Constraint::Length(3),
        ])
        .split(terminal_area);

    let list_area = chunks[1];
    let inner_list_area = Rect::new(
        list_area.x.saturating_add(1),
        list_area.y.saturating_add(1),
        list_area.width.saturating_sub(2),
        list_area.height.saturating_sub(2),
    );

    if mouse_event.kind == MouseEventKind::ScrollUp {
        let current = app.list_state.selected().unwrap_or(0);
        app.list_state.select(Some(current.saturating_sub(1)));
        // Wait, list_state.select automatically scrolls if needed during render.
        // We also want to change app.selected if it corresponds to a note, or we can just scroll the list_state?
        // Actually, in the original TUI, scrolling the list changes the selected NOTE.
        // Let's implement it by simulating UP/DOWN keys!
        handle_list_keys(app, KeyEvent::new(KeyCode::Up, KeyModifiers::NONE));
        return;
    }

    if mouse_event.kind == MouseEventKind::ScrollDown {
        handle_list_keys(app, KeyEvent::new(KeyCode::Down, KeyModifiers::NONE));
        return;
    }

    if !contains_cell(inner_list_area, mouse_event.column, mouse_event.row) {
        return;
    }

    if mouse_event.kind == MouseEventKind::Down(MouseButton::Left) {
        let visual_row = mouse_event.row.saturating_sub(inner_list_area.y) as usize;
        let clicked_visual_index = app.list_state.offset().saturating_add(visual_row);

        if clicked_visual_index < app.visual_list.len() {
            if app.visual_index == clicked_visual_index {
                // Click on already-selected item to open it
                app.open_selected();
            } else {
                app.visual_index = clicked_visual_index;
            }
        }
    }
}

pub fn handle_edit_mouse(
    app: &mut App,
    mouse_event: MouseEvent,
    terminal_area: Rect,
    focus: &mut EditFocus,
    mouse_selecting: &mut bool,
    mouse_dragged: &mut bool,
) {
    if let Some(menu) = &app.context_menu {
        let menu_rect = Rect::new(menu.x, menu.y, 14, 6);
        if contains_cell(menu_rect, mouse_event.column, mouse_event.row) {
            if mouse_event.kind == MouseEventKind::Down(MouseButton::Left) {
                let clicked_idx = mouse_event.row.saturating_sub(menu.y).saturating_sub(1) as usize;
                if clicked_idx < 4 {
                    app.handle_menu_action(clicked_idx, focus);
                }
                app.context_menu = None;
            } else if mouse_event.kind == MouseEventKind::ScrollUp {
                let mut menu_copy = app.context_menu.take().unwrap();
                menu_copy.selected = menu_copy.selected.saturating_sub(1);
                app.context_menu = Some(menu_copy);
            } else if mouse_event.kind == MouseEventKind::ScrollDown {
                let mut menu_copy = app.context_menu.take().unwrap();
                if menu_copy.selected < 3 {
                    menu_copy.selected += 1;
                }
                app.context_menu = Some(menu_copy);
            }
            return;
        } else if matches!(mouse_event.kind, MouseEventKind::Down(_)) {
            app.context_menu = None;
            if mouse_event.kind != MouseEventKind::Down(MouseButton::Right) {
                return;
            }
        } else {
            return;
        }
    }

    if mouse_event.kind == MouseEventKind::Down(MouseButton::Right) {
        let (title_inner, body_inner) = edit_view_input_areas(terminal_area);

        if contains_cell(title_inner, mouse_event.column, mouse_event.row) {
            *focus = EditFocus::Title;
            move_textarea_cursor_to_mouse(
                &mut app.title_editor,
                title_inner,
                mouse_event.column,
                mouse_event.row,
            );
        } else if contains_cell(body_inner, mouse_event.column, mouse_event.row) {
            *focus = EditFocus::Body;
            move_textarea_cursor_to_mouse(
                &mut app.editor,
                body_inner,
                mouse_event.column,
                mouse_event.row,
            );
        }

        let max_x = terminal_area.width.saturating_sub(14);
        let max_y = terminal_area.height.saturating_sub(6);
        app.context_menu = Some(ContextMenu {
            x: mouse_event.column.min(max_x),
            y: mouse_event.row.min(max_y),
            selected: 0,
        });
        return;
    }

    let (title_inner, body_inner) = edit_view_input_areas(terminal_area);

    match mouse_event.kind {
        MouseEventKind::Down(MouseButton::Left) => {
            *mouse_selecting = false;
            *mouse_dragged = false;
            if contains_cell(body_inner, mouse_event.column, mouse_event.row) {
                *focus = EditFocus::Body;
                move_textarea_cursor_to_mouse(
                    &mut app.editor,
                    body_inner,
                    mouse_event.column,
                    mouse_event.row,
                );
                app.editor.start_selection();
                *mouse_selecting = true;
            } else if contains_cell(title_inner, mouse_event.column, mouse_event.row) {
                *focus = EditFocus::Title;
                move_textarea_cursor_to_mouse(
                    &mut app.title_editor,
                    title_inner,
                    mouse_event.column,
                    mouse_event.row,
                );
                app.title_editor.start_selection();
                *mouse_selecting = true;
            }
        }
        MouseEventKind::Drag(MouseButton::Left) => {
            if *mouse_selecting {
                *mouse_dragged = true;
                if *focus == EditFocus::Body {
                    move_textarea_cursor_to_mouse(
                        &mut app.editor,
                        body_inner,
                        mouse_event.column,
                        mouse_event.row,
                    );
                } else {
                    move_textarea_cursor_to_mouse(
                        &mut app.title_editor,
                        title_inner,
                        mouse_event.column,
                        mouse_event.row,
                    );
                }
            }
        }
        MouseEventKind::Up(MouseButton::Left) => {
            if *mouse_selecting && !*mouse_dragged {
                if *focus == EditFocus::Body {
                    app.editor.cancel_selection();
                } else {
                    app.title_editor.cancel_selection();
                }
            }
            *mouse_selecting = false;
            *mouse_dragged = false;
        }
        MouseEventKind::ScrollDown => {
            if *focus == EditFocus::Body {
                app.editor.scroll((3, 0));
            }
        }
        MouseEventKind::ScrollUp => {
            if *focus == EditFocus::Body {
                app.editor.scroll((-3, 0));
            }
        }
        _ => {}
    }
}

pub fn move_textarea_cursor_to_mouse(
    textarea: &mut TextArea,
    body_inner: Rect,
    mouse_col: u16,
    mouse_row: u16,
) {
    if textarea.lines().is_empty() || body_inner.width == 0 || body_inner.height == 0 {
        return;
    }

    let mut scroll_row = 0;
    let mut scroll_col = 0;

    // Unfortunately, tui-textarea doesn't currently expose public viewport accessors.
    // We must rely on debug string formatting to extract the scroll offset.
    // This allocation happens on mouse drag, but it's the only way without upstream changes.
    let debug_str = format!("{textarea:?}");
    if let Some(start) = debug_str.find("viewport: Viewport(") {
        let after_start = &debug_str[start + "viewport: Viewport(".len()..];
        if let Some(end) = after_start.find(')') {
            let number_str = &after_start[..end];
            if let Ok(number) = number_str.parse::<u64>() {
                scroll_row = ((number >> 16) & 0xFFFF) as usize;
                scroll_col = (number & 0xFFFF) as usize;
            }
        }
    }

    let row = mouse_row.saturating_sub(body_inner.y) as usize + scroll_row;
    let col = mouse_col.saturating_sub(body_inner.x) as usize + scroll_col;

    let max_row = textarea.lines().len().saturating_sub(1);
    let target_row = row.min(max_row);
    let max_col = textarea.lines()[target_row].chars().count();
    let target_col = col.min(max_col);

    textarea.move_cursor(CursorMove::Jump(target_row as u16, target_col as u16));
}

pub fn edit_view_input_areas(area: Rect) -> (Rect, Rect) {
    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Length(3),
            Constraint::Min(8),
            Constraint::Length(3),
        ])
        .split(area);

    let title_inner = chunks[0].inner(Margin {
        vertical: 1,
        horizontal: 1,
    });
    let body_inner = chunks[1].inner(Margin {
        vertical: 1,
        horizontal: 1,
    });

    (title_inner, body_inner)
}

pub fn contains_cell(rect: Rect, col: u16, row: u16) -> bool {
    rect.width > 0
        && rect.height > 0
        && col >= rect.x
        && col < rect.x + rect.width
        && row >= rect.y
        && row < rect.y + rect.height
}

pub fn handle_os_shortcuts(
    keybinds: &Keybinds,
    textarea: &mut TextArea<'static>,
    key: KeyEvent,
) -> bool {
    // Use keybinds for customizable shortcuts
    if keybinds.matches_edit(EditAction::SelectAll, &key) {
        textarea.select_all();
        return true;
    }
    if keybinds.matches_edit(EditAction::Copy, &key) {
        textarea.copy();
        return true;
    }
    if keybinds.matches_edit(EditAction::Cut, &key) {
        let _ = textarea.cut();
        return true;
    }
    if keybinds.matches_edit(EditAction::Paste, &key) {
        let _ = textarea.paste();
        return true;
    }
    if keybinds.matches_edit(EditAction::Undo, &key) {
        let _ = textarea.undo();
        return true;
    }
    if keybinds.matches_edit(EditAction::Redo, &key) {
        let _ = textarea.redo();
        return true;
    }
    if keybinds.matches_edit(EditAction::DeleteWord, &key) {
        let _ = textarea.delete_word();
        return true;
    }
    if keybinds.matches_edit(EditAction::DeleteNextWord, &key) {
        let _ = textarea.delete_next_word();
        return true;
    }
    if keybinds.matches_edit(EditAction::MoveToTop, &key) {
        textarea.move_cursor(CursorMove::Top);
        return true;
    }
    if keybinds.matches_edit(EditAction::MoveToBottom, &key) {
        textarea.move_cursor(CursorMove::Bottom);
        return true;
    }

    false
}

pub fn make_title_editor(initial: &str) -> TextArea<'static> {
    let mut title = if initial.is_empty() {
        TextArea::default()
    } else {
        TextArea::from([initial.to_string()])
    };
    title.set_cursor_style(Style::default().fg(Color::Black).bg(Color::Cyan));
    title
}

use std::borrow::Cow;

pub fn get_title_text<'a>(title_editor: &'a TextArea<'static>) -> Cow<'a, str> {
    let lines = title_editor.lines();

    if lines.len() == 1 {
        let line = lines[0].trim();
        if !line.contains(['\r', '\n']) {
            return Cow::Borrowed(line);
        }
    }

    Cow::Owned(
        lines
            .join(" ")
            .replace(['\r', '\n'], " ")
            .trim()
            .to_string(),
    )
}