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