Show notification about creating a default config
Browse files at this commit51243a0a505a533057e7326fbbae882420f0d363Parent: 0ebcc3e2 changed files +51 -7
- src/config_error_notification.rs +46 -6
- src/main.rs +5 -1
src/config_error_notification.rsmodified+46 -6
| @@ -1,5 +1,6 @@ | |||
| 1 | use std::cell::RefCell; | 1 | use std::cell::RefCell; |
| 2 | use std::collections::HashMap; | 2 | use std::collections::HashMap; |
| 3 | +use std::path::{Path, PathBuf}; | ||
| 3 | use std::time::Duration; | 4 | use std::time::Duration; |
| 4 | 5 | ||
| 5 | use pangocairo::cairo::{self, ImageSurface}; | 6 | use pangocairo::cairo::{self, ImageSurface}; |
| @@ -26,6 +27,10 @@ const BORDER: i32 = 4; | |||
| 26 | pub struct ConfigErrorNotification { | 27 | pub struct ConfigErrorNotification { |
| 27 | state: State, | 28 | state: State, |
| 28 | buffers: RefCell<HashMap<i32, Option<MemoryRenderBuffer>>>, | 29 | buffers: RefCell<HashMap<i32, Option<MemoryRenderBuffer>>>, |
| 30 | + | ||
| 31 | + // If set, this is a "Created config at {path}" notification. If unset, this is a config error | ||
| 32 | + // notification. | ||
| 33 | + created_path: Option<PathBuf>, | ||
| 29 | } | 34 | } |
| 30 | 35 | ||
| 31 | enum State { | 36 | enum State { |
| @@ -43,10 +48,25 @@ impl ConfigErrorNotification { | |||
| 43 | Self { | 48 | Self { |
| 44 | state: State::Hidden, | 49 | state: State::Hidden, |
| 45 | buffers: RefCell::new(HashMap::new()), | 50 | buffers: RefCell::new(HashMap::new()), |
| 51 | + created_path: None, | ||
| 46 | } | 52 | } |
| 47 | } | 53 | } |
| 48 | 54 | ||
| 55 | + pub fn show_created(&mut self, created_path: Option<PathBuf>) { | ||
| 56 | + if self.created_path != created_path { | ||
| 57 | + self.created_path = created_path; | ||
| 58 | + self.buffers.borrow_mut().clear(); | ||
| 59 | + } | ||
| 60 | + | ||
| 61 | + self.state = State::Showing(Animation::new(0., 1., Duration::from_millis(250))); | ||
| 62 | + } | ||
| 63 | + | ||
| 49 | pub fn show(&mut self) { | 64 | pub fn show(&mut self) { |
| 65 | + if self.created_path.is_some() { | ||
| 66 | + self.created_path = None; | ||
| 67 | + self.buffers.borrow_mut().clear(); | ||
| 68 | + } | ||
| 69 | + | ||
| 50 | // Show from scratch even if already showing to bring attention. | 70 | // Show from scratch even if already showing to bring attention. |
| 51 | self.state = State::Showing(Animation::new(0., 1., Duration::from_millis(250))); | 71 | self.state = State::Showing(Animation::new(0., 1., Duration::from_millis(250))); |
| 52 | } | 72 | } |
| @@ -65,7 +85,15 @@ impl ConfigErrorNotification { | |||
| 65 | State::Showing(anim) => { | 85 | State::Showing(anim) => { |
| 66 | anim.set_current_time(target_presentation_time); | 86 | anim.set_current_time(target_presentation_time); |
| 67 | if anim.is_done() { | 87 | if anim.is_done() { |
| 68 | - self.state = State::Shown(target_presentation_time + Duration::from_secs(4)); | 88 | + let duration = if self.created_path.is_some() { |
| 89 | + // Make this quite a bit longer because it comes with a monitor modeset | ||
| 90 | + // (can take a while) and an important hotkeys popup diverting the | ||
| 91 | + // attention. | ||
| 92 | + Duration::from_secs(8) | ||
| 93 | + } else { | ||
| 94 | + Duration::from_secs(4) | ||
| 95 | + }; | ||
| 96 | + self.state = State::Shown(target_presentation_time + duration); | ||
| 69 | } | 97 | } |
| 70 | } | 98 | } |
| 71 | State::Shown(deadline) => { | 99 | State::Shown(deadline) => { |
| @@ -96,11 +124,12 @@ impl ConfigErrorNotification { | |||
| 96 | } | 124 | } |
| 97 | 125 | ||
| 98 | let scale = output.current_scale().integer_scale(); | 126 | let scale = output.current_scale().integer_scale(); |
| 127 | + let path = self.created_path.as_deref(); | ||
| 99 | 128 | ||
| 100 | let mut buffers = self.buffers.borrow_mut(); | 129 | let mut buffers = self.buffers.borrow_mut(); |
| 101 | let buffer = buffers | 130 | let buffer = buffers |
| 102 | .entry(scale) | 131 | .entry(scale) |
| 103 | - .or_insert_with_key(move |&scale| render(scale).ok()); | 132 | + .or_insert_with_key(move |&scale| render(scale, path).ok()); |
| 104 | let buffer = buffer.as_ref()?; | 133 | let buffer = buffer.as_ref()?; |
| 105 | 134 | ||
| 106 | let elem = MemoryRenderBufferRenderElement::from_buffer( | 135 | let elem = MemoryRenderBufferRenderElement::from_buffer( |
| @@ -138,11 +167,22 @@ impl ConfigErrorNotification { | |||
| 138 | } | 167 | } |
| 139 | } | 168 | } |
| 140 | 169 | ||
| 141 | -fn render(scale: i32) -> anyhow::Result<MemoryRenderBuffer> { | 170 | +fn render(scale: i32, created_path: Option<&Path>) -> anyhow::Result<MemoryRenderBuffer> { |
| 142 | let _span = tracy_client::span!("config_error_notification::render"); | 171 | let _span = tracy_client::span!("config_error_notification::render"); |
| 143 | 172 | ||
| 144 | let padding = PADDING * scale; | 173 | let padding = PADDING * scale; |
| 145 | 174 | ||
| 175 | + let mut text = String::from(TEXT); | ||
| 176 | + let mut border_color = (1., 0.3, 0.3); | ||
| 177 | + if let Some(path) = created_path { | ||
| 178 | + text = format!( | ||
| 179 | + "Created a default config file at \ | ||
| 180 | + <span face='monospace' bgcolor='#000000'>{:?}</span>", | ||
| 181 | + path | ||
| 182 | + ); | ||
| 183 | + border_color = (0.5, 1., 0.5); | ||
| 184 | + }; | ||
| 185 | + | ||
| 146 | let mut font = FontDescription::from_string(FONT); | 186 | let mut font = FontDescription::from_string(FONT); |
| 147 | font.set_absolute_size((font.size() * scale).into()); | 187 | font.set_absolute_size((font.size() * scale).into()); |
| 148 | 188 | ||
| @@ -150,7 +190,7 @@ fn render(scale: i32) -> anyhow::Result<MemoryRenderBuffer> { | |||
| 150 | let cr = cairo::Context::new(&surface)?; | 190 | let cr = cairo::Context::new(&surface)?; |
| 151 | let layout = pangocairo::create_layout(&cr); | 191 | let layout = pangocairo::create_layout(&cr); |
| 152 | layout.set_font_description(Some(&font)); | 192 | layout.set_font_description(Some(&font)); |
| 153 | - layout.set_markup(TEXT); | 193 | + layout.set_markup(&text); |
| 154 | 194 | ||
| 155 | let (mut width, mut height) = layout.pixel_size(); | 195 | let (mut width, mut height) = layout.pixel_size(); |
| 156 | width += padding * 2; | 196 | width += padding * 2; |
| @@ -168,7 +208,7 @@ fn render(scale: i32) -> anyhow::Result<MemoryRenderBuffer> { | |||
| 168 | cr.move_to(padding.into(), padding.into()); | 208 | cr.move_to(padding.into(), padding.into()); |
| 169 | let layout = pangocairo::create_layout(&cr); | 209 | let layout = pangocairo::create_layout(&cr); |
| 170 | layout.set_font_description(Some(&font)); | 210 | layout.set_font_description(Some(&font)); |
| 171 | - layout.set_markup(TEXT); | 211 | + layout.set_markup(&text); |
| 172 | 212 | ||
| 173 | cr.set_source_rgb(1., 1., 1.); | 213 | cr.set_source_rgb(1., 1., 1.); |
| 174 | pangocairo::show_layout(&cr, &layout); | 214 | pangocairo::show_layout(&cr, &layout); |
| @@ -178,7 +218,7 @@ fn render(scale: i32) -> anyhow::Result<MemoryRenderBuffer> { | |||
| 178 | cr.line_to(width.into(), height.into()); | 218 | cr.line_to(width.into(), height.into()); |
| 179 | cr.line_to(0., height.into()); | 219 | cr.line_to(0., height.into()); |
| 180 | cr.line_to(0., 0.); | 220 | cr.line_to(0., 0.); |
| 181 | - cr.set_source_rgb(1., 0.3, 0.3); | 221 | + cr.set_source_rgb(border_color.0, border_color.1, border_color.2); |
| 182 | cr.set_line_width((BORDER * scale).into()); | 222 | cr.set_line_width((BORDER * scale).into()); |
| 183 | cr.stroke()?; | 223 | cr.stroke()?; |
| 184 | drop(cr); | 224 | drop(cr); |
src/main.rsmodified+5 -1
| @@ -156,6 +156,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> { | |||
| 156 | info!("starting version {}", &version()); | 156 | info!("starting version {}", &version()); |
| 157 | 157 | ||
| 158 | // Load the config. | 158 | // Load the config. |
| 159 | + let mut config_created = false; | ||
| 159 | let path = cli.config.or_else(|| { | 160 | let path = cli.config.or_else(|| { |
| 160 | let default_path = default_config_path()?; | 161 | let default_path = default_config_path()?; |
| 161 | let default_parent = default_path.parent().unwrap(); | 162 | let default_parent = default_path.parent().unwrap(); |
| @@ -179,6 +180,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> { | |||
| 179 | let default = include_bytes!("../resources/default-config.kdl"); | 180 | let default = include_bytes!("../resources/default-config.kdl"); |
| 180 | match new_file.write_all(default) { | 181 | match new_file.write_all(default) { |
| 181 | Ok(()) => { | 182 | Ok(()) => { |
| 183 | + config_created = true; | ||
| 182 | info!("wrote default config to {:?}", &default_path); | 184 | info!("wrote default config to {:?}", &default_path); |
| 183 | } | 185 | } |
| 184 | Err(err) => { | 186 | Err(err) => { |
| @@ -255,7 +257,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> { | |||
| 255 | }; | 257 | }; |
| 256 | 258 | ||
| 257 | // Set up config file watcher. | 259 | // Set up config file watcher. |
| 258 | - let _watcher = if let Some(path) = path { | 260 | + let _watcher = if let Some(path) = path.clone() { |
| 259 | let (tx, rx) = calloop::channel::sync_channel(1); | 261 | let (tx, rx) = calloop::channel::sync_channel(1); |
| 260 | let watcher = Watcher::new(path.clone(), tx); | 262 | let watcher = Watcher::new(path.clone(), tx); |
| 261 | event_loop | 263 | event_loop |
| @@ -280,6 +282,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> { | |||
| 280 | // Show the config error notification right away if needed. | 282 | // Show the config error notification right away if needed. |
| 281 | if config_errored { | 283 | if config_errored { |
| 282 | state.niri.config_error_notification.show(); | 284 | state.niri.config_error_notification.show(); |
| 285 | + } else if config_created { | ||
| 286 | + state.niri.config_error_notification.show_created(path); | ||
| 283 | } | 287 | } |
| 284 | 288 | ||
| 285 | // Run the compositor. | 289 | // Run the compositor. |