Implement toggling pointer for the screenshot UI
Browse files at this commitf2766b103d46fd78d1eb71e0f40f2d470d5c9414Parent: 62c9d444 changed files +181 -34
- niri-config/src/lib.rs +2 -0
- src/input/mod.rs +4 -0
- src/niri.rs +35 -11
- src/ui/screenshot_ui.rs +140 -23
niri-config/src/lib.rsmodified+2 -0
| @@ -948,6 +948,8 @@ pub enum Action { | ||
| 948 | 948 | ConfirmScreenshot, |
| 949 | 949 | #[knuffel(skip)] |
| 950 | 950 | CancelScreenshot, |
| 951 | + #[knuffel(skip)] | |
| 952 | + ScreenshotTogglePointer, | |
| 951 | 953 | Screenshot, |
| 952 | 954 | ScreenshotScreen, |
| 953 | 955 | ScreenshotWindow, |
src/input/mod.rsmodified+4 -0
| @@ -508,6 +508,10 @@ impl State { | ||
| 508 | 508 | .set_cursor_image(CursorImageStatus::default_named()); |
| 509 | 509 | self.niri.queue_redraw_all(); |
| 510 | 510 | } |
| 511 | + Action::ScreenshotTogglePointer => { | |
| 512 | + self.niri.screenshot_ui.toggle_pointer(); | |
| 513 | + self.niri.queue_redraw_all(); | |
| 514 | + } | |
| 511 | 515 | Action::Screenshot => { |
| 512 | 516 | self.backend.with_primary_renderer(|renderer| { |
| 513 | 517 | self.niri.open_screenshot_ui(renderer); |
src/niri.rsmodified+35 -11
| @@ -124,13 +124,14 @@ use crate::render_helpers::primary_gpu_texture::PrimaryGpuTextureRenderElement; | ||
| 124 | 124 | use crate::render_helpers::renderer::NiriRenderer; |
| 125 | 125 | use crate::render_helpers::texture::TextureBuffer; |
| 126 | 126 | use crate::render_helpers::{ |
| 127 | - render_to_shm, render_to_texture, render_to_vec, shaders, RenderTarget, | |
| 127 | + render_to_encompassing_texture, render_to_shm, render_to_texture, render_to_vec, shaders, | |
| 128 | + RenderTarget, | |
| 128 | 129 | }; |
| 129 | 130 | use crate::ui::config_error_notification::ConfigErrorNotification; |
| 130 | 131 | use crate::ui::exit_confirm_dialog::ExitConfirmDialog; |
| 131 | 132 | use crate::ui::hotkey_overlay::HotkeyOverlay; |
| 132 | 133 | use crate::ui::screen_transition::{self, ScreenTransition}; |
| 133 | -use crate::ui::screenshot_ui::{ScreenshotUi, ScreenshotUiRenderElement}; | |
| 134 | +use crate::ui::screenshot_ui::{OutputScreenshot, ScreenshotUi, ScreenshotUiRenderElement}; | |
| 134 | 135 | use crate::utils::scale::{closest_representable_scale, guess_monitor_scale}; |
| 135 | 136 | use crate::utils::spawning::CHILD_ENV; |
| 136 | 137 | use crate::utils::{ |
| @@ -3790,8 +3791,8 @@ impl Niri { | ||
| 3790 | 3791 | RenderTarget::Screencast, |
| 3791 | 3792 | RenderTarget::ScreenCapture, |
| 3792 | 3793 | ]; |
| 3793 | - let textures = targets.map(|target| { | |
| 3794 | - let elements = self.render::<GlesRenderer>(renderer, &output, true, target); | |
| 3794 | + let screenshot = targets.map(|target| { | |
| 3795 | + let elements = self.render::<GlesRenderer>(renderer, &output, false, target); | |
| 3795 | 3796 | let elements = elements.iter().rev(); |
| 3796 | 3797 | |
| 3797 | 3798 | let res = render_to_texture( |
| @@ -3802,25 +3803,48 @@ impl Niri { | ||
| 3802 | 3803 | Fourcc::Abgr8888, |
| 3803 | 3804 | elements, |
| 3804 | 3805 | ); |
| 3805 | - | |
| 3806 | 3806 | if let Err(err) = &res { |
| 3807 | 3807 | warn!("error rendering output {}: {err:?}", output.name()); |
| 3808 | 3808 | } |
| 3809 | + let res_output = res.ok(); | |
| 3809 | 3810 | |
| 3810 | - res | |
| 3811 | + let pointer = self.pointer_element(renderer, &output); | |
| 3812 | + let res_pointer = if pointer.is_empty() { | |
| 3813 | + None | |
| 3814 | + } else { | |
| 3815 | + let res = render_to_encompassing_texture( | |
| 3816 | + renderer, | |
| 3817 | + scale, | |
| 3818 | + Transform::Normal, | |
| 3819 | + Fourcc::Abgr8888, | |
| 3820 | + &pointer, | |
| 3821 | + ); | |
| 3822 | + if let Err(err) = &res { | |
| 3823 | + warn!("error rendering pointer for {}: {err:?}", output.name()); | |
| 3824 | + } | |
| 3825 | + res.ok() | |
| 3826 | + }; | |
| 3827 | + | |
| 3828 | + res_output.map(|(texture, _)| { | |
| 3829 | + OutputScreenshot::from_textures( | |
| 3830 | + renderer, | |
| 3831 | + scale, | |
| 3832 | + texture, | |
| 3833 | + res_pointer.map(|(texture, _, geo)| (texture, geo)), | |
| 3834 | + ) | |
| 3835 | + }) | |
| 3811 | 3836 | }); |
| 3812 | 3837 | |
| 3813 | - if textures.iter().any(|res| res.is_err()) { | |
| 3838 | + if screenshot.iter().any(|res| res.is_none()) { | |
| 3814 | 3839 | return None; |
| 3815 | 3840 | } |
| 3816 | 3841 | |
| 3817 | - let textures = textures.map(|res| res.unwrap().0); | |
| 3818 | - Some((output, textures)) | |
| 3842 | + let screenshot = screenshot.map(|res| res.unwrap()); | |
| 3843 | + Some((output, screenshot)) | |
| 3819 | 3844 | }) |
| 3820 | 3845 | .collect(); |
| 3821 | 3846 | |
| 3822 | - self.screenshot_ui | |
| 3823 | - .open(renderer, screenshots, default_output); | |
| 3847 | + self.screenshot_ui.open(screenshots, default_output); | |
| 3824 | 3848 | self.cursor_manager |
| 3825 | 3849 | .set_cursor_image(CursorImageStatus::Named(CursorIcon::Crosshair)); |
| 3826 | 3850 | self.queue_redraw_all(); |
src/ui/screenshot_ui.rsmodified+140 -23
| @@ -13,13 +13,13 @@ use smithay::backend::renderer::gles::{GlesRenderer, GlesTexture}; | ||
| 13 | 13 | use smithay::backend::renderer::ExportMem; |
| 14 | 14 | use smithay::input::keyboard::{Keysym, ModifiersState}; |
| 15 | 15 | use smithay::output::{Output, WeakOutput}; |
| 16 | -use smithay::utils::{Physical, Point, Rectangle, Size, Transform}; | |
| 16 | +use smithay::utils::{Logical, Physical, Point, Rectangle, Scale, Size, Transform}; | |
| 17 | 17 | |
| 18 | 18 | use crate::niri_render_elements; |
| 19 | 19 | use crate::render_helpers::primary_gpu_texture::PrimaryGpuTextureRenderElement; |
| 20 | 20 | use crate::render_helpers::solid_color::{SolidColorBuffer, SolidColorRenderElement}; |
| 21 | 21 | use crate::render_helpers::texture::{TextureBuffer, TextureRenderElement}; |
| 22 | -use crate::render_helpers::RenderTarget; | |
| 22 | +use crate::render_helpers::{render_to_texture, RenderTarget}; | |
| 23 | 23 | use crate::utils::to_physical_precise_round; |
| 24 | 24 | |
| 25 | 25 | const BORDER: i32 = 2; |
| @@ -37,6 +37,7 @@ pub enum ScreenshotUi { | ||
| 37 | 37 | selection: (Output, Point<i32, Physical>, Point<i32, Physical>), |
| 38 | 38 | output_data: HashMap<Output, OutputData>, |
| 39 | 39 | mouse_down: bool, |
| 40 | + show_pointer: bool, | |
| 40 | 41 | }, |
| 41 | 42 | } |
| 42 | 43 | |
| @@ -45,12 +46,17 @@ pub struct OutputData { | ||
| 45 | 46 | scale: f64, |
| 46 | 47 | transform: Transform, |
| 47 | 48 | // Output, screencast, screen capture. |
| 48 | - texture: [GlesTexture; 3], | |
| 49 | - texture_buffer: [TextureBuffer<GlesTexture>; 3], | |
| 49 | + screenshot: [OutputScreenshot; 3], | |
| 50 | 50 | buffers: [SolidColorBuffer; 8], |
| 51 | 51 | locations: [Point<i32, Physical>; 8], |
| 52 | 52 | } |
| 53 | 53 | |
| 54 | +pub struct OutputScreenshot { | |
| 55 | + texture: GlesTexture, | |
| 56 | + buffer: TextureBuffer<GlesTexture>, | |
| 57 | + pointer: Option<(TextureBuffer<GlesTexture>, Rectangle<f64, Logical>)>, | |
| 58 | +} | |
| 59 | + | |
| 54 | 60 | niri_render_elements! { |
| 55 | 61 | ScreenshotUiRenderElement => { |
| 56 | 62 | Screenshot = PrimaryGpuTextureRenderElement, |
| @@ -67,9 +73,8 @@ impl ScreenshotUi { | ||
| 67 | 73 | |
| 68 | 74 | pub fn open( |
| 69 | 75 | &mut self, |
| 70 | - renderer: &GlesRenderer, | |
| 71 | 76 | // Output, screencast, screen capture. |
| 72 | - screenshots: HashMap<Output, [GlesTexture; 3]>, | |
| 77 | + screenshots: HashMap<Output, [OutputScreenshot; 3]>, | |
| 73 | 78 | default_output: Output, |
| 74 | 79 | ) -> bool { |
| 75 | 80 | if screenshots.is_empty() { |
| @@ -108,20 +113,11 @@ impl ScreenshotUi { | ||
| 108 | 113 | |
| 109 | 114 | let output_data = screenshots |
| 110 | 115 | .into_iter() |
| 111 | - .map(|(output, texture)| { | |
| 116 | + .map(|(output, screenshot)| { | |
| 112 | 117 | let transform = output.current_transform(); |
| 113 | 118 | let output_mode = output.current_mode().unwrap(); |
| 114 | 119 | let size = transform.transform_size(output_mode.size); |
| 115 | 120 | let scale = output.current_scale().fractional_scale(); |
| 116 | - let texture_buffer = texture.clone().map(|texture| { | |
| 117 | - TextureBuffer::from_texture( | |
| 118 | - renderer, | |
| 119 | - texture, | |
| 120 | - scale, | |
| 121 | - Transform::Normal, | |
| 122 | - Vec::new(), | |
| 123 | - ) | |
| 124 | - }); | |
| 125 | 121 | let buffers = [ |
| 126 | 122 | SolidColorBuffer::new((0., 0.), [1., 1., 1., 1.]), |
| 127 | 123 | SolidColorBuffer::new((0., 0.), [1., 1., 1., 1.]), |
| @@ -137,8 +133,7 @@ impl ScreenshotUi { | ||
| 137 | 133 | size, |
| 138 | 134 | scale, |
| 139 | 135 | transform, |
| 140 | - texture, | |
| 141 | - texture_buffer, | |
| 136 | + screenshot, | |
| 142 | 137 | buffers, |
| 143 | 138 | locations, |
| 144 | 139 | }; |
| @@ -150,6 +145,7 @@ impl ScreenshotUi { | ||
| 150 | 145 | selection, |
| 151 | 146 | output_data, |
| 152 | 147 | mouse_down: false, |
| 148 | + show_pointer: true, | |
| 153 | 149 | }; |
| 154 | 150 | |
| 155 | 151 | self.update_buffers(); |
| @@ -177,6 +173,12 @@ impl ScreenshotUi { | ||
| 177 | 173 | true |
| 178 | 174 | } |
| 179 | 175 | |
| 176 | + pub fn toggle_pointer(&mut self) { | |
| 177 | + if let Self::Open { show_pointer, .. } = self { | |
| 178 | + *show_pointer = !*show_pointer; | |
| 179 | + } | |
| 180 | + } | |
| 181 | + | |
| 180 | 182 | pub fn is_open(&self) -> bool { |
| 181 | 183 | matches!(self, ScreenshotUi::Open { .. }) |
| 182 | 184 | } |
| @@ -258,10 +260,15 @@ impl ScreenshotUi { | ||
| 258 | 260 | &self, |
| 259 | 261 | output: &Output, |
| 260 | 262 | target: RenderTarget, |
| 261 | - ) -> ArrayVec<ScreenshotUiRenderElement, 9> { | |
| 263 | + ) -> ArrayVec<ScreenshotUiRenderElement, 10> { | |
| 262 | 264 | let _span = tracy_client::span!("ScreenshotUi::render_output"); |
| 263 | 265 | |
| 264 | - let Self::Open { output_data, .. } = self else { | |
| 266 | + let Self::Open { | |
| 267 | + output_data, | |
| 268 | + show_pointer, | |
| 269 | + .. | |
| 270 | + } = self | |
| 271 | + else { | |
| 265 | 272 | panic!("screenshot UI must be open to render it"); |
| 266 | 273 | }; |
| 267 | 274 | |
| @@ -288,9 +295,26 @@ impl ScreenshotUi { | ||
| 288 | 295 | RenderTarget::Screencast => 1, |
| 289 | 296 | RenderTarget::ScreenCapture => 2, |
| 290 | 297 | }; |
| 298 | + | |
| 299 | + if *show_pointer { | |
| 300 | + if let Some((buffer, geo)) = output_data.screenshot[index].pointer.clone() { | |
| 301 | + elements.push( | |
| 302 | + PrimaryGpuTextureRenderElement(TextureRenderElement::from_texture_buffer( | |
| 303 | + buffer, | |
| 304 | + geo.loc, | |
| 305 | + 1., | |
| 306 | + None, | |
| 307 | + None, | |
| 308 | + Kind::Unspecified, | |
| 309 | + )) | |
| 310 | + .into(), | |
| 311 | + ); | |
| 312 | + } | |
| 313 | + } | |
| 314 | + | |
| 291 | 315 | elements.push( |
| 292 | 316 | PrimaryGpuTextureRenderElement(TextureRenderElement::from_texture_buffer( |
| 293 | - output_data.texture_buffer[index].clone(), | |
| 317 | + output_data.screenshot[index].buffer.clone(), | |
| 294 | 318 | (0., 0.), |
| 295 | 319 | 1., |
| 296 | 320 | None, |
| @@ -312,6 +336,7 @@ impl ScreenshotUi { | ||
| 312 | 336 | let Self::Open { |
| 313 | 337 | selection, |
| 314 | 338 | output_data, |
| 339 | + show_pointer, | |
| 315 | 340 | .. |
| 316 | 341 | } = self |
| 317 | 342 | else { |
| @@ -320,12 +345,67 @@ impl ScreenshotUi { | ||
| 320 | 345 | |
| 321 | 346 | let data = &output_data[&selection.0]; |
| 322 | 347 | let rect = rect_from_corner_points(selection.1, selection.2); |
| 348 | + | |
| 349 | + let screenshot = &data.screenshot[0]; | |
| 350 | + | |
| 351 | + // Composite the pointer on top if needed. | |
| 352 | + let mut tex_rect = None; | |
| 353 | + if *show_pointer { | |
| 354 | + if let Some((buffer, geo)) = screenshot.pointer.clone() { | |
| 355 | + let scale = buffer.texture_scale(); | |
| 356 | + let offset = rect.loc.to_f64().to_logical(scale); | |
| 357 | + let offset = offset.upscale(-1.); | |
| 358 | + | |
| 359 | + let mut elements = ArrayVec::<_, 2>::new(); | |
| 360 | + elements.push(PrimaryGpuTextureRenderElement( | |
| 361 | + TextureRenderElement::from_texture_buffer( | |
| 362 | + buffer, | |
| 363 | + geo.loc + offset, | |
| 364 | + 1., | |
| 365 | + None, | |
| 366 | + None, | |
| 367 | + Kind::Unspecified, | |
| 368 | + ), | |
| 369 | + )); | |
| 370 | + elements.push(PrimaryGpuTextureRenderElement( | |
| 371 | + TextureRenderElement::from_texture_buffer( | |
| 372 | + screenshot.buffer.clone(), | |
| 373 | + offset, | |
| 374 | + 1., | |
| 375 | + None, | |
| 376 | + None, | |
| 377 | + Kind::Unspecified, | |
| 378 | + ), | |
| 379 | + )); | |
| 380 | + let elements = elements.iter().rev(); | |
| 381 | + | |
| 382 | + let res = render_to_texture( | |
| 383 | + renderer, | |
| 384 | + rect.size, | |
| 385 | + scale, | |
| 386 | + Transform::Normal, | |
| 387 | + Fourcc::Abgr8888, | |
| 388 | + elements, | |
| 389 | + ); | |
| 390 | + match res { | |
| 391 | + Ok((texture, _)) => { | |
| 392 | + tex_rect = Some((texture, Rectangle::from_loc_and_size((0, 0), rect.size))); | |
| 393 | + } | |
| 394 | + Err(err) => { | |
| 395 | + warn!("error compositing pointer onto screenshot: {err:?}"); | |
| 396 | + } | |
| 397 | + } | |
| 398 | + } | |
| 399 | + } | |
| 400 | + | |
| 401 | + let (texture, rect) = tex_rect.unwrap_or_else(|| (screenshot.texture.clone(), rect)); | |
| 402 | + // The size doesn't actually matter because we're not transforming anything. | |
| 323 | 403 | let buf_rect = rect |
| 324 | 404 | .to_logical(1) |
| 325 | - .to_buffer(1, Transform::Normal, &data.size.to_logical(1)); | |
| 405 | + .to_buffer(1, Transform::Normal, &Size::from((1, 1))); | |
| 326 | 406 | |
| 327 | 407 | let mapping = renderer |
| 328 | - .copy_texture(&data.texture[0], buf_rect, Fourcc::Abgr8888) | |
| 408 | + .copy_texture(&texture, buf_rect, Fourcc::Abgr8888) | |
| 329 | 409 | .context("error copying texture")?; |
| 330 | 410 | let copy = renderer |
| 331 | 411 | .map_texture(&mapping) |
| @@ -389,6 +469,7 @@ impl ScreenshotUi { | ||
| 389 | 469 | selection, |
| 390 | 470 | output_data, |
| 391 | 471 | mouse_down, |
| 472 | + .. | |
| 392 | 473 | } = self |
| 393 | 474 | else { |
| 394 | 475 | return false; |
| @@ -438,6 +519,38 @@ impl Default for ScreenshotUi { | ||
| 438 | 519 | } |
| 439 | 520 | } |
| 440 | 521 | |
| 522 | +impl OutputScreenshot { | |
| 523 | + pub fn from_textures( | |
| 524 | + renderer: &mut GlesRenderer, | |
| 525 | + scale: Scale<f64>, | |
| 526 | + texture: GlesTexture, | |
| 527 | + pointer: Option<(GlesTexture, Rectangle<i32, Physical>)>, | |
| 528 | + ) -> Self { | |
| 529 | + Self { | |
| 530 | + texture: texture.clone(), | |
| 531 | + buffer: TextureBuffer::from_texture( | |
| 532 | + renderer, | |
| 533 | + texture, | |
| 534 | + scale, | |
| 535 | + Transform::Normal, | |
| 536 | + Vec::new(), | |
| 537 | + ), | |
| 538 | + pointer: pointer.map(|(texture, geo)| { | |
| 539 | + ( | |
| 540 | + TextureBuffer::from_texture( | |
| 541 | + renderer, | |
| 542 | + texture, | |
| 543 | + scale, | |
| 544 | + Transform::Normal, | |
| 545 | + Vec::new(), | |
| 546 | + ), | |
| 547 | + geo.to_f64().to_logical(scale), | |
| 548 | + ) | |
| 549 | + }), | |
| 550 | + } | |
| 551 | + } | |
| 552 | +} | |
| 553 | + | |
| 441 | 554 | fn action(raw: Keysym, mods: ModifiersState) -> Option<Action> { |
| 442 | 555 | if raw == Keysym::Escape { |
| 443 | 556 | return Some(Action::CancelScreenshot); |
| @@ -453,6 +566,10 @@ fn action(raw: Keysym, mods: ModifiersState) -> Option<Action> { | ||
| 453 | 566 | return Some(Action::ConfirmScreenshot); |
| 454 | 567 | } |
| 455 | 568 | |
| 569 | + if !mods.ctrl && raw == Keysym::p { | |
| 570 | + return Some(Action::ScreenshotTogglePointer); | |
| 571 | + } | |
| 572 | + | |
| 456 | 573 | None |
| 457 | 574 | } |
| 458 | 575 |