forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiB64a9351Ivan Molodetskikh2 years ago1319 commits

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