annatomic 0.4.0

The Annatomic annotation editor is intended to be used for the [RIDGES corpus](https://www.linguistik.hu-berlin.de/en/institut-en/professuren-en/korpuslinguistik/research/ridges-projekt). It is based on [graphANNIS](https://github.com/korpling/graphANNIS) and thus is internal data model is in principle suitable for a wide range of annotation concepts. "
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
use std::sync::{Arc, OnceLock};

use anyhow::Result;
use clap::Parser;
use editors::corpus_tree::CorpusTree;
use editors::spans::SpanEditor;
use eframe::IntegrationInfo;
use egui::{Button, Color32, FontData, Key, KeyboardShortcut, MenuBar, Modifiers, RichText, Theme};
use egui_file_dialog::{FileDialog, OpeningMode};
use graphannis::update::GraphUpdate;
use job_executor::JobExecutor;
use lazy_static::lazy_static;
use log::debug;
use messages::Notifier;
use project::Project;
use serde::{Deserialize, Serialize};
use views::Editor;

use crate::app::{job_executor::Job, project::ChangeSet};

pub(crate) mod actions;
pub(crate) mod data;
mod editors;
pub(crate) mod job_executor;
mod messages;
mod project;
#[cfg(test)]
mod tests;
pub(crate) mod util;
mod views;
pub(crate) mod widgets;

pub(crate) const APP_ID: &str = "annatomic";
pub const QUIT_SHORTCUT: KeyboardShortcut = KeyboardShortcut::new(Modifiers::COMMAND, Key::Q);
pub const SAVE_SHORTCUT: KeyboardShortcut = KeyboardShortcut::new(Modifiers::COMMAND, Key::S);
pub const UNDO_SHORTCUT: KeyboardShortcut = KeyboardShortcut::new(Modifiers::COMMAND, Key::Z);
pub const REDO_SHORTCUT: KeyboardShortcut = KeyboardShortcut::new(Modifiers::COMMAND, Key::Y);
pub const GO_BACK_SHORTCUT: KeyboardShortcut =
    KeyboardShortcut::new(Modifiers::ALT, Key::ArrowLeft);

pub const CHANGE_PENDING_COLOR_DARK: Color32 = Color32::from_rgb(160, 50, 50);
pub const CHANGE_PENDING_COLOR_LIGHT: Color32 = Color32::from_rgb(255, 128, 128);

lazy_static! {
    static ref OPEN_LABEL: String = format!("Open {}", egui_phosphor::regular::ARROW_RIGHT);
}

/// Which main view to show in the app
#[derive(Default, serde::Deserialize, serde::Serialize, Clone, PartialEq, Debug)]
pub(crate) enum MainView {
    #[default]
    Start,
    Import,
    Export,
    CorpusStructure,
    EditDocument {
        node_name: String,
    },
}

#[derive(Parser, Debug, Default, Serialize, Deserialize)]
pub struct AnnatomicArgs {
    /// Start in development mode which displays additional information only relevant for developers.
    #[arg(long)]
    dev: bool,
}

#[derive(Default)]
enum ShutdownRequest {
    #[default]
    None,
    Requested,
    ShutdownIsSafe,
}

#[derive(serde::Deserialize, serde::Serialize)]
#[serde(default)]
pub struct AnnatomicApp {
    main_view: MainView,
    new_corpus_name: String,
    project: Project,
    #[serde(skip)]
    current_editor: OnceLock<Box<dyn Editor>>,
    #[serde(skip)]
    last_selected_node: Option<String>,
    #[serde(skip)]
    shutdown_request: ShutdownRequest,
    #[serde(skip)]
    jobs: JobExecutor,
    #[serde(skip)]
    notifier: Notifier,
    #[serde(skip)]
    args: AnnatomicArgs,
    #[serde(skip)]
    file_dialog: FileDialog,
}

impl Default for AnnatomicApp {
    fn default() -> Self {
        let notifier = Notifier::default();
        let jobs = JobExecutor::default();
        let project = Project::new(notifier.clone(), jobs.clone());

        Self {
            main_view: MainView::Start,
            project,
            jobs,
            notifier,
            new_corpus_name: String::default(),
            last_selected_node: None,
            args: AnnatomicArgs::default(),
            current_editor: OnceLock::new(),
            shutdown_request: ShutdownRequest::None,
            file_dialog: FileDialog::new().opening_mode(OpeningMode::LastVisitedDir),
        }
    }
}

pub(crate) fn set_fonts(ctx: &egui::Context) {
    let mut defs = egui::FontDefinitions::default();

    // Symbols and Emojis
    defs.font_data.insert(
        "NotoEmoji-Regular".to_owned(),
        Arc::new(FontData::from_static(include_bytes!(
            "../assets/Noto_Emoji/static/NotoEmoji-Regular.ttf"
        ))),
    );
    defs.font_data.insert(
        "NotoSansMath-Regular".to_owned(),
        Arc::new(FontData::from_static(include_bytes!(
            "../assets/Noto_Sans_Math/NotoSansMath-Regular.ttf"
        ))),
    );
    defs.font_data.insert(
        "NotoSansSymbols2-Regular".to_owned(),
        Arc::new(FontData::from_static(include_bytes!(
            "../assets/Noto_Sans_Symbols_2/NotoSansSymbols2-Regular.ttf"
        ))),
    );

    // Regular font
    defs.font_data.insert(
        "NotoSans-Regular".to_owned(),
        Arc::new(FontData::from_static(include_bytes!(
            "../assets/Noto_Sans/static/NotoSans-Regular.ttf"
        ))),
    );

    // Monospaced font
    defs.font_data.insert(
        "NotoSansMono-Regular".to_owned(),
        Arc::new(FontData::from_static(include_bytes!(
            "../assets/Noto_Sans_Mono/static/NotoSansMono-Regular.ttf"
        ))),
    );

    // Define the fonts to use for each font family
    defs.families.insert(
        egui::FontFamily::Proportional,
        vec![
            "NotoSans-Regular".to_owned(),
            "NotoEmoji-Regular".to_owned(),
            "NotoSansMath-Regular".to_owned(),
            "NotoSansSymbols2-Regular".to_owned(),
        ],
    );
    defs.families.insert(
        egui::FontFamily::Monospace,
        vec!["NotoSansMono-Regular".to_owned()],
    );
    // Phosphor icons
    egui_phosphor::add_to_fonts(&mut defs, egui_phosphor::Variant::Regular);

    ctx.set_fonts(defs);
}

impl AnnatomicApp {
    /// Called once before the first frame.
    pub fn new(cc: &eframe::CreationContext<'_>, args: AnnatomicArgs) -> Result<Self> {
        // Set fonts once
        set_fonts(&cc.egui_ctx);

        // Load previous app state (if any).
        // Note that you must enable the `persistence` feature for this to work.
        let mut app = if let Some(storage) = cc.storage {
            let mut app_from_storage: AnnatomicApp =
                eframe::get_value(storage, eframe::APP_KEY).unwrap_or_default();
            app_from_storage.args = args;
            app_from_storage
        } else {
            Self {
                args,
                ..Default::default()
            }
        };
        // Rebuild the state that is not persisted but calculated
        app.project
            .load_after_init(app.notifier.clone(), app.jobs.clone())?;
        // Find all folders in the corpus directory that are not actually used and delete them
        app.project.cleanup_unused_corpus_files_in_background()?;

        Ok(app)
    }

    pub(crate) fn change_view(&mut self, new_view: MainView) {
        if self.main_view != new_view {
            self.main_view = new_view;
            self.reset_editor();
        }
    }

    fn reset_editor(&mut self) {
        if let Some(old_editor) = self.current_editor.take() {
            self.last_selected_node = old_editor.get_selected_nodes().first().cloned();
        }
    }

    pub(crate) fn load_editor_if_necessary(&mut self) {
        match &self.main_view {
            MainView::Start | MainView::Import | MainView::Export => {
                self.current_editor = OnceLock::new();
            }
            MainView::CorpusStructure => {
                if let Some(corpus) = &self.project.selected_corpus {
                    let job_title = "Creating corpus tree editor";

                    if self.current_editor.get().is_none()
                        && !self.jobs.has_job_with_title(job_title)
                    {
                        self.current_editor.take();

                        let corpus_cache = self.project.corpus_cache.clone();
                        let jobs = self.jobs.clone();
                        let notifier = self.notifier.clone();
                        let location = corpus.location.clone();
                        let root_corpus_name = self
                            .project
                            .selected_corpus
                            .as_ref()
                            .map(|c| c.name.clone())
                            .unwrap_or_default();
                        let last_selected_node = self.last_selected_node.clone();
                        self.jobs.add_foreground_job(
                            job_title,
                            move |job| {
                                job.update_message("Loading corpus");
                                let graph = corpus_cache.get(&location)?;

                                let last_selected_node = if let Some(node_name) = last_selected_node
                                {
                                    let graph = graph.read();
                                    graph.get_node_annos().get_node_id_from_name(&node_name)?
                                } else {
                                    None
                                };

                                job.update_message("Loading corpus graph");
                                let corpus_tree = CorpusTree::create_from_graph(
                                    root_corpus_name,
                                    graph,
                                    last_selected_node,
                                    jobs,
                                    notifier,
                                )?;
                                Ok(corpus_tree)
                            },
                            |corpus_tree, app| {
                                app.current_editor.get_or_init(|| Box::new(corpus_tree));
                            },
                        );
                    }
                } else {
                    self.current_editor = OnceLock::new();
                }
            }
            MainView::EditDocument { node_name } => {
                if let Some(corpus) = &self.project.selected_corpus {
                    let job_title = "Creating document editor";
                    if self.current_editor.get().is_none()
                        && !self.jobs.has_job_with_title(job_title)
                    {
                        self.current_editor.take();
                        let corpus_cache = self.project.corpus_cache.clone();
                        let location = corpus.location.clone();
                        let notifier = self.notifier.clone();
                        let node_name = node_name.clone();
                        self.jobs.add_foreground_job(
                            job_title,
                            move |job| {
                                job.update_message("Loading corpus");
                                let graph = corpus_cache.get(&location)?;
                                job.update_message("Loading document");
                                let start_time = std::time::Instant::now();

                                let document_editor = SpanEditor::create_from_graph(
                                    node_name, true, graph, notifier,
                                )?;
                                debug!(
                                    "Creating document editor took {} ms",
                                    start_time.elapsed().as_millis()
                                );

                                Ok(document_editor)
                            },
                            |document_editor, app| {
                                app.current_editor.get_or_init(|| Box::new(document_editor));
                            },
                        );
                    }
                }
            }
        }
    }

    fn handle_corpus_confirmation_dialog(&mut self, ctx: &egui::Context) {
        if self.project.scheduled_for_deletion.is_some() {
            egui::Modal::new("corpus_deletion_confirmation".into()).show(ctx, |ui| {
                let corpus_name = self
                    .project
                    .scheduled_for_deletion
                    .clone()
                    .unwrap_or_default();
                ui.horizontal(|ui| {
                    ui.label(RichText::new(egui_phosphor::regular::WARNING).color(Color32::ORANGE).size(32.0));
                    ui.label(format!("Are you sure to delete the corpus \"{corpus_name}\" permanently? This can not be undone."));
                });
                ui.separator();
                ui.horizontal(|ui| {
                    if ui
                        .button(RichText::new("Do not delete the corpus.").color(Color32::BLUE))
                        .clicked()
                    {
                        self.project.scheduled_for_deletion = None;
                    }
                    ui.add_space(5.0);
                    if ui
                        .button(
                            RichText::new(format!("Delete \"{corpus_name}\" permanently"))
                                .color(Color32::RED),
                        )
                        .clicked()
                    {
                        self.project.delete_corpus(corpus_name);
                    }
                });
            });
        }
    }

    pub(crate) fn select_corpus(&mut self, selection: Option<String>) {
        self.project.select_corpus(selection);
        self.reset_editor();
    }

    pub(crate) fn current_typed_editor<E: Editor + 'static>(&mut self) -> Option<&mut E> {
        if let Some(editor) = self.current_editor.get_mut() {
            editor.any_mut().downcast_mut()
        } else {
            None
        }
    }

    fn apply_pending_updates(&mut self) -> Result<()> {
        if let Some(editor) = self.current_editor.get_mut()
            && let Some(graph) = &self.project.get_selected_graph()?
            && editor.has_pending_updates()
        {
            let graph = graph.read();
            let pending_actions = editor.take_pending_updates();

            let mut changesets = Vec::new();
            for action in pending_actions {
                let mut change = ChangeSet::default();
                if let Some(editor) = self.current_editor.get() {
                    change.editor_state_updates = editor.editor_state_updates(&action)?;
                }
                let changeset_from_action = action.create_changeset(&graph)?;
                change.updates = changeset_from_action.updates;
                change.reverse_updates = changeset_from_action.reverse_updates;
                changesets.push(change);
            }

            if !changesets.is_empty() {
                self.add_changesets(changesets);
            }
        }
        Ok(())
    }

    pub(crate) fn add_changesets(&mut self, changesets: Vec<ChangeSet>) {
        if let Some(selected_corpus) = self.project.selected_corpus.clone() {
            for changes in changesets {
                if let Some(state_update_before) = changes.editor_state_updates.before {
                    state_update_before(self)
                }
                let corpus_cache = self.project.corpus_cache.clone();
                let selected_corpus = selected_corpus.clone();
                let worker = move |job: Job| {
                    job.update_message("Storing update events");
                    let mut update = GraphUpdate::new();
                    for c in &changes.updates {
                        update.add_event(c.clone())?;
                    }
                    job.update_message("Loading corpus if necessary");
                    let graph = corpus_cache.get(&selected_corpus.location)?;
                    job.update_message("Applying updates");
                    let mut graph = graph.write();
                    graph.apply_update_keep_statistics(&mut update, |msg| {
                        job.update_message(format!("Applying updates: {msg}"))
                    })?;

                    let changeset = ChangeSet::from((changes.updates, changes.reverse_updates));
                    Ok(changeset)
                };

                if let Some(state_update_after) = changes.editor_state_updates.after {
                    self.jobs
                        .add_foreground_job("Updating corpus", worker, |changeset, app| {
                            app.project.add_checkpoint(changeset);
                            state_update_after(app);
                        });
                } else {
                    self.jobs
                        .add_background_job("Updating corpus", worker, |changeset, app| {
                            app.project.add_checkpoint(changeset);
                        });
                }
            }
        }
    }

    fn consume_shortcuts(&mut self, ctx: &egui::Context) {
        // Consume any potential context sensitve shortcuts from the editor
        if let Some(editor) = self.current_editor.get_mut() {
            editor.consume_shortcuts(ctx);
        }

        // Consume all shortcuts from the application itself, which can be active at any time
        if ctx.input_mut(|i| i.consume_shortcut(&QUIT_SHORTCUT)) {
            ctx.send_viewport_cmd(egui::ViewportCommand::Close);
        }
        if ctx.input_mut(|i| i.consume_shortcut(&UNDO_SHORTCUT)) {
            self.project.undo();
        }
        if ctx.input_mut(|i| i.consume_shortcut(&REDO_SHORTCUT)) {
            self.project.redo();
        }
        if ctx.input_mut(|i| i.consume_shortcut(&SAVE_SHORTCUT)) {
            let result = self.apply_pending_updates();
            self.notifier.ok_or_report(result);
        }
        if ctx.input_mut(|i| i.consume_shortcut(&GO_BACK_SHORTCUT)) {
            if self.main_view == MainView::CorpusStructure {
                self.change_view(MainView::Start);
            } else if let MainView::EditDocument { .. } = self.main_view {
                self.change_view(MainView::CorpusStructure);
            }
        }
    }

    pub(crate) fn show(&mut self, ctx: &egui::Context, frame_info: &IntegrationInfo) {
        egui_extras::install_image_loaders(ctx);

        // Check if we need to react to a closing event
        if let ShutdownRequest::None = self.shutdown_request
            && ctx.input(|input_state| input_state.viewport().close_requested())
        {
            // We are currently not shutting down, so initiate the process
            if let Some(editor) = self.current_editor.get()
                && editor.has_pending_updates()
            {
                ctx.send_viewport_cmd(egui::ViewportCommand::CancelClose);
                let result = self.apply_pending_updates();
                self.notifier.ok_or_report(result);
                self.shutdown_request = ShutdownRequest::Requested;
            } else {
                self.shutdown_request = ShutdownRequest::ShutdownIsSafe;
            }
        }

        match self.shutdown_request {
            ShutdownRequest::None => {
                self.show_view(ctx, frame_info);
            }
            ShutdownRequest::Requested => {
                if !self.jobs.clone().show(ctx, self) {
                    // No more jobs to show
                    self.shutdown_request = ShutdownRequest::ShutdownIsSafe;
                    ctx.send_viewport_cmd(egui::ViewportCommand::Close);
                }
            }
            ShutdownRequest::ShutdownIsSafe => {
                egui::CentralPanel::default().show(ctx, |ui| {
                    ui.heading("Closing application...");
                    ui.label("Please wait until the corpus is persisted to disk.");
                });
            }
        }
    }

    fn show_view(&mut self, ctx: &egui::Context, frame_info: &IntegrationInfo) {
        self.consume_shortcuts(ctx);
        self.handle_corpus_confirmation_dialog(ctx);
        self.load_editor_if_necessary();

        let has_foreground_jobs = self.jobs.clone().show(ctx, self);
        if !has_foreground_jobs {
            egui::TopBottomPanel::top("top_panel").show(ctx, |ui| {
                MenuBar::new().ui(ui, |ui| {
                    ui.image(egui::include_image!("../assets/icon-32.png"));
                    ui.menu_button("File", |ui| {
                        if ui
                            .add(
                                Button::new("Quit")
                                    .shortcut_text(ctx.format_shortcut(&QUIT_SHORTCUT)),
                            )
                            .clicked()
                        {
                            ctx.send_viewport_cmd(egui::ViewportCommand::Close);
                        }
                    });
                    ui.menu_button("Edit", |ui| {
                        if let Some(editor) = self.current_editor.get_mut() {
                            editor.add_edit_menu_entries(ui);
                        }

                        if ui
                            .add_enabled(
                                self.project.can_undo(),
                                Button::new("Undo")
                                    .shortcut_text(ctx.format_shortcut(&UNDO_SHORTCUT)),
                            )
                            .clicked()
                        {
                            self.project.undo();
                        }
                        if ui
                            .add_enabled(
                                self.project.can_redo(),
                                Button::new("Redo")
                                    .shortcut_text(ctx.format_shortcut(&REDO_SHORTCUT)),
                            )
                            .clicked()
                        {
                            self.project.redo();
                        }
                    });
                    ui.menu_button("View", |ui| {
                        egui::gui_zoom::zoom_menu_buttons(ui);
                    });
                    ui.add_space(16.0);
                    ui.separator();
                    let marker_color = if ui.ctx().theme() == Theme::Light {
                        CHANGE_PENDING_COLOR_LIGHT
                    } else {
                        CHANGE_PENDING_COLOR_DARK
                    };
                    if let Some(editor) = self.current_editor.get()
                        && editor.has_pending_updates()
                    {
                        ui.label(RichText::new("Has pending changes").color(marker_color));
                        let result = self.apply_pending_updates();
                        self.notifier.ok_or_report(result);
                    } else {
                        ui.label("No pending changes");
                    }
                    ui.separator();
                    ui.add_space(16.0);
                    if self.args.dev
                        && let Some(seconds) = frame_info.cpu_usage
                    {
                        ui.label(format!("CPU usage: {:.1} ms / frame", seconds * 1000.0));
                        ui.add_space(16.0);
                    }

                    egui::widgets::global_theme_preference_switch(ui);
                });
            });
            egui::CentralPanel::default().show(ctx, |ui| {
                if !has_foreground_jobs {
                    self.notifier.show(ctx);
                    let response = match self.main_view {
                        MainView::Start => views::start::show(ui, self),
                        MainView::Import => views::import::show(ui, self),
                        MainView::Export => views::export::show(ui, self),
                        MainView::CorpusStructure => views::corpus_structure::show(ui, self),
                        MainView::EditDocument { .. } => views::edit::show(ui, self),
                    };
                    if let Err(e) = response {
                        self.notifier.report_error(e);
                    }
                }
            });
        }
    }
}

impl eframe::App for AnnatomicApp {
    /// Called by the frame work to save state before shutdown.
    fn save(&mut self, storage: &mut dyn eframe::Storage) {
        eframe::set_value(storage, eframe::APP_KEY, self);
    }

    /// Called each time the UI needs repainting, which may be many times per second.
    fn update(&mut self, ctx: &egui::Context, frame: &mut eframe::Frame) {
        self.show(ctx, frame.info());
        self.file_dialog.update(ctx);
    }

    fn on_exit(&mut self) {
        // Persist the changes in the annotation graph
        self.notifier
            .ok_or_report(self.project.persist_changes_on_exit());
    }
}