| 1 | use std::f32::consts::{FRAC_PI_4, PI}; |
| 2 | use std::time::Duration; |
| 3 | |
| 4 | use niri::layout::focus_ring::FocusRing; |
| 5 | use niri::render_helpers::border::BorderRenderElement; |
| 6 | use niri_config::{Color, CornerRadius, GradientInterpolation}; |
| 7 | use smithay::backend::renderer::element::RenderElement; |
| 8 | use smithay::backend::renderer::gles::GlesRenderer; |
| 9 | use smithay::utils::{Physical, Point, Rectangle, Size}; |
| 10 | |
| 11 | use super::{Args, TestCase}; |
| 12 | |
| 13 | pub struct GradientArea { |
| 14 | progress: f32, |
| 15 | border: FocusRing, |
| 16 | prev_time: Duration, |
| 17 | } |
| 18 | |
| 19 | impl GradientArea { |
| 20 | pub fn new(_args: Args) -> Self { |
| 21 | let border = FocusRing::new(niri_config::FocusRing { |
| 22 | off: false, |
| 23 | width: 1., |
| 24 | active_color: Color::from_rgba8_unpremul(255, 255, 255, 128), |
| 25 | inactive_color: Color::default(), |
| 26 | urgent_color: Color::default(), |
| 27 | active_gradient: None, |
| 28 | inactive_gradient: None, |
| 29 | urgent_gradient: None, |
| 30 | }); |
| 31 | |
| 32 | Self { |
| 33 | progress: 0., |
| 34 | border, |
| 35 | prev_time: Duration::ZERO, |
| 36 | } |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | impl TestCase for GradientArea { |
| 41 | fn are_animations_ongoing(&self) -> bool { |
| 42 | true |
| 43 | } |
| 44 | |
| 45 | fn advance_animations(&mut self, current_time: Duration) { |
| 46 | let delta = if self.prev_time.is_zero() { |
| 47 | Duration::ZERO |
| 48 | } else { |
| 49 | current_time.saturating_sub(self.prev_time) |
| 50 | }; |
| 51 | self.prev_time = current_time; |
| 52 | |
| 53 | self.progress += delta.as_secs_f32() * PI; |
| 54 | |
| 55 | if self.progress >= PI * 2. { |
| 56 | self.progress -= PI * 2. |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | fn render( |
| 61 | &mut self, |
| 62 | renderer: &mut GlesRenderer, |
| 63 | size: Size<i32, Physical>, |
| 64 | ) -> Vec<Box<dyn RenderElement<GlesRenderer>>> { |
| 65 | let mut rv = Vec::new(); |
| 66 | |
| 67 | let f = (self.progress.sin() + 1.) / 2.; |
| 68 | |
| 69 | let (a, b) = (size.w / 4, size.h / 4); |
| 70 | let rect_size = Size::from((size.w - a * 2, size.h - b * 2)); |
| 71 | let area = Rectangle::new(Point::from((a, b)), rect_size).to_f64(); |
| 72 | |
| 73 | let g_size = Size::from(( |
| 74 | (size.w as f32 / 8. + size.w as f32 / 8. * 7. * f).round() as i32, |
| 75 | (size.h as f32 / 8. + size.h as f32 / 8. * 7. * f).round() as i32, |
| 76 | )); |
| 77 | let g_loc = Point::from(((size.w - g_size.w) / 2, (size.h - g_size.h) / 2)).to_f64(); |
| 78 | let g_size = g_size.to_f64(); |
| 79 | let mut g_area = Rectangle::new(g_loc, g_size); |
| 80 | g_area.loc -= area.loc; |
| 81 | |
| 82 | self.border.update_render_elements( |
| 83 | g_size, |
| 84 | true, |
| 85 | true, |
| 86 | false, |
| 87 | Rectangle::default(), |
| 88 | CornerRadius::default(), |
| 89 | 1., |
| 90 | 1., |
| 91 | ); |
| 92 | self.border |
| 93 | .render(renderer, g_loc, &mut |elem| rv.push(Box::new(elem) as _)); |
| 94 | |
| 95 | rv.extend( |
| 96 | [BorderRenderElement::new( |
| 97 | area.size, |
| 98 | g_area, |
| 99 | GradientInterpolation::default(), |
| 100 | Color::new_unpremul(1., 0., 0., 1.), |
| 101 | Color::new_unpremul(0., 1., 0., 1.), |
| 102 | FRAC_PI_4, |
| 103 | Rectangle::from_size(rect_size).to_f64(), |
| 104 | 0., |
| 105 | CornerRadius::default(), |
| 106 | 1., |
| 107 | 1., |
| 108 | ) |
| 109 | .with_location(area.loc)] |
| 110 | .into_iter() |
| 111 | .map(|elem| Box::new(elem) as _), |
| 112 | ); |
| 113 | |
| 114 | rv |
| 115 | } |
| 116 | } |