| 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 ordered_float::NotNan; |
| 9 | use pangocairo::cairo::{self, ImageSurface}; |
| 10 | use pangocairo::pango::FontDescription; |
| 11 | use smithay::backend::renderer::element::Kind; |
| 12 | use smithay::backend::renderer::gles::{GlesRenderer, GlesTexture}; |
| 13 | use smithay::output::Output; |
| 14 | use smithay::reexports::gbm::Format as Fourcc; |
| 15 | use smithay::utils::{Point, Transform}; |
| 16 | |
| 17 | use crate::animation::Animation; |
| 18 | use crate::render_helpers::primary_gpu_texture::PrimaryGpuTextureRenderElement; |
| 19 | use crate::render_helpers::renderer::NiriRenderer; |
| 20 | use crate::render_helpers::texture::{TextureBuffer, TextureRenderElement}; |
| 21 | use crate::utils::{output_size, to_physical_precise_round}; |
| 22 | |
| 23 | const TEXT: &str = "Failed to parse the config file. \ |
| 24 | Please run <span face='monospace' bgcolor='#000000'>niri validate</span> \ |
| 25 | to see the errors."; |
| 26 | const PADDING: i32 = 8; |
| 27 | const FONT: &str = "sans 14px"; |
| 28 | const BORDER: i32 = 4; |
| 29 | |
| 30 | pub struct ConfigErrorNotification { |
| 31 | state: State, |
| 32 | buffers: RefCell<HashMap<NotNan<f64>, Option<TextureBuffer<GlesTexture>>>>, |
| 33 | |
| 34 | |
| 35 | |
| 36 | created_path: Option<PathBuf>, |
| 37 | |
| 38 | config: Rc<RefCell<Config>>, |
| 39 | } |
| 40 | |
| 41 | enum State { |
| 42 | Hidden, |
| 43 | Showing(Animation), |
| 44 | Shown(Duration), |
| 45 | Hiding(Animation), |
| 46 | } |
| 47 | |
| 48 | impl 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 | |
| 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 | |
| 98 | |
| 99 | |
| 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 | |
| 169 | fn 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 | |
| 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 | } |