piki-gui 0.4.0

FLTK-based GUI for Piki personal wiki application
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
mod app_icon;
mod autosave;
pub mod fltk_draw_context;
mod history;
mod link_handler;
mod menu;
mod note_picker;
mod recency;
pub mod responsive_scrollbar;
mod scroll_memory;
mod search_bar;
mod statusbar;
mod window_state;

use autosave::AutoSaveState;
use clap::Parser;
use fltk::{prelude::*, *};
use history::History;
use piki_core::{DocumentStore, IndexPlugin, PluginRegistry, TodoPlugin};
use piki_gui::note_ui::NoteUI;
use piki_gui::ui_adapters::StructuredRichUI;
use recency::RecentNotes;
use scroll_memory::ScrollMemory;
use search_bar::SearchBar;
use statusbar::StatusBar;
use std::cell::RefCell;
use std::path::PathBuf;
use std::rc::Rc;
use std::time::Instant;
use window_state::WindowGeometry;

// Timeout to save window state after resize/move
const WINDOW_STATE_SAVE_TIMEOUT_SECS: f64 = 3.0;
// Interval to autosave changes
const AUTOSAVE_INTERVAL_SECS: f64 = 10.0;
// Interval to update "X ago" display in save status
const SAVE_STATUS_UPDATE_INTERVAL_SECS: f64 = 30.0;

#[derive(Parser, Debug)]
#[command(name = "piki-gui")]
#[command(about = "Piki - a simple personal wiki", long_about = None)]
struct Args {
    /// Directory containing markdown files (default: ~/.piki)
    #[arg(short = 'd', long = "directory", value_name = "DIRECTORY")]
    directory: Option<PathBuf>,

    /// Initial note to load (default: frontpage)
    #[arg(short, long, default_value = "frontpage")]
    note: String,
}

struct AppState {
    store: DocumentStore,
    plugin_registry: PluginRegistry,
    current_note: String,
    history: History,
    /// When each note was last opened, used by the note picker to order notes
    /// and to resolve the "previous note" for a double Cmd-O/Ctrl-O.
    recent_notes: RecentNotes,
    /// Where `recent_notes` is persisted (None if no data dir is available).
    recent_notes_path: Option<PathBuf>,
    /// In-memory scroll positions for recently visited notes, so returning to a
    /// note resumes where the user left off.
    scroll_positions: ScrollMemory,
}

impl AppState {
    fn new(
        store: DocumentStore,
        plugin_registry: PluginRegistry,
        initial_note: String,
        recent_notes_path: Option<PathBuf>,
    ) -> Self {
        let recent_notes = recent_notes_path
            .as_deref()
            .map(RecentNotes::load)
            .unwrap_or_default();
        AppState {
            store,
            plugin_registry,
            current_note: initial_note,
            history: History::new(),
            recent_notes,
            recent_notes_path,
            scroll_positions: ScrollMemory::new(),
        }
    }

    /// Record that `note` was just opened and persist the updated recency store.
    fn mark_note_opened(&mut self, note: &str) {
        self.recent_notes.mark_opened(note);
        if let Some(path) = &self.recent_notes_path
            && let Err(e) = self.recent_notes.save(path)
        {
            eprintln!("Failed to save recent notes: {e}");
        }
    }

    /// Update all in-session state that refers to `old` to point at `new` after
    /// a note has been renamed: the current-note pointer, back/forward history,
    /// the picker's recency ordering, and remembered scroll positions. The
    /// on-disk file move is handled by `rename_current_note`.
    fn rename_note(&mut self, old: &str, new: &str) {
        if self.current_note == old {
            self.current_note = new.to_string();
        }
        self.history.rename_note(old, new);
        self.recent_notes.rename(old, new);
        self.scroll_positions.rename(old, new);
        if let Some(path) = &self.recent_notes_path
            && let Err(e) = self.recent_notes.save(path)
        {
            eprintln!("Failed to save recent notes: {e}");
        }
    }

