niri
Clone
https://git.tahinli.com/Tahinli/niri.git
0 watching·0 forks·Rust·GPL-3.0·12.1 MiB·9d8f4f8·Ivan Molodetskikh·1 month ago·2997 commits
| 1 | use std::cell::RefCell; |
| 2 | use std::cmp::min; |
| 3 | use std::collections::HashMap; |
| 4 | use std::mem; |
| 5 | use std::rc::Rc; |
| 6 | use std::time::Duration; |
| 7 | |
| 8 | use anyhow::ensure; |
| 9 | use niri_config::{ |
| 10 | Action, Bind, BoundAction, Color, Config, CornerRadius, GradientInterpolation, Key, Modifiers, |
| 11 | MruDirection, MruFilter, MruScope, Trigger, |
| 12 | }; |
| 13 | use pango::FontDescription; |
| 14 | use pangocairo::cairo::{self, ImageSurface}; |
| 15 | use smithay::backend::allocator::Fourcc; |
| 16 | use smithay::backend::renderer::element::utils::{ |
| 17 | Relocate, RelocateRenderElement, RescaleRenderElement, |
| 18 | }; |
| 19 | use smithay::backend::renderer::element::Kind; |
| 20 | use smithay::backend::renderer::gles::{GlesRenderer, GlesTexture}; |
| 21 | use smithay::backend::renderer::Color32F; |
| 22 | use smithay::input::keyboard::Keysym; |
| 23 | use smithay::output::Output; |
| 24 | use smithay::utils::{Logical, Point, Rectangle, Scale, Size, Transform}; |
| 25 | |
| 26 | use crate::animation::{Animation, Clock}; |
| 27 | use crate::layout::focus_ring::{FocusRing, FocusRingRenderElement}; |
| 28 | use crate::layout::{Layout, LayoutElement as _, LayoutElementRenderElement}; |
| 29 | use crate::niri::Niri; |
| 30 | use crate::niri_render_elements; |
| 31 | use crate::render_helpers::border::BorderRenderElement; |
| 32 | use crate::render_helpers::clipped_surface::ClippedSurfaceRenderElement; |
| 33 | use crate::render_helpers::gradient_fade_texture::GradientFadeTextureRenderElement; |
| 34 | use crate::render_helpers::offscreen::{OffscreenBuffer, OffscreenRenderElement}; |
| 35 | use crate::render_helpers::primary_gpu_texture::PrimaryGpuTextureRenderElement; |
| 36 | use crate::render_helpers::renderer::NiriRenderer; |
| 37 | use crate::render_helpers::solid_color::{SolidColorBuffer, SolidColorRenderElement}; |
| 38 | use crate::render_helpers::texture::{TextureBuffer, TextureRenderElement}; |
| 39 | use crate::render_helpers::RenderCtx; |
| 40 | use crate::utils::{ |
| 41 | baba_is_float_offset, output_size, round_logical_in_physical, to_physical_precise_round, |
| 42 | with_toplevel_role, |
| 43 | }; |
| 44 | use crate::window::mapped::MappedId; |
| 45 | use crate::window::Mapped; |
| 46 | |
| 47 | #[cfg(test)] |
| 48 | mod tests; |
| 49 | |
| 50 | /// Windows up to this size don't get scaled further down. |
| 51 | const PREVIEW_MIN_SIZE: f64 = 16.; |
| 52 | |
| 53 | /// Border width on the selected window preview. |
| 54 | const BORDER: f64 = 2.; |
| 55 | |
| 56 | /// Gap from the window preview to the window title. |
| 57 | const TITLE_GAP: f64 = 14.; |
| 58 | |
| 59 | /// Gap between thumbnails. |
| 60 | const GAP: f64 = 16.; |
| 61 | |
| 62 | /// How much of the next window will always peek from the side of the screen. |
| 63 | const STRUT: f64 = 192.; |
| 64 | |
| 65 | /// Padding in the scope indication panel. |
| 66 | const PANEL_PADDING: i32 = 12; |
| 67 | |
| 68 | /// Border size of the scope indication panel. |
| 69 | const PANEL_BORDER: i32 = 4; |
| 70 | |
| 71 | /// Backdrop color behind the previews. |
| 72 | const BACKDROP_COLOR: Color32F = Color32F::new(0., 0., 0., 0.8); |
| 73 | |
| 74 | /// Font used to render the window titles. |
| 75 | const FONT: &str = "sans 14px"; |
| 76 | |
| 77 | /// Scopes in the order they are cycled through. |
| 78 | /// |
| 79 | /// Count must match one defined in `generate_scope_panels()`. |
| 80 | static SCOPE_CYCLE: [MruScope; 3] = [MruScope::All, MruScope::Workspace, MruScope::Output]; |
| 81 | |
| 82 | /// Window MRU traversal context. |
| 83 | #[derive(Debug)] |
| 84 | pub struct WindowMru { |
| 85 | /// Windows in MRU order. |
| 86 | thumbnails: Vec<Thumbnail>, |
| 87 | |
| 88 | /// Id of the currently selected window. |
| 89 | current_id: Option<MappedId>, |
| 90 | |
| 91 | /// Current scope. |
| 92 | scope: MruScope, |
| 93 | |
| 94 | /// Current filter. |
| 95 | app_id_filter: Option<String>, |
| 96 | } |
| 97 | |
| 98 | pub struct WindowMruUi { |
| 99 | state: UiState, |
| 100 | preset_opened_binds: Vec<Bind>, |
| 101 | dynamic_opened_binds: Vec<Bind>, |
| 102 | config: Rc<RefCell<Config>>, |
| 103 | } |
| 104 | |
| 105 | pub enum MruCloseRequest { |
| 106 | Cancel, |
| 107 | Confirm, |
| 108 | } |
| 109 | |
| 110 | niri_render_elements! { |
| 111 | ThumbnailRenderElement<R> => { |
| 112 | LayoutElement = LayoutElementRenderElement<R>, |
| 113 | ClippedSurface = ClippedSurfaceRenderElement<R>, |
| 114 | Border = BorderRenderElement, |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | niri_render_elements! { |
| 119 | WindowMruUiRenderElement<R> => { |
| 120 | SolidColor = SolidColorRenderElement, |
| 121 | TextureElement = PrimaryGpuTextureRenderElement, |
| 122 | GradientFadeElem = GradientFadeTextureRenderElement, |
| 123 | FocusRing = FocusRingRenderElement, |
| 124 | Offscreen = OffscreenRenderElement, |
| 125 | Thumbnail = RelocateRenderElement<RescaleRenderElement<ThumbnailRenderElement<R>>>, |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | enum UiState { |
| 130 | Open(Inner), |
| 131 | Closing { |
| 132 | inner: Inner, |
| 133 | anim: Animation, |
| 134 | }, |
| 135 | Closed { |
| 136 | /// Scope used when the UI was last opened. |
| 137 | previous_scope: MruScope, |
| 138 | }, |
| 139 | } |
| 140 | |
| 141 | /// State of an opened MRU UI. |
| 142 | struct Inner { |
| 143 | /// List of Window Ids to display in the MRU UI. |
| 144 | wmru: WindowMru, |
| 145 | |
| 146 | /// View position relative to the leftmost visible window. |
| 147 | view_pos: ViewPos, |
| 148 | |
| 149 | // If true, don't automatically move the current thumbnail in-view. Set on pointer motion. |
| 150 | freeze_view: bool, |
| 151 | |
| 152 | /// Animation clock. |
| 153 | clock: Clock, |
| 154 | |
| 155 | /// Current config. |
| 156 | config: Rc<RefCell<Config>>, |
| 157 | |
| 158 | /// Time when the UI should appear. |
| 159 | open_at: Duration, |
| 160 | |
| 161 | /// Output the UI was opened on. |
| 162 | output: Output, |
| 163 | |
| 164 | /// Scope panel textures. |
| 165 | scope_panel: RefCell<ScopePanel>, |
| 166 | |
| 167 | /// Backdrop buffers for each output. |
| 168 | backdrop_buffers: RefCell<HashMap<Output, SolidColorBuffer>>, |
| 169 | |
| 170 | /// Offscreen buffer for the closing fade animation on the main output. |
| 171 | offscreen: OffscreenBuffer, |
| 172 | } |
| 173 | |
| 174 | #[derive(Debug)] |
| 175 | enum ViewPos { |
| 176 | /// The view position is static. |
| 177 | Static(f64), |
| 178 | /// The view position is animating. |
| 179 | Animation(Animation), |
| 180 | } |
| 181 | |
| 182 | #[derive(Debug)] |
| 183 | struct MoveAnimation { |
| 184 | anim: Animation, |
| 185 | from: f64, |
| 186 | } |
| 187 | |
| 188 | type MruTexture = TextureBuffer<GlesTexture>; |
| 189 | |
| 190 | /// Cached title texture. |
| 191 | #[derive(Debug, Default)] |
| 192 | struct TitleTexture { |
| 193 | title: String, |
| 194 | scale: f64, |
| 195 | texture: Option<Option<MruTexture>>, |
| 196 | } |
| 197 | |
| 198 | /// Cached scope panel textures. |
| 199 | #[derive(Debug, Default)] |
| 200 | struct ScopePanel { |
| 201 | scale: f64, |
| 202 | textures: Option<Option<[MruTexture; 3]>>, |
| 203 | } |
| 204 | |
| 205 | #[derive(Debug)] |
| 206 | struct Thumbnail { |
| 207 | id: MappedId, |
| 208 | |
| 209 | /// Focus timestamp, if any. |
| 210 | timestamp: Option<Duration>, |
| 211 | /// Whether the window is on the current MRU workspace. |
| 212 | on_current_workspace: bool, |
| 213 | /// Whether the window is on the current MRU output. |
| 214 | on_current_output: bool, |
| 215 | |
| 216 | /// Cached app ID of the window. |
| 217 | /// |
| 218 | /// Currently not updated live to avoid having to refilter windows. |
| 219 | app_id: Option<String>, |
| 220 | /// Cached size of the window. |
| 221 | size: Size<i32, Logical>, |
| 222 | |
| 223 | clock: Clock, |
| 224 | config: niri_config::MruPreviews, |
| 225 | open_animation: Option<Animation>, |
| 226 | move_animation: Option<MoveAnimation>, |
| 227 | title_texture: RefCell<TitleTexture>, |
| 228 | background: RefCell<FocusRing>, |
| 229 | border: RefCell<FocusRing>, |
| 230 | } |
| 231 | |
| 232 | impl Thumbnail { |
| 233 | fn from_mapped(mapped: &Mapped, clock: Clock, config: niri_config::MruPreviews) -> Self { |
| 234 | let app_id = with_toplevel_role(mapped.toplevel(), |role| role.app_id.clone()); |
| 235 | |
| 236 | let background = FocusRing::new(niri_config::FocusRing { |
| 237 | off: false, |
| 238 | width: 0., |
| 239 | active_gradient: None, |
| 240 | ..Default::default() |
| 241 | }); |
| 242 | let border = FocusRing::new(niri_config::FocusRing { |
| 243 | off: false, |
| 244 | active_gradient: None, |
| 245 | ..Default::default() |
| 246 | }); |
| 247 | |
| 248 | Self { |
| 249 | id: mapped.id(), |
| 250 | timestamp: mapped.get_focus_timestamp(), |
| 251 | on_current_output: false, |
| 252 | on_current_workspace: false, |
| 253 | app_id, |
| 254 | size: mapped.size(), |
| 255 | clock, |
| 256 | config, |
| 257 | open_animation: None, |
| 258 | move_animation: None, |
| 259 | title_texture: Default::default(), |
| 260 | background: RefCell::new(background), |
| 261 | border: RefCell::new(border), |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | fn are_animations_ongoing(&self) -> bool { |
| 266 | self.open_animation.is_some() || self.move_animation.is_some() |
| 267 | } |
| 268 | |
| 269 | fn advance_animations(&mut self) { |
| 270 | self.open_animation.take_if(|a| a.is_done()); |
| 271 | self.move_animation.take_if(|a| a.anim.is_done()); |
| 272 | } |
| 273 | |
| 274 | /// Animate thumbnail motion from given location. |
| 275 | fn animate_move_from_with_config(&mut self, from: f64, config: niri_config::Animation) { |
| 276 | let current_offset = self.render_offset(); |
| 277 | |
| 278 | // Preserve the previous config if ongoing. |
| 279 | let anim = self.move_animation.take().map(|ma| ma.anim); |
| 280 | let anim = anim |
| 281 | .map(|anim| anim.restarted(1., 0., 0.)) |
| 282 | .unwrap_or_else(|| Animation::new(self.clock.clone(), 1., 0., 0., config)); |
| 283 | |
| 284 | self.move_animation = Some(MoveAnimation { |
| 285 | anim, |
| 286 | from: from + current_offset, |
| 287 | }); |
| 288 | } |
| 289 | |
| 290 | fn animate_open_with_config(&mut self, config: niri_config::Animation) { |
| 291 | self.open_animation = Some(Animation::new(self.clock.clone(), 0., 1., 0., config)); |
| 292 | } |
| 293 | |
| 294 | fn render_offset(&self) -> f64 { |
| 295 | self.move_animation |
| 296 | .as_ref() |
| 297 | .map(|ma| ma.from * ma.anim.value()) |
| 298 | .unwrap_or_default() |
| 299 | } |
| 300 | |
| 301 | fn update_window(&mut self, mapped: &Mapped) { |
| 302 | self.size = mapped.size(); |
| 303 | } |
| 304 | |
| 305 | fn preview_size(&self, output_size: Size<f64, Logical>, scale: f64) -> Size<f64, Logical> { |
| 306 | let max_height = f64::max(1., self.config.max_height); |
| 307 | let max_scale = f64::max(0.001, self.config.max_scale); |
| 308 | |
| 309 | let max_height = f64::min(max_height, output_size.h * max_scale); |
| 310 | let output_ratio = output_size.w / output_size.h; |
| 311 | let max_width = max_height * output_ratio; |
| 312 | |
| 313 | let size = self.size.to_f64(); |
| 314 | let min_scale = f64::min(1., PREVIEW_MIN_SIZE / f64::max(size.w, size.h)); |
| 315 | |
| 316 | let thumb_scale = f64::min(max_width / size.w, max_height / size.h); |
| 317 | let thumb_scale = f64::min(max_scale, thumb_scale); |
| 318 | let thumb_scale = f64::max(min_scale, thumb_scale); |
| 319 | let size = size.to_f64().upscale(thumb_scale); |
| 320 | |
| 321 | // Round to physical pixels. |
| 322 | size.to_physical_precise_round(scale).to_logical(scale) |
| 323 | } |
| 324 | |
| 325 | fn title_texture( |
| 326 | &self, |
| 327 | renderer: &mut GlesRenderer, |
| 328 | mapped: &Mapped, |
| 329 | scale: f64, |
| 330 | ) -> Option<MruTexture> { |
| 331 | with_toplevel_role(mapped.toplevel(), |role| { |
| 332 | role.title |
| 333 | .as_ref() |
| 334 | .and_then(|title| self.title_texture.borrow_mut().get(renderer, title, scale)) |
| 335 | }) |
| 336 | } |
| 337 | |
| 338 | #[allow(clippy::too_many_arguments)] |
| 339 | fn render<R: NiriRenderer>( |
| 340 | &self, |
| 341 | mut ctx: RenderCtx<R>, |
| 342 | config: &niri_config::RecentWindows, |
| 343 | mapped: &Mapped, |
| 344 | preview_geo: Rectangle<f64, Logical>, |
| 345 | scale: f64, |
| 346 | is_active: bool, |
| 347 | bob_y: f64, |
| 348 | push: &mut dyn FnMut(WindowMruUiRenderElement<R>), |
| 349 | ) { |
| 350 | let _span = tracy_client::span!("Thumbnail::render"); |
| 351 | |
| 352 | let round = move |logical: f64| round_logical_in_physical(scale, logical); |
| 353 | let padding = round(config.highlight.padding); |
| 354 | let title_gap = round(TITLE_GAP); |
| 355 | |
| 356 | let s = Scale::from(scale); |
| 357 | |
| 358 | let preview_alpha = self |
| 359 | .open_animation |
| 360 | .as_ref() |
| 361 | .map_or(1., |a| a.clamped_value() as f32) |
| 362 | .clamp(0., 1.); |
| 363 | |
| 364 | let bob_y = if mapped.rules().baba_is_float == Some(true) { |
| 365 | bob_y |
| 366 | } else { |
| 367 | 0. |
| 368 | }; |
| 369 | let bob_offset = Point::new(0., bob_y); |
| 370 | |
| 371 | // Clip thumbnails to their geometry. |
| 372 | let radius = if mapped.sizing_mode().is_normal() { |
| 373 | mapped.geometry_corner_radius() |
| 374 | } else { |
| 375 | CornerRadius::default() |
| 376 | }; |
| 377 | |
| 378 | let has_border_shader = BorderRenderElement::has_shader(ctx.renderer); |
| 379 | let clip_shader = ClippedSurfaceRenderElement::shader(ctx.renderer).cloned(); |
| 380 | let geo = Rectangle::from_size(self.size.to_f64()); |
| 381 | // FIXME: deduplicate code with Tile::render_inner() |
| 382 | let clip = move |elem| match elem { |
| 383 | LayoutElementRenderElement::Wayland(elem) => { |
| 384 | if let Some(shader) = clip_shader.clone() { |
| 385 | if ClippedSurfaceRenderElement::will_clip(&elem, s, geo, radius) { |
| 386 | let elem = |
| 387 | ClippedSurfaceRenderElement::new(elem, s, geo, shader.clone(), radius); |
| 388 | return ThumbnailRenderElement::ClippedSurface(elem); |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | // If we don't have the shader, render it normally. |
| 393 | let elem = LayoutElementRenderElement::Wayland(elem); |
| 394 | ThumbnailRenderElement::LayoutElement(elem) |
| 395 | } |
| 396 | LayoutElementRenderElement::SolidColor(elem) => { |
| 397 | // In this branch we're rendering a blocked-out window with a solid |
| 398 | // color. We need to render it with a rounded corner shader even if |
| 399 | // clip_to_geometry is false, because in this case we're assuming that |
| 400 | // the unclipped window CSD already has corners rounded to the |
| 401 | // user-provided radius, so our blocked-out rendering should match that |
| 402 | // radius. |
| 403 | if radius != CornerRadius::default() && has_border_shader { |
| 404 | return BorderRenderElement::new( |
| 405 | geo.size, |
| 406 | Rectangle::from_size(geo.size), |
| 407 | GradientInterpolation::default(), |
| 408 | Color::from_color32f(elem.color()), |
| 409 | Color::from_color32f(elem.color()), |
| 410 | 0., |
| 411 | Rectangle::from_size(geo.size), |
| 412 | 0., |
| 413 | radius, |
| 414 | scale as f32, |
| 415 | 1., |
| 416 | ) |
| 417 | .into(); |
| 418 | } |
| 419 | |
| 420 | // Otherwise, render the solid color as is. |
| 421 | LayoutElementRenderElement::SolidColor(elem).into() |
| 422 | } |
| 423 | elem @ LayoutElementRenderElement::BackgroundEffect(_) => { |
| 424 | // This is only used on popups for now. If subsurface blur is implemented, this |
| 425 | // will need to be handled somehow. |
| 426 | error!("background effect clipping is unimplemented"); |
| 427 | elem.into() |
| 428 | } |
| 429 | }; |
| 430 | |
| 431 | let downscale = move |elem| { |
| 432 | let thumb_scale = Scale { |
| 433 | x: preview_geo.size.w / geo.size.w, |
| 434 | y: preview_geo.size.h / geo.size.h, |
| 435 | }; |
| 436 | let offset = Point::new( |
| 437 | preview_geo.size.w - (geo.size.w * thumb_scale.x), |
| 438 | preview_geo.size.h - (geo.size.h * thumb_scale.y), |
| 439 | ) |
| 440 | .downscale(2.); |
| 441 | let elem = RescaleRenderElement::from_element(elem, Point::new(0, 0), thumb_scale); |
| 442 | let elem = RelocateRenderElement::from_element( |
| 443 | elem, |
| 444 | (preview_geo.loc + offset + bob_offset).to_physical_precise_round(scale), |
| 445 | Relocate::Relative, |
| 446 | ); |
| 447 | WindowMruUiRenderElement::Thumbnail(elem) |
| 448 | }; |
| 449 | |
| 450 | // FIXME: this could use mipmaps, for that it should be rendered through an offscreen. |
| 451 | mapped.render_normal(ctx.r(), Point::new(0., 0.), s, preview_alpha, &mut |elem| { |
| 452 | let elem = clip(elem); |
| 453 | let elem = downscale(elem); |
| 454 | push(elem) |
| 455 | }); |
| 456 | |
| 457 | let mut title_size = None; |
| 458 | let title_texture = self.title_texture(ctx.as_gles().renderer, mapped, scale); |
| 459 | let title_texture = title_texture.map(|texture| { |
| 460 | let mut size = texture.logical_size(); |
| 461 | size.w = f64::min(size.w, preview_geo.size.w); |
| 462 | title_size = Some(size); |
| 463 | (texture, size) |
| 464 | }); |
| 465 | |
| 466 | // Hide title for blocked-out windows, but only after computing the title size. This way, |
| 467 | // the background and the border won't have to oscillate in size between normal and |
| 468 | // screencast renders, causing excessive damage. |
| 469 | let should_block_out = ctx.target.should_block_out(mapped.rules().block_out_from); |
| 470 | let title_texture = title_texture.filter(|_| !should_block_out); |
| 471 | |
| 472 | if let Some((texture, size)) = title_texture { |
| 473 | // Clip from the right if it doesn't fit. |
| 474 | let src = Rectangle::from_size(size); |
| 475 | |
| 476 | let loc = preview_geo.loc |
| 477 | + Point::new( |
| 478 | (preview_geo.size.w - size.w) / 2., |
| 479 | preview_geo.size.h + title_gap, |
| 480 | ); |
| 481 | let loc = loc.to_physical_precise_round(scale).to_logical(scale); |
| 482 | let texture = TextureRenderElement::from_texture_buffer( |
| 483 | texture, |
| 484 | loc, |
| 485 | preview_alpha, |
| 486 | Some(src), |
| 487 | None, |
| 488 | Kind::Unspecified, |
| 489 | ); |
| 490 | |
| 491 | let ctx = ctx.as_gles(); |
| 492 | if let Some(program) = GradientFadeTextureRenderElement::shader(ctx.renderer) { |
| 493 | let elem = GradientFadeTextureRenderElement::new(texture, program); |
| 494 | push(WindowMruUiRenderElement::GradientFadeElem(elem)); |
| 495 | } else { |
| 496 | let elem = PrimaryGpuTextureRenderElement(texture); |
| 497 | push(WindowMruUiRenderElement::TextureElement(elem)); |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | let is_urgent = mapped.is_urgent(); |
| 502 | if is_active || is_urgent { |
| 503 | let padding = Point::new(padding, padding); |
| 504 | |
| 505 | let mut size = preview_geo.size; |
| 506 | size += padding.to_size().upscale(2.); |
| 507 | |
| 508 | if let Some(title_size) = title_size { |
| 509 | size.h += title_gap + title_size.h; |
| 510 | // Subtract half the padding so it looks more balanced visually. |
| 511 | size.h -= round(padding.y / 2.); |
| 512 | } |
| 513 | |
| 514 | // FIXME: gradient support (will require passing down correct view_rect). |
| 515 | let mut color = if is_urgent { |
| 516 | config.highlight.urgent_color |
| 517 | } else { |
| 518 | config.highlight.active_color |
| 519 | }; |
| 520 | if !is_active { |
| 521 | color *= 0.4; |
| 522 | } |
| 523 | |
| 524 | let radius = CornerRadius::from(config.highlight.corner_radius as f32); |
| 525 | |
| 526 | let loc = preview_geo.loc - padding; |
| 527 | |
| 528 | let mut background = self.background.borrow_mut(); |
| 529 | let mut config = *background.config(); |
| 530 | config.active_color = color; |
| 531 | background.update_config(config); |
| 532 | background.update_render_elements( |
| 533 | size, |
| 534 | true, |
| 535 | false, |
| 536 | false, |
| 537 | Rectangle::default(), |
| 538 | radius, |
| 539 | scale, |
| 540 | 0.5, |
| 541 | ); |
| 542 | background.render(ctx.renderer, loc, &mut |elem| { |
| 543 | push(WindowMruUiRenderElement::FocusRing(elem)) |
| 544 | }); |
| 545 | |
| 546 | let mut border = self.border.borrow_mut(); |
| 547 | let mut config = *border.config(); |
| 548 | config.off = !is_active; |
| 549 | config.width = round(BORDER); |
| 550 | config.active_color = color; |
| 551 | border.update_config(config); |
| 552 | border.set_thicken_corners(false); |
| 553 | border.update_render_elements( |
| 554 | size, |
| 555 | true, |
| 556 | true, |
| 557 | false, |
| 558 | Rectangle::default(), |
| 559 | radius.expanded_by(config.width as f32), |
| 560 | scale, |
| 561 | 1., |
| 562 | ); |
| 563 | |
| 564 | border.render(ctx.renderer, loc, &mut |elem| { |
| 565 | push(WindowMruUiRenderElement::FocusRing(elem)) |
| 566 | }); |
| 567 | } |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | impl WindowMru { |
| 572 | pub fn new(niri: &Niri) -> Self { |
| 573 | let Some(output) = niri.layout.active_output() else { |
| 574 | return Self { |
| 575 | thumbnails: Vec::new(), |
| 576 | current_id: None, |
| 577 | scope: MruScope::All, |
| 578 | app_id_filter: None, |
| 579 | }; |
| 580 | }; |
| 581 | |
| 582 | let config = niri.config.borrow().recent_windows.previews; |
| 583 | let mut thumbnails = Vec::new(); |
| 584 | for (mon, ws_idx, ws) in niri.layout.workspaces() { |
| 585 | let mon = mon.expect("an active output exists so all workspaces have a monitor"); |
| 586 | let on_current_output = mon.output() == output; |
| 587 | let on_current_workspace = on_current_output && mon.active_workspace_idx() == ws_idx; |
| 588 | |
| 589 | for mapped in ws.windows() { |
| 590 | let mut thumbnail = Thumbnail::from_mapped(mapped, niri.clock.clone(), config); |
| 591 | thumbnail.on_current_output = on_current_output; |
| 592 | thumbnail.on_current_workspace = on_current_workspace; |
| 593 | thumbnails.push(thumbnail); |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | thumbnails |
| 598 | .sort_by(|Thumbnail { timestamp: t1, .. }, Thumbnail { timestamp: t2, .. }| t2.cmp(t1)); |
| 599 | |
| 600 | let current_id = thumbnails.first().map(|t| t.id); |
| 601 | Self { |
| 602 | thumbnails, |
| 603 | current_id, |
| 604 | scope: MruScope::All, |
| 605 | app_id_filter: None, |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | pub fn is_empty(&self) -> bool { |
| 610 | self.thumbnails.is_empty() |
| 611 | } |
| 612 | |
| 613 | #[cfg(test)] |
| 614 | fn verify_invariants(&self) { |
| 615 | if let Some(id) = self.current_id { |
| 616 | assert!( |
| 617 | self.thumbnails().any(|thumbnail| thumbnail.id == id), |
| 618 | "current_id must be present in the current filtered thumbnail list", |
| 619 | ); |
| 620 | } else { |
| 621 | assert!( |
| 622 | self.thumbnails().next().is_none(), |
| 623 | "unset current_id must mean that the filtered thumbnail list is empty", |
| 624 | ); |
| 625 | } |
| 626 | } |
| 627 | |
| 628 | fn thumbnails(&self) -> impl DoubleEndedIterator<Item = &Thumbnail> { |
| 629 | let matches = match_filter(self.scope, self.app_id_filter.as_deref()); |
| 630 | self.thumbnails.iter().filter(move |t| matches(t)) |
| 631 | } |
| 632 | |
| 633 | fn thumbnails_mut(&mut self) -> impl DoubleEndedIterator<Item = &mut Thumbnail> { |
| 634 | let matches = match_filter(self.scope, self.app_id_filter.as_deref()); |
| 635 | self.thumbnails.iter_mut().filter(move |t| matches(t)) |
| 636 | } |
| 637 | |
| 638 | fn thumbnails_with_idx(&self) -> impl DoubleEndedIterator<Item = (usize, &Thumbnail)> { |
| 639 | let matches = match_filter(self.scope, self.app_id_filter.as_deref()); |
| 640 | self.thumbnails |
| 641 | .iter() |
| 642 | .enumerate() |
| 643 | .filter(move |(_, t)| matches(t)) |
| 644 | } |
| 645 | |
| 646 | fn are_animations_ongoing(&self) -> bool { |
| 647 | self.thumbnails.iter().any(|t| t.are_animations_ongoing()) |
| 648 | } |
| 649 | |
| 650 | fn advance_animations(&mut self) { |
| 651 | for thumbnail in &mut self.thumbnails { |
| 652 | thumbnail.advance_animations(); |
| 653 | } |
| 654 | } |
| 655 | |
| 656 | fn forward(&mut self) { |
| 657 | let Some(id) = self.current_id else { |
| 658 | return; |
| 659 | }; |
| 660 | |
| 661 | let next = self.thumbnails().skip_while(|t| t.id != id).nth(1); |
| 662 | self.current_id = Some(if let Some(next) = next { |
| 663 | next.id |
| 664 | } else { |
| 665 | // We wrapped around. |
| 666 | self.thumbnails().next().unwrap().id |
| 667 | }); |
| 668 | } |
| 669 | |
| 670 | fn backward(&mut self) { |
| 671 | let Some(id) = self.current_id else { |
| 672 | return; |
| 673 | }; |
| 674 | |
| 675 | let next = self.thumbnails().rev().skip_while(|t| t.id != id).nth(1); |
| 676 | self.current_id = Some(if let Some(next) = next { |
| 677 | next.id |
| 678 | } else { |
| 679 | // We wrapped around. |
| 680 | self.thumbnails().next_back().unwrap().id |
| 681 | }); |
| 682 | } |
| 683 | |
| 684 | fn set_current(&mut self, id: MappedId) { |
| 685 | if self.thumbnails().any(|thumbnail| thumbnail.id == id) { |
| 686 | self.current_id = Some(id); |
| 687 | } |
| 688 | } |
| 689 | |
| 690 | fn first_id(&self) -> Option<MappedId> { |
| 691 | self.thumbnails().next().map(|thumbnail| thumbnail.id) |
| 692 | } |
| 693 | |
| 694 | fn first(&mut self) { |
| 695 | self.current_id = self.first_id(); |
| 696 | } |
| 697 | |
| 698 | fn last(&mut self) { |
| 699 | let id = self.thumbnails().next_back().map(|thumbnail| thumbnail.id); |
| 700 | self.current_id = id; |
| 701 | } |
| 702 | |
| 703 | pub fn set_scope(&mut self, scope: MruScope) -> Option<MruScope> { |
| 704 | if self.scope == scope { |
| 705 | return None; |
| 706 | } |
| 707 | let rv = Some(self.scope); |
| 708 | |
| 709 | if let Some(id) = self.current_id { |
| 710 | let (current_idx, _) = self |
| 711 | .thumbnails_with_idx() |
| 712 | .find(|(_, thumbnail)| thumbnail.id == id) |
| 713 | .unwrap(); |
| 714 | |
| 715 | self.scope = scope; |
| 716 | |
| 717 | // Try to select the same, or the first thumbnail to the left. Failing that, select the |
| 718 | // first one to the right. |
| 719 | let mut id = self.first_id(); |
| 720 | |
| 721 | for (idx, thumbnail) in self.thumbnails_with_idx() { |
| 722 | if idx > current_idx { |
| 723 | break; |
| 724 | } |
| 725 | id = Some(thumbnail.id); |
| 726 | } |
| 727 | self.current_id = id; |
| 728 | } else { |
| 729 | self.scope = scope; |
| 730 | self.current_id = self.first_id(); |
| 731 | } |
| 732 | |
| 733 | rv |
| 734 | } |
| 735 | |
| 736 | pub fn set_filter(&mut self, filter: MruFilter) -> Option<Option<String>> { |
| 737 | if self.app_id_filter.is_some() == (filter == MruFilter::AppId) { |
| 738 | // Filter unchanged. |
| 739 | return None; |
| 740 | } |
| 741 | |
| 742 | if let Some(id) = self.current_id { |
| 743 | let (current_idx, current_thumbnail) = self |
| 744 | .thumbnails_with_idx() |
| 745 | .find(|(_, thumbnail)| thumbnail.id == id) |
| 746 | .unwrap(); |
| 747 | |
| 748 | let old = match filter { |
| 749 | MruFilter::All => { |
| 750 | let old = self.app_id_filter.take(); |
| 751 | Some(old.expect("verified by early return at the top")) |
| 752 | } |
| 753 | MruFilter::AppId => { |
| 754 | // If the current thumbnail is missing an app id, we can't set the filter. |
| 755 | let current = current_thumbnail.app_id.clone()?; |
| 756 | let old = self.app_id_filter.replace(current); |
| 757 | assert!(old.is_none(), "verified by early return at the top"); |
| 758 | None |
| 759 | } |
| 760 | }; |
| 761 | |
| 762 | // Try to select the same, or the first thumbnail to the left. Failing that, select the |
| 763 | // first one to the right. |
| 764 | let mut id = self.first_id(); |
| 765 | |
| 766 | for (idx, thumbnail) in self.thumbnails_with_idx() { |
| 767 | if idx > current_idx { |
| 768 | break; |
| 769 | } |
| 770 | id = Some(thumbnail.id); |
| 771 | } |
| 772 | self.current_id = id; |
| 773 | |
| 774 | Some(old) |
| 775 | } else { |
| 776 | match filter { |
| 777 | MruFilter::All => { |
| 778 | let old = self.app_id_filter.take(); |
| 779 | let old = old.expect("verified by early return at the top"); |
| 780 | self.current_id = self.first_id(); |
| 781 | Some(Some(old)) |
| 782 | } |
| 783 | MruFilter::AppId => { |
| 784 | // We don't have a current window to set the app id filter. |
| 785 | None |
| 786 | } |
| 787 | } |
| 788 | } |
| 789 | } |
| 790 | |
| 791 | fn idx_of(&self, id: MappedId) -> Option<usize> { |
| 792 | self.thumbnails.iter().position(|t| t.id == id) |
| 793 | } |
| 794 | |
| 795 | fn remove_by_idx(&mut self, idx: usize) -> Option<Thumbnail> { |
| 796 | let id = self.thumbnails[idx].id; |
| 797 | |
| 798 | // Try to pick a different window when removing the current one. |
| 799 | if self.current_id == Some(id) { |
| 800 | self.forward(); |
| 801 | } |
| 802 | |
| 803 | // If we're still on the same window, that means it's the last visible one. |
| 804 | if self.current_id == Some(id) { |
| 805 | self.current_id = None; |
| 806 | } |
| 807 | |
| 808 | Some(self.thumbnails.remove(idx)) |
| 809 | } |
| 810 | |
| 811 | /// Returns the thumbnail if it's visible to the left of the currently selected one. |
| 812 | fn thumbnail_left_of_current(&self, id: MappedId) -> Option<&Thumbnail> { |
| 813 | for thumbnail in self.thumbnails() { |
| 814 | if Some(thumbnail.id) == self.current_id { |
| 815 | // We found the current window first, so the queried one is *not* to the left. |
| 816 | return None; |
| 817 | } else if thumbnail.id == id { |
| 818 | // We found the queried window first, so the current one is to the right of it. |
| 819 | return Some(thumbnail); |
| 820 | } |
| 821 | } |
| 822 | None |
| 823 | } |
| 824 | } |
| 825 | |
| 826 | fn matches(scope: MruScope, app_id_filter: Option<&str>, thumbnail: &Thumbnail) -> bool { |
| 827 | let x = match scope { |
| 828 | MruScope::All => true, |
| 829 | MruScope::Output => thumbnail.on_current_output, |
| 830 | MruScope::Workspace => thumbnail.on_current_workspace, |
| 831 | }; |
| 832 | if !x { |
| 833 | return false; |
| 834 | } |
| 835 | |
| 836 | if let Some(app_id) = app_id_filter { |
| 837 | thumbnail.app_id.as_deref() == Some(app_id) |
| 838 | } else { |
| 839 | true |
| 840 | } |
| 841 | } |
| 842 | |
| 843 | fn match_filter(scope: MruScope, app_id_filter: Option<&str>) -> impl Fn(&Thumbnail) -> bool + '_ { |
| 844 | move |thumbnail| matches(scope, app_id_filter, thumbnail) |
| 845 | } |
| 846 | |
| 847 | impl ViewPos { |
| 848 | fn current(&self) -> f64 { |
| 849 | match self { |
| 850 | ViewPos::Static(pos) => *pos, |
| 851 | ViewPos::Animation(anim) => anim.value(), |
| 852 | } |
| 853 | } |
| 854 | |
| 855 | fn target(&self) -> f64 { |
| 856 | match self { |
| 857 | ViewPos::Static(pos) => *pos, |
| 858 | ViewPos::Animation(anim) => anim.to(), |
| 859 | } |
| 860 | } |
| 861 | |
| 862 | fn are_animations_ongoing(&self) -> bool { |
| 863 | match self { |
| 864 | ViewPos::Static(_) => false, |
| 865 | ViewPos::Animation(_) => true, |
| 866 | } |
| 867 | } |
| 868 | |
| 869 | fn advance_animations(&mut self) { |
| 870 | if let ViewPos::Animation(anim) = self { |
| 871 | if anim.is_done() { |
| 872 | *self = ViewPos::Static(anim.to()); |
| 873 | } |
| 874 | } |
| 875 | } |
| 876 | |
| 877 | fn animate_from_with_config( |
| 878 | &mut self, |
| 879 | from: f64, |
| 880 | config: niri_config::Animation, |
| 881 | clock: Clock, |
| 882 | ) { |
| 883 | // FIXME: also compute and use current velocity. |
| 884 | let anim = Animation::new(clock, self.current() + from, self.target(), 0., config); |
| 885 | *self = ViewPos::Animation(anim); |
| 886 | } |
| 887 | |
| 888 | fn offset(&mut self, delta: f64) { |
| 889 | match self { |
| 890 | ViewPos::Static(pos) => *pos += delta, |
| 891 | ViewPos::Animation(anim) => anim.offset(delta), |
| 892 | } |
| 893 | } |
| 894 | } |
| 895 | |
| 896 | impl WindowMruUi { |
| 897 | pub fn new(config: Rc<RefCell<Config>>) -> Self { |
| 898 | let mut rv = Self { |
| 899 | state: UiState::Closed { |
| 900 | previous_scope: MruScope::default(), |
| 901 | }, |
| 902 | preset_opened_binds: make_preset_opened_binds(), |
| 903 | dynamic_opened_binds: Vec::new(), |
| 904 | config, |
| 905 | }; |
| 906 | rv.update_binds(); |
| 907 | rv |
| 908 | } |
| 909 | |
| 910 | pub fn update_binds(&mut self) { |
| 911 | self.dynamic_opened_binds = make_dynamic_opened_binds(&self.config.borrow()); |
| 912 | } |
| 913 | |
| 914 | pub fn update_config(&mut self) { |
| 915 | let inner = match &mut self.state { |
| 916 | UiState::Open(inner) => inner, |
| 917 | UiState::Closing { inner, .. } => inner, |
| 918 | UiState::Closed { .. } => return, |
| 919 | }; |
| 920 | inner.update_config(); |
| 921 | } |
| 922 | |
| 923 | pub fn is_open(&self) -> bool { |
| 924 | matches!(self.state, UiState::Open { .. }) |
| 925 | } |
| 926 | |
| 927 | pub fn open(&mut self, clock: Clock, wmru: WindowMru, output: Output) { |
| 928 | if self.is_open() { |
| 929 | return; |
| 930 | } |
| 931 | |
| 932 | let open_delay = self.config.borrow().recent_windows.open_delay_ms; |
| 933 | let open_delay = Duration::from_millis(u64::from(open_delay)); |
| 934 | |
| 935 | let mut inner = Inner { |
| 936 | wmru, |
| 937 | view_pos: ViewPos::Static(0.), |
| 938 | freeze_view: false, |
| 939 | open_at: clock.now_unadjusted() + open_delay, |
| 940 | clock, |
| 941 | config: self.config.clone(), |
| 942 | output, |
| 943 | scope_panel: Default::default(), |
| 944 | backdrop_buffers: Default::default(), |
| 945 | offscreen: OffscreenBuffer::default(), |
| 946 | }; |
| 947 | inner.view_pos = ViewPos::Static(inner.compute_view_pos()); |
| 948 | |
| 949 | self.state = UiState::Open(inner); |
| 950 | } |
| 951 | |
| 952 | pub fn close(&mut self, close_request: MruCloseRequest) -> Option<MappedId> { |
| 953 | if !self.is_open() { |
| 954 | return None; |
| 955 | } |
| 956 | let state = mem::replace( |
| 957 | &mut self.state, |
| 958 | UiState::Closed { |
| 959 | previous_scope: MruScope::default(), |
| 960 | }, |
| 961 | ); |
| 962 | let UiState::Open(inner) = state else { |
| 963 | unreachable!(); |
| 964 | }; |
| 965 | |
| 966 | let response = match close_request { |
| 967 | MruCloseRequest::Cancel => None, |
| 968 | MruCloseRequest::Confirm => inner.wmru.current_id, |
| 969 | }; |
| 970 | |
| 971 | if !inner.is_fully_open() { |
| 972 | // Hasn't displayed yet, no need to fade out. |
| 973 | let UiState::Closed { previous_scope } = &mut self.state else { |
| 974 | unreachable!() |
| 975 | }; |
| 976 | *previous_scope = inner.wmru.scope; |
| 977 | return response; |
| 978 | } |
| 979 | |
| 980 | let config = self.config.borrow(); |
| 981 | let config = config.animations.recent_windows_close.0; |
| 982 | |
| 983 | let anim = Animation::new(inner.clock.clone(), 1., 0., 0., config); |
| 984 | self.state = UiState::Closing { inner, anim }; |
| 985 | response |
| 986 | } |
| 987 | |
| 988 | pub fn advance(&mut self, dir: MruDirection, filter: Option<MruFilter>) { |
| 989 | let UiState::Open(inner) = &mut self.state else { |
| 990 | return; |
| 991 | }; |
| 992 | inner.freeze_view = false; |
| 993 | |
| 994 | if let Some(filter) = filter { |
| 995 | inner.set_filter(filter); |
| 996 | } |
| 997 | |
| 998 | match dir { |
| 999 | MruDirection::Forward => inner.wmru.forward(), |
| 1000 | MruDirection::Backward => inner.wmru.backward(), |
| 1001 | } |
| 1002 | } |
| 1003 | |
| 1004 | pub fn set_scope(&mut self, scope: MruScope) { |
| 1005 | let UiState::Open(inner) = &mut self.state else { |
| 1006 | return; |
| 1007 | }; |
| 1008 | inner.freeze_view = false; |
| 1009 | inner.set_scope(scope); |
| 1010 | } |
| 1011 | |
| 1012 | pub fn cycle_scope(&mut self) { |
| 1013 | let UiState::Open(inner) = &mut self.state else { |
| 1014 | return; |
| 1015 | }; |
| 1016 | |
| 1017 | let scope = inner.wmru.scope; |
| 1018 | let scope = SCOPE_CYCLE |
| 1019 | .into_iter() |
| 1020 | .cycle() |
| 1021 | .skip_while(|s| *s != scope) |
| 1022 | .nth(1) |
| 1023 | .unwrap(); |
| 1024 | self.set_scope(scope); |
| 1025 | } |
| 1026 | |
| 1027 | pub fn pointer_motion(&mut self, pos_within_output: Point<f64, Logical>) -> Option<MappedId> { |
| 1028 | let UiState::Open(inner) = &mut self.state else { |
| 1029 | return None; |
| 1030 | }; |
| 1031 | // Don't handle pointer until the UI is visible. |
| 1032 | if !inner.is_fully_open() { |
| 1033 | return None; |
| 1034 | } |
| 1035 | |
| 1036 | inner.freeze_view = true; |
| 1037 | |
| 1038 | let id = inner.thumbnail_under(pos_within_output); |
| 1039 | if let Some(id) = id { |
| 1040 | inner.wmru.set_current(id); |
| 1041 | } |
| 1042 | id |
| 1043 | } |
| 1044 | |
| 1045 | pub fn first(&mut self) { |
| 1046 | let UiState::Open(inner) = &mut self.state else { |
| 1047 | return; |
| 1048 | }; |
| 1049 | inner.freeze_view = false; |
| 1050 | inner.wmru.first(); |
| 1051 | } |
| 1052 | |
| 1053 | pub fn last(&mut self) { |
| 1054 | let UiState::Open(inner) = &mut self.state else { |
| 1055 | return; |
| 1056 | }; |
| 1057 | inner.freeze_view = false; |
| 1058 | inner.wmru.last(); |
| 1059 | } |
| 1060 | |
| 1061 | pub fn scope(&self) -> MruScope { |
| 1062 | match &self.state { |
| 1063 | UiState::Closed { previous_scope, .. } => *previous_scope, |
| 1064 | UiState::Open(inner) | UiState::Closing { inner, .. } => inner.wmru.scope, |
| 1065 | } |
| 1066 | } |
| 1067 | |
| 1068 | pub fn current_window_id(&self) -> Option<MappedId> { |
| 1069 | let UiState::Open(inner) = &self.state else { |
| 1070 | return None; |
| 1071 | }; |
| 1072 | inner.wmru.current_id |
| 1073 | } |
| 1074 | |
| 1075 | pub fn update_window(&mut self, layout: &Layout<Mapped>, id: MappedId) { |
| 1076 | let UiState::Open(inner) = &mut self.state else { |
| 1077 | return; |
| 1078 | }; |
| 1079 | inner.update_window(layout, id); |
| 1080 | } |
| 1081 | |
| 1082 | pub fn remove_window(&mut self, id: MappedId) { |
| 1083 | let UiState::Open(inner) = &mut self.state else { |
| 1084 | return; |
| 1085 | }; |
| 1086 | |
| 1087 | let Some(_thumbnail) = inner.remove_window(id) else { |
| 1088 | return; |
| 1089 | }; |
| 1090 | |
| 1091 | if inner.wmru.thumbnails.is_empty() { |
| 1092 | self.close(MruCloseRequest::Cancel); |
| 1093 | } |
| 1094 | } |
| 1095 | |
| 1096 | pub fn render_output<R: NiriRenderer>( |
| 1097 | &self, |
| 1098 | niri: &Niri, |
| 1099 | output: &Output, |
| 1100 | mut ctx: RenderCtx<R>, |
| 1101 | push: &mut dyn FnMut(WindowMruUiRenderElement<R>), |
| 1102 | ) { |
| 1103 | let (inner, progress) = match &self.state { |
| 1104 | UiState::Closed { .. } => return, |
| 1105 | UiState::Closing { inner, anim } => (inner, anim.clamped_value()), |
| 1106 | UiState::Open(inner) => { |
| 1107 | if inner.is_fully_open() { |
| 1108 | (inner, 1.) |
| 1109 | } else { |
| 1110 | return; |
| 1111 | } |
| 1112 | } |
| 1113 | }; |
| 1114 | |
| 1115 | let _span = tracy_client::span!("WindowMruUi::render_output"); |
| 1116 | |
| 1117 | let alpha = progress.clamp(0., 1.) as f32; |
| 1118 | |
| 1119 | // Put a backdrop above the current desktop view to contrast the thumbnails. |
| 1120 | let mut buffers = inner.backdrop_buffers.borrow_mut(); |
| 1121 | let buffer = buffers.entry(output.clone()).or_default(); |
| 1122 | buffer.resize(output_size(output)); |
| 1123 | buffer.set_color(BACKDROP_COLOR); |
| 1124 | let render_backdrop = |alpha| { |
| 1125 | SolidColorRenderElement::from_buffer( |
| 1126 | buffer, |
| 1127 | Point::new(0., 0.), |
| 1128 | alpha, |
| 1129 | Kind::Unspecified, |
| 1130 | ) |
| 1131 | // Can't wrap into WindowMruUiRenderElement::SolidColor() right here since we have |
| 1132 | // different <R> generic in offscreen vs. normal path. |
| 1133 | }; |
| 1134 | |
| 1135 | // During the closing fade, use an offscreen to avoid transparent compositing artifacts. |
| 1136 | let mut pushed_offscreen = false; |
| 1137 | if *output == inner.output && alpha < 1. { |
| 1138 | let mut ctx = ctx.as_gles(); |
| 1139 | |
| 1140 | let mut elems = Vec::new(); |
| 1141 | inner.render(niri, ctx.r(), &mut |elem| elems.push(elem)); |
| 1142 | elems.push(WindowMruUiRenderElement::SolidColor(render_backdrop(1.))); |
| 1143 | |
| 1144 | let scale = output.current_scale().fractional_scale(); |
| 1145 | match inner |
| 1146 | .offscreen |
| 1147 | .render(ctx.renderer, Scale::from(scale), &elems) |
| 1148 | { |
| 1149 | Ok((elem, _sync, _data)) => { |
| 1150 | // FIXME: would be good to passthrough offscreen data to visible windows here. |
| 1151 | // As is, during the closing fade, windows from other workspaces stop receiving |
| 1152 | // frame callbacks. |
| 1153 | // |
| 1154 | // However, we need to refactor our offscreen data a bit to make this nicer. |
| 1155 | // Currently it supports a stack of offscreens, but not a several unrelated |
| 1156 | // offscreens showing the same window (possibly in addition to the window |
| 1157 | // itself). |
| 1158 | // |
| 1159 | // Anyhow, this is not very noticeable since Alt-Tab closing happens quickly. |
| 1160 | push(WindowMruUiRenderElement::Offscreen(elem.with_alpha(alpha))); |
| 1161 | pushed_offscreen = true; |
| 1162 | } |
| 1163 | Err(err) => { |
| 1164 | warn!("error rendering MRU to offscreen for fade-out: {err:?}"); |
| 1165 | } |
| 1166 | } |
| 1167 | } |
| 1168 | |
| 1169 | // When alpha is 1., render everything directly, without an offscreen. |
| 1170 | // |
| 1171 | // This is not used as fallback when offscreen fails to render because it looks better to |
| 1172 | // hide the previews immediately than to render them with alpha = 1. during a fade-out. |
| 1173 | if *output == inner.output && alpha == 1. { |
| 1174 | inner.render(niri, ctx, &mut |elem| push(elem)); |
| 1175 | } |
| 1176 | |
| 1177 | // This is used for both normal elems and for other outputs. |
| 1178 | if !pushed_offscreen { |
| 1179 | push(WindowMruUiRenderElement::SolidColor(render_backdrop(alpha))); |
| 1180 | } |
| 1181 | } |
| 1182 | |
| 1183 | pub fn are_animations_ongoing(&self) -> bool { |
| 1184 | match &self.state { |
| 1185 | UiState::Open(inner) => inner.are_animations_ongoing(), |
| 1186 | UiState::Closing { .. } => true, |
| 1187 | UiState::Closed { .. } => false, |
| 1188 | } |
| 1189 | } |
| 1190 | |
| 1191 | pub fn advance_animations(&mut self) { |
| 1192 | match &mut self.state { |
| 1193 | UiState::Open(inner) => inner.advance_animations(), |
| 1194 | UiState::Closing { inner, anim } => { |
| 1195 | if anim.is_done() { |
| 1196 | self.state = UiState::Closed { |
| 1197 | previous_scope: inner.wmru.scope, |
| 1198 | }; |
| 1199 | return; |
| 1200 | } |
| 1201 | inner.advance_animations(); |
| 1202 | } |
| 1203 | UiState::Closed { .. } => {} |
| 1204 | } |
| 1205 | } |
| 1206 | |
| 1207 | pub fn opened_bindings(&mut self, mods: Modifiers) -> impl Iterator<Item = &Bind> + Clone { |
| 1208 | // Fill modifiers with the current mods. |
| 1209 | for bind in &mut self.preset_opened_binds { |
| 1210 | bind.key.modifiers = mods; |
| 1211 | } |
| 1212 | for bind in &mut self.dynamic_opened_binds { |
| 1213 | bind.key.modifiers = mods; |
| 1214 | } |
| 1215 | |
| 1216 | self.preset_opened_binds |
| 1217 | .iter() |
| 1218 | .chain(&self.dynamic_opened_binds) |
| 1219 | } |
| 1220 | |
| 1221 | pub fn output(&self) -> Option<&Output> { |
| 1222 | match &self.state { |
| 1223 | UiState::Open(inner) => Some(&inner.output), |
| 1224 | _ => None, |
| 1225 | } |
| 1226 | } |
| 1227 | |
| 1228 | #[cfg(feature = "dbus")] |
| 1229 | pub fn a11y_scope_text(&self) -> String { |
| 1230 | let scope = match self.scope() { |
| 1231 | MruScope::All => "all", |
| 1232 | MruScope::Output => "output", |
| 1233 | MruScope::Workspace => "workspace", |
| 1234 | }; |
| 1235 | format!("Scope {scope}") |
| 1236 | } |
| 1237 | } |
| 1238 | |
| 1239 | fn compute_view_offset(cur_x: f64, working_width: f64, new_col_x: f64, new_col_width: f64) -> f64 { |
| 1240 | let new_x = new_col_x; |
| 1241 | let new_right_x = new_col_x + new_col_width; |
| 1242 | |
| 1243 | // If the column is already fully visible, leave the view as is. |
| 1244 | if cur_x <= new_x && new_right_x <= cur_x + working_width { |
| 1245 | return -(new_col_x - cur_x); |
| 1246 | } |
| 1247 | |
| 1248 | // Otherwise, prefer the alignment that results in less motion from the current position. |
| 1249 | let dist_to_left = (cur_x - new_x).abs(); |
| 1250 | let dist_to_right = ((cur_x + working_width) - new_right_x).abs(); |
| 1251 | if dist_to_left <= dist_to_right { |
| 1252 | 0. |
| 1253 | } else { |
| 1254 | -(working_width - new_col_width) |
| 1255 | } |
| 1256 | } |
| 1257 | |
| 1258 | impl Inner { |
| 1259 | fn update_config(&mut self) { |
| 1260 | self.freeze_view = false; |
| 1261 | |
| 1262 | let config = self.config.borrow().recent_windows.previews; |
| 1263 | for thumbnail in &mut self.wmru.thumbnails { |
| 1264 | thumbnail.config = config; |
| 1265 | } |
| 1266 | } |
| 1267 | |
| 1268 | fn are_animations_ongoing(&self) -> bool { |
| 1269 | self.clock.now_unadjusted() < self.open_at |
| 1270 | || self.view_pos.are_animations_ongoing() |
| 1271 | || self.wmru.are_animations_ongoing() |
| 1272 | } |
| 1273 | |
| 1274 | fn advance_animations(&mut self) { |
| 1275 | self.view_pos.advance_animations(); |
| 1276 | self.wmru.advance_animations(); |
| 1277 | |
| 1278 | if !self.freeze_view { |
| 1279 | let new_view_pos = self.compute_view_pos(); |
| 1280 | let delta = new_view_pos - self.view_pos.target(); |
| 1281 | let pixel = 1. / self.output.current_scale().fractional_scale(); |
| 1282 | if delta.abs() > pixel { |
| 1283 | self.animate_view_pos_from(-delta); |
| 1284 | } |
| 1285 | self.view_pos.offset(delta); |
| 1286 | } |
| 1287 | } |
| 1288 | |
| 1289 | fn is_fully_open(&self) -> bool { |
| 1290 | self.open_at <= self.clock.now_unadjusted() |
| 1291 | } |
| 1292 | |
| 1293 | fn animate_view_pos_from(&mut self, from: f64) { |
| 1294 | let config = self.config.borrow().animations.window_movement.0; |
| 1295 | self.view_pos |
| 1296 | .animate_from_with_config(from, config, self.clock.clone()); |
| 1297 | } |
| 1298 | |
| 1299 | fn compute_view_pos(&self) -> f64 { |
| 1300 | let Some(current_id) = self.wmru.current_id else { |
| 1301 | return 0.; |
| 1302 | }; |
| 1303 | |
| 1304 | let output_size = output_size(&self.output); |
| 1305 | |
| 1306 | let working_x = STRUT + GAP; |
| 1307 | let working_width = (output_size.w - working_x * 2.).max(0.); |
| 1308 | |
| 1309 | let mut current_geo = Rectangle::default(); |
| 1310 | let mut strip_width = 0.; |
| 1311 | for (thumbnail, geo) in self.thumbnails() { |
| 1312 | if thumbnail.id == current_id { |
| 1313 | current_geo = geo; |
| 1314 | } |
| 1315 | strip_width = geo.loc.x + geo.size.w; |
| 1316 | |
| 1317 | // If we found current_geo, and the strip width is already bigger than the working |
| 1318 | // width, no need to compute further. |
| 1319 | if current_geo.size.w != 0. && strip_width > working_width { |
| 1320 | break; |
| 1321 | } |
| 1322 | } |
| 1323 | |
| 1324 | // If the whole strip fits on screen, center it. |
| 1325 | if strip_width <= working_width { |
| 1326 | return -(output_size.w - strip_width) / 2.; |
| 1327 | } |
| 1328 | |
| 1329 | compute_view_offset( |
| 1330 | self.view_pos.target() + working_x, |
| 1331 | working_width, |
| 1332 | current_geo.loc.x, |
| 1333 | current_geo.size.w, |
| 1334 | ) + current_geo.loc.x |
| 1335 | - working_x |
| 1336 | } |
| 1337 | |
| 1338 | fn update_window(&mut self, layout: &Layout<Mapped>, id: MappedId) { |
| 1339 | let output_size = output_size(&self.output); |
| 1340 | let scale = self.output.current_scale().fractional_scale(); |
| 1341 | |
| 1342 | // If the updated window is to the left of the currently selected one, we need to offset |
| 1343 | // the view position to compensate for the change in size. |
| 1344 | let left = self.wmru.thumbnail_left_of_current(id); |
| 1345 | let prev_size = left.map(|thumbnail| thumbnail.preview_size(output_size, scale)); |
| 1346 | |
| 1347 | let Some(thumbnail) = self.wmru.thumbnails.iter_mut().find(|t| t.id == id) else { |
| 1348 | return; |
| 1349 | }; |
| 1350 | |
| 1351 | let Some((_, mapped)) = layout.windows().find(|(_, m)| m.id() == id) else { |
| 1352 | error!("window in the MRU must be present in the layout"); |
| 1353 | return; |
| 1354 | }; |
| 1355 | |
| 1356 | thumbnail.update_window(mapped); |
| 1357 | |
| 1358 | if let Some(prev) = prev_size { |
| 1359 | let new = thumbnail.preview_size(output_size, scale); |
| 1360 | let delta = new.w - prev.w; |
| 1361 | self.view_pos.offset(delta); |
| 1362 | } |
| 1363 | } |
| 1364 | |
| 1365 | fn remove_window(&mut self, id: MappedId) -> Option<Thumbnail> { |
| 1366 | let idx = self.wmru.idx_of(id)?; |
| 1367 | |
| 1368 | let last_visible = self.wmru.thumbnails().next_back(); |
| 1369 | let removing_last_visible = last_visible.is_some_and(|t| t.id == id); |
| 1370 | |
| 1371 | // When removing the last visible thumbnail, nothing needs to be animated. |
| 1372 | // - If it's not currently selected, then it can't cause changes to view position. |
| 1373 | // - If it's currently selected, then the first step in removal (focusing the next window) |
| 1374 | // will wrap back to the start, and no animations should happen. |
| 1375 | if !removing_last_visible { |
| 1376 | let output_size = output_size(&self.output); |
| 1377 | let scale = self.output.current_scale().fractional_scale(); |
| 1378 | let round = move |logical: f64| round_logical_in_physical(scale, logical); |
| 1379 | |
| 1380 | let padding = self.config.borrow().recent_windows.highlight.padding; |
| 1381 | let padding = round(padding) + round(BORDER); |
| 1382 | let gap = padding + round(GAP) + padding; |
| 1383 | |
| 1384 | let prev_size = self.wmru.thumbnails[idx].preview_size(output_size, scale); |
| 1385 | let delta = prev_size.w + gap; |
| 1386 | |
| 1387 | let config = self.config.borrow().animations.window_movement.0; |
| 1388 | |
| 1389 | // If the removed window is to the left of the currently selected one, we need to offset |
| 1390 | // the view position to compensate for the change. |
| 1391 | if self.wmru.thumbnail_left_of_current(id).is_some() { |
| 1392 | self.view_pos.offset(-delta); |
| 1393 | |
| 1394 | // And animate movement of windows left of it. |
| 1395 | for thumbnail in self.wmru.thumbnails_mut().take_while(|t| t.id != id) { |
| 1396 | thumbnail.animate_move_from_with_config(-delta, config); |
| 1397 | } |
| 1398 | } else { |
| 1399 | // Otherwise, animate movement of windows right of it. |
| 1400 | for thumbnail in self.wmru.thumbnails_mut().rev().take_while(|t| t.id != id) { |
| 1401 | thumbnail.animate_move_from_with_config(delta, config); |
| 1402 | } |
| 1403 | } |
| 1404 | } |
| 1405 | |
| 1406 | self.wmru.remove_by_idx(idx) |
| 1407 | } |
| 1408 | |
| 1409 | fn set_scope(&mut self, scope: MruScope) { |
| 1410 | let was_empty = self.wmru.current_id.is_none(); |
| 1411 | if let Some(old_scope) = self.wmru.set_scope(scope) { |
| 1412 | self.animate_scope_filter_change(was_empty, old_scope, None); |
| 1413 | } |
| 1414 | } |
| 1415 | |
| 1416 | fn set_filter(&mut self, filter: MruFilter) { |
| 1417 | let was_empty = self.wmru.current_id.is_none(); |
| 1418 | if let Some(old_filter) = self.wmru.set_filter(filter) { |
| 1419 | let old_filter = Some(old_filter.as_deref()); |
| 1420 | self.animate_scope_filter_change(was_empty, self.wmru.scope, old_filter); |
| 1421 | } |
| 1422 | } |
| 1423 | |
| 1424 | fn animate_scope_filter_change( |
| 1425 | &mut self, |
| 1426 | was_empty: bool, |
| 1427 | old_scope: MruScope, |
| 1428 | old_filter: Option<Option<&str>>, |
| 1429 | ) { |
| 1430 | let Some(id) = self.wmru.current_id else { |
| 1431 | // If there's no current_id then the new filter caused all windows to disappear, so |
| 1432 | // there's nothing to animate. |
| 1433 | return; |
| 1434 | }; |
| 1435 | let idx = self.wmru.idx_of(id).unwrap(); |
| 1436 | |
| 1437 | // Animate opening for newly appeared thumbnails. |
| 1438 | let config = self.config.borrow().animations.window_open.anim; |
| 1439 | let old_filter = old_filter.unwrap_or(self.wmru.app_id_filter.as_deref()); |
| 1440 | let matches_old = match_filter(old_scope, old_filter); |
| 1441 | let matches_new = match_filter(self.wmru.scope, self.wmru.app_id_filter.as_deref()); |
| 1442 | for thumbnail in &mut self.wmru.thumbnails { |
| 1443 | if matches_new(thumbnail) && !matches_old(thumbnail) { |
| 1444 | thumbnail.animate_open_with_config(config); |
| 1445 | } |
| 1446 | } |
| 1447 | |
| 1448 | if was_empty { |
| 1449 | self.view_pos = ViewPos::Static(self.compute_view_pos()); |
| 1450 | return; |
| 1451 | } |
| 1452 | |
| 1453 | let output_size = output_size(&self.output); |
| 1454 | let scale = self.output.current_scale().fractional_scale(); |
| 1455 | let round = move |logical: f64| round_logical_in_physical(scale, logical); |
| 1456 | |
| 1457 | let padding = self.config.borrow().recent_windows.highlight.padding; |
| 1458 | let padding = round(padding) + round(BORDER); |
| 1459 | let gap = padding + round(GAP) + padding; |
| 1460 | |
| 1461 | let config = self.config.borrow().animations.window_movement.0; |
| 1462 | |
| 1463 | let mut delta = 0.; |
| 1464 | for t in &mut self.wmru.thumbnails[idx + 1..] { |
| 1465 | match (matches_old(t), matches_new(t)) { |
| 1466 | (true, true) => t.animate_move_from_with_config(delta, config), |
| 1467 | (true, false) => delta += t.preview_size(output_size, scale).w + gap, |
| 1468 | (false, true) => delta -= t.preview_size(output_size, scale).w + gap, |
| 1469 | (false, false) => (), |
| 1470 | } |
| 1471 | } |
| 1472 | |
| 1473 | let mut delta = 0.; |
| 1474 | for t in self.wmru.thumbnails[..idx].iter_mut().rev() { |
| 1475 | match (matches_old(t), matches_new(t)) { |
| 1476 | (true, true) => t.animate_move_from_with_config(-delta, config), |
| 1477 | (true, false) => delta += t.preview_size(output_size, scale).w + gap, |
| 1478 | (false, true) => delta -= t.preview_size(output_size, scale).w + gap, |
| 1479 | (false, false) => (), |
| 1480 | } |
| 1481 | } |
| 1482 | |
| 1483 | self.view_pos.offset(-delta); |
| 1484 | } |
| 1485 | |
| 1486 | fn thumbnails(&self) -> impl Iterator<Item = (&Thumbnail, Rectangle<f64, Logical>)> { |
| 1487 | let output_size = output_size(&self.output); |
| 1488 | let scale = self.output.current_scale().fractional_scale(); |
| 1489 | let round = move |logical: f64| round_logical_in_physical(scale, logical); |
| 1490 | |
| 1491 | let padding = self.config.borrow().recent_windows.highlight.padding; |
| 1492 | let padding = round(padding) + round(BORDER); |
| 1493 | let gap = padding + round(GAP) + padding; |
| 1494 | |
| 1495 | let mut x = 0.; |
| 1496 | self.wmru.thumbnails().map(move |thumbnail| { |
| 1497 | let size = thumbnail.preview_size(output_size, scale); |
| 1498 | let y = round((output_size.h - size.h) / 2.); |
| 1499 | |
| 1500 | let loc = Point::new(x, y); |
| 1501 | x += size.w + gap; |
| 1502 | |
| 1503 | let geo = Rectangle::new(loc, size); |
| 1504 | (thumbnail, geo) |
| 1505 | }) |
| 1506 | } |
| 1507 | |
| 1508 | fn thumbnails_in_view_static( |
| 1509 | &self, |
| 1510 | ) -> impl Iterator<Item = (&Thumbnail, Rectangle<f64, Logical>)> { |
| 1511 | let output_size = output_size(&self.output); |
| 1512 | let scale = self.output.current_scale().fractional_scale(); |
| 1513 | let round = |logical: f64| round_logical_in_physical(scale, logical); |
| 1514 | |
| 1515 | let view_pos = round(self.view_pos.current()); |
| 1516 | |
| 1517 | let leftmost = view_pos; |
| 1518 | let rightmost = view_pos + output_size.w; |
| 1519 | |
| 1520 | self.thumbnails() |
| 1521 | .skip_while(move |(_, geo)| geo.loc.x + geo.size.w <= leftmost) |
| 1522 | .map_while(move |(thumbnail, mut geo)| { |
| 1523 | if rightmost <= geo.loc.x { |
| 1524 | return None; |
| 1525 | } |
| 1526 | |
| 1527 | geo.loc.x -= view_pos; |
| 1528 | Some((thumbnail, geo)) |
| 1529 | }) |
| 1530 | } |
| 1531 | |
| 1532 | fn thumbnails_in_view_render( |
| 1533 | &self, |
| 1534 | ) -> impl Iterator<Item = (&Thumbnail, Rectangle<f64, Logical>)> { |
| 1535 | let output_size = output_size(&self.output); |
| 1536 | let scale = self.output.current_scale().fractional_scale(); |
| 1537 | let round = move |logical: f64| round_logical_in_physical(scale, logical); |
| 1538 | |
| 1539 | let view_pos = round(self.view_pos.current()); |
| 1540 | |
| 1541 | self.thumbnails().filter_map(move |(thumbnail, mut geo)| { |
| 1542 | geo.loc.x -= view_pos; |
| 1543 | geo.loc.x += round(thumbnail.render_offset()); |
| 1544 | |
| 1545 | if geo.loc.x + geo.size.w < 0. || output_size.w < geo.loc.x { |
| 1546 | return None; |
| 1547 | } |
| 1548 | |
| 1549 | Some((thumbnail, geo)) |
| 1550 | }) |
| 1551 | } |
| 1552 | |
| 1553 | fn render<R: NiriRenderer>( |
| 1554 | &self, |
| 1555 | niri: &Niri, |
| 1556 | mut ctx: RenderCtx<R>, |
| 1557 | push: &mut dyn FnMut(WindowMruUiRenderElement<R>), |
| 1558 | ) { |
| 1559 | let output_size = output_size(&self.output); |
| 1560 | let scale = self.output.current_scale().fractional_scale(); |
| 1561 | |
| 1562 | let panel_texture = |
| 1563 | self.scope_panel |
| 1564 | .borrow_mut() |
| 1565 | .get(ctx.as_gles().renderer, scale, self.wmru.scope); |
| 1566 | if let Some(texture) = panel_texture { |
| 1567 | let padding = round_logical_in_physical(scale, f64::from(PANEL_PADDING)); |
| 1568 | |
| 1569 | let size = texture.logical_size(); |
| 1570 | let location = Point::new((output_size.w - size.w) / 2., padding * 2.); |
| 1571 | let elem = PrimaryGpuTextureRenderElement(TextureRenderElement::from_texture_buffer( |
| 1572 | texture.clone(), |
| 1573 | location, |
| 1574 | 1., |
| 1575 | None, |
| 1576 | None, |
| 1577 | Kind::Unspecified, |
| 1578 | )); |
| 1579 | push(WindowMruUiRenderElement::TextureElement(elem)); |
| 1580 | } |
| 1581 | |
| 1582 | let current_id = self.wmru.current_id; |
| 1583 | |
| 1584 | let bob_y = baba_is_float_offset(self.clock.now(), output_size.h); |
| 1585 | let bob_y = round_logical_in_physical(scale, bob_y); |
| 1586 | |
| 1587 | let config = self.config.borrow(); |
| 1588 | |
| 1589 | for (thumbnail, geo) in self.thumbnails_in_view_render() { |
| 1590 | let id = thumbnail.id; |
| 1591 | let Some((_, mapped)) = niri.layout.windows().find(|(_, m)| m.id() == id) else { |
| 1592 | error!("window in the MRU must be present in the layout"); |
| 1593 | continue; |
| 1594 | }; |
| 1595 | |
| 1596 | let config = &config.recent_windows; |
| 1597 | |
| 1598 | let is_active = Some(id) == current_id; |
| 1599 | thumbnail.render(ctx.r(), config, mapped, geo, scale, is_active, bob_y, push); |
| 1600 | } |
| 1601 | } |
| 1602 | |
| 1603 | fn thumbnail_under(&self, pos: Point<f64, Logical>) -> Option<MappedId> { |
| 1604 | let scale = self.output.current_scale().fractional_scale(); |
| 1605 | let round = move |logical: f64| round_logical_in_physical(scale, logical); |
| 1606 | let padding = self.config.borrow().recent_windows.highlight.padding; |
| 1607 | let padding = round(padding) + round(BORDER); |
| 1608 | let padding = Point::new(padding, padding); |
| 1609 | let title_gap = round(TITLE_GAP); |
| 1610 | |
| 1611 | for (thumbnail, mut geo) in self.thumbnails_in_view_static() { |
| 1612 | geo.loc -= padding; |
| 1613 | geo.size += padding.to_size().upscale(2.); |
| 1614 | |
| 1615 | // It doesn't really matter all that much if the title texture is stale here, and it |
| 1616 | // would be annoying to thread the rendering into this function. The texture might be |
| 1617 | // one frame stale or so. |
| 1618 | if let Some(texture) = thumbnail.title_texture.borrow().get_stale() { |
| 1619 | let title_size = texture.logical_size(); |
| 1620 | geo.size.h += title_gap + title_size.h; |
| 1621 | // Subtract half the padding so it looks more balanced visually. |
| 1622 | geo.size.h -= round(padding.y / 2.); |
| 1623 | } |
| 1624 | |
| 1625 | if geo.contains(pos) { |
| 1626 | return Some(thumbnail.id); |
| 1627 | } |
| 1628 | } |
| 1629 | |
| 1630 | None |
| 1631 | } |
| 1632 | } |
| 1633 | |
| 1634 | impl TitleTexture { |
| 1635 | fn get(&mut self, renderer: &mut GlesRenderer, title: &str, scale: f64) -> Option<MruTexture> { |
| 1636 | if self.title != title || self.scale != scale { |
| 1637 | self.texture = None; |
| 1638 | self.title = title.to_owned(); |
| 1639 | self.scale = scale; |
| 1640 | } |
| 1641 | |
| 1642 | self.texture |
| 1643 | .get_or_insert_with(|| generate_title_texture(renderer, title, scale).ok()) |
| 1644 | .clone() |
| 1645 | } |
| 1646 | |
| 1647 | fn get_stale(&self) -> Option<&MruTexture> { |
| 1648 | if let Some(Some(texture)) = &self.texture { |
| 1649 | Some(texture) |
| 1650 | } else { |
| 1651 | None |
| 1652 | } |
| 1653 | } |
| 1654 | } |
| 1655 | |
| 1656 | fn generate_title_texture( |
| 1657 | renderer: &mut GlesRenderer, |
| 1658 | title: &str, |
| 1659 | scale: f64, |
| 1660 | ) -> anyhow::Result<MruTexture> { |
| 1661 | let _span = tracy_client::span!("mru::generate_title_texture"); |
| 1662 | |
| 1663 | let mut font = FontDescription::from_string(FONT); |
| 1664 | font.set_absolute_size(to_physical_precise_round(scale, font.size())); |
| 1665 | |
| 1666 | let surface = ImageSurface::create(cairo::Format::ARgb32, 0, 0)?; |
| 1667 | let cr = cairo::Context::new(&surface)?; |
| 1668 | let layout = pangocairo::functions::create_layout(&cr); |
| 1669 | layout.context().set_round_glyph_positions(false); |
| 1670 | // On Window CSD, line breaks are either stripped or replaced with the linebreak symbol anyway. |
| 1671 | // No use rendering it as multiple lines. |
| 1672 | layout.set_single_paragraph_mode(true); |
| 1673 | layout.set_font_description(Some(&font)); |
| 1674 | layout.set_text(title); |
| 1675 | |
| 1676 | let (width, height) = layout.pixel_size(); |
| 1677 | ensure!(width > 0 && height > 0); |
| 1678 | |
| 1679 | // Guard against overly long window titles. |
| 1680 | let width = min(width, 16383); |
| 1681 | let height = min(height, 16383); |
| 1682 | |
| 1683 | let surface = ImageSurface::create(cairo::Format::ARgb32, width, height)?; |
| 1684 | let cr = cairo::Context::new(&surface)?; |
| 1685 | cr.set_source_rgb(1., 1., 1.); |
| 1686 | pangocairo::functions::show_layout(&cr, &layout); |
| 1687 | |
| 1688 | drop(cr); |
| 1689 | let data = surface.take_data().unwrap(); |
| 1690 | let buffer = TextureBuffer::from_memory( |
| 1691 | renderer, |
| 1692 | &data, |
| 1693 | Fourcc::Argb8888, |
| 1694 | (width, height), |
| 1695 | false, |
| 1696 | scale, |
| 1697 | Transform::Normal, |
| 1698 | Vec::new(), |
| 1699 | )?; |
| 1700 | |
| 1701 | Ok(buffer) |
| 1702 | } |
| 1703 | |
| 1704 | impl ScopePanel { |
| 1705 | fn get( |
| 1706 | &mut self, |
| 1707 | renderer: &mut GlesRenderer, |
| 1708 | scale: f64, |
| 1709 | scope: MruScope, |
| 1710 | ) -> Option<MruTexture> { |
| 1711 | if self.scale != scale { |
| 1712 | self.textures = None; |
| 1713 | self.scale = scale; |
| 1714 | } |
| 1715 | |
| 1716 | self.textures |
| 1717 | .get_or_insert_with(|| generate_scope_panels(renderer, scale).ok()) |
| 1718 | .as_ref() |
| 1719 | .map(|x| x[scope as usize].clone()) |
| 1720 | } |
| 1721 | } |
| 1722 | |
| 1723 | fn generate_scope_panels( |
| 1724 | renderer: &mut GlesRenderer, |
| 1725 | scale: f64, |
| 1726 | ) -> anyhow::Result<[MruTexture; 3]> { |
| 1727 | fn make_panel_text(idx: usize) -> String { |
| 1728 | let span_unselected = "<span fgcolor='#999999'>"; |
| 1729 | let span_end = "</span>"; |
| 1730 | let span_shortcut = "<span face='mono' bgcolor='#2C2C2C' letter_spacing='5000'><b>"; |
| 1731 | let span_shortcut_end = "</b></span>"; |
| 1732 | |
| 1733 | // Starts with a zero-width space to make letter_spacing work on the left. |
| 1734 | let mut buf = |
| 1735 | format!("\u{200B}{span_unselected}{span_shortcut}S{span_shortcut_end}cope:{span_end}"); |
| 1736 | |
| 1737 | for scope in SCOPE_CYCLE { |
| 1738 | buf.push_str(" "); |
| 1739 | if scope as usize != idx { |
| 1740 | buf.push_str(span_unselected); |
| 1741 | } |
| 1742 | let text = match scope { |
| 1743 | MruScope::All => format!("{span_shortcut}A{span_shortcut_end}ll"), |
| 1744 | MruScope::Output => format!("{span_shortcut}O{span_shortcut_end}utput"), |
| 1745 | MruScope::Workspace => format!("{span_shortcut}W{span_shortcut_end}orkspace"), |
| 1746 | }; |
| 1747 | buf.push_str(&text); |
| 1748 | if scope as usize != idx { |
| 1749 | buf.push_str(span_end); |
| 1750 | } |
| 1751 | } |
| 1752 | |
| 1753 | buf |
| 1754 | } |
| 1755 | |
| 1756 | // Can't wait for array::try_map() |
| 1757 | Ok([ |
| 1758 | render_panel(renderer, scale, &make_panel_text(0))?, |
| 1759 | render_panel(renderer, scale, &make_panel_text(1))?, |
| 1760 | render_panel(renderer, scale, &make_panel_text(2))?, |
| 1761 | ]) |
| 1762 | } |
| 1763 | |
| 1764 | fn render_panel(renderer: &mut GlesRenderer, scale: f64, text: &str) -> anyhow::Result<MruTexture> { |
| 1765 | let _span = tracy_client::span!("mru::render_panel"); |
| 1766 | |
| 1767 | let mut font = FontDescription::from_string(FONT); |
| 1768 | font.set_absolute_size(to_physical_precise_round(scale, font.size())); |
| 1769 | |
| 1770 | let padding: i32 = to_physical_precise_round(scale, PANEL_PADDING); |
| 1771 | // Keep the border width even to avoid blurry edges. |
| 1772 | // Render to a dummy surface to determine the size. |
| 1773 | let surface = ImageSurface::create(cairo::Format::ARgb32, 0, 0)?; |
| 1774 | let cr = cairo::Context::new(&surface)?; |
| 1775 | let layout = pangocairo::functions::create_layout(&cr); |
| 1776 | layout.context().set_round_glyph_positions(false); |
| 1777 | layout.set_font_description(Some(&font)); |
| 1778 | layout.set_markup(text); |
| 1779 | let (mut width, mut height) = layout.pixel_size(); |
| 1780 | |
| 1781 | width += padding * 2; |
| 1782 | height += padding * 2; |
| 1783 | |
| 1784 | let surface = ImageSurface::create(cairo::Format::ARgb32, width, height)?; |
| 1785 | let cr = cairo::Context::new(&surface)?; |
| 1786 | cr.set_source_rgb(0.1, 0.1, 0.1); |
| 1787 | cr.paint()?; |
| 1788 | |
| 1789 | let padding = f64::from(padding); |
| 1790 | |
| 1791 | cr.move_to(padding, padding); |
| 1792 | |
| 1793 | let layout = pangocairo::functions::create_layout(&cr); |
| 1794 | layout.context().set_round_glyph_positions(false); |
| 1795 | layout.set_font_description(Some(&font)); |
| 1796 | layout.set_markup(text); |
| 1797 | |
| 1798 | cr.set_source_rgb(1., 1., 1.); |
| 1799 | pangocairo::functions::show_layout(&cr, &layout); |
| 1800 | |
| 1801 | cr.move_to(0., 0.); |
| 1802 | cr.line_to(width.into(), 0.); |
| 1803 | cr.line_to(width.into(), height.into()); |
| 1804 | cr.line_to(0., height.into()); |
| 1805 | cr.line_to(0., 0.); |
| 1806 | cr.set_source_rgb(0.5, 0.5, 0.5); |
| 1807 | cr.set_line_width((f64::from(PANEL_BORDER) / 2. * scale).round() * 2.); |
| 1808 | cr.stroke()?; |
| 1809 | |
| 1810 | drop(cr); |
| 1811 | let data = surface.take_data().unwrap(); |
| 1812 | let buffer = TextureBuffer::from_memory( |
| 1813 | renderer, |
| 1814 | &data, |
| 1815 | Fourcc::Argb8888, |
| 1816 | (width, height), |
| 1817 | false, |
| 1818 | scale, |
| 1819 | Transform::Normal, |
| 1820 | Vec::new(), |
| 1821 | )?; |
| 1822 | |
| 1823 | Ok(buffer) |
| 1824 | } |
| 1825 | |
| 1826 | /// Returns key bindings available when the MRU UI is open. |
| 1827 | fn make_preset_opened_binds() -> Vec<Bind> { |
| 1828 | let mut rv = Vec::new(); |
| 1829 | |
| 1830 | let mut push = |trigger, action| { |
| 1831 | rv.push(Bind { |
| 1832 | key: Key { |
| 1833 | trigger: Trigger::Keysym(trigger), |
| 1834 | // The modifier is filled dynamically. |
| 1835 | modifiers: Modifiers::empty(), |
| 1836 | }, |
| 1837 | action: BoundAction::Press(action), |
| 1838 | repeat: true, |
| 1839 | cooldown: None, |
| 1840 | allow_when_locked: false, |
| 1841 | allow_inhibiting: false, |
| 1842 | hotkey_overlay_title: None, |
| 1843 | }) |
| 1844 | }; |
| 1845 | |
| 1846 | push(Keysym::Escape, Action::MruCancel); |
| 1847 | push(Keysym::Return, Action::MruConfirm); |
| 1848 | push(Keysym::space, Action::MruConfirm); |
| 1849 | push(Keysym::a, Action::MruSetScope(MruScope::All)); |
| 1850 | push(Keysym::o, Action::MruSetScope(MruScope::Output)); |
| 1851 | push(Keysym::w, Action::MruSetScope(MruScope::Workspace)); |
| 1852 | push(Keysym::s, Action::MruCycleScope); |
| 1853 | |
| 1854 | // Leave these in since they are the most expected and generally uncontroversial keys, so that |
| 1855 | // they work even if these actions are absent from the normal binds. |
| 1856 | push(Keysym::Home, Action::MruFirst); |
| 1857 | push(Keysym::End, Action::MruLast); |
| 1858 | push( |
| 1859 | Keysym::Left, |
| 1860 | Action::MruAdvance { |
| 1861 | direction: MruDirection::Backward, |
| 1862 | scope: None, |
| 1863 | filter: None, |
| 1864 | }, |
| 1865 | ); |
| 1866 | push( |
| 1867 | Keysym::Right, |
| 1868 | Action::MruAdvance { |
| 1869 | direction: MruDirection::Forward, |
| 1870 | scope: None, |
| 1871 | filter: None, |
| 1872 | }, |
| 1873 | ); |
| 1874 | |
| 1875 | rv |
| 1876 | } |
| 1877 | |
| 1878 | /// Returns dynamic key bindings available when the MRU UI is open. |
| 1879 | /// |
| 1880 | /// These ones are generated based on the normal bindings. |
| 1881 | fn make_dynamic_opened_binds(config: &Config) -> Vec<Bind> { |
| 1882 | let mut binds: HashMap<Trigger, Vec<Bind>> = HashMap::new(); |
| 1883 | |
| 1884 | for bind in &config.binds.0 { |
| 1885 | let Some(press_action) = bind.press_action() else { |
| 1886 | continue; |
| 1887 | }; |
| 1888 | |
| 1889 | // The idea with dynamic binds is that their modifiers are ignored |
| 1890 | // (set to whatever the user is currently holding). This idea does not |
| 1891 | // make sense with order-independent generic-modifier-only binds. |
| 1892 | // Effectively, they would trigger on every key press. |
| 1893 | if bind.key.trigger.is_generic_modifier() { |
| 1894 | continue; |
| 1895 | } |
| 1896 | |
| 1897 | let action = match press_action { |
| 1898 | Action::FocusColumnRight |
| 1899 | | Action::FocusColumnRightOrFirst |
| 1900 | | Action::FocusColumnOrMonitorRight |
| 1901 | | Action::FocusWindowDownOrColumnRight => Action::MruAdvance { |
| 1902 | direction: MruDirection::Forward, |
| 1903 | scope: None, |
| 1904 | filter: None, |
| 1905 | }, |
| 1906 | Action::FocusColumnLeft |
| 1907 | | Action::FocusColumnLeftOrLast |
| 1908 | | Action::FocusColumnOrMonitorLeft |
| 1909 | | Action::FocusWindowUpOrColumnLeft => Action::MruAdvance { |
| 1910 | direction: MruDirection::Backward, |
| 1911 | scope: None, |
| 1912 | filter: None, |
| 1913 | }, |
| 1914 | Action::FocusColumnFirst => Action::MruFirst, |
| 1915 | Action::FocusColumnLast => Action::MruLast, |
| 1916 | Action::CloseWindow => Action::MruCloseCurrentWindow, |
| 1917 | x @ Action::Screenshot(_, _) => x.clone(), |
| 1918 | _ => continue, |
| 1919 | }; |
| 1920 | |
| 1921 | binds.entry(bind.key.trigger).or_default().push(Bind { |
| 1922 | action: BoundAction::Press(action), |
| 1923 | ..bind.clone() |
| 1924 | }); |
| 1925 | } |
| 1926 | |
| 1927 | let mut rv = Vec::new(); |
| 1928 | |
| 1929 | // For each trigger, take the bind with the lowest number of modifiers. |
| 1930 | for binds in binds.into_values() { |
| 1931 | let bind = binds |
| 1932 | .into_iter() |
| 1933 | .min_by_key(|bind| bind.key.modifiers.iter().count()) |
| 1934 | .unwrap(); |
| 1935 | |
| 1936 | rv.push(Bind { |
| 1937 | key: Key { |
| 1938 | trigger: bind.key.trigger, |
| 1939 | // The modifier is filled dynamically. |
| 1940 | modifiers: Modifiers::empty(), |
| 1941 | }, |
| 1942 | ..bind |
| 1943 | }); |
| 1944 | } |
| 1945 | |
| 1946 | rv |
| 1947 | } |