niri
Clone
https://git.tahinli.com/Tahinli/niri.git
0 watching·0 forks·Rust·GPL-3.0·12.1 MiB·64a9351·Ivan Molodetskikh·2 years ago·1319 commits
| 1 | use std::cell::RefCell; |
| 2 | use std::cmp::{max, min}; |
| 3 | use std::rc::Rc; |
| 4 | |
| 5 | use niri::layout::{ |
| 6 | ConfigureIntent, InteractiveResizeData, LayoutElement, LayoutElementRenderElement, |
| 7 | LayoutElementRenderSnapshot, |
| 8 | }; |
| 9 | use niri::render_helpers::renderer::NiriRenderer; |
| 10 | use niri::render_helpers::solid_color::{SolidColorBuffer, SolidColorRenderElement}; |
| 11 | use niri::render_helpers::{RenderTarget, SplitElements}; |
| 12 | use niri::utils::transaction::Transaction; |
| 13 | use niri::window::ResolvedWindowRules; |
| 14 | use smithay::backend::renderer::element::{Id, Kind}; |
| 15 | use smithay::output::{self, Output}; |
| 16 | use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface; |
| 17 | use smithay::utils::{Logical, Point, Scale, Serial, Size, Transform}; |
| 18 | |
| 19 | #[derive(Debug)] |
| 20 | struct TestWindowInner { |
| 21 | size: Size<i32, Logical>, |
| 22 | requested_size: Option<Size<i32, Logical>>, |
| 23 | min_size: Size<i32, Logical>, |
| 24 | max_size: Size<i32, Logical>, |
| 25 | buffer: SolidColorBuffer, |
| 26 | pending_fullscreen: bool, |
| 27 | csd_shadow_width: i32, |
| 28 | csd_shadow_buffer: SolidColorBuffer, |
| 29 | } |
| 30 | |
| 31 | #[derive(Debug, Clone)] |
| 32 | pub struct TestWindow { |
| 33 | id: usize, |
| 34 | inner: Rc<RefCell<TestWindowInner>>, |
| 35 | } |
| 36 | |
| 37 | impl TestWindow { |
| 38 | pub fn freeform(id: usize) -> Self { |
| 39 | let size = Size::from((100, 200)); |
| 40 | let min_size = Size::from((0, 0)); |
| 41 | let max_size = Size::from((0, 0)); |
| 42 | let buffer = SolidColorBuffer::new(size.to_f64(), [0.15, 0.64, 0.41, 1.]); |
| 43 | |
| 44 | Self { |
| 45 | id, |
| 46 | inner: Rc::new(RefCell::new(TestWindowInner { |
| 47 | size, |
| 48 | requested_size: None, |
| 49 | min_size, |
| 50 | max_size, |
| 51 | buffer, |
| 52 | pending_fullscreen: false, |
| 53 | csd_shadow_width: 0, |
| 54 | csd_shadow_buffer: SolidColorBuffer::new((0., 0.), [0., 0., 0., 0.3]), |
| 55 | })), |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | pub fn fixed_size(id: usize) -> Self { |
| 60 | let rv = Self::freeform(id); |
| 61 | rv.set_min_size((200, 400).into()); |
| 62 | rv.set_max_size((200, 400).into()); |
| 63 | rv.set_color([0.88, 0.11, 0.14, 1.]); |
| 64 | rv.communicate(); |
| 65 | rv |
| 66 | } |
| 67 | |
| 68 | pub fn set_min_size(&self, size: Size<i32, Logical>) { |
| 69 | self.inner.borrow_mut().min_size = size; |
| 70 | } |
| 71 | |
| 72 | pub fn set_max_size(&self, size: Size<i32, Logical>) { |
| 73 | self.inner.borrow_mut().max_size = size; |
| 74 | } |
| 75 | |
| 76 | pub fn set_color(&self, color: [f32; 4]) { |
| 77 | self.inner.borrow_mut().buffer.set_color(color); |
| 78 | } |
| 79 | |
| 80 | pub fn set_csd_shadow_width(&self, width: i32) { |
| 81 | self.inner.borrow_mut().csd_shadow_width = width; |
| 82 | } |
| 83 | |
| 84 | pub fn communicate(&self) -> bool { |
| 85 | let mut rv = false; |
| 86 | let mut inner = self.inner.borrow_mut(); |
| 87 | |
| 88 | let mut new_size = inner.size; |
| 89 | |
| 90 | if let Some(size) = inner.requested_size.take() { |
| 91 | assert!(size.w >= 0); |
| 92 | assert!(size.h >= 0); |
| 93 | |
| 94 | if size.w != 0 { |
| 95 | new_size.w = size.w; |
| 96 | } |
| 97 | if size.h != 0 { |
| 98 | new_size.h = size.h; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | if inner.max_size.w > 0 { |
| 103 | new_size.w = min(new_size.w, inner.max_size.w); |
| 104 | } |
| 105 | if inner.max_size.h > 0 { |
| 106 | new_size.h = min(new_size.h, inner.max_size.h); |
| 107 | } |
| 108 | if inner.min_size.w > 0 { |
| 109 | new_size.w = max(new_size.w, inner.min_size.w); |
| 110 | } |
| 111 | if inner.min_size.h > 0 { |
| 112 | new_size.h = max(new_size.h, inner.min_size.h); |
| 113 | } |
| 114 | |
| 115 | if inner.size != new_size { |
| 116 | inner.size = new_size; |
| 117 | inner.buffer.resize(new_size.to_f64()); |
| 118 | rv = true; |
| 119 | } |
| 120 | |
| 121 | let mut csd_shadow_size = new_size; |
| 122 | csd_shadow_size.w += inner.csd_shadow_width * 2; |
| 123 | csd_shadow_size.h += inner.csd_shadow_width * 2; |
| 124 | inner.csd_shadow_buffer.resize(csd_shadow_size.to_f64()); |
| 125 | |
| 126 | rv |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | impl LayoutElement for TestWindow { |
| 131 | type Id = usize; |
| 132 | |
| 133 | fn id(&self) -> &Self::Id { |
| 134 | &self.id |
| 135 | } |
| 136 | |
| 137 | fn size(&self) -> Size<i32, Logical> { |
| 138 | self.inner.borrow().size |
| 139 | } |
| 140 | |
| 141 | fn buf_loc(&self) -> Point<i32, Logical> { |
| 142 | (0, 0).into() |
| 143 | } |
| 144 | |
| 145 | fn is_in_input_region(&self, _point: Point<f64, Logical>) -> bool { |
| 146 | false |
| 147 | } |
| 148 | |
| 149 | fn render<R: NiriRenderer>( |
| 150 | &self, |
| 151 | _renderer: &mut R, |
| 152 | location: Point<f64, Logical>, |
| 153 | _scale: Scale<f64>, |
| 154 | alpha: f32, |
| 155 | _target: RenderTarget, |
| 156 | ) -> SplitElements<LayoutElementRenderElement<R>> { |
| 157 | let inner = self.inner.borrow(); |
| 158 | |
| 159 | SplitElements { |
| 160 | normal: vec![ |
| 161 | SolidColorRenderElement::from_buffer( |
| 162 | &inner.buffer, |
| 163 | location, |
| 164 | alpha, |
| 165 | Kind::Unspecified, |
| 166 | ) |
| 167 | .into(), |
| 168 | SolidColorRenderElement::from_buffer( |
| 169 | &inner.csd_shadow_buffer, |
| 170 | location |
| 171 | - Point::from((inner.csd_shadow_width, inner.csd_shadow_width)).to_f64(), |
| 172 | alpha, |
| 173 | Kind::Unspecified, |
| 174 | ) |
| 175 | .into(), |
| 176 | ], |
| 177 | popups: vec![], |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | fn request_size( |
| 182 | &mut self, |
| 183 | size: Size<i32, Logical>, |
| 184 | _animate: bool, |
| 185 | _transaction: Option<Transaction>, |
| 186 | ) { |
| 187 | self.inner.borrow_mut().requested_size = Some(size); |
| 188 | self.inner.borrow_mut().pending_fullscreen = false; |
| 189 | } |
| 190 | |
| 191 | fn request_fullscreen(&self, _size: Size<i32, Logical>) { |
| 192 | self.inner.borrow_mut().pending_fullscreen = true; |
| 193 | } |
| 194 | |
| 195 | fn min_size(&self) -> Size<i32, Logical> { |
| 196 | self.inner.borrow().min_size |
| 197 | } |
| 198 | |
| 199 | fn max_size(&self) -> Size<i32, Logical> { |
| 200 | self.inner.borrow().max_size |
| 201 | } |
| 202 | |
| 203 | fn is_wl_surface(&self, _wl_surface: &WlSurface) -> bool { |
| 204 | false |
| 205 | } |
| 206 | |
| 207 | fn set_preferred_scale_transform(&self, _scale: output::Scale, _transform: Transform) {} |
| 208 | |
| 209 | fn has_ssd(&self) -> bool { |
| 210 | false |
| 211 | } |
| 212 | |
| 213 | fn output_enter(&self, _output: &Output) {} |
| 214 | |
| 215 | fn output_leave(&self, _output: &Output) {} |
| 216 | |
| 217 | fn set_offscreen_element_id(&self, _id: Option<Id>) {} |
| 218 | |
| 219 | fn set_activated(&mut self, _active: bool) {} |
| 220 | |
| 221 | fn set_active_in_column(&mut self, _active: bool) {} |
| 222 | |
| 223 | fn set_bounds(&self, _bounds: Size<i32, Logical>) {} |
| 224 | |
| 225 | fn configure_intent(&self) -> ConfigureIntent { |
| 226 | ConfigureIntent::CanSend |
| 227 | } |
| 228 | |
| 229 | fn send_pending_configure(&mut self) {} |
| 230 | |
| 231 | fn is_fullscreen(&self) -> bool { |
| 232 | false |
| 233 | } |
| 234 | |
| 235 | fn is_pending_fullscreen(&self) -> bool { |
| 236 | self.inner.borrow().pending_fullscreen |
| 237 | } |
| 238 | |
| 239 | fn refresh(&self) {} |
| 240 | |
| 241 | fn rules(&self) -> &ResolvedWindowRules { |
| 242 | static EMPTY: ResolvedWindowRules = ResolvedWindowRules::empty(); |
| 243 | &EMPTY |
| 244 | } |
| 245 | |
| 246 | fn animation_snapshot(&self) -> Option<&LayoutElementRenderSnapshot> { |
| 247 | None |
| 248 | } |
| 249 | |
| 250 | fn take_animation_snapshot(&mut self) -> Option<LayoutElementRenderSnapshot> { |
| 251 | None |
| 252 | } |
| 253 | |
| 254 | fn set_interactive_resize(&mut self, _data: Option<InteractiveResizeData>) {} |
| 255 | |
| 256 | fn cancel_interactive_resize(&mut self) {} |
| 257 | |
| 258 | fn update_interactive_resize(&mut self, _serial: Serial) {} |
| 259 | |
| 260 | fn interactive_resize_data(&self) -> Option<InteractiveResizeData> { |
| 261 | None |
| 262 | } |
| 263 | } |