v0.3.0 cf370a0

drag.go RawHistoryFolder

1package core
2
3import (
4	"context"
5	"slices"
6	"time"
7
8	"github.com/bnema/neferwl/internal/ports"
9)
10
11// Pointer drag: cmd + left button moves the window under the pointer,
12// cmd + right button resizes a float; xdg_toplevel.move/resize start the
13// same drag with the held button. A float follows the pointer. A tile
14// stays in place until the button is released, and NeferWL draws where
15// it lands (Scene.DropHints):
16//
17//   - top or bottom quarter of a window: stack above or below it;
18//   - middle of a window: swap the columns (a stacked window becomes a
19//     new column there);
20//   - scroll overflow, the gap between two columns: a new column there.
21//
22// The client gets no pointer events during the drag; Escape cancels it.
23
24const (
25	btnLeft  = 0x110
26	btnRight = 0x111
27	// dropBand is the share of a window's height, top and bottom, that
28	// stacks above or below it.
29	dropBandDiv = 4
30	// gapPad is the least half-width of a gap's insert zone, logical px.
31	gapPad = 12
32	// edgeScroll is the distance from the usable edge, logical px, that
33	// scrolls a scroll-overflow workspace during a tile drag, at most
34	// once per edgeScrollInterval.
35	edgeScroll         = 32
36	edgeScrollInterval = 250 * time.Millisecond
37)
38
39type dropKind int
40
41const (
42	dropNone dropKind = iota
43	dropAbove
44	dropBelow
45	dropSwap
46	dropInsert
47)
48
49// dropTarget is where a dragged tile lands. anchor is the hovered window
50// (above, below, swap) or a window of the column the new one goes before
51// (insert; 0 is the end).
52type dropTarget struct {
53	kind   dropKind
54	screen *screen
55	ws     *Workspace
56	anchor WindowID
57	hints  []Rect
58}
59
60type dragState struct {
61	id     WindowID
62	button uint32
63	resize bool
64	edges  ports.ResizeEdges
65	// start is the cursor at the press, global; from is the float as it
66	// was, restored by Escape; rect is its place then.
67	start  [2]float64
68	from   Float
69	rect   Rect
70	float  bool
71	target dropTarget
72	// scrolled is the time of the last edge scroll.
73	scrolled time.Duration
74}
75
76// draggable returns the screen and workspace of a window a drag may
77// take: a tile or a native or free float of the workspace on screen,
78// not fullscreen, outside the overview.
79func (c *Core) draggable(id WindowID) (*screen, *Workspace, bool) {
80	s, w := c.screenOf(id)
81	if s == nil || w != s.mon.Current() || s.mon.ov.open || id == w.fullscreen || w.cover() != 0 {
82		return nil, nil, false
83	}
84	if w.floatIndex(id) < 0 && w.columnOf(id) < 0 {
85		return nil, nil, false
86	}
87	return s, w, true
88}
89
90// startDrag starts a cmd + button drag on the window under the pointer.
91// It reports whether it did: the press is then not forwarded.
92func (c *Core) startDrag(ctx context.Context, v ports.PointerButton) (bool, error) {
93	if !v.Pressed || c.drag != nil || c.held() != 0 || c.grab != 0 || c.cmdMod == 0 || c.mods&c.cmdMod == 0 {
94		return false, nil
95	}
96	if v.Button != btnLeft && v.Button != btnRight {
97		return false, nil
98	}
99	id := c.pointer
100	if id == 0 || c.keyboard.inhibited(c.keyboardFocus()) {
101		return false, nil
102	}
103	_, w, ok := c.draggable(id)
104	// Tiles only move; resizing them with the pointer is not supported.
105	if !ok || v.Button == btnRight && w.floatIndex(id) < 0 {
106		return false, nil
107	}
108	var edges ports.ResizeEdges
109	if v.Button == btnRight {
110		edges = ports.ResizeRight | ports.ResizeBottom
111	}
112	return true, c.beginDrag(ctx, id, v.Button, v.Button == btnRight, edges)
113}
114
115// held is the number of held buttons a client saw pressed: swallowed
116// ones stay in c.buttons only for the security gate.
117func (c *Core) held() int {
118	n := 0
119	for b := range c.buttons {
120		if !c.swallow[b] {
121			n++
122		}
123	}
124	return n
125}
126
127// clientDrag starts a drag for xdg_toplevel.move/resize: the window must
128// hold the pointer grab of a held button.
129func (c *Core) clientDrag(ctx context.Context, v ports.WindowMoveRequest) error {
130	// The adapter checked the serial of the last press: that button drags.
131	button := c.lastButton
132	if c.drag != nil || c.grab != v.ID || !c.buttons[button] || c.swallow[button] {
133		return nil
134	}
135	_, w, ok := c.draggable(v.ID)
136	if !ok || v.Resize && (w.floatIndex(v.ID) < 0 || v.Edges == 0) {
137		return nil
138	}
139	// Other held buttons release without a client to tell.
140	for b := range c.buttons {
141		if b != button {
142			c.swallow[b] = true
143		}
144	}
145	return c.beginDrag(ctx, v.ID, button, v.Resize, v.Edges)
146}
147
148func (c *Core) beginDrag(ctx context.Context, id WindowID, button uint32, resize bool, edges ports.ResizeEdges) error {
149	if err := c.dismissGrabs(ctx, id); err != nil {
150		return err
151	}
152	s, w, ok := c.draggable(id)
153	if !ok {
154		return nil
155	}
156	// Click raises a float above the columns: Escape keeps that.
157	w.Click(id)
158	d := &dragState{id: id, button: button, resize: resize, edges: edges, start: [2]float64{c.cursorX, c.cursorY}}
159	if i := w.floatIndex(id); i >= 0 {
160		d.float, d.from, d.rect = true, w.Floats[i], w.floatRect(w.Floats[i])
161	}
162	c.drag = d
163	c.buttons[button] = true
164	c.focusScreen = c.screenIndex(s.name())
165	c.grab = 0
166	if c.pointer != 0 {
167		c.pointer = 0
168		if err := c.command(ctx, ports.PointerFocus{}); err != nil {
169			return err
170		}
171	}
172	return c.publish(ctx)
173}
174
175// dragMotion follows the cursor: a float moves or resizes, a tile shows
176// where it would land.
177func (c *Core) dragMotion(ctx context.Context, at time.Duration) error {
178	d := c.drag
179	_, w, ok := c.draggable(d.id)
180	if !ok {
181		c.abortDrag()
182		return c.publish(ctx)
183	}
184	dx, dy := int(c.cursorX-d.start[0]), int(c.cursorY-d.start[1])
185	if d.float {
186		i := w.floatIndex(d.id)
187		if i < 0 {
188			c.abortDrag()
189			return c.publish(ctx)
190		}
191		f := &w.Floats[i]
192		w.makeFree(f)
193		if d.resize {
194			r := resized(d.rect, d.edges, dx, dy, 2*w.border+freeMinSize, w.Usable)
195			f.W, f.H = r.W-2*w.border, r.H-2*w.border
196			w.moveFree(f, r, 0, 0)
197		} else {
198			w.moveFree(f, d.rect, dx, dy)
199		}
200		return c.publish(ctx)
201	}
202	scrolled := c.edgeScroll(at)
203	t := c.dropAt(d.id, c.cursorX, c.cursorY)
204	if !scrolled && t.kind == d.target.kind && t.anchor == d.target.anchor && t.ws == d.target.ws && slices.Equal(t.hints, d.target.hints) {
205		return nil
206	}
207	d.target = t
208	return c.publish(ctx)
209}
210
211// resized is r with the edges moved by dx, dy, at least min wide and
212// high, inside area u.
213func resized(r Rect, edges ports.ResizeEdges, dx, dy, least int, u Rect) Rect {
214	left, top, right, bottom := r.X, r.Y, r.X+r.W, r.Y+r.H
215	if edges&ports.ResizeLeft != 0 {
216		left = min(max(left+dx, u.X), right-least)
217	}
218	if edges&ports.ResizeRight != 0 {
219		right = max(min(right+dx, u.X+u.W), left+least)
220	}
221	if edges&ports.ResizeTop != 0 {
222		top = min(max(top+dy, u.Y), bottom-least)
223	}
224	if edges&ports.ResizeBottom != 0 {
225		bottom = max(min(bottom+dy, u.Y+u.H), top+least)
226	}
227	return Rect{X: left, Y: top, W: right - left, H: bottom - top}
228}
229
230// endDrag drops the window on release: a tile lands on its target.
231func (c *Core) endDrag(ctx context.Context) error {
232	d := c.drag
233	c.drag = nil
234	if !d.float {
235		if t := c.dropAt(d.id, c.cursorX, c.cursorY); t.kind != dropNone {
236			c.drop(d.id, t)
237			// Slots and guests follow the windows, as after a bind.
238			if err := c.workspaceVisible(ctx, false); err != nil {
239				return err
240			}
241		}
242	}
243	if err := c.publish(ctx); err != nil {
244		return err
245	}
246	return c.rehit(ctx)
247}
248
249// cancelDrag puts a dragged float back; a tile never moved.
250func (c *Core) cancelDrag() {
251	d := c.drag
252	if d.float {
253		if _, w := c.screenOf(d.id); w != nil {
254			if i := w.floatIndex(d.id); i >= 0 {
255				w.Floats[i] = d.from
256			}
257		}
258	}
259	c.abortDrag()
260}
261
262// abortDrag ends the drag where it is; the held button's release is
263// dropped.
264func (c *Core) abortDrag() {
265	if c.drag != nil {
266		c.swallow[c.drag.button] = true
267		c.drag = nil
268	}
269}
270
271// dropAt is the drop target under the global point x, y.
272func (c *Core) dropAt(id WindowID, x, y float64) dropTarget {
273	i := c.screenAt(x, y)
274	if i < 0 {
275		return dropTarget{}
276	}
277	sc := c.screens[i]
278	m := sc.mon
279	lx, ly := int(x)-sc.x, int(y)-sc.y
280	w := m.Current()
281	if m.ov.open || w.pinned() || !m.frameHas(float64(lx), float64(ly)) {
282		return dropTarget{}
283	}
284	t := w.dropIn(id, lx, ly, max(4*c.cfg.Border.Width, 4))
285	t.screen, t.ws = sc, w
286	return t
287}
288
289// visibleColumn is a column on screen and the bounds of its windows.
290type visibleColumn struct {
291	index int
292	rect  Rect
293}
294
295// dropIn resolves the drop of window id at the output-local point x, y,
296// hints t logical px thick.
297func (w *Workspace) dropIn(id WindowID, x, y, t int) dropTarget {
298	layout := w.Layout()
299	for _, p := range slices.Backward(layout) {
300		// Floats and the stash are over the tiles: nothing lands there.
301		if p.Floating && !p.Hidden && !p.Below && p.ID != id && contains(p.Rect, x, y) {
302			return dropTarget{}
303		}
304	}
305	src, srcRow := w.columnOf(id), -1
306	if src >= 0 {
307		srcRow = slices.Index(w.Columns[src].Windows, id)
308	}
309	lone := src >= 0 && len(w.Columns[src].Windows) == 1
310	var cols []visibleColumn
311	for _, p := range layout {
312		if p.Floating || p.Hidden || !p.Rect.Overlaps(w.Output) {
313			continue
314		}
315		ci := w.columnOf(p.ID)
316		if ci < 0 {
317			continue
318		}
319		if n := len(cols); n > 0 && cols[n-1].index == ci {
320			cols[n-1].rect = union(cols[n-1].rect, p.Rect)
321		} else {
322			cols = append(cols, visibleColumn{index: ci, rect: p.Rect})
323		}
324	}
325	if len(w.Columns) == 0 || src < 0 && len(cols) == 0 {
326		if !contains(w.Usable, x, y) {
327			return dropTarget{}
328		}
329		g := w.gap()
330		r := Rect{X: w.Usable.X + g, Y: w.Usable.Y + g, W: max(w.Usable.W-2*g, 0), H: max(w.Usable.H-2*g, 0)}
331		return dropTarget{kind: dropInsert, hints: outline(r, t)}
332	}
333	if w.Overflow != OverflowFixed {
334		if tgt, ok := w.gapDrop(cols, id, src, lone, x, y, t); ok {
335			return tgt
336		}
337	}
338	for _, p := range layout {
339		if p.Floating || p.Hidden || !contains(p.Rect, x, y) {
340			continue
341		}
342		ci := w.columnOf(p.ID)
343		if ci < 0 {
344			continue
345		}
346		r := p.Rect
347		band := max(r.H/dropBandDiv, 1)
348		row := slices.Index(w.Columns[ci].Windows, p.ID)
349		switch {
350		case y < r.Y+band:
351			// Above the window just below it, or above itself: no move.
352			if p.ID == id || ci == src && row == srcRow+1 {
353				return dropTarget{}
354			}
355			return dropTarget{kind: dropAbove, anchor: p.ID, hints: []Rect{{X: r.X, Y: r.Y, W: r.W, H: min(t, r.H)}}}
356		case y >= r.Y+r.H-band:
357			if p.ID == id || ci == src && row == srcRow-1 {
358				return dropTarget{}
359			}
360			return dropTarget{kind: dropBelow, anchor: p.ID, hints: []Rect{{X: r.X, Y: r.Y + r.H - min(t, r.H), W: r.W, H: min(t, r.H)}}}
361		default:
362			if ci == src {
363				return dropTarget{}
364			}
365			for _, vc := range cols {
366				if vc.index == ci {
367					return dropTarget{kind: dropSwap, anchor: p.ID, hints: outline(vc.rect, t)}
368				}
369			}
370			return dropTarget{}
371		}
372	}
373	return dropTarget{}
374}
375
376// gapDrop is the insert zone of scroll overflow: the gaps between the
377// columns on screen and beside the outer ones.
378func (w *Workspace) gapDrop(cols []visibleColumn, id WindowID, src int, lone bool, x, y, t int) (dropTarget, bool) {
379	pad := max(w.gap()/2, gapPad)
380	for i := 0; i <= len(cols); i++ {
381		var mid, at int
382		var ref Rect
383		switch {
384		case i == 0:
385			ref = cols[0].rect
386			mid, at = ref.X-w.gap()/2, cols[0].index
387		case i == len(cols):
388			ref = cols[i-1].rect
389			mid, at = ref.X+ref.W+w.gap()/2, cols[i-1].index+1
390		default:
391			a, b := cols[i-1].rect, cols[i].rect
392			ref = union(a, b)
393			mid, at = (a.X+a.W+b.X)/2, cols[i].index
394		}
395		if x < mid-pad || x >= mid+pad || y < ref.Y || y >= ref.Y+ref.H {
396			continue
397		}
398		// A lone column next to its own place does not move.
399		if lone && (at == src || at == src+1) {
400			return dropTarget{}, true
401		}
402		var anchor WindowID
403		if at < len(w.Columns) {
404			anchor = w.Columns[at].Windows[0]
405			if anchor == id {
406				anchor = w.Columns[at].Windows[1]
407			}
408		}
409		hint := Rect{X: mid - t/2, Y: ref.Y, W: t, H: ref.H}
410		return dropTarget{kind: dropInsert, anchor: anchor, hints: []Rect{hint}}, true
411	}
412	return dropTarget{}, false
413}
414
415// contains reports whether r holds the point x, y.
416func contains(r Rect, x, y int) bool {
417	return x >= r.X && x < r.X+r.W && y >= r.Y && y < r.Y+r.H
418}
419
420// union is the smallest rect holding a and b.
421func union(a, b Rect) Rect {
422	x, y := min(a.X, b.X), min(a.Y, b.Y)
423	return Rect{X: x, Y: y, W: max(a.X+a.W, b.X+b.W) - x, H: max(a.Y+a.H, b.Y+b.H) - y}
424}
425
426// outline is the four sides of r, t thick, inside it.
427func outline(r Rect, t int) []Rect {
428	t = min(t, r.W/2, r.H/2)
429	if t <= 0 {
430		return nil
431	}
432	return []Rect{
433		{X: r.X, Y: r.Y, W: r.W, H: t},
434		{X: r.X, Y: r.Y + r.H - t, W: r.W, H: t},
435		{X: r.X, Y: r.Y + t, W: t, H: r.H - 2*t},
436		{X: r.X + r.W - t, Y: r.Y + t, W: t, H: r.H - 2*t},
437	}
438}
439
440// drop moves tile id to target t, possibly on another workspace or
441// screen; focus follows it.
442func (c *Core) drop(id WindowID, t dropTarget) {
443	from, src := c.screenOf(id)
444	if from == nil || t.ws == nil || src.columnOf(id) < 0 {
445		return
446	}
447	w := t.ws
448	ci := src.columnOf(id)
449	lone := len(src.Columns[ci].Windows) == 1
450	if t.kind == dropSwap && w == src && lone {
451		to := w.columnOf(t.anchor)
452		// Their fields move with them, as MoveColumn.
453		w.Columns[ci], w.Columns[to] = w.Columns[to], w.Columns[ci]
454		w.FocusID(id)
455	} else {
456		width := Width{}
457		if lone {
458			width = src.Columns[ci].Width
459		}
460		srcIndex := ci
461		src.dropSlotOf(id)
462		src.RemoveWindow(id)
463		col := Column{Windows: []WindowID{id}, Width: width}
464		switch t.kind {
465		case dropAbove, dropBelow:
466			ai := w.columnOf(t.anchor)
467			if ai < 0 {
468				w.receive(col, -1)
469				break
470			}
471			tc := &w.Columns[ai]
472			row := slices.Index(tc.Windows, t.anchor)
473			if t.kind == dropBelow {
474				row++
475			}
476			if row == 0 {
477				// The slot follows the first window.
478				tc.Slot = 0
479			}
480			tc.Windows = slices.Insert(tc.Windows, row, id)
481			tc.Shares = nil
482			w.Activate(id)
483		default:
484			at, anchorCol := len(w.Columns), -1
485			if t.anchor != 0 {
486				if ai := w.columnOf(t.anchor); ai >= 0 {
487					at, anchorCol = ai, ai
488					// A stacked window dropped on a column right of its
489					// own lands after it.
490					if t.kind == dropSwap && w == src && ai >= srcIndex {
491						at = ai + 1
492					}
493				}
494			}
495			if w.full() {
496				// No room for a column: it joins the outlined one.
497				i := min(at, len(w.Columns)-1)
498				if anchorCol >= 0 {
499					i = anchorCol
500				}
501				w.Columns[i].Windows = append(w.Columns[i].Windows, id)
502				w.Columns[i].Shares = nil
503				w.Activate(id)
504			} else {
505				w.receive(col, at)
506			}
507		}
508	}
509	from.mon.normalize()
510	t.screen.mon.normalize()
511	c.focusScreen = c.screenIndex(t.screen.name())
512}
513
514// edgeScroll scrolls a scroll-overflow workspace by one column when a
515// tile drag moves the pointer against its usable edge. It reports whether
516// the view moved.
517func (c *Core) edgeScroll(t time.Duration) bool {
518	i := c.screenAt(c.cursorX, c.cursorY)
519	if i < 0 || t-c.drag.scrolled < edgeScrollInterval && c.drag.scrolled != 0 {
520		return false
521	}
522	sc := c.screens[i]
523	w := sc.mon.Current()
524	if w.Overflow == OverflowFixed || len(w.Columns) == 0 || w.pinned() {
525		return false
526	}
527	lx := int(c.cursorX) - sc.x
528	dir := 0
529	switch {
530	case lx < w.Usable.X+edgeScroll:
531		dir = -1
532	case lx >= w.Usable.X+w.Usable.W-edgeScroll:
533		dir = 1
534	default:
535		return false
536	}
537	if !w.scrollBy(dir) {
538		return false
539	}
540	c.drag.scrolled = max(t, 1)
541	return true
542}
543
544// scrollBy moves the view one column toward dir, within the row; the
545// focus stays. It reports whether the view moved.
546func (w *Workspace) scrollBy(dir int) bool {
547	g := w.gap()
548	n := len(w.Columns)
549	maxView := max(w.columnX(n-1)+w.columnWidth(n-1)+g-(w.Usable.X+w.Usable.W), 0)
550	view := w.ViewX
551	if dir < 0 {
552		// The first column not fully on screen at the left.
553		for i := n - 1; i >= 0; i-- {
554			if w.columnX(i)-view < w.Usable.X+g {
555				view = w.columnX(i) - w.Usable.X - g
556				break
557			}
558		}
559	} else {
560		for i := range n {
561			if w.columnX(i)+w.columnWidth(i)-view > w.Usable.X+w.Usable.W-g {
562				view = w.columnX(i) + w.columnWidth(i) + g - w.Usable.X - w.Usable.W
563				break
564			}
565		}
566	}
567	view = min(max(view, 0), maxView)
568	if view == w.ViewX {
569		return false
570	}
571	w.stopSlide()
572	w.ViewX = view
573	return true
574}
575
576// makeFree turns native float f into a free float where it is now.
577func (w *Workspace) makeFree(f *Float) {
578	if f.free {
579		return
580	}
581	r := w.floatRect(*f)
582	if f.W <= 0 || f.H <= 0 {
583		f.W, f.H = max(r.W-2*w.border, 1), max(r.H-2*w.border, 1)
584	}
585	f.free = true
586	w.moveFree(f, r, 0, 0)
587}

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