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