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