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
203
204
205
206
|
use super::*;
use super::support::{parse_direction, WindowAction};
impl Engine {
// ---- srd.window.* ------------------------------------------------------
pub(super) fn fn_window_focused(&self) -> Result<mlua::Function<'_>> {
let state = self.state.clone();
Ok(self.lua.create_function(move |lua, ()| {
let wm = state.borrow().wm.clone();
let wm = wm.borrow();
let Some(w) = wm.focused_window() else { return Ok(Value::Nil) };
let t = lua.create_table()?;
t.set("id", w.id)?;
t.set("title", w.title.clone())?;
t.set("x", w.geometry.x)?;
t.set("y", w.geometry.y)?;
t.set("width", w.geometry.width)?;
t.set("height", w.geometry.height)?;
t.set("floating", w.floating)?;
t.set("maximized", w.maximized)?;
t.set("minimized", w.minimized)?;
t.set("scratchpad", w.scratchpad)?;
Ok(Value::Table(t))
})?)
}
pub(super) fn fn_window_action(&self, action: WindowAction) -> Result<mlua::Function<'_>> {
let state = self.state.clone();
Ok(self.lua.create_function(move |_, ()| {
let wm = state.borrow().wm.clone();
let mut wm = wm.borrow_mut();
if let Some(id) = wm.focused_id() {
match action {
WindowAction::Close => wm.close_window(id),
WindowAction::Minimize => wm.minimize_window(id),
WindowAction::Maximize => wm.toggle_maximize(id),
WindowAction::Fullscreen => wm.toggle_fullscreen(id),
WindowAction::ToggleFloating => wm.toggle_floating(id),
WindowAction::TogglePin => wm.toggle_always_on_top(id),
WindowAction::ScratchpadAdd => wm.scratchpad_add(id),
}
}
Ok(())
})?)
}
pub(super) fn fn_scratchpad_show(&self) -> Result<mlua::Function<'_>> {
let state = self.state.clone();
Ok(self.lua.create_function(move |_, ()| {
let wm = state.borrow().wm.clone();
wm.borrow_mut().scratchpad_show();
Ok(())
})?)
}
/// `srd.window.move("left")` - swap the focused window with its
/// neighbour in that direction (Hyprland's `movewindow l/r/u/d`).
pub(super) fn fn_window_move_direction(&self) -> Result<mlua::Function<'_>> {
let state = self.state.clone();
Ok(self.lua.create_function(move |_, direction: String| {
let dir = parse_direction(&direction, "srd.window.move")?;
let wm = state.borrow().wm.clone();
wm.borrow_mut().move_window_direction(dir);
Ok(())
})?)
}
/// `srd.window.next()` / `srd.window.prev()` - cycle focus through the
/// windows on the current workspace (Hyprland's `cyclenext`).
pub(super) fn fn_window_cycle(&self, forward: bool) -> Result<mlua::Function<'_>> {
let state = self.state.clone();
Ok(self.lua.create_function(move |_, ()| {
let wm = state.borrow().wm.clone();
let mut wm = wm.borrow_mut();
if forward {
wm.focus_next();
} else {
wm.focus_previous();
}
// Bring it to the top, matching the `bringactivetotop` the
// Hyprland binding pairs with `cyclenext`.
if let Some(id) = wm.focused_id() {
wm.raise_window(id);
}
Ok(())
})?)
}
pub(super) fn fn_window_focus_direction(&self) -> Result<mlua::Function<'_>> {
let state = self.state.clone();
Ok(self.lua.create_function(move |_, direction: String| {
let dir = parse_direction(&direction, "srd.window.focus")?;
let wm = state.borrow().wm.clone();
wm.borrow_mut().focus_direction(dir);
Ok(())
})?)
}
pub(super) fn fn_window_set_decorations(&self) -> Result<mlua::Function<'_>> {
let state = self.state.clone();
Ok(self.lua.create_function(move |_, enabled: bool| {
let wm = state.borrow().wm.clone();
let mut wm = wm.borrow_mut();
if let Some(id) = wm.focused_id() {
if let Some(w) = wm.window_mut(id) {
w.decorated = enabled;
}
}
Ok(())
})?)
}
pub(super) fn fn_window_set_border_color(&self) -> Result<mlua::Function<'_>> {
let state = self.state.clone();
Ok(self.lua.create_function(move |_, (r, g, b): (u8, u8, u8)| {
let wm = state.borrow().wm.clone();
let mut wm = wm.borrow_mut();
if let Some(id) = wm.focused_id() {
if let Some(w) = wm.window_mut(id) {
w.border_color = (r, g, b);
}
}
Ok(())
})?)
}
pub(super) fn fn_window_set_border_width(&self) -> Result<mlua::Function<'_>> {
let state = self.state.clone();
Ok(self.lua.create_function(move |_, width: u32| {
let wm = state.borrow().wm.clone();
let mut wm = wm.borrow_mut();
if let Some(id) = wm.focused_id() {
if let Some(w) = wm.window_mut(id) {
w.border_width = width;
}
}
Ok(())
})?)
}
pub(super) fn fn_window_set_corner_radius(&self) -> Result<mlua::Function<'_>> {
let state = self.state.clone();
Ok(self.lua.create_function(move |_, radius: u32| {
let wm = state.borrow().wm.clone();
let mut wm = wm.borrow_mut();
if let Some(id) = wm.focused_id() {
if let Some(w) = wm.window_mut(id) {
w.corner_radius = radius;
}
}
Ok(())
})?)
}
pub(super) fn fn_window_set_opacity(&self) -> Result<mlua::Function<'_>> {
let state = self.state.clone();
Ok(self.lua.create_function(move |_, opacity: f32| {
let wm = state.borrow().wm.clone();
let mut wm = wm.borrow_mut();
if let Some(id) = wm.focused_id() {
if let Some(w) = wm.window_mut(id) {
w.opacity = opacity.clamp(0.0, 1.0);
}
}
Ok(())
})?)
}
pub(super) fn fn_window_set_resize_margin(&self) -> Result<mlua::Function<'_>> {
let state = self.state.clone();
Ok(self.lua.create_function(move |_, margin: i32| {
let wm = state.borrow().wm.clone();
let mut wm = wm.borrow_mut();
if let Some(id) = wm.focused_id() {
if let Some(w) = wm.window_mut(id) {
w.resize_margin = Some(margin);
}
}
Ok(())
})?)
}
pub(super) fn fn_window_set_floating(&self) -> Result<mlua::Function<'_>> {
let state = self.state.clone();
Ok(self.lua.create_function(move |_, floating: bool| {
let wm = state.borrow().wm.clone();
let mut wm = wm.borrow_mut();
if let Some(id) = wm.focused_id() {
if let Some(w) = wm.window_mut(id) {
w.floating = floating;
}
}
Ok(())
})?)
}
pub(super) fn fn_window_is_floating(&self) -> Result<mlua::Function<'_>> {
let state = self.state.clone();
Ok(self.lua.create_function(move |_, ()| {
let wm = state.borrow().wm.clone();
let wm = wm.borrow();
Ok(wm.focused_id().map(|id| wm.is_floating(id)).unwrap_or(false))
})?)
}
}
|