options.rs - icy_draw - [fork] icy_draw is the successor to mystic draw.
HTML git clone https://git.drkhsh.at/icy_draw.git
DIR Log
DIR Files
DIR Refs
DIR README
---
options.rs (2498B)
---
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 }