-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlayout-stacking.lua
More file actions
272 lines (215 loc) · 8.03 KB
/
Copy pathlayout-stacking.lua
File metadata and controls
272 lines (215 loc) · 8.03 KB
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
-- plugins/layout-stacking.lua
-- Traditional stacking window layout plugin
-- Windows are freely positioned and can overlap, with focus-based z-ordering
local wl = require("shared.wayland.server")
local ffi = wl._ffi
local bit = wl.bit
--------------------------------------------------------------------------------
-- Stacking Layout Implementation
--------------------------------------------------------------------------------
local stacking = {}
local function has_scene(surface)
return surface and surface.scene_node and surface:scene_node() ~= nil
end
local function surface_position(surface)
return surface:position()
end
local function surface_geometry(surface)
return surface:geometry()
end
function stacking:init(monitor_state)
monitor_state.stacking = {
grabbed_surface = nil,
grab_x = 0,
grab_y = 0,
grab_geobox = nil,
resize_edges = 0,
mode = "none", -- "none", "move", "resize"
}
end
function stacking:shutdown(monitor_state)
monitor_state.stacking = nil
end
--------------------------------------------------------------------------------
-- Window Lifecycle
--------------------------------------------------------------------------------
function stacking:on_window_add(monitor_state, surface)
end
function stacking:on_window_remove(monitor_state, surface)
if not monitor_state.stacking then
return
end
if monitor_state.stacking.grabbed_surface == surface then
monitor_state.stacking.grabbed_surface = nil
monitor_state.stacking.mode = "none"
end
end
function stacking:on_window_resize(monitor_state, surface, width, height)
-- No special handling needed
end
function stacking:position_window(monitor_state, surface)
-- Stacking doesn't enforce positions - windows stay where placed
end
--------------------------------------------------------------------------------
-- Hit Testing
--------------------------------------------------------------------------------
function stacking:get_window_at(monitor_state, x, y)
local surface_service = require("compositor.services.surface")
local surface, _, _, _ = surface_service:surface_at(x, y)
return surface
end
--------------------------------------------------------------------------------
-- Interactive Move
--------------------------------------------------------------------------------
function stacking:begin_move(monitor_state, surface, cursor_x, cursor_y)
if not has_scene(surface) then
return false
end
local st = monitor_state.stacking
st.grabbed_surface = surface
st.mode = "move"
local x, y = surface_position(surface)
st.grab_x = cursor_x - x
st.grab_y = cursor_y - y
return true
end
function stacking:update_move(monitor_state, cursor_x, cursor_y)
local st = monitor_state.stacking
if st.mode ~= "move" or not st.grabbed_surface then
return
end
local surface = st.grabbed_surface
if not has_scene(surface) then return end
local new_x = cursor_x - st.grab_x
local new_y = cursor_y - st.grab_y
surface:set_position(new_x, new_y)
end
function stacking:end_move(monitor_state)
local st = monitor_state.stacking
st.grabbed_surface = nil
st.mode = "none"
end
--------------------------------------------------------------------------------
-- Interactive Resize
--------------------------------------------------------------------------------
function stacking:begin_resize(monitor_state, surface, cursor_x, cursor_y, edges)
if not has_scene(surface) then
return false
end
local st = monitor_state.stacking
st.grabbed_surface = surface
st.mode = "resize"
st.resize_edges = edges
local geo_box = surface_geometry(surface)
local x, y = surface_position(surface)
local border_x = x + geo_box.x
if bit.band(edges, wl.WLR_EDGE_RIGHT) ~= 0 then
border_x = border_x + geo_box.width
end
local border_y = y + geo_box.y
if bit.band(edges, wl.WLR_EDGE_BOTTOM) ~= 0 then
border_y = border_y + geo_box.height
end
st.grab_x = cursor_x - border_x
st.grab_y = cursor_y - border_y
st.grab_geobox = ffi.new("struct wlr_box")
st.grab_geobox.x = geo_box.x + x
st.grab_geobox.y = geo_box.y + y
st.grab_geobox.width = geo_box.width
st.grab_geobox.height = geo_box.height
return true
end
function stacking:update_resize(monitor_state, cursor_x, cursor_y)
local st = monitor_state.stacking
if st.mode ~= "resize" or not st.grabbed_surface then
return
end
local surface = st.grabbed_surface
if not has_scene(surface) then return end
local border_x = cursor_x - st.grab_x
local border_y = cursor_y - st.grab_y
local new_left = st.grab_geobox.x
local new_right = st.grab_geobox.x + st.grab_geobox.width
local new_top = st.grab_geobox.y
local new_bottom = st.grab_geobox.y + st.grab_geobox.height
if bit.band(st.resize_edges, wl.WLR_EDGE_TOP) ~= 0 then
new_top = border_y
if new_top >= new_bottom then new_top = new_bottom - 1 end
elseif bit.band(st.resize_edges, wl.WLR_EDGE_BOTTOM) ~= 0 then
new_bottom = border_y
if new_bottom <= new_top then new_bottom = new_top + 1 end
end
if bit.band(st.resize_edges, wl.WLR_EDGE_LEFT) ~= 0 then
new_left = border_x
if new_left >= new_right then new_left = new_right - 1 end
elseif bit.band(st.resize_edges, wl.WLR_EDGE_RIGHT) ~= 0 then
new_right = border_x
if new_right <= new_left then new_right = new_left + 1 end
end
local geo_box = surface_geometry(surface)
surface:set_position(new_left - geo_box.x, new_top - geo_box.y)
local new_width = new_right - new_left
local new_height = new_bottom - new_top
surface:set_size(new_width, new_height)
end
function stacking:end_resize(monitor_state)
local st = monitor_state.stacking
st.grabbed_surface = nil
st.grab_geobox = nil
st.mode = "none"
end
-- Fullscreen
--------------------------------------------------------------------------------
function stacking:enter_fullscreen(monitor_state, surface)
if not has_scene(surface) then
return false
end
if monitor_state.fullscreen_surface and monitor_state.fullscreen_surface ~= surface then
self:exit_fullscreen(monitor_state)
end
local surface_service = require("compositor.services.surface")
local current_width, current_height = surface_service:get_surface_dimensions(surface)
surface._pre_fullscreen = {
x = (select(1, surface_position(surface))),
y = (select(2, surface_position(surface))),
width = current_width,
height = current_height,
}
local output_service = require("compositor.services.output")
local width, height = output_service:get_output_dimensions(monitor_state.output)
surface:set_position(0, 0)
surface:set_size(width, height)
monitor_state.fullscreen_surface = surface
return true
end
function stacking:exit_fullscreen(monitor_state)
local surface = monitor_state.fullscreen_surface
if not surface then
return false
end
if surface._pre_fullscreen and has_scene(surface) then
surface:set_position(surface._pre_fullscreen.x, surface._pre_fullscreen.y)
if surface._pre_fullscreen.width > 0 and surface._pre_fullscreen.height > 0 then
surface:set_size(surface._pre_fullscreen.width, surface._pre_fullscreen.height)
end
end
surface._pre_fullscreen = nil
monitor_state.fullscreen_surface = nil
return true
end
--------------------------------------------------------------------------------
-- Plugin Wrapper
--------------------------------------------------------------------------------
local plugin = {
name = "layout-stacking",
}
function plugin:mount(config)
local layout_service = require("compositor.services.layout")
layout_service:register("stacking", stacking)
return { layout = stacking }
end
function plugin:unmount()
local layout_service = require("compositor.services.layout")
layout_service:unregister("stacking")
end
return plugin