| 1 | use std::f32::consts::{FRAC_PI_2, PI}; |
| 2 | use std::sync::atomic::Ordering; |
| 3 | use std::time::Duration; |
| 4 | |
| 5 | use niri::animation::ANIMATION_SLOWDOWN; |
| 6 | use niri::render_helpers::border::BorderRenderElement; |
| 7 | use niri_config::{Color, CornerRadius, GradientInterpolation}; |
| 8 | use smithay::backend::renderer::element::RenderElement; |
| 9 | use smithay::backend::renderer::gles::GlesRenderer; |
| 10 | use smithay::utils::{Logical, Physical, Rectangle, Size}; |
| 11 | |
| 12 | use super::TestCase; |
| 13 | |
| 14 | pub struct GradientAngle { |
| 15 | angle: f32, |
| 16 | prev_time: Duration, |
| 17 | } |
| 18 | |
| 19 | impl GradientAngle { |
| 20 | pub fn new(_size: Size<i32, Logical>) -> Self { |
| 21 | Self { |
| 22 | angle: 0., |
| 23 | prev_time: Duration::ZERO, |
| 24 | } |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | impl TestCase for GradientAngle { |
| 29 | fn are_animations_ongoing(&self) -> bool { |
| 30 | true |
| 31 | } |
| 32 | |
| 33 | fn advance_animations(&mut self, current_time: Duration) { |
| 34 | let mut delta = if self.prev_time.is_zero() { |
| 35 | Duration::ZERO |
| 36 | } else { |
| 37 | current_time.saturating_sub(self.prev_time) |
| 38 | }; |
| 39 | self.prev_time = current_time; |
| 40 | |
| 41 | let slowdown = ANIMATION_SLOWDOWN.load(Ordering::SeqCst); |
| 42 | if slowdown == 0. { |
| 43 | delta = Duration::ZERO |
| 44 | } else { |
| 45 | delta = delta.div_f64(slowdown); |
| 46 | } |
| 47 | |
| 48 | self.angle += delta.as_secs_f32() * PI; |
| 49 | |
| 50 | if self.angle >= PI * 2. { |
| 51 | self.angle -= PI * 2. |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | fn render( |
| 56 | &mut self, |
| 57 | _renderer: &mut GlesRenderer, |
| 58 | size: Size<i32, Physical>, |
| 59 | ) -> Vec<Box<dyn RenderElement<GlesRenderer>>> { |
| 60 | let (a, b) = (size.w / 4, size.h / 4); |
| 61 | let size = (size.w - a * 2, size.h - b * 2); |
| 62 | let area = Rectangle::from_loc_and_size((a, b), size).to_f64(); |
| 63 | |
| 64 | [BorderRenderElement::new( |
| 65 | area.size, |
| 66 | Rectangle::from_loc_and_size((0., 0.), area.size), |
| 67 | GradientInterpolation::default(), |
| 68 | Color::new_unpremul(1., 0., 0., 1.), |
| 69 | Color::new_unpremul(0., 1., 0., 1.), |
| 70 | self.angle - FRAC_PI_2, |
| 71 | Rectangle::from_loc_and_size((0., 0.), area.size), |
| 72 | 0., |
| 73 | CornerRadius::default(), |
| 74 | 1., |
| 75 | ) |
| 76 | .with_location(area.loc)] |
| 77 | .into_iter() |
| 78 | .map(|elem| Box::new(elem) as _) |
| 79 | .collect() |
| 80 | } |
| 81 | } |