    fn load_note(&mut self, note_name: &str) -> Result<String, String> {
        // Check if this is a plugin note (starts with !)
        if let Some(plugin_name) = note_name.strip_prefix('!') {
            // Generate content using the plugin
            self.current_note = note_name.to_string();
            return self.plugin_registry.generate(plugin_name, &self.store);
        }

        // Normal file loading
        match self.store.load(note_name) {
            Ok(doc) => {
                self.current_note = note_name.to_string();
                Ok(doc.content)
            }
            Err(e) => Err(e),
        }
    }
}
/// Flush any pending changes of the currently open note to disk immediately.
///
/// This is the "save when walking away" safeguard: it runs before navigating to
/// another note (links, history, note picker, new note) and when the window is
/// closing, so edits are never lost to the debounced autosave timer. Saving is a
/// no-op when the content is unchanged or the note is a read-only plugin note
/// (handled inside `AutoSaveState::trigger_save`).
fn save_current_note(
    app_state: &Rc<RefCell<AppState>>,
    autosave_state: &Rc<RefCell<AutoSaveState>>,
    active_editor: &Rc<RefCell<Rc<RefCell<dyn NoteUI>>>>,
    statusbar: &Rc<RefCell<StatusBar>>,
) {
    if let (Ok(ed_ptr), Ok(mut as_state), Ok(app_st)) = (
        active_editor.try_borrow(),
        autosave_state.try_borrow_mut(),
        app_state.try_borrow(),
    ) {
        let ed_ref = (*ed_ptr).borrow();
        match as_state.trigger_save(&*ed_ref, &app_st.store) {
            Ok(()) => {
                if let Ok(mut sb) = statusbar.try_borrow_mut() {
                    sb.set_status(&as_state.get_status_text());
                }
            }
            Err(e) => {
                if let Ok(mut sb) = statusbar.try_borrow_mut() {
                    sb.set_status(&format!("Error: {}", e));
                }
            }
        }
    }
}

/// Rename the currently open note: move its file on disk and update every piece
/// of in-session state to follow it. Backs the "Rename Note …" menu item, which
/// is how a quick, auto-named `untitled_…` note gets a real name.
///
/// The current content is flushed to the old file first, so an edit that has not
/// yet hit the debounced autosave is not lost and there is a file to move. A
/// brand-new untitled note the user has not typed into has no file yet, so the
/// move is skipped and the next autosave simply writes to the new name. Returns
/// an error (surfaced by the dialog) when the target name is already taken or
/// the move fails; read-only plugin notes ("!…") cannot be renamed.
fn rename_current_note(
    new_name: &str,
    app_state: &Rc<RefCell<AppState>>,
    autosave_state: &Rc<RefCell<AutoSaveState>>,
    active_editor: &Rc<RefCell<Rc<RefCell<dyn NoteUI>>>>,
    statusbar: &Rc<RefCell<StatusBar>>,
) -> Result<(), String> {
    let new_name = new_name.trim();
    if new_name.is_empty() {
        return Err("Please enter a name.".to_string());
    }

    let old_name = app_state.borrow().current_note.clone();
    if new_name == old_name {
        return Ok(());
    }
    if old_name.starts_with('!') {
        return Err("This note cannot be renamed.".to_string());
    }

    // Flush current content to the old file first, so a not-yet-autosaved edit
    // is not lost and there is a file to move.
    save_current_note(app_state, autosave_state, active_editor, statusbar);

    let (old_path, new_path) = {
        let st = app_state.borrow();
        (st.store.path_for(&old_name), st.store.path_for(new_name))
    };
    if new_path.exists() {
        return Err(format!("A note named '{new_name}' already exists."));
    }
    // A never-typed-into untitled note has no file yet; nothing to move, the new
    // name is picked up by the next autosave.
    if old_path.exists() {
        if let Some(parent) = new_path.parent() {
            std::fs::create_dir_all(parent)
                .map_err(|e| format!("Failed to create folder for '{new_name}': {e}"))?;
        }
        std::fs::rename(&old_path, &new_path).map_err(|e| format!("Failed to rename note: {e}"))?;
    }

    // Point all in-session state at the new name. The editor already holds the
    // content, so we deliberately do not reload it.
    app_state.borrow_mut().rename_note(&old_name, new_name);
    if let Ok(mut as_state) = autosave_state.try_borrow_mut() {
        as_state.current_note = new_name.to_string();
    }
    statusbar
        .borrow_mut()
        .set_note(&format!("Note: {new_name}"));

    Ok(())
}

