| 1 | pub mod grid; |
| 2 | pub mod monocle; |
| 3 | pub mod normie; |
| 4 | pub mod tiling; |
| 5 | |
| 6 | use x11rb::protocol::xproto::Window; |
| 7 | |
| 8 | pub type LayoutBox = Box<dyn Layout>; |
| 9 | |
| 10 | pub struct GapConfig { |
| 11 | pub inner_horizontal: u32, |
| 12 | pub inner_vertical: u32, |
| 13 | pub outer_horizontal: u32, |
| 14 | pub outer_vertical: u32, |
| 15 | } |
| 16 | |
| 17 | pub enum LayoutType { |
| 18 | Tiling, |
| 19 | Normie, |
| 20 | Grid, |
| 21 | Monocle, |
| 22 | } |
| 23 | |
| 24 | impl LayoutType { |
| 25 | pub fn new(&self) -> LayoutBox { |
| 26 | match self { |
| 27 | Self::Tiling => Box::new(tiling::TilingLayout), |
| 28 | Self::Normie => Box::new(normie::NormieLayout), |
| 29 | Self::Grid => Box::new(grid::GridLayout), |
| 30 | Self::Monocle => Box::new(monocle::MonocleLayout), |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | pub fn next(&self) -> Self { |
| 35 | match self { |
| 36 | Self::Tiling => Self::Normie, |
| 37 | Self::Normie => Self::Grid, |
| 38 | Self::Grid => Self::Monocle, |
| 39 | Self::Monocle => Self::Tiling, |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | pub fn as_str(&self) -> &'static str { |
| 44 | match self { |
| 45 | Self::Tiling => "tiling", |
| 46 | Self::Normie => "normie", |
| 47 | Self::Grid => "grid", |
| 48 | Self::Monocle => "monocle", |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | pub fn from_str(s: &str) -> Result<Self, String> { |
| 53 | match s.to_lowercase().as_str() { |
| 54 | "tiling" => Ok(Self::Tiling), |
| 55 | "normie" | "floating" => Ok(Self::Normie), |
| 56 | "grid" => Ok(Self::Grid), |
| 57 | "monocle" => Ok(Self::Monocle), |
| 58 | _ => Err(format!("Invalid Layout Type: {}", s)), |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | pub fn layout_from_str(s: &str) -> Result<LayoutBox, String> { |
| 64 | let layout_type = LayoutType::from_str(s)?; |
| 65 | Ok(layout_type.new()) |
| 66 | } |
| 67 | |
| 68 | pub fn next_layout(current_name: &str) -> &'static str { |
| 69 | LayoutType::from_str(current_name) |
| 70 | .ok() |
| 71 | .map(|layout_type| layout_type.next()) |
| 72 | .unwrap_or(LayoutType::Tiling) |
| 73 | .as_str() |
| 74 | } |
| 75 | |
| 76 | pub trait Layout { |
| 77 | fn arrange( |
| 78 | &self, |
| 79 | windows: &[Window], |
| 80 | screen_width: u32, |
| 81 | screen_height: u32, |
| 82 | gaps: &GapConfig, |
| 83 | ) -> Vec<WindowGeometry>; |
| 84 | fn name(&self) -> &'static str; |
| 85 | fn symbol(&self) -> &'static str; |
| 86 | } |
| 87 | |
| 88 | #[derive(Clone)] |
| 89 | pub struct WindowGeometry { |
| 90 | pub x_coordinate: i32, |
| 91 | pub y_coordinate: i32, |
| 92 | pub width: u32, |
| 93 | pub height: u32, |
| 94 | } |