avis-imgv 0.1.0

Image viewer based on egui. Makes use of modern RAM amounts by loading images ahead of time for very fast responsiveness. Minimal UI with heavy use of shortcuts.
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
use crate::{
    callback::Callback,
    config::{Config, GeneralConfig},
    crawler,
    db::Db,
    metadata::Metadata,
    multi_gallery::MultiGallery,
    navigator,
    perf_metrics::PerfMetrics,
    single_gallery::SingleGallery,
    tree, utils, Order, VALID_EXTENSIONS,
};
use eframe::egui::{self, KeyboardShortcut};
use notify::{Event, INotifyWatcher, RecursiveMode, Watcher};
use rand::seq::SliceRandom;
use rand::thread_rng;
use rfd::FileDialog;
use std::{
    path::{Path, PathBuf},
    sync::{Arc, Mutex},
    time::SystemTime,
};

pub struct App {
    paths: Vec<PathBuf>,
    gallery: SingleGallery,
    ///used when switching between galleries
    gallery_selected_index: Option<usize>,
    multi_gallery: MultiGallery,
    perf_metrics_visible: bool,
    multi_gallery_visible: bool,
    top_menu_visible: bool,
    dir_tree_visible: bool,
    base_path: PathBuf,
    dir_flattened: bool, //Fetches images for all subdirectories recursively
    navigator_visible: bool,
    navigator_search: String, //TODO: Investigate why this exists in the app struct
    perf_metrics: PerfMetrics,
    config: GeneralConfig,
    order: Order,
    watcher: Option<INotifyWatcher>,
    watcher_events: Arc<Mutex<Vec<Event>>>,
}

impl App {
    pub fn new(cc: &eframe::CreationContext<'_>) -> Self {
        crate::theme::apply_theme(&cc.egui_ctx);
        let cfg = Config::new();

        let mut style = (*cc.egui_ctx.style()).clone();

        for t_styles in style.text_styles.iter_mut() {
            t_styles.1.size *= cfg.general.text_scaling;
        }

        cc.egui_ctx.set_style(style);

        let (mut img_paths, opened_img_path) = crawler::paths_from_args();

        //TODO: Implement a default ordering
        Self::sort_images(&mut img_paths, &Order::DateDesc);
        img_paths.sort();

        match Db::init_db() {
            Ok(_) => {
                println!("Database initiated successfully");
                match Db::trim_db(&cfg.general.limit_cached) {
                    Ok(_) => Metadata::cache_metadata_for_images(&img_paths),
                    Err(e) => {
                        println!("Failure trimming db {}", e);
                    }
                };
            }
            Err(e) => {
                println!("Failure initiating db -> {}", e);
            }
        };

        Self {
            gallery: SingleGallery::new(
                &img_paths,
                &opened_img_path,
                cfg.gallery,
                &cfg.general.output_icc_profile,
            ),
            gallery_selected_index: None,
            multi_gallery: MultiGallery::new(
                &img_paths,
                cfg.multi_gallery,
                &cfg.general.output_icc_profile,
            ),
            perf_metrics_visible: false,
            multi_gallery_visible: false,
            top_menu_visible: false,
            dir_tree_visible: false,
            dir_flattened: false,
            base_path: Self::get_base_path(&img_paths),
            navigator_visible: false,
            navigator_search: Self::get_base_path(&img_paths)
                .to_str()
                .unwrap_or_default()
                .to_string(),
            perf_metrics: PerfMetrics::new(),
            config: cfg.general,
            order: Order::Asc,
            paths: img_paths,
            watcher: None,
            watcher_events: Arc::new(Mutex::new(vec![])),
        }
    }

    ///Returns the path to the opened image directory, if it's not unable to do this it then
    ///tries to return the users home, if this fail it just returns a default PathBuf
    fn get_base_path(paths: &[PathBuf]) -> PathBuf {
        if let Some(first_path) = paths.get(0) {
            if let Some(parent) = first_path.parent() {
                return parent.to_path_buf();
            }
        }

        if let Some(user_dirs) = directories::UserDirs::new() {
            return user_dirs.home_dir().to_path_buf();
        }

        PathBuf::default()
    }

