From 2a163bb4b54954614ab07e1ce8b06abada169c7e Mon Sep 17 00:00:00 2001 From: Ivan Molodetskikh Date: Mon, 14 Aug 2023 18:29:50 +0400 Subject: Add Mod+R to toggle between preset widths --- src/layout.rs | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) (limited to 'src/layout.rs') diff --git a/src/layout.rs b/src/layout.rs index 618f6fe0..6e4d9306 100644 --- a/src/layout.rs +++ b/src/layout.rs @@ -50,6 +50,11 @@ use smithay::wayland::shell::xdg::XdgToplevelSurfaceData; use crate::animation::Animation; const PADDING: i32 = 16; +const WIDTH_PROPORTIONS: [ColumnWidth; 3] = [ + ColumnWidth::Proportion(1. / 3.), + ColumnWidth::Proportion(0.5), + ColumnWidth::Proportion(2. / 3.), +]; #[derive(Debug, Clone, PartialEq, Eq)] pub struct OutputId(String); @@ -135,6 +140,11 @@ pub struct Workspace { enum ColumnWidth { /// Proportion of the current view width. Proportion(f64), + /// One of the proportion presets. + /// + /// This is separate from Proportion in order to be able to reliably cycle between preset + /// proportions. + PresetProportion(usize), /// Fixed width in logical pixels. Fixed(i32), } @@ -199,6 +209,7 @@ impl ColumnWidth { fn resolve(self, view_width: i32) -> i32 { match self { ColumnWidth::Proportion(proportion) => (view_width as f64 * proportion).floor() as i32, + ColumnWidth::PresetProportion(idx) => WIDTH_PROPORTIONS[idx].resolve(view_width), ColumnWidth::Fixed(width) => width, } } @@ -741,6 +752,13 @@ impl MonitorSet { } } } + + pub fn toggle_width(&mut self) { + let Some(monitor) = self.active_monitor() else { + return; + }; + monitor.toggle_width(); + } } impl MonitorSet { @@ -954,6 +972,10 @@ impl Monitor { ws.advance_animations(current_time); } } + + fn toggle_width(&mut self) { + self.active_workspace().toggle_width(); + } } impl Monitor { @@ -1362,6 +1384,14 @@ impl Workspace { None } + + fn toggle_width(&mut self) { + if self.columns.is_empty() { + return; + } + + self.columns[self.active_column_idx].toggle_width(self.view_size); + } } impl Workspace { @@ -1510,6 +1540,21 @@ impl Column { assert!(!self.windows.is_empty(), "columns can't be empty"); assert!(self.active_window_idx < self.windows.len()); } + + fn toggle_width(&mut self, view_size: Size) { + let idx = match self.width { + ColumnWidth::PresetProportion(idx) => (idx + 1) % WIDTH_PROPORTIONS.len(), + _ => { + let current = self.size().w; + WIDTH_PROPORTIONS + .into_iter() + .position(|prop| prop.resolve(view_size.w - PADDING) - PADDING > current) + .unwrap_or(0) + } + }; + let width = ColumnWidth::PresetProportion(idx); + self.set_width(view_size, width); + } } pub fn output_size(output: &Output) -> Size { -- cgit