From 48f0f6fb3ceb68fdb559ab38c8dcbb7b9ba3a13e Mon Sep 17 00:00:00 2001 From: Ivan Molodetskikh Date: Wed, 21 Feb 2024 21:27:44 +0400 Subject: Implement gradient borders --- src/layout/focus_ring.rs | 101 ++++++++++++++++++++++++++++++++++++----------- 1 file changed, 79 insertions(+), 22 deletions(-) (limited to 'src/layout/focus_ring.rs') diff --git a/src/layout/focus_ring.rs b/src/layout/focus_ring.rs index 0addd204..24013510 100644 --- a/src/layout/focus_ring.rs +++ b/src/layout/focus_ring.rs @@ -1,26 +1,41 @@ use std::iter::zip; use arrayvec::ArrayVec; -use niri_config; +use niri_config::{self, GradientRelativeTo}; use smithay::backend::renderer::element::solid::{SolidColorBuffer, SolidColorRenderElement}; use smithay::backend::renderer::element::Kind; -use smithay::utils::{Logical, Point, Scale, Size}; +use smithay::utils::{Logical, Point, Rectangle, Scale, Size}; + +use crate::niri_render_elements; +use crate::render_helpers::gradient::GradientRenderElement; +use crate::render_helpers::renderer::NiriRenderer; #[derive(Debug)] pub struct FocusRing { buffers: [SolidColorBuffer; 4], locations: [Point; 4], + sizes: [Size; 4], + full_size: Size, + is_active: bool, is_border: bool, config: niri_config::FocusRing, } -pub type FocusRingRenderElement = SolidColorRenderElement; +niri_render_elements! { + FocusRingRenderElement => { + SolidColor = SolidColorRenderElement, + Gradient = GradientRenderElement, + } +} impl FocusRing { pub fn new(config: niri_config::FocusRing) -> Self { Self { buffers: Default::default(), locations: Default::default(), + sizes: Default::default(), + full_size: Default::default(), + is_active: false, is_border: false, config, } @@ -32,20 +47,25 @@ impl FocusRing { pub fn update(&mut self, win_size: Size, is_border: bool) { let width = i32::from(self.config.width); + self.full_size = win_size + Size::from((width * 2, width * 2)); if is_border { - self.buffers[0].resize((win_size.w + width * 2, width)); - self.buffers[1].resize((win_size.w + width * 2, width)); - self.buffers[2].resize((width, win_size.h)); - self.buffers[3].resize((width, win_size.h)); + self.sizes[0] = Size::from((win_size.w + width * 2, width)); + self.sizes[1] = Size::from((win_size.w + width * 2, width)); + self.sizes[2] = Size::from((width, win_size.h)); + self.sizes[3] = Size::from((width, win_size.h)); + + for (buf, size) in zip(&mut self.buffers, self.sizes) { + buf.resize(size); + } self.locations[0] = Point::from((-width, -width)); self.locations[1] = Point::from((-width, win_size.h)); self.locations[2] = Point::from((-width, 0)); self.locations[3] = Point::from((win_size.w, 0)); } else { - let size = win_size + Size::from((width * 2, width * 2)); - self.buffers[0].resize(size); + self.sizes[0] = self.full_size; + self.buffers[0].resize(self.sizes[0]); self.locations[0] = Point::from((-width, -width)); } @@ -62,12 +82,16 @@ impl FocusRing { for buf in &mut self.buffers { buf.set_color(color); } + + self.is_active = is_active; } - pub fn render( + pub fn render( &self, + renderer: &mut R, location: Point, scale: Scale, + view_size: Size, ) -> impl Iterator { let mut rv = ArrayVec::<_, 4>::new(); @@ -75,23 +99,56 @@ impl FocusRing { return rv.into_iter(); } - let mut push = |buffer, location: Point| { - let elem = SolidColorRenderElement::from_buffer( - buffer, - location.to_physical_precise_round(scale), - scale, - 1., - Kind::Unspecified, - ); - rv.push(elem.into()); + let gradient = if self.is_active { + self.config.active_gradient + } else { + self.config.inactive_gradient + }; + + let full_rect = Rectangle::from_loc_and_size(location + self.locations[0], self.full_size); + let view_rect = Rectangle::from_loc_and_size((0, 0), view_size); + + let mut push = |buffer, location: Point, size: Size| { + let elem = gradient.and_then(|gradient| { + let gradient_area = match gradient.relative_to { + GradientRelativeTo::Window => full_rect, + GradientRelativeTo::WorkspaceView => view_rect, + }; + GradientRenderElement::new( + renderer, + scale, + Rectangle::from_loc_and_size(location, size), + gradient_area, + gradient.from.into(), + gradient.to.into(), + ((gradient.angle as f32) - 90.).to_radians(), + ) + .map(Into::into) + }); + + let elem = elem.unwrap_or_else(|| { + SolidColorRenderElement::from_buffer( + buffer, + location.to_physical_precise_round(scale), + scale, + 1., + Kind::Unspecified, + ) + .into() + }); + rv.push(elem); }; if self.is_border { - for (buf, loc) in zip(&self.buffers, self.locations) { - push(buf, location + loc); + for (buf, (loc, size)) in zip(&self.buffers, zip(self.locations, self.sizes)) { + push(buf, location + loc, size); } } else { - push(&self.buffers[0], location + self.locations[0]); + push( + &self.buffers[0], + location + self.locations[0], + self.sizes[0], + ); } rv.into_iter() -- cgit