|
1 | | -use std::sync::Arc; |
| 1 | +use std::{path::Path, sync::Arc}; |
2 | 2 |
|
3 | | -use iced::theme::Custom; |
| 3 | +use iced::{theme::Custom, widget::image::Handle}; |
4 | 4 | use serde::{Deserialize, Serialize}; |
5 | 5 |
|
| 6 | +use crate::{app::App, utils::handle_from_icns}; |
| 7 | + |
6 | 8 | #[derive(Debug, Clone, Deserialize, Serialize)] |
| 9 | +#[serde(default)] |
7 | 10 | pub struct Config { |
8 | | - pub toggle_mod: Option<String>, |
9 | | - pub toggle_key: Option<String>, |
10 | | - pub buffer_rules: Option<Buffer>, |
11 | | - pub theme: Option<Theme>, |
12 | | - pub placeholder: Option<String>, |
| 11 | + pub toggle_mod: String, |
| 12 | + pub toggle_key: String, |
| 13 | + pub buffer_rules: Buffer, |
| 14 | + pub theme: Theme, |
| 15 | + pub placeholder: String, |
| 16 | + pub shells: Vec<Shelly>, |
13 | 17 | } |
14 | 18 |
|
15 | 19 | impl Default for Config { |
16 | 20 | fn default() -> Self { |
17 | 21 | Self { |
18 | | - toggle_mod: Some("ALT".to_string()), |
19 | | - toggle_key: Some("Space".to_string()), |
20 | | - buffer_rules: Some(Buffer::default()), |
21 | | - theme: Some(Theme::default()), |
22 | | - placeholder: Some(String::from("Time to be productive!")), |
| 22 | + toggle_mod: "ALT".to_string(), |
| 23 | + toggle_key: "Space".to_string(), |
| 24 | + buffer_rules: Buffer::default(), |
| 25 | + theme: Theme::default(), |
| 26 | + placeholder: String::from("Time to be productive!"), |
| 27 | + shells: vec![], |
23 | 28 | } |
24 | 29 | } |
25 | 30 | } |
26 | 31 |
|
27 | 32 | #[derive(Debug, Clone, Deserialize, Serialize)] |
| 33 | +#[serde(default)] |
28 | 34 | pub struct Theme { |
29 | | - pub text_color: Option<(f32, f32, f32)>, |
30 | | - pub background_color: Option<(f32, f32, f32)>, |
31 | | - pub background_opacity: Option<f32>, |
32 | | - pub blur: Option<bool>, |
33 | | - pub show_icons: Option<bool>, |
34 | | - pub show_scroll_bar: Option<bool>, |
| 35 | + pub text_color: (f32, f32, f32), |
| 36 | + pub background_color: (f32, f32, f32), |
| 37 | + pub background_opacity: f32, |
| 38 | + pub blur: bool, |
| 39 | + pub show_icons: bool, |
| 40 | + pub show_scroll_bar: bool, |
35 | 41 | } |
36 | 42 |
|
37 | 43 | impl Default for Theme { |
38 | 44 | fn default() -> Self { |
39 | 45 | Self { |
40 | | - text_color: Some((0.95, 0.95, 0.96)), |
41 | | - background_color: Some((0.11, 0.11, 0.13)), |
42 | | - background_opacity: Some(1.), |
43 | | - blur: Some(false), |
44 | | - show_icons: Some(true), |
45 | | - show_scroll_bar: Some(true), |
| 46 | + text_color: (0.95, 0.95, 0.96), |
| 47 | + background_color: (0.11, 0.11, 0.13), |
| 48 | + background_opacity: 1., |
| 49 | + blur: false, |
| 50 | + show_icons: true, |
| 51 | + show_scroll_bar: true, |
46 | 52 | } |
47 | 53 | } |
48 | 54 | } |
49 | 55 |
|
50 | 56 | impl Theme { |
51 | 57 | pub fn to_iced_theme(&self) -> iced::Theme { |
52 | | - let default = Self::default(); |
53 | | - let text_color = self.text_color.unwrap_or(default.text_color.unwrap()); |
54 | | - let bg_color = self |
55 | | - .background_color |
56 | | - .unwrap_or(default.background_color.unwrap()); |
| 58 | + let text_color = self.text_color; |
| 59 | + let bg_color = self.background_color; |
57 | 60 | let palette = iced::theme::Palette { |
58 | 61 | background: iced::Color { |
59 | 62 | r: bg_color.0, |
60 | 63 | g: bg_color.1, |
61 | 64 | b: bg_color.2, |
62 | | - a: self.background_opacity.unwrap_or(1.), |
| 65 | + a: self.background_opacity, |
63 | 66 | }, |
64 | 67 | text: iced::Color { |
65 | 68 | r: text_color.0, |
@@ -97,16 +100,51 @@ impl Theme { |
97 | 100 | } |
98 | 101 |
|
99 | 102 | #[derive(Debug, Clone, Deserialize, Serialize)] |
| 103 | +#[serde(default)] |
100 | 104 | pub struct Buffer { |
101 | | - pub clear_on_hide: Option<bool>, |
102 | | - pub clear_on_enter: Option<bool>, |
| 105 | + pub clear_on_hide: bool, |
| 106 | + pub clear_on_enter: bool, |
103 | 107 | } |
104 | 108 |
|
105 | 109 | impl Default for Buffer { |
106 | 110 | fn default() -> Self { |
107 | 111 | Buffer { |
108 | | - clear_on_hide: Some(true), |
109 | | - clear_on_enter: Some(true), |
| 112 | + clear_on_hide: true, |
| 113 | + clear_on_enter: true, |
| 114 | + } |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +/// Command is the command it will run when the button is clicked |
| 119 | +/// Icon_path is the path to an icon, but this is optional |
| 120 | +/// Alias is the text that is used to call this command / search for it |
| 121 | +#[derive(Debug, Clone, Deserialize, Serialize)] |
| 122 | +pub struct Shelly { |
| 123 | + command: Vec<String>, |
| 124 | + icon_path: Option<String>, |
| 125 | + alias: String, |
| 126 | + alias_lc: String, |
| 127 | +} |
| 128 | + |
| 129 | +impl Shelly { |
| 130 | + pub fn to_app(&self) -> App { |
| 131 | + let self_clone = self.clone(); |
| 132 | + let icon = self_clone |
| 133 | + .icon_path |
| 134 | + .map(|x| { |
| 135 | + let x = x.replace("~", &std::env::var("HOME").unwrap()); |
| 136 | + if x.ends_with(".icns") { |
| 137 | + handle_from_icns(Path::new(&x)) |
| 138 | + } else { |
| 139 | + Some(Handle::from_path(Path::new(&x))) |
| 140 | + } |
| 141 | + }) |
| 142 | + .flatten(); |
| 143 | + App { |
| 144 | + open_command: self_clone.command, |
| 145 | + icons: icon, |
| 146 | + name: self_clone.alias, |
| 147 | + name_lc: self_clone.alias_lc, |
110 | 148 | } |
111 | 149 | } |
112 | 150 | } |
0 commit comments