Add set-column-width action
Browse files at this commit7558ac14e6473c98bc4eea442b28a5ae330115efParent: bb3fbe24 changed files +188 -5
- resources/default-config.kdl +11 -0
- src/config.rs +95 -3
- src/input.rs +3 -0
- src/layout.rs +79 -2
resources/default-config.kdlmodified+11 -0
| @@ -173,6 +173,17 @@ binds { | |||
| 173 | Mod+F { maximize-column; } | 173 | Mod+F { maximize-column; } |
| 174 | Mod+Shift+F { fullscreen-window; } | 174 | Mod+Shift+F { fullscreen-window; } |
| 175 | 175 | ||
| 176 | + // Finer width adjustments. | ||
| 177 | + // This command can also: | ||
| 178 | + // * set width in pixels: "1000" | ||
| 179 | + // * adjust width in pixels: "-5" or "+5" | ||
| 180 | + // * set width as a percentage of screen width: "25%" | ||
| 181 | + // * adjust width as a percentage of screen width: "-10%" or "+10%" | ||
| 182 | + // Pixel sizes use logical, or scaled, pixels. I.e. on an output with scale 2.0, | ||
| 183 | + // set-column-width "100" will make the column occupy 200 physical screen pixels. | ||
| 184 | + Mod+Minus { set-column-width "-10%"; } | ||
| 185 | + Mod+Equal { set-column-width "+10%"; } | ||
| 186 | + | ||
| 176 | Print { screenshot; } | 187 | Print { screenshot; } |
| 177 | Mod+Shift+E { quit; } | 188 | Mod+Shift+E { quit; } |
| 178 | 189 | ||
src/config.rsmodified+95 -3
| @@ -181,10 +181,10 @@ impl Default for Cursor { | |||
| 181 | } | 181 | } |
| 182 | } | 182 | } |
| 183 | 183 | ||
| 184 | -#[derive(knuffel::Decode, Debug, Default, PartialEq, Eq)] | 184 | +#[derive(knuffel::Decode, Debug, Default, PartialEq)] |
| 185 | pub struct Binds(#[knuffel(children)] pub Vec<Bind>); | 185 | pub struct Binds(#[knuffel(children)] pub Vec<Bind>); |
| 186 | 186 | ||
| 187 | -#[derive(knuffel::Decode, Debug, PartialEq, Eq)] | 187 | +#[derive(knuffel::Decode, Debug, PartialEq)] |
| 188 | pub struct Bind { | 188 | pub struct Bind { |
| 189 | #[knuffel(node_name)] | 189 | #[knuffel(node_name)] |
| 190 | pub key: Key, | 190 | pub key: Key, |
| @@ -209,7 +209,7 @@ bitflags! { | |||
| 209 | } | 209 | } |
| 210 | } | 210 | } |
| 211 | 211 | ||
| 212 | -#[derive(knuffel::Decode, Debug, Clone, PartialEq, Eq)] | 212 | +#[derive(knuffel::Decode, Debug, Clone, PartialEq)] |
| 213 | pub enum Action { | 213 | pub enum Action { |
| 214 | #[knuffel(skip)] | 214 | #[knuffel(skip)] |
| 215 | None, | 215 | None, |
| @@ -248,6 +248,15 @@ pub enum Action { | |||
| 248 | MoveWindowToMonitorUp, | 248 | MoveWindowToMonitorUp, |
| 249 | SwitchPresetColumnWidth, | 249 | SwitchPresetColumnWidth, |
| 250 | MaximizeColumn, | 250 | MaximizeColumn, |
| 251 | + SetColumnWidth(#[knuffel(argument, str)] SizeChange), | ||
| 252 | +} | ||
| 253 | + | ||
| 254 | +#[derive(Debug, Clone, Copy, PartialEq)] | ||
| 255 | +pub enum SizeChange { | ||
| 256 | + SetFixed(i32), | ||
| 257 | + SetProportion(f64), | ||
| 258 | + AdjustFixed(i32), | ||
| 259 | + AdjustProportion(f64), | ||
| 251 | } | 260 | } |
| 252 | 261 | ||
| 253 | #[derive(knuffel::Decode, Debug, PartialEq)] | 262 | #[derive(knuffel::Decode, Debug, PartialEq)] |
| @@ -383,6 +392,58 @@ impl FromStr for Key { | |||
| 383 | } | 392 | } |
| 384 | } | 393 | } |
| 385 | 394 | ||
| 395 | +impl FromStr for SizeChange { | ||
| 396 | + type Err = miette::Error; | ||
| 397 | + | ||
| 398 | + fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
| 399 | + match s.split_once('%') { | ||
| 400 | + Some((value, empty)) => { | ||
| 401 | + if !empty.is_empty() { | ||
| 402 | + return Err(miette!("trailing characters after '%' are not allowed")); | ||
| 403 | + } | ||
| 404 | + | ||
| 405 | + match value.bytes().next() { | ||
| 406 | + Some(b'-' | b'+') => { | ||
| 407 | + let value = value | ||
| 408 | + .parse() | ||
| 409 | + .into_diagnostic() | ||
| 410 | + .context("error parsing value")?; | ||
| 411 | + Ok(Self::AdjustProportion(value)) | ||
| 412 | + } | ||
| 413 | + Some(_) => { | ||
| 414 | + let value = value | ||
| 415 | + .parse() | ||
| 416 | + .into_diagnostic() | ||
| 417 | + .context("error parsing value")?; | ||
| 418 | + Ok(Self::SetProportion(value)) | ||
| 419 | + } | ||
| 420 | + None => Err(miette!("value is missing")), | ||
| 421 | + } | ||
| 422 | + } | ||
| 423 | + None => { | ||
| 424 | + let value = s; | ||
| 425 | + match value.bytes().next() { | ||
| 426 | + Some(b'-' | b'+') => { | ||
| 427 | + let value = value | ||
| 428 | + .parse() | ||
| 429 | + .into_diagnostic() | ||
| 430 | + .context("error parsing value")?; | ||
| 431 | + Ok(Self::AdjustFixed(value)) | ||
| 432 | + } | ||
| 433 | + Some(_) => { | ||
| 434 | + let value = value | ||
| 435 | + .parse() | ||
| 436 | + .into_diagnostic() | ||
| 437 | + .context("error parsing value")?; | ||
| 438 | + Ok(Self::SetFixed(value)) | ||
| 439 | + } | ||
| 440 | + None => Err(miette!("value is missing")), | ||
| 441 | + } | ||
| 442 | + } | ||
| 443 | + } | ||
| 444 | + } | ||
| 445 | +} | ||
| 446 | + | ||
| 386 | #[cfg(test)] | 447 | #[cfg(test)] |
| 387 | mod tests { | 448 | mod tests { |
| 388 | use miette::NarratableReportHandler; | 449 | use miette::NarratableReportHandler; |
| @@ -586,4 +647,35 @@ mod tests { | |||
| 586 | assert!("1920x1080@".parse::<Mode>().is_err()); | 647 | assert!("1920x1080@".parse::<Mode>().is_err()); |
| 587 | assert!("1920x1080@60Hz".parse::<Mode>().is_err()); | 648 | assert!("1920x1080@60Hz".parse::<Mode>().is_err()); |
| 588 | } | 649 | } |
| 650 | + | ||
| 651 | + #[test] | ||
| 652 | + fn parse_size_change() { | ||
| 653 | + assert_eq!( | ||
| 654 | + "10".parse::<SizeChange>().unwrap(), | ||
| 655 | + SizeChange::SetFixed(10), | ||
| 656 | + ); | ||
| 657 | + assert_eq!( | ||
| 658 | + "+10".parse::<SizeChange>().unwrap(), | ||
| 659 | + SizeChange::AdjustFixed(10), | ||
| 660 | + ); | ||
| 661 | + assert_eq!( | ||
| 662 | + "-10".parse::<SizeChange>().unwrap(), | ||
| 663 | + SizeChange::AdjustFixed(-10), | ||
| 664 | + ); | ||
| 665 | + assert_eq!( | ||
| 666 | + "10%".parse::<SizeChange>().unwrap(), | ||
| 667 | + SizeChange::SetProportion(10.), | ||
| 668 | + ); | ||
| 669 | + assert_eq!( | ||
| 670 | + "+10%".parse::<SizeChange>().unwrap(), | ||
| 671 | + SizeChange::AdjustProportion(10.), | ||
| 672 | + ); | ||
| 673 | + assert_eq!( | ||
| 674 | + "-10%".parse::<SizeChange>().unwrap(), | ||
| 675 | + SizeChange::AdjustProportion(-10.), | ||
| 676 | + ); | ||
| 677 | + | ||
| 678 | + assert!("-".parse::<SizeChange>().is_err()); | ||
| 679 | + assert!("10% ".parse::<SizeChange>().is_err()); | ||
| 680 | + } | ||
| 589 | } | 681 | } |
src/input.rsmodified+3 -0
| @@ -296,6 +296,9 @@ impl State { | |||
| 296 | self.move_cursor_to_output(&output); | 296 | self.move_cursor_to_output(&output); |
| 297 | } | 297 | } |
| 298 | } | 298 | } |
| 299 | + Action::SetColumnWidth(change) => { | ||
| 300 | + self.niri.monitor_set.set_column_width(change); | ||
| 301 | + } | ||
| 299 | } | 302 | } |
| 300 | } | 303 | } |
| 301 | } | 304 | } |
src/layout.rsmodified+79 -2
| @@ -52,7 +52,7 @@ use smithay::wayland::compositor::with_states; | |||
| 52 | use smithay::wayland::shell::xdg::SurfaceCachedState; | 52 | use smithay::wayland::shell::xdg::SurfaceCachedState; |
| 53 | 53 | ||
| 54 | use crate::animation::Animation; | 54 | use crate::animation::Animation; |
| 55 | -use crate::config::{Color, Config}; | 55 | +use crate::config::{Color, Config, SizeChange}; |
| 56 | 56 | ||
| 57 | const PADDING: i32 = 16; | 57 | const PADDING: i32 = 16; |
| 58 | const WIDTH_PROPORTIONS: [ColumnWidth; 3] = [ | 58 | const WIDTH_PROPORTIONS: [ColumnWidth; 3] = [ |
| @@ -288,7 +288,8 @@ impl ColumnWidth { | |||
| 288 | match self { | 288 | match self { |
| 289 | ColumnWidth::Proportion(proportion) => (view_width as f64 * proportion).floor() as i32, | 289 | ColumnWidth::Proportion(proportion) => (view_width as f64 * proportion).floor() as i32, |
| 290 | ColumnWidth::PresetProportion(idx) => WIDTH_PROPORTIONS[idx].resolve(view_width), | 290 | ColumnWidth::PresetProportion(idx) => WIDTH_PROPORTIONS[idx].resolve(view_width), |
| 291 | - ColumnWidth::Fixed(width) => width, | 291 | + // FIXME: remove this PADDING from here after redesigning how padding works. |
| 292 | + ColumnWidth::Fixed(width) => width + PADDING, | ||
| 292 | } | 293 | } |
| 293 | } | 294 | } |
| 294 | } | 295 | } |
| @@ -912,6 +913,13 @@ impl<W: LayoutElement> MonitorSet<W> { | |||
| 912 | monitor.toggle_full_width(); | 913 | monitor.toggle_full_width(); |
| 913 | } | 914 | } |
| 914 | 915 | ||
| 916 | + pub fn set_column_width(&mut self, change: SizeChange) { | ||
| 917 | + let Some(monitor) = self.active_monitor() else { | ||
| 918 | + return; | ||
| 919 | + }; | ||
| 920 | + monitor.set_column_width(change); | ||
| 921 | + } | ||
| 922 | + | ||
| 915 | pub fn focus_output(&mut self, output: &Output) { | 923 | pub fn focus_output(&mut self, output: &Output) { |
| 916 | if let MonitorSet::Normal { | 924 | if let MonitorSet::Normal { |
| 917 | monitors, | 925 | monitors, |
| @@ -1278,6 +1286,10 @@ impl<W: LayoutElement> Monitor<W> { | |||
| 1278 | fn toggle_full_width(&mut self) { | 1286 | fn toggle_full_width(&mut self) { |
| 1279 | self.active_workspace().toggle_full_width(); | 1287 | self.active_workspace().toggle_full_width(); |
| 1280 | } | 1288 | } |
| 1289 | + | ||
| 1290 | + fn set_column_width(&mut self, change: SizeChange) { | ||
| 1291 | + self.active_workspace().set_column_width(change); | ||
| 1292 | + } | ||
| 1281 | } | 1293 | } |
| 1282 | 1294 | ||
| 1283 | impl Monitor<Window> { | 1295 | impl Monitor<Window> { |
| @@ -1871,6 +1883,18 @@ impl<W: LayoutElement> Workspace<W> { | |||
| 1871 | self.columns[self.active_column_idx].toggle_full_width(self.view_size, self.working_area); | 1883 | self.columns[self.active_column_idx].toggle_full_width(self.view_size, self.working_area); |
| 1872 | } | 1884 | } |
| 1873 | 1885 | ||
| 1886 | + fn set_column_width(&mut self, change: SizeChange) { | ||
| 1887 | + if self.columns.is_empty() { | ||
| 1888 | + return; | ||
| 1889 | + } | ||
| 1890 | + | ||
| 1891 | + self.columns[self.active_column_idx].set_column_width( | ||
| 1892 | + self.view_size, | ||
| 1893 | + self.working_area, | ||
| 1894 | + change, | ||
| 1895 | + ); | ||
| 1896 | + } | ||
| 1897 | + | ||
| 1874 | pub fn set_fullscreen(&mut self, window: &W, is_fullscreen: bool) { | 1898 | pub fn set_fullscreen(&mut self, window: &W, is_fullscreen: bool) { |
| 1875 | let (mut col_idx, win_idx) = self | 1899 | let (mut col_idx, win_idx) = self |
| 1876 | .columns | 1900 | .columns |
| @@ -2201,6 +2225,48 @@ impl<W: LayoutElement> Column<W> { | |||
| 2201 | self.set_width(view_size, working_area, width); | 2225 | self.set_width(view_size, working_area, width); |
| 2202 | } | 2226 | } |
| 2203 | 2227 | ||
| 2228 | + fn set_column_width( | ||
| 2229 | + &mut self, | ||
| 2230 | + view_size: Size<i32, Logical>, | ||
| 2231 | + working_area: Rectangle<i32, Logical>, | ||
| 2232 | + change: SizeChange, | ||
| 2233 | + ) { | ||
| 2234 | + let current_px = self.width.resolve(working_area.size.w - PADDING) - PADDING; | ||
| 2235 | + | ||
| 2236 | + let current = match self.width { | ||
| 2237 | + ColumnWidth::PresetProportion(idx) => WIDTH_PROPORTIONS[idx], | ||
| 2238 | + current => current, | ||
| 2239 | + }; | ||
| 2240 | + | ||
| 2241 | + // FIXME: fix overflows then remove limits. | ||
| 2242 | + const MAX_PX: i32 = 100000; | ||
| 2243 | + const MAX_F: f64 = 10000.; | ||
| 2244 | + | ||
| 2245 | + let width = match (current, change) { | ||
| 2246 | + (_, SizeChange::SetFixed(fixed)) => ColumnWidth::Fixed(fixed.clamp(1, MAX_PX)), | ||
| 2247 | + (_, SizeChange::SetProportion(proportion)) => { | ||
| 2248 | + ColumnWidth::Proportion((proportion / 100.).clamp(0., MAX_F)) | ||
| 2249 | + } | ||
| 2250 | + (_, SizeChange::AdjustFixed(delta)) => { | ||
| 2251 | + let width = current_px.saturating_add(delta).clamp(1, MAX_PX); | ||
| 2252 | + ColumnWidth::Fixed(width) | ||
| 2253 | + } | ||
| 2254 | + (ColumnWidth::Proportion(current), SizeChange::AdjustProportion(delta)) => { | ||
| 2255 | + let proportion = (current + delta / 100.).clamp(0., MAX_F); | ||
| 2256 | + ColumnWidth::Proportion(proportion) | ||
| 2257 | + } | ||
| 2258 | + (ColumnWidth::Fixed(_), SizeChange::AdjustProportion(delta)) => { | ||
| 2259 | + let current = | ||
| 2260 | + (current_px + PADDING) as f64 / (working_area.size.w - PADDING) as f64; | ||
| 2261 | + let proportion = (current + delta / 100.).clamp(0., MAX_F); | ||
| 2262 | + ColumnWidth::Proportion(proportion) | ||
| 2263 | + } | ||
| 2264 | + (ColumnWidth::PresetProportion(_), _) => unreachable!(), | ||
| 2265 | + }; | ||
| 2266 | + | ||
| 2267 | + self.set_width(view_size, working_area, width); | ||
| 2268 | + } | ||
| 2269 | + | ||
| 2204 | fn set_fullscreen( | 2270 | fn set_fullscreen( |
| 2205 | &mut self, | 2271 | &mut self, |
| 2206 | view_size: Size<i32, Logical>, | 2272 | view_size: Size<i32, Logical>, |
| @@ -2384,6 +2450,15 @@ mod tests { | |||
| 2384 | }) | 2450 | }) |
| 2385 | } | 2451 | } |
| 2386 | 2452 | ||
| 2453 | + fn arbitrary_size_change() -> impl Strategy<Value = SizeChange> { | ||
| 2454 | + prop_oneof![ | ||
| 2455 | + (0..).prop_map(SizeChange::SetFixed), | ||
| 2456 | + (0f64..).prop_map(SizeChange::SetProportion), | ||
| 2457 | + any::<i32>().prop_map(SizeChange::AdjustFixed), | ||
| 2458 | + any::<f64>().prop_map(SizeChange::AdjustProportion), | ||
| 2459 | + ] | ||
| 2460 | + } | ||
| 2461 | + | ||
| 2387 | #[derive(Debug, Clone, Copy, Arbitrary)] | 2462 | #[derive(Debug, Clone, Copy, Arbitrary)] |
| 2388 | enum Op { | 2463 | enum Op { |
| 2389 | AddOutput(#[proptest(strategy = "1..=5usize")] usize), | 2464 | AddOutput(#[proptest(strategy = "1..=5usize")] usize), |
| @@ -2417,6 +2492,7 @@ mod tests { | |||
| 2417 | MoveWindowToOutput(#[proptest(strategy = "1..=5u8")] u8), | 2492 | MoveWindowToOutput(#[proptest(strategy = "1..=5u8")] u8), |
| 2418 | SwitchPresetColumnWidth, | 2493 | SwitchPresetColumnWidth, |
| 2419 | MaximizeColumn, | 2494 | MaximizeColumn, |
| 2495 | + SetColumnWidth(#[proptest(strategy = "arbitrary_size_change()")] SizeChange), | ||
| 2420 | Communicate(#[proptest(strategy = "1..=5usize")] usize), | 2496 | Communicate(#[proptest(strategy = "1..=5usize")] usize), |
| 2421 | } | 2497 | } |
| 2422 | 2498 | ||
| @@ -2506,6 +2582,7 @@ mod tests { | |||
| 2506 | } | 2582 | } |
| 2507 | Op::SwitchPresetColumnWidth => monitor_set.toggle_width(), | 2583 | Op::SwitchPresetColumnWidth => monitor_set.toggle_width(), |
| 2508 | Op::MaximizeColumn => monitor_set.toggle_full_width(), | 2584 | Op::MaximizeColumn => monitor_set.toggle_full_width(), |
| 2585 | + Op::SetColumnWidth(change) => monitor_set.set_column_width(change), | ||
| 2509 | Op::Communicate(id) => { | 2586 | Op::Communicate(id) => { |
| 2510 | let mut window = None; | 2587 | let mut window = None; |
| 2511 | match monitor_set { | 2588 | match monitor_set { |