1package core
2
3import (
4 "strconv"
5 "strings"
6
7 "github.com/bnema/neferwl/internal/ports"
8)
9
10type Action string
11
12const (
13 ActionSpawnTerminal Action = "spawn-terminal"
14 ActionFocusColumnLeft Action = "focus-column-left"
15 ActionFocusColumnRight Action = "focus-column-right"
16 ActionFocusWindowUp Action = "focus-window-up"
17 ActionFocusWindowDown Action = "focus-window-down"
18 ActionMoveColumnLeft Action = "move-column-left"
19 ActionMoveColumnRight Action = "move-column-right"
20 ActionCycleColumnWidth Action = "cycle-column-width"
21 ActionMaximizeColumn Action = "maximize-column"
22 ActionToggleFullscreen Action = "toggle-fullscreen"
23 ActionToggleWindowStash Action = "toggle-window-stash"
24 ActionToggleStashVisible Action = "toggle-stash-visible"
25 ActionToggleOverview Action = "toggle-overview"
26 ActionCloseWindow Action = "close-window"
27 ActionQuit Action = "quit"
28 // Workspaces stack vertically; prev/next stop at the ends.
29 ActionFocusWorkspacePrev Action = "focus-workspace-prev"
30 ActionFocusWorkspaceNext Action = "focus-workspace-next"
31 // Move the focused column, or only the focused window, to the previous
32 // or next workspace.
33 ActionMoveColumnToWorkspacePrev Action = "move-column-to-workspace-prev"
34 ActionMoveColumnToWorkspaceNext Action = "move-column-to-workspace-next"
35 ActionMoveWindowToWorkspacePrev Action = "move-window-to-workspace-prev"
36 ActionMoveWindowToWorkspaceNext Action = "move-window-to-workspace-next"
37 // Monitors act on the neighbor in that direction of the global layout
38 // (ADR 011).
39 ActionFocusMonitorLeft Action = "focus-monitor-left"
40 ActionFocusMonitorRight Action = "focus-monitor-right"
41 ActionFocusMonitorUp Action = "focus-monitor-up"
42 ActionFocusMonitorDown Action = "focus-monitor-down"
43 ActionMoveWorkspaceLeft Action = "move-workspace-to-monitor-left"
44 ActionMoveWorkspaceRight Action = "move-workspace-to-monitor-right"
45 ActionMoveWorkspaceToMonitorUp Action = "move-workspace-to-monitor-up"
46 ActionMoveWorkspaceToMonitorDown Action = "move-workspace-to-monitor-down"
47 // Scale steps through the clean scales of the output (see CleanScales).
48 ActionScaleUp Action = "scale-up"
49 ActionScaleDown Action = "scale-down"
50 // Consume or expel the focused window (ADR 016).
51 ActionConsumeOrExpelLeft Action = "consume-or-expel-window-left"
52 ActionConsumeOrExpelRight Action = "consume-or-expel-window-right"
53 // Move the focused window up or down inside its column.
54 ActionMoveWindowUp Action = "move-window-up"
55 ActionMoveWindowDown Action = "move-window-down"
56 // Swap the active numbered workspace with its neighbor.
57 ActionMoveWorkspacePrev Action = "move-workspace-prev"
58 ActionMoveWorkspaceNext Action = "move-workspace-next"
59 // Turn the focused tile into a free floating window, or back.
60 ActionToggleFloating Action = "toggle-floating"
61 // Switch to the column used before this one, further back while the
62 // command key is held (switcher.go).
63 ActionSwitchColumnNext Action = "switch-column-next"
64 ActionSwitchColumnPrev Action = "switch-column-prev"
65)
66
67// monitorDirection returns the direction of the neighbor screen targeted by
68// a focus-monitor or move-workspace-to-monitor action, 0 for other actions.
69func monitorDirection(a Action) direction {
70 switch a {
71 case ActionFocusMonitorLeft, ActionMoveWorkspaceLeft:
72 return dirLeft
73 case ActionFocusMonitorRight, ActionMoveWorkspaceRight:
74 return dirRight
75 case ActionFocusMonitorUp, ActionMoveWorkspaceToMonitorUp:
76 return dirUp
77 case ActionFocusMonitorDown, ActionMoveWorkspaceToMonitorDown:
78 return dirDown
79 }
80 return 0
81}
82
83// Resize axes of ResizeArg.
84const (
85 ResizeWidth = 0
86 ResizeHeight = 1
87)
88
89var resizePrefixes = [...]string{
90 ResizeWidth: "set-column-width ",
91 ResizeHeight: "set-window-height ",
92}
93
94// ResizeArg parses "set-column-width +N" (axis ResizeWidth) and
95// "set-window-height -N%" (axis ResizeHeight). The sign is required and the
96// unit depends on the axis: step is in cells for a width (N from 1 to 16,
97// no %), in percent for a height (N from 1 to 100, with %).
98func ResizeArg(a Action) (axis, step int, ok bool) {
99 for i, prefix := range resizePrefixes {
100 rest, found := strings.CutPrefix(string(a), prefix)
101 if !found {
102 continue
103 }
104 rest = strings.TrimSpace(rest)
105 if i == ResizeWidth {
106 if p, ok := parseResizeStep(rest, maxCells); ok {
107 return i, p, true
108 }
109 return 0, 0, false
110 }
111 if num, found := strings.CutSuffix(rest, "%"); found {
112 if p, ok := parseResizeStep(num, 100); ok {
113 return i, p, true
114 }
115 }
116 return 0, 0, false
117 }
118 return 0, 0, false
119}
120
121// parseResizeStep parses "+N" or "-N" with N from 1 to limit.
122func parseResizeStep(s string, limit int) (int, bool) {
123 if len(s) < 2 || (s[0] != '+' && s[0] != '-') {
124 return 0, false
125 }
126 n, err := strconv.Atoi(s[1:])
127 if err != nil || n < 1 || n > limit || s[1] == '+' || s[1] == '-' {
128 return 0, false
129 }
130 if s[0] == '-' {
131 n = -n
132 }
133 return n, true
134}
135
136// WorkspaceOp is what a numbered workspace action does.
137type WorkspaceOp int
138
139const (
140 FocusWorkspace WorkspaceOp = iota
141 MoveColumnToWorkspace
142 MoveWindowToWorkspace
143)
144
145var workspacePrefixes = [...]string{
146 FocusWorkspace: "focus-workspace ",
147 MoveColumnToWorkspace: "move-column-to-workspace ",
148 MoveWindowToWorkspace: "move-window-to-workspace ",
149}
150
151// WorkspaceArg parses "focus-workspace N", "move-column-to-workspace N" and
152// "move-window-to-workspace N" (N from 1).
153func WorkspaceArg(a Action) (n int, op WorkspaceOp, ok bool) {
154 for i, prefix := range workspacePrefixes {
155 rest, found := strings.CutPrefix(string(a), prefix)
156 if !found {
157 continue
158 }
159 n, err := strconv.Atoi(strings.TrimSpace(rest))
160 if err != nil || n < 1 || n > 99 {
161 return 0, 0, false
162 }
163 return n, WorkspaceOp(i), true
164 }
165 return 0, 0, false
166}
167
168// namedPrefix toggles a named workspace: "workspace dev".
169const namedPrefix = "workspace "
170
171// NamedArg returns the name of a "workspace <name>" action.
172func NamedArg(a Action) (string, bool) {
173 rest, ok := strings.CutPrefix(string(a), namedPrefix)
174 name := strings.TrimSpace(rest)
175 return name, ok && name != "" && !strings.ContainsAny(name, " \t")
176}
177
178// spawnPrefix starts a bind action that runs commands: "spawn fuzzel --flag".
179// They are split by ports.SplitCommands; there is no shell.
180const spawnPrefix = "spawn "
181
182// SpawnCommands returns the commands of a "spawn <cmd>[; <cmd>...]" action.
183func SpawnCommands(a Action) ([][]string, bool) {
184 rest, ok := strings.CutPrefix(string(a), spawnPrefix)
185 if !ok {
186 return nil, false
187 }
188 commands := ports.SplitCommands(rest)
189 return commands, len(commands) > 0
190}
191
192type Effect struct {
193 Spawn bool
194 Commands [][]string // commands to run; nil means the configured terminal
195 Close WindowID
196 Quit bool
197}
198
199// applyAction runs a bind action. Monitor actions change the focused
200// screen; column focus past the edge column moves to the neighbor screen,
201// else along the layout's workspace axis; everything else applies to the
202// focused screen's monitor.
203func (c *Core) applyAction(a Action) Effect {
204 if e, ok := c.cur().mon.overviewAction(a); ok {
205 return e
206 }
207 // A bind acts on the window on screen: under a covering fullscreen
208 // window, that is the one Focused reports.
209 c.cur().mon.Current().focusCover()
210 // Before the edge moves below: a free float moves on its screen.
211 if c.cur().mon.Current().freeAction(a) {
212 return Effect{}
213 }
214 if name, ok := NamedArg(a); ok && c.bringNamed(name) {
215 return Effect{}
216 }
217 switch a {
218 case ActionFocusMonitorLeft, ActionFocusMonitorRight, ActionFocusMonitorUp, ActionFocusMonitorDown:
219 if i := c.neighbor(monitorDirection(a)); i >= 0 {
220 c.focusScreen = i
221 }
222 return Effect{}
223 case ActionMoveWorkspaceLeft, ActionMoveWorkspaceRight, ActionMoveWorkspaceToMonitorUp, ActionMoveWorkspaceToMonitorDown:
224 c.moveWorkspace(monitorDirection(a))
225 return Effect{}
226 case ActionFocusColumnLeft, ActionFocusColumnRight:
227 w := c.cur().mon.Current()
228 dir, d := 1, dirRight
229 if a == ActionFocusColumnLeft {
230 dir, d = -1, dirLeft
231 }
232 // A covering fullscreen window is left for the window on that side;
233 // with none (the workspace, band or stash edge), the move goes past
234 // the edge (see edgeFocus) and fullscreen stays.
235 if w.pinned() && !w.stashOverCover() {
236 if !w.FocusColumn(dir) {
237 c.edgeFocus(dir, d)
238 }
239 return Effect{}
240 }
241 // The first move from a float stays here unless nothing is under it:
242 // a native float leaves for the stash or the columns, and the stash
243 // keeps the focus at its ends.
244 edge := w.leavesBand(w.columnToward(dir)) && (!w.onFloat() || w.floatFocus && !w.canLeaveFloat())
245 if edge && c.edgeFocus(dir, d) {
246 return Effect{}
247 }
248 case ActionConsumeOrExpelLeft, ActionConsumeOrExpelRight:
249 dir, d := 1, dirRight
250 if a == ActionConsumeOrExpelLeft {
251 dir, d = -1, dirLeft
252 }
253 from := c.cur()
254 if from.mon.Current().ConsumeOrExpel(dir) {
255 from.mon.normalize()
256 return Effect{}
257 }
258 // Full fixed workspace, stacked edge column: one hop to the
259 // neighbor monitor, if any; focus follows the window.
260 i := c.neighbor(d)
261 if i < 0 {
262 return Effect{}
263 }
264 id, ok := from.mon.Current().takeWindow()
265 if !ok {
266 return Effect{}
267 }
268 c.focusScreen = i
269 c.cur().mon.Current().expelTo(id, dir)
270 from.mon.normalize()
271 c.cur().mon.normalize()
272 return Effect{}
273 case ActionMoveColumnLeft, ActionMoveColumnRight:
274 // Past the edge of the workspace or cascade band the column moves to
275 // the neighbor screen, on the side facing this one, and focus
276 // follows it. With no screen there, a cascade column crosses bands.
277 w := c.cur().mon.Current()
278 dir, d := 1, dirRight
279 if a == ActionMoveColumnLeft {
280 dir, d = -1, dirLeft
281 }
282 if i := c.neighbor(d); w.leavesBand(w.Focus+dir) && i >= 0 {
283 col, ok := w.takeColumn()
284 if !ok {
285 return Effect{}
286 }
287 from := c.cur()
288 c.focusScreen = i
289 to := c.cur().mon.Current()
290 at := 0
291 if d == dirLeft {
292 at = len(to.Columns)
293 }
294 to.receive(col, at)
295 from.mon.normalize()
296 c.cur().mon.normalize()
297 return Effect{}
298 }
299 }
300 return c.cur().mon.Apply(a)
301}
302
303// edgeFocus moves focus-column-left/right (dir -1/1, toward d) past the
304// content edge, in order: to the occupied workspace on that side when the
305// workspace axis is horizontal; else to the neighbor monitor; else to the
306// trailing empty workspace. It reports whether focus moved.
307func (c *Core) edgeFocus(dir int, d direction) bool {
308 m := c.cur().mon
309 if to := m.occupiedStep(horizontalAxis, dir); to >= 0 {
310 m.Focus(to)
311 return true
312 }
313 if i := c.neighbor(d); i >= 0 {
314 c.focusScreen = i
315 return true
316 }
317 return m.stepWorkspace(horizontalAxis, dir)
318}
319
320// Apply runs a bind action on the monitor.
321func (m *Monitor) Apply(a Action) Effect {
322 if e, ok := m.overviewAction(a); ok {
323 return e
324 }
325 if movesToWorkspace(a) {
326 if i, column, ok := m.moveDest(a); ok {
327 m.MoveToWorkspace(i, column)
328 }
329 return Effect{}
330 }
331 if n, _, ok := WorkspaceArg(a); ok {
332 before := m.Current()
333 m.FocusNumber(n)
334 if m.ov.open && m.Current() != before {
335 m.selectRow()
336 }
337 return Effect{}
338 }
339 if name, ok := NamedArg(a); ok {
340 before := m.Current()
341 m.ToggleNamed(name)
342 if m.ov.open && m.Current() != before {
343 m.selectRow()
344 }
345 return Effect{}
346 }
347 switch a {
348 case ActionFocusWindowUp, ActionFocusWindowDown:
349 // Past the column and any on-screen neighbor or demoted float,
350 // move to the next workspace if the layout's workspace axis is
351 // vertical. Cascade workspaces sit side by side: up/down stop at the
352 // first and last band.
353 dir := 1
354 if a == ActionFocusWindowUp {
355 dir = -1
356 }
357 if !m.Current().FocusWindow(dir) {
358 m.stepWorkspace(verticalAxis, dir)
359 }
360 return Effect{}
361 case ActionToggleFullscreen:
362 m.ToggleFullscreen()
363 return Effect{}
364 case ActionToggleWindowStash:
365 m.Current().ToggleWindowStash()
366 return Effect{}
367 case ActionToggleStashVisible:
368 m.Current().ToggleStashVisible()
369 return Effect{}
370 case ActionToggleOverview:
371 m.ToggleOverview()
372 return Effect{}
373 case ActionMaximizeColumn:
374 // A client that made itself fullscreen (Wine at monitor size) goes
375 // back to its column width; the next press maximizes the column.
376 if w := m.Current(); w.fullscreen != 0 {
377 if id, ok := w.Focused(); ok && id == w.fullscreen {
378 m.ToggleFullscreen()
379 return Effect{}
380 }
381 }
382 case ActionFocusWorkspacePrev, ActionFocusWorkspaceNext:
383 if m.shown == nil && a == ActionFocusWorkspacePrev {
384 m.Focus(m.Active - 1)
385 } else if m.shown == nil {
386 m.Focus(m.Active + 1)
387 }
388 return Effect{}
389 case ActionMoveWorkspacePrev:
390 m.MoveWorkspace(-1)
391 return Effect{}
392 case ActionMoveWorkspaceNext:
393 m.MoveWorkspace(1)
394 return Effect{}
395 }
396 return m.Current().Apply(a)
397}
398
399// movesToWorkspace reports a bind that moves a window to another workspace.
400func movesToWorkspace(a Action) bool {
401 if _, op, ok := WorkspaceArg(a); ok {
402 return op != FocusWorkspace
403 }
404 switch a {
405 case ActionMoveColumnToWorkspacePrev, ActionMoveColumnToWorkspaceNext,
406 ActionMoveWindowToWorkspacePrev, ActionMoveWindowToWorkspaceNext:
407 return true
408 }
409 return false
410}
411
412// moveDest resolves a move-to-workspace bind to the workspace index it
413// moves to and whether the whole column goes; ok is false when it has
414// nowhere to go. Up and down do nothing on a named workspace.
415func (m *Monitor) moveDest(a Action) (i int, column, ok bool) {
416 if n, op, isArg := WorkspaceArg(a); isArg {
417 return min(max(n-1, 0), len(m.Workspaces)-1), op == MoveColumnToWorkspace, true
418 }
419 if m.shown != nil {
420 return 0, false, false
421 }
422 switch a {
423 case ActionMoveColumnToWorkspacePrev, ActionMoveWindowToWorkspacePrev:
424 return m.Active - 1, a == ActionMoveColumnToWorkspacePrev, m.Active > 0
425 case ActionMoveColumnToWorkspaceNext, ActionMoveWindowToWorkspaceNext:
426 return min(m.Active+1, len(m.Workspaces)-1), a == ActionMoveColumnToWorkspaceNext, true
427 }
428 return 0, false, false
429}
430
431// Apply runs a bind action that only touches this workspace.
432func (w *Workspace) Apply(a Action) Effect {
433 if commands, ok := SpawnCommands(a); ok {
434 return Effect{Spawn: true, Commands: commands}
435 }
436 if w.freeAction(a) {
437 return Effect{}
438 }
439 if axis, step, ok := ResizeArg(a); ok {
440 if axis == ResizeWidth {
441 for range max(step, -step) {
442 if step > 0 {
443 w.GrowColumn(false)
444 } else {
445 w.ShrinkColumn()
446 }
447 }
448 } else {
449 w.ResizeRow(step)
450 }
451 return Effect{}
452 }
453 switch a {
454 case ActionSpawnTerminal:
455 return Effect{Spawn: true}
456 case ActionFocusColumnLeft:
457 w.FocusColumn(-1)
458 case ActionFocusColumnRight:
459 w.FocusColumn(1)
460 case ActionFocusWindowUp:
461 w.FocusWindow(-1)
462 case ActionFocusWindowDown:
463 w.FocusWindow(1)
464 case ActionMoveColumnLeft:
465 w.MoveColumn(-1)
466 case ActionMoveColumnRight:
467 w.MoveColumn(1)
468 case ActionMoveWindowUp:
469 w.MoveWindow(-1)
470 case ActionMoveWindowDown:
471 w.MoveWindow(1)
472 case ActionCycleColumnWidth:
473 w.GrowColumn(true)
474 case ActionMaximizeColumn:
475 w.ToggleFullWidth()
476 case ActionToggleFullscreen:
477 w.ToggleFullscreen()
478 case ActionToggleWindowStash:
479 w.ToggleWindowStash()
480 case ActionToggleStashVisible:
481 w.ToggleStashVisible()
482 case ActionToggleFloating:
483 w.ToggleFloating()
484 case ActionCloseWindow:
485 id, _ := w.Focused()
486 return Effect{Close: id}
487 case ActionQuit:
488 return Effect{Quit: true}
489 }
490 return Effect{}
491}