1package core
2
3import (
4 "math"
5 "slices"
6 "time"
7)
8
9// Slides move what is on screen smoothly: a workspace's columns (view), its
10// stash (stashView) and the monitor between workspaces (switchView). All
11// are pure presentation over the settled state (ViewX, Focus, stashAt,
12// Active): the layout ends where the state says as soon as a slide stops.
13
14// slide is a view offset that a swipe sets while the fingers follow it, and
15// a spring then takes to 0 (off: pixels for the columns, windows for the
16// stash, workspaces for the monitor). Pure presentation over settled state.
17type slide struct {
18 off float64
19 motion motion
20}
21
22// stop lands the slide where its state says.
23func (s *slide) stop() { *s = slide{} }
24
25// step moves a running spring to now and stops it once it settles.
26func (s *slide) step(now time.Time) {
27 if !s.motion.on {
28 return
29 }
30 v, done := s.motion.at(now)
31 s.off = v
32 if done {
33 s.stop()
34 }
35}
36
37// busy reports whether the slide is off its state or its spring runs.
38func (s *slide) busy() bool { return s.motion.on || s.off != 0 }
39
40// slidable reports whether a swipe can scroll the columns: only scroll
41// overflow, with the focus on the columns and no fullscreen window.
42func (w *Workspace) slidable() bool {
43 return w.Overflow != OverflowFixed && len(w.Columns) > 0 && w.fullscreen == 0 && !w.onFloat()
44}
45
46// retarget keeps the view where it is on screen when ViewX moved from
47// before during a landing slide: the slide heads for the new view.
48// Without a landing slide the view moves at once, as it always has.
49func (w *Workspace) retarget(before int) {
50 if w.ViewX == before || !w.view.motion.on {
51 return
52 }
53 w.view.off += float64(before - w.ViewX)
54 w.view.motion = newMotion(viewSpring(w.view.off, w.view.motion.velocity()), time.Time{}, w.view.motion.slow)
55}
56
57func (w *Workspace) shiftPixels() int { return int(math.Round(w.view.off)) }
58
59// switchSpring is the workspace slide's spring from off: it snaps once it
60// is under half a logical pixel of the output height from its target
61// (slideLayout rounds the offset times the height).
62func (m *Monitor) switchSpring(off, velocity float64) spring {
63 s := workspaceSpring(off, velocity)
64 if h := m.template.Output.H; h > 0 {
65 s.Snap = pixelSnap / float64(h)
66 }
67 return s
68}
69
70// swipeScale turns touchpad distance into pixels: viewSwipeMovement
71// scrolls one usable width.
72func (w *Workspace) swipeScale() float64 {
73 return float64(w.Usable.W) / viewSwipeMovement
74}
75
76// snapPoints are the sorted views that align a column edge with the usable
77// area, never past the first or last column. Points closer than a
78// snapSpacing of the usable width merge into the first: a swipe step
79// always moves the view visibly.
80func (w *Workspace) snapPoints() []float64 {
81 g := w.gap()
82 minX, maxX := w.Usable.X+g, w.Usable.X+w.Usable.W-g
83 last := len(w.Columns) - 1
84 lo := w.columnX(0) - minX
85 hi := max(w.columnX(last)+w.columnWidth(last)-maxX, lo)
86 views := make([]int, 0, 2*len(w.Columns))
87 for i := range w.Columns {
88 views = append(views, min(max(w.columnX(i)-minX, lo), hi), min(max(w.columnX(i)+w.columnWidth(i)-maxX, lo), hi))
89 }
90 slices.Sort(views)
91 minStep := float64(w.Usable.W) * snapSpacing
92 points := []float64{float64(lo)}
93 for _, v := range views {
94 if float64(v)-points[len(points)-1] >= minStep {
95 points = append(points, float64(v))
96 }
97 }
98 // The last column's end stays a point; a point close before it goes.
99 if n := len(points); float64(hi) != points[n-1] {
100 if n > 1 && float64(hi)-points[n-1] < minStep {
101 points = points[:n-1]
102 }
103 points = append(points, float64(hi))
104 }
105 return points
106}
107
108// snapSpacing is the smallest distance between column snap points, in
109// usable widths.
110const snapSpacing = 0.1
111
112// snapFocus is the column to focus at view: the current one while it stays
113// fully shown at the current view, else the furthest fully shown column
114// toward the swipe (forward is rightward).
115func (w *Workspace) snapFocus(view int, forward bool) (focus int) {
116 g := w.gap()
117 minX, maxX := w.Usable.X+g, w.Usable.X+w.Usable.W-g
118 full := func(i int) bool {
119 x := w.columnX(i) - view
120 return x >= minX && x+w.columnWidth(i) <= maxX
121 }
122 if full(w.Focus) && view == w.ViewX {
123 return w.Focus
124 }
125 focus = -1
126 for i := range w.Columns {
127 if full(i) && (focus < 0 || forward) {
128 focus = i
129 }
130 }
131 if focus < 0 {
132 // Columns wider than the view: the one at the left edge.
133 focus = 0
134 for i := range w.Columns {
135 if w.columnX(i)-view <= minX {
136 focus = i
137 }
138 }
139 }
140 return focus
141}
142
143// stopSwitch lands the workspace slide and forgets the list it measured in.
144func (m *Monitor) stopSwitch() { m.switchView.stop(); m.switchList = nil }
145
146// framedSwitch keeps transitions involving a sized workspace settled. A
147// monitor-wide animation has no per-workspace crop; it must not expose
148// columns outside their viewport while sliding between different frames.
149func (m *Monitor) framedSwitch() bool {
150 if m.Current().Output != m.Output() {
151 return true
152 }
153 list := m.Workspaces
154 if m.switchList != nil {
155 list = m.switchList
156 }
157 base := indexOf(list, m.Current())
158 if base < 0 {
159 return false
160 }
161 pos := float64(base) + m.switchView.off
162 for _, i := range [2]int{int(math.Floor(pos)), int(math.Ceil(pos))} {
163 if i >= 0 && i < len(list) && m.has(list[i]) && list[i].Output != m.Output() {
164 return true
165 }
166 }
167 return false
168}
169
170// slideLayout adds the workspaces a workspace slide shows next to the
171// current one, moved by their distance, to the current layout (moved too).
172// A landing slide places them as the list was when the swipe began
173// (switchList): an empty workspace dropped since shows as background.
174func (m *Monitor) slideLayout(cur []Placement) []Placement {
175 if m.switchView.off == 0 || m.shown != nil || m.framedSwitch() {
176 return cur
177 }
178 list := m.Workspaces
179 if m.switchList != nil {
180 list = m.switchList
181 }
182 base := indexOf(list, m.Current())
183 if base < 0 {
184 return cur
185 }
186 h := float64(m.template.Output.H)
187 pos := float64(base) + m.switchView.off
188 offset := func(p []Placement, k int) []Placement {
189 dy := int(math.Round((float64(k) - pos) * h))
190 for i := range p {
191 if !p[i].Hidden {
192 p[i].Rect.Y += dy
193 }
194 }
195 return p
196 }
197 cur = offset(cur, base)
198 for _, k := range []int{int(math.Floor(pos)), int(math.Ceil(pos))} {
199 if k == base || k < 0 || k >= len(list) || !m.has(list[k]) {
200 continue
201 }
202 next := list[k].layoutInto(m.slideBuf)
203 m.slideBuf = next
204 for i := range next {
205 next[i].Focused = false
206 }
207 // Windows of the neighbor are already listed hidden: replace them.
208 if m.slideShown == nil {
209 m.slideShown = map[WindowID]Placement{}
210 }
211 shown := m.slideShown
212 clear(shown)
213 for _, p := range offset(next, k) {
214 shown[p.ID] = p
215 }
216 for i, p := range cur {
217 if v, ok := shown[p.ID]; ok {
218 cur[i] = v
219 }
220 }
221 }
222 return cur
223}