forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiB9ff2f83Ivan Molodetskikh1 year ago2099 commits

config_error_notification.rs248 lines, 7.7 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: PathBuf) {
72 let created_path = Some(created_path);
73 if self.created_path != created_path {
74 self.created_path = created_path;
75 self.buffers.borrow_mut().clear();
76 }
77
78 self.state = State::Showing(self.animation(0., 1.));
79 }
80
81 pub fn show(&mut self) {
82 if self.created_path.is_some() {
83 self.created_path = None;
84 self.buffers.borrow_mut().clear();
85 }
86
87 // Show from scratch even if already showing to bring attention.
88 self.state = State::Showing(self.animation(0., 1.));
89 }
90
91 pub fn hide(&mut self) {
92 if matches!(self.state, State::Hidden) {
93 return;
94 }
95
96 self.state = State::Hiding(self.animation(1., 0.));
97 }
98
99 pub fn advance_animations(&mut self) {
100 match &mut self.state {
101 State::Hidden => (),
102 State::Showing(anim) => {
103 if anim.is_done() {
104 let duration = if self.created_path.is_some() {
105 // Make this quite a bit longer because it comes with a monitor modeset
106 // (can take a while) and an important hotkeys popup diverting the
107 // attention.
108 Duration::from_secs(8)
109 } else {
110 Duration::from_secs(4)
111 };
112 self.state = State::Shown(self.clock.now() + duration);
113 }
114 }
115 State::Shown(deadline) => {
116 if self.clock.now() >= *deadline {
117 self.hide();
118 }
119 }
120 State::Hiding(anim) => {
121 if anim.is_clamped_done() {
122 self.state = State::Hidden;
123 }
124 }
125 }
126 }
127
128 pub fn are_animations_ongoing(&self) -> bool {
129 !matches!(self.state, State::Hidden)
130 }
131
132 pub fn render<R: NiriRenderer>(
133 &self,
134 renderer: &mut R,
135 output: &Output,
136 ) -> Option<PrimaryGpuTextureRenderElement> {
137 if matches!(self.state, State::Hidden) {
138 return None;
139 }
140
141 let scale = output.current_scale().fractional_scale();
142 let output_size = output_size(output);
143 let path = self.created_path.as_deref();
144
145 let mut buffers = self.buffers.borrow_mut();
146 let buffer = buffers
147 .entry(NotNan::new(scale).unwrap())
148 .or_insert_with(move || render(renderer.as_gles_renderer(), scale, path).ok());
149 let buffer = buffer.clone()?;
150
151 let size = buffer.logical_size();
152 let y_range = size.h + f64::from(PADDING) * 2.;
153
154 let x = (output_size.w - size.w).max(0.) / 2.;
155 let y = match &self.state {
156 State::Hidden => unreachable!(),
157 State::Showing(anim) | State::Hiding(anim) => -size.h + anim.value() * y_range,
158 State::Shown(_) => f64::from(PADDING) * 2.,
159 };
160
161 let location = Point::from((x, y));
162 let location = location.to_physical_precise_round(scale).to_logical(scale);
163
164 let elem = TextureRenderElement::from_texture_buffer(
165 buffer,
166 location,
167 1.,
168 None,
169 None,
170 Kind::Unspecified,
171 );
172 Some(PrimaryGpuTextureRenderElement(elem))
173 }
174}
175
176fn render(
177 renderer: &mut GlesRenderer,
178 scale: f64,
179 created_path: Option<&Path>,
180) -> anyhow::Result<TextureBuffer<GlesTexture>> {
181 let _span = tracy_client::span!("config_error_notification::render");
182
183 let padding: i32 = to_physical_precise_round(scale, PADDING);
184
185 let mut text = String::from(TEXT);
186 let mut border_color = (1., 0.3, 0.3);
187 if let Some(path) = created_path {
188 text = format!(
189 "Created a default config file at \
190 <span face='monospace' bgcolor='#000000'>{:?}</span>",
191 path
192 );
193 border_color = (0.5, 1., 0.5);
194 };
195
196 let mut font = FontDescription::from_string(FONT);
197 font.set_absolute_size(to_physical_precise_round(scale, font.size()));
198
199 let surface = ImageSurface::create(cairo::Format::ARgb32, 0, 0)?;
200 let cr = cairo::Context::new(&surface)?;
201 let layout = pangocairo::functions::create_layout(&cr);
202 layout.context().set_round_glyph_positions(false);
203 layout.set_font_description(Some(&font));
204 layout.set_markup(&text);
205
206 let (mut width, mut height) = layout.pixel_size();
207 width += padding * 2;
208 height += padding * 2;
209
210 let surface = ImageSurface::create(cairo::Format::ARgb32, width, height)?;
211 let cr = cairo::Context::new(&surface)?;
212 cr.set_source_rgb(0.1, 0.1, 0.1);
213 cr.paint()?;
214
215 cr.move_to(padding.into(), padding.into());
216 let layout = pangocairo::functions::create_layout(&cr);
217 layout.context().set_round_glyph_positions(false);
218 layout.set_font_description(Some(&font));
219 layout.set_markup(&text);
220
221 cr.set_source_rgb(1., 1., 1.);
222 pangocairo::functions::show_layout(&cr, &layout);
223
224 cr.move_to(0., 0.);
225 cr.line_to(width.into(), 0.);
226 cr.line_to(width.into(), height.into());
227 cr.line_to(0., height.into());
228 cr.line_to(0., 0.);
229 cr.set_source_rgb(border_color.0, border_color.1, border_color.2);
230 // Keep the border width even to avoid blurry edges.
231 cr.set_line_width((f64::from(BORDER) / 2. * scale).round() * 2.);
232 cr.stroke()?;
233 drop(cr);
234
235 let data = surface.take_data().unwrap();
236 let buffer = TextureBuffer::from_memory(
237 renderer,
238 &data,
239 Fourcc::Argb8888,
240 (width, height),
241 false,
242 scale,
243 Transform::Normal,
244 Vec::new(),
245 )?;
246
247 Ok(buffer)
248}