fn load_note_helper(
    note_name: &str,
    app_state: &Rc<RefCell<AppState>>,
    autosave_state: &Rc<RefCell<AutoSaveState>>,
    active_editor: &Rc<RefCell<Rc<RefCell<dyn NoteUI>>>>,
    statusbar: &Rc<RefCell<StatusBar>>,
    restore_scroll: Option<i32>,
) {
    // Save the note we're leaving before its content is replaced below, so
    // switching notes (or creating a new one) never drops unsaved edits.
    save_current_note(app_state, autosave_state, active_editor, statusbar);

    // Record the scroll position of the note we're leaving: into the current
    // back/forward history entry (only for non-history navigation), and always
    // into the recent-notes scroll memory so returning to it later — via a link
    // or the picker — resumes where we were.
    {
        let leaving_scroll = active_editor.borrow().borrow().scroll_pos();
        let mut state = app_state.borrow_mut();
        if restore_scroll.is_none() {
            state.history.update_scroll_position(leaving_scroll);
        }
        let leaving_note = state.current_note.clone();
        state
            .scroll_positions
            .remember(&leaving_note, leaving_scroll);
    }

    // Check if this is a plugin note
    let is_plugin = note_name.starts_with('!');

    // Load content through AppState::load_note (handles plugins)
    let content_result = app_state.borrow_mut().load_note(note_name);

    match content_result {
        Ok(content) => {
            // For non-plugin notes, get the modification time
            let modified_time = if !is_plugin {
                app_state
                    .borrow()
                    .store
                    .load(note_name)
                    .ok()
                    .and_then(|doc| doc.modified_time)
            } else {
                None
            };

            {
                let active = active_editor.borrow();
                let mut editor_mut = active.borrow_mut();
                editor_mut.set_content_from_markdown(&content);

                // Set read-only mode for plugin notes, editable for regular notes
                editor_mut.set_readonly(is_plugin);
            }

            // Decide where to scroll: an explicit position from back/forward
            // history wins; otherwise resume the remembered position for this
            // note (if it is still one of the recent ones), falling back to top.
            let target_scroll = restore_scroll
                .or_else(|| app_state.borrow().scroll_positions.get(note_name))
                .unwrap_or(0);
            {
                let active = active_editor.borrow();
                let mut ed = (*active).borrow_mut();
                ed.set_scroll_pos(target_scroll);
            }
            let final_scroll_pos = target_scroll;

            // Drop the editor borrow before manipulating history

            // If normal navigation (not history), add new note to history
            if restore_scroll.is_none() {
                app_state
                    .borrow_mut()
                    .history
                    .push(note_name.to_string(), final_scroll_pos);
            }

            // Record the open so the note picker can order notes by recency and
            // resolve the "previous note" for a double Cmd-O. Plugin notes (e.g.
            // !index) are generated views that never appear in the picker list.
            if !is_plugin {
                app_state.borrow_mut().mark_note_opened(note_name);
            }

            // Reset autosave state for the new note
            if let Ok(mut as_state) = autosave_state.try_borrow_mut() {
                as_state.reset_for_note(note_name, &content);

                // Set last_save_time to file's modification time if it exists
                if let Some(mtime) = modified_time {
                    as_state.last_save_time = Some(mtime);
                }
            }

            // Determine note status text based on note type
            let note_text = if let Some(plugin_name) = note_name.strip_prefix('!') {
                format!("Plugin: {}", plugin_name)
            } else if content.is_empty() {
                format!("Note: {} (new)", note_name)
            } else {
                format!("Note: {}", note_name)
            };

            statusbar.borrow_mut().set_note(&note_text);

            // Set initial save status based on modification time
            if let Ok(as_state) = autosave_state.try_borrow() {
                statusbar
                    .borrow_mut()
                    .set_status(&as_state.get_status_text());
            } else {
                statusbar.borrow_mut().set_status("");
            }

            app::redraw();
        }
        Err(e) => {
            statusbar.borrow_mut().set_note(&format!("Error: {}", e));
            statusbar.borrow_mut().set_status("");
            app::redraw();
        }
    }
}

fn navigate_back(
    app_state: &Rc<RefCell<AppState>>,
    autosave_state: &Rc<RefCell<AutoSaveState>>,
    active_editor: &Rc<RefCell<Rc<RefCell<dyn NoteUI>>>>,
    statusbar: &Rc<RefCell<StatusBar>>,
) {
    // Update current entry's scroll position before navigating
    let scroll_pos = active_editor.borrow().borrow().scroll_pos();
    app_state
        .borrow_mut()
        .history
        .update_scroll_position(scroll_pos);

    // Try to navigate back and extract values before calling load_note_helper
    let target = {
        let mut state = app_state.borrow_mut();
        state
            .history
            .go_back()
            .map(|entry| (entry.note_name.clone(), entry.scroll_position))
    }; // Borrow is dropped here

    if let Some((note_name, scroll_position)) = target {
        load_note_helper(
            &note_name,
            app_state,
            autosave_state,
            active_editor,
            statusbar,
            Some(scroll_position),
        );
    }
}

