tony / oxwm

Added proper error handling for when a user logs in to a deprecated config. Browse files
tonybtw committed 2025-11-13 parent 115e1de commit 2d0ce7825c6353a42f12e3dccd6f032122174bb8
Showing 4 changed files with 38 additions and 23 deletions. UnifiedSplit
+1 −1 Cargo.lock
@@ -297,7 +297,7 @@ checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d"
297 297
298 [[package]] 298 [[package]]
299 name = "oxwm" 299 name = "oxwm"
300 -version = "0.6.0" 300 +version = "0.7.0"
301 dependencies = [ 301 dependencies = [
302 "anyhow", 302 "anyhow",
303 "chrono", 303 "chrono",
+17 −4 src/bin/main.rs
@@ -30,9 +30,14 @@ fn main() -> Result<()> {
30 _ => {} 30 _ => {}
31 } 31 }
32 32
33 - let config = load_config(custom_config_path)?; 33 + let (config, had_broken_config) = load_config(custom_config_path)?;
34 34
35 let mut wm = oxwm::window_manager::WindowManager::new(config)?; 35 let mut wm = oxwm::window_manager::WindowManager::new(config)?;
36 +
37 + if had_broken_config {
38 + wm.show_migration_overlay();
39 + }
40 +
36 let should_restart = wm.run()?; 41 let should_restart = wm.run()?;
37 42
38 drop(wm); 43 drop(wm);
@@ -46,7 +51,7 @@ fn main() -> Result<()> {
46 Ok(()) 51 Ok(())
47 } 52 }
48 53
49 -fn load_config(custom_path: Option<PathBuf>) -> Result<oxwm::Config> { 54 +fn load_config(custom_path: Option<PathBuf>) -> Result<(oxwm::Config, bool)> {
50 let config_path = if let Some(path) = custom_path { 55 let config_path = if let Some(path) = custom_path {
51 path 56 path
52 } else { 57 } else {
@@ -77,8 +82,16 @@ fn load_config(custom_path: Option<PathBuf>) -> Result<oxwm::Config> {
77 std::fs::read_to_string(&config_path).with_context(|| "Failed to read config file")?; 82 std::fs::read_to_string(&config_path).with_context(|| "Failed to read config file")?;
78 83
79 let config_dir = config_path.parent(); 84 let config_dir = config_path.parent();
80 - oxwm::config::parse_lua_config(&config_str, config_dir) 85 +
81 - .with_context(|| "Failed to parse Lua config") 86 + match oxwm::config::parse_lua_config(&config_str, config_dir) {
87 + Ok(config) => Ok((config, false)),
88 + Err(_) => {
89 + let template = include_str!("../../templates/config.lua");
90 + let config = oxwm::config::parse_lua_config(template, None)
91 + .with_context(|| "Failed to parse default template config")?;
92 + Ok((config, true))
93 + }
94 + }
82 } 95 }
83 96
84 fn init_config() -> Result<()> { 97 fn init_config() -> Result<()> {
+0 −14 src/overlay/error.rs
@@ -1,7 +1,6 @@
1 use super::{Overlay, OverlayBase}; 1 use super::{Overlay, OverlayBase};
2 use crate::bar::font::Font; 2 use crate::bar::font::Font;
3 use crate::errors::X11Error; 3 use crate::errors::X11Error;
4 -use std::time::Instant;
5 use x11rb::connection::Connection; 4 use x11rb::connection::Connection;
6 use x11rb::protocol::xproto::*; 5 use x11rb::protocol::xproto::*;
7 use x11rb::rust_connection::RustConnection; 6 use x11rb::rust_connection::RustConnection;
@@ -10,11 +9,9 @@ const PADDING: i16 = 20;
10 const LINE_SPACING: i16 = 5; 9 const LINE_SPACING: i16 = 5;
11 const BORDER_WIDTH: u16 = 2; 10 const BORDER_WIDTH: u16 = 2;
12 const BORDER_COLOR: u32 = 0xff5555; 11 const BORDER_COLOR: u32 = 0xff5555;
13 -const AUTO_DISMISS_SECONDS: u64 = 10;
14 12
15 pub struct ErrorOverlay { 13 pub struct ErrorOverlay {
16 base: OverlayBase, 14 base: OverlayBase,
17 - created_at: Option<Instant>,
18 lines: Vec<String>, 15 lines: Vec<String>,
19 } 16 }
20 17
@@ -42,7 +39,6 @@ impl ErrorOverlay {
42 39
43 Ok(ErrorOverlay { 40 Ok(ErrorOverlay {
44 base, 41 base,
45 - created_at: None,
46 lines: Vec::new(), 42 lines: Vec::new(),
47 }) 43 })
48 } 44 }
@@ -76,19 +72,10 @@ impl ErrorOverlay {
76 72
77 self.base.configure(connection, x, y, width, height)?; 73 self.base.configure(connection, x, y, width, height)?;
78 self.base.show(connection)?; 74 self.base.show(connection)?;
79 - self.created_at = Some(Instant::now());
80 self.draw(connection, font)?; 75 self.draw(connection, font)?;
81 Ok(()) 76 Ok(())
82 } 77 }
83 78
84 - pub fn should_auto_dismiss(&self) -> bool {
85 - if let Some(created_at) = self.created_at {
86 - created_at.elapsed().as_secs() >= AUTO_DISMISS_SECONDS
87 - } else {
88 - false
89 - }
90 - }
91 -
92 fn wrap_text(&self, text: &str, font: &Font, max_width: u16) -> Vec<String> { 79 fn wrap_text(&self, text: &str, font: &Font, max_width: u16) -> Vec<String> {
93 let mut lines = Vec::new(); 80 let mut lines = Vec::new();
94 for paragraph in text.lines() { 81 for paragraph in text.lines() {
@@ -134,7 +121,6 @@ impl Overlay for ErrorOverlay {
134 121
135 fn hide(&mut self, connection: &RustConnection) -> Result<(), X11Error> { 122 fn hide(&mut self, connection: &RustConnection) -> Result<(), X11Error> {
136 self.base.hide(connection)?; 123 self.base.hide(connection)?;
137 - self.created_at = None;
138 self.lines.clear(); 124 self.lines.clear();
139 Ok(()) 125 Ok(())
140 } 126 }
+20 −4 src/window_manager.rs
@@ -244,6 +244,26 @@ impl WindowManager {
244 Ok(tag_mask(0)) 244 Ok(tag_mask(0))
245 } 245 }
246 246
247 + pub fn show_migration_overlay(&mut self) {
248 + let message = "Your config.lua uses legacy syntax or has errors.\n\n\
249 + You are now running with default configuration.\n\n\
250 + Press Mod+Shift+/ to see default keybinds\n\
251 + Press Mod+Shift+R to reload after fixing your config";
252 +
253 + let screen_width = self.screen.width_in_pixels;
254 + let screen_height = self.screen.height_in_pixels;
255 +
256 + if let Err(e) = self.overlay.show_error(
257 + &self.connection,
258 + &self.font,
259 + message,
260 + screen_width,
261 + screen_height,
262 + ) {
263 + eprintln!("Failed to show migration overlay: {:?}", e);
264 + }
265 + }
266 +
247 fn try_reload_config(&mut self) -> Result<(), String> { 267 fn try_reload_config(&mut self) -> Result<(), String> {
248 let config_dir = if let Some(xdg_config) = std::env::var_os("XDG_CONFIG_HOME") { 268 let config_dir = if let Some(xdg_config) = std::env::var_os("XDG_CONFIG_HOME") {
249 std::path::PathBuf::from(xdg_config).join("oxwm") 269 std::path::PathBuf::from(xdg_config).join("oxwm")
@@ -427,10 +447,6 @@ impl WindowManager {
427 self.update_bar()?; 447 self.update_bar()?;
428 } 448 }
429 449
430 - if self.overlay.is_visible() && self.overlay.should_auto_dismiss() {
431 - let _ = self.overlay.hide(&self.connection);
432 - }
433 -
434 std::thread::sleep(std::time::Duration::from_millis(100)); 450 std::thread::sleep(std::time::Duration::from_millis(100));
435 } 451 }
436 } 452 }