gesture.go RawHistoryFolder

1package core
2
3import (
4	"math"
5	"slices"
6	"time"
7
8	"github.com/bnema/neferwl/internal/ports"
9)
10
11// A three-finger swipe follows the fingers: sideways it scrolls the columns,
12// up or down it slides between workspaces. Each swipe moves one step at
13// most (snapswipe.go): the next column edge or the next workspace. When
14// the fingers lift, a quick swipe goes on to that step, a slow one settles
15// on the closest, and a spring takes the view there (motion.go). Sideways
16// over the shown stash the swipe slides the stash the same way, one window
17// at most. Where the view cannot slide (fixed overflow, a float, a
18// fullscreen window), the swipe runs one focus action instead.
19// In the overview, three fingers use distance-based selection during movement.
20
21const (
22	// swipeDecide is the touchpad distance before the swipe picks its axis
23	// (GNOME Shell's threshold).
24	swipeDecide = 16.0
25	// viewSwipeMovement is the touchpad distance that scrolls the columns by
26	// one usable width.
27	viewSwipeMovement = 1200.0
28	// workspaceSwipeMovement is the touchpad distance that slides one
29	// workspace.
30	workspaceSwipeMovement = 300.0
31	// stashSwipeMovement is the touchpad distance that slides the stash by
32	// one window.
33	stashSwipeMovement = 300.0
34)
35
36type swipeMode uint8
37
38const (
39	swipeUndecided swipeMode = iota
40	swipeColumns
41	swipeWorkspaces
42	swipeDiscrete
43	// swipeNavigation navigates overview cards during a three-finger gesture.
44	swipeNavigation
45	// swipeOverview is a vertical four-finger swipe: up opens the
46	// overview, down closes it on the selection (niri's gesture). With
47	// the overview closed, down shows or hides the stash, and up hides
48	// it when it is shown, else opens the overview.
49	swipeOverview
50	// swipeStash is a sideways swipe over the shown stash: its view
51	// follows the fingers, with a stop on each window, and lands on a
52	// neighbor of the selection at most.
53	swipeStash
54	// swipeDropped ignores the rest of a swipe whose workspace changed.
55	swipeDropped
56)
57
58// swipeGesture is the swipe in progress, on the screen focused when it began.
59type swipeGesture struct {
60	screen     *screen
61	fingers    int
62	mode       swipeMode
63	horizontal bool
64	cx, cy     float64
65	// snap follows the movement along the chosen axis, natural scroll
66	// applied: positive scrolls right or down. Its view is absolute: the
67	// columns' x in pixels or the monitor's fractional workspace index,
68	// so a focus change during the swipe does not move the fingers' view.
69	// A discrete or overview swipe counts steps of -1, 0 or 1.
70	snap snapSwipe
71	// ws is the workspace whose columns scroll, or the active one when a
72	// workspace slide began.
73	ws *Workspace
74	// points are ws's column snap points when a column swipe began: the
75	// swipe ends if the layout no longer has them.
76	points []float64
77	// list is the numbered workspaces when a workspace slide began: the
78	// slide indexes it, so it ends if the list changes.
79	list []*Workspace
80	// ids and home are the stash windows and the selection when a stash
81	// swipe began: it ends if either changes.
82	ids  []WindowID
83	home int
84	// opens is the monitor's overviewOpens when the swipe picked its
85	// mode: a slide the overview opened over is dropped, even once the
86	// overview closed again.
87	opens int
88}
89
90// listChanged reports whether the numbered workspaces or the active one
91// changed since a workspace slide began, or the overview opened over it.
92func (g *swipeGesture) listChanged(m *Monitor) bool {
93	return m.overviewOpens != g.opens || m.shown != nil || m.Workspaces[m.Active] != g.ws || g.horizontal != (g.ws.policy().workspace == horizontalAxis) || !slices.Equal(m.Workspaces, g.list)
94}
95
96// columnsChanged reports whether a column swipe lost its workspace: it is
97// no longer shown or slidable, its snap points moved (an output resize, a
98// panel, a window mapped or resized, other gaps), or the overview opened
99// over it.
100func (g *swipeGesture) columnsChanged(m *Monitor) bool {
101	if m.overviewOpens != g.opens || m.Current() != g.ws || !g.ws.slidable() || g.horizontal != (g.ws.policy().content == horizontalAxis) {
102		return true
103	}
104	if g.ws.policy().wraps {
105		n, spacing := g.ws.cascadeBands()
106		if len(g.points) != n {
107			return true
108		}
109		for i, p := range g.points {
110			if p != float64(i*spacing) {
111				return true
112			}
113		}
114		return false
115	}
116	return !slices.Equal(g.ws.snapPoints(), g.points)
117}
118
119// stashChanged reports whether a stash swipe lost its stash: it is no
120// longer shown or focused, its windows or selection changed, or the
121// overview opened over it.
122func (g *swipeGesture) stashChanged(m *Monitor) bool {
123	w := g.ws
124	return m.overviewOpens != g.opens || m.Current() != w || !w.stashFocused() || w.floatFocus || !w.stashShown() || w.stashAt != g.home ||
125		!slices.EqualFunc(w.Stash, g.ids, func(f Float, id WindowID) bool { return f.ID == id })
126}
127
128// swipeSign turns finger movement into view movement: natural scroll moves
129// the content with the fingers.
130func (c *Core) swipeSign() float64 {
131	if c.cfg.Touchpad.NaturalScroll {
132		return -1
133	}
134	return 1
135}
136
137// swipeBegin starts a swipe. Input streams one swipe at a time; one still
138// running (its end was lost) ends cancelled first: its slide settles back.
139func (c *Core) swipeBegin(b ports.SwipeBegin) bool {
140	changed := false
141	if c.swipe != nil {
142		c.swipeEnd(ports.SwipeEnd{Cancelled: true, Time: b.Time})
143		changed = true
144	}
145	c.swipe = &swipeGesture{screen: c.cur(), fingers: b.Fingers}
146	if b.Fingers == 3 && c.cur().mon.ov.open && !c.overviewKeyboardTaken() {
147		c.scrollStop(ports.PointerAxis{Horizontal: ports.ScrollAxis{Stop: true}})
148		c.swipe.mode = swipeNavigation
149	}
150	return changed
151}
152
153// swipeUpdate moves the swipe; it reports whether the scene changed.
154func (c *Core) swipeUpdate(u ports.SwipeUpdate) bool {
155	g := c.swipe
156	if g == nil {
157		return false
158	}
159	if !c.hasScreen(g.screen) {
160		// Its screen went: let go of what it moved, or that stays shifted.
161		c.dropSwipe()
162		c.swipe = nil
163		return false
164	}
165	if g.mode == swipeNavigation {
166		m := g.screen.mon
167		if !m.ov.open || c.cur() != g.screen || c.overviewKeyboardTaken() {
168			g.mode = swipeDropped
169			return false
170		}
171		now := c.now()
172		shots := c.snapshot(now)
173		changed := m.overviewScroll(ports.PointerAxis{Source: ports.AxisFinger,
174			Horizontal: ports.ScrollAxis{Set: true, Value: u.DX * c.swipeSign()},
175			Vertical:   ports.ScrollAxis{Set: true, Value: u.DY * c.swipeSign()}})
176		if changed {
177			c.keyboard.takeBack()
178			c.transition(shots, now)
179		}
180		return changed
181	}
182	if g.mode == swipeUndecided {
183		g.cx += u.DX
184		g.cy += u.DY
185		if g.cx*g.cx+g.cy*g.cy < swipeDecide*swipeDecide {
186			return false
187		}
188		c.decide(g)
189	}
190	d := u.DY
191	if g.horizontal {
192		d = u.DX
193	}
194	if g.mode == swipeOverview {
195		// Like niri, the overview gesture ignores natural scroll: the
196		// fingers going up open it.
197		g.snap.push(d, u.Time)
198		return false
199	}
200	g.snap.push(d*c.swipeSign(), u.Time)
201	m := g.screen.mon
202	switch g.mode {
203	case swipeColumns:
204		if g.columnsChanged(m) {
205			g.ws.view.stop()
206			g.mode = swipeDropped
207			return true
208		}
209		g.ws.view.off = g.snap.pos() - float64(g.ws.View)
210		return true
211	case swipeWorkspaces:
212		if g.listChanged(m) {
213			m.stopSwitch()
214			g.mode = swipeDropped
215			return true
216		}
217		m.switchView.off = g.snap.pos() - float64(m.Active)
218		return true
219	case swipeStash:
220		if g.stashChanged(m) {
221			g.ws.stashView.stop()
222			g.mode = swipeDropped
223			return true
224		}
225		g.ws.stashView.off = g.snap.pos() - float64(g.ws.stashAt)
226		return true
227	}
228	return false
229}
230
231// decide picks the swipe's axis and what it moves. A running slide stops
232// where it is: the fingers take it from there.
233func (c *Core) decide(g *swipeGesture) {
234	g.horizontal = math.Abs(g.cx) > math.Abs(g.cy)
235	m := g.screen.mon
236	g.opens = m.overviewOpens
237	w := m.Current()
238	switch {
239	case g.fingers == 4 && !g.horizontal:
240		g.mode, g.snap = swipeOverview, newStepSwipe()
241	case g.fingers == 4:
242		// Four fingers sideways do nothing.
243		g.mode = swipeDropped
244	case m.ov.open:
245		// Overview navigation is chosen at begin, never on release.
246		g.mode = swipeDropped
247	case !c.animOn():
248		// With animations off nothing slides: the swipe runs a
249		// focus action when the fingers lift.
250		g.mode, g.snap = swipeDiscrete, newStepSwipe()
251	case g.horizontal && w.stashFocused() && !w.floatFocus && w.stashShown():
252		g.mode, g.ws, g.home = swipeStash, w, w.stashAt
253		g.ids = make([]WindowID, 0, len(w.Stash))
254		for _, f := range w.Stash {
255			g.ids = append(g.ids, f.ID)
256		}
257		w.stashView.motion = motion{}
258		g.snap = newSnapSwipe(float64(w.stashAt)+w.stashView.off, float64(w.stashAt), 1/stashSwipeMovement, indexPoints(len(w.Stash)), workspaceBand)
259	case g.horizontal == (w.policy().content == horizontalAxis) && w.slidable():
260		g.mode, g.ws, g.points = swipeColumns, w, w.snapPoints()
261		w.view.motion = motion{}
262		g.snap = newSnapSwipe(float64(w.View)+w.view.off, float64(w.View), w.swipeScale(), g.points, workspaceBand.scaled(float64(w.policy().content.span(w.Usable))))
263	case g.horizontal == (w.policy().workspace == horizontalAxis) && m.shown == nil:
264		// A landing slide measured in an older list lands at once first.
265		if m.switchList != nil && !slices.Equal(m.switchList, m.Workspaces) {
266			m.stopSwitch()
267		}
268		g.mode, g.ws = swipeWorkspaces, m.Workspaces[m.Active]
269		axis := w.policy().workspace
270		if m.switchView.busy() && m.switchAxis != axis {
271			m.stopSwitch()
272		}
273		m.switchAxis = axis
274		g.list = slices.Clone(m.Workspaces)
275		m.switchView.motion, m.switchList = motion{}, nil
276		g.snap = newSnapSwipe(float64(m.Active)+m.switchView.off, float64(m.Active), 1/workspaceSwipeMovement, indexPoints(len(m.Workspaces)), workspaceBand)
277	default:
278		g.mode, g.snap = swipeDiscrete, newStepSwipe()
279	}
280}
281
282// swipeEnd lands the swipe. shown reports that another workspace came on
283// screen.
284func (c *Core) swipeEnd(e ports.SwipeEnd) (shown bool) {
285	g := c.swipe
286	if g != nil && !c.hasScreen(g.screen) {
287		c.dropSwipe()
288	}
289	c.swipe = nil
290	if g == nil || !c.hasScreen(g.screen) {
291		return false
292	}
293	now := c.now()
294	m := g.screen.mon
295	switch g.mode {
296	case swipeNavigation:
297		c.scrollStop(ports.PointerAxis{Horizontal: ports.ScrollAxis{Stop: true}})
298		return false
299	case swipeColumns:
300		w := g.ws
301		if g.columnsChanged(m) {
302			w.view.stop()
303			return false
304		}
305		shown := float64(w.View) + w.view.off
306		target, velocity := g.snap.end(e.Cancelled, e.Time)
307		if !e.Cancelled {
308			view := int(math.Round(target))
309			focus := w.snapFocus(view, target >= shown)
310			if focus != w.Focus && w.policy().equalCells {
311				w.unmaximize()
312			}
313			w.Focus = focus
314			w.View = view
315			// A column wider than the view aligns as focus moves align it.
316			w.scroll()
317			c.focusScreen = c.screenIndex(g.screen.name())
318			c.keyboard.takeBack()
319		}
320		w.view.off = shown - float64(w.View)
321		if c.animOn() {
322			w.view.motion = c.spring(viewSpring(w.view.off, velocity), now)
323		} else {
324			w.view.stop()
325		}
326	case swipeWorkspaces:
327		if g.listChanged(m) {
328			m.stopSwitch()
329			return false
330		}
331		cur := float64(m.Active) + m.switchView.off
332		target, velocity := g.snap.end(e.Cancelled, e.Time)
333		idx := m.Active
334		if !e.Cancelled {
335			idx = int(math.Round(target))
336		}
337		// Focus may drop the empty workspace left behind and renumber the
338		// list: the slide keeps measuring in the list it began on.
339		off := cur - float64(idx)
340		if idx != m.Active {
341			m.Focus(idx)
342			shown = true
343		}
344		if !e.Cancelled {
345			c.focusScreen = c.screenIndex(g.screen.name())
346			c.keyboard.takeBack()
347		}
348		m.switchView.off, m.switchList = off, g.list
349		if c.animOn() {
350			m.switchView.motion = c.spring(m.switchSpring(off, velocity), now)
351		} else {
352			m.stopSwitch()
353		}
354	case swipeStash:
355		w := g.ws
356		if g.stashChanged(m) {
357			w.stashView.stop()
358			return false
359		}
360		drawn := float64(w.stashAt) + w.stashView.off
361		target, velocity := g.snap.end(e.Cancelled, e.Time)
362		dir := 0
363		if !e.Cancelled {
364			dir = int(math.Round(target)) - w.stashAt
365		}
366		if c.cur() == g.screen && dir != 0 {
367			// The landing is the focus action's: the selection, the focus
368			// and the veils move as a bind moves them.
369			a := ActionFocusColumnRight
370			if dir < 0 {
371				a = ActionFocusColumnLeft
372			}
373			c.keyboard.takeBack()
374			c.applyAction(a)
375		}
376		w.stashView.off = drawn - float64(w.stashAt)
377		if c.animOn() {
378			w.stashView.motion = c.spring(w.stashSpring(w.stashView.off, velocity), now)
379		} else {
380			w.stashView.stop()
381		}
382		// The stash never brings another workspace on screen.
383		return false
384	case swipeOverview:
385		step := g.snap.step(e.Cancelled, e.Time)
386		if step == 0 {
387			return false
388		}
389		if !m.ov.open && (step > 0 || m.Current().stashShown()) {
390			return c.swipeStash(g, now)
391		}
392		if (step < 0) == m.ov.open {
393			return false
394		}
395		before := m.Current()
396		shots := c.snapshot(now)
397		m.ToggleOverview()
398		c.focusScreen = c.screenIndex(g.screen.name())
399		c.keyboard.takeBack()
400		c.transition(shots, now)
401		return m.Current() != before
402	case swipeDiscrete:
403		step := g.snap.step(e.Cancelled, e.Time)
404		if step == 0 {
405			return false
406		}
407		w := g.screen.mon.Current()
408		p := w.policy()
409		workspace := g.horizontal == (p.workspace == horizontalAxis)
410		a := ActionFocusColumnRight
411		if step < 0 {
412			a = ActionFocusColumnLeft
413		}
414		if workspace {
415			a = ActionFocusWorkspaceDown
416			if step < 0 {
417				a = ActionFocusWorkspaceUp
418			}
419		} else if p.wraps {
420			// A content swipe cannot escape the last band to another workspace.
421			if w.onFloat() || w.fullscreen != 0 || w.bandNeighbor(step) < 0 {
422				return false
423			}
424		}
425		// Focus actions act on the focused screen: the swipe's, unless the
426		// pointer took the focus to another output meanwhile.
427		if c.cur() != g.screen {
428			return false
429		}
430		if c.cur().mon.ov.open {
431			return false
432		}
433		before := c.cur().mon.Current()
434		shots := c.snapshot(now)
435		c.keyboard.takeBack()
436		if p.wraps && !workspace {
437			w.focusBand(step)
438		} else {
439			c.applyAction(a)
440		}
441		c.transition(shots, now)
442		return c.cur().mon.Current() != before
443	}
444	return shown
445}
446
447// swipeStash runs toggle-stash-visible for a four-finger swipe down, or up
448// while the stash is shown, with the overview closed, on the swipe's screen
449// unless the pointer took the focus to another output meanwhile. It reports
450// whether the stash showed or hid: an empty stash stays as it is.
451func (c *Core) swipeStash(g *swipeGesture, now time.Time) bool {
452	if c.cur() != g.screen {
453		return false
454	}
455	w := g.screen.mon.Current()
456	hidden, over := w.stashHidden, w.stashOver
457	shots := c.snapshot(now)
458	c.keyboard.takeBack()
459	c.applyAction(ActionToggleStashVisible)
460	c.transition(shots, now)
461	return w.stashHidden != hidden || w.stashOver != over
462}
463
464// swipedWorkspace is the workspace on the swipe's screen, or nil without
465// a swipe.
466func (c *Core) swipedWorkspace() *Workspace {
467	if c.swipe == nil {
468		return nil
469	}
470	return c.swipe.screen.mon.Current()
471}
472
473// dropSwipe lets go of the swipe in progress: a bind changed the workspace
474// on its screen. Its slide stops where the bind left the view; the rest of
475// the swipe is ignored, even if a later bind shows its workspace again.
476func (c *Core) dropSwipe() {
477	g := c.swipe
478	if g == nil {
479		return
480	}
481	switch g.mode {
482	case swipeColumns:
483		g.ws.view.stop()
484	case swipeWorkspaces:
485		g.screen.mon.stopSwitch()
486	case swipeStash:
487		g.ws.stashView.stop()
488	}
489	g.mode = swipeDropped
490}
491
492func (c *Core) hasScreen(s *screen) bool {
493	for _, v := range c.screens {
494		if v == s {
495			return true
496		}
497	}
498	return false
499}
500
501// animOn reports whether transitions run (animations = on).
502func (c *Core) animOn() bool { return c.cfg.Animations.On }
503
504// spring starts s at now, stretched by animations.speed (Slowdown). Every motion
505// core creates goes through it, except a retarget, which keeps the slowdown
506// of the motion it replaces.
507func (c *Core) spring(s spring, now time.Time) motion {
508	return newMotion(s, now, c.cfg.Animations.Slowdown)
509}
510
511// stopAnimations settles every running spring where it is going and stops
512// the frame timer: a session lock or a switch to animations = off. A swipe
513// in progress keeps its fingers' view: only running springs stop. Per-window
514// rect motions stop too.
515func (c *Core) stopAnimations() {
516	for _, sc := range c.screens {
517		sc.stopAnimations()
518	}
519	c.stopFrame()
520}
521
522// stopAnimations settles the screen's running springs where they are going.
523func (s *screen) stopAnimations() {
524	if s.mon.switchView.motion.on {
525		s.mon.stopSwitch()
526	}
527	for w := range s.mon.all() {
528		if w.view.motion.on {
529			w.view.stop()
530		}
531		if w.stashView.motion.on {
532			w.stashView.stop()
533		}
534	}
535	s.stopRects()
536}
537
538// animate moves the running springs to now; settled ones stop. A non-nil only
539// limits it to that screen (its own page flip); nil moves every screen.
540func (c *Core) animate(now time.Time, only *screen) {
541	for _, sc := range c.screens {
542		if only != nil && sc != only {
543			continue
544		}
545		m := sc.mon
546		if m.switchView.motion.on {
547			m.switchView.step(now)
548			if !m.switchView.motion.on {
549				m.switchList = nil
550			}
551		}
552		for w := range m.all() {
553			w.view.step(now)
554			w.stashView.step(now)
555		}
556		for id, rm := range sc.rects {
557			rm.advance(now)
558			if rm.on() {
559				sc.rects[id] = rm
560			} else {
561				delete(sc.rects, id)
562			}
563		}
564	}
565}
566
567// sliding reports whether a spring runs on the named output.
568func (c *Core) sliding(output string) bool {
569	i := c.screenIndex(output)
570	return i >= 0 && (c.screens[i].springing() || c.pulsing(c.screens[i]))
571}
572
573// animating reports whether a spring or the focus pulse runs on any output.
574func (c *Core) animating() bool {
575	if c.pulse.id != 0 {
576		return true
577	}
578	for _, sc := range c.screens {
579		if sc.springing() {
580			return true
581		}
582	}
583	return false
584}
585
586func (m *Monitor) springing() bool {
587	if m.switchView.motion.on {
588		return true
589	}
590	for w := range m.all() {
591		if w.view.motion.on || w.stashView.motion.on {
592			return true
593		}
594	}
595	return false
596}
597
598// frameFallback is how long core waits for a page flip before it moves a
599// running slide on its own (an output that stopped flipping).
600func (c *Core) frameFallback() time.Duration {
601	refresh := 60000
602	for _, sc := range c.screens {
603		if r := sc.info.RefreshMilli; r > 0 && (sc.springing() || c.pulsing(sc)) {
604			refresh = min(refresh, r)
605		}
606	}
607	return 2 * time.Second * 1000 / time.Duration(refresh)
608}
609
610// armFrame starts the fallback timer of a running slide. The timer is made
611// on the first arm and reset afterwards (Go 1.23 timers deliver nothing
612// stale after a Stop or Reset), so a frame costs no allocation.
613func (c *Core) armFrame() {
614	d := c.frameFallback()
615	if c.frameTimer == nil {
616		c.frameTimer = newPortTimer(c.opts.Clock, d)
617	} else {
618		c.frameTimer.Stop()
619		c.frameTimer.Reset(d)
620	}
621	c.frameC = c.frameTimer.C()
622}
623
624func (c *Core) stopFrame() {
625	if c.frameC != nil {
626		c.frameTimer.Stop()
627	}
628	c.frameC = nil
629}

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 26

Tags 10

↑↓ moveenter openesc close