fn navigate_forward(
    app_state: &Rc<RefCell<AppState>>,
    autosave_state: &Rc<RefCell<AutoSaveState>>,
    active_editor: &Rc<RefCell<Rc<RefCell<dyn NoteUI>>>>,
    statusbar: &Rc<RefCell<StatusBar>>,
) {
    // Update current entry's scroll position before navigating
    let scroll_pos = active_editor.borrow().borrow().scroll_pos();
    app_state
        .borrow_mut()
        .history
        .update_scroll_position(scroll_pos);

    // Try to navigate forward and extract values before calling load_note_helper
    let target = {
        let mut state = app_state.borrow_mut();
        state
            .history
            .go_forward()
            .map(|entry| (entry.note_name.clone(), entry.scroll_position))
    }; // Borrow is dropped here

    if let Some((note_name, scroll_position)) = target {
        load_note_helper(
            &note_name,
            app_state,
            autosave_state,
            active_editor,
            statusbar,
            Some(scroll_position),
        );
    }
}

fn get_directory(dir_opt: Option<PathBuf>) -> PathBuf {
    dir_opt.unwrap_or_else(|| {
        std::env::var("HOME")
            .ok()
            .map(|home| PathBuf::from(home).join(".piki"))
            .unwrap_or_else(|| PathBuf::from(".piki"))
    })
}