    //Maybe have gallery show this
    fn handle_input(&mut self, ctx: &egui::Context) {
        if ctx.input_mut(|i| i.consume_shortcut(&self.config.sc_exit.kbd_shortcut)) {
            std::process::exit(0);
        }

        if utils::are_inputs_muted(ctx) {
            return;
        }

        if ctx.input(|i| i.key_pressed(egui::Key::F10)) {
            self.perf_metrics_visible = !self.perf_metrics_visible;
        }

        if ctx.input_mut(|i| i.consume_shortcut(&self.config.sc_menu.kbd_shortcut)) {
            self.top_menu_visible = !self.top_menu_visible;
        }

        if ctx.input_mut(|i| i.consume_shortcut(&self.config.sc_toggle_gallery.kbd_shortcut)) {
            self.multi_gallery_visible = !self.multi_gallery_visible;
            self.gallery_selected_index = Some(self.gallery.selected_img_index);
        }

        if ctx.input_mut(|i| i.consume_shortcut(&self.config.sc_flatten_dir.kbd_shortcut)) {
            self.flatten_open_dir();
        }

        if ctx.input_mut(|i| i.consume_shortcut(&self.config.sc_watch_directory.kbd_shortcut)) {
            self.enable_watcher();
        }
    }

    //Muter inputs will block all other inputs
    //This is required so typing in text boxes and the like doesn't
    //trigger shortcuts
    fn handle_input_muters(&mut self, ctx: &egui::Context) {
        let to_check: Vec<(&mut bool, &KeyboardShortcut)> = vec![
            (
                &mut self.navigator_visible,
                &self.config.sc_navigator.kbd_shortcut,
            ),
            (
                &mut self.dir_tree_visible,
                &self.config.sc_dir_tree.kbd_shortcut,
            ),
        ];

        let is_muted = utils::are_inputs_muted(ctx);

        //Assumes all muters can and will be closed with Escape
        for (active, shortcut) in to_check {
            if (is_muted && *active && ctx.input_mut(|i| i.consume_shortcut(shortcut)))
                || (!is_muted && ctx.input_mut(|i| i.consume_shortcut(shortcut)))
                || (*active && ctx.input(|i| i.key_pressed(egui::Key::Escape)))
            {
                *active = !*active;

                if *active {
                    utils::set_mute_state(ctx, true);
                    return;
                } else {
                    utils::set_mute_state(ctx, false);
                }
            }
        }
    }

    fn folder_picker(&mut self) {
        let folder = self.get_file_dialog().pick_folder();

        if let Some(folder) = folder {
            self.set_images_from_path(&folder, &None)
        }
    }

    fn files_picker(&mut self) {
        let files = self
            .get_file_dialog()
            .add_filter("image", VALID_EXTENSIONS)
            .pick_files();

        if files.is_none() {
            return;
        }

        if let Some(files) = files {
            if let Some(parent) = &files[0].parent() {
                self.set_images_from_path(parent, &Some(files[0].clone()))
            }
        }
    }

    fn get_file_dialog(&mut self) -> FileDialog {
        let mut file_dialog = FileDialog::new();

        if let Some(path) = self.gallery.get_active_img_path() {
            if let Some(parent) = path.parent() {
                file_dialog = file_dialog.set_directory(parent);
            }
        }

        file_dialog
    }

    fn sort_and_set(&mut self) {
        Self::sort_images(&mut self.paths, &self.order);
        self.set_images(&None, false);
    }

    //Will crawl, assumes new directory
    fn set_images_from_path(&mut self, path: &Path, selected_img: &Option<PathBuf>) {
        self.paths = crawler::crawl(path, self.dir_flattened);
        Self::sort_images(&mut self.paths, &self.order);
        self.set_images(selected_img, true);
    }

    fn set_images(&mut self, selected_img: &Option<PathBuf>, new_dir_opened: bool) {
        Metadata::cache_metadata_for_images(&self.paths);
        self.load_images(selected_img, new_dir_opened);
    }

