screenshot-ui: Animate opening
Browse files at this commit451366308446aeb8ae4ee1cd55559381e93b9b39Parent: 092cf6c5 changed files +111 -22
- niri-config/src/lib.rs +33 -0
- src/input/mod.rs +1 -1
- src/niri.rs +5 -1
- src/ui/screenshot_ui.rs +52 -20
- wiki/Configuration:-Animations.md +20 -0
niri-config/src/lib.rsmodified+33 -0
| @@ -571,6 +571,8 @@ pub struct Animations { | |||
| 571 | pub window_resize: WindowResizeAnim, | 571 | pub window_resize: WindowResizeAnim, |
| 572 | #[knuffel(child, default)] | 572 | #[knuffel(child, default)] |
| 573 | pub config_notification_open_close: ConfigNotificationOpenCloseAnim, | 573 | pub config_notification_open_close: ConfigNotificationOpenCloseAnim, |
| 574 | + #[knuffel(child, default)] | ||
| 575 | + pub screenshot_ui_open: ScreenshotUiOpenAnim, | ||
| 574 | } | 576 | } |
| 575 | 577 | ||
| 576 | impl Default for Animations { | 578 | impl Default for Animations { |
| @@ -585,6 +587,7 @@ impl Default for Animations { | |||
| 585 | window_close: Default::default(), | 587 | window_close: Default::default(), |
| 586 | window_resize: Default::default(), | 588 | window_resize: Default::default(), |
| 587 | config_notification_open_close: Default::default(), | 589 | config_notification_open_close: Default::default(), |
| 590 | + screenshot_ui_open: Default::default(), | ||
| 588 | } | 591 | } |
| 589 | } | 592 | } |
| 590 | } | 593 | } |
| @@ -717,6 +720,21 @@ impl Default for ConfigNotificationOpenCloseAnim { | |||
| 717 | } | 720 | } |
| 718 | } | 721 | } |
| 719 | 722 | ||
| 723 | +#[derive(Debug, Clone, Copy, PartialEq)] | ||
| 724 | +pub struct ScreenshotUiOpenAnim(pub Animation); | ||
| 725 | + | ||
| 726 | +impl Default for ScreenshotUiOpenAnim { | ||
| 727 | + fn default() -> Self { | ||
| 728 | + Self(Animation { | ||
| 729 | + off: false, | ||
| 730 | + kind: AnimationKind::Easing(EasingParams { | ||
| 731 | + duration_ms: 200, | ||
| 732 | + curve: AnimationCurve::EaseOutQuad, | ||
| 733 | + }), | ||
| 734 | + }) | ||
| 735 | + } | ||
| 736 | +} | ||
| 737 | + | ||
| 720 | #[derive(Debug, Clone, Copy, PartialEq)] | 738 | #[derive(Debug, Clone, Copy, PartialEq)] |
| 721 | pub struct Animation { | 739 | pub struct Animation { |
| 722 | pub off: bool, | 740 | pub off: bool, |
| @@ -1802,6 +1820,21 @@ where | |||
| 1802 | } | 1820 | } |
| 1803 | } | 1821 | } |
| 1804 | 1822 | ||
| 1823 | +impl<S> knuffel::Decode<S> for ScreenshotUiOpenAnim | ||
| 1824 | +where | ||
| 1825 | + S: knuffel::traits::ErrorSpan, | ||
| 1826 | +{ | ||
| 1827 | + fn decode_node( | ||
| 1828 | + node: &knuffel::ast::SpannedNode<S>, | ||
| 1829 | + ctx: &mut knuffel::decode::Context<S>, | ||
| 1830 | + ) -> Result<Self, DecodeError<S>> { | ||
| 1831 | + let default = Self::default().0; | ||
| 1832 | + Ok(Self(Animation::decode_node(node, ctx, default, |_, _| { | ||
| 1833 | + Ok(false) | ||
| 1834 | + })?)) | ||
| 1835 | + } | ||
| 1836 | +} | ||
| 1837 | + | ||
| 1805 | impl Animation { | 1838 | impl Animation { |
| 1806 | pub fn new_off() -> Self { | 1839 | pub fn new_off() -> Self { |
| 1807 | Self { | 1840 | Self { |
src/input/mod.rsmodified+1 -1
| @@ -2569,7 +2569,7 @@ mod tests { | |||
| 2569 | let comp_mod = CompositorMod::Super; | 2569 | let comp_mod = CompositorMod::Super; |
| 2570 | let mut suppressed_keys = HashSet::new(); | 2570 | let mut suppressed_keys = HashSet::new(); |
| 2571 | 2571 | ||
| 2572 | - let screenshot_ui = ScreenshotUi::new(); | 2572 | + let screenshot_ui = ScreenshotUi::new(Default::default()); |
| 2573 | let disable_power_key_handling = false; | 2573 | let disable_power_key_handling = false; |
| 2574 | 2574 | ||
| 2575 | // The key_code we pick is arbitrary, the only thing | 2575 | // The key_code we pick is arbitrary, the only thing |
src/niri.rsmodified+5 -1
| @@ -1589,7 +1589,7 @@ impl Niri { | |||
| 1589 | let mods_with_finger_scroll_binds = | 1589 | let mods_with_finger_scroll_binds = |
| 1590 | mods_with_finger_scroll_binds(backend.mod_key(), &config_.binds); | 1590 | mods_with_finger_scroll_binds(backend.mod_key(), &config_.binds); |
| 1591 | 1591 | ||
| 1592 | - let screenshot_ui = ScreenshotUi::new(); | 1592 | + let screenshot_ui = ScreenshotUi::new(config.clone()); |
| 1593 | let config_error_notification = ConfigErrorNotification::new(config.clone()); | 1593 | let config_error_notification = ConfigErrorNotification::new(config.clone()); |
| 1594 | 1594 | ||
| 1595 | let mut hotkey_overlay = HotkeyOverlay::new(config.clone(), backend.mod_key()); | 1595 | let mut hotkey_overlay = HotkeyOverlay::new(config.clone(), backend.mod_key()); |
| @@ -2973,6 +2973,10 @@ impl Niri { | |||
| 2973 | state.unfinished_animations_remain |= | 2973 | state.unfinished_animations_remain |= |
| 2974 | self.config_error_notification.are_animations_ongoing(); | 2974 | self.config_error_notification.are_animations_ongoing(); |
| 2975 | 2975 | ||
| 2976 | + self.screenshot_ui | ||
| 2977 | + .advance_animations(target_presentation_time); | ||
| 2978 | + state.unfinished_animations_remain |= self.screenshot_ui.are_animations_ongoing(); | ||
| 2979 | + | ||
| 2976 | // Also keep redrawing if the current cursor is animated. | 2980 | // Also keep redrawing if the current cursor is animated. |
| 2977 | state.unfinished_animations_remain |= self | 2981 | state.unfinished_animations_remain |= self |
| 2978 | .cursor_manager | 2982 | .cursor_manager |
src/ui/screenshot_ui.rsmodified+52 -20
| @@ -1,11 +1,13 @@ | |||
| 1 | +use std::cell::RefCell; | ||
| 1 | use std::cmp::{max, min}; | 2 | use std::cmp::{max, min}; |
| 2 | use std::collections::HashMap; | 3 | use std::collections::HashMap; |
| 3 | use std::iter::zip; | 4 | use std::iter::zip; |
| 4 | -use std::mem; | 5 | +use std::rc::Rc; |
| 6 | +use std::time::Duration; | ||
| 5 | 7 | ||
| 6 | use anyhow::Context; | 8 | use anyhow::Context; |
| 7 | use arrayvec::ArrayVec; | 9 | use arrayvec::ArrayVec; |
| 8 | -use niri_config::Action; | 10 | +use niri_config::{Action, Config}; |
| 9 | use pango::{Alignment, FontDescription}; | 11 | use pango::{Alignment, FontDescription}; |
| 10 | use pangocairo::cairo::{self, ImageSurface}; | 12 | use pangocairo::cairo::{self, ImageSurface}; |
| 11 | use smithay::backend::allocator::Fourcc; | 13 | use smithay::backend::allocator::Fourcc; |
| @@ -18,6 +20,7 @@ use smithay::input::keyboard::{Keysym, ModifiersState}; | |||
| 18 | use smithay::output::{Output, WeakOutput}; | 20 | use smithay::output::{Output, WeakOutput}; |
| 19 | use smithay::utils::{Physical, Point, Rectangle, Scale, Size, Transform}; | 21 | use smithay::utils::{Physical, Point, Rectangle, Scale, Size, Transform}; |
| 20 | 22 | ||
| 23 | +use crate::animation::Animation; | ||
| 21 | use crate::niri_render_elements; | 24 | use crate::niri_render_elements; |
| 22 | use crate::render_helpers::primary_gpu_texture::PrimaryGpuTextureRenderElement; | 25 | use crate::render_helpers::primary_gpu_texture::PrimaryGpuTextureRenderElement; |
| 23 | use crate::render_helpers::solid_color::{SolidColorBuffer, SolidColorRenderElement}; | 26 | use crate::render_helpers::solid_color::{SolidColorBuffer, SolidColorRenderElement}; |
| @@ -42,15 +45,19 @@ const TEXT_SHOW_P: &str = | |||
| 42 | // allows only single-output selections for now. | 45 | // allows only single-output selections for now. |
| 43 | // | 46 | // |
| 44 | // As a consequence of this, selection coordinates are in output-local coordinate space. | 47 | // As a consequence of this, selection coordinates are in output-local coordinate space. |
| 48 | +#[allow(clippy::large_enum_variant)] | ||
| 45 | pub enum ScreenshotUi { | 49 | pub enum ScreenshotUi { |
| 46 | Closed { | 50 | Closed { |
| 47 | last_selection: Option<(WeakOutput, Rectangle<i32, Physical>)>, | 51 | last_selection: Option<(WeakOutput, Rectangle<i32, Physical>)>, |
| 52 | + config: Rc<RefCell<Config>>, | ||
| 48 | }, | 53 | }, |
| 49 | Open { | 54 | Open { |
| 50 | selection: (Output, Point<i32, Physical>, Point<i32, Physical>), | 55 | selection: (Output, Point<i32, Physical>, Point<i32, Physical>), |
| 51 | output_data: HashMap<Output, OutputData>, | 56 | output_data: HashMap<Output, OutputData>, |
| 52 | mouse_down: bool, | 57 | mouse_down: bool, |
| 53 | show_pointer: bool, | 58 | show_pointer: bool, |
| 59 | + open_anim: Animation, | ||
| 60 | + config: Rc<RefCell<Config>>, | ||
| 54 | }, | 61 | }, |
| 55 | } | 62 | } |
| 56 | 63 | ||
| @@ -79,9 +86,10 @@ niri_render_elements! { | |||
| 79 | } | 86 | } |
| 80 | 87 | ||
| 81 | impl ScreenshotUi { | 88 | impl ScreenshotUi { |
| 82 | - pub fn new() -> Self { | 89 | + pub fn new(config: Rc<RefCell<Config>>) -> Self { |
| 83 | Self::Closed { | 90 | Self::Closed { |
| 84 | last_selection: None, | 91 | last_selection: None, |
| 92 | + config, | ||
| 85 | } | 93 | } |
| 86 | } | 94 | } |
| 87 | 95 | ||
| @@ -96,7 +104,11 @@ impl ScreenshotUi { | |||
| 96 | return false; | 104 | return false; |
| 97 | } | 105 | } |
| 98 | 106 | ||
| 99 | - let Self::Closed { last_selection } = self else { | 107 | + let Self::Closed { |
| 108 | + last_selection, | ||
| 109 | + config, | ||
| 110 | + } = self | ||
| 111 | + else { | ||
| 100 | return false; | 112 | return false; |
| 101 | }; | 113 | }; |
| 102 | 114 | ||
| @@ -167,11 +179,18 @@ impl ScreenshotUi { | |||
| 167 | }) | 179 | }) |
| 168 | .collect(); | 180 | .collect(); |
| 169 | 181 | ||
| 182 | + let open_anim = { | ||
| 183 | + let c = config.borrow(); | ||
| 184 | + Animation::new(0., 1., 0., c.animations.screenshot_ui_open.0) | ||
| 185 | + }; | ||
| 186 | + | ||
| 170 | *self = Self::Open { | 187 | *self = Self::Open { |
| 171 | selection, | 188 | selection, |
| 172 | output_data, | 189 | output_data, |
| 173 | mouse_down: false, | 190 | mouse_down: false, |
| 174 | show_pointer: true, | 191 | show_pointer: true, |
| 192 | + open_anim, | ||
| 193 | + config: config.clone(), | ||
| 175 | }; | 194 | }; |
| 176 | 195 | ||
| 177 | self.update_buffers(); | 196 | self.update_buffers(); |
| @@ -180,13 +199,11 @@ impl ScreenshotUi { | |||
| 180 | } | 199 | } |
| 181 | 200 | ||
| 182 | pub fn close(&mut self) -> bool { | 201 | pub fn close(&mut self) -> bool { |
| 183 | - let selection = match mem::take(self) { | 202 | + let Self::Open { |
| 184 | - Self::Open { selection, .. } => selection, | 203 | + selection, config, .. |
| 185 | - closed @ Self::Closed { .. } => { | 204 | + } = self |
| 186 | - // Put it back. | 205 | + else { |
| 187 | - *self = closed; | 206 | + return false; |
| 188 | - return false; | ||
| 189 | - } | ||
| 190 | }; | 207 | }; |
| 191 | 208 | ||
| 192 | let last_selection = Some(( | 209 | let last_selection = Some(( |
| @@ -194,7 +211,10 @@ impl ScreenshotUi { | |||
| 194 | rect_from_corner_points(selection.1, selection.2), | 211 | rect_from_corner_points(selection.1, selection.2), |
| 195 | )); | 212 | )); |
| 196 | 213 | ||
| 197 | - *self = Self::Closed { last_selection }; | 214 | + *self = Self::Closed { |
| 215 | + last_selection, | ||
| 216 | + config: config.clone(), | ||
| 217 | + }; | ||
| 198 | 218 | ||
| 199 | true | 219 | true |
| 200 | } | 220 | } |
| @@ -209,6 +229,22 @@ impl ScreenshotUi { | |||
| 209 | matches!(self, ScreenshotUi::Open { .. }) | 229 | matches!(self, ScreenshotUi::Open { .. }) |
| 210 | } | 230 | } |
| 211 | 231 | ||
| 232 | + pub fn advance_animations(&mut self, current_time: Duration) { | ||
| 233 | + let Self::Open { open_anim, .. } = self else { | ||
| 234 | + return; | ||
| 235 | + }; | ||
| 236 | + | ||
| 237 | + open_anim.set_current_time(current_time); | ||
| 238 | + } | ||
| 239 | + | ||
| 240 | + pub fn are_animations_ongoing(&self) -> bool { | ||
| 241 | + let Self::Open { open_anim, .. } = self else { | ||
| 242 | + return false; | ||
| 243 | + }; | ||
| 244 | + | ||
| 245 | + !open_anim.is_done() | ||
| 246 | + } | ||
| 247 | + | ||
| 212 | fn update_buffers(&mut self) { | 248 | fn update_buffers(&mut self) { |
| 213 | let Self::Open { | 249 | let Self::Open { |
| 214 | selection, | 250 | selection, |
| @@ -293,6 +329,7 @@ impl ScreenshotUi { | |||
| 293 | output_data, | 329 | output_data, |
| 294 | show_pointer, | 330 | show_pointer, |
| 295 | mouse_down, | 331 | mouse_down, |
| 332 | + open_anim, | ||
| 296 | .. | 333 | .. |
| 297 | } = self | 334 | } = self |
| 298 | else { | 335 | else { |
| @@ -306,6 +343,7 @@ impl ScreenshotUi { | |||
| 306 | }; | 343 | }; |
| 307 | 344 | ||
| 308 | let scale = output_data.scale; | 345 | let scale = output_data.scale; |
| 346 | + let progress = open_anim.clamped_value().clamp(0., 1.) as f32; | ||
| 309 | 347 | ||
| 310 | // The help panel goes on top. | 348 | // The help panel goes on top. |
| 311 | if let Some((show, hide)) = &output_data.panel { | 349 | if let Some((show, hide)) = &output_data.panel { |
| @@ -324,7 +362,7 @@ impl ScreenshotUi { | |||
| 324 | let elem = PrimaryGpuTextureRenderElement(TextureRenderElement::from_texture_buffer( | 362 | let elem = PrimaryGpuTextureRenderElement(TextureRenderElement::from_texture_buffer( |
| 325 | buffer.clone(), | 363 | buffer.clone(), |
| 326 | location, | 364 | location, |
| 327 | - alpha, | 365 | + alpha * progress, |
| 328 | None, | 366 | None, |
| 329 | None, | 367 | None, |
| 330 | Kind::Unspecified, | 368 | Kind::Unspecified, |
| @@ -337,7 +375,7 @@ impl ScreenshotUi { | |||
| 337 | SolidColorRenderElement::from_buffer( | 375 | SolidColorRenderElement::from_buffer( |
| 338 | buffer, | 376 | buffer, |
| 339 | loc.to_f64().to_logical(scale), | 377 | loc.to_f64().to_logical(scale), |
| 340 | - 1., | 378 | + progress, |
| 341 | Kind::Unspecified, | 379 | Kind::Unspecified, |
| 342 | ) | 380 | ) |
| 343 | .into() | 381 | .into() |
| @@ -530,12 +568,6 @@ impl ScreenshotUi { | |||
| 530 | } | 568 | } |
| 531 | } | 569 | } |
| 532 | 570 | ||
| 533 | -impl Default for ScreenshotUi { | ||
| 534 | - fn default() -> Self { | ||
| 535 | - Self::new() | ||
| 536 | - } | ||
| 537 | -} | ||
| 538 | - | ||
| 539 | impl OutputScreenshot { | 571 | impl OutputScreenshot { |
| 540 | pub fn from_textures( | 572 | pub fn from_textures( |
| 541 | renderer: &mut GlesRenderer, | 573 | renderer: &mut GlesRenderer, |
wiki/Configuration:-Animations.mdmodified+20 -0
| @@ -45,6 +45,11 @@ animations { | |||
| 45 | config-notification-open-close { | 45 | config-notification-open-close { |
| 46 | spring damping-ratio=0.6 stiffness=1000 epsilon=0.001 | 46 | spring damping-ratio=0.6 stiffness=1000 epsilon=0.001 |
| 47 | } | 47 | } |
| 48 | + | ||
| 49 | + screenshot-ui-open { | ||
| 50 | + duration-ms 200 | ||
| 51 | + curve "ease-out-quad" | ||
| 52 | + } | ||
| 48 | } | 53 | } |
| 49 | ``` | 54 | ``` |
| 50 | 55 | ||
| @@ -354,6 +359,21 @@ animations { | |||
| 354 | } | 359 | } |
| 355 | ``` | 360 | ``` |
| 356 | 361 | ||
| 362 | +#### `screenshot-ui-open` | ||
| 363 | + | ||
| 364 | +<sup>Since: 0.1.8</sup> | ||
| 365 | + | ||
| 366 | +The open (fade-in) animation of the screenshot UI. | ||
| 367 | + | ||
| 368 | +``` | ||
| 369 | +animations { | ||
| 370 | + screenshot-ui-open { | ||
| 371 | + duration-ms 200 | ||
| 372 | + curve "ease-out-quad" | ||
| 373 | + } | ||
| 374 | +} | ||
| 375 | +``` | ||
| 376 | + | ||
| 357 | ### Synchronized Animations | 377 | ### Synchronized Animations |
| 358 | 378 | ||
| 359 | <sup>Since: 0.1.5</sup> | 379 | <sup>Since: 0.1.5</sup> |