| 1 | use std::cell::RefCell; |
| 2 | use std::collections::HashMap; |
| 3 | use std::path::{Path, PathBuf}; |
| 4 | use std::rc::Rc; |
| 5 | use std::time::Duration; |
| 6 | |
| 7 | use niri_config::Config; |
| 8 | use pangocairo::cairo::{self, ImageSurface}; |
| 9 | use pangocairo::pango::FontDescription; |
| 10 | use smithay::backend::renderer::element::memory::{ |
| 11 | MemoryRenderBuffer, MemoryRenderBufferRenderElement, |
| 12 | }; |
| 13 | use smithay::backend::renderer::element::utils::{Relocate, RelocateRenderElement}; |
| 14 | use smithay::backend::renderer::element::{Element, Kind}; |
| 15 | use smithay::output::Output; |
| 16 | use smithay::reexports::gbm::Format as Fourcc; |
| 17 | use smithay::utils::Transform; |
| 18 | |
| 19 | use crate::animation::Animation; |
| 20 | use crate::render_helpers::renderer::NiriRenderer; |
| 21 | |
| 22 | const TEXT: &str = "Failed to parse the config file. \ |
| 23 | Please run <span face='monospace' bgcolor='#000000'>niri validate</span> \ |
| 24 | to see the errors."; |
| 25 | const PADDING: i32 = 8; |
| 26 | const FONT: &str = "sans 14px"; |
| 27 | const BORDER: i32 = 4; |
| 28 | |
| 29 | pub struct ConfigErrorNotification { |
| 30 | state: State, |
| 31 | buffers: RefCell<HashMap<i32, Option<MemoryRenderBuffer>>>, |
| 32 | |
| 33 | |
| 34 | |
| 35 | created_path: Option<PathBuf>, |
| 36 | |
| 37 | config: Rc<RefCell<Config>>, |
| 38 | } |
| 39 | |
| 40 | enum State { |
| 41 | Hidden, |
| 42 | Showing(Animation), |
| 43 | Shown(Duration), |
| 44 | Hiding(Animation), |
| 45 | } |
| 46 | |
| 47 | pub type ConfigErrorNotificationRenderElement<R> = |
| 48 | RelocateRenderElement<MemoryRenderBufferRenderElement<R>>; |
| 49 | |
| 50 | impl ConfigErrorNotification { |
| 51 | pub fn new(config: Rc<RefCell<Config>>) -> Self { |
| 52 | Self { |
| 53 | state: State::Hidden, |
| 54 | buffers: RefCell::new(HashMap::new()), |
| 55 | created_path: None, |
| 56 | config, |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | fn animation(&self, from: f64, to: f64) -> Animation { |
| 61 | let c = self.config.borrow(); |
| 62 | Animation::new(from, to, 0., c.animations.config_notification_open_close.0) |
| 63 | } |
| 64 | |
| 65 | pub fn show_created(&mut self, created_path: Option<PathBuf>) { |
| 66 | if self.created_path != created_path { |
| 67 | self.created_path = created_path; |
| 68 | self.buffers.borrow_mut().clear(); |
| 69 | } |
| 70 | |
| 71 | self.state = State::Showing(self.animation(0., 1.)); |
| 72 | } |
| 73 | |
| 74 | pub fn show(&mut self) { |
| 75 | if self.created_path.is_some() { |
| 76 | self.created_path = None; |
| 77 | self.buffers.borrow_mut().clear(); |
| 78 | } |
| 79 | |
| 80 | |
| 81 | self.state = State::Showing(self.animation(0., 1.)); |
| 82 | } |
| 83 | |
| 84 | pub fn hide(&mut self) { |
| 85 | if matches!(self.state, State::Hidden) { |
| 86 | return; |
| 87 | } |
| 88 | |
| 89 | self.state = State::Hiding(self.animation(1., 0.)); |
| 90 | } |
| 91 | |
| 92 | pub fn advance_animations(&mut self, target_presentation_time: Duration) { |
| 93 | match &mut self.state { |
| 94 | State::Hidden => (), |
| 95 | State::Showing(anim) => { |
| 96 | anim.set_current_time(target_presentation_time); |
| 97 | if anim.is_done() { |
| 98 | let duration = if self.created_path.is_some() { |
| 99 | |
| 100 | |
| 101 | |
| 102 | Duration::from_secs(8) |
| 103 | } else { |
| 104 | Duration::from_secs(4) |
| 105 | }; |
| 106 | self.state = State::Shown(target_presentation_time + duration); |
| 107 | } |
| 108 | } |
| 109 | State::Shown(deadline) => { |
| 110 | if target_presentation_time >= *deadline { |
| 111 | self.hide(); |
| 112 | } |
| 113 | } |
| 114 | State::Hiding(anim) => { |
| 115 | anim.set_current_time(target_presentation_time); |
| 116 | if anim.is_clamped_done() { |
| 117 | self.state = State::Hidden; |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | pub fn are_animations_ongoing(&self) -> bool { |
| 124 | !matches!(self.state, State::Hidden) |
| 125 | } |
| 126 | |
| 127 | pub fn render<R: NiriRenderer>( |
| 128 | &self, |
| 129 | renderer: &mut R, |
| 130 | output: &Output, |
| 131 | ) -> Option<ConfigErrorNotificationRenderElement<R>> { |
| 132 | if matches!(self.state, State::Hidden) { |
| 133 | return None; |
| 134 | } |
| 135 | |
| 136 | let scale = output.current_scale().integer_scale(); |
| 137 | let path = self.created_path.as_deref(); |
| 138 | |
| 139 | let mut buffers = self.buffers.borrow_mut(); |
| 140 | let buffer = buffers |
| 141 | .entry(scale) |
| 142 | .or_insert_with_key(move |&scale| render(scale, path).ok()); |
| 143 | let buffer = buffer.as_ref()?; |
| 144 | |
| 145 | let elem = MemoryRenderBufferRenderElement::from_buffer( |
| 146 | renderer, |
| 147 | (0., 0.), |
| 148 | buffer, |
| 149 | Some(0.9), |
| 150 | None, |
| 151 | None, |
| 152 | Kind::Unspecified, |
| 153 | ) |
| 154 | .ok()?; |
| 155 | |
| 156 | let output_transform = output.current_transform(); |
| 157 | let output_mode = output.current_mode().unwrap(); |
| 158 | let output_size = output_transform.transform_size(output_mode.size); |
| 159 | |
| 160 | let buffer_size = elem |
| 161 | .geometry(output.current_scale().fractional_scale().into()) |
| 162 | .size; |
| 163 | |
| 164 | let y_range = buffer_size.h + PADDING * 2 * scale; |
| 165 | |
| 166 | let x = (output_size.w / 2 - buffer_size.w / 2).max(0); |
| 167 | let y = match &self.state { |
| 168 | State::Hidden => unreachable!(), |
| 169 | State::Showing(anim) | State::Hiding(anim) => { |
| 170 | (-buffer_size.h as f64 + anim.value() * y_range as f64).round() as i32 |
| 171 | } |
| 172 | State::Shown(_) => PADDING * 2 * scale, |
| 173 | }; |
| 174 | let elem = RelocateRenderElement::from_element(elem, (x, y), Relocate::Absolute); |
| 175 | |
| 176 | Some(elem) |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | fn render(scale: i32, created_path: Option<&Path>) -> anyhow::Result<MemoryRenderBuffer> { |
| 181 | let _span = tracy_client::span!("config_error_notification::render"); |
| 182 | |
| 183 | let padding = PADDING * scale; |
| 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((font.size() * scale).into()); |
| 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.set_font_description(Some(&font)); |
| 203 | layout.set_markup(&text); |
| 204 | |
| 205 | let (mut width, mut height) = layout.pixel_size(); |
| 206 | width += padding * 2; |
| 207 | height += padding * 2; |
| 208 | |
| 209 | |
| 210 | width = (width + scale - 1) / scale * scale; |
| 211 | height = (height + scale - 1) / scale * scale; |
| 212 | |
| 213 | let surface = ImageSurface::create(cairo::Format::ARgb32, width, height)?; |
| 214 | let cr = cairo::Context::new(&surface)?; |
| 215 | cr.set_source_rgb(0.1, 0.1, 0.1); |
| 216 | cr.paint()?; |
| 217 | |
| 218 | cr.move_to(padding.into(), padding.into()); |
| 219 | let layout = pangocairo::functions::create_layout(&cr); |
| 220 | layout.set_font_description(Some(&font)); |
| 221 | layout.set_markup(&text); |
| 222 | |
| 223 | cr.set_source_rgb(1., 1., 1.); |
| 224 | pangocairo::functions::show_layout(&cr, &layout); |
| 225 | |
| 226 | cr.move_to(0., 0.); |
| 227 | cr.line_to(width.into(), 0.); |
| 228 | cr.line_to(width.into(), height.into()); |
| 229 | cr.line_to(0., height.into()); |
| 230 | cr.line_to(0., 0.); |
| 231 | cr.set_source_rgb(border_color.0, border_color.1, border_color.2); |
| 232 | cr.set_line_width((BORDER * scale).into()); |
| 233 | cr.stroke()?; |
| 234 | drop(cr); |
| 235 | |
| 236 | let data = surface.take_data().unwrap(); |
| 237 | let buffer = MemoryRenderBuffer::from_slice( |
| 238 | &data, |
| 239 | Fourcc::Argb8888, |
| 240 | (width, height), |
| 241 | scale, |
| 242 | Transform::Normal, |
| 243 | None, |
| 244 | ); |
| 245 | |
| 246 | Ok(buffer) |
| 247 | } |