    fn load_images(&mut self, selected_img: &Option<PathBuf>, new_dir_opened: bool) {
        self.gallery.set_images(&self.paths, selected_img);
        self.multi_gallery.set_images(&self.paths);

        if new_dir_opened {
            self.base_path = Self::get_base_path(&self.paths);
            self.navigator_search = self.base_path.to_str().unwrap_or_default().to_string();
        }
    }

    //TODO: Fix order by date/etc/validate everything
    fn sort_images(paths: &mut [PathBuf], order: &Order) {
        println!("sorting images...");
        for path in paths.iter() {
            println!("{}", path.display());
        }

        if order == &Order::Random {
            paths.shuffle(&mut thread_rng());
        } else if order == &Order::Asc || order == &Order::Desc {
            paths.sort_by(|a, b| {
                let first: String;
                let second: String;

                if order == &Order::Asc {
                    first = a.file_name().unwrap().to_string_lossy().to_string();
                    second = b.file_name().unwrap().to_string_lossy().to_string();
                } else {
                    first = b.file_name().unwrap().to_string_lossy().to_string();
                    second = a.file_name().unwrap().to_string_lossy().to_string();
                }

                first.cmp(&second)
            });
        } else if order == &Order::DateAsc || order == &Order::DateDesc {
            paths.sort_by(|a, b| {
                let first: SystemTime;
                let second: SystemTime;

                if order == &Order::DateAsc {
                    first = a.metadata().unwrap().modified().unwrap();
                    second = b.metadata().unwrap().modified().unwrap();
                } else {
                    first = b.metadata().unwrap().modified().unwrap();
                    second = a.metadata().unwrap().modified().unwrap();
                }

                first
                    .duration_since(std::time::UNIX_EPOCH)
                    .unwrap()
                    .as_secs()
                    .cmp(
                        &second
                            .duration_since(std::time::UNIX_EPOCH)
                            .unwrap()
                            .as_secs(),
                    )
            });
        }

        for path in paths.iter() {
            println!("{}", path.display());
        }
    }

    fn enable_watcher(&mut self) {
        if self.watcher.is_some() {
            println!("Disabling watcher");
            self.watcher = None;
            return;
        }

        println!("Enabling watcher at {:?}", self.base_path);

        let watcher_events = self.watcher_events.clone();
        let mut watcher = notify::recommended_watcher(
            move |res: Result<notify::Event, notify::Error>| match res {
                Ok(event) => {
                    if let Ok(mut lock) = watcher_events.try_lock() {
                        lock.push(event.clone());
                    } else {
                        println!("Failure locking file watcher events queue");
                    }
                }
                Err(e) => println!("Error watching directory: {:?}", e),
            },
        )
        .unwrap();

        //Can be expensive on trees with a lot of files, but it's up to the user.
        let recursive_mode = if self.dir_flattened {
            RecursiveMode::Recursive
        } else {
            RecursiveMode::NonRecursive
        };

        watcher.watch(&self.base_path, recursive_mode).unwrap();

        self.watcher = Some(watcher);
    }

    fn process_file_watcher_events(&mut self) {
        //Ignore when we can't lock the mutex, it'll try next frame anyway
        if let Ok(mut events) = self.watcher_events.clone().try_lock() {
            if events.len() == 0 {
                return;
            }

            let mut should_reload = false;
            let mut selected_img_path = None;
            for event in events.iter() {
                let mut event_paths = event.paths.clone();

                event_paths.reverse();

                let first = event_paths
                    .first()
                    .unwrap();

                if !VALID_EXTENSIONS.contains(
                    &first
                        .extension()
                        .unwrap_or_default()
                        .to_str()
                        .unwrap_or_default()
                        .to_lowercase()
                        .as_str(),
                ) {
                    continue;
                }

                if event.kind.is_modify() {
                    if self.paths.contains(first) {
                        self.reload_galleries_image(Some(first.clone()));
                    } else {
                        self.paths.push(first.clone());
                        selected_img_path = Some(first.clone());
                        should_reload = true;
                    }
                } else if event.kind.is_create() {
                    self.paths.push(first.clone());
                    selected_img_path = Some(first.clone());
                    should_reload = true;
                }
            }

            if should_reload {
                self.set_images(&selected_img_path, false);
            }

            events.clear();
        }
    }

