edith
Clone
https://git.tahinli.com/Tahinli/edith.git
0 watching·0 forks·Rust·18.3 MiB·ff99ad6·Tahinli·6 days ago·1055 commits
| 1 | use std::{ |
| 2 | cell::{Ref, RefCell, RefMut}, |
| 3 | ffi::c_void, |
| 4 | ptr::NonNull, |
| 5 | rc::Rc, |
| 6 | sync::Arc, |
| 7 | }; |
| 8 | |
| 9 | use blade_graphics as gpu; |
| 10 | use collections::HashMap; |
| 11 | use futures::channel::oneshot::Receiver; |
| 12 | |
| 13 | use raw_window_handle as rwh; |
| 14 | use wayland_backend::client::ObjectId; |
| 15 | use wayland_client::WEnum; |
| 16 | use wayland_client::{Proxy, protocol::wl_surface}; |
| 17 | use wayland_protocols::wp::viewporter::client::wp_viewport; |
| 18 | use wayland_protocols::xdg::decoration::zv1::client::zxdg_toplevel_decoration_v1; |
| 19 | use wayland_protocols::xdg::shell::client::xdg_surface; |
| 20 | use wayland_protocols::xdg::shell::client::xdg_toplevel::{self}; |
| 21 | use wayland_protocols::{ |
| 22 | wp::fractional_scale::v1::client::wp_fractional_scale_v1, |
| 23 | xdg::shell::client::xdg_toplevel::XdgToplevel, |
| 24 | }; |
| 25 | use wayland_protocols_plasma::blur::client::org_kde_kwin_blur; |
| 26 | |
| 27 | use crate::{ |
| 28 | AnyWindowHandle, Bounds, Decorations, Globals, GpuSpecs, Modifiers, Output, Pixels, |
| 29 | PlatformDisplay, PlatformInput, Point, PromptButton, PromptLevel, RequestFrameOptions, |
| 30 | ResizeEdge, Size, Tiling, WaylandClientStatePtr, WindowAppearance, WindowBackgroundAppearance, |
| 31 | WindowBounds, WindowControlArea, WindowControls, WindowDecorations, WindowParams, px, size, |
| 32 | }; |
| 33 | use crate::{ |
| 34 | Capslock, |
| 35 | platform::{ |
| 36 | PlatformAtlas, PlatformInputHandler, PlatformWindow, |
| 37 | blade::{BladeContext, BladeRenderer, BladeSurfaceConfig}, |
| 38 | linux::wayland::{display::WaylandDisplay, serial::SerialKind}, |
| 39 | }, |
| 40 | }; |
| 41 | use crate::{WindowKind, scene::Scene}; |
| 42 | |
| 43 | #[derive(Default)] |
| 44 | pub(crate) struct Callbacks { |
| 45 | request_frame: Option<Box<dyn FnMut(RequestFrameOptions)>>, |
| 46 | input: Option<Box<dyn FnMut(crate::PlatformInput) -> crate::DispatchEventResult>>, |
| 47 | active_status_change: Option<Box<dyn FnMut(bool)>>, |
| 48 | hover_status_change: Option<Box<dyn FnMut(bool)>>, |
| 49 | resize: Option<Box<dyn FnMut(Size<Pixels>, f32)>>, |
| 50 | moved: Option<Box<dyn FnMut()>>, |
| 51 | should_close: Option<Box<dyn FnMut() -> bool>>, |
| 52 | close: Option<Box<dyn FnOnce()>>, |
| 53 | appearance_changed: Option<Box<dyn FnMut()>>, |
| 54 | } |
| 55 | |
| 56 | struct RawWindow { |
| 57 | window: *mut c_void, |
| 58 | display: *mut c_void, |
| 59 | } |
| 60 | |
| 61 | impl rwh::HasWindowHandle for RawWindow { |
| 62 | fn window_handle(&self) -> Result<rwh::WindowHandle<'_>, rwh::HandleError> { |
| 63 | let window = NonNull::new(self.window).unwrap(); |
| 64 | let handle = rwh::WaylandWindowHandle::new(window); |
| 65 | Ok(unsafe { rwh::WindowHandle::borrow_raw(handle.into()) }) |
| 66 | } |
| 67 | } |
| 68 | impl rwh::HasDisplayHandle for RawWindow { |
| 69 | fn display_handle(&self) -> Result<rwh::DisplayHandle<'_>, rwh::HandleError> { |
| 70 | let display = NonNull::new(self.display).unwrap(); |
| 71 | let handle = rwh::WaylandDisplayHandle::new(display); |
| 72 | Ok(unsafe { rwh::DisplayHandle::borrow_raw(handle.into()) }) |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | #[derive(Debug)] |
| 77 | struct InProgressConfigure { |
| 78 | size: Option<Size<Pixels>>, |
| 79 | fullscreen: bool, |
| 80 | maximized: bool, |
| 81 | resizing: bool, |
| 82 | tiling: Tiling, |
| 83 | } |
| 84 | |
| 85 | pub struct WaylandWindowState { |
| 86 | xdg_surface: xdg_surface::XdgSurface, |
| 87 | acknowledged_first_configure: bool, |
| 88 | pub surface: wl_surface::WlSurface, |
| 89 | /// An output to go fullscreen on once the window is mapped (see `fullscreen_on_next_frame`). |
| 90 | pending_fullscreen: Option<wayland_client::protocol::wl_output::WlOutput>, |
| 91 | decoration: Option<zxdg_toplevel_decoration_v1::ZxdgToplevelDecorationV1>, |
| 92 | app_id: Option<String>, |
| 93 | appearance: WindowAppearance, |
| 94 | blur: Option<org_kde_kwin_blur::OrgKdeKwinBlur>, |
| 95 | toplevel: xdg_toplevel::XdgToplevel, |
| 96 | viewport: Option<wp_viewport::WpViewport>, |
| 97 | outputs: HashMap<ObjectId, Output>, |
| 98 | display: Option<(ObjectId, Output)>, |
| 99 | globals: Globals, |
| 100 | renderer: BladeRenderer, |
| 101 | bounds: Bounds<Pixels>, |
| 102 | scale: f32, |
| 103 | input_handler: Option<PlatformInputHandler>, |
| 104 | decorations: WindowDecorations, |
| 105 | background_appearance: WindowBackgroundAppearance, |
| 106 | fullscreen: bool, |
| 107 | maximized: bool, |
| 108 | tiling: Tiling, |
| 109 | window_bounds: Bounds<Pixels>, |
| 110 | client: WaylandClientStatePtr, |
| 111 | handle: AnyWindowHandle, |
| 112 | active: bool, |
| 113 | hovered: bool, |
| 114 | in_progress_configure: Option<InProgressConfigure>, |
| 115 | resize_throttle: bool, |
| 116 | in_progress_window_controls: Option<WindowControls>, |
| 117 | window_controls: WindowControls, |
| 118 | client_inset: Option<Pixels>, |
| 119 | } |
| 120 | |
| 121 | #[derive(Clone)] |
| 122 | pub struct WaylandWindowStatePtr { |
| 123 | state: Rc<RefCell<WaylandWindowState>>, |
| 124 | callbacks: Rc<RefCell<Callbacks>>, |
| 125 | } |
| 126 | |
| 127 | impl WaylandWindowState { |
| 128 | pub(crate) fn new( |
| 129 | handle: AnyWindowHandle, |
| 130 | surface: wl_surface::WlSurface, |
| 131 | xdg_surface: xdg_surface::XdgSurface, |
| 132 | toplevel: xdg_toplevel::XdgToplevel, |
| 133 | decoration: Option<zxdg_toplevel_decoration_v1::ZxdgToplevelDecorationV1>, |
| 134 | appearance: WindowAppearance, |
| 135 | viewport: Option<wp_viewport::WpViewport>, |
| 136 | client: WaylandClientStatePtr, |
| 137 | globals: Globals, |
| 138 | gpu_context: &BladeContext, |
| 139 | options: WindowParams, |
| 140 | ) -> anyhow::Result<Self> { |
| 141 | let renderer = { |
| 142 | let raw_window = RawWindow { |
| 143 | window: surface.id().as_ptr().cast::<c_void>(), |
| 144 | display: surface |
| 145 | .backend() |
| 146 | .upgrade() |
| 147 | .unwrap() |
| 148 | .display_ptr() |
| 149 | .cast::<c_void>(), |
| 150 | }; |
| 151 | let config = BladeSurfaceConfig { |
| 152 | size: gpu::Extent { |
| 153 | width: options.bounds.size.width.0 as u32, |
| 154 | height: options.bounds.size.height.0 as u32, |
| 155 | depth: 1, |
| 156 | }, |
| 157 | transparent: true, |
| 158 | }; |
| 159 | BladeRenderer::new(gpu_context, &raw_window, config)? |
| 160 | }; |
| 161 | |
| 162 | Ok(Self { |
| 163 | xdg_surface, |
| 164 | acknowledged_first_configure: false, |
| 165 | surface, |
| 166 | decoration, |
| 167 | app_id: None, |
| 168 | blur: None, |
| 169 | toplevel, |
| 170 | viewport, |
| 171 | globals, |
| 172 | outputs: HashMap::default(), |
| 173 | display: None, |
| 174 | renderer, |
| 175 | bounds: options.bounds, |
| 176 | scale: 1.0, |
| 177 | input_handler: None, |
| 178 | decorations: WindowDecorations::Client, |
| 179 | background_appearance: WindowBackgroundAppearance::Opaque, |
| 180 | fullscreen: false, |
| 181 | pending_fullscreen: None, |
| 182 | maximized: false, |
| 183 | tiling: Tiling::default(), |
| 184 | window_bounds: options.bounds, |
| 185 | in_progress_configure: None, |
| 186 | resize_throttle: false, |
| 187 | client, |
| 188 | appearance, |
| 189 | handle, |
| 190 | active: false, |
| 191 | hovered: false, |
| 192 | in_progress_window_controls: None, |
| 193 | window_controls: WindowControls::default(), |
| 194 | client_inset: None, |
| 195 | }) |
| 196 | } |
| 197 | |
| 198 | pub fn is_transparent(&self) -> bool { |
| 199 | self.decorations == WindowDecorations::Client |
| 200 | || self.background_appearance != WindowBackgroundAppearance::Opaque |
| 201 | } |
| 202 | |
| 203 | pub fn primary_output_scale(&mut self) -> i32 { |
| 204 | let mut scale = 1; |
| 205 | let mut current_output = self.display.take(); |
| 206 | for (id, output) in self.outputs.iter() { |
| 207 | if let Some((_, output_data)) = ¤t_output { |
| 208 | if output.scale > output_data.scale { |
| 209 | current_output = Some((id.clone(), output.clone())); |
| 210 | } |
| 211 | } else { |
| 212 | current_output = Some((id.clone(), output.clone())); |
| 213 | } |
| 214 | scale = scale.max(output.scale); |
| 215 | } |
| 216 | self.display = current_output; |
| 217 | scale |
| 218 | } |
| 219 | |
| 220 | pub fn inset(&self) -> Pixels { |
| 221 | match self.decorations { |
| 222 | WindowDecorations::Server => px(0.0), |
| 223 | WindowDecorations::Client => self.client_inset.unwrap_or(px(0.0)), |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | pub(crate) struct WaylandWindow(pub WaylandWindowStatePtr); |
| 229 | pub enum ImeInput { |
| 230 | InsertText(String), |
| 231 | SetMarkedText(String), |
| 232 | UnmarkText, |
| 233 | DeleteText, |
| 234 | } |
| 235 | |
| 236 | impl Drop for WaylandWindow { |
| 237 | fn drop(&mut self) { |
| 238 | let mut state = self.0.state.borrow_mut(); |
| 239 | let surface_id = state.surface.id(); |
| 240 | let client = state.client.clone(); |
| 241 | |
| 242 | state.renderer.destroy(); |
| 243 | if let Some(decoration) = &state.decoration { |
| 244 | decoration.destroy(); |
| 245 | } |
| 246 | if let Some(blur) = &state.blur { |
| 247 | blur.release(); |
| 248 | } |
| 249 | state.toplevel.destroy(); |
| 250 | if let Some(viewport) = &state.viewport { |
| 251 | viewport.destroy(); |
| 252 | } |
| 253 | state.xdg_surface.destroy(); |
| 254 | state.surface.destroy(); |
| 255 | |
| 256 | let state_ptr = self.0.clone(); |
| 257 | state |
| 258 | .globals |
| 259 | .executor |
| 260 | .spawn(async move { |
| 261 | state_ptr.close(); |
| 262 | client.drop_window(&surface_id) |
| 263 | }) |
| 264 | .detach(); |
| 265 | drop(state); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | impl WaylandWindow { |
| 270 | fn borrow(&self) -> Ref<'_, WaylandWindowState> { |
| 271 | self.0.state.borrow() |
| 272 | } |
| 273 | |
| 274 | fn borrow_mut(&self) -> RefMut<'_, WaylandWindowState> { |
| 275 | self.0.state.borrow_mut() |
| 276 | } |
| 277 | |
| 278 | pub fn new( |
| 279 | handle: AnyWindowHandle, |
| 280 | globals: Globals, |
| 281 | gpu_context: &BladeContext, |
| 282 | client: WaylandClientStatePtr, |
| 283 | params: WindowParams, |
| 284 | appearance: WindowAppearance, |
| 285 | parent: Option<XdgToplevel>, |
| 286 | ) -> anyhow::Result<(Self, ObjectId)> { |
| 287 | let surface = globals.compositor.create_surface(&globals.qh, ()); |
| 288 | let xdg_surface = globals |
| 289 | .wm_base |
| 290 | .get_xdg_surface(&surface, &globals.qh, surface.id()); |
| 291 | let toplevel = xdg_surface.get_toplevel(&globals.qh, surface.id()); |
| 292 | |
| 293 | if params.kind == WindowKind::Floating { |
| 294 | toplevel.set_parent(parent.as_ref()); |
| 295 | } |
| 296 | |
| 297 | if let Some(size) = params.window_min_size { |
| 298 | toplevel.set_min_size(size.width.0 as i32, size.height.0 as i32); |
| 299 | } |
| 300 | |
| 301 | if let Some(fractional_scale_manager) = globals.fractional_scale_manager.as_ref() { |
| 302 | fractional_scale_manager.get_fractional_scale(&surface, &globals.qh, surface.id()); |
| 303 | } |
| 304 | |
| 305 | // Attempt to set up window decorations based on the requested configuration |
| 306 | let decoration = globals |
| 307 | .decoration_manager |
| 308 | .as_ref() |
| 309 | .map(|decoration_manager| { |
| 310 | decoration_manager.get_toplevel_decoration(&toplevel, &globals.qh, surface.id()) |
| 311 | }); |
| 312 | |
| 313 | let viewport = globals |
| 314 | .viewporter |
| 315 | .as_ref() |
| 316 | .map(|viewporter| viewporter.get_viewport(&surface, &globals.qh, ())); |
| 317 | |
| 318 | let this = Self(WaylandWindowStatePtr { |
| 319 | state: Rc::new(RefCell::new(WaylandWindowState::new( |
| 320 | handle, |
| 321 | surface.clone(), |
| 322 | xdg_surface, |
| 323 | toplevel, |
| 324 | decoration, |
| 325 | appearance, |
| 326 | viewport, |
| 327 | client, |
| 328 | globals, |
| 329 | gpu_context, |
| 330 | params, |
| 331 | )?)), |
| 332 | callbacks: Rc::new(RefCell::new(Callbacks::default())), |
| 333 | }); |
| 334 | |
| 335 | // Kick things off |
| 336 | surface.commit(); |
| 337 | |
| 338 | Ok((this, surface.id())) |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | impl WaylandWindowStatePtr { |
| 343 | pub fn handle(&self) -> AnyWindowHandle { |
| 344 | self.state.borrow().handle |
| 345 | } |
| 346 | |
| 347 | pub fn surface(&self) -> wl_surface::WlSurface { |
| 348 | self.state.borrow().surface.clone() |
| 349 | } |
| 350 | |
| 351 | pub fn toplevel(&self) -> xdg_toplevel::XdgToplevel { |
| 352 | self.state.borrow().toplevel.clone() |
| 353 | } |
| 354 | |
| 355 | pub fn ptr_eq(&self, other: &Self) -> bool { |
| 356 | Rc::ptr_eq(&self.state, &other.state) |
| 357 | } |
| 358 | |
| 359 | /// Asks for fullscreen on `output` once a frame has been presented. sway ignores the output of a |
| 360 | /// `set_fullscreen` sent before the window is mapped and fullscreens it where it stands. |
| 361 | pub fn fullscreen_on_next_frame(&self, output: wayland_client::protocol::wl_output::WlOutput) { |
| 362 | self.state.borrow_mut().pending_fullscreen = Some(output); |
| 363 | } |
| 364 | |
| 365 | /// Sends the fullscreen request queued by `fullscreen_on_next_frame`; called from a frame callback. |
| 366 | pub fn apply_pending_fullscreen(&self) { |
| 367 | let mut state = self.state.borrow_mut(); |
| 368 | if let Some(output) = state.pending_fullscreen.take() { |
| 369 | state.toplevel.set_fullscreen(Some(&output)); |
| 370 | state.surface.commit(); |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | pub fn frame(&self) { |
| 375 | let mut state = self.state.borrow_mut(); |
| 376 | state.surface.frame(&state.globals.qh, state.surface.id()); |
| 377 | state.resize_throttle = false; |
| 378 | drop(state); |
| 379 | |
| 380 | let mut cb = self.callbacks.borrow_mut(); |
| 381 | if let Some(fun) = cb.request_frame.as_mut() { |
| 382 | fun(Default::default()); |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | pub fn handle_xdg_surface_event(&self, event: xdg_surface::Event) { |
| 387 | if let xdg_surface::Event::Configure { serial } = event { |
| 388 | { |
| 389 | let mut state = self.state.borrow_mut(); |
| 390 | if let Some(window_controls) = state.in_progress_window_controls.take() { |
| 391 | state.window_controls = window_controls; |
| 392 | |
| 393 | drop(state); |
| 394 | let mut callbacks = self.callbacks.borrow_mut(); |
| 395 | if let Some(appearance_changed) = callbacks.appearance_changed.as_mut() { |
| 396 | appearance_changed(); |
| 397 | } |
| 398 | } |
| 399 | } |
| 400 | { |
| 401 | let mut state = self.state.borrow_mut(); |
| 402 | |
| 403 | if let Some(mut configure) = state.in_progress_configure.take() { |
| 404 | let got_unmaximized = state.maximized && !configure.maximized; |
| 405 | state.fullscreen = configure.fullscreen; |
| 406 | state.maximized = configure.maximized; |
| 407 | state.tiling = configure.tiling; |
| 408 | // Limit interactive resizes to once per vblank |
| 409 | if configure.resizing && state.resize_throttle { |
| 410 | return; |
| 411 | } else if configure.resizing { |
| 412 | state.resize_throttle = true; |
| 413 | } |
| 414 | if !configure.fullscreen && !configure.maximized { |
| 415 | configure.size = if got_unmaximized { |
| 416 | Some(state.window_bounds.size) |
| 417 | } else { |
| 418 | compute_outer_size(state.inset(), configure.size, state.tiling) |
| 419 | }; |
| 420 | if let Some(size) = configure.size { |
| 421 | state.window_bounds = Bounds { |
| 422 | origin: Point::default(), |
| 423 | size, |
| 424 | }; |
| 425 | } |
| 426 | } |
| 427 | drop(state); |
| 428 | if let Some(size) = configure.size { |
| 429 | self.resize(size); |
| 430 | } |
| 431 | } |
| 432 | } |
| 433 | let mut state = self.state.borrow_mut(); |
| 434 | state.xdg_surface.ack_configure(serial); |
| 435 | |
| 436 | let window_geometry = inset_by_tiling( |
| 437 | state.bounds.map_origin(|_| px(0.0)), |
| 438 | state.inset(), |
| 439 | state.tiling, |
| 440 | ) |
| 441 | .map(|v| v.0 as i32) |
| 442 | .map_size(|v| if v <= 0 { 1 } else { v }); |
| 443 | |
| 444 | state.xdg_surface.set_window_geometry( |
| 445 | window_geometry.origin.x, |
| 446 | window_geometry.origin.y, |
| 447 | window_geometry.size.width, |
| 448 | window_geometry.size.height, |
| 449 | ); |
| 450 | |
| 451 | let request_frame_callback = !state.acknowledged_first_configure; |
| 452 | if request_frame_callback { |
| 453 | state.acknowledged_first_configure = true; |
| 454 | drop(state); |
| 455 | self.frame(); |
| 456 | } |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | pub fn handle_toplevel_decoration_event(&self, event: zxdg_toplevel_decoration_v1::Event) { |
| 461 | if let zxdg_toplevel_decoration_v1::Event::Configure { mode } = event { |
| 462 | match mode { |
| 463 | WEnum::Value(zxdg_toplevel_decoration_v1::Mode::ServerSide) => { |
| 464 | self.state.borrow_mut().decorations = WindowDecorations::Server; |
| 465 | if let Some(mut appearance_changed) = |
| 466 | self.callbacks.borrow_mut().appearance_changed.as_mut() |
| 467 | { |
| 468 | appearance_changed(); |
| 469 | } |
| 470 | } |
| 471 | WEnum::Value(zxdg_toplevel_decoration_v1::Mode::ClientSide) => { |
| 472 | self.state.borrow_mut().decorations = WindowDecorations::Client; |
| 473 | // Update background to be transparent |
| 474 | if let Some(mut appearance_changed) = |
| 475 | self.callbacks.borrow_mut().appearance_changed.as_mut() |
| 476 | { |
| 477 | appearance_changed(); |
| 478 | } |
| 479 | } |
| 480 | WEnum::Value(_) => { |
| 481 | log::warn!("Unknown decoration mode"); |
| 482 | } |
| 483 | WEnum::Unknown(v) => { |
| 484 | log::warn!("Unknown decoration mode: {}", v); |
| 485 | } |
| 486 | } |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | pub fn handle_fractional_scale_event(&self, event: wp_fractional_scale_v1::Event) { |
| 491 | if let wp_fractional_scale_v1::Event::PreferredScale { scale } = event { |
| 492 | self.rescale(scale as f32 / 120.0); |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | pub fn handle_toplevel_event(&self, event: xdg_toplevel::Event) -> bool { |
| 497 | match event { |
| 498 | xdg_toplevel::Event::Configure { |
| 499 | width, |
| 500 | height, |
| 501 | states, |
| 502 | } => { |
| 503 | let mut size = if width == 0 || height == 0 { |
| 504 | None |
| 505 | } else { |
| 506 | Some(size(px(width as f32), px(height as f32))) |
| 507 | }; |
| 508 | |
| 509 | let states = extract_states::<xdg_toplevel::State>(&states); |
| 510 | |
| 511 | let mut tiling = Tiling::default(); |
| 512 | let mut fullscreen = false; |
| 513 | let mut maximized = false; |
| 514 | let mut resizing = false; |
| 515 | |
| 516 | for state in states { |
| 517 | match state { |
| 518 | xdg_toplevel::State::Maximized => { |
| 519 | maximized = true; |
| 520 | } |
| 521 | xdg_toplevel::State::Fullscreen => { |
| 522 | fullscreen = true; |
| 523 | } |
| 524 | xdg_toplevel::State::Resizing => resizing = true, |
| 525 | xdg_toplevel::State::TiledTop => { |
| 526 | tiling.top = true; |
| 527 | } |
| 528 | xdg_toplevel::State::TiledLeft => { |
| 529 | tiling.left = true; |
| 530 | } |
| 531 | xdg_toplevel::State::TiledRight => { |
| 532 | tiling.right = true; |
| 533 | } |
| 534 | xdg_toplevel::State::TiledBottom => { |
| 535 | tiling.bottom = true; |
| 536 | } |
| 537 | _ => { |
| 538 | // noop |
| 539 | } |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | if fullscreen || maximized { |
| 544 | tiling = Tiling::tiled(); |
| 545 | } |
| 546 | |
| 547 | let mut state = self.state.borrow_mut(); |
| 548 | state.in_progress_configure = Some(InProgressConfigure { |
| 549 | size, |
| 550 | fullscreen, |
| 551 | maximized, |
| 552 | resizing, |
| 553 | tiling, |
| 554 | }); |
| 555 | |
| 556 | false |
| 557 | } |
| 558 | xdg_toplevel::Event::Close => { |
| 559 | let mut cb = self.callbacks.borrow_mut(); |
| 560 | if let Some(mut should_close) = cb.should_close.take() { |
| 561 | let result = (should_close)(); |
| 562 | cb.should_close = Some(should_close); |
| 563 | if result { |
| 564 | drop(cb); |
| 565 | self.close(); |
| 566 | } |
| 567 | result |
| 568 | } else { |
| 569 | true |
| 570 | } |
| 571 | } |
| 572 | xdg_toplevel::Event::WmCapabilities { capabilities } => { |
| 573 | let mut window_controls = WindowControls::default(); |
| 574 | |
| 575 | let states = extract_states::<xdg_toplevel::WmCapabilities>(&capabilities); |
| 576 | |
| 577 | for state in states { |
| 578 | match state { |
| 579 | xdg_toplevel::WmCapabilities::Maximize => { |
| 580 | window_controls.maximize = true; |
| 581 | } |
| 582 | xdg_toplevel::WmCapabilities::Minimize => { |
| 583 | window_controls.minimize = true; |
| 584 | } |
| 585 | xdg_toplevel::WmCapabilities::Fullscreen => { |
| 586 | window_controls.fullscreen = true; |
| 587 | } |
| 588 | xdg_toplevel::WmCapabilities::WindowMenu => { |
| 589 | window_controls.window_menu = true; |
| 590 | } |
| 591 | _ => {} |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | let mut state = self.state.borrow_mut(); |
| 596 | state.in_progress_window_controls = Some(window_controls); |
| 597 | false |
| 598 | } |
| 599 | _ => false, |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | #[allow(clippy::mutable_key_type)] |
| 604 | pub fn handle_surface_event( |
| 605 | &self, |
| 606 | event: wl_surface::Event, |
| 607 | outputs: HashMap<ObjectId, Output>, |
| 608 | ) { |
| 609 | let mut state = self.state.borrow_mut(); |
| 610 | |
| 611 | match event { |
| 612 | wl_surface::Event::Enter { output } => { |
| 613 | let id = output.id(); |
| 614 | |
| 615 | let Some(output) = outputs.get(&id) else { |
| 616 | return; |
| 617 | }; |
| 618 | |
| 619 | state.outputs.insert(id, output.clone()); |
| 620 | |
| 621 | let scale = state.primary_output_scale(); |
| 622 | |
| 623 | // We use `PreferredBufferScale` instead to set the scale if it's available |
| 624 | if state.surface.version() < wl_surface::EVT_PREFERRED_BUFFER_SCALE_SINCE { |
| 625 | state.surface.set_buffer_scale(scale); |
| 626 | drop(state); |
| 627 | self.rescale(scale as f32); |
| 628 | } |
| 629 | } |
| 630 | wl_surface::Event::Leave { output } => { |
| 631 | state.outputs.remove(&output.id()); |
| 632 | |
| 633 | let scale = state.primary_output_scale(); |
| 634 | |
| 635 | // We use `PreferredBufferScale` instead to set the scale if it's available |
| 636 | if state.surface.version() < wl_surface::EVT_PREFERRED_BUFFER_SCALE_SINCE { |
| 637 | state.surface.set_buffer_scale(scale); |
| 638 | drop(state); |
| 639 | self.rescale(scale as f32); |
| 640 | } |
| 641 | } |
| 642 | wl_surface::Event::PreferredBufferScale { factor } => { |
| 643 | // We use `WpFractionalScale` instead to set the scale if it's available |
| 644 | if state.globals.fractional_scale_manager.is_none() { |
| 645 | state.surface.set_buffer_scale(factor); |
| 646 | drop(state); |
| 647 | self.rescale(factor as f32); |
| 648 | } |
| 649 | } |
| 650 | _ => {} |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | pub fn handle_ime(&self, ime: ImeInput) { |
| 655 | let mut state = self.state.borrow_mut(); |
| 656 | if let Some(mut input_handler) = state.input_handler.take() { |
| 657 | drop(state); |
| 658 | match ime { |
| 659 | ImeInput::InsertText(text) => { |
| 660 | input_handler.replace_text_in_range(None, &text); |
| 661 | } |
| 662 | ImeInput::SetMarkedText(text) => { |
| 663 | input_handler.replace_and_mark_text_in_range(None, &text, None); |
| 664 | } |
| 665 | ImeInput::UnmarkText => { |
| 666 | input_handler.unmark_text(); |
| 667 | } |
| 668 | ImeInput::DeleteText => { |
| 669 | if let Some(marked) = input_handler.marked_text_range() { |
| 670 | input_handler.replace_text_in_range(Some(marked), ""); |
| 671 | } |
| 672 | } |
| 673 | } |
| 674 | self.state.borrow_mut().input_handler = Some(input_handler); |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | pub fn get_ime_area(&self) -> Option<Bounds<Pixels>> { |
| 679 | let mut state = self.state.borrow_mut(); |
| 680 | let mut bounds: Option<Bounds<Pixels>> = None; |
| 681 | if let Some(mut input_handler) = state.input_handler.take() { |
| 682 | drop(state); |
| 683 | if let Some(selection) = input_handler.marked_text_range() { |
| 684 | bounds = input_handler.bounds_for_range(selection.start..selection.start); |
| 685 | } |
| 686 | self.state.borrow_mut().input_handler = Some(input_handler); |
| 687 | } |
| 688 | bounds |
| 689 | } |
| 690 | |
| 691 | pub fn set_size_and_scale(&self, size: Option<Size<Pixels>>, scale: Option<f32>) { |
| 692 | let (size, scale) = { |
| 693 | let mut state = self.state.borrow_mut(); |
| 694 | if size.is_none_or(|size| size == state.bounds.size) |
| 695 | && scale.is_none_or(|scale| scale == state.scale) |
| 696 | { |
| 697 | return; |
| 698 | } |
| 699 | if let Some(size) = size { |
| 700 | state.bounds.size = size; |
| 701 | } |
| 702 | if let Some(scale) = scale { |
| 703 | state.scale = scale; |
| 704 | } |
| 705 | let device_bounds = state.bounds.to_device_pixels(state.scale); |
| 706 | state.renderer.update_drawable_size(device_bounds.size); |
| 707 | (state.bounds.size, state.scale) |
| 708 | }; |
| 709 | |
| 710 | if let Some(ref mut fun) = self.callbacks.borrow_mut().resize { |
| 711 | fun(size, scale); |
| 712 | } |
| 713 | |
| 714 | { |
| 715 | let state = self.state.borrow(); |
| 716 | if let Some(viewport) = &state.viewport { |
| 717 | viewport.set_destination(size.width.0 as i32, size.height.0 as i32); |
| 718 | } |
| 719 | } |
| 720 | } |
| 721 | |
| 722 | pub fn resize(&self, size: Size<Pixels>) { |
| 723 | self.set_size_and_scale(Some(size), None); |
| 724 | } |
| 725 | |
| 726 | pub fn rescale(&self, scale: f32) { |
| 727 | self.set_size_and_scale(None, Some(scale)); |
| 728 | } |
| 729 | |
| 730 | pub fn close(&self) { |
| 731 | let mut callbacks = self.callbacks.borrow_mut(); |
| 732 | if let Some(fun) = callbacks.close.take() { |
| 733 | fun() |
| 734 | } |
| 735 | } |
| 736 | |
| 737 | pub fn handle_input(&self, input: PlatformInput) { |
| 738 | if let Some(ref mut fun) = self.callbacks.borrow_mut().input |
| 739 | && !fun(input.clone()).propagate |
| 740 | { |
| 741 | return; |
| 742 | } |
| 743 | if let PlatformInput::KeyDown(event) = input |
| 744 | && event.keystroke.modifiers.is_subset_of(&Modifiers::shift()) |
| 745 | && let Some(key_char) = &event.keystroke.key_char |
| 746 | { |
| 747 | let mut state = self.state.borrow_mut(); |
| 748 | if let Some(mut input_handler) = state.input_handler.take() { |
| 749 | drop(state); |
| 750 | input_handler.replace_text_in_range(None, key_char); |
| 751 | self.state.borrow_mut().input_handler = Some(input_handler); |
| 752 | } |
| 753 | } |
| 754 | } |
| 755 | |
| 756 | pub fn set_focused(&self, focus: bool) { |
| 757 | self.state.borrow_mut().active = focus; |
| 758 | if let Some(ref mut fun) = self.callbacks.borrow_mut().active_status_change { |
| 759 | fun(focus); |
| 760 | } |
| 761 | } |
| 762 | |
| 763 | pub fn set_hovered(&self, focus: bool) { |
| 764 | if let Some(ref mut fun) = self.callbacks.borrow_mut().hover_status_change { |
| 765 | fun(focus); |
| 766 | } |
| 767 | } |
| 768 | |
| 769 | pub fn set_appearance(&mut self, appearance: WindowAppearance) { |
| 770 | self.state.borrow_mut().appearance = appearance; |
| 771 | |
| 772 | let mut callbacks = self.callbacks.borrow_mut(); |
| 773 | if let Some(ref mut fun) = callbacks.appearance_changed { |
| 774 | (fun)() |
| 775 | } |
| 776 | } |
| 777 | |
| 778 | pub fn primary_output_scale(&self) -> i32 { |
| 779 | self.state.borrow_mut().primary_output_scale() |
| 780 | } |
| 781 | } |
| 782 | |
| 783 | fn extract_states<'a, S: TryFrom<u32> + 'a>(states: &'a [u8]) -> impl Iterator<Item = S> + 'a |
| 784 | where |
| 785 | <S as TryFrom<u32>>::Error: 'a, |
| 786 | { |
| 787 | states |
| 788 | .chunks_exact(4) |
| 789 | .flat_map(TryInto::<[u8; 4]>::try_into) |
| 790 | .map(u32::from_ne_bytes) |
| 791 | .flat_map(S::try_from) |
| 792 | } |
| 793 | |
| 794 | impl rwh::HasWindowHandle for WaylandWindow { |
| 795 | fn window_handle(&self) -> Result<rwh::WindowHandle<'_>, rwh::HandleError> { |
| 796 | let surface = self.0.surface().id().as_ptr() as *mut libc::c_void; |
| 797 | let c_ptr = NonNull::new(surface).ok_or(rwh::HandleError::Unavailable)?; |
| 798 | let handle = rwh::WaylandWindowHandle::new(c_ptr); |
| 799 | let raw_handle = rwh::RawWindowHandle::Wayland(handle); |
| 800 | Ok(unsafe { rwh::WindowHandle::borrow_raw(raw_handle) }) |
| 801 | } |
| 802 | } |
| 803 | |
| 804 | impl rwh::HasDisplayHandle for WaylandWindow { |
| 805 | fn display_handle(&self) -> Result<rwh::DisplayHandle<'_>, rwh::HandleError> { |
| 806 | let display = self |
| 807 | .0 |
| 808 | .surface() |
| 809 | .backend() |
| 810 | .upgrade() |
| 811 | .ok_or(rwh::HandleError::Unavailable)? |
| 812 | .display_ptr() as *mut libc::c_void; |
| 813 | |
| 814 | let c_ptr = NonNull::new(display).ok_or(rwh::HandleError::Unavailable)?; |
| 815 | let handle = rwh::WaylandDisplayHandle::new(c_ptr); |
| 816 | let raw_handle = rwh::RawDisplayHandle::Wayland(handle); |
| 817 | Ok(unsafe { rwh::DisplayHandle::borrow_raw(raw_handle) }) |
| 818 | } |
| 819 | } |
| 820 | |
| 821 | impl PlatformWindow for WaylandWindow { |
| 822 | fn bounds(&self) -> Bounds<Pixels> { |
| 823 | self.borrow().bounds |
| 824 | } |
| 825 | |
| 826 | fn is_maximized(&self) -> bool { |
| 827 | self.borrow().maximized |
| 828 | } |
| 829 | |
| 830 | fn window_bounds(&self) -> WindowBounds { |
| 831 | let state = self.borrow(); |
| 832 | if state.fullscreen { |
| 833 | WindowBounds::Fullscreen(state.window_bounds) |
| 834 | } else if state.maximized { |
| 835 | WindowBounds::Maximized(state.window_bounds) |
| 836 | } else { |
| 837 | drop(state); |
| 838 | WindowBounds::Windowed(self.bounds()) |
| 839 | } |
| 840 | } |
| 841 | |
| 842 | fn inner_window_bounds(&self) -> WindowBounds { |
| 843 | let state = self.borrow(); |
| 844 | if state.fullscreen { |
| 845 | WindowBounds::Fullscreen(state.window_bounds) |
| 846 | } else if state.maximized { |
| 847 | WindowBounds::Maximized(state.window_bounds) |
| 848 | } else { |
| 849 | let inset = state.inset(); |
| 850 | drop(state); |
| 851 | WindowBounds::Windowed(self.bounds().inset(inset)) |
| 852 | } |
| 853 | } |
| 854 | |
| 855 | fn content_size(&self) -> Size<Pixels> { |
| 856 | self.borrow().bounds.size |
| 857 | } |
| 858 | |
| 859 | fn resize(&mut self, size: Size<Pixels>) { |
| 860 | let state = self.borrow(); |
| 861 | let state_ptr = self.0.clone(); |
| 862 | let dp_size = size.to_device_pixels(self.scale_factor()); |
| 863 | |
| 864 | state.xdg_surface.set_window_geometry( |
| 865 | state.bounds.origin.x.0 as i32, |
| 866 | state.bounds.origin.y.0 as i32, |
| 867 | dp_size.width.0, |
| 868 | dp_size.height.0, |
| 869 | ); |
| 870 | |
| 871 | state |
| 872 | .globals |
| 873 | .executor |
| 874 | .spawn(async move { state_ptr.resize(size) }) |
| 875 | .detach(); |
| 876 | } |
| 877 | |
| 878 | fn scale_factor(&self) -> f32 { |
| 879 | self.borrow().scale |
| 880 | } |
| 881 | |
| 882 | fn appearance(&self) -> WindowAppearance { |
| 883 | self.borrow().appearance |
| 884 | } |
| 885 | |
| 886 | fn display(&self) -> Option<Rc<dyn PlatformDisplay>> { |
| 887 | let state = self.borrow(); |
| 888 | state.display.as_ref().map(|(id, display)| { |
| 889 | Rc::new(WaylandDisplay { |
| 890 | id: id.clone(), |
| 891 | name: display.name.clone(), |
| 892 | bounds: display.bounds.to_pixels(state.scale), |
| 893 | }) as Rc<dyn PlatformDisplay> |
| 894 | }) |
| 895 | } |
| 896 | |
| 897 | fn mouse_position(&self) -> Point<Pixels> { |
| 898 | self.borrow() |
| 899 | .client |
| 900 | .get_client() |
| 901 | .borrow() |
| 902 | .mouse_location |
| 903 | .unwrap_or_default() |
| 904 | } |
| 905 | |
| 906 | fn modifiers(&self) -> Modifiers { |
| 907 | self.borrow().client.get_client().borrow().modifiers |
| 908 | } |
| 909 | |
| 910 | fn capslock(&self) -> Capslock { |
| 911 | self.borrow().client.get_client().borrow().capslock |
| 912 | } |
| 913 | |
| 914 | fn set_input_handler(&mut self, input_handler: PlatformInputHandler) { |
| 915 | self.borrow_mut().input_handler = Some(input_handler); |
| 916 | } |
| 917 | |
| 918 | fn take_input_handler(&mut self) -> Option<PlatformInputHandler> { |
| 919 | self.borrow_mut().input_handler.take() |
| 920 | } |
| 921 | |
| 922 | fn prompt( |
| 923 | &self, |
| 924 | _level: PromptLevel, |
| 925 | _msg: &str, |
| 926 | _detail: Option<&str>, |
| 927 | _answers: &[PromptButton], |
| 928 | ) -> Option<Receiver<usize>> { |
| 929 | None |
| 930 | } |
| 931 | |
| 932 | fn activate(&self) { |
| 933 | // Try to request an activation token. Even though the activation is likely going to be rejected, |
| 934 | // KWin and Mutter can use the app_id to visually indicate we're requesting attention. |
| 935 | let state = self.borrow(); |
| 936 | if let (Some(activation), Some(app_id)) = (&state.globals.activation, state.app_id.clone()) |
| 937 | { |
| 938 | state.client.set_pending_activation(state.surface.id()); |
| 939 | let token = activation.get_activation_token(&state.globals.qh, ()); |
| 940 | // The serial isn't exactly important here, since the activation is probably going to be rejected anyway. |
| 941 | let serial = state.client.get_serial(SerialKind::MousePress); |
| 942 | token.set_app_id(app_id); |
| 943 | token.set_serial(serial, &state.globals.seat); |
| 944 | token.set_surface(&state.surface); |
| 945 | token.commit(); |
| 946 | } |
| 947 | } |
| 948 | |
| 949 | fn is_active(&self) -> bool { |
| 950 | self.borrow().active |
| 951 | } |
| 952 | |
| 953 | fn is_hovered(&self) -> bool { |
| 954 | self.borrow().hovered |
| 955 | } |
| 956 | |
| 957 | fn set_title(&mut self, title: &str) { |
| 958 | self.borrow().toplevel.set_title(title.to_string()); |
| 959 | } |
| 960 | |
| 961 | fn set_app_id(&mut self, app_id: &str) { |
| 962 | let mut state = self.borrow_mut(); |
| 963 | state.toplevel.set_app_id(app_id.to_owned()); |
| 964 | state.app_id = Some(app_id.to_owned()); |
| 965 | } |
| 966 | |
| 967 | fn set_background_appearance(&self, background_appearance: WindowBackgroundAppearance) { |
| 968 | let mut state = self.borrow_mut(); |
| 969 | state.background_appearance = background_appearance; |
| 970 | update_window(state); |
| 971 | } |
| 972 | |
| 973 | fn minimize(&self) { |
| 974 | self.borrow().toplevel.set_minimized(); |
| 975 | } |
| 976 | |
| 977 | fn zoom(&self) { |
| 978 | let state = self.borrow(); |
| 979 | if !state.maximized { |
| 980 | state.toplevel.set_maximized(); |
| 981 | } else { |
| 982 | state.toplevel.unset_maximized(); |
| 983 | } |
| 984 | } |
| 985 | |
| 986 | fn toggle_fullscreen(&self) { |
| 987 | let mut state = self.borrow_mut(); |
| 988 | if !state.fullscreen { |
| 989 | state.toplevel.set_fullscreen(None); |
| 990 | } else { |
| 991 | state.toplevel.unset_fullscreen(); |
| 992 | } |
| 993 | } |
| 994 | |
| 995 | fn is_fullscreen(&self) -> bool { |
| 996 | self.borrow().fullscreen |
| 997 | } |
| 998 | |
| 999 | fn on_request_frame(&self, callback: Box<dyn FnMut(RequestFrameOptions)>) { |
| 1000 | self.0.callbacks.borrow_mut().request_frame = Some(callback); |
| 1001 | } |
| 1002 | |
| 1003 | fn on_input(&self, callback: Box<dyn FnMut(PlatformInput) -> crate::DispatchEventResult>) { |
| 1004 | self.0.callbacks.borrow_mut().input = Some(callback); |
| 1005 | } |
| 1006 | |
| 1007 | fn on_active_status_change(&self, callback: Box<dyn FnMut(bool)>) { |
| 1008 | self.0.callbacks.borrow_mut().active_status_change = Some(callback); |
| 1009 | } |
| 1010 | |
| 1011 | fn on_hover_status_change(&self, callback: Box<dyn FnMut(bool)>) { |
| 1012 | self.0.callbacks.borrow_mut().hover_status_change = Some(callback); |
| 1013 | } |
| 1014 | |
| 1015 | fn on_resize(&self, callback: Box<dyn FnMut(Size<Pixels>, f32)>) { |
| 1016 | self.0.callbacks.borrow_mut().resize = Some(callback); |
| 1017 | } |
| 1018 | |
| 1019 | fn on_moved(&self, callback: Box<dyn FnMut()>) { |
| 1020 | self.0.callbacks.borrow_mut().moved = Some(callback); |
| 1021 | } |
| 1022 | |
| 1023 | fn on_should_close(&self, callback: Box<dyn FnMut() -> bool>) { |
| 1024 | self.0.callbacks.borrow_mut().should_close = Some(callback); |
| 1025 | } |
| 1026 | |
| 1027 | fn on_close(&self, callback: Box<dyn FnOnce()>) { |
| 1028 | self.0.callbacks.borrow_mut().close = Some(callback); |
| 1029 | } |
| 1030 | |
| 1031 | fn on_hit_test_window_control(&self, _callback: Box<dyn FnMut() -> Option<WindowControlArea>>) { |
| 1032 | } |
| 1033 | |
| 1034 | fn on_appearance_changed(&self, callback: Box<dyn FnMut()>) { |
| 1035 | self.0.callbacks.borrow_mut().appearance_changed = Some(callback); |
| 1036 | } |
| 1037 | |
| 1038 | fn draw(&self, scene: &Scene) { |
| 1039 | let mut state = self.borrow_mut(); |
| 1040 | state.renderer.draw(scene); |
| 1041 | } |
| 1042 | |
| 1043 | fn completed_frame(&self) { |
| 1044 | let state = self.borrow(); |
| 1045 | state.surface.commit(); |
| 1046 | } |
| 1047 | |
| 1048 | fn sprite_atlas(&self) -> Arc<dyn PlatformAtlas> { |
| 1049 | let state = self.borrow(); |
| 1050 | state.renderer.sprite_atlas().clone() |
| 1051 | } |
| 1052 | |
| 1053 | fn show_window_menu(&self, position: Point<Pixels>) { |
| 1054 | let state = self.borrow(); |
| 1055 | let serial = state.client.get_serial(SerialKind::MousePress); |
| 1056 | state.toplevel.show_window_menu( |
| 1057 | &state.globals.seat, |
| 1058 | serial, |
| 1059 | position.x.0 as i32, |
| 1060 | position.y.0 as i32, |
| 1061 | ); |
| 1062 | } |
| 1063 | |
| 1064 | fn start_window_move(&self) { |
| 1065 | let state = self.borrow(); |
| 1066 | let serial = state.client.get_serial(SerialKind::MousePress); |
| 1067 | state.toplevel._move(&state.globals.seat, serial); |
| 1068 | } |
| 1069 | |
| 1070 | fn start_window_resize(&self, edge: crate::ResizeEdge) { |
| 1071 | let state = self.borrow(); |
| 1072 | state.toplevel.resize( |
| 1073 | &state.globals.seat, |
| 1074 | state.client.get_serial(SerialKind::MousePress), |
| 1075 | edge.to_xdg(), |
| 1076 | ) |
| 1077 | } |
| 1078 | |
| 1079 | fn window_decorations(&self) -> Decorations { |
| 1080 | let state = self.borrow(); |
| 1081 | match state.decorations { |
| 1082 | WindowDecorations::Server => Decorations::Server, |
| 1083 | WindowDecorations::Client => Decorations::Client { |
| 1084 | tiling: state.tiling, |
| 1085 | }, |
| 1086 | } |
| 1087 | } |
| 1088 | |
| 1089 | fn request_decorations(&self, decorations: WindowDecorations) { |
| 1090 | let mut state = self.borrow_mut(); |
| 1091 | state.decorations = decorations; |
| 1092 | if let Some(decoration) = state.decoration.as_ref() { |
| 1093 | decoration.set_mode(decorations.to_xdg()); |
| 1094 | update_window(state); |
| 1095 | } |
| 1096 | } |
| 1097 | |
| 1098 | fn window_controls(&self) -> WindowControls { |
| 1099 | self.borrow().window_controls |
| 1100 | } |
| 1101 | |
| 1102 | fn set_client_inset(&self, inset: Pixels) { |
| 1103 | let mut state = self.borrow_mut(); |
| 1104 | if Some(inset) != state.client_inset { |
| 1105 | state.client_inset = Some(inset); |
| 1106 | update_window(state); |
| 1107 | } |
| 1108 | } |
| 1109 | |
| 1110 | fn update_ime_position(&self, bounds: Bounds<Pixels>) { |
| 1111 | let state = self.borrow(); |
| 1112 | state.client.update_ime_position(bounds); |
| 1113 | } |
| 1114 | |
| 1115 | fn gpu_specs(&self) -> Option<GpuSpecs> { |
| 1116 | self.borrow().renderer.gpu_specs().into() |
| 1117 | } |
| 1118 | } |
| 1119 | |
| 1120 | fn update_window(mut state: RefMut<WaylandWindowState>) { |
| 1121 | let opaque = !state.is_transparent(); |
| 1122 | |
| 1123 | state.renderer.update_transparency(!opaque); |
| 1124 | let mut opaque_area = state.window_bounds.map(|v| v.0 as i32); |
| 1125 | opaque_area.inset(state.inset().0 as i32); |
| 1126 | |
| 1127 | let region = state |
| 1128 | .globals |
| 1129 | .compositor |
| 1130 | .create_region(&state.globals.qh, ()); |
| 1131 | region.add( |
| 1132 | opaque_area.origin.x, |
| 1133 | opaque_area.origin.y, |
| 1134 | opaque_area.size.width, |
| 1135 | opaque_area.size.height, |
| 1136 | ); |
| 1137 | |
| 1138 | // Note that rounded corners make this rectangle API hard to work with. |
| 1139 | // As this is common when using CSD, let's just disable this API. |
| 1140 | if state.background_appearance == WindowBackgroundAppearance::Opaque |
| 1141 | && state.decorations == WindowDecorations::Server |
| 1142 | { |
| 1143 | // Promise the compositor that this region of the window surface |
| 1144 | // contains no transparent pixels. This allows the compositor to skip |
| 1145 | // updating whatever is behind the surface for better performance. |
| 1146 | state.surface.set_opaque_region(Some(®ion)); |
| 1147 | } else { |
| 1148 | state.surface.set_opaque_region(None); |
| 1149 | } |
| 1150 | |
| 1151 | if let Some(ref blur_manager) = state.globals.blur_manager { |
| 1152 | if state.background_appearance == WindowBackgroundAppearance::Blurred { |
| 1153 | if state.blur.is_none() { |
| 1154 | let blur = blur_manager.create(&state.surface, &state.globals.qh, ()); |
| 1155 | state.blur = Some(blur); |
| 1156 | } |
| 1157 | state.blur.as_ref().unwrap().commit(); |
| 1158 | } else { |
| 1159 | // It probably doesn't hurt to clear the blur for opaque windows |
| 1160 | blur_manager.unset(&state.surface); |
| 1161 | if let Some(b) = state.blur.take() { |
| 1162 | b.release() |
| 1163 | } |
| 1164 | } |
| 1165 | } |
| 1166 | |
| 1167 | region.destroy(); |
| 1168 | } |
| 1169 | |
| 1170 | impl WindowDecorations { |
| 1171 | fn to_xdg(self) -> zxdg_toplevel_decoration_v1::Mode { |
| 1172 | match self { |
| 1173 | WindowDecorations::Client => zxdg_toplevel_decoration_v1::Mode::ClientSide, |
| 1174 | WindowDecorations::Server => zxdg_toplevel_decoration_v1::Mode::ServerSide, |
| 1175 | } |
| 1176 | } |
| 1177 | } |
| 1178 | |
| 1179 | impl ResizeEdge { |
| 1180 | fn to_xdg(self) -> xdg_toplevel::ResizeEdge { |
| 1181 | match self { |
| 1182 | ResizeEdge::Top => xdg_toplevel::ResizeEdge::Top, |
| 1183 | ResizeEdge::TopRight => xdg_toplevel::ResizeEdge::TopRight, |
| 1184 | ResizeEdge::Right => xdg_toplevel::ResizeEdge::Right, |
| 1185 | ResizeEdge::BottomRight => xdg_toplevel::ResizeEdge::BottomRight, |
| 1186 | ResizeEdge::Bottom => xdg_toplevel::ResizeEdge::Bottom, |
| 1187 | ResizeEdge::BottomLeft => xdg_toplevel::ResizeEdge::BottomLeft, |
| 1188 | ResizeEdge::Left => xdg_toplevel::ResizeEdge::Left, |
| 1189 | ResizeEdge::TopLeft => xdg_toplevel::ResizeEdge::TopLeft, |
| 1190 | } |
| 1191 | } |
| 1192 | } |
| 1193 | |
| 1194 | /// The configuration event is in terms of the window geometry, which we are constantly |
| 1195 | /// updating to account for the client decorations. But that's not the area we want to render |
| 1196 | /// to, due to our intrusize CSD. So, here we calculate the 'actual' size, by adding back in the insets |
| 1197 | fn compute_outer_size( |
| 1198 | inset: Pixels, |
| 1199 | new_size: Option<Size<Pixels>>, |
| 1200 | tiling: Tiling, |
| 1201 | ) -> Option<Size<Pixels>> { |
| 1202 | new_size.map(|mut new_size| { |
| 1203 | if !tiling.top { |
| 1204 | new_size.height += inset; |
| 1205 | } |
| 1206 | if !tiling.bottom { |
| 1207 | new_size.height += inset; |
| 1208 | } |
| 1209 | if !tiling.left { |
| 1210 | new_size.width += inset; |
| 1211 | } |
| 1212 | if !tiling.right { |
| 1213 | new_size.width += inset; |
| 1214 | } |
| 1215 | |
| 1216 | new_size |
| 1217 | }) |
| 1218 | } |
| 1219 | |
| 1220 | fn inset_by_tiling(mut bounds: Bounds<Pixels>, inset: Pixels, tiling: Tiling) -> Bounds<Pixels> { |
| 1221 | if !tiling.top { |
| 1222 | bounds.origin.y += inset; |
| 1223 | bounds.size.height -= inset; |
| 1224 | } |
| 1225 | if !tiling.bottom { |
| 1226 | bounds.size.height -= inset; |
| 1227 | } |
| 1228 | if !tiling.left { |
| 1229 | bounds.origin.x += inset; |
| 1230 | bounds.size.width -= inset; |
| 1231 | } |
| 1232 | if !tiling.right { |
| 1233 | bounds.size.width -= inset; |
| 1234 | } |
| 1235 | |
| 1236 | bounds |
| 1237 | } |