tony / oxwm

Branch: 7fdee0e
Switch branches/tags
master
emzywastaken layout/mod.rs: apply clippy lints 3816a44 2025-12-15
109 lines · 2.6 KB Raw Blame History
1 pub mod grid;
2 pub mod monocle;
3 pub mod normie;
4 pub mod tabbed;
5 pub mod tiling;
6
7 use std::str::FromStr;
8
9 use x11rb::protocol::xproto::Window;
10
11 pub type LayoutBox = Box<dyn Layout>;
12
13 pub struct GapConfig {
14 pub inner_horizontal: u32,
15 pub inner_vertical: u32,
16 pub outer_horizontal: u32,
17 pub outer_vertical: u32,
18 }
19
20 pub enum LayoutType {
21 Tiling,
22 Normie,
23 Grid,
24 Monocle,
25 Tabbed,
26 }
27
28 impl LayoutType {
29 pub fn to_boxed_layout(&self) -> LayoutBox {
30 match self {
31 Self::Tiling => Box::new(tiling::TilingLayout),
32 Self::Normie => Box::new(normie::NormieLayout),
33 Self::Grid => Box::new(grid::GridLayout),
34 Self::Monocle => Box::new(monocle::MonocleLayout),
35 Self::Tabbed => Box::new(tabbed::TabbedLayout),
36 }
37 }
38
39 pub fn next(&self) -> Self {
40 match self {
41 Self::Tiling => Self::Normie,
42 Self::Normie => Self::Grid,
43 Self::Grid => Self::Monocle,
44 Self::Monocle => Self::Tabbed,
45 Self::Tabbed => Self::Tiling,
46 }
47 }
48
49 pub fn as_str(&self) -> &'static str {
50 match self {
51 Self::Tiling => "tiling",
52 Self::Normie => "normie",
53 Self::Grid => "grid",
54 Self::Monocle => "monocle",
55 Self::Tabbed => "tabbed",
56 }
57 }
58 }
59
60 impl FromStr for LayoutType {
61 type Err = String;
62
63 fn from_str(s: &str) -> Result<Self, String> {
64 match s.to_lowercase().as_str() {
65 "tiling" => Ok(Self::Tiling),
66 "normie" | "floating" => Ok(Self::Normie),
67 "grid" => Ok(Self::Grid),
68 "monocle" => Ok(Self::Monocle),
69 "tabbed" => Ok(Self::Tabbed),
70 _ => Err(format!("Invalid Layout Type: {}", s)),
71 }
72 }
73 }
74
75 pub fn layout_from_str(s: &str) -> Result<LayoutBox, String> {
76 let layout_type = LayoutType::from_str(s)?;
77 Ok(layout_type.to_boxed_layout())
78 }
79
80 pub fn next_layout(current_name: &str) -> &'static str {
81 LayoutType::from_str(current_name)
82 .ok()
83 .map(|layout_type| layout_type.next())
84 .unwrap_or(LayoutType::Tiling)
85 .as_str()
86 }
87
88 pub trait Layout {
89 fn arrange(
90 &self,
91 windows: &[Window],
92 screen_width: u32,
93 screen_height: u32,
94 gaps: &GapConfig,
95 master_factor: f32,
96 num_master: i32,
97 smartgaps_enabled: bool,
98 ) -> Vec<WindowGeometry>;
99 fn name(&self) -> &'static str;
100 fn symbol(&self) -> &'static str;
101 }
102
103 #[derive(Clone)]
104 pub struct WindowGeometry {
105 pub x_coordinate: i32,
106 pub y_coordinate: i32,
107 pub width: u32,
108 pub height: u32,
109 }