forked from leftwm/leftwm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.rs
202 lines (187 loc) · 6.3 KB
/
mod.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
use super::models::Window;
use super::models::Workspace;
use crate::models::Tag;
use serde::{Deserialize, Serialize};
use std::str::FromStr;
use thiserror::Error;
mod center_main;
mod center_main_balanced;
mod center_main_fluid;
mod even_horizontal;
mod even_vertical;
mod fibonacci;
mod grid_horizontal;
mod left_main;
mod main_and_deck;
mod main_and_horizontal_stack;
mod main_and_vert_stack;
mod monocle;
mod right_main_and_vert_stack;
#[derive(Debug, Serialize, Deserialize, Clone, Copy, PartialEq, Eq)]
pub enum Layout {
MainAndVertStack,
MainAndHorizontalStack,
MainAndDeck,
GridHorizontal,
EvenHorizontal,
EvenVertical,
Fibonacci,
LeftMain,
CenterMain,
CenterMainBalanced,
CenterMainFluid,
Monocle,
RightWiderLeftStack,
LeftWiderRightStack,
}
pub const LAYOUTS: &[Layout] = &[
Layout::MainAndVertStack,
Layout::MainAndHorizontalStack,
Layout::MainAndDeck,
Layout::GridHorizontal,
Layout::EvenHorizontal,
Layout::EvenVertical,
Layout::Fibonacci,
Layout::LeftMain,
Layout::CenterMain,
Layout::CenterMainBalanced,
Layout::CenterMainFluid,
Layout::Monocle,
Layout::RightWiderLeftStack,
Layout::LeftWiderRightStack,
];
impl Default for Layout {
fn default() -> Self {
Self::MainAndVertStack
}
}
// This is tedious, but simple and effective.
impl Layout {
pub fn update_windows(&self, workspace: &Workspace, windows: &mut [&mut Window], tag: &Tag) {
match self {
Self::MainAndVertStack | Self::LeftWiderRightStack => {
main_and_vert_stack::update(workspace, tag, windows);
}
Self::MainAndHorizontalStack => {
main_and_horizontal_stack::update(workspace, tag, windows);
}
Self::MainAndDeck => main_and_deck::update(workspace, tag, windows),
Self::GridHorizontal => grid_horizontal::update(workspace, tag, windows),
Self::EvenHorizontal => even_horizontal::update(workspace, windows),
Self::EvenVertical => even_vertical::update(workspace, windows),
Self::Fibonacci => fibonacci::update(workspace, tag, windows),
Self::LeftMain => left_main::update(workspace, tag, windows),
Self::CenterMain => center_main::update(workspace, tag, windows),
Self::CenterMainBalanced => center_main_balanced::update(workspace, tag, windows),
Self::CenterMainFluid => center_main_fluid::update(workspace, tag, windows),
Self::Monocle => monocle::update(workspace, windows),
Self::RightWiderLeftStack => {
right_main_and_vert_stack::update(workspace, tag, windows);
}
}
}
pub const fn main_width(&self) -> u8 {
match self {
Self::RightWiderLeftStack | Self::LeftWiderRightStack => 75,
_ => 50,
}
}
//The possible permutations that a layout can be flipped => (flipable_horz, flipable_vert)
pub fn rotations(&self) -> Vec<(bool, bool)> {
match self {
//Layouts that can be flipped both ways
Self::Fibonacci | Self::GridHorizontal => {
[(false, false), (true, false), (true, true), (false, true)].to_vec()
}
//Layouts that can be flipped vertically
Self::MainAndHorizontalStack => [(false, false), (false, true)].to_vec(),
//Layouts that can be flipped horizontally
_ => [(false, false), (true, false)].to_vec(),
}
}
}
#[derive(Debug, Error)]
#[error("Could not parse layout: {0}")]
pub struct ParseLayoutError(String);
impl FromStr for Layout {
type Err = ParseLayoutError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"MainAndVertStack" => Ok(Self::MainAndVertStack),
"MainAndHorizontalStack" => Ok(Self::MainAndHorizontalStack),
"MainAndDeck" => Ok(Self::MainAndDeck),
"GridHorizontal" => Ok(Self::GridHorizontal),
"EvenHorizontal" => Ok(Self::EvenHorizontal),
"EvenVertical" => Ok(Self::EvenVertical),
"Fibonacci" => Ok(Self::Fibonacci),
"LeftMain" => Ok(Self::LeftMain),
"CenterMain" => Ok(Self::CenterMain),
"CenterMainBalanced" => Ok(Self::CenterMainBalanced),
"CenterMainFluid" => Ok(Self::CenterMainFluid),
"Monocle" => Ok(Self::Monocle),
"RightWiderLeftStack" => Ok(Self::RightWiderLeftStack),
"LeftWiderRightStack" => Ok(Self::LeftWiderRightStack),
_ => Err(ParseLayoutError(s.to_string())),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::models::{BBox, Margins, WindowHandle};
#[test]
fn should_fullscreen_a_single_window() {
//size defaults to 600x800
let mut ws = Workspace::new(
None,
BBox {
width: 0,
height: 0,
x: 0,
y: 0,
},
Layout::default(),
None,
);
ws.margin = Margins::new(0);
ws.xyhw.set_minh(600);
ws.xyhw.set_minw(800);
ws.update_avoided_areas();
let mut w = Window::new(WindowHandle::MockHandle(1), None, None);
w.border = 0;
w.margin = Margins::new(0);
let mut windows = vec![&mut w];
even_horizontal::update(&ws, &mut windows);
assert!(
w.height() == 600,
"window was not size to the correct height"
);
assert!(w.width() == 800, "window was not size to the correct width");
}
#[test]
fn test_from_str() {
let layout_strs: [&str; 14] = [
"MainAndVertStack",
"MainAndHorizontalStack",
"MainAndDeck",
"GridHorizontal",
"EvenHorizontal",
"EvenVertical",
"Fibonacci",
"LeftMain",
"CenterMain",
"CenterMainBalanced",
"CenterMainFluid",
"Monocle",
"RightWiderLeftStack",
"LeftWiderRightStack",
];
assert_eq!(layout_strs.len(), LAYOUTS.len());
for (i, layout) in LAYOUTS.iter().enumerate() {
assert_eq!(
layout,
&Layout::from_str(layout_strs[i]).expect("Layout String")
);
}
}
}