tony / oxwm

Branch: 89f2203
Switch branches/tags
master
tonybtw Added proper error handling in blocks, and font. added error modal so config syntax error doesnt crash when you reload your oxwm, added keybind overlay modal to show the important keybinds (inspired by niri), moved keysms helper function into const file to reduce code duplication. ad06fa5 2025-11-09
157 lines · 4.4 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 - PADDING * 4).max(300) as u16;
59 self.lines = self.wrap_text(error_text, font, max_line_width);
60
61 let mut content_width = 0u16;
62 for line in &self.lines {
63 let line_width = font.text_width(line);
64 if line_width > content_width {
65 content_width = line_width;
66 }
67 }
68
69 let width = content_width + (PADDING as u16 * 2);
70 let line_height = font.height() + LINE_SPACING as u16;
71 let height = (self.lines.len() as u16 * line_height) + (PADDING as u16 * 2);
72
73 let x = ((screen_width - width) / 2) as i16;
74 let y = ((screen_height - height) / 2) as i16;
75
76 self.base.configure(connection, x, y, width, height)?;
77 self.base.show(connection)?;
78 self.created_at = Some(Instant::now());
79 self.draw(connection, font)?;
80 Ok(())
81 }
82
83 pub fn should_auto_dismiss(&self) -> bool {
84 if let Some(created_at) = self.created_at {
85 created_at.elapsed().as_secs() >= AUTO_DISMISS_SECONDS
86 } else {
87 false
88 }
89 }
90
91 fn wrap_text(&self, text: &str, font: &Font, max_width: u16) -> Vec<String> {
92 let mut lines = Vec::new();
93 for paragraph in text.lines() {
94 if paragraph.trim().is_empty() {
95 lines.push(String::new());
96 continue;
97 }
98
99 let words: Vec<&str> = paragraph.split_whitespace().collect();
100 let mut current_line = String::new();
101
102 for word in words {
103 let test_line = if current_line.is_empty() {
104 word.to_string()
105 } else {
106 format!("{} {}", current_line, word)
107 };
108 if font.text_width(&test_line) <= max_width {
109 current_line = test_line;
110 } else {
111 if !current_line.is_empty() {
112 lines.push(current_line);
113 }
114 current_line = word.to_string();
115 }
116 }
117 if !current_line.is_empty() {
118 lines.push(current_line);
119 }
120 }
121 lines
122 }
123 }
124
125 impl Overlay for ErrorOverlay {
126 fn window(&self) -> Window {
127 self.base.window
128 }
129
130 fn is_visible(&self) -> bool {
131 self.base.is_visible
132 }
133
134 fn hide(&mut self, connection: &RustConnection) -> Result<(), X11Error> {
135 self.base.hide(connection)?;
136 self.created_at = None;
137 self.lines.clear();
138 Ok(())
139 }
140
141 fn draw(&self, connection: &RustConnection, font: &Font) -> Result<(), X11Error> {
142 if !self.base.is_visible {
143 return Ok(());
144 }
145 self.base.draw_background(connection)?;
146 let line_height = font.height() + LINE_SPACING as u16;
147 let mut y = PADDING + font.ascent();
148 for line in &self.lines {
149 self.base
150 .font_draw
151 .draw_text(font, self.base.foreground_color, PADDING, y, line);
152 y += line_height as i16;
153 }
154 connection.flush()?;
155 Ok(())
156 }
157 }