niri
Clone
https://git.tahinli.com/Tahinli/niri.git
0 watching·0 forks·Rust·GPL-3.0·12.1 MiB·64a9351·Ivan Molodetskikh·2 years ago·1319 commits
| 1 | use std::rc::Rc; |
| 2 | use std::time::Duration; |
| 3 | |
| 4 | use niri_config::{Color, CornerRadius, GradientInterpolation}; |
| 5 | use smithay::backend::allocator::Fourcc; |
| 6 | use smithay::backend::renderer::element::{Element, Kind}; |
| 7 | use smithay::backend::renderer::gles::GlesRenderer; |
| 8 | use smithay::utils::{Logical, Point, Rectangle, Scale, Size, Transform}; |
| 9 | |
| 10 | use super::focus_ring::{FocusRing, FocusRingRenderElement}; |
| 11 | use super::opening_window::{OpenAnimation, OpeningWindowRenderElement}; |
| 12 | use super::{ |
| 13 | LayoutElement, LayoutElementRenderElement, LayoutElementRenderSnapshot, Options, |
| 14 | RESIZE_ANIMATION_THRESHOLD, |
| 15 | }; |
| 16 | use crate::animation::Animation; |
| 17 | use crate::niri_render_elements; |
| 18 | use crate::render_helpers::border::BorderRenderElement; |
| 19 | use crate::render_helpers::clipped_surface::{ClippedSurfaceRenderElement, RoundedCornerDamage}; |
| 20 | use crate::render_helpers::damage::ExtraDamage; |
| 21 | use crate::render_helpers::renderer::NiriRenderer; |
| 22 | use crate::render_helpers::resize::ResizeRenderElement; |
| 23 | use crate::render_helpers::snapshot::RenderSnapshot; |
| 24 | use crate::render_helpers::solid_color::{SolidColorBuffer, SolidColorRenderElement}; |
| 25 | use crate::render_helpers::{render_to_encompassing_texture, RenderTarget}; |
| 26 | use crate::utils::transaction::Transaction; |
| 27 | |
| 28 | /// Toplevel window with decorations. |
| 29 | #[derive(Debug)] |
| 30 | pub struct Tile<W: LayoutElement> { |
| 31 | /// The toplevel window itself. |
| 32 | window: W, |
| 33 | |
| 34 | /// The border around the window. |
| 35 | border: FocusRing, |
| 36 | |
| 37 | /// The focus ring around the window. |
| 38 | /// |
| 39 | /// It's supposed to be on the Workspace, but for the sake of a nicer open animation it's |
| 40 | /// currently here. |
| 41 | focus_ring: FocusRing, |
| 42 | |
| 43 | /// Whether this tile is fullscreen. |
| 44 | /// |
| 45 | /// This will update only when the `window` actually goes fullscreen, rather than right away, |
| 46 | /// to avoid black backdrop flicker before the window has had a chance to resize. |
| 47 | is_fullscreen: bool, |
| 48 | |
| 49 | /// The black backdrop for fullscreen windows. |
| 50 | fullscreen_backdrop: SolidColorBuffer, |
| 51 | |
| 52 | /// The size we were requested to fullscreen into. |
| 53 | fullscreen_size: Size<f64, Logical>, |
| 54 | |
| 55 | /// The animation upon opening a window. |
| 56 | open_animation: Option<OpenAnimation>, |
| 57 | |
| 58 | /// The animation of the window resizing. |
| 59 | resize_animation: Option<ResizeAnimation>, |
| 60 | |
| 61 | /// The animation of a tile visually moving horizontally. |
| 62 | move_x_animation: Option<MoveAnimation>, |
| 63 | |
| 64 | /// The animation of a tile visually moving vertically. |
| 65 | move_y_animation: Option<MoveAnimation>, |
| 66 | |
| 67 | /// Snapshot of the last render for use in the close animation. |
| 68 | unmap_snapshot: Option<TileRenderSnapshot>, |
| 69 | |
| 70 | /// Extra damage for clipped surface corner radius changes. |
| 71 | rounded_corner_damage: RoundedCornerDamage, |
| 72 | |
| 73 | /// Scale of the output the tile is on (and rounds its sizes to). |
| 74 | scale: f64, |
| 75 | |
| 76 | /// Configurable properties of the layout. |
| 77 | pub options: Rc<Options>, |
| 78 | } |
| 79 | |
| 80 | niri_render_elements! { |
| 81 | TileRenderElement<R> => { |
| 82 | LayoutElement = LayoutElementRenderElement<R>, |
| 83 | FocusRing = FocusRingRenderElement, |
| 84 | SolidColor = SolidColorRenderElement, |
| 85 | Opening = OpeningWindowRenderElement, |
| 86 | Resize = ResizeRenderElement, |
| 87 | Border = BorderRenderElement, |
| 88 | ClippedSurface = ClippedSurfaceRenderElement<R>, |
| 89 | ExtraDamage = ExtraDamage, |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | type TileRenderSnapshot = |
| 94 | RenderSnapshot<TileRenderElement<GlesRenderer>, TileRenderElement<GlesRenderer>>; |
| 95 | |
| 96 | #[derive(Debug)] |
| 97 | struct ResizeAnimation { |
| 98 | anim: Animation, |
| 99 | size_from: Size<f64, Logical>, |
| 100 | snapshot: LayoutElementRenderSnapshot, |
| 101 | } |
| 102 | |
| 103 | #[derive(Debug)] |
| 104 | struct MoveAnimation { |
| 105 | anim: Animation, |
| 106 | from: f64, |
| 107 | } |
| 108 | |
| 109 | impl<W: LayoutElement> Tile<W> { |
| 110 | pub fn new(window: W, scale: f64, options: Rc<Options>) -> Self { |
| 111 | let rules = window.rules(); |
| 112 | let border_config = rules.border.resolve_against(options.border); |
| 113 | let focus_ring_config = rules.focus_ring.resolve_against(options.focus_ring.into()); |
| 114 | |
| 115 | Self { |
| 116 | window, |
| 117 | border: FocusRing::new(border_config.into()), |
| 118 | focus_ring: FocusRing::new(focus_ring_config.into()), |
| 119 | is_fullscreen: false, // FIXME: up-to-date fullscreen right away, but we need size. |
| 120 | fullscreen_backdrop: SolidColorBuffer::new((0., 0.), [0., 0., 0., 1.]), |
| 121 | fullscreen_size: Default::default(), |
| 122 | open_animation: None, |
| 123 | resize_animation: None, |
| 124 | move_x_animation: None, |
| 125 | move_y_animation: None, |
| 126 | unmap_snapshot: None, |
| 127 | rounded_corner_damage: Default::default(), |
| 128 | scale, |
| 129 | options, |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | pub fn update_config(&mut self, scale: f64, options: Rc<Options>) { |
| 134 | self.scale = scale; |
| 135 | self.options = options; |
| 136 | |
| 137 | let rules = self.window.rules(); |
| 138 | |
| 139 | let border_config = rules.border.resolve_against(self.options.border); |
| 140 | self.border.update_config(border_config.into()); |
| 141 | |
| 142 | let focus_ring_config = rules |
| 143 | .focus_ring |
| 144 | .resolve_against(self.options.focus_ring.into()); |
| 145 | self.focus_ring.update_config(focus_ring_config.into()); |
| 146 | } |
| 147 | |
| 148 | pub fn update_shaders(&mut self) { |
| 149 | self.border.update_shaders(); |
| 150 | self.focus_ring.update_shaders(); |
| 151 | } |
| 152 | |
| 153 | pub fn update_window(&mut self) { |
| 154 | // FIXME: remove when we can get a fullscreen size right away. |
| 155 | if self.fullscreen_size != Size::from((0., 0.)) { |
| 156 | self.is_fullscreen = self.window.is_fullscreen(); |
| 157 | } |
| 158 | |
| 159 | if let Some(animate_from) = self.window.take_animation_snapshot() { |
| 160 | let size_from = if let Some(resize) = self.resize_animation.take() { |
| 161 | // Compute like in animated_window_size(), but using the snapshot geometry (since |
| 162 | // the current one is already overwritten). |
| 163 | let mut size = animate_from.size; |
| 164 | |
| 165 | let val = resize.anim.value(); |
| 166 | let size_from = resize.size_from; |
| 167 | |
| 168 | size.w = size_from.w + (size.w - size_from.w) * val; |
| 169 | size.h = size_from.h + (size.h - size_from.h) * val; |
| 170 | |
| 171 | size |
| 172 | } else { |
| 173 | animate_from.size |
| 174 | }; |
| 175 | |
| 176 | let change = self.window.size().to_f64().to_point() - size_from.to_point(); |
| 177 | let change = f64::max(change.x.abs(), change.y.abs()); |
| 178 | if change > RESIZE_ANIMATION_THRESHOLD { |
| 179 | let anim = Animation::new(0., 1., 0., self.options.animations.window_resize.anim); |
| 180 | self.resize_animation = Some(ResizeAnimation { |
| 181 | anim, |
| 182 | size_from, |
| 183 | snapshot: animate_from, |
| 184 | }); |
| 185 | } else { |
| 186 | self.resize_animation = None; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | let rules = self.window.rules(); |
| 191 | let border_config = rules.border.resolve_against(self.options.border); |
| 192 | self.border.update_config(border_config.into()); |
| 193 | let focus_ring_config = rules |
| 194 | .focus_ring |
| 195 | .resolve_against(self.options.focus_ring.into()); |
| 196 | self.focus_ring.update_config(focus_ring_config.into()); |
| 197 | |
| 198 | let window_size = self.window_size(); |
| 199 | let radius = rules |
| 200 | .geometry_corner_radius |
| 201 | .unwrap_or_default() |
| 202 | .fit_to(window_size.w as f32, window_size.h as f32); |
| 203 | self.rounded_corner_damage.set_corner_radius(radius); |
| 204 | self.rounded_corner_damage.set_size(window_size); |
| 205 | } |
| 206 | |
| 207 | pub fn advance_animations(&mut self, current_time: Duration) { |
| 208 | if let Some(open) = &mut self.open_animation { |
| 209 | open.advance_animations(current_time); |
| 210 | if open.is_done() { |
| 211 | self.open_animation = None; |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | if let Some(resize) = &mut self.resize_animation { |
| 216 | resize.anim.set_current_time(current_time); |
| 217 | if resize.anim.is_done() { |
| 218 | self.resize_animation = None; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | if let Some(move_) = &mut self.move_x_animation { |
| 223 | move_.anim.set_current_time(current_time); |
| 224 | if move_.anim.is_done() { |
| 225 | self.move_x_animation = None; |
| 226 | } |
| 227 | } |
| 228 | if let Some(move_) = &mut self.move_y_animation { |
| 229 | move_.anim.set_current_time(current_time); |
| 230 | if move_.anim.is_done() { |
| 231 | self.move_y_animation = None; |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | pub fn are_animations_ongoing(&self) -> bool { |
| 237 | self.open_animation.is_some() |
| 238 | || self.resize_animation.is_some() |
| 239 | || self.move_x_animation.is_some() |
| 240 | || self.move_y_animation.is_some() |
| 241 | } |
| 242 | |
| 243 | pub fn update(&mut self, is_active: bool, view_rect: Rectangle<f64, Logical>) { |
| 244 | let rules = self.window.rules(); |
| 245 | |
| 246 | let draw_border_with_background = rules |
| 247 | .draw_border_with_background |
| 248 | .unwrap_or_else(|| !self.window.has_ssd()); |
| 249 | let border_width = self.effective_border_width().unwrap_or(0.); |
| 250 | let radius = if self.is_fullscreen { |
| 251 | CornerRadius::default() |
| 252 | } else { |
| 253 | rules |
| 254 | .geometry_corner_radius |
| 255 | .map_or(CornerRadius::default(), |radius| { |
| 256 | radius.expanded_by(border_width as f32) |
| 257 | }) |
| 258 | }; |
| 259 | self.border.update_render_elements( |
| 260 | self.animated_window_size(), |
| 261 | is_active, |
| 262 | !draw_border_with_background, |
| 263 | Rectangle::from_loc_and_size( |
| 264 | view_rect.loc - Point::from((border_width, border_width)), |
| 265 | view_rect.size, |
| 266 | ), |
| 267 | radius, |
| 268 | self.scale, |
| 269 | ); |
| 270 | |
| 271 | let draw_focus_ring_with_background = if self.effective_border_width().is_some() { |
| 272 | false |
| 273 | } else { |
| 274 | draw_border_with_background |
| 275 | }; |
| 276 | let radius = if self.is_fullscreen { |
| 277 | CornerRadius::default() |
| 278 | } else if self.effective_border_width().is_some() { |
| 279 | radius |
| 280 | } else { |
| 281 | rules.geometry_corner_radius.unwrap_or_default() |
| 282 | } |
| 283 | .expanded_by(self.focus_ring.width() as f32); |
| 284 | self.focus_ring.update_render_elements( |
| 285 | self.animated_tile_size(), |
| 286 | is_active, |
| 287 | !draw_focus_ring_with_background, |
| 288 | view_rect, |
| 289 | radius, |
| 290 | self.scale, |
| 291 | ); |
| 292 | } |
| 293 | |
| 294 | pub fn scale(&self) -> f64 { |
| 295 | self.scale |
| 296 | } |
| 297 | |
| 298 | pub fn render_offset(&self) -> Point<f64, Logical> { |
| 299 | let mut offset = Point::from((0., 0.)); |
| 300 | |
| 301 | if let Some(move_) = &self.move_x_animation { |
| 302 | offset.x += move_.from * move_.anim.value(); |
| 303 | } |
| 304 | if let Some(move_) = &self.move_y_animation { |
| 305 | offset.y += move_.from * move_.anim.value(); |
| 306 | } |
| 307 | |
| 308 | offset |
| 309 | } |
| 310 | |
| 311 | pub fn start_open_animation(&mut self) { |
| 312 | self.open_animation = Some(OpenAnimation::new(Animation::new( |
| 313 | 0., |
| 314 | 1., |
| 315 | 0., |
| 316 | self.options.animations.window_open.anim, |
| 317 | ))); |
| 318 | } |
| 319 | |
| 320 | pub fn resize_animation(&self) -> Option<&Animation> { |
| 321 | self.resize_animation.as_ref().map(|resize| &resize.anim) |
| 322 | } |
| 323 | |
| 324 | pub fn animate_move_from(&mut self, from: Point<f64, Logical>) { |
| 325 | self.animate_move_x_from(from.x); |
| 326 | self.animate_move_y_from(from.y); |
| 327 | } |
| 328 | |
| 329 | pub fn animate_move_x_from(&mut self, from: f64) { |
| 330 | self.animate_move_x_from_with_config(from, self.options.animations.window_movement.0); |
| 331 | } |
| 332 | |
| 333 | pub fn animate_move_x_from_with_config(&mut self, from: f64, config: niri_config::Animation) { |
| 334 | let current_offset = self.render_offset().x; |
| 335 | |
| 336 | // Preserve the previous config if ongoing. |
| 337 | let anim = self.move_x_animation.take().map(|move_| move_.anim); |
| 338 | let anim = anim |
| 339 | .map(|anim| anim.restarted(1., 0., 0.)) |
| 340 | .unwrap_or_else(|| Animation::new(1., 0., 0., config)); |
| 341 | |
| 342 | self.move_x_animation = Some(MoveAnimation { |
| 343 | anim, |
| 344 | from: from + current_offset, |
| 345 | }); |
| 346 | } |
| 347 | |
| 348 | pub fn animate_move_y_from(&mut self, from: f64) { |
| 349 | self.animate_move_y_from_with_config(from, self.options.animations.window_movement.0); |
| 350 | } |
| 351 | |
| 352 | pub fn animate_move_y_from_with_config(&mut self, from: f64, config: niri_config::Animation) { |
| 353 | let current_offset = self.render_offset().y; |
| 354 | |
| 355 | // Preserve the previous config if ongoing. |
| 356 | let anim = self.move_y_animation.take().map(|move_| move_.anim); |
| 357 | let anim = anim |
| 358 | .map(|anim| anim.restarted(1., 0., 0.)) |
| 359 | .unwrap_or_else(|| Animation::new(1., 0., 0., config)); |
| 360 | |
| 361 | self.move_y_animation = Some(MoveAnimation { |
| 362 | anim, |
| 363 | from: from + current_offset, |
| 364 | }); |
| 365 | } |
| 366 | |
| 367 | pub fn window(&self) -> &W { |
| 368 | &self.window |
| 369 | } |
| 370 | |
| 371 | pub fn window_mut(&mut self) -> &mut W { |
| 372 | &mut self.window |
| 373 | } |
| 374 | |
| 375 | pub fn into_window(self) -> W { |
| 376 | self.window |
| 377 | } |
| 378 | |
| 379 | pub fn is_fullscreen(&self) -> bool { |
| 380 | self.is_fullscreen |
| 381 | } |
| 382 | |
| 383 | /// Returns `None` if the border is hidden and `Some(width)` if it should be shown. |
| 384 | fn effective_border_width(&self) -> Option<f64> { |
| 385 | if self.is_fullscreen { |
| 386 | return None; |
| 387 | } |
| 388 | |
| 389 | if self.border.is_off() { |
| 390 | return None; |
| 391 | } |
| 392 | |
| 393 | Some(self.border.width()) |
| 394 | } |
| 395 | |
| 396 | /// Returns the location of the window's visual geometry within this Tile. |
| 397 | pub fn window_loc(&self) -> Point<f64, Logical> { |
| 398 | let mut loc = Point::from((0., 0.)); |
| 399 | |
| 400 | // In fullscreen, center the window in the given size. |
| 401 | if self.is_fullscreen { |
| 402 | let window_size = self.window_size(); |
| 403 | let target_size = self.fullscreen_size; |
| 404 | |
| 405 | // Windows aren't supposed to be larger than the fullscreen size, but in case we get |
| 406 | // one, leave it at the top-left as usual. |
| 407 | if window_size.w < target_size.w { |
| 408 | loc.x += (target_size.w - window_size.w) / 2.; |
| 409 | } |
| 410 | if window_size.h < target_size.h { |
| 411 | loc.y += (target_size.h - window_size.h) / 2.; |
| 412 | } |
| 413 | |
| 414 | // Round to physical pixels. |
| 415 | loc = loc |
| 416 | .to_physical_precise_round(self.scale) |
| 417 | .to_logical(self.scale); |
| 418 | } |
| 419 | |
| 420 | if let Some(width) = self.effective_border_width() { |
| 421 | loc += (width, width).into(); |
| 422 | } |
| 423 | |
| 424 | loc |
| 425 | } |
| 426 | |
| 427 | pub fn tile_size(&self) -> Size<f64, Logical> { |
| 428 | let mut size = self.window_size(); |
| 429 | |
| 430 | if self.is_fullscreen { |
| 431 | // Normally we'd just return the fullscreen size here, but this makes things a bit |
| 432 | // nicer if a fullscreen window is bigger than the fullscreen size for some reason. |
| 433 | size.w = f64::max(size.w, self.fullscreen_size.w); |
| 434 | size.h = f64::max(size.h, self.fullscreen_size.h); |
| 435 | return size; |
| 436 | } |
| 437 | |
| 438 | if let Some(width) = self.effective_border_width() { |
| 439 | size.w += width * 2.; |
| 440 | size.h += width * 2.; |
| 441 | } |
| 442 | |
| 443 | size |
| 444 | } |
| 445 | |
| 446 | pub fn window_size(&self) -> Size<f64, Logical> { |
| 447 | let mut size = self.window.size().to_f64(); |
| 448 | size = size |
| 449 | .to_physical_precise_round(self.scale) |
| 450 | .to_logical(self.scale); |
| 451 | size |
| 452 | } |
| 453 | |
| 454 | fn animated_window_size(&self) -> Size<f64, Logical> { |
| 455 | let mut size = self.window_size(); |
| 456 | |
| 457 | if let Some(resize) = &self.resize_animation { |
| 458 | let val = resize.anim.value(); |
| 459 | let size_from = resize.size_from.to_f64(); |
| 460 | |
| 461 | size.w = f64::max(1., size_from.w + (size.w - size_from.w) * val); |
| 462 | size.h = f64::max(1., size_from.h + (size.h - size_from.h) * val); |
| 463 | size = size |
| 464 | .to_physical_precise_round(self.scale) |
| 465 | .to_logical(self.scale); |
| 466 | } |
| 467 | |
| 468 | size |
| 469 | } |
| 470 | |
| 471 | fn animated_tile_size(&self) -> Size<f64, Logical> { |
| 472 | let mut size = self.animated_window_size(); |
| 473 | |
| 474 | if self.is_fullscreen { |
| 475 | // Normally we'd just return the fullscreen size here, but this makes things a bit |
| 476 | // nicer if a fullscreen window is bigger than the fullscreen size for some reason. |
| 477 | size.w = f64::max(size.w, self.fullscreen_size.w); |
| 478 | size.h = f64::max(size.h, self.fullscreen_size.h); |
| 479 | return size; |
| 480 | } |
| 481 | |
| 482 | if let Some(width) = self.effective_border_width() { |
| 483 | size.w += width * 2.; |
| 484 | size.h += width * 2.; |
| 485 | } |
| 486 | |
| 487 | size |
| 488 | } |
| 489 | |
| 490 | pub fn buf_loc(&self) -> Point<f64, Logical> { |
| 491 | let mut loc = Point::from((0., 0.)); |
| 492 | loc += self.window_loc(); |
| 493 | loc += self.window.buf_loc().to_f64(); |
| 494 | loc |
| 495 | } |
| 496 | |
| 497 | pub fn is_in_input_region(&self, mut point: Point<f64, Logical>) -> bool { |
| 498 | point -= self.window_loc().to_f64(); |
| 499 | self.window.is_in_input_region(point) |
| 500 | } |
| 501 | |
| 502 | pub fn is_in_activation_region(&self, point: Point<f64, Logical>) -> bool { |
| 503 | let activation_region = Rectangle::from_loc_and_size((0., 0.), self.tile_size()); |
| 504 | activation_region.contains(point) |
| 505 | } |
| 506 | |
| 507 | pub fn request_tile_size( |
| 508 | &mut self, |
| 509 | mut size: Size<f64, Logical>, |
| 510 | animate: bool, |
| 511 | transaction: Option<Transaction>, |
| 512 | ) { |
| 513 | // Can't go through effective_border_width() because we might be fullscreen. |
| 514 | if !self.border.is_off() { |
| 515 | let width = self.border.width(); |
| 516 | size.w = f64::max(1., size.w - width * 2.); |
| 517 | size.h = f64::max(1., size.h - width * 2.); |
| 518 | } |
| 519 | |
| 520 | // The size request has to be i32 unfortunately, due to Wayland. We floor here instead of |
| 521 | // round to avoid situations where proportionally-sized columns don't fit on the screen |
| 522 | // exactly. |
| 523 | self.window |
| 524 | .request_size(size.to_i32_floor(), animate, transaction); |
| 525 | } |
| 526 | |
| 527 | pub fn tile_width_for_window_width(&self, size: f64) -> f64 { |
| 528 | if self.border.is_off() { |
| 529 | size |
| 530 | } else { |
| 531 | size + self.border.width() * 2. |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | pub fn tile_height_for_window_height(&self, size: f64) -> f64 { |
| 536 | if self.border.is_off() { |
| 537 | size |
| 538 | } else { |
| 539 | size + self.border.width() * 2. |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | pub fn window_width_for_tile_width(&self, size: f64) -> f64 { |
| 544 | if self.border.is_off() { |
| 545 | size |
| 546 | } else { |
| 547 | size - self.border.width() * 2. |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | pub fn window_height_for_tile_height(&self, size: f64) -> f64 { |
| 552 | if self.border.is_off() { |
| 553 | size |
| 554 | } else { |
| 555 | size - self.border.width() * 2. |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | pub fn request_fullscreen(&mut self, size: Size<f64, Logical>) { |
| 560 | self.fullscreen_backdrop.resize(size); |
| 561 | self.fullscreen_size = size; |
| 562 | self.window.request_fullscreen(size.to_i32_round()); |
| 563 | } |
| 564 | |
| 565 | pub fn min_size(&self) -> Size<f64, Logical> { |
| 566 | let mut size = self.window.min_size().to_f64(); |
| 567 | |
| 568 | if let Some(width) = self.effective_border_width() { |
| 569 | size.w = f64::max(1., size.w); |
| 570 | size.h = f64::max(1., size.h); |
| 571 | |
| 572 | size.w += width * 2.; |
| 573 | size.h += width * 2.; |
| 574 | } |
| 575 | |
| 576 | size |
| 577 | } |
| 578 | |
| 579 | pub fn max_size(&self) -> Size<f64, Logical> { |
| 580 | let mut size = self.window.max_size().to_f64(); |
| 581 | |
| 582 | if let Some(width) = self.effective_border_width() { |
| 583 | if size.w > 0. { |
| 584 | size.w += width * 2.; |
| 585 | } |
| 586 | if size.h > 0. { |
| 587 | size.h += width * 2.; |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | size |
| 592 | } |
| 593 | |
| 594 | pub fn draw_border_with_background(&self) -> bool { |
| 595 | if self.effective_border_width().is_some() { |
| 596 | return false; |
| 597 | } |
| 598 | |
| 599 | self.window |
| 600 | .rules() |
| 601 | .draw_border_with_background |
| 602 | .unwrap_or_else(|| !self.window.has_ssd()) |
| 603 | } |
| 604 | |
| 605 | fn render_inner<R: NiriRenderer>( |
| 606 | &self, |
| 607 | renderer: &mut R, |
| 608 | location: Point<f64, Logical>, |
| 609 | scale: Scale<f64>, |
| 610 | focus_ring: bool, |
| 611 | target: RenderTarget, |
| 612 | ) -> impl Iterator<Item = TileRenderElement<R>> { |
| 613 | let _span = tracy_client::span!("Tile::render_inner"); |
| 614 | |
| 615 | let alpha = if self.is_fullscreen { |
| 616 | 1. |
| 617 | } else { |
| 618 | self.window.rules().opacity.unwrap_or(1.).clamp(0., 1.) |
| 619 | }; |
| 620 | |
| 621 | let window_loc = self.window_loc(); |
| 622 | let window_size = self.window_size().to_f64(); |
| 623 | let animated_window_size = self.animated_window_size(); |
| 624 | let window_render_loc = location + window_loc; |
| 625 | let area = Rectangle::from_loc_and_size(window_render_loc, animated_window_size); |
| 626 | |
| 627 | let rules = self.window.rules(); |
| 628 | let clip_to_geometry = !self.is_fullscreen && rules.clip_to_geometry == Some(true); |
| 629 | let radius = rules.geometry_corner_radius.unwrap_or_default(); |
| 630 | |
| 631 | // If we're resizing, try to render a shader, or a fallback. |
| 632 | let mut resize_shader = None; |
| 633 | let mut resize_popups = None; |
| 634 | let mut resize_fallback = None; |
| 635 | |
| 636 | if let Some(resize) = &self.resize_animation { |
| 637 | resize_popups = Some( |
| 638 | self.window |
| 639 | .render_popups(renderer, window_render_loc, scale, alpha, target) |
| 640 | .into_iter() |
| 641 | .map(Into::into), |
| 642 | ); |
| 643 | |
| 644 | if ResizeRenderElement::has_shader(renderer) { |
| 645 | let gles_renderer = renderer.as_gles_renderer(); |
| 646 | |
| 647 | if let Some(texture_from) = resize.snapshot.texture(gles_renderer, scale, target) { |
| 648 | let window_elements = self.window.render_normal( |
| 649 | gles_renderer, |
| 650 | Point::from((0., 0.)), |
| 651 | scale, |
| 652 | 1., |
| 653 | target, |
| 654 | ); |
| 655 | |
| 656 | let current = render_to_encompassing_texture( |
| 657 | gles_renderer, |
| 658 | scale, |
| 659 | Transform::Normal, |
| 660 | Fourcc::Abgr8888, |
| 661 | &window_elements, |
| 662 | ) |
| 663 | .map_err(|err| warn!("error rendering window to texture: {err:?}")) |
| 664 | .ok(); |
| 665 | |
| 666 | // Clip blocked-out resizes unconditionally because they use solid color render |
| 667 | // elements. |
| 668 | let clip_to_geometry = if target |
| 669 | .should_block_out(resize.snapshot.block_out_from) |
| 670 | && target.should_block_out(rules.block_out_from) |
| 671 | { |
| 672 | true |
| 673 | } else { |
| 674 | clip_to_geometry |
| 675 | }; |
| 676 | |
| 677 | if let Some((texture_current, _sync_point, texture_current_geo)) = current { |
| 678 | let elem = ResizeRenderElement::new( |
| 679 | area, |
| 680 | scale, |
| 681 | texture_from.clone(), |
| 682 | resize.snapshot.size, |
| 683 | (texture_current, texture_current_geo), |
| 684 | window_size, |
| 685 | resize.anim.value() as f32, |
| 686 | resize.anim.clamped_value().clamp(0., 1.) as f32, |
| 687 | radius, |
| 688 | clip_to_geometry, |
| 689 | alpha, |
| 690 | ); |
| 691 | // FIXME: with split popups, this will use the resize element ID for |
| 692 | // popups, but we want the real IDs. |
| 693 | self.window |
| 694 | .set_offscreen_element_id(Some(elem.id().clone())); |
| 695 | resize_shader = Some(elem.into()); |
| 696 | } |
| 697 | } |
| 698 | } |
| 699 | |
| 700 | if resize_shader.is_none() { |
| 701 | let fallback_buffer = SolidColorBuffer::new(area.size, [1., 0., 0., 1.]); |
| 702 | resize_fallback = Some( |
| 703 | SolidColorRenderElement::from_buffer( |
| 704 | &fallback_buffer, |
| 705 | area.loc, |
| 706 | alpha, |
| 707 | Kind::Unspecified, |
| 708 | ) |
| 709 | .into(), |
| 710 | ); |
| 711 | self.window.set_offscreen_element_id(None); |
| 712 | } |
| 713 | } |
| 714 | |
| 715 | // If we're not resizing, render the window itself. |
| 716 | let mut window_surface = None; |
| 717 | let mut window_popups = None; |
| 718 | let mut rounded_corner_damage = None; |
| 719 | if resize_shader.is_none() && resize_fallback.is_none() { |
| 720 | let window = self |
| 721 | .window |
| 722 | .render(renderer, window_render_loc, scale, alpha, target); |
| 723 | |
| 724 | let geo = Rectangle::from_loc_and_size(window_render_loc, window_size); |
| 725 | let radius = radius.fit_to(window_size.w as f32, window_size.h as f32); |
| 726 | |
| 727 | let clip_shader = ClippedSurfaceRenderElement::shader(renderer).cloned(); |
| 728 | let has_border_shader = BorderRenderElement::has_shader(renderer); |
| 729 | |
| 730 | if clip_to_geometry && clip_shader.is_some() { |
| 731 | let damage = self.rounded_corner_damage.element(); |
| 732 | rounded_corner_damage = Some(damage.with_location(window_render_loc).into()); |
| 733 | } |
| 734 | |
| 735 | window_surface = Some(window.normal.into_iter().map(move |elem| match elem { |
| 736 | LayoutElementRenderElement::Wayland(elem) => { |
| 737 | // If we should clip to geometry, render a clipped window. |
| 738 | if clip_to_geometry { |
| 739 | if let Some(shader) = clip_shader.clone() { |
| 740 | if ClippedSurfaceRenderElement::will_clip(&elem, scale, geo, radius) { |
| 741 | return ClippedSurfaceRenderElement::new( |
| 742 | elem, |
| 743 | scale, |
| 744 | geo, |
| 745 | shader.clone(), |
| 746 | radius, |
| 747 | ) |
| 748 | .into(); |
| 749 | } |
| 750 | } |
| 751 | } |
| 752 | |
| 753 | // Otherwise, render it normally. |
| 754 | LayoutElementRenderElement::Wayland(elem).into() |
| 755 | } |
| 756 | LayoutElementRenderElement::SolidColor(elem) => { |
| 757 | // In this branch we're rendering a blocked-out window with a solid |
| 758 | // color. We need to render it with a rounded corner shader even if |
| 759 | // clip_to_geometry is false, because in this case we're assuming that |
| 760 | // the unclipped window CSD already has corners rounded to the |
| 761 | // user-provided radius, so our blocked-out rendering should match that |
| 762 | // radius. |
| 763 | if radius != CornerRadius::default() && has_border_shader { |
| 764 | return BorderRenderElement::new( |
| 765 | geo.size, |
| 766 | Rectangle::from_loc_and_size((0., 0.), geo.size), |
| 767 | GradientInterpolation::default(), |
| 768 | Color::from_color32f(elem.color()), |
| 769 | Color::from_color32f(elem.color()), |
| 770 | 0., |
| 771 | Rectangle::from_loc_and_size((0., 0.), geo.size), |
| 772 | 0., |
| 773 | radius, |
| 774 | scale.x as f32, |
| 775 | ) |
| 776 | .with_location(geo.loc) |
| 777 | .into(); |
| 778 | } |
| 779 | |
| 780 | // Otherwise, render the solid color as is. |
| 781 | LayoutElementRenderElement::SolidColor(elem).into() |
| 782 | } |
| 783 | })); |
| 784 | |
| 785 | window_popups = Some(window.popups.into_iter().map(Into::into)); |
| 786 | } |
| 787 | |
| 788 | let rv = resize_popups |
| 789 | .into_iter() |
| 790 | .flatten() |
| 791 | .chain(resize_shader) |
| 792 | .chain(resize_fallback) |
| 793 | .chain(window_popups.into_iter().flatten()) |
| 794 | .chain(rounded_corner_damage) |
| 795 | .chain(window_surface.into_iter().flatten()); |
| 796 | |
| 797 | let elem = self.is_fullscreen.then(|| { |
| 798 | SolidColorRenderElement::from_buffer( |
| 799 | &self.fullscreen_backdrop, |
| 800 | location, |
| 801 | 1., |
| 802 | Kind::Unspecified, |
| 803 | ) |
| 804 | .into() |
| 805 | }); |
| 806 | let rv = rv.chain(elem); |
| 807 | |
| 808 | let elem = self.effective_border_width().map(|width| { |
| 809 | self.border |
| 810 | .render(renderer, location + Point::from((width, width))) |
| 811 | .map(Into::into) |
| 812 | }); |
| 813 | let rv = rv.chain(elem.into_iter().flatten()); |
| 814 | |
| 815 | let elem = focus_ring.then(|| self.focus_ring.render(renderer, location).map(Into::into)); |
| 816 | rv.chain(elem.into_iter().flatten()) |
| 817 | } |
| 818 | |
| 819 | pub fn render<R: NiriRenderer>( |
| 820 | &self, |
| 821 | renderer: &mut R, |
| 822 | location: Point<f64, Logical>, |
| 823 | scale: Scale<f64>, |
| 824 | focus_ring: bool, |
| 825 | target: RenderTarget, |
| 826 | ) -> impl Iterator<Item = TileRenderElement<R>> { |
| 827 | let _span = tracy_client::span!("Tile::render"); |
| 828 | |
| 829 | let mut open_anim_elem = None; |
| 830 | let mut window_elems = None; |
| 831 | |
| 832 | if let Some(open) = &self.open_animation { |
| 833 | let renderer = renderer.as_gles_renderer(); |
| 834 | let elements = |
| 835 | self.render_inner(renderer, Point::from((0., 0.)), scale, focus_ring, target); |
| 836 | let elements = elements.collect::<Vec<TileRenderElement<_>>>(); |
| 837 | match open.render(renderer, &elements, self.tile_size(), location, scale) { |
| 838 | Ok(elem) => { |
| 839 | self.window() |
| 840 | .set_offscreen_element_id(Some(elem.id().clone())); |
| 841 | open_anim_elem = Some(elem.into()); |
| 842 | } |
| 843 | Err(err) => { |
| 844 | warn!("error rendering window opening animation: {err:?}"); |
| 845 | } |
| 846 | } |
| 847 | } |
| 848 | |
| 849 | if open_anim_elem.is_none() { |
| 850 | self.window().set_offscreen_element_id(None); |
| 851 | window_elems = Some(self.render_inner(renderer, location, scale, focus_ring, target)); |
| 852 | } |
| 853 | |
| 854 | open_anim_elem |
| 855 | .into_iter() |
| 856 | .chain(window_elems.into_iter().flatten()) |
| 857 | } |
| 858 | |
| 859 | pub fn store_unmap_snapshot_if_empty( |
| 860 | &mut self, |
| 861 | renderer: &mut GlesRenderer, |
| 862 | scale: Scale<f64>, |
| 863 | ) { |
| 864 | if self.unmap_snapshot.is_some() { |
| 865 | return; |
| 866 | } |
| 867 | |
| 868 | self.unmap_snapshot = Some(self.render_snapshot(renderer, scale)); |
| 869 | } |
| 870 | |
| 871 | fn render_snapshot( |
| 872 | &self, |
| 873 | renderer: &mut GlesRenderer, |
| 874 | scale: Scale<f64>, |
| 875 | ) -> TileRenderSnapshot { |
| 876 | let _span = tracy_client::span!("Tile::render_snapshot"); |
| 877 | |
| 878 | let contents = self.render( |
| 879 | renderer, |
| 880 | Point::from((0., 0.)), |
| 881 | scale, |
| 882 | false, |
| 883 | RenderTarget::Output, |
| 884 | ); |
| 885 | |
| 886 | // A bit of a hack to render blocked out as for screencast, but I think it's fine here. |
| 887 | let blocked_out_contents = self.render( |
| 888 | renderer, |
| 889 | Point::from((0., 0.)), |
| 890 | scale, |
| 891 | false, |
| 892 | RenderTarget::Screencast, |
| 893 | ); |
| 894 | |
| 895 | RenderSnapshot { |
| 896 | contents: contents.collect(), |
| 897 | blocked_out_contents: blocked_out_contents.collect(), |
| 898 | block_out_from: self.window.rules().block_out_from, |
| 899 | size: self.animated_tile_size(), |
| 900 | texture: Default::default(), |
| 901 | blocked_out_texture: Default::default(), |
| 902 | } |
| 903 | } |
| 904 | |
| 905 | pub fn take_unmap_snapshot(&mut self) -> Option<TileRenderSnapshot> { |
| 906 | self.unmap_snapshot.take() |
| 907 | } |
| 908 | } |