screenshot-ui: Add a help panel
Browse files at this commit236f96e6764f4157050c5294df1bb15b9f1eac37Parent: 887ca972 changed files +134 -6
- src/niri.rs +6 -1
- src/ui/screenshot_ui.rs +128 -5
src/niri.rsmodified+6 -1
| @@ -1240,7 +1240,12 @@ impl State { | |||
| 1240 | get_monotonic_time().as_millis() as u32, | 1240 | get_monotonic_time().as_millis() as u32, |
| 1241 | ); | 1241 | ); |
| 1242 | 1242 | ||
| 1243 | - self.niri.screenshot_ui.open(screenshots, default_output); | 1243 | + self.backend.with_primary_renderer(|renderer| { |
| 1244 | + self.niri | ||
| 1245 | + .screenshot_ui | ||
| 1246 | + .open(renderer, screenshots, default_output) | ||
| 1247 | + }); | ||
| 1248 | + | ||
| 1244 | self.niri | 1249 | self.niri |
| 1245 | .cursor_manager | 1250 | .cursor_manager |
| 1246 | .set_cursor_image(CursorImageStatus::Named(CursorIcon::Crosshair)); | 1251 | .set_cursor_image(CursorImageStatus::Named(CursorIcon::Crosshair)); |
src/ui/screenshot_ui.rsmodified+128 -5
| @@ -6,12 +6,14 @@ use std::mem; | |||
| 6 | use anyhow::Context; | 6 | use anyhow::Context; |
| 7 | use arrayvec::ArrayVec; | 7 | use arrayvec::ArrayVec; |
| 8 | use niri_config::Action; | 8 | use niri_config::Action; |
| 9 | +use pango::{Alignment, FontDescription}; | ||
| 10 | +use pangocairo::cairo::{self, ImageSurface}; | ||
| 9 | use smithay::backend::allocator::Fourcc; | 11 | use smithay::backend::allocator::Fourcc; |
| 10 | use smithay::backend::input::{ButtonState, MouseButton}; | 12 | use smithay::backend::input::{ButtonState, MouseButton}; |
| 11 | use smithay::backend::renderer::element::utils::{Relocate, RelocateRenderElement}; | 13 | use smithay::backend::renderer::element::utils::{Relocate, RelocateRenderElement}; |
| 12 | use smithay::backend::renderer::element::Kind; | 14 | use smithay::backend::renderer::element::Kind; |
| 13 | use smithay::backend::renderer::gles::{GlesRenderer, GlesTexture}; | 15 | use smithay::backend::renderer::gles::{GlesRenderer, GlesTexture}; |
| 14 | -use smithay::backend::renderer::ExportMem; | 16 | +use smithay::backend::renderer::{ExportMem, Texture as _}; |
| 15 | use smithay::input::keyboard::{Keysym, ModifiersState}; | 17 | use smithay::input::keyboard::{Keysym, ModifiersState}; |
| 16 | use smithay::output::{Output, WeakOutput}; | 18 | use smithay::output::{Output, WeakOutput}; |
| 17 | use smithay::utils::{Physical, Point, Rectangle, Scale, Size, Transform}; | 19 | use smithay::utils::{Physical, Point, Rectangle, Scale, Size, Transform}; |
| @@ -23,7 +25,17 @@ use crate::render_helpers::texture::{TextureBuffer, TextureRenderElement}; | |||
| 23 | use crate::render_helpers::{render_to_texture, RenderTarget}; | 25 | use crate::render_helpers::{render_to_texture, RenderTarget}; |
| 24 | use crate::utils::to_physical_precise_round; | 26 | use crate::utils::to_physical_precise_round; |
| 25 | 27 | ||
| 26 | -const BORDER: i32 = 2; | 28 | +const SELECTION_BORDER: i32 = 2; |
| 29 | + | ||
| 30 | +const PADDING: i32 = 8; | ||
| 31 | +const FONT: &str = "sans 14px"; | ||
| 32 | +const BORDER: i32 = 4; | ||
| 33 | +const TEXT_HIDE_P: &str = | ||
| 34 | + "Press <span face='mono' bgcolor='#2C2C2C'> Space </span> to save the screenshot.\n\ | ||
| 35 | + Press <span face='mono' bgcolor='#2C2C2C'> P </span> to hide the pointer."; | ||
| 36 | +const TEXT_SHOW_P: &str = | ||
| 37 | + "Press <span face='mono' bgcolor='#2C2C2C'> Space </span> to save the screenshot.\n\ | ||
| 38 | + Press <span face='mono' bgcolor='#2C2C2C'> P </span> to show the pointer."; | ||
| 27 | 39 | ||
| 28 | // Ideally the screenshot UI should support cross-output selections. However, that poses some | 40 | // Ideally the screenshot UI should support cross-output selections. However, that poses some |
| 29 | // technical challenges when the outputs have different scales and such. So, this implementation | 41 | // technical challenges when the outputs have different scales and such. So, this implementation |
| @@ -50,6 +62,7 @@ pub struct OutputData { | |||
| 50 | screenshot: [OutputScreenshot; 3], | 62 | screenshot: [OutputScreenshot; 3], |
| 51 | buffers: [SolidColorBuffer; 8], | 63 | buffers: [SolidColorBuffer; 8], |
| 52 | locations: [Point<i32, Physical>; 8], | 64 | locations: [Point<i32, Physical>; 8], |
| 65 | + panel: Option<(TextureBuffer<GlesTexture>, TextureBuffer<GlesTexture>)>, | ||
| 53 | } | 66 | } |
| 54 | 67 | ||
| 55 | pub struct OutputScreenshot { | 68 | pub struct OutputScreenshot { |
| @@ -74,6 +87,7 @@ impl ScreenshotUi { | |||
| 74 | 87 | ||
| 75 | pub fn open( | 88 | pub fn open( |
| 76 | &mut self, | 89 | &mut self, |
| 90 | + renderer: &mut GlesRenderer, | ||
| 77 | // Output, screencast, screen capture. | 91 | // Output, screencast, screen capture. |
| 78 | screenshots: HashMap<Output, [OutputScreenshot; 3]>, | 92 | screenshots: HashMap<Output, [OutputScreenshot; 3]>, |
| 79 | default_output: Output, | 93 | default_output: Output, |
| @@ -130,6 +144,16 @@ impl ScreenshotUi { | |||
| 130 | SolidColorBuffer::new((0., 0.), [0., 0., 0., 0.5]), | 144 | SolidColorBuffer::new((0., 0.), [0., 0., 0., 0.5]), |
| 131 | ]; | 145 | ]; |
| 132 | let locations = [Default::default(); 8]; | 146 | let locations = [Default::default(); 8]; |
| 147 | + | ||
| 148 | + let mut render_panel_ = |text| { | ||
| 149 | + render_panel(renderer, scale, text) | ||
| 150 | + .map_err(|err| warn!("error rendering help panel: {err:?}")) | ||
| 151 | + .ok() | ||
| 152 | + }; | ||
| 153 | + let panel_show = render_panel_(TEXT_SHOW_P); | ||
| 154 | + let panel_hide = render_panel_(TEXT_HIDE_P); | ||
| 155 | + let panel = Option::zip(panel_show, panel_hide); | ||
| 156 | + | ||
| 133 | let data = OutputData { | 157 | let data = OutputData { |
| 134 | size, | 158 | size, |
| 135 | scale, | 159 | scale, |
| @@ -137,6 +161,7 @@ impl ScreenshotUi { | |||
| 137 | screenshot, | 161 | screenshot, |
| 138 | buffers, | 162 | buffers, |
| 139 | locations, | 163 | locations, |
| 164 | + panel, | ||
| 140 | }; | 165 | }; |
| 141 | (output, data) | 166 | (output, data) |
| 142 | }) | 167 | }) |
| @@ -214,7 +239,7 @@ impl ScreenshotUi { | |||
| 214 | *b = rect.loc + rect.size - Size::from((1, 1)); | 239 | *b = rect.loc + rect.size - Size::from((1, 1)); |
| 215 | } | 240 | } |
| 216 | 241 | ||
| 217 | - let border = to_physical_precise_round(scale, BORDER); | 242 | + let border = to_physical_precise_round(scale, SELECTION_BORDER); |
| 218 | 243 | ||
| 219 | let resize = move |buffer: &mut SolidColorBuffer, w: i32, h: i32| { | 244 | let resize = move |buffer: &mut SolidColorBuffer, w: i32, h: i32| { |
| 220 | let size = Size::<_, Physical>::from((w, h)); | 245 | let size = Size::<_, Physical>::from((w, h)); |
| @@ -261,12 +286,13 @@ impl ScreenshotUi { | |||
| 261 | &self, | 286 | &self, |
| 262 | output: &Output, | 287 | output: &Output, |
| 263 | target: RenderTarget, | 288 | target: RenderTarget, |
| 264 | - ) -> ArrayVec<ScreenshotUiRenderElement, 10> { | 289 | + ) -> ArrayVec<ScreenshotUiRenderElement, 11> { |
| 265 | let _span = tracy_client::span!("ScreenshotUi::render_output"); | 290 | let _span = tracy_client::span!("ScreenshotUi::render_output"); |
| 266 | 291 | ||
| 267 | let Self::Open { | 292 | let Self::Open { |
| 268 | output_data, | 293 | output_data, |
| 269 | show_pointer, | 294 | show_pointer, |
| 295 | + mouse_down, | ||
| 270 | .. | 296 | .. |
| 271 | } = self | 297 | } = self |
| 272 | else { | 298 | else { |
| @@ -279,11 +305,38 @@ impl ScreenshotUi { | |||
| 279 | return elements; | 305 | return elements; |
| 280 | }; | 306 | }; |
| 281 | 307 | ||
| 308 | + let scale = output_data.scale; | ||
| 309 | + | ||
| 310 | + // The help panel goes on top. | ||
| 311 | + if let Some((show, hide)) = &output_data.panel { | ||
| 312 | + let buffer = if *show_pointer { hide } else { show }; | ||
| 313 | + | ||
| 314 | + let size = buffer.texture().size(); | ||
| 315 | + let padding: i32 = to_physical_precise_round(scale, PADDING); | ||
| 316 | + let x = max(0, (output_data.size.w - size.w) / 2); | ||
| 317 | + let y = max(0, output_data.size.h - size.h - padding * 2); | ||
| 318 | + let location = Point::<_, Physical>::from((x, y)) | ||
| 319 | + .to_f64() | ||
| 320 | + .to_logical(scale); | ||
| 321 | + | ||
| 322 | + let alpha = if *mouse_down { 0.3 } else { 0.9 }; | ||
| 323 | + | ||
| 324 | + let elem = PrimaryGpuTextureRenderElement(TextureRenderElement::from_texture_buffer( | ||
| 325 | + buffer.clone(), | ||
| 326 | + location, | ||
| 327 | + alpha, | ||
| 328 | + None, | ||
| 329 | + None, | ||
| 330 | + Kind::Unspecified, | ||
| 331 | + )); | ||
| 332 | + elements.push(elem.into()); | ||
| 333 | + } | ||
| 334 | + | ||
| 282 | let buf_loc = zip(&output_data.buffers, &output_data.locations); | 335 | let buf_loc = zip(&output_data.buffers, &output_data.locations); |
| 283 | elements.extend(buf_loc.map(|(buffer, loc)| { | 336 | elements.extend(buf_loc.map(|(buffer, loc)| { |
| 284 | SolidColorRenderElement::from_buffer( | 337 | SolidColorRenderElement::from_buffer( |
| 285 | buffer, | 338 | buffer, |
| 286 | - loc.to_f64().to_logical(output_data.scale), | 339 | + loc.to_f64().to_logical(scale), |
| 287 | 1., | 340 | 1., |
| 288 | Kind::Unspecified, | 341 | Kind::Unspecified, |
| 289 | ) | 342 | ) |
| @@ -564,3 +617,73 @@ pub fn rect_from_corner_points( | |||
| 564 | // screen worth of selection we must add back that + 1. | 617 | // screen worth of selection we must add back that + 1. |
| 565 | Rectangle::from_extemities((x1, y1), (x2 + 1, y2 + 1)) | 618 | Rectangle::from_extemities((x1, y1), (x2 + 1, y2 + 1)) |
| 566 | } | 619 | } |
| 620 | + | ||
| 621 | +fn render_panel( | ||
| 622 | + renderer: &mut GlesRenderer, | ||
| 623 | + scale: f64, | ||
| 624 | + text: &str, | ||
| 625 | +) -> anyhow::Result<TextureBuffer<GlesTexture>> { | ||
| 626 | + let _span = tracy_client::span!("screenshot_ui::render_panel"); | ||
| 627 | + | ||
| 628 | + let padding: i32 = to_physical_precise_round(scale, PADDING); | ||
| 629 | + | ||
| 630 | + // Add 2 px of spacing to separate the backgrounds of the "Space" and "P" keys. | ||
| 631 | + let spacing = to_physical_precise_round::<i32>(scale, 2) * 1024; | ||
| 632 | + | ||
| 633 | + let mut font = FontDescription::from_string(FONT); | ||
| 634 | + font.set_absolute_size(to_physical_precise_round(scale, font.size())); | ||
| 635 | + | ||
| 636 | + let surface = ImageSurface::create(cairo::Format::ARgb32, 0, 0)?; | ||
| 637 | + let cr = cairo::Context::new(&surface)?; | ||
| 638 | + let layout = pangocairo::functions::create_layout(&cr); | ||
| 639 | + layout.context().set_round_glyph_positions(false); | ||
| 640 | + layout.set_font_description(Some(&font)); | ||
| 641 | + layout.set_alignment(Alignment::Center); | ||
| 642 | + layout.set_markup(text); | ||
| 643 | + layout.set_spacing(spacing); | ||
| 644 | + | ||
| 645 | + let (mut width, mut height) = layout.pixel_size(); | ||
| 646 | + width += padding * 2; | ||
| 647 | + height += padding * 2; | ||
| 648 | + | ||
| 649 | + let surface = ImageSurface::create(cairo::Format::ARgb32, width, height)?; | ||
| 650 | + let cr = cairo::Context::new(&surface)?; | ||
| 651 | + cr.set_source_rgb(0.1, 0.1, 0.1); | ||
| 652 | + cr.paint()?; | ||
| 653 | + | ||
| 654 | + cr.move_to(padding.into(), padding.into()); | ||
| 655 | + let layout = pangocairo::functions::create_layout(&cr); | ||
| 656 | + layout.context().set_round_glyph_positions(false); | ||
| 657 | + layout.set_font_description(Some(&font)); | ||
| 658 | + layout.set_alignment(Alignment::Center); | ||
| 659 | + layout.set_markup(text); | ||
| 660 | + layout.set_spacing(spacing); | ||
| 661 | + | ||
| 662 | + cr.set_source_rgb(1., 1., 1.); | ||
| 663 | + pangocairo::functions::show_layout(&cr, &layout); | ||
| 664 | + | ||
| 665 | + cr.move_to(0., 0.); | ||
| 666 | + cr.line_to(width.into(), 0.); | ||
| 667 | + cr.line_to(width.into(), height.into()); | ||
| 668 | + cr.line_to(0., height.into()); | ||
| 669 | + cr.line_to(0., 0.); | ||
| 670 | + cr.set_source_rgb(0.3, 0.3, 0.3); | ||
| 671 | + // Keep the border width even to avoid blurry edges. | ||
| 672 | + cr.set_line_width((f64::from(BORDER) / 2. * scale).round() * 2.); | ||
| 673 | + cr.stroke()?; | ||
| 674 | + drop(cr); | ||
| 675 | + | ||
| 676 | + let data = surface.take_data().unwrap(); | ||
| 677 | + let buffer = TextureBuffer::from_memory( | ||
| 678 | + renderer, | ||
| 679 | + &data, | ||
| 680 | + Fourcc::Argb8888, | ||
| 681 | + (width, height), | ||
| 682 | + false, | ||
| 683 | + scale, | ||
| 684 | + Transform::Normal, | ||
| 685 | + Vec::new(), | ||
| 686 | + )?; | ||
| 687 | + | ||
| 688 | + Ok(buffer) | ||
| 689 | +} | ||