1package core
2
3import (
4 "context"
5 "slices"
6
7 "github.com/bnema/neferwl/internal/ports"
8)
9
10// popupState is a placed xdg_popup. Rect is relative to the parent's
11// window geometry, as sent to the client.
12type popupState struct {
13 id, parent WindowID
14 rect Rect
15 grab bool
16 mapped bool
17}
18
19// place computes a popup rectangle relative to its parent, kept inside
20// bounds (also parent-relative) as the positioner's adjustments allow.
21func place(p ports.Positioner, bounds Rect) Rect {
22 r := initial(p, p.Anchor, p.Gravity)
23 if bounds.W <= 0 || bounds.H <= 0 {
24 return r
25 }
26 outX := func(r Rect) bool { return r.X < bounds.X || r.X+r.W > bounds.X+bounds.W }
27 outY := func(r Rect) bool { return r.Y < bounds.Y || r.Y+r.H > bounds.Y+bounds.H }
28 if outX(r) && p.Adjust&ports.AdjustFlipX != 0 {
29 if f := initial(p, flipX(p.Anchor), flipX(p.Gravity)); !outX(f) {
30 r.X = f.X
31 }
32 }
33 if outY(r) && p.Adjust&ports.AdjustFlipY != 0 {
34 if f := initial(p, flipY(p.Anchor), flipY(p.Gravity)); !outY(f) {
35 r.Y = f.Y
36 }
37 }
38 if outX(r) && p.Adjust&ports.AdjustSlideX != 0 {
39 r.X = max(min(r.X, bounds.X+bounds.W-r.W), bounds.X)
40 }
41 if outY(r) && p.Adjust&ports.AdjustSlideY != 0 {
42 r.Y = max(min(r.Y, bounds.Y+bounds.H-r.H), bounds.Y)
43 }
44 if outX(r) && p.Adjust&ports.AdjustResizeX != 0 {
45 x0, x1 := max(r.X, bounds.X), min(r.X+r.W, bounds.X+bounds.W)
46 if x1 > x0 {
47 r.X, r.W = x0, x1-x0
48 }
49 }
50 if outY(r) && p.Adjust&ports.AdjustResizeY != 0 {
51 y0, y1 := max(r.Y, bounds.Y), min(r.Y+r.H, bounds.Y+bounds.H)
52 if y1 > y0 {
53 r.Y, r.H = y0, y1-y0
54 }
55 }
56 return r
57}
58
59// initial is the unconstrained placement for an anchor and gravity.
60func initial(p ports.Positioner, anchor, gravity uint32) Rect {
61 a := p.AnchorRect
62 x, y := a.X+a.W/2, a.Y+a.H/2
63 switch anchor {
64 case ports.EdgeLeft, ports.EdgeTopLeft, ports.EdgeBottomLeft:
65 x = a.X
66 case ports.EdgeRight, ports.EdgeTopRight, ports.EdgeBottomRight:
67 x = a.X + a.W
68 }
69 switch anchor {
70 case ports.EdgeTop, ports.EdgeTopLeft, ports.EdgeTopRight:
71 y = a.Y
72 case ports.EdgeBottom, ports.EdgeBottomLeft, ports.EdgeBottomRight:
73 y = a.Y + a.H
74 }
75 x, y = x+p.OffsetX, y+p.OffsetY
76 r := Rect{X: x - p.Width/2, Y: y - p.Height/2, W: p.Width, H: p.Height}
77 switch gravity {
78 case ports.EdgeLeft, ports.EdgeTopLeft, ports.EdgeBottomLeft:
79 r.X = x - p.Width
80 case ports.EdgeRight, ports.EdgeTopRight, ports.EdgeBottomRight:
81 r.X = x
82 }
83 switch gravity {
84 case ports.EdgeTop, ports.EdgeTopLeft, ports.EdgeTopRight:
85 r.Y = y - p.Height
86 case ports.EdgeBottom, ports.EdgeBottomLeft, ports.EdgeBottomRight:
87 r.Y = y
88 }
89 return r
90}
91
92func flipX(e uint32) uint32 {
93 switch e {
94 case ports.EdgeLeft:
95 return ports.EdgeRight
96 case ports.EdgeRight:
97 return ports.EdgeLeft
98 case ports.EdgeTopLeft:
99 return ports.EdgeTopRight
100 case ports.EdgeTopRight:
101 return ports.EdgeTopLeft
102 case ports.EdgeBottomLeft:
103 return ports.EdgeBottomRight
104 case ports.EdgeBottomRight:
105 return ports.EdgeBottomLeft
106 }
107 return e
108}
109
110func flipY(e uint32) uint32 {
111 switch e {
112 case ports.EdgeTop:
113 return ports.EdgeBottom
114 case ports.EdgeBottom:
115 return ports.EdgeTop
116 case ports.EdgeTopLeft:
117 return ports.EdgeBottomLeft
118 case ports.EdgeBottomLeft:
119 return ports.EdgeTopLeft
120 case ports.EdgeTopRight:
121 return ports.EdgeBottomRight
122 case ports.EdgeBottomRight:
123 return ports.EdgeTopRight
124 }
125 return e
126}
127
128// windowRect is where a window, layer surface or popup is on its screen: its client
129// area, output-local and logical. ok is false when it is not on screen.
130func (c *Core) windowRect(id WindowID) (*screen, Rect, bool) {
131 if p := c.popups[id]; p != nil {
132 sc, pr, ok := c.windowRect(p.parent)
133 if !ok {
134 return nil, Rect{}, false
135 }
136 return sc, Rect{X: pr.X + p.rect.X, Y: pr.Y + p.rect.Y, W: p.rect.W, H: p.rect.H}, true
137 }
138 if sc, l, ok := c.layerOf(id); ok {
139 if !shown(sc, l.Layer) {
140 return nil, Rect{}, false
141 }
142 return sc, l.Rect, true
143 }
144 for _, sc := range c.screens {
145 for _, pl := range sc.mon.Layout() {
146 if pl.ID == id {
147 if pl.Hidden {
148 return nil, Rect{}, false
149 }
150 return sc, c.clientRect(pl), true
151 }
152 }
153 }
154 return nil, Rect{}, false
155}
156
157// placePopup places a new or repositioned popup and configures it; a popup
158// whose parent is not on screen is dismissed.
159func (c *Core) placePopup(ctx context.Context, v ports.PopupRequest) error {
160 sc, pr, ok := c.windowRect(v.Parent)
161 if !ok {
162 if c.popups[v.ID] != nil {
163 return c.closePopup(ctx, v.ID)
164 }
165 return c.command(ctx, ports.ClosePopup{ID: v.ID})
166 }
167 o := sc.mon.Output()
168 bounds := Rect{X: o.X - pr.X, Y: o.Y - pr.Y, W: o.W, H: o.H}
169 p := c.popups[v.ID]
170 if p == nil {
171 p = &popupState{id: v.ID, parent: v.Parent}
172 c.popups[v.ID] = p
173 c.popupOrder = append(c.popupOrder, v.ID)
174 }
175 p.grab = v.Grab
176 p.rect = place(v.Positioner, bounds)
177 return c.command(ctx, ports.ConfigurePopup{ID: v.ID, Rect: p.rect, Reposition: v.Reposition, Token: v.Token})
178}
179
180// closePopup dismisses a popup after its children, topmost first as
181// xdg-shell requires, and forgets them.
182func (c *Core) closePopup(ctx context.Context, id WindowID) error {
183 if err := c.closePopupsOf(ctx, id); err != nil {
184 return err
185 }
186 if err := c.command(ctx, ports.ClosePopup{ID: id}); err != nil {
187 return err
188 }
189 c.forgetPopup(id)
190 return nil
191}
192
193// closePopupsOf dismisses the popups hanging from a window or popup,
194// newest first.
195func (c *Core) closePopupsOf(ctx context.Context, id WindowID) error {
196 for _, child := range slices.Backward(slices.Clone(c.popupOrder)) {
197 if p := c.popups[child]; p != nil && p.parent == id {
198 if err := c.closePopup(ctx, child); err != nil {
199 return err
200 }
201 }
202 }
203 return nil
204}
205
206// dropPopup forgets a popup the client unmapped and dismisses its children.
207func (c *Core) dropPopup(ctx context.Context, id WindowID) error {
208 if c.popups[id] == nil {
209 return nil
210 }
211 if err := c.closePopupsOf(ctx, id); err != nil {
212 return err
213 }
214 c.forgetPopup(id)
215 return nil
216}
217
218func (c *Core) forgetPopup(id WindowID) {
219 delete(c.popups, id)
220 c.popupOrder = slices.DeleteFunc(c.popupOrder, func(v WindowID) bool { return v == id })
221 if c.pointer == id {
222 c.pointer = 0
223 }
224}
225
226// closeHiddenPopups dismisses popups whose window left the screen.
227func (c *Core) closeHiddenPopups(ctx context.Context) error {
228 for _, id := range slices.Backward(slices.Clone(c.popupOrder)) {
229 if c.popups[id] == nil {
230 continue
231 }
232 if _, _, ok := c.windowRect(id); !ok {
233 if err := c.closePopup(ctx, id); err != nil {
234 return err
235 }
236 }
237 }
238 return nil
239}
240
241// dismissGrabs closes the grabbing popups unless the click hit one of
242// them: a click outside a menu closes it.
243func (c *Core) dismissGrabs(ctx context.Context, hit WindowID) error {
244 for _, id := range slices.Backward(slices.Clone(c.popupOrder)) {
245 p := c.popups[id]
246 if p == nil || !p.grab {
247 continue
248 }
249 // A click on a popup of the same chain keeps it; a click on
250 // anything else, its parent window included, closes it.
251 if c.popups[hit] != nil && (hit == id || c.isAncestor(id, hit) || c.isAncestor(hit, id)) {
252 continue
253 }
254 if err := c.closePopup(ctx, id); err != nil {
255 return err
256 }
257 }
258 return nil
259}
260
261// isAncestor reports whether a is a popup ancestor of b.
262func (c *Core) isAncestor(a, b WindowID) bool {
263 for p := c.popups[b]; p != nil; p = c.popups[p.parent] {
264 if p.parent == a {
265 return true
266 }
267 }
268 return false
269}
270
271// grabFocus is the topmost mapped grabbing popup, which takes the keyboard.
272func (c *Core) grabFocus() WindowID {
273 for i := len(c.popupOrder) - 1; i >= 0; i-- {
274 if p := c.popups[c.popupOrder[i]]; p != nil && p.grab && p.mapped {
275 return p.id
276 }
277 }
278 return 0
279}
280
281// popupAt returns the topmost mapped popup under the output-local point,
282// among layer popups (overLayers) or window popups.
283func (c *Core) popupAt(sc *screen, lx, ly float64, overLayers bool) (WindowID, float64, float64) {
284 for i := len(c.popupOrder) - 1; i >= 0; i-- {
285 p := c.popups[c.popupOrder[i]]
286 if p == nil || !p.mapped || c.onLayer(c.popupRoot(p.id)) != overLayers {
287 continue
288 }
289 s, r, ok := c.windowRect(p.id)
290 if !ok || s != sc {
291 continue
292 }
293 if lx >= float64(r.X) && lx < float64(r.X+r.W) && ly >= float64(r.Y) && ly < float64(r.Y+r.H) {
294 return p.id, lx - float64(r.X), ly - float64(r.Y)
295 }
296 }
297 return 0, 0, 0
298}
299
300// popupRoot is the window or layer a popup chain hangs from; id itself
301// when it is not a popup.
302func (c *Core) popupRoot(id WindowID) WindowID {
303 for p := c.popups[id]; p != nil; p = c.popups[id] {
304 id = p.parent
305 }
306 return id
307}
308
309// onLayer reports whether id is a mapped layer surface.
310func (c *Core) onLayer(id WindowID) bool {
311 for _, sc := range c.screens {
312 for _, l := range sc.layers {
313 if l.ID == id {
314 return true
315 }
316 }
317 }
318 return false
319}
320
321// scenePopups are the mapped popups on a screen, bottom to top.
322func (c *Core) scenePopups(sc *screen) []ports.SceneWindow {
323 var out []ports.SceneWindow
324 for _, id := range c.popupOrder {
325 p := c.popups[id]
326 if p == nil || !p.mapped {
327 continue
328 }
329 if s, r, ok := c.windowRect(id); ok && s == sc {
330 out = append(out, ports.SceneWindow{ID: id, Rect: r, Popup: true, OverLayers: c.onLayer(c.popupRoot(id))})
331 }
332 }
333 return out
334}