niri
Clone
https://git.tahinli.com/Tahinli/niri.git
0 watching·0 forks·Rust·GPL-3.0·12.1 MiB·b0eb8ad·Ivan Molodetskikh·3 months ago·2857 commits
| 1 | use std::cell::{Cell, Ref, RefCell}; |
| 2 | use std::time::Duration; |
| 3 | |
| 4 | use niri_config::{Color, Config, CornerRadius, GradientInterpolation, WindowRule}; |
| 5 | use smithay::backend::renderer::element::surface::WaylandSurfaceRenderElement; |
| 6 | use smithay::backend::renderer::element::Kind; |
| 7 | use smithay::backend::renderer::gles::GlesRenderer; |
| 8 | use smithay::desktop::space::SpaceElement as _; |
| 9 | use smithay::desktop::{PopupKind, PopupManager, Window}; |
| 10 | use smithay::output::{self, Output}; |
| 11 | use smithay::reexports::wayland_protocols::xdg::decoration::zv1::server::zxdg_toplevel_decoration_v1; |
| 12 | use smithay::reexports::wayland_protocols::xdg::shell::server::xdg_toplevel; |
| 13 | use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface; |
| 14 | use smithay::reexports::wayland_server::Resource as _; |
| 15 | use smithay::utils::{Logical, Point, Rectangle, Scale, Serial, Size, Transform}; |
| 16 | use smithay::wayland::compositor::{remove_pre_commit_hook, with_states, HookId, SurfaceData}; |
| 17 | use smithay::wayland::seat::WaylandFocus; |
| 18 | use smithay::wayland::shell::xdg::{ |
| 19 | SurfaceCachedState, ToplevelCachedState, ToplevelConfigure, ToplevelSurface, |
| 20 | XdgToplevelSurfaceData, |
| 21 | }; |
| 22 | use wayland_backend::server::Credentials; |
| 23 | |
| 24 | use super::{ResolvedWindowRules, WindowRef}; |
| 25 | use crate::handlers::KdeDecorationsModeState; |
| 26 | use crate::layout::{ |
| 27 | ConfigureIntent, InteractiveResizeData, LayoutElement, LayoutElementRenderElement, |
| 28 | LayoutElementRenderSnapshot, SizingMode, |
| 29 | }; |
| 30 | use crate::niri_render_elements; |
| 31 | use crate::render_helpers::background_effect::BackgroundEffectElement; |
| 32 | use crate::render_helpers::border::BorderRenderElement; |
| 33 | use crate::render_helpers::offscreen::OffscreenData; |
| 34 | use crate::render_helpers::renderer::NiriRenderer; |
| 35 | use crate::render_helpers::snapshot::RenderSnapshot; |
| 36 | use crate::render_helpers::solid_color::{SolidColorBuffer, SolidColorRenderElement}; |
| 37 | use crate::render_helpers::surface::{ |
| 38 | push_elements_from_surface_tree, render_snapshot_from_surface_tree, |
| 39 | }; |
| 40 | use crate::render_helpers::xray::XrayPos; |
| 41 | use crate::render_helpers::{background_effect, BakedBuffer, RenderCtx, RenderTarget}; |
| 42 | use crate::utils::id::IdCounter; |
| 43 | use crate::utils::transaction::Transaction; |
| 44 | use crate::utils::{ |
| 45 | get_credentials_for_surface, send_scale_transform, update_tiled_state, |
| 46 | with_toplevel_last_uncommitted_configure, with_toplevel_role, with_toplevel_role_and_current, |
| 47 | ResizeEdge, |
| 48 | }; |
| 49 | |
| 50 | #[derive(Debug)] |
| 51 | pub struct Mapped { |
| 52 | pub window: Window, |
| 53 | |
| 54 | /// Unique ID of this `Mapped`. |
| 55 | id: MappedId, |
| 56 | |
| 57 | /// Credentials of the process that created the Wayland connection. |
| 58 | credentials: Option<Credentials>, |
| 59 | |
| 60 | /// Pre-commit hook that we have on all mapped toplevel surfaces. |
| 61 | pre_commit_hook: HookId, |
| 62 | |
| 63 | /// Up-to-date rules. |
| 64 | rules: ResolvedWindowRules, |
| 65 | |
| 66 | /// Whether the window rules need to be recomputed. |
| 67 | /// |
| 68 | /// This is not used in all cases; for example, app ID and title changes recompute the rules |
| 69 | /// immediately, rather than setting this flag. |
| 70 | need_to_recompute_rules: bool, |
| 71 | |
| 72 | /// Whether this window needs a configure this loop cycle. |
| 73 | /// |
| 74 | /// Certain Wayland requests require a configure in response, like un/fullscreen. |
| 75 | needs_configure: bool, |
| 76 | |
| 77 | /// Whether this window needs a frame callback. |
| 78 | /// |
| 79 | /// We set this after sending a configure to give invisible windows a chance to respond to |
| 80 | /// resizes immediately, without waiting for a 1 second throttled callback. |
| 81 | needs_frame_callback: bool, |
| 82 | |
| 83 | /// Data of the offscreen element rendered in place of this window. |
| 84 | /// |
| 85 | /// If `None`, then the window is not offscreened. |
| 86 | offscreen_data: RefCell<Option<OffscreenData>>, |
| 87 | |
| 88 | /// Whether this has an urgent indicator. |
| 89 | is_urgent: bool, |
| 90 | |
| 91 | /// Whether this window has the keyboard focus. |
| 92 | is_focused: bool, |
| 93 | |
| 94 | /// Whether this window is the active window in its column. |
| 95 | is_active_in_column: bool, |
| 96 | |
| 97 | /// Whether this window is floating. |
| 98 | is_floating: bool, |
| 99 | |
| 100 | /// Whether this window is a target of a window cast. |
| 101 | is_window_cast_target: bool, |
| 102 | |
| 103 | /// Whether this window should ignore opacity set through window rules. |
| 104 | ignore_opacity_window_rule: bool, |
| 105 | |
| 106 | /// Buffer to draw instead of the window when it should be blocked out. |
| 107 | block_out_buffer: RefCell<SolidColorBuffer>, |
| 108 | |
| 109 | /// The blur config, passed for background effect rendering. |
| 110 | blur_config: niri_config::Blur, |
| 111 | |
| 112 | /// Whether the next configure should be animated, if the configured state changed. |
| 113 | animate_next_configure: bool, |
| 114 | |
| 115 | /// Serials of commits that should be animated. |
| 116 | animate_serials: Vec<Serial>, |
| 117 | |
| 118 | /// Snapshot right before an animated commit, without popups. |
| 119 | animation_snapshot: Option<LayoutElementRenderSnapshot>, |
| 120 | |
| 121 | /// State for the logic to request a size once (for floating windows). |
| 122 | request_size_once: Option<RequestSizeOnce>, |
| 123 | |
| 124 | /// Transaction that the next configure should take part in, if any. |
| 125 | transaction_for_next_configure: Option<Transaction>, |
| 126 | |
| 127 | /// Pending transactions that have not been added as blockers for this window yet. |
| 128 | pending_transactions: Vec<(Serial, Transaction)>, |
| 129 | |
| 130 | /// State of an ongoing interactive resize. |
| 131 | interactive_resize: Option<InteractiveResize>, |
| 132 | |
| 133 | /// Last time interactive resize was started. |
| 134 | /// |
| 135 | /// Used for double-resize-click tracking. |
| 136 | last_interactive_resize_start: Cell<Option<(Duration, ResizeEdge)>>, |
| 137 | |
| 138 | /// Whether this window is in windowed (fake) fullscreen. |
| 139 | /// |
| 140 | /// In this mode, the underlying window is told that it's fullscreen, while keeping it as |
| 141 | /// a regular, non-fullscreen tile. |
| 142 | is_windowed_fullscreen: bool, |
| 143 | |
| 144 | /// Whether this window is pending to go to windowed (fake) fullscreen. |
| 145 | /// |
| 146 | /// Several places in the layout code assume that is_fullscreen() can flip only on a commit. |
| 147 | /// Which is something that we do want to flip when changing is_windowed_fullscreen. Flipping |
| 148 | /// it right away would mean remembering to call layout.update_window() after any operation |
| 149 | /// that may change is_windowed_fullscreen, which is quite tricky and error-prone, especially |
| 150 | /// for deeply nested operations. |
| 151 | /// |
| 152 | /// It's also not clear what's the best way to go about it. Ideally we'd wait for configure ack |
| 153 | /// and commit before "committing" to is_windowed_fullscreen, however, since it's not real |
| 154 | /// Wayland state, we may end up with no Wayland state change to configure at all. |
| 155 | /// |
| 156 | /// For example: when the window is in real fullscreen, but its non-fullscreen size matches |
| 157 | /// its fullscreen size. Then turning on is_windowed_fullscreen will both keep the |
| 158 | /// fullscreen state, and keep the size (since it matches), resulting in no configure. |
| 159 | /// |
| 160 | /// So we work around this by emulating a configure-ack/commit cycle through |
| 161 | /// is_pending_windowed_fullscreen and uncommitted_windowed_fullscreen. We ensure we send |
| 162 | /// actual configures in all cases through needs_configure. This can result in unnecessary |
| 163 | /// configures (like in the example above), but in most cases there will be a configure |
| 164 | /// anyway to change the Fullscreen state and/or the size. What this gives us is being able |
| 165 | /// to synchronize our windowed fullscreen state to the real window updates to avoid any |
| 166 | /// flickering. |
| 167 | is_pending_windowed_fullscreen: bool, |
| 168 | |
| 169 | /// Pending windowed fullscreen updates. |
| 170 | /// |
| 171 | /// These have been "sent" to the window in form of configures, but the window hadn't committed |
| 172 | /// in response yet. |
| 173 | uncommitted_windowed_fullscreen: Vec<(Serial, bool)>, |
| 174 | |
| 175 | /// Whether this window is maximized. |
| 176 | /// |
| 177 | /// We have to track this ourselves in addition to the Maximized toplevel state in order to |
| 178 | /// support windowed fullscreen, since in windowed fullscreen the toplevel state is always |
| 179 | /// Fullscreen. So we need this variable to be able to report accurate sizing mode and pending |
| 180 | /// sizing mode. |
| 181 | is_maximized: bool, |
| 182 | |
| 183 | /// Whether this window is pending to be maximized. |
| 184 | /// |
| 185 | /// We have to track this ourselves due to windowed fullscreen. |
| 186 | is_pending_maximized: bool, |
| 187 | |
| 188 | /// Pending maximized updates. |
| 189 | /// |
| 190 | /// These have been "sent" to the window in form of configures, but the window hadn't committed |
| 191 | /// in response yet. |
| 192 | uncommitted_maximized: Vec<(Serial, bool)>, |
| 193 | |
| 194 | /// Most recent monotonic time when the window had the focus. |
| 195 | focus_timestamp: Option<Duration>, |
| 196 | } |
| 197 | |
| 198 | niri_render_elements! { |
| 199 | WindowCastRenderElements<R> => { |
| 200 | Layout = LayoutElementRenderElement<R>, |
| 201 | // Blocked-out window with rounded corners. |
| 202 | Border = BorderRenderElement, |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | static MAPPED_ID_COUNTER: IdCounter = IdCounter::new(); |
| 207 | |
| 208 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| 209 | pub struct MappedId(u64); |
| 210 | |
| 211 | impl MappedId { |
| 212 | pub fn next() -> MappedId { |
| 213 | MappedId(MAPPED_ID_COUNTER.next()) |
| 214 | } |
| 215 | |
| 216 | pub fn get(self) -> u64 { |
| 217 | self.0 |
| 218 | } |
| 219 | |
| 220 | /// Converts the ID to a string that can be used as an identifier in |
| 221 | /// ext_foreign_toplevel_handle_v1::identifier |
| 222 | /// |
| 223 | /// > An identifier is a string that contains up to 32 printable ASCII bytes. |
| 224 | /// > An identifier must not be an empty string. |
| 225 | /// |
| 226 | /// Since the ID is exposed to IPC, it's useful for this conversion to be stable and reversible. |
| 227 | /// That way, clients can associate a foreign toplevel handle with an IPC window ID. |
| 228 | /// |
| 229 | /// We use the decimal representation of the ID, which is up to 20 characters long for u64::MAX. |
| 230 | /// This is within the 32-character limit, and is nice because it matches up with how `niri msg` |
| 231 | /// prints the IDs to the console. |
| 232 | /// |
| 233 | /// This namespace can be extended in the future, with any non-numeric prefix to disambiguate. |
| 234 | pub fn to_protocol_identifier(self) -> String { |
| 235 | format!("{}", self.0) |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | /// Interactive resize state. |
| 240 | #[derive(Debug)] |
| 241 | enum InteractiveResize { |
| 242 | /// The resize is ongoing. |
| 243 | Ongoing(InteractiveResizeData), |
| 244 | /// The resize has stopped and we're waiting to send the last configure. |
| 245 | WaitingForLastConfigure(InteractiveResizeData), |
| 246 | /// We had sent the last resize configure and are waiting for the corresponding commit. |
| 247 | WaitingForLastCommit { |
| 248 | data: InteractiveResizeData, |
| 249 | serial: Serial, |
| 250 | }, |
| 251 | } |
| 252 | |
| 253 | impl InteractiveResize { |
| 254 | fn data(&self) -> InteractiveResizeData { |
| 255 | match self { |
| 256 | InteractiveResize::Ongoing(data) => *data, |
| 257 | InteractiveResize::WaitingForLastConfigure(data) => *data, |
| 258 | InteractiveResize::WaitingForLastCommit { data, .. } => *data, |
| 259 | } |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | /// Request-size-once logic state. |
| 264 | #[derive(Debug, Clone, Copy)] |
| 265 | enum RequestSizeOnce { |
| 266 | /// Waiting for configure to be sent with the requested size. |
| 267 | WaitingForConfigure, |
| 268 | /// Waiting for the window to commit in response to the configure. |
| 269 | WaitingForCommit(Serial), |
| 270 | /// When configuring, use the current window size. |
| 271 | UseWindowSize, |
| 272 | } |
| 273 | |
| 274 | impl Mapped { |
| 275 | pub fn new(window: Window, rules: ResolvedWindowRules, hook: HookId, config: &Config) -> Self { |
| 276 | let surface = window.wl_surface().expect("no X11 support"); |
| 277 | let credentials = get_credentials_for_surface(&surface); |
| 278 | let mut rv = Self { |
| 279 | window, |
| 280 | id: MappedId::next(), |
| 281 | credentials, |
| 282 | pre_commit_hook: hook, |
| 283 | rules, |
| 284 | need_to_recompute_rules: false, |
| 285 | needs_configure: false, |
| 286 | needs_frame_callback: false, |
| 287 | offscreen_data: RefCell::new(None), |
| 288 | is_urgent: false, |
| 289 | is_focused: false, |
| 290 | is_active_in_column: true, |
| 291 | is_floating: false, |
| 292 | is_window_cast_target: false, |
| 293 | ignore_opacity_window_rule: false, |
| 294 | block_out_buffer: RefCell::new(SolidColorBuffer::new((0., 0.), [0., 0., 0., 1.])), |
| 295 | blur_config: config.blur, |
| 296 | animate_next_configure: false, |
| 297 | animate_serials: Vec::new(), |
| 298 | animation_snapshot: None, |
| 299 | request_size_once: None, |
| 300 | transaction_for_next_configure: None, |
| 301 | pending_transactions: Vec::new(), |
| 302 | interactive_resize: None, |
| 303 | last_interactive_resize_start: Cell::new(None), |
| 304 | is_windowed_fullscreen: false, |
| 305 | is_pending_windowed_fullscreen: false, |
| 306 | uncommitted_windowed_fullscreen: Vec::new(), |
| 307 | is_maximized: false, |
| 308 | is_pending_maximized: false, |
| 309 | uncommitted_maximized: Vec::new(), |
| 310 | focus_timestamp: None, |
| 311 | }; |
| 312 | |
| 313 | rv.is_maximized = rv.sizing_mode().is_maximized(); |
| 314 | rv.is_pending_maximized = rv.pending_sizing_mode().is_maximized(); |
| 315 | |
| 316 | rv |
| 317 | } |
| 318 | |
| 319 | pub fn toplevel(&self) -> &ToplevelSurface { |
| 320 | self.window.toplevel().expect("no X11 support") |
| 321 | } |
| 322 | |
| 323 | /// Recomputes the resolved window rules and returns whether they changed. |
| 324 | pub fn recompute_window_rules(&mut self, rules: &[WindowRule], is_at_startup: bool) -> bool { |
| 325 | self.need_to_recompute_rules = false; |
| 326 | |
| 327 | let new_rules = ResolvedWindowRules::compute(rules, WindowRef::Mapped(self), is_at_startup); |
| 328 | if new_rules == self.rules { |
| 329 | return false; |
| 330 | } |
| 331 | |
| 332 | // If the opacity window rule no longer makes the window semitransparent, reset the ignore |
| 333 | // flag to reduce surprises down the line. |
| 334 | if !new_rules.opacity.is_some_and(|o| o < 1.) { |
| 335 | self.ignore_opacity_window_rule = false; |
| 336 | } |
| 337 | |
| 338 | self.rules = new_rules; |
| 339 | true |
| 340 | } |
| 341 | |
| 342 | pub fn recompute_window_rules_if_needed( |
| 343 | &mut self, |
| 344 | rules: &[WindowRule], |
| 345 | is_at_startup: bool, |
| 346 | ) -> bool { |
| 347 | if !self.need_to_recompute_rules { |
| 348 | return false; |
| 349 | } |
| 350 | |
| 351 | self.recompute_window_rules(rules, is_at_startup) |
| 352 | } |
| 353 | |
| 354 | pub fn set_needs_configure(&mut self) { |
| 355 | self.needs_configure = true; |
| 356 | } |
| 357 | |
| 358 | pub fn id(&self) -> MappedId { |
| 359 | self.id |
| 360 | } |
| 361 | |
| 362 | pub fn credentials(&self) -> Option<&Credentials> { |
| 363 | self.credentials.as_ref() |
| 364 | } |
| 365 | |
| 366 | pub fn offscreen_data(&self) -> Ref<'_, Option<OffscreenData>> { |
| 367 | self.offscreen_data.borrow() |
| 368 | } |
| 369 | |
| 370 | pub fn is_focused(&self) -> bool { |
| 371 | self.is_focused |
| 372 | } |
| 373 | |
| 374 | pub fn is_active_in_column(&self) -> bool { |
| 375 | self.is_active_in_column |
| 376 | } |
| 377 | |
| 378 | pub fn is_floating(&self) -> bool { |
| 379 | self.is_floating |
| 380 | } |
| 381 | |
| 382 | pub fn is_window_cast_target(&self) -> bool { |
| 383 | self.is_window_cast_target |
| 384 | } |
| 385 | |
| 386 | pub fn toggle_ignore_opacity_window_rule(&mut self) { |
| 387 | self.ignore_opacity_window_rule = !self.ignore_opacity_window_rule; |
| 388 | } |
| 389 | |
| 390 | pub fn set_is_focused(&mut self, is_focused: bool) { |
| 391 | if self.is_focused == is_focused { |
| 392 | return; |
| 393 | } |
| 394 | |
| 395 | self.is_focused = is_focused; |
| 396 | self.is_urgent = false; |
| 397 | self.need_to_recompute_rules = true; |
| 398 | } |
| 399 | |
| 400 | pub fn set_is_window_cast_target(&mut self, value: bool) { |
| 401 | if self.is_window_cast_target == value { |
| 402 | return; |
| 403 | } |
| 404 | |
| 405 | self.is_window_cast_target = value; |
| 406 | self.need_to_recompute_rules = true; |
| 407 | } |
| 408 | |
| 409 | /// Renders a snapshot of the window without popups. |
| 410 | fn render_snapshot(&self, renderer: &mut GlesRenderer) -> LayoutElementRenderSnapshot { |
| 411 | let _span = tracy_client::span!("Mapped::render_snapshot"); |
| 412 | |
| 413 | let size = self.size().to_f64(); |
| 414 | |
| 415 | let mut buffer = self.block_out_buffer.borrow_mut(); |
| 416 | buffer.resize(size); |
| 417 | let blocked_out_contents = vec![BakedBuffer { |
| 418 | buffer: buffer.clone(), |
| 419 | location: Point::from((0., 0.)), |
| 420 | src: None, |
| 421 | dst: None, |
| 422 | }]; |
| 423 | |
| 424 | let buf_pos = self.window.geometry().loc.upscale(-1).to_f64(); |
| 425 | |
| 426 | let mut contents = vec![]; |
| 427 | |
| 428 | let surface = self.toplevel().wl_surface(); |
| 429 | render_snapshot_from_surface_tree(renderer, surface, buf_pos, &mut contents); |
| 430 | |
| 431 | RenderSnapshot { |
| 432 | contents, |
| 433 | contents_with_blocked_out_bg: None, |
| 434 | blocked_out_contents, |
| 435 | block_out_from: self.rules().block_out_from, |
| 436 | size, |
| 437 | texture: Default::default(), |
| 438 | texture_with_blocked_out_bg: Default::default(), |
| 439 | blocked_out_texture: Default::default(), |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | pub fn should_animate_commit(&mut self, commit_serial: Serial) -> bool { |
| 444 | let mut should_animate = false; |
| 445 | self.animate_serials.retain_mut(|serial| { |
| 446 | if commit_serial.is_no_older_than(serial) { |
| 447 | should_animate = true; |
| 448 | false |
| 449 | } else { |
| 450 | true |
| 451 | } |
| 452 | }); |
| 453 | should_animate |
| 454 | } |
| 455 | |
| 456 | pub fn store_animation_snapshot(&mut self, renderer: &mut GlesRenderer) { |
| 457 | self.animation_snapshot = Some(self.render_snapshot(renderer)); |
| 458 | } |
| 459 | |
| 460 | pub fn take_pending_transaction(&mut self, commit_serial: Serial) -> Option<Transaction> { |
| 461 | let mut rv = None; |
| 462 | |
| 463 | // Pending transactions are appended in order by serial, so we can loop from the start |
| 464 | // until we hit a serial that is too new. |
| 465 | while let Some((serial, _)) = self.pending_transactions.first() { |
| 466 | // In this loop, we will complete the transaction corresponding to the commit, as well |
| 467 | // as all transactions corresponding to previous serials. This can happen when we |
| 468 | // request resizes too quickly, and the surface only responds to the last one. |
| 469 | // |
| 470 | // Note that in this case, completing the previous transactions can result in an |
| 471 | // inconsistent visual state, if another window is waiting for this window to assume a |
| 472 | // specific size (in a previous transaction), which is now different (in this commit). |
| 473 | // |
| 474 | // However, there isn't really a good way to deal with that. We cannot cancel any |
| 475 | // transactions because we need to keep sending frame callbacks, and cancelling a |
| 476 | // transaction will make the corresponding frame callbacks get lost, and the window |
| 477 | // will hang. |
| 478 | // |
| 479 | // This is why resize throttling (implemented separately) is important: it prevents |
| 480 | // visually inconsistent states by way of never having more than one transaction in |
| 481 | // flight. |
| 482 | if commit_serial.is_no_older_than(serial) { |
| 483 | let (_, transaction) = self.pending_transactions.remove(0); |
| 484 | // Previous transaction is dropped here, signaling completion. |
| 485 | rv = Some(transaction); |
| 486 | } else { |
| 487 | break; |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | rv |
| 492 | } |
| 493 | |
| 494 | pub fn last_interactive_resize_start(&self) -> &Cell<Option<(Duration, ResizeEdge)>> { |
| 495 | &self.last_interactive_resize_start |
| 496 | } |
| 497 | |
| 498 | pub fn render_for_screen_cast<R: NiriRenderer>( |
| 499 | &self, |
| 500 | renderer: &mut R, |
| 501 | scale: Scale<f64>, |
| 502 | push: &mut dyn FnMut(WindowCastRenderElements<R>), |
| 503 | ) { |
| 504 | let bbox = self.window.bbox_with_popups().to_physical_precise_up(scale); |
| 505 | |
| 506 | let has_border_shader = BorderRenderElement::has_shader(renderer); |
| 507 | let radius = self.geometry_corner_radius(); |
| 508 | let window_size = self |
| 509 | .size() |
| 510 | .to_f64() |
| 511 | .to_physical_precise_round(scale) |
| 512 | .to_logical(scale); |
| 513 | let radius = radius.fit_to(window_size.w as f32, window_size.h as f32); |
| 514 | let location = self.window.geometry().loc.to_f64() - bbox.loc.to_logical(scale); |
| 515 | |
| 516 | let use_border = |elem| { |
| 517 | if let LayoutElementRenderElement::SolidColor(elem) = &elem { |
| 518 | // In this branch we're rendering a blocked-out window with a solid color. We need |
| 519 | // to render it with a rounded corner shader even if clip_to_geometry is false, |
| 520 | // because in this case we're assuming that the unclipped window CSD already has |
| 521 | // corners rounded to the user-provided radius, so our blocked-out rendering should |
| 522 | // match that radius. |
| 523 | if radius != CornerRadius::default() && has_border_shader { |
| 524 | let geo = elem.geo(); |
| 525 | return BorderRenderElement::new( |
| 526 | geo.size, |
| 527 | Rectangle::from_size(geo.size), |
| 528 | GradientInterpolation::default(), |
| 529 | Color::from_color32f(elem.color()), |
| 530 | Color::from_color32f(elem.color()), |
| 531 | 0., |
| 532 | Rectangle::from_size(geo.size), |
| 533 | 0., |
| 534 | radius, |
| 535 | scale.x as f32, |
| 536 | 1., |
| 537 | ) |
| 538 | .with_location(geo.loc) |
| 539 | .into(); |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | WindowCastRenderElements::from(elem) |
| 544 | }; |
| 545 | |
| 546 | self.render( |
| 547 | RenderCtx { |
| 548 | renderer, |
| 549 | target: RenderTarget::Screencast, |
| 550 | xray: None, |
| 551 | }, |
| 552 | location, |
| 553 | scale, |
| 554 | 1., |
| 555 | XrayPos::default(), |
| 556 | &mut |elem| push(use_border(elem)), |
| 557 | ); |
| 558 | } |
| 559 | |
| 560 | pub fn get_focus_timestamp(&self) -> Option<Duration> { |
| 561 | self.focus_timestamp |
| 562 | } |
| 563 | |
| 564 | pub fn set_focus_timestamp(&mut self, timestamp: Duration) { |
| 565 | self.focus_timestamp.replace(timestamp); |
| 566 | } |
| 567 | |
| 568 | pub fn send_frame<T, F>( |
| 569 | &mut self, |
| 570 | output: &Output, |
| 571 | time: T, |
| 572 | throttle: Option<Duration>, |
| 573 | mut primary_scan_out_output: F, |
| 574 | ) where |
| 575 | T: Into<Duration>, |
| 576 | F: FnMut(&WlSurface, &SurfaceData) -> Option<Output> + Copy, |
| 577 | { |
| 578 | let needs_frame_callback = self.needs_frame_callback; |
| 579 | self.needs_frame_callback = false; |
| 580 | |
| 581 | let should_send = move |surface: &WlSurface, states: &SurfaceData| { |
| 582 | // Let primary_scan_out_output() run its logic and update internal state. |
| 583 | if let Some(output) = primary_scan_out_output(surface, states) { |
| 584 | return Some(output); |
| 585 | } |
| 586 | |
| 587 | // Send unconditionally to all surfaces if the window needs a surface callback. |
| 588 | needs_frame_callback.then(|| output.clone()) |
| 589 | }; |
| 590 | self.window.send_frame(output, time, throttle, should_send); |
| 591 | } |
| 592 | |
| 593 | pub fn update_tiled_state(&self, prefer_no_csd: bool) { |
| 594 | update_tiled_state(self.toplevel(), prefer_no_csd, self.rules.tiled_state); |
| 595 | } |
| 596 | |
| 597 | pub fn is_windowed_fullscreen(&self) -> bool { |
| 598 | self.is_windowed_fullscreen |
| 599 | } |
| 600 | |
| 601 | pub fn set_urgent(&mut self, urgent: bool) { |
| 602 | if self.is_focused && urgent { |
| 603 | return; |
| 604 | } |
| 605 | |
| 606 | let changed = self.is_urgent != urgent; |
| 607 | self.is_urgent = urgent; |
| 608 | self.need_to_recompute_rules |= changed; |
| 609 | } |
| 610 | |
| 611 | pub fn is_urgent(&self) -> bool { |
| 612 | self.is_urgent |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | impl Drop for Mapped { |
| 617 | fn drop(&mut self) { |
| 618 | remove_pre_commit_hook(self.toplevel().wl_surface(), &self.pre_commit_hook); |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | impl LayoutElement for Mapped { |
| 623 | type Id = Window; |
| 624 | |
| 625 | fn id(&self) -> &Self::Id { |
| 626 | &self.window |
| 627 | } |
| 628 | |
| 629 | fn update_config(&mut self, blur_config: niri_config::Blur) { |
| 630 | self.blur_config = blur_config; |
| 631 | } |
| 632 | |
| 633 | fn size(&self) -> Size<i32, Logical> { |
| 634 | self.window.geometry().size |
| 635 | } |
| 636 | |
| 637 | fn buf_loc(&self) -> Point<i32, Logical> { |
| 638 | Point::from((0, 0)) - self.window.geometry().loc |
| 639 | } |
| 640 | |
| 641 | fn is_in_input_region(&self, point: Point<f64, Logical>) -> bool { |
| 642 | let surface_local = point + self.window.geometry().loc.to_f64(); |
| 643 | self.window.is_in_input_region(&surface_local) |
| 644 | } |
| 645 | |
| 646 | fn render_normal<R: NiriRenderer>( |
| 647 | &self, |
| 648 | ctx: RenderCtx<R>, |
| 649 | location: Point<f64, Logical>, |
| 650 | scale: Scale<f64>, |
| 651 | alpha: f32, |
| 652 | push: &mut dyn FnMut(LayoutElementRenderElement<R>), |
| 653 | ) { |
| 654 | if ctx.target.should_block_out(self.rules.block_out_from) { |
| 655 | let mut buffer = self.block_out_buffer.borrow_mut(); |
| 656 | buffer.resize(self.window.geometry().size.to_f64()); |
| 657 | let elem = |
| 658 | SolidColorRenderElement::from_buffer(&buffer, location, alpha, Kind::Unspecified); |
| 659 | push(elem.into()); |
| 660 | } else { |
| 661 | let buf_pos = location - self.window.geometry().loc.to_f64(); |
| 662 | let surface = self.toplevel().wl_surface(); |
| 663 | let mut push = |elem: WaylandSurfaceRenderElement<R>| push(elem.into()); |
| 664 | push_elements_from_surface_tree( |
| 665 | ctx.renderer, |
| 666 | surface, |
| 667 | buf_pos.to_physical_precise_round(scale), |
| 668 | scale, |
| 669 | alpha, |
| 670 | Kind::ScanoutCandidate, |
| 671 | &mut push, |
| 672 | ) |
| 673 | } |
| 674 | } |
| 675 | |
| 676 | fn render_popups<R: NiriRenderer>( |
| 677 | &self, |
| 678 | mut ctx: RenderCtx<R>, |
| 679 | location: Point<f64, Logical>, |
| 680 | scale: Scale<f64>, |
| 681 | alpha: f32, |
| 682 | xray_pos: XrayPos, |
| 683 | push: &mut dyn FnMut(LayoutElementRenderElement<R>), |
| 684 | ) { |
| 685 | if ctx.target.should_block_out(self.rules.block_out_from) { |
| 686 | return; |
| 687 | } |
| 688 | |
| 689 | let surface = self.toplevel().wl_surface(); |
| 690 | for (popup, offset) in PopupManager::popups_for_surface(surface) { |
| 691 | let popup_rules = match popup { |
| 692 | PopupKind::Xdg(_) => self.rules.popups, |
| 693 | // IME popups aren't affected by rules for regular popups. |
| 694 | PopupKind::InputMethod(_) => niri_config::ResolvedPopupsRules::default(), |
| 695 | }; |
| 696 | let alpha = alpha * popup_rules.opacity.unwrap_or(1.).clamp(0., 1.); |
| 697 | |
| 698 | let surface = popup.wl_surface(); |
| 699 | let popup_geo = popup.geometry(); |
| 700 | let surface_loc = location + (offset - popup.geometry().loc).to_f64(); |
| 701 | |
| 702 | push_elements_from_surface_tree( |
| 703 | ctx.renderer, |
| 704 | surface, |
| 705 | surface_loc.to_physical_precise_round(scale), |
| 706 | scale, |
| 707 | alpha, |
| 708 | Kind::ScanoutCandidate, |
| 709 | &mut |elem| push(elem.into()), |
| 710 | ); |
| 711 | |
| 712 | let geometry = Rectangle::new(location + offset.to_f64(), popup_geo.size.to_f64()); |
| 713 | let surface_off = popup_geo.loc.upscale(-1).to_f64(); |
| 714 | let surface_anim_scale = Scale::from(1.); |
| 715 | let mut effect = popup_rules.background_effect; |
| 716 | // Default xray to false for pop-ups since they're always on top of something. |
| 717 | if effect.xray.is_none() { |
| 718 | effect.xray = Some(false); |
| 719 | } |
| 720 | let xray_pos = xray_pos.offset(offset.to_f64()); |
| 721 | background_effect::render_for_tile( |
| 722 | ctx.as_gles(), |
| 723 | None, |
| 724 | geometry, |
| 725 | scale.x, |
| 726 | false, |
| 727 | surface, |
| 728 | surface_off, |
| 729 | surface_anim_scale, |
| 730 | self.blur_config, |
| 731 | popup_rules.geometry_corner_radius.unwrap_or_default(), |
| 732 | effect, |
| 733 | false, |
| 734 | xray_pos, |
| 735 | &mut |elem| push(elem.into()), |
| 736 | ); |
| 737 | } |
| 738 | } |
| 739 | |
| 740 | fn render_background_effect( |
| 741 | &self, |
| 742 | ctx: RenderCtx<GlesRenderer>, |
| 743 | geometry: Rectangle<f64, Logical>, |
| 744 | scale: f64, |
| 745 | clip_to_geometry: bool, |
| 746 | surface_anim_scale: Scale<f64>, |
| 747 | radius: CornerRadius, |
| 748 | xray_pos: XrayPos, |
| 749 | push: &mut dyn FnMut(BackgroundEffectElement), |
| 750 | ) { |
| 751 | let should_block_out = ctx.target.should_block_out(self.rules.block_out_from); |
| 752 | background_effect::render_for_tile( |
| 753 | ctx, |
| 754 | None, |
| 755 | geometry, |
| 756 | scale, |
| 757 | clip_to_geometry, |
| 758 | self.toplevel().wl_surface(), |
| 759 | self.buf_loc().to_f64(), |
| 760 | surface_anim_scale, |
| 761 | self.blur_config, |
| 762 | radius, |
| 763 | self.rules.background_effect, |
| 764 | should_block_out, |
| 765 | xray_pos, |
| 766 | push, |
| 767 | ); |
| 768 | } |
| 769 | |
| 770 | fn request_size( |
| 771 | &mut self, |
| 772 | size: Size<i32, Logical>, |
| 773 | mode: SizingMode, |
| 774 | animate: bool, |
| 775 | transaction: Option<Transaction>, |
| 776 | ) { |
| 777 | // Going into real fullscreen resets windowed fullscreen. |
| 778 | if mode == SizingMode::Fullscreen { |
| 779 | self.is_pending_windowed_fullscreen = false; |
| 780 | |
| 781 | if self.is_windowed_fullscreen { |
| 782 | // Make sure we receive a commit to update self.is_windowed_fullscreen to false |
| 783 | // later on. |
| 784 | self.needs_configure = true; |
| 785 | } |
| 786 | } |
| 787 | |
| 788 | self.is_pending_maximized = mode == SizingMode::Maximized; |
| 789 | if self.is_maximized != self.is_pending_maximized { |
| 790 | // Make sure we receive a commit to update self.is_maximized later on. |
| 791 | self.needs_configure = true; |
| 792 | } |
| 793 | |
| 794 | let changed = self.toplevel().with_pending_state(|state| { |
| 795 | let changed = state.size != Some(size); |
| 796 | state.size = Some(size); |
| 797 | |
| 798 | if mode.is_fullscreen() || self.is_pending_windowed_fullscreen { |
| 799 | state.states.set(xdg_toplevel::State::Fullscreen); |
| 800 | state.states.unset(xdg_toplevel::State::Maximized); |
| 801 | } else if mode.is_maximized() { |
| 802 | state.states.unset(xdg_toplevel::State::Fullscreen); |
| 803 | state.states.set(xdg_toplevel::State::Maximized); |
| 804 | } else { |
| 805 | state.states.unset(xdg_toplevel::State::Fullscreen); |
| 806 | state.states.unset(xdg_toplevel::State::Maximized); |
| 807 | } |
| 808 | |
| 809 | changed |
| 810 | }); |
| 811 | |
| 812 | if changed && animate { |
| 813 | self.animate_next_configure = true; |
| 814 | } |
| 815 | |
| 816 | self.request_size_once = None; |
| 817 | |
| 818 | // Store the transaction regardless of whether the size changed. This is because with 3+ |
| 819 | // windows in a column, the size may change among windows 1 and 2 and then right away among |
| 820 | // windows 2 and 3, and we want all windows 1, 2 and 3 to use the last transaction, rather |
| 821 | // than window 1 getting stuck with the previous transaction that is immediately released |
| 822 | // by 2. |
| 823 | if let Some(transaction) = transaction { |
| 824 | self.transaction_for_next_configure = Some(transaction); |
| 825 | } |
| 826 | } |
| 827 | |
| 828 | fn request_size_once(&mut self, size: Size<i32, Logical>, animate: bool) { |
| 829 | // Assume that when calling this function, the window is going floating, so it can no |
| 830 | // longer participate in any transactions with other windows. |
| 831 | self.transaction_for_next_configure = None; |
| 832 | |
| 833 | self.is_pending_maximized = false; |
| 834 | if self.is_maximized != self.is_pending_maximized { |
| 835 | // Make sure we receive a commit to update self.is_maximized later on. |
| 836 | self.needs_configure = true; |
| 837 | } |
| 838 | |
| 839 | // If our last requested size already matches the size we want to request-once, clear the |
| 840 | // size request right away. However, we must also check if we're unfullscreening, because |
| 841 | // in that case the window itself will restore its previous size upon receiving a (0, 0) |
| 842 | // configure, whereas what we potentially want is to unfullscreen the window into its |
| 843 | // fullscreen size. |
| 844 | let already_sent = with_toplevel_last_uncommitted_configure(self.toplevel(), |configure| { |
| 845 | let ToplevelConfigure { state, serial } = configure?; |
| 846 | |
| 847 | let same_size = state.size.unwrap_or_default() == size; |
| 848 | let has_fullscreen = state.states.contains(xdg_toplevel::State::Fullscreen); |
| 849 | let same_fullscreen = has_fullscreen == self.is_pending_windowed_fullscreen; |
| 850 | let has_maximized = state.states.contains(xdg_toplevel::State::Maximized); |
| 851 | let same_maximized = !has_maximized; |
| 852 | (same_size && same_fullscreen && same_maximized).then_some(*serial) |
| 853 | }); |
| 854 | |
| 855 | if let Some(serial) = already_sent { |
| 856 | let current_serial = with_states(self.toplevel().wl_surface(), |states| { |
| 857 | states |
| 858 | .cached_state |
| 859 | .get::<ToplevelCachedState>() |
| 860 | .current() |
| 861 | .last_acked |
| 862 | .as_ref() |
| 863 | .map(|c| c.serial) |
| 864 | }); |
| 865 | if let Some(current_serial) = current_serial { |
| 866 | // God this triple negative... |
| 867 | if !current_serial.is_no_older_than(&serial) { |
| 868 | // We have already sent a request for the new size, but the surface has not |
| 869 | // committed in response yet, so we will wait for that commit. |
| 870 | self.request_size_once = Some(RequestSizeOnce::WaitingForCommit(serial)); |
| 871 | } else { |
| 872 | // We have already sent a request for the new size, and the surface has |
| 873 | // committed in response, so we will start using the current size right away. |
| 874 | self.request_size_once = Some(RequestSizeOnce::UseWindowSize); |
| 875 | } |
| 876 | } else { |
| 877 | warn!("no current serial; did the surface not ack the initial configure?"); |
| 878 | self.request_size_once = Some(RequestSizeOnce::UseWindowSize); |
| 879 | }; |
| 880 | return; |
| 881 | } |
| 882 | |
| 883 | let changed = self.toplevel().with_pending_state(|state| { |
| 884 | let changed = state.size != Some(size); |
| 885 | state.size = Some(size); |
| 886 | if !self.is_pending_windowed_fullscreen { |
| 887 | state.states.unset(xdg_toplevel::State::Fullscreen); |
| 888 | } |
| 889 | state.states.unset(xdg_toplevel::State::Maximized); |
| 890 | changed |
| 891 | }); |
| 892 | |
| 893 | if changed && animate { |
| 894 | self.animate_next_configure = true; |
| 895 | } |
| 896 | |
| 897 | self.request_size_once = Some(RequestSizeOnce::WaitingForConfigure); |
| 898 | } |
| 899 | |
| 900 | fn min_size(&self) -> Size<i32, Logical> { |
| 901 | let min_size = with_states(self.toplevel().wl_surface(), |state| { |
| 902 | let mut guard = state.cached_state.get::<SurfaceCachedState>(); |
| 903 | guard.current().min_size |
| 904 | }); |
| 905 | |
| 906 | self.rules.apply_min_size(min_size) |
| 907 | } |
| 908 | |
| 909 | fn max_size(&self) -> Size<i32, Logical> { |
| 910 | let max_size = with_states(self.toplevel().wl_surface(), |state| { |
| 911 | let mut guard = state.cached_state.get::<SurfaceCachedState>(); |
| 912 | guard.current().max_size |
| 913 | }); |
| 914 | |
| 915 | self.rules.apply_max_size(max_size) |
| 916 | } |
| 917 | |
| 918 | fn is_wl_surface(&self, wl_surface: &WlSurface) -> bool { |
| 919 | self.toplevel().wl_surface() == wl_surface |
| 920 | } |
| 921 | |
| 922 | fn set_preferred_scale_transform(&self, scale: output::Scale, transform: Transform) { |
| 923 | self.window.with_surfaces(|surface, data| { |
| 924 | send_scale_transform(surface, data, scale, transform); |
| 925 | }); |
| 926 | } |
| 927 | |
| 928 | fn has_ssd(&self) -> bool { |
| 929 | let toplevel = self.toplevel(); |
| 930 | let mode = self |
| 931 | .toplevel() |
| 932 | .with_committed_state(|current| current.and_then(|s| s.decoration_mode)); |
| 933 | |
| 934 | match mode { |
| 935 | Some(zxdg_toplevel_decoration_v1::Mode::ServerSide) => true, |
| 936 | // Check KDE decorations when XDG are not in use. |
| 937 | None => with_states(toplevel.wl_surface(), |states| { |
| 938 | states |
| 939 | .data_map |
| 940 | .get::<KdeDecorationsModeState>() |
| 941 | .map(KdeDecorationsModeState::is_server) |
| 942 | == Some(true) |
| 943 | }), |
| 944 | _ => false, |
| 945 | } |
| 946 | } |
| 947 | |
| 948 | fn output_enter(&self, output: &Output) { |
| 949 | let overlap = Rectangle::from_size(Size::from((i32::MAX, i32::MAX))); |
| 950 | self.window.output_enter(output, overlap) |
| 951 | } |
| 952 | |
| 953 | fn output_leave(&self, output: &Output) { |
| 954 | self.window.output_leave(output) |
| 955 | } |
| 956 | |
| 957 | fn set_offscreen_data(&self, data: Option<OffscreenData>) { |
| 958 | let Some(data) = data else { |
| 959 | self.offscreen_data.replace(None); |
| 960 | return; |
| 961 | }; |
| 962 | |
| 963 | let mut offscreen_data = self.offscreen_data.borrow_mut(); |
| 964 | match &mut *offscreen_data { |
| 965 | None => { |
| 966 | *offscreen_data = Some(data); |
| 967 | } |
| 968 | Some(existing) => { |
| 969 | // Replace the id, amend existing element states. This is necessary to handle |
| 970 | // multiple layers of offscreen (e.g. resize animation + alpha animation). |
| 971 | existing.id = data.id; |
| 972 | existing.states.states.extend(data.states.states); |
| 973 | } |
| 974 | } |
| 975 | } |
| 976 | |
| 977 | fn is_urgent(&self) -> bool { |
| 978 | self.is_urgent |
| 979 | } |
| 980 | |
| 981 | fn set_activated(&mut self, active: bool) { |
| 982 | let changed = self.toplevel().with_pending_state(|state| { |
| 983 | if active { |
| 984 | state.states.set(xdg_toplevel::State::Activated) |
| 985 | } else { |
| 986 | state.states.unset(xdg_toplevel::State::Activated) |
| 987 | } |
| 988 | }); |
| 989 | self.need_to_recompute_rules |= changed; |
| 990 | } |
| 991 | |
| 992 | fn set_active_in_column(&mut self, active: bool) { |
| 993 | let changed = self.is_active_in_column != active; |
| 994 | self.is_active_in_column = active; |
| 995 | self.need_to_recompute_rules |= changed; |
| 996 | } |
| 997 | |
| 998 | fn set_floating(&mut self, floating: bool) { |
| 999 | let changed = self.is_floating != floating; |
| 1000 | self.is_floating = floating; |
| 1001 | self.need_to_recompute_rules |= changed; |
| 1002 | } |
| 1003 | |
| 1004 | fn set_bounds(&self, bounds: Size<i32, Logical>) { |
| 1005 | self.toplevel().with_pending_state(|state| { |
| 1006 | state.bounds = Some(bounds); |
| 1007 | }); |
| 1008 | } |
| 1009 | |
| 1010 | fn configure_intent(&self) -> ConfigureIntent { |
| 1011 | let _span = |
| 1012 | trace_span!("configure_intent", surface = ?self.toplevel().wl_surface().id()).entered(); |
| 1013 | |
| 1014 | if self.needs_configure { |
| 1015 | trace!("the window needs_configure"); |
| 1016 | return ConfigureIntent::ShouldSend; |
| 1017 | } |
| 1018 | |
| 1019 | with_toplevel_role_and_current(self.toplevel(), |attributes, current_committed| { |
| 1020 | if let Some(server_pending) = &attributes.server_pending { |
| 1021 | let current_server = attributes.current_server_state(); |
| 1022 | if *server_pending != current_server { |
| 1023 | // Something changed. Check if the only difference is the size, and if the |
| 1024 | // current server size matches the current committed size. |
| 1025 | let mut current_server_same_size = current_server.clone(); |
| 1026 | current_server_same_size.size = server_pending.size; |
| 1027 | if current_server_same_size == *server_pending { |
| 1028 | // Only the size changed. Check if the window committed our previous size |
| 1029 | // request. |
| 1030 | let Some(current_committed) = current_committed else { |
| 1031 | error!("mapped must have had initial commit"); |
| 1032 | return ConfigureIntent::ShouldSend; |
| 1033 | }; |
| 1034 | |
| 1035 | if current_committed.size == current_server.size { |
| 1036 | // The window had committed for our previous size change, so we can |
| 1037 | // change the size again. |
| 1038 | trace!( |
| 1039 | "current size matches server size: {:?}", |
| 1040 | current_committed.size |
| 1041 | ); |
| 1042 | ConfigureIntent::CanSend |
| 1043 | } else { |
| 1044 | // The window had not committed for our previous size change yet. Since |
| 1045 | // nothing else changed, do not send the new size request yet. This |
| 1046 | // throttling is done because some clients do not batch size requests, |
| 1047 | // leading to bad behavior with very fast input devices (i.e. a 1000 Hz |
| 1048 | // mouse). This throttling also helps interactive resize transactions |
| 1049 | // preserve visual consistency. |
| 1050 | trace!("throttling resize"); |
| 1051 | ConfigureIntent::Throttled |
| 1052 | } |
| 1053 | } else { |
| 1054 | // Something else changed other than the size; send it. |
| 1055 | trace!("something changed other than the size"); |
| 1056 | ConfigureIntent::ShouldSend |
| 1057 | } |
| 1058 | } else { |
| 1059 | // Nothing changed since the last configure. |
| 1060 | ConfigureIntent::NotNeeded |
| 1061 | } |
| 1062 | } else { |
| 1063 | // Nothing changed since the last configure. |
| 1064 | ConfigureIntent::NotNeeded |
| 1065 | } |
| 1066 | }) |
| 1067 | } |
| 1068 | |
| 1069 | fn send_pending_configure(&mut self) { |
| 1070 | let toplevel = self.toplevel(); |
| 1071 | let _span = |
| 1072 | trace_span!("send_pending_configure", surface = ?toplevel.wl_surface().id()).entered(); |
| 1073 | |
| 1074 | // If the window needs a configure, send it regardless. |
| 1075 | let has_pending_changes = self.needs_configure |
| 1076 | || with_toplevel_role(self.toplevel(), |role| { |
| 1077 | // Check for pending changes manually to account for RequestSizeOnce::UseWindowSize. |
| 1078 | if role.server_pending.is_none() { |
| 1079 | return false; |
| 1080 | } |
| 1081 | |
| 1082 | let current_server_size = role.current_server_state().size; |
| 1083 | let server_pending = role.server_pending.as_mut().unwrap(); |
| 1084 | |
| 1085 | // With UseWindowSize, we do not consider size-only changes, because we will |
| 1086 | // request the current window size and do not expect it to actually change. |
| 1087 | if let Some(RequestSizeOnce::UseWindowSize) = self.request_size_once { |
| 1088 | server_pending.size = current_server_size; |
| 1089 | } |
| 1090 | |
| 1091 | let server_pending = role.server_pending.as_ref().unwrap(); |
| 1092 | *server_pending != role.current_server_state() |
| 1093 | }); |
| 1094 | |
| 1095 | if has_pending_changes { |
| 1096 | // If needed, replace the pending size with the current window size. |
| 1097 | if let Some(RequestSizeOnce::UseWindowSize) = self.request_size_once { |
| 1098 | let size = self.window.geometry().size; |
| 1099 | toplevel.with_pending_state(|state| { |
| 1100 | state.size = Some(size); |
| 1101 | }); |
| 1102 | } |
| 1103 | |
| 1104 | let serial = toplevel.send_configure(); |
| 1105 | trace!(?serial, "sending configure"); |
| 1106 | |
| 1107 | self.needs_configure = false; |
| 1108 | |
| 1109 | // Send the window a frame callback unconditionally to let it respond to size changes |
| 1110 | // and such immediately, even when it's hidden. This especially matters for cases like |
| 1111 | // tabbed columns which compute their width based on all windows in the column, even |
| 1112 | // hidden ones. |
| 1113 | self.needs_frame_callback = true; |
| 1114 | |
| 1115 | if self.animate_next_configure { |
| 1116 | self.animate_serials.push(serial); |
| 1117 | } |
| 1118 | |
| 1119 | if let Some(transaction) = self.transaction_for_next_configure.take() { |
| 1120 | self.pending_transactions.push((serial, transaction)); |
| 1121 | } |
| 1122 | |
| 1123 | self.interactive_resize = match self.interactive_resize.take() { |
| 1124 | Some(InteractiveResize::WaitingForLastConfigure(data)) => { |
| 1125 | Some(InteractiveResize::WaitingForLastCommit { data, serial }) |
| 1126 | } |
| 1127 | x => x, |
| 1128 | }; |
| 1129 | |
| 1130 | if let Some(RequestSizeOnce::WaitingForConfigure) = self.request_size_once { |
| 1131 | self.request_size_once = Some(RequestSizeOnce::WaitingForCommit(serial)); |
| 1132 | } |
| 1133 | |
| 1134 | // If is_pending_windowed_fullscreen changed compared to the last value that we "sent" |
| 1135 | // to the window, store the configure serial. |
| 1136 | let last_sent_windowed_fullscreen = self |
| 1137 | .uncommitted_windowed_fullscreen |
| 1138 | .last() |
| 1139 | .map(|(_, value)| *value) |
| 1140 | .unwrap_or(self.is_windowed_fullscreen); |
| 1141 | if last_sent_windowed_fullscreen != self.is_pending_windowed_fullscreen { |
| 1142 | self.uncommitted_windowed_fullscreen |
| 1143 | .push((serial, self.is_pending_windowed_fullscreen)); |
| 1144 | } |
| 1145 | |
| 1146 | // If is_pending_maximized changed compared to the last value that we "sent" to the |
| 1147 | // window, store the configure serial. |
| 1148 | let last_sent_maximized = self |
| 1149 | .uncommitted_maximized |
| 1150 | .last() |
| 1151 | .map(|(_, value)| *value) |
| 1152 | .unwrap_or(self.is_maximized); |
| 1153 | if last_sent_maximized != self.is_pending_maximized { |
| 1154 | self.uncommitted_maximized |
| 1155 | .push((serial, self.is_pending_maximized)); |
| 1156 | } |
| 1157 | } else { |
| 1158 | self.interactive_resize = match self.interactive_resize.take() { |
| 1159 | // We probably started and stopped resizing in the same loop cycle without anything |
| 1160 | // changing. |
| 1161 | Some(InteractiveResize::WaitingForLastConfigure { .. }) => None, |
| 1162 | x => x, |
| 1163 | }; |
| 1164 | } |
| 1165 | |
| 1166 | self.animate_next_configure = false; |
| 1167 | self.transaction_for_next_configure = None; |
| 1168 | } |
| 1169 | |
| 1170 | fn sizing_mode(&self) -> SizingMode { |
| 1171 | if self.is_windowed_fullscreen { |
| 1172 | return if self.is_maximized { |
| 1173 | SizingMode::Maximized |
| 1174 | } else { |
| 1175 | SizingMode::Normal |
| 1176 | }; |
| 1177 | } |
| 1178 | |
| 1179 | self.toplevel().with_committed_state(|state| { |
| 1180 | // This must always be Some() for mapped windows. However, this function is called on |
| 1181 | // the code path when removing a just-unmapped window in the commit handler, at which |
| 1182 | // point state is already None. |
| 1183 | let Some(state) = state else { |
| 1184 | return SizingMode::Normal; |
| 1185 | }; |
| 1186 | |
| 1187 | if state.states.contains(xdg_toplevel::State::Fullscreen) { |
| 1188 | SizingMode::Fullscreen |
| 1189 | } else if state.states.contains(xdg_toplevel::State::Maximized) { |
| 1190 | SizingMode::Maximized |
| 1191 | } else { |
| 1192 | SizingMode::Normal |
| 1193 | } |
| 1194 | }) |
| 1195 | } |
| 1196 | |
| 1197 | fn pending_sizing_mode(&self) -> SizingMode { |
| 1198 | if self.is_pending_windowed_fullscreen { |
| 1199 | return if self.is_pending_maximized { |
| 1200 | SizingMode::Maximized |
| 1201 | } else { |
| 1202 | SizingMode::Normal |
| 1203 | }; |
| 1204 | } |
| 1205 | |
| 1206 | self.toplevel().with_pending_state(|state| { |
| 1207 | if state.states.contains(xdg_toplevel::State::Fullscreen) { |
| 1208 | SizingMode::Fullscreen |
| 1209 | } else if state.states.contains(xdg_toplevel::State::Maximized) { |
| 1210 | SizingMode::Maximized |
| 1211 | } else { |
| 1212 | SizingMode::Normal |
| 1213 | } |
| 1214 | }) |
| 1215 | } |
| 1216 | |
| 1217 | fn is_ignoring_opacity_window_rule(&self) -> bool { |
| 1218 | self.ignore_opacity_window_rule |
| 1219 | } |
| 1220 | |
| 1221 | fn requested_size(&self) -> Option<Size<i32, Logical>> { |
| 1222 | self.toplevel().with_pending_state(|state| state.size) |
| 1223 | } |
| 1224 | |
| 1225 | fn expected_size(&self) -> Option<Size<i32, Logical>> { |
| 1226 | // We can only use current size if it's not maximized or fullscreen. |
| 1227 | let current_size = (self.sizing_mode().is_normal()).then(|| self.window.geometry().size); |
| 1228 | |
| 1229 | // Check if we should be using the current window size. |
| 1230 | // |
| 1231 | // This branch can be useful (give different result than the logic below) in this example |
| 1232 | // case: |
| 1233 | // |
| 1234 | // 1. We request_size_once a size change. |
| 1235 | // 2. We send a second configure requesting a state change. |
| 1236 | // 3. The window acks and commits-to the first configure but not the second, with a |
| 1237 | // different size. |
| 1238 | // |
| 1239 | // In this case self.request_size_once will already flip to UseWindowSize and this branch |
| 1240 | // will return the window's own new size, but the logic below would see an uncommitted size |
| 1241 | // change and return our size. |
| 1242 | if let Some(RequestSizeOnce::UseWindowSize) = self.request_size_once { |
| 1243 | return current_size; |
| 1244 | } |
| 1245 | |
| 1246 | let pending = with_states(self.toplevel().wl_surface(), |states| { |
| 1247 | let role = states |
| 1248 | .data_map |
| 1249 | .get::<XdgToplevelSurfaceData>() |
| 1250 | .unwrap() |
| 1251 | .lock() |
| 1252 | .unwrap(); |
| 1253 | |
| 1254 | // If we have a server-pending size change that we haven't sent yet, use that size. |
| 1255 | let server_pending = role.server_pending.as_ref()?; |
| 1256 | |
| 1257 | let current_server = role.current_server_state(); |
| 1258 | if server_pending.size != current_server.size { |
| 1259 | return Some(( |
| 1260 | server_pending.size.unwrap_or_default(), |
| 1261 | server_pending |
| 1262 | .states |
| 1263 | .contains(xdg_toplevel::State::Fullscreen), |
| 1264 | server_pending |
| 1265 | .states |
| 1266 | .contains(xdg_toplevel::State::Maximized), |
| 1267 | )); |
| 1268 | } |
| 1269 | |
| 1270 | None |
| 1271 | }) |
| 1272 | .or_else(|| { |
| 1273 | with_toplevel_last_uncommitted_configure(self.toplevel(), |configure| { |
| 1274 | // If we have a sent-but-not-committed-to size, use that. |
| 1275 | let ToplevelConfigure { state, .. } = configure?; |
| 1276 | |
| 1277 | Some(( |
| 1278 | state.size.unwrap_or_default(), |
| 1279 | state.states.contains(xdg_toplevel::State::Fullscreen), |
| 1280 | state.states.contains(xdg_toplevel::State::Maximized), |
| 1281 | )) |
| 1282 | }) |
| 1283 | }); |
| 1284 | |
| 1285 | if let Some((mut size, fullscreen, maximized)) = pending { |
| 1286 | // If the pending change is maximized or fullscreen, we can't use that size. |
| 1287 | // |
| 1288 | // Pending windowed fullscreen is good (means not real fullscreen), unless it's also |
| 1289 | // pending maximized (means maximized windowed fullscreen, so maximized size, bad). |
| 1290 | if maximized |
| 1291 | || (fullscreen |
| 1292 | && (!self.is_pending_windowed_fullscreen || self.is_pending_maximized)) |
| 1293 | { |
| 1294 | return None; |
| 1295 | } |
| 1296 | |
| 1297 | // If some component of the pending size is zero, substitute it with the current window |
| 1298 | // size. But only if the current size is not fullscreen. |
| 1299 | if size.w == 0 { |
| 1300 | size.w = current_size?.w; |
| 1301 | } |
| 1302 | if size.h == 0 { |
| 1303 | size.h = current_size?.h; |
| 1304 | } |
| 1305 | |
| 1306 | Some(size) |
| 1307 | } else { |
| 1308 | // No pending size, return the current size if it's non-fullscreen. |
| 1309 | current_size |
| 1310 | } |
| 1311 | } |
| 1312 | |
| 1313 | fn is_windowed_fullscreen(&self) -> bool { |
| 1314 | self.is_windowed_fullscreen |
| 1315 | } |
| 1316 | |
| 1317 | fn is_pending_windowed_fullscreen(&self) -> bool { |
| 1318 | self.is_pending_windowed_fullscreen |
| 1319 | } |
| 1320 | |
| 1321 | fn request_windowed_fullscreen(&mut self, value: bool) { |
| 1322 | if self.is_pending_windowed_fullscreen == value { |
| 1323 | return; |
| 1324 | } |
| 1325 | |
| 1326 | self.is_pending_windowed_fullscreen = value; |
| 1327 | |
| 1328 | // Set the fullscreen state to match. |
| 1329 | // |
| 1330 | // When going from windowed to real fullscreen, we'll use request_size() which will set the |
| 1331 | // fullscreen state back. |
| 1332 | self.toplevel().with_pending_state(|state| { |
| 1333 | if value { |
| 1334 | state.states.set(xdg_toplevel::State::Fullscreen); |
| 1335 | state.states.unset(xdg_toplevel::State::Maximized); |
| 1336 | } else { |
| 1337 | state.states.unset(xdg_toplevel::State::Fullscreen); |
| 1338 | |
| 1339 | if self.is_pending_maximized { |
| 1340 | state.states.set(xdg_toplevel::State::Maximized); |
| 1341 | } |
| 1342 | } |
| 1343 | }); |
| 1344 | |
| 1345 | // Make sure we receive a commit later to update self.is_windowed_fullscreen. |
| 1346 | self.needs_configure = true; |
| 1347 | } |
| 1348 | |
| 1349 | fn is_child_of(&self, parent: &Self) -> bool { |
| 1350 | self.toplevel().parent().as_ref() == Some(parent.toplevel().wl_surface()) |
| 1351 | } |
| 1352 | |
| 1353 | fn refresh(&self) { |
| 1354 | self.window.refresh(); |
| 1355 | } |
| 1356 | |
| 1357 | fn rules(&self) -> &ResolvedWindowRules { |
| 1358 | &self.rules |
| 1359 | } |
| 1360 | |
| 1361 | fn take_animation_snapshot(&mut self) -> Option<LayoutElementRenderSnapshot> { |
| 1362 | self.animation_snapshot.take() |
| 1363 | } |
| 1364 | |
| 1365 | fn set_interactive_resize(&mut self, data: Option<InteractiveResizeData>) { |
| 1366 | self.toplevel().with_pending_state(|state| { |
| 1367 | if data.is_some() { |
| 1368 | state.states.set(xdg_toplevel::State::Resizing); |
| 1369 | } else { |
| 1370 | state.states.unset(xdg_toplevel::State::Resizing); |
| 1371 | } |
| 1372 | }); |
| 1373 | |
| 1374 | if let Some(data) = data { |
| 1375 | self.interactive_resize = Some(InteractiveResize::Ongoing(data)); |
| 1376 | } else { |
| 1377 | self.interactive_resize = match self.interactive_resize.take() { |
| 1378 | Some(InteractiveResize::Ongoing(data)) => { |
| 1379 | Some(InteractiveResize::WaitingForLastConfigure(data)) |
| 1380 | } |
| 1381 | x => x, |
| 1382 | } |
| 1383 | } |
| 1384 | } |
| 1385 | |
| 1386 | fn cancel_interactive_resize(&mut self) { |
| 1387 | self.set_interactive_resize(None); |
| 1388 | self.interactive_resize = None; |
| 1389 | } |
| 1390 | |
| 1391 | fn interactive_resize_data(&self) -> Option<InteractiveResizeData> { |
| 1392 | Some(self.interactive_resize.as_ref()?.data()) |
| 1393 | } |
| 1394 | |
| 1395 | fn on_commit(&mut self, commit_serial: Serial) { |
| 1396 | if let Some(InteractiveResize::WaitingForLastCommit { serial, .. }) = |
| 1397 | &self.interactive_resize |
| 1398 | { |
| 1399 | if commit_serial.is_no_older_than(serial) { |
| 1400 | self.interactive_resize = None; |
| 1401 | } |
| 1402 | } |
| 1403 | |
| 1404 | if let Some(RequestSizeOnce::WaitingForCommit(serial)) = &self.request_size_once { |
| 1405 | if commit_serial.is_no_older_than(serial) { |
| 1406 | self.request_size_once = Some(RequestSizeOnce::UseWindowSize); |
| 1407 | } |
| 1408 | } |
| 1409 | |
| 1410 | // "Commit" our "acked" pending windowed fullscreen state. |
| 1411 | self.uncommitted_windowed_fullscreen |
| 1412 | .retain_mut(|(serial, value)| { |
| 1413 | if commit_serial.is_no_older_than(serial) { |
| 1414 | self.is_windowed_fullscreen = *value; |
| 1415 | false |
| 1416 | } else { |
| 1417 | true |
| 1418 | } |
| 1419 | }); |
| 1420 | |
| 1421 | // "Commit" our "acked" pending maximized state. |
| 1422 | self.uncommitted_maximized.retain_mut(|(serial, value)| { |
| 1423 | if commit_serial.is_no_older_than(serial) { |
| 1424 | self.is_maximized = *value; |
| 1425 | false |
| 1426 | } else { |
| 1427 | true |
| 1428 | } |
| 1429 | }); |
| 1430 | } |
| 1431 | } |