    //TODO: When flattening cancel watcher and vice versa
    fn flatten_open_dir(&mut self) {
        if self.dir_flattened {
            println!("Returning to original directory");
            self.dir_flattened = false;

            //restart watcher in non-recursive mode
            if self.watcher.is_some() {
                self.watcher = None;
                self.enable_watcher();
            }

            self.set_images_from_path(&self.base_path.clone(), &None);
        } else {
            println!("Flattening open directory");
            self.dir_flattened = true;

            //restart watcher in recursive mode
            if self.watcher.is_some() {
                self.watcher = None;
                self.enable_watcher();
            }

            self.set_images_from_path(&self.base_path.clone(), &self.gallery.get_active_img_path());
        }
    }

    //Some callbacks affect both collections so it's important
    //to deal them in the base of the app
    fn execute_callback(&mut self, callback: Callback) {
        println!("Executing callback with {:?}", callback);
        match callback {
            Callback::Pop(path) => self.callback_pop(path),
            Callback::Reload(path) => self.reload_galleries_image(path),
            Callback::ReloadAll => self.callback_reload_all(),
            Callback::NoAction => {}
        }
    }

    fn callback_pop(&mut self, path: Option<PathBuf>) {
        if let Some(path) = path {
            self.gallery.pop(&path);
            self.multi_gallery.pop(&path);
        }
    }

    fn reload_galleries_image(&mut self, path: Option<PathBuf>) {
        if let Some(path) = path {
            self.gallery.reload_at(&path);
            self.multi_gallery.reload_at(&path);
        }
    }

    fn callback_reload_all(&mut self) {
        self.set_images_from_path(&self.base_path.clone(), &self.gallery.get_active_img_path());
    }
}

impl eframe::App for App {
    fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
        ctx.request_repaint();
        let mut order_changed = false;

        self.perf_metrics.new_frame();
        self.handle_input_muters(ctx);
        self.handle_input(ctx);

        egui::TopBottomPanel::top("performance_metrics")
            .show_separator_line(false)
            .show_animated(ctx, self.perf_metrics_visible, |ui| {
                self.perf_metrics.display_metrics(ui);
                ctx.texture_ui(ui);
            });

        egui::TopBottomPanel::top("menu")
            .show_separator_line(false)
            .show_animated(ctx, self.top_menu_visible, |ui| {
                ui.menu_button("File", |ui| {
                    if ui.button("Open Folder").clicked() {
                        self.folder_picker();
                        ui.close_menu();
                    }

                    if ui.button("Open Files").clicked() {
                        self.files_picker();
                        ui.close_menu();
                    }
                });
            });

        if self.navigator_visible && navigator::ui(&mut self.navigator_search, ctx) {
            self.navigator_visible = false;
            utils::set_mute_state(ctx, false);
            self.set_images_from_path(&PathBuf::from(self.navigator_search.clone()), &None);
        }

        if self.dir_tree_visible {
            if let Some(path) = self.gallery.get_active_img_path() {
                if let Some(path) = tree::ui(path.to_str().unwrap_or(""), ctx) {
                    self.dir_tree_visible = false;
                    utils::set_mute_state(ctx, false);
                    self.set_images_from_path(&path, &None);
                }
            }
        }

        if self.multi_gallery_visible {
            self.multi_gallery.ui(ctx, &mut self.gallery_selected_index);

            if let Some(img_name) = self.multi_gallery.selected_image_name() {
                self.gallery.select_by_name(img_name);
                self.multi_gallery_visible = false;
            }

            if let Some(callback) = self.multi_gallery.take_callback() {
                self.execute_callback(callback);
            }
        } else {
            self.gallery.ui(
                ctx,
                &mut self.order,
                &mut order_changed,
                self.dir_flattened,
                self.watcher.is_some(),
            );

            if let Some(callback) = self.gallery.take_callback() {
                self.execute_callback(callback);
            }
        }

        if order_changed {
            self.sort_and_set();
        }

        self.process_file_watcher_events();

        self.perf_metrics.end_frame();
    }
}