Skip to content
This repository was archived by the owner on Apr 10, 2024. It is now read-only.

Commit 351f98a

Browse files
committed
Added .config file & autoscroll command line option.
1 parent 5bff6b3 commit 351f98a

File tree

6 files changed

+136
-37
lines changed

6 files changed

+136
-37
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "icy_view"
3-
version = "0.6.0"
3+
version = "0.6.1"
44
edition = "2021"
55
description = "A fast ansi art viewer."
66
authors = ["Mike Krüger <mkrueger@posteo.de>"]

src/main.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::path::PathBuf;
44

55
use clap::Parser;
66
use semver::Version;
7-
use view_library::MainWindow;
7+
use view_library::{options::Options, MainWindow};
88

99
lazy_static::lazy_static! {
1010
static ref VERSION: Version = Version::parse( env!("CARGO_PKG_VERSION")).unwrap();
@@ -23,14 +23,22 @@ lazy_static::lazy_static! {
2323
}
2424

2525
#[derive(Parser, Debug)]
26+
#[command(version, about, long_about = None)]
2627
pub struct Cli {
2728
path: Option<PathBuf>,
29+
30+
#[clap(long, default_value_t = false, help = "Enable auto-scrolling")]
31+
auto: bool,
2832
}
2933

3034
fn main() {
3135
let args = Cli::parse();
36+
let mut options = Options::load_options();
37+
if args.auto {
38+
options.auto_scroll_enabled = true;
39+
}
3240

33-
let options = eframe::NativeOptions {
41+
let native_options = eframe::NativeOptions {
3442
//initial_window_size: Some(egui::vec2(1284. + 8., 839.)),
3543
multisampling: 0,
3644
renderer: eframe::Renderer::Glow,
@@ -40,16 +48,16 @@ fn main() {
4048
// options.viewport.icon = Some(IconData::from( &include_bytes!("../build/linux/256x256.png")[..]).unwrap());
4149
eframe::run_native(
4250
&DEFAULT_TITLE,
43-
options,
51+
native_options,
4452
Box::new(|cc| {
4553
let gl = cc.gl.as_ref().expect("You need to run eframe with the glow backend");
4654
egui_extras::install_image_loaders(&cc.egui_ctx);
4755

48-
let mut fd = MainWindow::new(gl, args.path);
56+
let mut fd = MainWindow::new(gl, args.path, options);
57+
fd.store_options = true;
4958
if *VERSION < *LATEST_VERSION {
5059
fd.file_view.upgrade_version = Some(LATEST_VERSION.to_string());
5160
}
52-
5361
let cmd = fd.file_view.refresh();
5462
fd.handle_command(cmd);
5563
Box::new(fd)

view_library/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ egui-modal = "0.3.3"
2323
egui-notify = "0.13.0"
2424
thiserror = "1.0"
2525
anyhow = "1.0.75"
26+
serde = "1.0.197"
27+
toml = "0.8.10"
2628

2729
image = { version = "0.24", features = ["jpeg", "png", "gif", "bmp"] }
2830
icy_engine = { git = "https://github.com/mkrueger/icy_engine.git" }

view_library/src/ui/file_view.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ use std::{
1414
path::{Path, PathBuf},
1515
};
1616

17+
use super::options::{Options, ScrollSpeed};
18+
1719
pub enum Message {
1820
Select(usize, bool),
1921
Open(usize),
@@ -103,14 +105,13 @@ pub struct FileView {
103105
pub files: Vec<FileEntry>,
104106
pub upgrade_version: Option<String>,
105107

106-
pub auto_scroll_enabled: bool,
107-
pub scroll_speed: usize,
108+
pub options: super::options::Options,
108109
pub filter: String,
109110
pre_select_file: Option<String>,
110111
}
111112

112113
impl FileView {
113-
pub fn new(initial_path: Option<PathBuf>) -> Self {
114+
pub fn new(initial_path: Option<PathBuf>, options: Options) -> Self {
114115
let mut path = if let Some(path) = initial_path {
115116
path
116117
} else if let Some(user_dirs) = UserDirs::new() {
@@ -138,8 +139,7 @@ impl FileView {
138139
scroll_pos: None,
139140
files: Vec::new(),
140141
filter: String::new(),
141-
auto_scroll_enabled: true,
142-
scroll_speed: 1,
142+
options,
143143
upgrade_version: None,
144144
}
145145
}
@@ -228,18 +228,17 @@ impl FileView {
228228
ui.close_menu();
229229
}
230230
ui.separator();
231-
let mut b = self.auto_scroll_enabled;
231+
let mut b = self.options.auto_scroll_enabled;
232232
if ui.checkbox(&mut b, fl!(crate::LANGUAGE_LOADER, "menu-item-auto-scroll")).clicked() {
233233
command = Some(Message::ToggleAutoScroll);
234234
ui.close_menu();
235235
}
236-
let title = match self.scroll_speed {
237-
2 => fl!(crate::LANGUAGE_LOADER, "menu-item-scroll-speed-slow"),
238-
0 => {
236+
let title = match self.options.scroll_speed {
237+
ScrollSpeed::Slow => fl!(crate::LANGUAGE_LOADER, "menu-item-scroll-speed-slow"),
238+
ScrollSpeed::Medium => {
239239
fl!(crate::LANGUAGE_LOADER, "menu-item-scroll-speed-medium")
240240
}
241-
1 => fl!(crate::LANGUAGE_LOADER, "menu-item-scroll-speed-fast"),
242-
_ => panic!(),
241+
ScrollSpeed::Fast => fl!(crate::LANGUAGE_LOADER, "menu-item-scroll-speed-fast"),
243242
};
244243

245244
let r = ui.selectable_label(false, title);

view_library/src/ui/mod.rs

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,14 @@ use std::{
1515
time::Duration,
1616
};
1717

18-
use self::file_view::{FileEntry, FileView, Message};
18+
use self::{
19+
file_view::{FileEntry, FileView, Message},
20+
options::{Options, ScrollSpeed},
21+
};
1922

2023
mod file_view;
2124
mod help_dialog;
25+
pub mod options;
2226
mod sauce_dialog;
2327

2428
pub struct MainWindow<'a> {
@@ -45,11 +49,10 @@ pub struct MainWindow<'a> {
4549
toasts: egui_notify::Toasts,
4650
is_closed: bool,
4751
pub opened_file: Option<FileEntry>,
48-
52+
pub store_options: bool,
4953
// animations
5054
animation: Option<Arc<Mutex<Animator>>>,
5155
}
52-
const SCROLL_SPEED: [f32; 3] = [80.0, 160.0, 320.0];
5356
const EXT_WHITE_LIST: [&str; 5] = ["seq", "diz", "nfo", "ice", "bbs"];
5457

5558
const EXT_BLACK_LIST: [&str; 8] = ["zip", "rar", "gz", "tar", "7z", "pdf", "exe", "com"];
@@ -73,7 +76,7 @@ impl<'a> App for MainWindow<'a> {
7376
ui.set_enabled(self.sauce_dialog.is_none() && self.help_dialog.is_none());
7477
self.paint_main_area(ui)
7578
});
76-
self.in_scroll &= self.file_view.auto_scroll_enabled;
79+
self.in_scroll &= self.file_view.options.auto_scroll_enabled;
7780
if self.in_scroll {
7881
// ctx.request_repaint_after(Duration::from_millis(10));
7982
ctx.request_repaint();
@@ -121,10 +124,17 @@ impl<'a> App for MainWindow<'a> {
121124
}
122125
}
123126
}
127+
128+
fn on_exit(&mut self, _gl: Option<&glow::Context>) {
129+
println!("store options: {}", self.store_options);
130+
if self.store_options {
131+
self.file_view.options.store_options();
132+
}
133+
}
124134
}
125135

126136
impl<'a> MainWindow<'a> {
127-
pub fn new(gl: &Arc<glow::Context>, mut initial_path: Option<PathBuf>) -> Self {
137+
pub fn new(gl: &Arc<glow::Context>, mut initial_path: Option<PathBuf>, options: Options) -> Self {
128138
let mut view = BufferView::new(gl);
129139
view.interactive = false;
130140

@@ -137,9 +147,10 @@ impl<'a> MainWindow<'a> {
137147
}
138148
}
139149
}
150+
140151
Self {
141152
buffer_view: Arc::new(eframe::epaint::mutex::Mutex::new(view)),
142-
file_view: FileView::new(initial_path),
153+
file_view: FileView::new(initial_path, options),
143154
in_scroll: false,
144155
retained_image: None,
145156
texture_handle: None,
@@ -157,6 +168,7 @@ impl<'a> MainWindow<'a> {
157168
opened_file: None,
158169
is_closed: false,
159170
animation: None,
171+
store_options: false,
160172
}
161173
}
162174

@@ -193,7 +205,7 @@ impl<'a> MainWindow<'a> {
193205
.inner_margin(egui::style::Margin::same(0.0))
194206
.fill(Color32::BLACK);
195207
egui::CentralPanel::default().frame(frame_no_margins).show(ctx, |ui| self.paint_main_area(ui));
196-
self.in_scroll &= self.file_view.auto_scroll_enabled;
208+
self.in_scroll &= self.file_view.options.auto_scroll_enabled;
197209
if self.in_scroll {
198210
// ctx.request_repaint_after(Duration::from_millis(10));
199211
ctx.request_repaint();
@@ -389,7 +401,7 @@ impl<'a> MainWindow<'a> {
389401

390402
let dt = ui.input(|i| i.unstable_dt);
391403
let sp = if self.in_scroll {
392-
(self.cur_scroll_pos + SCROLL_SPEED[self.file_view.scroll_speed] * dt).round()
404+
(self.cur_scroll_pos + self.file_view.options.scroll_speed.get_speed() * dt).round()
393405
} else {
394406
self.cur_scroll_pos.round()
395407
};
@@ -514,9 +526,7 @@ impl<'a> MainWindow<'a> {
514526
result.buffer_type = icy_engine::BufferType::Unicode;
515527
}
516528
match parse_with_parser(&mut result, &mut rip_parser, &text, true) {
517-
Ok(_) => {
518-
rip_parser
519-
}
529+
Ok(_) => rip_parser,
520530
Err(err) => {
521531
log::error!("Error while parsing rip file: {err}");
522532
rip_parser
@@ -607,10 +617,10 @@ impl<'a> MainWindow<'a> {
607617
}
608618
}
609619
Message::ToggleAutoScroll => {
610-
self.file_view.auto_scroll_enabled = !self.in_scroll;
611-
self.in_scroll = self.file_view.auto_scroll_enabled;
620+
self.file_view.options.auto_scroll_enabled = !self.file_view.options.auto_scroll_enabled;
621+
self.in_scroll = self.file_view.options.auto_scroll_enabled;
612622

613-
if self.file_view.auto_scroll_enabled {
623+
if self.file_view.options.auto_scroll_enabled {
614624
self.toasts
615625
.info(fl!(crate::LANGUAGE_LOADER, "toast-auto-scroll-on"))
616626
.set_duration(Some(Duration::from_secs(3)));
@@ -631,25 +641,24 @@ impl<'a> MainWindow<'a> {
631641
self.help_dialog = Some(help_dialog::HelpDialog::new());
632642
}
633643
Message::ChangeScrollSpeed => {
634-
self.file_view.scroll_speed = (self.file_view.scroll_speed + 1) % SCROLL_SPEED.len();
644+
self.file_view.options.scroll_speed = self.file_view.options.scroll_speed.next();
635645

636-
match self.file_view.scroll_speed {
637-
0 => {
646+
match self.file_view.options.scroll_speed {
647+
ScrollSpeed::Slow => {
638648
self.toasts
639649
.info(fl!(crate::LANGUAGE_LOADER, "toast-scroll-slow"))
640650
.set_duration(Some(Duration::from_secs(3)));
641651
}
642-
1 => {
652+
ScrollSpeed::Medium => {
643653
self.toasts
644654
.info(fl!(crate::LANGUAGE_LOADER, "toast-scroll-medium"))
645655
.set_duration(Some(Duration::from_secs(3)));
646656
}
647-
2 => {
657+
ScrollSpeed::Fast => {
648658
self.toasts
649659
.info(fl!(crate::LANGUAGE_LOADER, "toast-scroll-fast"))
650660
.set_duration(Some(Duration::from_secs(3)));
651661
}
652-
_ => {}
653662
}
654663
}
655664
}

view_library/src/ui/options.rs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
use serde::{Deserialize, Serialize};
2+
use std::fs;
3+
4+
const SCROLL_SPEED: [f32; 3] = [80.0, 160.0, 320.0];
5+
6+
#[derive(Serialize, Deserialize, Debug)]
7+
pub enum ScrollSpeed {
8+
Slow,
9+
Medium,
10+
Fast,
11+
}
12+
13+
impl ScrollSpeed {
14+
pub fn get_speed(&self) -> f32 {
15+
match self {
16+
ScrollSpeed::Slow => SCROLL_SPEED[0],
17+
ScrollSpeed::Medium => SCROLL_SPEED[1],
18+
ScrollSpeed::Fast => SCROLL_SPEED[2],
19+
}
20+
}
21+
22+
pub(crate) fn next(&self) -> ScrollSpeed {
23+
match self {
24+
ScrollSpeed::Slow => ScrollSpeed::Medium,
25+
ScrollSpeed::Medium => ScrollSpeed::Fast,
26+
ScrollSpeed::Fast => ScrollSpeed::Slow,
27+
}
28+
}
29+
}
30+
31+
#[derive(Serialize, Deserialize, Debug)]
32+
pub struct Options {
33+
pub auto_scroll_enabled: bool,
34+
pub scroll_speed: ScrollSpeed,
35+
}
36+
37+
impl Default for Options {
38+
fn default() -> Self {
39+
Self {
40+
auto_scroll_enabled: true,
41+
scroll_speed: ScrollSpeed::Medium,
42+
}
43+
}
44+
}
45+
46+
impl Options {
47+
pub fn load_options() -> Self {
48+
if let Some(proj_dirs) = directories::ProjectDirs::from("com", "GitHub", "icy_view") {
49+
if !proj_dirs.config_dir().exists() && fs::create_dir_all(proj_dirs.config_dir()).is_err() {
50+
log::error!("Can't create configuration directory {:?}", proj_dirs.config_dir());
51+
return Self::default();
52+
}
53+
let options_file = proj_dirs.config_dir().join("options.toml");
54+
if options_file.exists() {
55+
match fs::read_to_string(options_file) {
56+
Ok(txt) => {
57+
if let Ok(result) = toml::from_str(&txt) {
58+
return result;
59+
}
60+
}
61+
Err(err) => log::error!("Error reading options file: {}", err),
62+
}
63+
}
64+
}
65+
Self::default()
66+
}
67+
68+
pub fn store_options(&self) {
69+
if let Some(proj_dirs) = directories::ProjectDirs::from("com", "GitHub", "icy_view") {
70+
let file_name = proj_dirs.config_dir().join("options.toml");
71+
match toml::to_string(self) {
72+
Ok(text) => {
73+
if let Err(err) = fs::write(file_name, text) {
74+
log::error!("Error writing options file: {}", err);
75+
}
76+
}
77+
Err(err) => log::error!("Error writing options file: {}", err),
78+
}
79+
}
80+
}
81+
}

0 commit comments

Comments
 (0)