Repository navigation
Expand file tree
/
Copy pathlayout-stacking.lua
More file actions
288 lines (260 loc) · 11.1 KB
/
Copy pathlayout-stacking.lua
File metadata and controls
288 lines (260 loc) · 11.1 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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
-- plugins/layout-stacking.lua
-- Floating windows with optional server-side title bars.
--
-- local stacking = plugin("@boring/layout-stacking"):mount({
-- outputs = { "DP-1" }, -- default: every output
-- theme = {
-- height = 24, -- title bar height
-- font = "Sans Bold 10",
-- title = "#0a0a0a", -- title text
-- decorations = "#f59e0b", -- title bar
-- button_background = "#f59e0b",
-- button_foreground = "#0a0a0a",
-- },
-- controls = { -- left to right
-- { type = "title" },
-- { type = "spacer" },
-- { type = "button", icon = "minimize" },
-- { type = "button", icon = "maximize" },
-- { type = "button", icon = "close" },
-- -- or your own: { type = "button", icon = "★", on_click = function(w) ... end },
-- },
-- })
-- ["Mod4+m"] = stacking.actions.maximize, -- toggle, focused window
-- ["Mod4+n"] = stacking.actions.minimize,
--
-- Theme colours can be animations (library/animate) for the bar and button
-- backgrounds; text colours use their first frame.
--
-- Without `controls` (or with `decorations = false`) windows get no title
-- bar. New windows open centred; dragging the title bar moves a window and
-- double-clicking it maximizes. Maximized windows fill the usable area.
--
-- Window properties (w.props, e.g. from the rules plugin) it follows:
-- size = { w, h }, position = { x, y } (in the output), maximized = true.
local color = require("shared.color")
local spaces = require("@boring/library/spaces")
local text = require("@boring/library/text")
local animate = require("@boring/library/animate")
local scene = require("@boring/library/scene")
local plugin = { name = "layout-stacking" }
local ICONS = { close = "×", maximize = "□", minimize = "–" }
local BUTTON_LEFT = 0x110
local function make_stacking(config)
local theme = config.theme or {}
local controls = config.controls
local decorated = controls ~= nil and config.decorations ~= false
local height = theme.height or 24
local font = theme.font or "Sans Bold 10"
local function still(v) return animate.is(v) and animate.sample(v, 0) or v end
local bar_color = theme.decorations or "#333333"
local title_color = color.parse(still(theme.title), { 1, 1, 1, 1 })
local button_bg = theme.button_background or bar_color
local button_fg = color.parse(still(theme.button_foreground), title_color)
local stacking = {}
local function managed(w)
local space = spaces.of(w)
return space ~= nil and space.impl == stacking
end
-- Maximize --------------------------------------------------------------
function stacking.maximize(w, on)
if not managed(w) or w.fullscreen or w.maximized == (on and true or false) then return end
local d = w.data
if on then
local x, y = w:position()
local width, h = w:size()
d.stacking_restore = { x = x, y = y, width = width, height = h }
local area, g = spaces.of(w).output.usable, w:geometry()
local top = decorated and height or 0
w:set_maximized(true)
w:set_position(area.x - g.x, area.y + top - g.y)
w:set_size(area.width, area.height - top)
else
local r = d.stacking_restore
d.stacking_restore = nil
w:set_maximized(false)
if r then
w:set_position(r.x, r.y)
w:set_size(r.width, r.height)
end
end
end
local DEFAULT_CLICK = {
close = function(w) w:close() end,
maximize = function(w) stacking.maximize(w, not w.maximized) end,
minimize = function(w) spaces.minimize(w, true) end,
}
-- Title bars ------------------------------------------------------------
local function destroy_decorations(w)
local d = w.data.stacking
if not d then return end
for _, off in ipairs(d.handlers) do off() end
for _, b in ipairs(d.bindings) do animate.stop(b) end
d.tree:destroy()
w.data.stacking = nil
end
local function build_decorations(w)
destroy_decorations(w)
if not decorated then return end
local g = w:geometry()
if g.width <= 0 then return end
local tree = scene.tree(scene.window(w))
tree:set_position(g.x, g.y - height)
local d = { tree = tree, width = g.width, x = g.x, y = g.y, title = w:title(), handlers = {}, bindings = {} }
w.data.stacking = d
-- A rect painted with a (possibly animated) colour.
local function rect(width, h, value)
local r = scene.rect(tree, width, h, { 0, 0, 0, 0 })
d.bindings[#d.bindings + 1] = animate.bind(nil, value, function(c)
r:set_color(color.parse(c, { 0, 0, 0, 1 }))
end)
return r
end
local bar = rect(g.width, height, bar_color)
local last_click = 0
d.handlers[#d.handlers + 1] = scene.on_click(bar, function(win, button)
if button ~= BUTTON_LEFT then return end
local now = boring.now()
if now - last_click < 300 then
last_click = 0
stacking.maximize(win, not win.maximized)
return
end
last_click = now
if not win.maximized then boring.begin_move(win) end
end)
-- Lay controls out left to right; a spacer takes the remaining width.
local fixed, spacers = 0, 0
for _, c in ipairs(controls) do
if c.type == "button" then fixed = fixed + height
elseif c.type == "spacer" then spacers = spacers + 1 end
end
local x = 0
for _, c in ipairs(controls) do
if c.type == "title" then
local node, _, th = text.draw(tree, d.title, {
font = font, color = title_color, max_width = math.max(0, g.width - fixed - 16),
})
if node then node:set_position(x + 8, math.floor((height - th) / 2)) end
elseif c.type == "spacer" then
x = x + math.max(0, (g.width - fixed) / math.max(spacers, 1))
elseif c.type == "button" then
local bx = math.floor(x)
local button = rect(height, height, button_bg)
button:set_position(bx, 0)
local node, iw, ih = text.draw(tree, ICONS[c.icon] or c.icon or "", { font = font, color = button_fg })
if node then
node:set_position(math.floor(bx + (height - iw) / 2), math.floor((height - ih) / 2))
end
local on_click = c.on_click or DEFAULT_CLICK[c.icon]
d.handlers[#d.handlers + 1] = scene.on_click(button, function(win)
if on_click then on_click(win) end
end)
x = x + height
end
end
end
function stacking.update_decorations(w)
if not decorated or not managed(w) then return end
local d = w.data.stacking
if w.fullscreen then
if d then d.tree:set_enabled(false) end
return
end
local g = w:geometry()
if not d or d.width ~= g.width or d.title ~= w:title() then
build_decorations(w)
else
d.tree:set_enabled(true)
if d.x ~= g.x or d.y ~= g.y then
d.x, d.y = g.x, g.y
d.tree:set_position(g.x, g.y - height)
end
end
end
--- X11 windows asking for a place and size get it.
function stacking.request_geometry(w, box)
if not managed(w) or w.fullscreen or w.maximized then return end
w:set_position(box.x, box.y)
w:set_size(box.width, box.height)
end
-- Arrangement -----------------------------------------------------------
function stacking:add(space, w)
build_decorations(w)
-- Windows keep their place when the layout is re-registered (reload).
if w.data.stacking_placed then return end
w.data.stacking_placed = true
local props = w.props
local g = w:geometry()
local width, h = g.width, g.height
if type(props.size) == "table" then
width, h = props.size[1] or width, props.size[2] or h
w:set_size(width, h)
end
local area = space.output.usable
local top = decorated and height or 0
if type(props.position) == "table" then
local box = space.output:box()
w:set_position(box.x + (props.position[1] or 0) - g.x, box.y + (props.position[2] or 0) - g.y)
elseif not w.impl.positioned then
-- Centre in the usable area, title bar included.
local x = area.x + math.max(0, (area.width - width) / 2) - g.x
local y = area.y + top + math.max(0, (area.height - h - top) / 2) - g.y
w:set_position(x, y)
end
if props.maximized then stacking.maximize(w, true) end
end
function stacking:remove(_, w)
destroy_decorations(w)
end
-- Maximized windows follow the usable area (a panel appeared).
function stacking:arrange(space)
for _, w in ipairs(spaces.arranged(space)) do
if w.maximized then
local area, g = space.output.usable, w:geometry()
local top = decorated and height or 0
w:set_position(area.x - g.x, area.y + top - g.y)
w:set_size(area.width, area.height - top)
end
end
end
function stacking:shutdown(space)
for _, w in ipairs(space.windows) do
destroy_decorations(w)
if w.maximized then stacking.maximize(w, false) end
end
end
-- No move/resize here: windows follow the pointer (spaces' default),
-- except maximized ones, which stay put.
function stacking:move(_, w)
if not w.maximized then boring.begin_move(w) end
end
function stacking:resize(_, w, edges)
if not w.maximized then boring.begin_resize(w, edges) end
end
return stacking
end
function plugin:mount(config, ctx)
local stacking = make_stacking(config)
local scope = ctx.scope
scope:subscribe(boring.events, "window:commit", stacking.update_decorations)
scope:subscribe(boring.events, "window:title", stacking.update_decorations)
scope:subscribe(boring.events, "window:fullscreen", stacking.update_decorations)
scope:subscribe(boring.events, "window:request_geometry", stacking.request_geometry)
scope:subscribe(boring.events, "window:request_maximize", stacking.maximize)
scope:defer(spaces.register("stacking", stacking, { outputs = config.outputs }))
local function focused(fn)
return function()
local w = boring.focused()
if w then fn(w) end
end
end
return {
layout = stacking,
actions = {
maximize = focused(function(w) stacking.maximize(w, not w.maximized) end),
minimize = focused(function(w) spaces.minimize(w, true) end),
},
}
end
return plugin