forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiB055a94dIvan Molodetskikh1 year ago2247 commits

config_error_notification.rs246 lines, 7.6 KiBHistoryRawDownload
1use std::cell::RefCell;
2use std::collections::HashMap;
3use std::path::{Path, PathBuf};
4use std::rc::Rc;
5use std::time::Duration;
6
7use niri_config::Config;
8use ordered_float::NotNan;
9use pangocairo::cairo::{self, ImageSurface};
10use pangocairo::pango::FontDescription;
11use smithay::backend::renderer::element::Kind;
12use smithay::backend::renderer::gles::{GlesRenderer, GlesTexture};
13use smithay::output::Output;
14use smithay::reexports::gbm::Format as Fourcc;
15use smithay::utils::{Point, Transform};
16
17use crate::animation::{Animation, Clock};
18use crate::render_helpers::primary_gpu_texture::PrimaryGpuTextureRenderElement;
19use crate::render_helpers::renderer::NiriRenderer;
20use crate::render_helpers::texture::{TextureBuffer, TextureRenderElement};
21use crate::utils::{output_size, to_physical_precise_round};
22
23const TEXT: &str = "Failed to parse the config file. \
24 Please run <span face='monospace' bgcolor='#000000'>niri validate</span> \
25 to see the errors.";
26const PADDING: i32 = 8;
27const FONT: &str = "sans 14px";
28const BORDER: i32 = 4;
29
30pub struct ConfigErrorNotification {
31 state: State,
32 buffers: RefCell<HashMap<NotNan<f64>, Option<TextureBuffer<GlesTexture>>>>,
33
34 // If set, this is a "Created config at {path}" notification. If unset, this is a config error
35 // notification.
36 created_path: Option<PathBuf>,
37
38 clock: Clock,
39 config: Rc<RefCell<Config>>,
40}
41
42enum State {
43 Hidden,
44 Showing(Animation),
45 Shown(Duration),
46 Hiding(Animation),
47}
48
49impl ConfigErrorNotification {
50 pub fn new(clock: Clock, config: Rc<RefCell<Config>>) -> Self {
51 Self {
52 state: State::Hidden,
53 buffers: RefCell::new(HashMap::new()),
54 created_path: None,
55 clock,
56 config,
57 }
58 }
59
60 fn animation(&self, from: f64, to: f64) -> Animation {
61 let c = self.config.borrow();
62 Animation::new(
63 self.clock.clone(),
64 from,
65 to,
66 0.,
67 c.animations.config_notification_open_close.0,
68 )
69 }
70
71 pub fn show_created(&mut self, created_path: &Path) {
72 if self.created_path.as_deref() != Some(created_path) {
73 self.created_path = Some(created_path.to_owned());
74 self.buffers.borrow_mut().clear();
75 }
76
77 self.state = State::Showing(self.animation(0., 1.));
78 }
79
80 pub fn show(&mut self) {
81 if self.created_path.is_some() {
82 self.created_path = None;
83 self.buffers.borrow_mut().clear();
84 }
85
86 // Show from scratch even if already showing to bring attention.
87 self.state = State::Showing(self.animation(0., 1.));
88 }
89
90 pub fn hide(&mut self) {
91 if matches!(self.state, State::Hidden) {
92 return;
93 }
94
95 self.state = State::Hiding(self.animation(1., 0.));
96 }
97
98 pub fn advance_animations(&mut self) {
99 match &mut self.state {
100 State::Hidden => (),
101 State::Showing(anim) => {
102 if anim.is_done() {
103 let duration = if self.created_path.is_some() {
104 // Make this quite a bit longer because it comes with a monitor modeset
105 // (can take a while) and an important hotkeys popup diverting the
106 // attention.
107 Duration::from_secs(8)
108 } else {
109 Duration::from_secs(4)
110 };
111 self.state = State::Shown(self.clock.now() + duration);
112 }
113 }
114 State::Shown(deadline) => {
115 if self.clock.now() >= *deadline {
116 self.hide();
117 }
118 }
119 State::Hiding(anim) => {
120 if anim.is_clamped_done() {
121 self.state = State::Hidden;
122 }
123 }
124 }
125 }
126
127 pub fn are_animations_ongoing(&self) -> bool {
128 !matches!(self.state, State::Hidden)
129 }
130
131 pub fn render<R: NiriRenderer>(
132 &self,
133 renderer: &mut R,
134 output: &Output,
135 ) -> Option<PrimaryGpuTextureRenderElement> {
136 if matches!(self.state, State::Hidden) {
137 return None;
138 }
139
140 let scale = output.current_scale().fractional_scale();
141 let output_size = output_size(output);
142 let path = self.created_path.as_deref();
143
144 let mut buffers = self.buffers.borrow_mut();
145 let buffer = buffers
146 .entry(NotNan::new(scale).unwrap())
147 .or_insert_with(move || render(renderer.as_gles_renderer(), scale, path).ok());
148 let buffer = buffer.clone()?;
149
150 let size = buffer.logical_size();
151 let y_range = size.h + f64::from(PADDING) * 2.;
152
153 let x = (output_size.w - size.w).max(0.) / 2.;
154 let y = match &self.state {
155 State::Hidden => unreachable!(),
156 State::Showing(anim) | State::Hiding(anim) => -size.h + anim.value() * y_range,
157 State::Shown(_) => f64::from(PADDING) * 2.,
158 };
159
160 let location = Point::from((x, y));
161 let location = location.to_physical_precise_round(scale).to_logical(scale);
162
163 let elem = TextureRenderElement::from_texture_buffer(
164 buffer,
165 location,
166 1.,
167 None,
168 None,
169 Kind::Unspecified,
170 );
171 Some(PrimaryGpuTextureRenderElement(elem))
172 }
173}
174
175fn render(
176 renderer: &mut GlesRenderer,
177 scale: f64,
178 created_path: Option<&Path>,
179) -> anyhow::Result<TextureBuffer<GlesTexture>> {
180 let _span = tracy_client::span!("config_error_notification::render");
181
182 let padding: i32 = to_physical_precise_round(scale, PADDING);
183
184 let mut text = String::from(TEXT);
185 let mut border_color = (1., 0.3, 0.3);
186 if let Some(path) = created_path {
187 text = format!(
188 "Created a default config file at \
189 <span face='monospace' bgcolor='#000000'>{path:?}</span>",
190 );
191 border_color = (0.5, 1., 0.5);
192 };
193
194 let mut font = FontDescription::from_string(FONT);
195 font.set_absolute_size(to_physical_precise_round(scale, font.size()));
196
197 let surface = ImageSurface::create(cairo::Format::ARgb32, 0, 0)?;
198 let cr = cairo::Context::new(&surface)?;
199 let layout = pangocairo::functions::create_layout(&cr);
200 layout.context().set_round_glyph_positions(false);
201 layout.set_font_description(Some(&font));
202 layout.set_markup(&text);
203
204 let (mut width, mut height) = layout.pixel_size();
205 width += padding * 2;
206 height += padding * 2;
207
208 let surface = ImageSurface::create(cairo::Format::ARgb32, width, height)?;
209 let cr = cairo::Context::new(&surface)?;
210 cr.set_source_rgb(0.1, 0.1, 0.1);
211 cr.paint()?;
212
213 cr.move_to(padding.into(), padding.into());
214 let layout = pangocairo::functions::create_layout(&cr);
215 layout.context().set_round_glyph_positions(false);
216 layout.set_font_description(Some(&font));
217 layout.set_markup(&text);
218
219 cr.set_source_rgb(1., 1., 1.);
220 pangocairo::functions::show_layout(&cr, &layout);
221
222 cr.move_to(0., 0.);
223 cr.line_to(width.into(), 0.);
224 cr.line_to(width.into(), height.into());
225 cr.line_to(0., height.into());
226 cr.line_to(0., 0.);
227 cr.set_source_rgb(border_color.0, border_color.1, border_color.2);
228 // Keep the border width even to avoid blurry edges.
229 cr.set_line_width((f64::from(BORDER) / 2. * scale).round() * 2.);
230 cr.stroke()?;
231 drop(cr);
232
233 let data = surface.take_data().unwrap();
234 let buffer = TextureBuffer::from_memory(
235 renderer,
236 &data,
237 Fourcc::Argb8888,
238 (width, height),
239 false,
240 scale,
241 Transform::Normal,
242 Vec::new(),
243 )?;
244
245 Ok(buffer)
246}