tony / oxwm

Branch: 115e1de
Switch branches/tags
master
tonybtw Updated error handling in lua api, and configured restart correctly with super shift r keybind. also handled bar redraw correctly. 27a77c7 2025-11-13
158 lines · 4.5 KB Raw Blame History
1 use super::{Overlay, OverlayBase};
2 use crate::bar::font::Font;
3 use crate::errors::X11Error;
4 use std::time::Instant;
5 use x11rb::connection::Connection;
6 use x11rb::protocol::xproto::*;
7 use x11rb::rust_connection::RustConnection;
8
9 const PADDING: i16 = 20;
10 const LINE_SPACING: i16 = 5;
11 const BORDER_WIDTH: u16 = 2;
12 const BORDER_COLOR: u32 = 0xff5555;
13 const AUTO_DISMISS_SECONDS: u64 = 10;
14
15 pub struct ErrorOverlay {
16 base: OverlayBase,
17 created_at: Option<Instant>,
18 lines: Vec<String>,
19 }
20
21 impl ErrorOverlay {
22 pub fn new(
23 connection: &RustConnection,
24 screen: &Screen,
25 screen_num: usize,
26 display: *mut x11::xlib::Display,
27 _font: &Font,
28 _max_width: u16,
29 ) -> Result<Self, X11Error> {
30 let base = OverlayBase::new(
31 connection,
32 screen,
33 screen_num,
34 display,
35 400,
36 200,
37 BORDER_WIDTH,
38 BORDER_COLOR,
39 0x1a1a1a,
40 0xffffff,
41 )?;
42
43 Ok(ErrorOverlay {
44 base,
45 created_at: None,
46 lines: Vec::new(),
47 })
48 }
49
50 pub fn show_error(
51 &mut self,
52 connection: &RustConnection,
53 font: &Font,
54 error_text: &str,
55 screen_width: u16,
56 screen_height: u16,
57 ) -> Result<(), X11Error> {
58 let max_line_width = (screen_width as i16 / 2 - PADDING * 4).max(300) as u16;
59 let error_with_instruction = format!("{}\n\nFix the config file and reload.", error_text);
60 self.lines = self.wrap_text(&error_with_instruction, font, max_line_width);
61
62 let mut content_width = 0u16;
63 for line in &self.lines {
64 let line_width = font.text_width(line);
65 if line_width > content_width {
66 content_width = line_width;
67 }
68 }
69
70 let width = content_width + (PADDING as u16 * 2);
71 let line_height = font.height() + LINE_SPACING as u16;
72 let height = (self.lines.len() as u16 * line_height) + (PADDING as u16 * 2);
73
74 let x = ((screen_width - width) / 2) as i16;
75 let y = ((screen_height - height) / 2) as i16;
76
77 self.base.configure(connection, x, y, width, height)?;
78 self.base.show(connection)?;
79 self.created_at = Some(Instant::now());
80 self.draw(connection, font)?;
81 Ok(())
82 }
83
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> {
93 let mut lines = Vec::new();
94 for paragraph in text.lines() {
95 if paragraph.trim().is_empty() {
96 lines.push(String::new());
97 continue;
98 }
99
100 let words: Vec<&str> = paragraph.split_whitespace().collect();
101 let mut current_line = String::new();
102
103 for word in words {
104 let test_line = if current_line.is_empty() {
105 word.to_string()
106 } else {
107 format!("{} {}", current_line, word)
108 };
109 if font.text_width(&test_line) <= max_width {
110 current_line = test_line;
111 } else {
112 if !current_line.is_empty() {
113 lines.push(current_line);
114 }
115 current_line = word.to_string();
116 }
117 }
118 if !current_line.is_empty() {
119 lines.push(current_line);
120 }
121 }
122 lines
123 }
124 }
125
126 impl Overlay for ErrorOverlay {
127 fn window(&self) -> Window {
128 self.base.window
129 }
130
131 fn is_visible(&self) -> bool {
132 self.base.is_visible
133 }
134
135 fn hide(&mut self, connection: &RustConnection) -> Result<(), X11Error> {
136 self.base.hide(connection)?;
137 self.created_at = None;
138 self.lines.clear();
139 Ok(())
140 }
141
142 fn draw(&self, connection: &RustConnection, font: &Font) -> Result<(), X11Error> {
143 if !self.base.is_visible {
144 return Ok(());
145 }
146 self.base.draw_background(connection)?;
147 let line_height = font.height() + LINE_SPACING as u16;
148 let mut y = PADDING + font.ascent();
149 for line in &self.lines {
150 self.base
151 .font_draw
152 .draw_text(font, self.base.foreground_color, PADDING, y, line);
153 y += line_height as i16;
154 }
155 connection.flush()?;
156 Ok(())
157 }
158 }