niri
Clone
https://git.tahinli.com/Tahinli/niri.git
0 watching·0 forks·Rust·GPL-3.0·12.1 MiB·9d8f4f8·Ivan Molodetskikh·1 month ago·2997 commits
| 1 | use std::collections::HashMap; |
| 2 | use std::time::Duration; |
| 3 | |
| 4 | use niri::animation::Clock; |
| 5 | use niri::layout::{ActivateWindow, AddWindowTarget, LayoutElement as _, Options, SizingMode}; |
| 6 | use niri::render_helpers::{RenderCtx, RenderTarget}; |
| 7 | use niri_config::{Color, OutputName, PresetSize}; |
| 8 | use smithay::backend::renderer::element::RenderElement; |
| 9 | use smithay::backend::renderer::gles::GlesRenderer; |
| 10 | use smithay::desktop::layer_map_for_output; |
| 11 | use smithay::output::{Mode, Output, PhysicalProperties, Subpixel}; |
| 12 | use smithay::utils::{Physical, Size}; |
| 13 | |
| 14 | use super::{Args, TestCase}; |
| 15 | use crate::test_window::TestWindow; |
| 16 | |
| 17 | type DynStepFn = Box<dyn FnOnce(&mut Layout)>; |
| 18 | |
| 19 | pub struct Layout { |
| 20 | output: Output, |
| 21 | windows: Vec<TestWindow>, |
| 22 | clock: Clock, |
| 23 | layout: niri::layout::Layout<TestWindow>, |
| 24 | start_time: Duration, |
| 25 | steps: HashMap<Duration, DynStepFn>, |
| 26 | } |
| 27 | |
| 28 | impl Layout { |
| 29 | pub fn new(args: Args) -> Self { |
| 30 | let Args { size, clock } = args; |
| 31 | |
| 32 | let output = Output::new( |
| 33 | String::new(), |
| 34 | PhysicalProperties { |
| 35 | size: Size::from((size.w, size.h)), |
| 36 | subpixel: Subpixel::Unknown, |
| 37 | make: String::new(), |
| 38 | model: String::new(), |
| 39 | serial_number: String::new(), |
| 40 | }, |
| 41 | ); |
| 42 | let mode = Some(Mode { |
| 43 | size: size.to_physical(1), |
| 44 | refresh: 60000, |
| 45 | }); |
| 46 | output.change_current_state(mode, None, None, None); |
| 47 | output.user_data().insert_if_missing(|| OutputName { |
| 48 | connector: String::new(), |
| 49 | make: None, |
| 50 | model: None, |
| 51 | serial: None, |
| 52 | }); |
| 53 | |
| 54 | let options = Options { |
| 55 | layout: niri_config::Layout { |
| 56 | focus_ring: niri_config::FocusRing { |
| 57 | off: true, |
| 58 | ..Default::default() |
| 59 | }, |
| 60 | border: niri_config::Border { |
| 61 | off: false, |
| 62 | width: 4., |
| 63 | active_color: Color::from_rgba8_unpremul(255, 163, 72, 255), |
| 64 | inactive_color: Color::from_rgba8_unpremul(50, 50, 50, 255), |
| 65 | urgent_color: Color::from_rgba8_unpremul(155, 0, 0, 255), |
| 66 | active_gradient: None, |
| 67 | inactive_gradient: None, |
| 68 | urgent_gradient: None, |
| 69 | }, |
| 70 | ..Default::default() |
| 71 | }, |
| 72 | ..Default::default() |
| 73 | }; |
| 74 | let mut layout = niri::layout::Layout::with_options(clock.clone(), options); |
| 75 | layout.add_output(output.clone(), None); |
| 76 | |
| 77 | let start_time = clock.now_unadjusted(); |
| 78 | |
| 79 | Self { |
| 80 | output, |
| 81 | windows: Vec::new(), |
| 82 | clock, |
| 83 | layout, |
| 84 | start_time, |
| 85 | steps: HashMap::new(), |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | pub fn open_in_between(args: Args) -> Self { |
| 90 | let mut rv = Self::new(args); |
| 91 | |
| 92 | rv.add_window(TestWindow::freeform(0), Some(PresetSize::Proportion(0.3))); |
| 93 | rv.add_window(TestWindow::freeform(1), Some(PresetSize::Proportion(0.3))); |
| 94 | rv.layout.activate_window(&0); |
| 95 | |
| 96 | rv.add_step(500, |l| { |
| 97 | let win = TestWindow::freeform(2); |
| 98 | l.add_window(win.clone(), Some(PresetSize::Proportion(0.3))); |
| 99 | l.layout.start_open_animation_for_window(win.id()); |
| 100 | }); |
| 101 | |
| 102 | rv |
| 103 | } |
| 104 | |
| 105 | pub fn open_multiple_quickly(args: Args) -> Self { |
| 106 | let mut rv = Self::new(args); |
| 107 | |
| 108 | for delay in [100, 200, 300] { |
| 109 | rv.add_step(delay, move |l| { |
| 110 | let win = TestWindow::freeform(delay as usize); |
| 111 | l.add_window(win.clone(), Some(PresetSize::Proportion(0.3))); |
| 112 | l.layout.start_open_animation_for_window(win.id()); |
| 113 | }); |
| 114 | } |
| 115 | |
| 116 | rv |
| 117 | } |
| 118 | |
| 119 | pub fn open_multiple_quickly_big(args: Args) -> Self { |
| 120 | let mut rv = Self::new(args); |
| 121 | |
| 122 | for delay in [100, 200, 300] { |
| 123 | rv.add_step(delay, move |l| { |
| 124 | let win = TestWindow::freeform(delay as usize); |
| 125 | l.add_window(win.clone(), Some(PresetSize::Proportion(0.5))); |
| 126 | l.layout.start_open_animation_for_window(win.id()); |
| 127 | }); |
| 128 | } |
| 129 | |
| 130 | rv |
| 131 | } |
| 132 | |
| 133 | pub fn open_to_the_left(args: Args) -> Self { |
| 134 | let mut rv = Self::new(args); |
| 135 | |
| 136 | rv.add_window(TestWindow::freeform(0), Some(PresetSize::Proportion(0.3))); |
| 137 | rv.add_window(TestWindow::freeform(1), Some(PresetSize::Proportion(0.3))); |
| 138 | |
| 139 | rv.add_step(500, |l| { |
| 140 | let win = TestWindow::freeform(2); |
| 141 | let right_of = l.windows[0].clone(); |
| 142 | l.add_window_right_of(&right_of, win.clone(), Some(PresetSize::Proportion(0.3))); |
| 143 | l.layout.start_open_animation_for_window(win.id()); |
| 144 | }); |
| 145 | |
| 146 | rv |
| 147 | } |
| 148 | |
| 149 | pub fn open_to_the_left_big(args: Args) -> Self { |
| 150 | let mut rv = Self::new(args); |
| 151 | |
| 152 | rv.add_window(TestWindow::freeform(0), Some(PresetSize::Proportion(0.3))); |
| 153 | rv.add_window(TestWindow::freeform(1), Some(PresetSize::Proportion(0.8))); |
| 154 | |
| 155 | rv.add_step(500, |l| { |
| 156 | let win = TestWindow::freeform(2); |
| 157 | let right_of = l.windows[0].clone(); |
| 158 | l.add_window_right_of(&right_of, win.clone(), Some(PresetSize::Proportion(0.5))); |
| 159 | l.layout.start_open_animation_for_window(win.id()); |
| 160 | }); |
| 161 | |
| 162 | rv |
| 163 | } |
| 164 | |
| 165 | fn add_window(&mut self, mut window: TestWindow, width: Option<PresetSize>) { |
| 166 | let ws = self.layout.active_workspace().unwrap(); |
| 167 | let min_size = window.min_size(); |
| 168 | let max_size = window.max_size(); |
| 169 | window.request_size( |
| 170 | ws.new_window_size(width, None, false, window.rules(), (min_size, max_size)), |
| 171 | SizingMode::Normal, |
| 172 | false, |
| 173 | None, |
| 174 | ); |
| 175 | window.communicate(); |
| 176 | |
| 177 | self.layout.add_window( |
| 178 | window.clone(), |
| 179 | AddWindowTarget::Auto, |
| 180 | width, |
| 181 | None, |
| 182 | false, |
| 183 | false, |
| 184 | ActivateWindow::default(), |
| 185 | ); |
| 186 | self.windows.push(window); |
| 187 | } |
| 188 | |
| 189 | fn add_window_right_of( |
| 190 | &mut self, |
| 191 | right_of: &TestWindow, |
| 192 | mut window: TestWindow, |
| 193 | width: Option<PresetSize>, |
| 194 | ) { |
| 195 | let ws = self.layout.active_workspace().unwrap(); |
| 196 | let min_size = window.min_size(); |
| 197 | let max_size = window.max_size(); |
| 198 | window.request_size( |
| 199 | ws.new_window_size(width, None, false, window.rules(), (min_size, max_size)), |
| 200 | SizingMode::Normal, |
| 201 | false, |
| 202 | None, |
| 203 | ); |
| 204 | window.communicate(); |
| 205 | |
| 206 | self.layout.add_window( |
| 207 | window.clone(), |
| 208 | AddWindowTarget::NextTo(right_of.id()), |
| 209 | width, |
| 210 | None, |
| 211 | false, |
| 212 | false, |
| 213 | ActivateWindow::default(), |
| 214 | ); |
| 215 | self.windows.push(window); |
| 216 | } |
| 217 | |
| 218 | fn add_step(&mut self, delay_ms: u64, f: impl FnOnce(&mut Self) + 'static) { |
| 219 | self.steps |
| 220 | .insert(Duration::from_millis(delay_ms), Box::new(f) as _); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | impl TestCase for Layout { |
| 225 | fn resize(&mut self, width: i32, height: i32) { |
| 226 | let mode = Some(Mode { |
| 227 | size: Size::from((width, height)), |
| 228 | refresh: 60000, |
| 229 | }); |
| 230 | self.output.change_current_state(mode, None, None, None); |
| 231 | layer_map_for_output(&self.output).arrange(); |
| 232 | self.layout.update_output_size(&self.output); |
| 233 | for win in &self.windows { |
| 234 | if win.communicate() { |
| 235 | self.layout.update_window(win.id(), None); |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | fn are_animations_ongoing(&self) -> bool { |
| 241 | self.layout.are_animations_ongoing(Some(&self.output)) || !self.steps.is_empty() |
| 242 | } |
| 243 | |
| 244 | fn advance_animations(&mut self, _current_time: Duration) { |
| 245 | let now_unadjusted = self.clock.now_unadjusted(); |
| 246 | let run = self |
| 247 | .steps |
| 248 | .keys() |
| 249 | .copied() |
| 250 | .filter(|delay| self.start_time + *delay <= now_unadjusted) |
| 251 | .collect::<Vec<_>>(); |
| 252 | for delay in &run { |
| 253 | let now = self.start_time + *delay; |
| 254 | self.clock.set_unadjusted(now); |
| 255 | self.layout.advance_animations(); |
| 256 | |
| 257 | let f = self.steps.remove(delay).unwrap(); |
| 258 | f(self); |
| 259 | } |
| 260 | |
| 261 | self.clock.set_unadjusted(now_unadjusted); |
| 262 | self.layout.advance_animations(); |
| 263 | } |
| 264 | |
| 265 | fn render( |
| 266 | &mut self, |
| 267 | renderer: &mut GlesRenderer, |
| 268 | _size: Size<i32, Physical>, |
| 269 | ) -> Vec<Box<dyn RenderElement<GlesRenderer>>> { |
| 270 | self.layout.update_render_elements(Some(&self.output)); |
| 271 | |
| 272 | let mut rv = Vec::new(); |
| 273 | let ctx = RenderCtx { |
| 274 | renderer, |
| 275 | target: RenderTarget::Output, |
| 276 | xray: None, |
| 277 | }; |
| 278 | self.layout |
| 279 | .monitor_for_output(&self.output) |
| 280 | .unwrap() |
| 281 | .render_workspaces(ctx, true, &mut |elem| rv.push(Box::new(elem) as _)); |
| 282 | rv |
| 283 | } |
| 284 | } |