fn main() {
    let args = Args::parse();
    let directory = get_directory(args.directory);

    // Ensure directory exists
    if !directory.exists()
        && let Err(e) = std::fs::create_dir_all(&directory)
    {
        eprintln!(
            "Error: Failed to create directory '{}': {}",
            directory.display(),
            e
        );
        std::process::exit(1);
    }

    // Validate directory
    if !directory.is_dir() {
        eprintln!("Error: '{}' is not a directory", directory.display());
        std::process::exit(1);
    }

    // Initialize FLTK
    let app = app::App::default();
    // Set the Dock icon on macOS (works even for the unbundled binary).
    app_icon::set_macos_dock_icon();
    let window_state_path = window_state::state_file_path().map(Rc::new);
    let mut wind = window::Window::default()
        .with_size(400, 650) // Golden ratio 1:1.618 approx
        .with_label("Piki");

    if let Some(path) = window_state_path.as_ref()
        && let Some(saved_state) = window_state::load_state(path.as_path())
        && saved_state.width > 0
        && saved_state.height > 0
    {
        wind.resize(
            saved_state.x,
            saved_state.y,
            saved_state.width,
            saved_state.height,
        );
    }

    app_icon::set_window_icon(&mut wind);

    // #[cfg(target_os = "macos")]
    // wind.set_color(Color::White);

    wind.begin();

    // Create state and register plugins
    let store = DocumentStore::new(directory.clone());
    let mut plugin_registry = PluginRegistry::new();
    plugin_registry.register("index", Box::new(IndexPlugin));
    plugin_registry.register("todo", Box::new(TodoPlugin));

    let recent_notes_path = window_state::recent_notes_file(&directory);

    let app_state = Rc::new(RefCell::new(AppState::new(
        store,
        plugin_registry,
        args.note.clone(),
        recent_notes_path,
    )));
    let autosave_state = Rc::new(RefCell::new(AutoSaveState::new()));

    #[cfg(target_os = "macos")]
    let editor_padding = 0;
    #[cfg(not(target_os = "macos"))]
    let editor_padding = 0;

    let statusbar_size = 25;

    // macOS uses system menu bar (no space needed), other platforms use window menu bar (25px)
    #[cfg(target_os = "macos")]
    let (editor_y, editor_height) = (editor_padding, wind.h() - statusbar_size - editor_padding);
    #[cfg(not(target_os = "macos"))]
    let (editor_y, editor_height) = (
        25 + editor_padding,
        wind.h() - statusbar_size - editor_padding - 25,
    );

    // Create only the initially active editor (structured rich editor)
    let editor_x = editor_padding;
    let editor_w = wind.w() - 2 * editor_padding;
    let editor_h = editor_height;
    let rich_editor: Rc<RefCell<dyn NoteUI>> = Rc::new(RefCell::new(StructuredRichUI::new(
        editor_x, editor_y, editor_w, editor_h, true,
    )));
    let active_editor: Rc<RefCell<Rc<RefCell<dyn NoteUI>>>> = Rc::new(RefCell::new(rich_editor));

    // Create status bar at the bottom using the custom StatusBar widget
    let statusbar = Rc::new(RefCell::new(StatusBar::new(
        0,
        wind.h() - statusbar_size,
        wind.w(),
        statusbar_size,
    )));

    // Create a clone handle to the window for callbacks
    let wind_ref = Rc::new(RefCell::new(wind.clone()));

    // Initialize window geometry state (with fullscreen from saved state if available)
    let saved_fullscreen = window_state_path
        .as_ref()
        .and_then(|path| window_state::load_state(path.as_path()))
        .map(|state| state.fullscreen)
        .unwrap_or(false);
    let window_geometry = Rc::new(RefCell::new(WindowGeometry {
        x: wind.x(),
        y: wind.y(),
        width: wind.width(),
        height: wind.height(),
        fullscreen: saved_fullscreen,
    }));

    // Create search bar (uses a sub-window so it floats on top)
    let search_bar = Rc::new(RefCell::new(SearchBar::new(editor_x, editor_y, editor_w)));

    // Create menu (system menu bar on macOS, window menu bar on other platforms)
    #[cfg(target_os = "macos")]
    menu::setup_menu(
        app_state.clone(),
        autosave_state.clone(),
        active_editor.clone(),
        statusbar.clone(),
        wind_ref.clone(),
        window_geometry.clone(),
        search_bar.clone(),
    );

    #[cfg(not(target_os = "macos"))]
    let _menu_bar = menu::setup_menu(
        app_state.clone(),
        autosave_state.clone(),
        active_editor.clone(),
        statusbar.clone(),
        wind_ref.clone(),
        window_geometry.clone(),
        search_bar.clone(),
    );

    // Configure editor UI
    active_editor
        .borrow()
        .borrow_mut()
        .set_bg_color(enums::Color::from_rgb(255, 255, 245));

    // Wire up search bar callbacks
    {
        let search_bar_for_search = search_bar.clone();
        let editor_for_search = active_editor.clone();

        // On search text change
        search_bar.borrow().on_search(move |term| {
            if let Ok(ed_ptr) = editor_for_search.try_borrow()
                && let Ok(mut ed) = ed_ptr.try_borrow_mut()
                && let Some(structured) = ed.as_any_mut().downcast_mut::<StructuredRichUI>()
            {
                let count = structured.search(&term);
                if let Ok(mut sb) = search_bar_for_search.try_borrow_mut() {
                    sb.set_match_count(if count > 0 { Some(0) } else { None }, count);
                }
                // Scroll to first match if any
                if count > 0 {
                    structured.scroll_to_current_match();
                }
                app::redraw();
            }
        });
    }

    {
        let search_bar_for_next = search_bar.clone();
        let editor_for_next = active_editor.clone();

        // On next match
        search_bar.borrow().on_next(move || {
            if let Ok(ed_ptr) = editor_for_next.try_borrow()
                && let Ok(mut ed) = ed_ptr.try_borrow_mut()
                && let Some(structured) = ed.as_any_mut().downcast_mut::<StructuredRichUI>()
                && structured.next_match()
            {
                let total = structured.search_matches().len();
                let current = structured.search_current_index();
                if let Ok(mut sb) = search_bar_for_next.try_borrow_mut() {
                    sb.set_match_count(current, total);
                }
                structured.scroll_to_current_match();
                app::redraw();
            }
        });
    }

    {
        let search_bar_for_prev = search_bar.clone();
        let editor_for_prev = active_editor.clone();

        // On previous match
        search_bar.borrow().on_prev(move || {
            if let Ok(ed_ptr) = editor_for_prev.try_borrow()
                && let Ok(mut ed) = ed_ptr.try_borrow_mut()
                && let Some(structured) = ed.as_any_mut().downcast_mut::<StructuredRichUI>()
                && structured.prev_match()
            {
                let total = structured.search_matches().len();
                let current = structured.search_current_index();
                if let Ok(mut sb) = search_bar_for_prev.try_borrow_mut() {
                    sb.set_match_count(current, total);
                }
                structured.scroll_to_current_match();
                app::redraw();
            }
        });
    }

    {
        let search_bar_for_close = search_bar.clone();
        let editor_for_close = active_editor.clone();

        // On close
        search_bar.borrow().on_close(move || {
            // Restore editor position (move up to fill the space)
            if let Ok(ed_ptr) = editor_for_close.try_borrow()
                && let Ok(mut ed) = ed_ptr.try_borrow_mut()
                && let Some(structured) = ed.as_any_mut().downcast_mut::<StructuredRichUI>()
            {
                let bar_h = search_bar::BAR_HEIGHT;
                let x = structured.x();
                let y = structured.y();
                let w = structured.width();
                let h = structured.height();
                structured.resize(x, y - bar_h, w, h + bar_h);
                structured.clear_search();
            }

            if let Ok(mut sb) = search_bar_for_close.try_borrow_mut() {
                sb.hide();
            }

            // Return focus to editor
            if let Ok(ed_ptr) = editor_for_close.try_borrow()
                && let Ok(mut ed) = ed_ptr.try_borrow_mut()
            {
                ed.take_focus();
            }
            app::redraw();
        });
    }

    wind.end();
    let pending_save_handle = Rc::new(RefCell::new(None::<app::TimeoutHandle>));

    {
        let geometry = window_geometry.clone();
        let pending = pending_save_handle.clone();
        let state_path_for_handler = window_state_path.clone();
        let search_bar_for_resize = search_bar.clone();
        let active_editor_for_resize = active_editor.clone();
        let statusbar_for_resize = statusbar.clone();
        let app_state_for_close = app_state.clone();
        let autosave_for_close = autosave_state.clone();

        wind.handle(move |win, event| match event {
            enums::Event::Move | enums::Event::Resize => {
                // Don't update geometry while in fullscreen mode - preserve the
                // pre-fullscreen window position for when we exit fullscreen
                if geometry.borrow().fullscreen {
                    return false;
                }

                if (win.x() == geometry.borrow().x)
                    && (win.y() == geometry.borrow().y)
                    && (win.width() == geometry.borrow().width)
                    && (win.height() == geometry.borrow().height)
                {
                    return false;
                }

                // Skip custom resize logic when in fullscreen mode
                // (fullscreen has its own layout with padding)
                let is_fullscreen = geometry.borrow().fullscreen;

                if !is_fullscreen {
                    // Check if search bar is visible
                    let search_bar_visible = search_bar_for_resize
                        .try_borrow()
                        .map(|sb| sb.visible())
                        .unwrap_or(false);

                    // Only resize search bar when visible to avoid FLTK resize side effects
                    if search_bar_visible && let Ok(mut sb) = search_bar_for_resize.try_borrow_mut()
                    {
                        sb.resize(0, editor_y, win.width());
                    }

                    // Resize editor based on whether search bar is visible
                    let statusbar_h = statusbar_for_resize
                        .try_borrow()
                        .map(|s| if s.visible() { s.height() } else { 0 })
                        .unwrap_or(0);

                    if let Ok(ed_ptr) = active_editor_for_resize.try_borrow()
                        && let Ok(mut ed) = ed_ptr.try_borrow_mut()
                        && let Some(structured) = ed.as_any_mut().downcast_mut::<StructuredRichUI>()
                    {
                        if search_bar_visible {
                            let bar_h = search_bar::BAR_HEIGHT;
                            let editor_top = editor_y + bar_h;
                            let editor_h = win.height() - editor_top - statusbar_h;
                            structured.resize(0, editor_top, win.width(), editor_h);
                        } else {
                            // Search bar hidden - editor fills full space
                            let editor_h = win.height() - editor_y - statusbar_h;
                            structured.resize(0, editor_y, win.width(), editor_h);
                        }
                    }
                }

                {
                    let mut geom = geometry.borrow_mut();
                    geom.x = win.x();
                    geom.y = win.y();
                    geom.width = win.width();
                    geom.height = win.height();
                }

                if let Some(handle) = {
                    let mut slot = pending.borrow_mut();
                    slot.take()
                } {
                    app::remove_timeout3(handle);
                }

                if let Some(path) = state_path_for_handler.as_ref() {
                    let geometry_for_timer = geometry.clone();
                    let pending_for_timer = pending.clone();
                    let path_for_timer = path.clone();
                    let new_handle = app::add_timeout3(WINDOW_STATE_SAVE_TIMEOUT_SECS, move |_| {
                        let snapshot = geometry_for_timer.borrow().clone();
                        if let Err(err) =
                            window_state::save_state(path_for_timer.as_path(), &snapshot)
                        {
                            eprintln!("Failed to save window state: {err}");
                        }
                        pending_for_timer.borrow_mut().take();
                    });
                    pending.borrow_mut().replace(new_handle);
                }
                false
            }
            enums::Event::Close => {
                // Flush the open note before the window goes away.
                save_current_note(
                    &app_state_for_close,
                    &autosave_for_close,
                    &active_editor_for_resize,
                    &statusbar_for_resize,
                );
                if let Some(handle) = {
                    let mut slot = pending.borrow_mut();
                    slot.take()
                } {
                    app::remove_timeout3(handle);
                }
                if let Some(path) = state_path_for_handler.as_ref() {
                    let snapshot = geometry.borrow().clone();
                    if let Err(err) = window_state::save_state(path.as_path(), &snapshot) {
                        eprintln!("Failed to save window state on close: {err}");
                    }
                }
                false
            }
            _ => false,
        });
    }

    active_editor.borrow().borrow().set_resizable(&mut wind);
    wind.show();

    // Restore fullscreen mode if it was previously enabled
    if saved_fullscreen {
        // Determine which screen the window is on using its center point
        let win_center_x = wind.x() + wind.width() / 2;
        let win_center_y = wind.y() + wind.height() / 2;
        let screen_num = app::screen_num(win_center_x, win_center_y);

        // Enter fullscreen mode
        wind.fullscreen(true);

        // Calculate and apply padding using the correct screen dimensions
        let (_, _, screen_w, screen_h) = app::screen_xywh(screen_num);
        let font_size = 14; // Default body text font size from theme
        let char_width = (font_size as f32 * 0.55) as i32;
        let target_text_width = char_width * 90; // ~90 chars
        let scrollbar_width = 15;
        let available_width = screen_w - scrollbar_width;
        let padding = ((available_width - target_text_width) / 2).max(25);

        // Apply padding and resize the editor to take full height
        if let Ok(active_ptr) = active_editor.try_borrow()
            && let Ok(mut editor) = active_ptr.try_borrow_mut()
            && let Some(structured) = editor.as_any_mut().downcast_mut::<StructuredRichUI>()
        {
            structured.set_horizontal_padding(padding);
            // Expand editor to full screen height (no statusbar)
            let y = structured.y();
            structured.resize(0, y, screen_w, screen_h - y);
        }

        // Hide status bar
        statusbar.borrow_mut().hide();
    }

    // Clicking the note status opens the note picker
    {
        let app_state = app_state.clone();
        let autosave_state = autosave_state.clone();
        let active_editor = active_editor.clone();
        let statusbar_for_click = statusbar.clone();
        let wind_for_click = wind.clone();
        statusbar.borrow_mut().on_note_click(move |_| {
            note_picker::show_note_picker(
                app_state.clone(),
                autosave_state.clone(),
                active_editor.clone(),
                statusbar_for_click.clone(),
                &wind_for_click,
            );
        });
    }

    // Load initial note
    load_note_helper(
        &args.note,
        &app_state,
        &autosave_state,
        &active_editor,
        &statusbar,
        None,
    );

    // Wire callbacks for active editor
    wire_editor_callbacks(&active_editor, &autosave_state, &app_state, &statusbar);

    // Set up periodic timer to update "X ago" display
    {
        let autosave_ref = autosave_state.clone();
        let statusbar_ref = statusbar.clone();

        app::add_timeout3(SAVE_STATUS_UPDATE_INTERVAL_SECS, move |handle| {
            // Update the status text
            if let (Ok(as_state), Ok(mut sb)) =
                (autosave_ref.try_borrow(), statusbar_ref.try_borrow_mut())
                && !as_state.is_saving
                && as_state.last_save_time.is_some()
            {
                sb.set_status(&as_state.get_status_text());
                app::redraw();
            }

            // Repeat every second
            app::repeat_timeout3(SAVE_STATUS_UPDATE_INTERVAL_SECS, handle);
        });
    }

    // Set up a lightweight tick for blinking cursor and animations
    {
        let start = Instant::now();
        let editor_ref = active_editor.clone();
        app::add_timeout3(0.1, move |handle| {
            let ms = start.elapsed().as_millis() as u64;
            if let Ok(ed_ptr) = editor_ref.try_borrow()
                && let Ok(mut ed) = (*ed_ptr).try_borrow_mut()
            {
                ed.tick(ms);
            }
            app::repeat_timeout3(0.1, handle);
        });
    }

    // No window activation forwarding needed; cursor shows when widget has focus

    // Rename the macOS application menu now that the system menu bar exists, so
    // an unbundled binary shows "Piki" instead of "piki-gui".
    app_icon::set_macos_app_name("Piki");

    // Replace FLTK's default about box with a proper macOS about panel (real
    // name, version, icon, description and homepage link).
    app_icon::set_macos_about();

    app.run().unwrap();
}

