1package core
2
3import (
4 "math"
5 "slices"
6 "time"
7
8 "github.com/bnema/neferwl/internal/ports"
9)
10
11// Transitions start after a user action (a bind, a click that focuses, a
12// discrete swipe step): a snapshot of what each screen shows is taken before
13// the action and diffed after it. The state the action leaves is the
14// settled one; the transition only adds presentation springs over it
15// (Workspace.view, stashView, Monitor.switchView), starting from what was on screen.
16// Nothing here runs for events that come from clients or outputs.
17//
18// A window's rect transition (presentation-only rect transition) is presentation too: configures keep
19// using the settled rect, so the client resizes once to its final size; what
20// is drawn and hit-tested is the settled rect plus an offset that a spring
21// takes to zero. The offsets are relative to the camera-shifted layout, so a
22// window the view scrolls moves with the camera spring only.
23
24// viewShot is what one screen showed before an action.
25type viewShot struct {
26 sc *screen
27 ws *Workspace
28 // overview is set when the screen showed its overview: ws is then the
29 // row on screen, the camera fields stay zero, and rects are the cards.
30 overview bool
31 // settledView is ws's settled view and view where its columns were on screen
32 // (settledView plus shift).
33 settledView int
34 view float64
35 // viewV and switchV are the speeds the running view and workspace
36 // springs have at the snapshot's time (units per second, 0 without
37 // one), not at their last frame. An action may stop them
38 // (Monitor.Focus), so the transition reads them here.
39 viewV, switchV float64
40 switchAxis layoutAxis
41 // stashShown, stashAt, stashLen and stashPos are whether the stash was
42 // on screen, its selection, size and drawn position (selection plus
43 // stashView.off, in stash windows); stashV is its slide's speed.
44 stashShown bool
45 stashAt, stashLen int
46 stashPos, stashV float64
47 // list and pos are the numbered workspaces and the monitor's fractional
48 // position in them (index of the current one plus switchView.off); ok is
49 // false when there is none (a shown stash workspace).
50 list []*Workspace
51 pos float64
52 ok bool
53 // rects are the windows the screen drew (cards in the overview),
54 // hidden windows left out. The slice is reused like list.
55 rects []rectShot
56 // stash holds the settled placements of the stash windows the screen
57 // drew, for the ones an action hides (transitionStash). Reused too.
58 stash []Placement
59}
60
61// rectOffsets are the four components of a rect motion: x, y, w, h.
62type rectOffsets struct{ x, y, w, h float64 }
63
64// rectShot is where a window was drawn: its settled rect plus the float
65// offsets of its running rect motion (zero without one), which the drawn
66// rect only rounds. vel is the speed of each offset component, in units per
67// second. Offsets and speeds are sampled at the snapshot's time.
68type rectShot struct {
69 id WindowID
70 rect Rect
71 off, vel rectOffsets
72 // fade and fadeV are the fade offset of a running fade motion and its
73 // speed, so an overview toggle carries it over (transitionOverview).
74 fade, fadeV float64
75 // dim and dimV are the dim offset of a running dim motion and its
76 // speed; veil is the veil the window's settled placement drew
77 // (Stash.Dim times Placement.Veil, 0 for another window). An action
78 // that changes it takes the veil from what was on screen to the new
79 // one (transitionRects).
80 dim, dimV, veil float64
81}
82
83// rectMotion takes a window's drawn rect to its settled one: each component
84// is an offset from the settled value, sprung to 0 (dx, dy, dw, dh hold the
85// current offsets, updated by advance). fade and dim are offsets too, from
86// the settled Fade (0, or 1 for a leaving window) and the settled Dim.
87//
88// With scale, the window's content follows its drawn size: the shown
89// placement gets a Zoom of the settled zoom (its Preview, or 1) times
90// shown.W / settled.W (refreshShown), which the scene carries as Preview.
91// The walk shrinks content toward the content rect's top-left, so this
92// suits aspect-preserving changes: the zoom follows the smaller of the width
93// and height ratios, so content never exceeds its frame when the aspect
94// differs a little (rounding) or a lot (the frame is then letterboxed). An overview card's settled zoom is
95// its Preview: a window that opens into a card goes from 1 to Preview with
96// its width, and back.
97//
98// A leaving motion draws a window that closed or hid while it fades out:
99// left is its last settled placement, which refreshShown appends (Hidden
100// and Leaving) to the settled and shown layouts, keeping them aligned.
101type rectMotion struct {
102 x, y, w, h motion
103 dx, dy, dw, dh float64
104 fade, dim motion
105 df, ddim float64
106 scale bool
107 leaving bool
108 left Placement
109}
110
111// apply is r moved by the current offsets; a size never drops below 1.
112func (m *rectMotion) apply(r Rect) Rect {
113 return Rect{
114 X: r.X + int(math.Round(m.dx)), Y: r.Y + int(math.Round(m.dy)),
115 W: max(r.W+int(math.Round(m.dw)), 1), H: max(r.H+int(math.Round(m.dh)), 1),
116 }
117}
118
119// sample is where the offsets and their speeds would be at now, without
120// moving the motion: a running component is sampled on a copy.
121func (m *rectMotion) sample(now time.Time) (off, vel rectOffsets) {
122 off = rectOffsets{m.dx, m.dy, m.dw, m.dh}
123 sampleComponent(m.x, &off.x, &vel.x, now)
124 sampleComponent(m.y, &off.y, &vel.y, now)
125 sampleComponent(m.w, &off.w, &vel.w, now)
126 sampleComponent(m.h, &off.h, &vel.h, now)
127 return off, vel
128}
129
130func sampleComponent(m motion, off, vel *float64, now time.Time) {
131 if m.on {
132 *off, *vel = m.sampleAt(now)
133 }
134}
135
136// advance moves the offsets to now; a settled component stops at 0.
137func (m *rectMotion) advance(now time.Time) {
138 m.dx = stepComponent(&m.x, m.dx, now)
139 m.dy = stepComponent(&m.y, m.dy, now)
140 m.dw = stepComponent(&m.w, m.dw, now)
141 m.dh = stepComponent(&m.h, m.dh, now)
142 m.df = stepComponent(&m.fade, m.df, now)
143 m.ddim = stepComponent(&m.dim, m.ddim, now)
144}
145
146func stepComponent(m *motion, cur float64, now time.Time) float64 {
147 if !m.on {
148 return cur
149 }
150 v, done := m.at(now)
151 if done {
152 *m = motion{}
153 return 0
154 }
155 return v
156}
157
158// on reports whether any component still runs.
159func (m *rectMotion) on() bool {
160 return m.x.on || m.y.on || m.w.on || m.h.on || m.fade.on || m.dim.on
161}
162
163// settledFade is the Fade the motion springs to: a leaving window fades
164// out, any other is opaque.
165func (m *rectMotion) settledFade() float64 {
166 if m.leaving {
167 return 1
168 }
169 return 0
170}
171
172// show is p as drawn: its rect moved by the offsets, its fade and dim
173// offsets added, and its content zoomed with its size when scale is set.
174func (m *rectMotion) show(p *Placement) {
175 settled := p.Rect
176 p.Rect = m.apply(settled)
177 p.Fade = max(0, min(1, m.settledFade()+m.df))
178 // A peek's Dim is an offset from its veil (publish adds Stash.Dim and
179 // clamps the sum): it may go below 0, down to cancelling the veil.
180 lo := 0.0
181 if p.Veil > 0 {
182 lo = -1
183 }
184 p.Dim = max(lo, min(1, p.Dim+m.ddim))
185 if m.scale && settled.W > 0 && settled.H > 0 {
186 zoom := p.Preview
187 if zoom == 0 {
188 zoom = 1
189 if m.leaving {
190 // A leaving window settles shrunk, its content with it.
191 zoom = appearScale
192 }
193 }
194 zoom *= min(float64(p.Rect.W)/float64(settled.W), float64(p.Rect.H)/float64(settled.H))
195 if zoom >= 0.999 {
196 // Drawn at its size: content is never magnified. publish
197 // emits no Zoom for a window, and Zoom 1 for a card (whose
198 // Preview would shrink it).
199 zoom = 1
200 }
201 p.Zoom = zoom
202 }
203}
204
205// retargetComponent starts m for an offset of off from its settled value at
206// now, with the speed v it had then. A component with nothing to move and no
207// speed stops.
208func (c *Core) retargetComponent(m *motion, cur *float64, off, v float64, now time.Time) {
209 c.retargetWith(m, cur, viewSpring(off, v), now)
210}
211
212// retargetLevel is retargetComponent for a fade or veil offset (a level in
213// 0..1): its spring runs to its fine epsilon, no pixel snap.
214func (c *Core) retargetLevel(m *motion, cur *float64, off, v float64, now time.Time) {
215 c.retargetWith(m, cur, levelSpring(off, v), now)
216}
217
218func (c *Core) retargetWith(m *motion, cur *float64, s spring, now time.Time) {
219 *cur = s.From
220 if s.From == 0 && s.Velocity == 0 {
221 *m = motion{}
222 return
223 }
224 *m = c.spring(s, now)
225}
226
227// refreshShown builds the layouts of every screen once per publish: the
228// settled one (what configures are sized from) and the shown one (the same
229// with the rect motions applied: what is drawn and hit-tested). Rect motions
230// belong to the workspace they began on, or to the open overview (rectsOwner):
231// any other change drops them.
232//
233// Leaving motions add an entry for a window the layout no longer shows:
234// appended to both layouts in ID order, Hidden (no input, focus, popups or
235// configure) and Leaving (drawn, fading). The two layouts stay
236// index-aligned: publish reads the settled entry at the shown one's index.
237func (c *Core) refreshShown() {
238 for _, sc := range c.screens {
239 m := sc.mon
240 if len(sc.rects) > 0 && sc.rectsWS != sc.rectsOwner() {
241 sc.stopRects()
242 }
243 sc.layoutBuf = m.layoutInto(sc.layoutBuf)
244 sc.settledLayout = sc.layoutBuf
245 if len(sc.settledLayout) == 0 {
246 // Empty stays nil, as a fresh Layout() was: shown nil falls
247 // back to the live layout until the next publish.
248 sc.settledLayout = nil
249 }
250 if len(sc.rects) > 0 {
251 // A window that left the layout (or is hidden) keeps no motion:
252 // it would ask for frames with nothing to move. A leaving one
253 // is the opposite: it goes once the layout shows the window.
254 for id, rm := range sc.rects {
255 shown := slices.ContainsFunc(sc.settledLayout, func(p Placement) bool {
256 return p.ID == id && !p.Hidden
257 })
258 if shown == rm.leaving {
259 delete(sc.rects, id)
260 }
261 }
262 }
263 if len(sc.rects) == 0 {
264 sc.shown = sc.settledLayout
265 continue
266 }
267 sc.settledLayout = sc.withLeaving(sc.settledLayout)
268 buf := append(sc.shownBuf[:0], sc.settledLayout...)
269 for i := range buf {
270 p := &buf[i]
271 if p.Hidden && !p.Leaving {
272 continue
273 }
274 if rm, ok := sc.rects[p.ID]; ok {
275 rm.show(p)
276 }
277 }
278 sc.shownBuf, sc.shown = buf, buf
279 }
280}
281
282// withLeaving is layout with the leaving windows' last placements added,
283// Hidden and Leaving, into the screen's reused buffer (the layout itself
284// is left alone). A window the layout still lists, Hidden (a hidden stash),
285// is replaced where it is. A leaving tile goes where the tiles are painted: before
286// the first window that opens the floats (a float above the tiles, as the
287// renderer orders them), so it never flashes over a float; a leaving float
288// goes last, over the tiles. Within a group the absent ones go smallest ID
289// first: a stable order keeps the scenes comparable frame to frame.
290func (s *screen) withLeaving(layout []Placement) []Placement {
291 out := append(s.settledBuf[:0], layout...)
292 // Pass 1: an entry the layout still lists, Hidden (a stash that hid),
293 // replaces it where it is, never twice. Where the pass goes through the
294 // map does not matter: every entry has its own place. The absent ones
295 // are collected for pass 2.
296 ids := s.leaveIDs[:0]
297 for id, rm := range s.rects {
298 if !rm.leaving {
299 continue
300 }
301 j := slices.IndexFunc(out, func(q Placement) bool { return q.ID == id && q.Hidden && !q.Leaving })
302 if j < 0 {
303 ids = append(ids, id)
304 continue
305 }
306 out[j] = rm.leavingPlacement()
307 }
308 // Pass 2: the absent ones in ID order, each at the end of its group
309 // (tiles before the first float, a leaving stash float included;
310 // floats last), so the order never depends on the map's.
311 floats := len(out)
312 for i, p := range out {
313 if p.Floating && !p.Below && p.Preview == 0 && (!p.Hidden || p.Leaving) {
314 floats = i
315 break
316 }
317 }
318 slices.Sort(ids)
319 for _, id := range ids {
320 p := s.rects[id].leavingPlacement()
321 if p.Floating {
322 out = append(out, p)
323 continue
324 }
325 out = slices.Insert(out, floats, p)
326 floats++
327 }
328 s.leaveIDs, s.settledBuf = ids, out
329 return out
330}
331
332// leavingPlacement is the placement a leaving motion draws: its last
333// settled one, Hidden and Leaving (the scene draws it; input, focus,
334// popups and configures see a hidden window, as the C0 design has it).
335// Hidden stays set on purpose: the leaving entry never takes input, even
336// though its stash window is still in the layout.
337func (m rectMotion) leavingPlacement() Placement {
338 p := m.left
339 p.Hidden, p.Leaving, p.Focused, p.Fade, p.Dim, p.Zoom = true, true, false, 0, 0, 0
340 return p
341}
342
343// appearAt starts the entrance of a window the action made visible on sc,
344// whose settled placement is p: it fades in from invisible and grows from
345// 90 % of its settled rect, around its centre. Nothing with animations off
346// or for a hidden placement. A leaving motion of the same window (a show
347// during a hide) is continued from, not replaced: the entrance starts at
348// the fade, rect and speeds it was drawn with.
349func (c *Core) appearAt(sc *screen, p Placement, now time.Time) {
350 if !c.animOn() || p.Hidden {
351 return
352 }
353 if sc.rects == nil {
354 sc.rects = make(map[WindowID]rectMotion)
355 }
356 var rm rectMotion
357 rm.scale = true
358 if old, ok := sc.rects[p.ID]; ok && old.leaving {
359 c.appearFrom(&rm, p, &old, now)
360 } else {
361 c.scaleFrom(&rm, p.Rect, appearScale, now)
362 c.retargetLevel(&rm.fade, &rm.df, 1, 0, now)
363 }
364 sc.rects[p.ID], sc.rectsWS = rm, sc.mon.Current()
365}
366
367// appearFrom starts rm, the entrance to the settled placement p, from where
368// the leaving motion old draws its window at now: its rect (old.left.Rect
369// plus its offsets, sampled), fade, veil and their speeds. The leaving fade
370// is an offset from 1, the entrance's from 0.
371func (c *Core) appearFrom(rm *rectMotion, p Placement, old *rectMotion, now time.Time) {
372 settled := p.Rect
373 off, vel := old.sample(now)
374 base := old.left.Rect
375 fade, fadeV := old.df, 0.0
376 sampleComponent(old.fade, &fade, &fadeV, now)
377 dim, dimV := old.ddim, 0.0
378 sampleComponent(old.dim, &dim, &dimV, now)
379 // The veil the leaving entry drew (its peek's, plus the offset) to the
380 // one the settled placement draws.
381 dim += c.peekDim(old.left) - c.peekDim(p)
382 c.retargetComponent(&rm.x, &rm.dx, float64(base.X-settled.X)+off.x, vel.x, now)
383 c.retargetComponent(&rm.y, &rm.dy, float64(base.Y-settled.Y)+off.y, vel.y, now)
384 c.retargetComponent(&rm.w, &rm.dw, float64(base.W-settled.W)+off.w, vel.w, now)
385 c.retargetComponent(&rm.h, &rm.dh, float64(base.H-settled.H)+off.h, vel.h, now)
386 c.retargetLevel(&rm.fade, &rm.df, 1+fade, fadeV, now)
387 c.retargetLevel(&rm.dim, &rm.ddim, dim, dimV, now)
388}
389
390// mapWindow handles a window's map: it joins its workspace, the tiles the
391// new one displaces re-flow with rect motions, and a window that comes on
392// screen appears (appearMapped). A map is the one client event that
393// animates; the settled layout is the same with animations off, so the
394// client is configured once, to its final size.
395func (c *Core) mapWindow(v ports.WindowMapped) {
396 now := c.now()
397 c.windows.mapped(v, now)
398 if s, _ := c.screenOf(v.ID); s != nil || !c.animOn() || c.security.Protected {
399 // Mapped before (a re-map keeps its place), animations off or a
400 // session lock: nothing to animate.
401 c.placement.place(c, v)
402 return
403 }
404 shots := c.snapshot(now)
405 for i := range shots {
406 // A screen with its overview open re-lays its cards at once: a map
407 // is not an overview action (transition skips a shot without ws).
408 if shots[i].overview {
409 shots[i].ws = nil
410 }
411 }
412 c.placement.place(c, v)
413 c.transition(shots, now)
414 c.appearMapped(v.ID, now)
415}
416
417// appearMapped starts the entrance of a window that just mapped when it
418// shows on its screen's current workspace as a tile or an ordinary float:
419// not with the overview open or a session lock, not fullscreen (it keeps
420// the direct scanout path), not a float below the columns, not a stash
421// peek.
422func (c *Core) appearMapped(id WindowID, now time.Time) {
423 sc, w := c.screenOf(id)
424 if sc == nil || c.security.Protected || sc.mon.ov.open || sc.mon.Current() != w {
425 return
426 }
427 for _, p := range sc.mon.Layout() {
428 if p.ID != id {
429 continue
430 }
431 if !p.Hidden && !p.Fullscreen && !p.Below && !p.Peek && p.Preview == 0 {
432 c.appearAt(sc, p, now)
433 }
434 return
435 }
436}
437
438// unmapWindow handles a window's unmap: it leaves its workspace, the tiles
439// around it re-flow with rect motions, and a window that was drawn as a
440// tile or an ordinary float keeps fading out from where it was drawn
441// (leaveFrom with the snapshot's record of it: a window closed mid-motion
442// leaves from its drawn rect, fade and speed, not from its settled rect).
443// Like a map, an unmap is a client event that animates; the settled layout
444// is the same with animations off. A fullscreen window, one under a
445// session lock or with the overview open just goes.
446func (c *Core) unmapWindow(v ports.WindowUnmapped) {
447 // The window is unmapped: a toplevel that maps again keeps its ID and
448 // must be configured from nothing (a leaving entry keeps no configure).
449 c.configures.forget(v.ID)
450 sc, w := c.screenOf(v.ID)
451 if sc == nil {
452 return
453 }
454 if !c.animOn() || c.security.Protected || sc.mon.ov.open {
455 sc.mon.RemoveWindow(v.ID)
456 delete(sc.rects, v.ID)
457 return
458 }
459 now := c.now()
460 // The placement that leaves is the settled one of the last publish:
461 // where the window was going.
462 var left Placement
463 if sc.mon.Current() == w {
464 for _, p := range sc.settledLayout {
465 if p.ID == v.ID {
466 left = p
467 break
468 }
469 }
470 }
471 shots := c.snapshot(now)
472 var shot *rectShot
473 for i := range shots {
474 if shots[i].overview {
475 shots[i].ws = nil
476 }
477 if shots[i].sc == sc {
478 if j := slices.IndexFunc(shots[i].rects, func(r rectShot) bool { return r.id == v.ID }); j >= 0 {
479 shot = &shots[i].rects[j]
480 }
481 }
482 }
483 sc.mon.RemoveWindow(v.ID)
484 // A window already leaving (a stash hide still fading) keeps its
485 // motion: the fade goes on from where it is, drawn from the content
486 // the renderer kept. Any other motion goes with the window.
487 if rm, ok := sc.rects[v.ID]; !ok || !rm.leaving {
488 delete(sc.rects, v.ID)
489 }
490 c.transition(shots, now)
491 if left.ID == v.ID && !left.Leaving && !left.Fullscreen && !left.Below && !left.Peek {
492 c.leaveFrom(sc, left, shot, now)
493 }
494}
495
496// resizeFloating applies a floating window's committed size. A float maps
497// before its first commit, so core first lays it out at a default size;
498// when its entrance runs, the settled rect changing under it would break
499// the aspect of the 90 % rect. The entrance's rect offsets (and their
500// springs, which are linear in their start and speed) are scaled by the
501// settled size ratio: the progress and speed carry over, and the window
502// still grows from 90 % of its real size around its centre.
503func (c *Core) resizeFloating(v ports.WindowResized) {
504 sc, w := c.screenOf(v.ID)
505 if w == nil {
506 return
507 }
508 rm, running := sc.rects[v.ID]
509 running = running && rm.scale && !rm.leaving
510 var old Rect
511 if running {
512 old = settledRect(sc.mon, v.ID)
513 }
514 w.ResizeFloating(v.ID, v.Width, v.Height)
515 if !running {
516 return
517 }
518 cur := settledRect(sc.mon, v.ID)
519 if old.W <= 0 || old.H <= 0 || cur.W <= 0 || cur.H <= 0 || old == cur {
520 return
521 }
522 kw, kh := float64(cur.W)/float64(old.W), float64(cur.H)/float64(old.H)
523 rm.dx, rm.dw = rm.dx*kw, rm.dw*kw
524 rm.dy, rm.dh = rm.dy*kh, rm.dh*kh
525 rm.x.scale(kw)
526 rm.w.scale(kw)
527 rm.y.scale(kh)
528 rm.h.scale(kh)
529 sc.rects[v.ID] = rm
530}
531
532// settledRect is where m's layout puts the visible window id, zero if it
533// shows none.
534func settledRect(m *Monitor, id WindowID) Rect {
535 for _, p := range m.Layout() {
536 if p.ID == id && !p.Hidden {
537 return p.Rect
538 }
539 }
540 return Rect{}
541}
542
543// leaveFrom keeps drawing a window the action closed or hid, from its last
544// settled placement p: it fades out and shrinks to 90 % around its centre,
545// then its entry goes. Nothing with animations off, or for a placement that
546// was not drawn. shot is the snapshot's record of how the window was drawn
547// (nil for a window at rest): the exit starts at the rect, fade and dim the
548// window had on screen, with their speeds, so a hide
549// during an entrance or a slide does not jump.
550func (c *Core) leaveFrom(sc *screen, p Placement, shot *rectShot, now time.Time) {
551 if !c.animOn() || p.Hidden || p.Preview > 0 || p.Rect.W <= 0 || p.Rect.H <= 0 {
552 return
553 }
554 if sc.rects == nil {
555 sc.rects = make(map[WindowID]rectMotion)
556 }
557 rm := rectMotion{scale: true, leaving: true, left: p}
558 rm.left.Hidden, rm.left.Leaving, rm.left.Focused = true, true, false
559 var off, vel rectOffsets
560 fade, fadeV, dim, dimV := 0.0, 0.0, 0.0, 0.0
561 if shot != nil {
562 off, vel, fade, fadeV, dim, dimV = shot.off, shot.vel, shot.fade, shot.fadeV, shot.dim, shot.dimV
563 }
564 // The entry settles at 90 % of the rect it left, its content zoomed
565 // to match (show): the offsets run from the rect it was drawn at (the
566 // full one at rest) to that.
567 end := scaledRect(p.Rect, appearScale)
568 c.retargetComponent(&rm.x, &rm.dx, float64(p.Rect.X-end.X)+off.x, vel.x, now)
569 c.retargetComponent(&rm.y, &rm.dy, float64(p.Rect.Y-end.Y)+off.y, vel.y, now)
570 c.retargetComponent(&rm.w, &rm.dw, float64(p.Rect.W-end.W)+off.w, vel.w, now)
571 c.retargetComponent(&rm.h, &rm.dh, float64(p.Rect.H-end.H)+off.h, vel.h, now)
572 rm.left.Rect = end
573 // The leaving fade is an offset from 1: the window drawn at fade f is
574 // at f-1.
575 c.retargetLevel(&rm.fade, &rm.df, fade-1, fadeV, now)
576 c.retargetLevel(&rm.dim, &rm.ddim, dim, dimV, now)
577 sc.rects[p.ID], sc.rectsWS = rm, sc.mon.Current()
578}
579
580// appearScale is the size, relative to the settled rect, a window appears
581// from and leaves to.
582const appearScale = 0.9
583
584// scaledRect is r scaled by k around its centre, in float.
585func scaledRect(r Rect, k float64) Rect {
586 w, h := float64(r.W)*k, float64(r.H)*k
587 x := float64(r.X) + (float64(r.W)-w)/2
588 y := float64(r.Y) + (float64(r.H)-h)/2
589 return Rect{X: int(math.Round(x)), Y: int(math.Round(y)), W: int(math.Round(w)), H: int(math.Round(h))}
590}
591
592// scaleFrom starts rm's rect components from settled scaled by k around
593// its centre, toward settled.
594func (c *Core) scaleFrom(rm *rectMotion, settled Rect, k float64, now time.Time) {
595 w, h := float64(settled.W)*k, float64(settled.H)*k
596 c.retargetComponent(&rm.x, &rm.dx, (float64(settled.W)-w)/2, 0, now)
597 c.retargetComponent(&rm.y, &rm.dy, (float64(settled.H)-h)/2, 0, now)
598 c.retargetComponent(&rm.w, &rm.dw, w-float64(settled.W), 0, now)
599 c.retargetComponent(&rm.h, &rm.dh, h-float64(settled.H), 0, now)
600}
601
602// snapshot records what every screen without an open overview shows at now.
603// Running motions are sampled at now on a copy, so a transition that
604// follows chains from their speed at the time of the action, not from the
605// last frame's. The view is the one drawn (the layouts it is measured
606// against are), only its speed is sampled. The returned slice and the lists
607// in it belong to Core and are reused by the next snapshot: use it before
608// the next action only.
609func (c *Core) snapshot(now time.Time) []viewShot {
610 full := c.shots[:cap(c.shots)]
611 out := c.shots[:0]
612 for _, sc := range c.screens {
613 m := sc.mon
614 var s viewShot
615 if len(out) < len(full) {
616 s.list = full[len(out)].list[:0]
617 s.rects = full[len(out)].rects[:0]
618 s.stash = full[len(out)].stash[:0]
619 }
620 s.sc = sc
621 w := m.Current()
622 s.ws, s.overview = w, m.ov.open
623 if !m.ov.open {
624 // The overview does not scroll or slide: no camera.
625 s.settledView, s.view = w.View, float64(w.View)+w.view.off
626 s.switchAxis = w.policy().workspace
627 if m.switchView.busy() {
628 s.switchAxis = m.switchAxis
629 }
630 }
631 if w.view.motion.on {
632 _, s.viewV = w.view.motion.sampleAt(now)
633 }
634 if m.switchView.motion.on {
635 _, s.switchV = m.switchView.motion.sampleAt(now)
636 }
637 s.stashShown, s.stashAt, s.stashLen, s.stashPos = w.stashShown(), w.stashAt, len(w.Stash), float64(w.stashAt)+w.stashView.off
638 if w.stashView.motion.on {
639 _, s.stashV = w.stashView.motion.sampleAt(now)
640 }
641 // settledLayout and shown are index-aligned and differ only by
642 // the offsets of sc.rects: the shot keeps both unrounded. The
643 // overview's cards are recorded like windows.
644 for _, p := range sc.settledLayout {
645 if p.Hidden {
646 continue
647 }
648 r := rectShot{id: p.ID, rect: p.Rect}
649 if rm, ok := sc.rects[p.ID]; ok {
650 r.off, r.vel = rm.sample(now)
651 r.fade = rm.df
652 sampleComponent(rm.fade, &r.fade, &r.fadeV, now)
653 r.dim = rm.ddim
654 sampleComponent(rm.dim, &r.dim, &r.dimV, now)
655 }
656 r.veil = c.peekDim(p)
657 s.rects = append(s.rects, r)
658 if !m.ov.open && w.stashIndex(p.ID) >= 0 {
659 s.stash = append(s.stash, p)
660 }
661 }
662 if m.shown == nil && !m.ov.open {
663 // A landing slide measures in the list it began on.
664 list := m.Workspaces
665 if m.switchList != nil {
666 list = m.switchList
667 }
668 if i := indexOf(list, w); i >= 0 {
669 s.list = append(s.list, list...)
670 s.pos, s.ok = float64(i)+m.switchView.off, true
671 }
672 }
673 out = append(out, s)
674 }
675 for i := len(out); i < len(full); i++ {
676 // Shots of unplugged screens keep nothing of them alive.
677 full[i].sc, full[i].ws = nil, nil
678 clear(full[i].list[:cap(full[i].list)])
679 full[i].list = full[i].list[:0]
680 }
681 c.shots = out
682 return out
683}
684
685// transition starts the springs that take each screen from what before
686// showed to what the action left, at now. It does nothing with animations
687// off, or while a swipe follows the fingers on that screen. Per-window rect
688// motions join the camera here; with the overview open before or after
689// there is no camera, only the cards' (transitionOverview).
690//
691// A workspace moved to another monitor (move-workspace-to-monitor-*, or a
692// named workspace pulled to the focused one by a "workspace <name>" bind
693// while it shows on another) is animated on both screens: the destination's
694// windows slide in from where the source drew them (no workspace slide
695// there), the source slides to its next workspace. The focus-monitor-*
696// actions move nothing: no window changes place, so they are not animated
697// (the focus pulse marks the new focus).
698func (c *Core) transition(before []viewShot, now time.Time) {
699 if !c.animOn() {
700 return
701 }
702 for i := range before {
703 b := &before[i]
704 if b.ws == nil || !c.hasScreen(b.sc) || c.following(b.sc) {
705 continue
706 }
707 if b.overview || b.sc.mon.ov.open {
708 c.transitionOverview(b, now)
709 continue
710 }
711 c.transitionCamera(b, before, now)
712 c.transitionRects(b, before, now)
713 c.transitionStash(b, now)
714 }
715}
716
717// following reports whether a swipe moves sc's view with the fingers.
718func (c *Core) following(sc *screen) bool {
719 g := c.swipe
720 return g != nil && g.screen == sc && (g.mode == swipeColumns || g.mode == swipeWorkspaces || g.mode == swipeStash)
721}
722
723// transitionCamera slides the columns or the monitor from what b showed.
724func (c *Core) transitionCamera(b *viewShot, before []viewShot, now time.Time) {
725 m := b.sc.mon
726 w := m.Current()
727 switch {
728 case w == b.ws:
729 if stashSlid(b, w) {
730 // A stash navigation slides the view from where it was drawn:
731 // the windows keep their settled places (transitionRects).
732 w.stashView.off = b.stashPos - float64(w.stashAt)
733 w.stashView.motion = c.spring(w.stashSpring(w.stashView.off, b.stashV), now)
734 return
735 }
736 if w.View == b.settledView || !w.slidable() {
737 return
738 }
739 // A running landing slide was retargeted by scroll(): its velocity
740 // carries over, and the spring starts now like every action's.
741 w.view.off = b.view - float64(w.View)
742 w.view.motion = c.spring(viewSpring(w.view.off, b.viewV), now)
743 case b.ok && m.shown == nil && movedFrom(b, before) == nil:
744 // A screen that received its workspace from another monitor does
745 // not slide to it: a running slide's list (switchList) may still
746 // hold the workspace, which would start a slide from nowhere. Only
747 // its windows move (transitionRects).
748 j := indexOf(b.list, w)
749 if j < 0 {
750 return
751 }
752 off := b.pos - float64(j)
753 // The snapshot's list is reused by the next action: the slide keeps
754 // its own copy.
755 m.switchView.off, m.switchList = off, slices.Clone(b.list)
756 m.switchAxis = b.switchAxis
757 if off == 0 || m.framedSwitch() {
758 m.stopSwitch()
759 return
760 }
761 m.switchView.motion = c.spring(m.switchSpring(off, b.switchV), now)
762 }
763}
764
765// stashSlid reports whether an action moved the selection of the stash b
766// showed on w: a navigation, not a toggle, an unstash or a hide.
767func stashSlid(b *viewShot, w *Workspace) bool {
768 return b.stashShown && w.stashAt != b.stashAt && len(w.Stash) == b.stashLen && w.stashShown()
769}
770
771// transitionRects starts or retargets the rect motions of the windows whose
772// layout the action changed. The new layout is measured with the camera as
773// transitionCamera left it (the view where it was on screen), so a window
774// the view scrolls differs only by what the layout itself changed. Windows
775// that were not drawn before (new, or hidden) or are hidden now get none.
776//
777// A workspace that arrived from another monitor starts from the rects its
778// source screen drew (found in before), translated to this screen's
779// coordinates; a source whose workspace left, or any other change of
780// workspace, gets none.
781func (c *Core) transitionRects(b *viewShot, before []viewShot, now time.Time) {
782 sc := b.sc
783 m := sc.mon
784 from, dx, dy := b, 0, 0
785 if m.Current() != b.ws {
786 // The motions are those of the workspace this screen left.
787 sc.stopRects()
788 if from = movedFrom(b, before); from == nil {
789 return
790 }
791 dx, dy = from.sc.x-sc.x, from.sc.y-sc.y
792 }
793 for _, p := range m.Layout() {
794 if p.Hidden || p.Preview > 0 {
795 continue
796 }
797 i := slices.IndexFunc(from.rects, func(r rectShot) bool { return r.id == p.ID })
798 if i < 0 {
799 continue
800 }
801 old := &from.rects[i]
802 oldRect := Rect{X: old.rect.X + dx, Y: old.rect.Y + dy, W: old.rect.W, H: old.rect.H}
803 // A window that becomes a peek or stops being one (a stash
804 // toggle) changes its veil: the offset takes the one drawn to the
805 // new one. A stash slide moves the veil with the view, so the veil
806 // of a navigation matches. Otherwise a running dim motion goes on
807 // as it is.
808 dimOff := old.dim + old.veil - c.peekDim(p)
809 dimMoves := math.Abs(old.veil-c.peekDim(p)) > veilEps
810 if oldRect == p.Rect && !dimMoves {
811 // The settled rect did not change: a running motion goes on
812 // as it is, and there is none to start.
813 continue
814 }
815 if sc.rects == nil {
816 sc.rects = make(map[WindowID]rectMotion)
817 }
818 // Where the window was drawn, unrounded, relative to where it goes.
819 rm := sc.rects[p.ID]
820 if oldRect != p.Rect {
821 // A re-flow may change the aspect: the content zoom (appear)
822 // stops following the frame and snaps to the settled size; the
823 // fade goes on.
824 rm.scale = false
825 c.retargetComponent(&rm.x, &rm.dx, float64(oldRect.X-p.Rect.X)+old.off.x, old.vel.x, now)
826 c.retargetComponent(&rm.y, &rm.dy, float64(oldRect.Y-p.Rect.Y)+old.off.y, old.vel.y, now)
827 c.retargetComponent(&rm.w, &rm.dw, float64(oldRect.W-p.Rect.W)+old.off.w, old.vel.w, now)
828 c.retargetComponent(&rm.h, &rm.dh, float64(oldRect.H-p.Rect.H)+old.off.h, old.vel.h, now)
829 }
830 if dimMoves {
831 c.retargetLevel(&rm.dim, &rm.ddim, dimOff, old.dimV, now)
832 }
833 if rm.on() {
834 sc.rects[p.ID], sc.rectsWS = rm, m.Current()
835 } else {
836 delete(sc.rects, p.ID)
837 }
838 }
839}
840
841// transitionStash animates the stash windows an action shows or hides on
842// the workspace b's screen keeps on screen (a toggle-stash-visible, a click
843// that closes the stash; a navigation slides the view instead, see
844// stashSlid): one drawn after and not before appears (appearAt), one
845// drawn before and hidden after leaves from its last settled placement
846// (leave). A window that is gone from the layout (unstashed, moved) or
847// fullscreen is not touched: its rect motion, if any, is transitionRects's.
848// Nothing during a session lock or with a covering fullscreen window (it
849// keeps its direct scanout path).
850func (c *Core) transitionStash(b *viewShot, now time.Time) {
851 sc := b.sc
852 m := sc.mon
853 w := m.Current()
854 if w != b.ws || len(w.Stash) == 0 || c.security.Protected || w.cover() != 0 || stashSlid(b, w) {
855 return
856 }
857 layout := m.Layout()
858 for _, p := range b.stash {
859 i := slices.IndexFunc(layout, func(q Placement) bool { return q.ID == p.ID })
860 if i >= 0 && layout[i].Hidden && !p.Fullscreen {
861 var shot *rectShot
862 if j := slices.IndexFunc(b.rects, func(r rectShot) bool { return r.id == p.ID }); j >= 0 {
863 shot = &b.rects[j]
864 }
865 c.leaveFrom(sc, p, shot, now)
866 }
867 }
868 for _, p := range layout {
869 if p.Hidden || p.Fullscreen || w.stashIndex(p.ID) < 0 || slices.ContainsFunc(b.rects, func(r rectShot) bool { return r.id == p.ID }) {
870 continue
871 }
872 c.appearAt(sc, p, now)
873 }
874}
875
876// veilEps absorbs the float drift between a slide's drawn veil and the
877// settled one: a veil that differs by less is the same.
878const veilEps = 1e-9
879
880// peekDim is the veil the settled placement p draws: the configured stash
881// dim for a peek, none for another window. The placement's Dim is an
882// offset from it.
883func (c *Core) peekDim(p Placement) float64 {
884 return c.cfg.Stash.Dim * p.Veil
885}
886
887// transitionOverview starts the motions of an action that opened or closed
888// the overview, or moved inside it (the selection, the rows). The camera
889// springs stay stopped (ToggleOverview): every window drawn before and
890// after moves from its shown rect to its settled one, its content scaled
891// with it (a card's Zoom, rectMotion.show). A window drawn after and not
892// before (another row, a column the overview hid) fades in with no motion
893// of its own; one drawn before and not after just goes. Motions of the
894// other state are dropped first: their rects are not the ones snapshot
895// measured from. A fade that ran on a window drawn in both states carries
896// over (picking a card that still fades in does not pop it).
897func (c *Core) transitionOverview(b *viewShot, now time.Time) {
898 sc := b.sc
899 changed := b.overview != sc.mon.ov.open
900 if changed {
901 sc.stopRects()
902 }
903 for _, p := range sc.mon.Layout() {
904 if p.Hidden {
905 continue
906 }
907 rm := sc.rects[p.ID]
908 i := slices.IndexFunc(b.rects, func(r rectShot) bool { return r.id == p.ID })
909 if i >= 0 && changed {
910 c.retargetLevel(&rm.fade, &rm.df, b.rects[i].fade, b.rects[i].fadeV, now)
911 }
912 if i < 0 {
913 c.retargetLevel(&rm.fade, &rm.df, 1, 0, now)
914 } else if old := &b.rects[i]; old.rect != p.Rect {
915 rm.scale = true
916 c.retargetComponent(&rm.x, &rm.dx, float64(old.rect.X-p.Rect.X)+old.off.x, old.vel.x, now)
917 c.retargetComponent(&rm.y, &rm.dy, float64(old.rect.Y-p.Rect.Y)+old.off.y, old.vel.y, now)
918 c.retargetComponent(&rm.w, &rm.dw, float64(old.rect.W-p.Rect.W)+old.off.w, old.vel.w, now)
919 c.retargetComponent(&rm.h, &rm.dh, float64(old.rect.H-p.Rect.H)+old.off.h, old.vel.h, now)
920 } else {
921 continue
922 }
923 if !rm.on() {
924 delete(sc.rects, p.ID)
925 continue
926 }
927 if sc.rects == nil {
928 sc.rects = make(map[WindowID]rectMotion)
929 }
930 sc.rects[p.ID], sc.rectsWS = rm, sc.rectsOwner()
931 }
932}
933
934// movedFrom is the shot of the other screen that showed the workspace b's
935// screen shows now (moved to it by an action), or nil.
936func movedFrom(b *viewShot, before []viewShot) *viewShot {
937 w := b.sc.mon.Current()
938 for i := range before {
939 if o := &before[i]; o.ws == w && o.sc != b.sc && !o.overview {
940 return o
941 }
942 }
943 return nil
944}