Add ConfigLoaded event to IPC, option to disable built-in notification (#1829)
Browse files at this commit9 changed files +107 -5
- niri-config/src/lib.rs +11 -0
- niri-ipc/src/lib.rs +10 -0
- niri-ipc/src/state.rs +30 -0
- src/ipc/client.rs +8 -0
- src/ipc/server.rs +11 -0
- src/main.rs +1 -0
- src/ui/config_error_notification.rs +5 -0
- src/utils/watcher.rs +12 -5
- wiki/Configuration:-Miscellaneous.md +19 -0
niri-config/src/lib.rsmodified+11 -0
| @@ -60,6 +60,8 @@ pub struct Config { | |||
| 60 | #[knuffel(child, default)] | 60 | #[knuffel(child, default)] |
| 61 | pub hotkey_overlay: HotkeyOverlay, | 61 | pub hotkey_overlay: HotkeyOverlay, |
| 62 | #[knuffel(child, default)] | 62 | #[knuffel(child, default)] |
| 63 | + pub config_notification: ConfigNotification, | ||
| 64 | + #[knuffel(child, default)] | ||
| 63 | pub animations: Animations, | 65 | pub animations: Animations, |
| 64 | #[knuffel(child, default)] | 66 | #[knuffel(child, default)] |
| 65 | pub gestures: Gestures, | 67 | pub gestures: Gestures, |
| @@ -1047,6 +1049,12 @@ pub struct HotkeyOverlay { | |||
| 1047 | pub hide_not_bound: bool, | 1049 | pub hide_not_bound: bool, |
| 1048 | } | 1050 | } |
| 1049 | 1051 | ||
| 1052 | +#[derive(knuffel::Decode, Debug, Default, Clone, Copy, PartialEq, Eq)] | ||
| 1053 | +pub struct ConfigNotification { | ||
| 1054 | + #[knuffel(child)] | ||
| 1055 | + pub disable_failed: bool, | ||
| 1056 | +} | ||
| 1057 | + | ||
| 1050 | #[derive(knuffel::Decode, Debug, Default, Clone, Copy, PartialEq, Eq)] | 1058 | #[derive(knuffel::Decode, Debug, Default, Clone, Copy, PartialEq, Eq)] |
| 1051 | pub struct Clipboard { | 1059 | pub struct Clipboard { |
| 1052 | #[knuffel(child)] | 1060 | #[knuffel(child)] |
| @@ -4769,6 +4777,9 @@ mod tests { | |||
| 4769 | skip_at_startup: true, | 4777 | skip_at_startup: true, |
| 4770 | hide_not_bound: false, | 4778 | hide_not_bound: false, |
| 4771 | }, | 4779 | }, |
| 4780 | + config_notification: ConfigNotification { | ||
| 4781 | + disable_failed: false, | ||
| 4782 | + }, | ||
| 4772 | animations: Animations { | 4783 | animations: Animations { |
| 4773 | off: false, | 4784 | off: false, |
| 4774 | slowdown: FloatOrInt( | 4785 | slowdown: FloatOrInt( |
niri-ipc/src/lib.rsmodified+10 -0
| @@ -1399,6 +1399,16 @@ pub enum Event { | |||
| 1399 | /// The new state of the overview. | 1399 | /// The new state of the overview. |
| 1400 | is_open: bool, | 1400 | is_open: bool, |
| 1401 | }, | 1401 | }, |
| 1402 | + /// The configuration was reloaded. | ||
| 1403 | + /// | ||
| 1404 | + /// You will always receive this event when connecting to the event stream, indicating the last | ||
| 1405 | + /// config load attempt. | ||
| 1406 | + ConfigLoaded { | ||
| 1407 | + /// Whether the loading failed. | ||
| 1408 | + /// | ||
| 1409 | + /// For example, the config file couldn't be parsed. | ||
| 1410 | + failed: bool, | ||
| 1411 | + }, | ||
| 1402 | } | 1412 | } |
| 1403 | 1413 | ||
| 1404 | impl FromStr for WorkspaceReferenceArg { | 1414 | impl FromStr for WorkspaceReferenceArg { |
niri-ipc/src/state.rsmodified+30 -0
| @@ -43,6 +43,9 @@ pub struct EventStreamState { | |||
| 43 | 43 | ||
| 44 | /// State of the overview. | 44 | /// State of the overview. |
| 45 | pub overview: OverviewState, | 45 | pub overview: OverviewState, |
| 46 | + | ||
| 47 | + /// State of the config. | ||
| 48 | + pub config: ConfigState, | ||
| 46 | } | 49 | } |
| 47 | 50 | ||
| 48 | /// The workspaces state communicated over the event stream. | 51 | /// The workspaces state communicated over the event stream. |
| @@ -73,6 +76,13 @@ pub struct OverviewState { | |||
| 73 | pub is_open: bool, | 76 | pub is_open: bool, |
| 74 | } | 77 | } |
| 75 | 78 | ||
| 79 | +/// The config state communicated over the event stream. | ||
| 80 | +#[derive(Debug, Default)] | ||
| 81 | +pub struct ConfigState { | ||
| 82 | + /// Whether the last config load attempt had failed. | ||
| 83 | + pub failed: bool, | ||
| 84 | +} | ||
| 85 | + | ||
| 76 | impl EventStreamStatePart for EventStreamState { | 86 | impl EventStreamStatePart for EventStreamState { |
| 77 | fn replicate(&self) -> Vec<Event> { | 87 | fn replicate(&self) -> Vec<Event> { |
| 78 | let mut events = Vec::new(); | 88 | let mut events = Vec::new(); |
| @@ -80,6 +90,7 @@ impl EventStreamStatePart for EventStreamState { | |||
| 80 | events.extend(self.windows.replicate()); | 90 | events.extend(self.windows.replicate()); |
| 81 | events.extend(self.keyboard_layouts.replicate()); | 91 | events.extend(self.keyboard_layouts.replicate()); |
| 82 | events.extend(self.overview.replicate()); | 92 | events.extend(self.overview.replicate()); |
| 93 | + events.extend(self.config.replicate()); | ||
| 83 | events | 94 | events |
| 84 | } | 95 | } |
| 85 | 96 | ||
| @@ -88,6 +99,7 @@ impl EventStreamStatePart for EventStreamState { | |||
| 88 | let event = self.windows.apply(event)?; | 99 | let event = self.windows.apply(event)?; |
| 89 | let event = self.keyboard_layouts.apply(event)?; | 100 | let event = self.keyboard_layouts.apply(event)?; |
| 90 | let event = self.overview.apply(event)?; | 101 | let event = self.overview.apply(event)?; |
| 102 | + let event = self.config.apply(event)?; | ||
| 91 | Some(event) | 103 | Some(event) |
| 92 | } | 104 | } |
| 93 | } | 105 | } |
| @@ -244,3 +256,21 @@ impl EventStreamStatePart for OverviewState { | |||
| 244 | None | 256 | None |
| 245 | } | 257 | } |
| 246 | } | 258 | } |
| 259 | + | ||
| 260 | +impl EventStreamStatePart for ConfigState { | ||
| 261 | + fn replicate(&self) -> Vec<Event> { | ||
| 262 | + vec![Event::ConfigLoaded { | ||
| 263 | + failed: self.failed, | ||
| 264 | + }] | ||
| 265 | + } | ||
| 266 | + | ||
| 267 | + fn apply(&mut self, event: Event) -> Option<Event> { | ||
| 268 | + match event { | ||
| 269 | + Event::ConfigLoaded { failed } => { | ||
| 270 | + self.failed = failed; | ||
| 271 | + } | ||
| 272 | + event => return Some(event), | ||
| 273 | + } | ||
| 274 | + None | ||
| 275 | + } | ||
| 276 | +} | ||
src/ipc/client.rsmodified+8 -0
| @@ -459,6 +459,14 @@ pub fn handle_msg(msg: Msg, json: bool) -> anyhow::Result<()> { | |||
| 459 | Event::OverviewOpenedOrClosed { is_open: opened } => { | 459 | Event::OverviewOpenedOrClosed { is_open: opened } => { |
| 460 | println!("Overview toggled: {opened}"); | 460 | println!("Overview toggled: {opened}"); |
| 461 | } | 461 | } |
| 462 | + Event::ConfigLoaded { failed } => { | ||
| 463 | + let status = if failed { | ||
| 464 | + "with an error" | ||
| 465 | + } else { | ||
| 466 | + "successfully" | ||
| 467 | + }; | ||
| 468 | + println!("Config loaded {status}"); | ||
| 469 | + } | ||
| 462 | } | 470 | } |
| 463 | } | 471 | } |
| 464 | } | 472 | } |
src/ipc/server.rsmodified+11 -0
| @@ -762,4 +762,15 @@ impl State { | |||
| 762 | state.apply(event.clone()); | 762 | state.apply(event.clone()); |
| 763 | server.send_event(event); | 763 | server.send_event(event); |
| 764 | } | 764 | } |
| 765 | + | ||
| 766 | + pub fn ipc_config_loaded(&mut self, failed: bool) { | ||
| 767 | + let Some(server) = &self.niri.ipc_server else { | ||
| 768 | + return; | ||
| 769 | + }; | ||
| 770 | + let mut state = server.event_stream_state.borrow_mut(); | ||
| 771 | + | ||
| 772 | + let event = Event::ConfigLoaded { failed }; | ||
| 773 | + state.apply(event.clone()); | ||
| 774 | + server.send_event(event); | ||
| 775 | + } | ||
| 765 | } | 776 | } |
src/main.rsmodified+1 -0
| @@ -241,6 +241,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> { | |||
| 241 | // Show the config error notification right away if needed. | 241 | // Show the config error notification right away if needed. |
| 242 | if config_errored { | 242 | if config_errored { |
| 243 | state.niri.config_error_notification.show(); | 243 | state.niri.config_error_notification.show(); |
| 244 | + state.ipc_config_loaded(true); | ||
| 244 | } else if let Some(path) = config_created_at { | 245 | } else if let Some(path) = config_created_at { |
| 245 | state.niri.config_error_notification.show_created(path); | 246 | state.niri.config_error_notification.show_created(path); |
| 246 | } | 247 | } |
src/ui/config_error_notification.rsmodified+5 -0
| @@ -78,6 +78,11 @@ impl ConfigErrorNotification { | |||
| 78 | } | 78 | } |
| 79 | 79 | ||
| 80 | pub fn show(&mut self) { | 80 | pub fn show(&mut self) { |
| 81 | + let c = self.config.borrow(); | ||
| 82 | + if c.config_notification.disable_failed { | ||
| 83 | + return; | ||
| 84 | + } | ||
| 85 | + | ||
| 81 | if self.created_path.is_some() { | 86 | if self.created_path.is_some() { |
| 82 | self.created_path = None; | 87 | self.created_path = None; |
| 83 | self.buffers.borrow_mut().clear(); | 88 | self.buffers.borrow_mut().clear(); |
src/utils/watcher.rsmodified+12 -5
| @@ -5,7 +5,7 @@ use std::sync::mpsc; | |||
| 5 | use std::time::{Duration, SystemTime}; | 5 | use std::time::{Duration, SystemTime}; |
| 6 | use std::{io, thread}; | 6 | use std::{io, thread}; |
| 7 | 7 | ||
| 8 | -use niri_config::ConfigPath; | 8 | +use niri_config::{Config, ConfigPath}; |
| 9 | use smithay::reexports::calloop::channel::SyncSender; | 9 | use smithay::reexports::calloop::channel::SyncSender; |
| 10 | 10 | ||
| 11 | use crate::niri::State; | 11 | use crate::niri::State; |
| @@ -137,10 +137,17 @@ pub fn setup(state: &mut State, config_path: &ConfigPath) { | |||
| 137 | state | 137 | state |
| 138 | .niri | 138 | .niri |
| 139 | .event_loop | 139 | .event_loop |
| 140 | - .insert_source(rx, |event, _, state| match event { | 140 | + .insert_source( |
| 141 | - calloop::channel::Event::Msg(config) => state.reload_config(config), | 141 | + rx, |
| 142 | - calloop::channel::Event::Closed => (), | 142 | + |event: calloop::channel::Event<Result<Config, ()>>, _, state| match event { |
| 143 | - }) | 143 | + calloop::channel::Event::Msg(config) => { |
| 144 | + let failed = config.is_err(); | ||
| 145 | + state.reload_config(config); | ||
| 146 | + state.ipc_config_loaded(failed); | ||
| 147 | + } | ||
| 148 | + calloop::channel::Event::Closed => (), | ||
| 149 | + }, | ||
| 150 | + ) | ||
| 144 | .unwrap(); | 151 | .unwrap(); |
| 145 | 152 | ||
| 146 | state.niri.config_file_watcher = Some(Watcher::new(config_path.clone(), process, tx)); | 153 | state.niri.config_file_watcher = Some(Watcher::new(config_path.clone(), process, tx)); |
wiki/Configuration:-Miscellaneous.mdmodified+19 -0
| @@ -49,6 +49,10 @@ hotkey-overlay { | |||
| 49 | skip-at-startup | 49 | skip-at-startup |
| 50 | hide-not-bound | 50 | hide-not-bound |
| 51 | } | 51 | } |
| 52 | + | ||
| 53 | +config-notification { | ||
| 54 | + disable-failed | ||
| 55 | +} | ||
| 52 | ``` | 56 | ``` |
| 53 | 57 | ||
| 54 | ### `spawn-at-startup` | 58 | ### `spawn-at-startup` |
| @@ -277,3 +281,18 @@ hotkey-overlay { | |||
| 277 | ``` | 281 | ``` |
| 278 | 282 | ||
| 279 | You can customize which binds the hotkey overlay shows using the [`hotkey-overlay-title` property](./Configuration:-Key-Bindings.md#custom-hotkey-overlay-titles). | 283 | You can customize which binds the hotkey overlay shows using the [`hotkey-overlay-title` property](./Configuration:-Key-Bindings.md#custom-hotkey-overlay-titles). |
| 284 | + | ||
| 285 | +### `config-notification` | ||
| 286 | + | ||
| 287 | +<sup>Since: next release</sup> | ||
| 288 | + | ||
| 289 | +Settings for the config created/failed notification. | ||
| 290 | + | ||
| 291 | +Set the `disable-failed` flag to disable the "Failed to parse the config file" notification. | ||
| 292 | +For example, if you have a custom one. | ||
| 293 | + | ||
| 294 | +```kdl | ||
| 295 | +config-notification { | ||
| 296 | + disable-failed | ||
| 297 | +} | ||
| 298 | +``` | ||