-
Notifications
You must be signed in to change notification settings - Fork 0
/
common_actions.ts
135 lines (116 loc) · 3.68 KB
/
common_actions.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import {
Action,
Command,
Layer,
Mode,
Operator,
SetLayer,
SetMode,
SetOperator,
SetSpecifier,
Specifier,
VAR,
command,
} from "./base.ts";
import { UltraOpts } from "./mod.ts";
import { Daemon, sendToDaemon } from "./daemon.ts";
import { EventHandler, registerEventHandler } from "./yabai_events.ts";
import { powar } from "./deps.ts";
import { CommonBindings } from "./common_bindings.ts";
export type SetUltraVars = {
[V in keyof typeof VAR]?: {
mode: Mode;
layer: Layer;
operator: Operator;
application: string;
specifier: Specifier;
}[V];
};
export type CompassDirection = "north" | "south" | "east" | "west";
export type Direction = "next" | "prev";
export class CommonActions {
constructor(public opts: UltraOpts, private bindings: CommonBindings) {}
focusWindow = (direction: CompassDirection): Command => {
return command(`${this.opts.yabaiCliPath} -m window --focus ${direction}`);
};
moveWindow = (direction: CompassDirection): Command => {
return command(`${this.opts.yabaiCliPath} -m window --warp ${direction}`);
};
resizeWindow = (resize_command: string): Command => {
return command(
`${this.opts.yabaiCliPath} -m window --resize ${resize_command}`
);
};
focusSpace = (direction: Direction): Action => {
if (direction == "next") {
return this.bindings.binding("right_arrow", ["left_control"]);
} else {
return this.bindings.binding("left_arrow", ["left_control"]);
}
};
focusDisplay = (direction: Direction): Command => {
return command(`${this.opts.yabaiCliPath} -m display --focus ${direction}`);
};
moveToDisplay = (direction: Direction): Command => {
return command(
`${this.opts.yabaiCliPath} -m window --display ${direction}`
);
};
closeWindow = (): Command => {
return command(`${this.opts.yabaiCliPath} -m window --close`);
};
toggleFloat = (): Command => {
return command(
`${this.opts.yabaiCliPath} -m window --toggle float; ${this.opts.yabaiCliPath} -m window --grid 6:4:1:1:2:4`
);
};
toggleZen = (): Command => {
return command(
`${this.opts.yabaiCliPath} -m window --toggle float; ${this.opts.yabaiCliPath} -m window --grid 1:4:1:1:2:1`
);
};
toggleFullscreen = (): Command => {
return command(
`${this.opts.yabaiCliPath} -m window --toggle native-fullscreen`
);
};
setUltraVarsFromCli = (data: SetUltraVars): Command => {
return command(
`${this.opts.karabinerCliPath} --set-variables '${JSON.stringify(
Object.fromEntries(
(Object.entries(data) as [keyof typeof VAR, string][]).map(
([k, v]) => [VAR[k], v]
)
)
)}'`
);
};
applescript = (cmd: string): Command => {
return command(`osascript -e ${JSON.stringify(cmd)}`);
};
actionAsCommand = (
action: Command | SetMode | SetLayer | SetOperator | SetSpecifier
): Command => {
switch (action.kind) {
case "command":
return action;
case "set-mode":
return this.setUltraVarsFromCli({ mode: action.mode });
case "set-layer":
return this.setUltraVarsFromCli({ layer: action.layer });
case "set-operator":
return this.setUltraVarsFromCli({ operator: action.operator });
case "set-specifier":
return this.setUltraVarsFromCli({ specifier: action.specifier });
}
};
executeCommand = (command: Command): Promise<powar.Output> => {
return powar.execute(command.command);
};
sendToDaemon = <D>(spec: Daemon<D>, data: D): Command => {
return sendToDaemon(spec, data);
};
registerEventHandler = (handler: EventHandler): Command => {
return registerEventHandler(this.opts, handler);
};
}