fn wire_editor_callbacks(
    active_editor: &Rc<RefCell<Rc<RefCell<dyn NoteUI>>>>,
    autosave_state: &Rc<RefCell<AutoSaveState>>,
    app_state: &Rc<RefCell<AppState>>,
    statusbar: &Rc<RefCell<StatusBar>>,
) {
    let editor_for_callback = active_editor.clone();
    let autosave_for_callback = autosave_state.clone();
    let app_state_for_callback = app_state.clone();
    let statusbar_for_callback = statusbar.clone();
    let current_for_change = active_editor.borrow().clone();
    current_for_change.borrow_mut().on_change(Box::new(move || {
        // Restyle if supported
        let editor_clone = editor_for_callback.clone();
        app::awake_callback(move || {
            if let Ok(ed_ptr) = editor_clone.try_borrow() {
                let mut ed_ref = (*ed_ptr).borrow_mut();
                ed_ref.restyle();
            }
        });

        if let Ok(mut as_state) = autosave_for_callback.try_borrow_mut() {
            as_state.mark_changed();
        }

        let editor_clone = editor_for_callback.clone();
        let autosave_clone = autosave_for_callback.clone();
        let app_state_clone = app_state_for_callback.clone();
        let statusbar_clone = statusbar_for_callback.clone();

        app::add_timeout3(AUTOSAVE_INTERVAL_SECS, move |_| {
            let should_save = autosave_clone
                .try_borrow()
                .map(|s| s.pending_save)
                .unwrap_or(false);

            if should_save {
                if let Ok(mut sb) = statusbar_clone.try_borrow_mut() {
                    sb.set_status("Saving …");
                    app::redraw();
                }

                if let (Ok(ed_ptr), Ok(mut as_state), Ok(app_st)) = (
                    editor_clone.try_borrow(),
                    autosave_clone.try_borrow_mut(),
                    app_state_clone.try_borrow(),
                ) {
                    let ed_ref = (*ed_ptr).borrow();
                    match as_state.trigger_save(&*ed_ref, &app_st.store) {
                        Ok(()) => {
                            if let Ok(mut sb) = statusbar_clone.try_borrow_mut() {
                                sb.set_status(&as_state.get_status_text());
                                app::redraw();
                            }
                        }
                        Err(e) => {
                            if let Ok(mut sb) = statusbar_clone.try_borrow_mut() {
                                sb.set_status(&format!("Error: {}", e));
                                app::redraw();
                            }
                        }
                    }
                }
            }
        });
    }));

    // Link click handler via NoteUI uses active editor
    let app_state_links = app_state.clone();
    let autosave_links = autosave_state.clone();
    let statusbar_links = statusbar.clone();
    let current_for_links = active_editor.borrow().clone();
    {
        let mut cur = current_for_links.borrow_mut();
        let active_clone = active_editor.clone();
        cur.on_link_click(Box::new(move |link_dest: String| {
            // External links (http(s)://, mailto:, ...) open in the system
            // browser/handler instead of being loaded as a wiki note.
            if link_handler::is_external_link(&link_dest) {
                let statusbar = statusbar_links.clone();
                app::awake_callback(move || {
                    if let Err(e) = webbrowser::open(&link_dest) {
                        statusbar
                            .borrow_mut()
                            .set_status(&format!("Failed to open link: {}", e));
                        app::redraw();
                    }
                });
                return;
            }

            let app_state = app_state_links.clone();
            let autosave_state = autosave_links.clone();
            let editor_ref = active_clone.clone();
            let statusbar = statusbar_links.clone();
            app::awake_callback(move || {
                load_note_helper(
                    &link_dest,
                    &app_state,
                    &autosave_state,
                    &editor_ref,
                    &statusbar,
                    None,
                );
            });
        }));
    }

    // Hover handler to show link destinations in the note status bar
    let current_for_hover = active_editor.borrow().clone();
    {
        let mut cur = current_for_hover.borrow_mut();
        let statusbar_clone = statusbar.clone();
        let base_label: Rc<RefCell<Option<String>>> = Rc::new(RefCell::new(None));
        cur.on_link_hover(Box::new(move |target: Option<String>| {
            let statusbar_for_cb = statusbar_clone.clone();
            let base_label_for_cb = base_label.clone();
            let tgt = target.clone();
            app::awake_callback(move || {
                match &tgt {
                    Some(dest) => {
                        let dest = dest.clone();
                        if base_label_for_cb.borrow().is_none() {
                            let current = statusbar_for_cb.borrow().note_status_widget().label();
                            *base_label_for_cb.borrow_mut() = Some(current);
                        }
                        statusbar_for_cb.borrow_mut().set_note(&dest);
                    }
                    None => {
                        if let Some(orig) = base_label_for_cb.borrow_mut().take() {
                            statusbar_for_cb.borrow_mut().set_note(&orig);
                        }
                    }
                }
                app::redraw();
            });
        }));
    }
}