-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphoenix.js
174 lines (148 loc) · 3.82 KB
/
phoenix.js
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
const HYPER = ["ctrl", "alt", "cmd", "shift"];
const pipe = (...fns) => x => fns.reduce((v, f) => f(v), x);
const areNumbers = ns => !ns.some(Number.isNaN);
const HIST_LEN = 20;
const history = new Array(HIST_LEN);
history.add = v => {
history.pop();
history.splice(0, 0, v);
};
const move = getCoords => {
const screen = Screen.main();
const screenFrame = screen.flippedVisibleFrame();
const window = Window.focused();
if (window) {
const windowFrame = window.frame();
const { x, y, w, h } = getCoords({
window,
screenFrame,
screen,
windowFrame
});
if (!areNumbers([x, y, w, h]))
return console.log("not numbers", { x, y, w, h });
window.setFrame({
x,
y,
width: w,
height: h
});
}
};
const mod1 = (key, fn) => {
Key.on(key, HYPER, () => {
fn();
return history.add(key);
});
};
const useRepeats = key => {
const h = history.join("");
const start = new RegExp(`^(${key}*)`);
const [, keys] = h.match(start);
const repeats = keys.length;
return repeats;
};
const thirds = n => {
switch (n % 3) {
case 0:
return 2 / 3;
case 1:
return 1 / 2;
case 2:
return 1 / 3;
default:
return 1 / 2;
}
};
const useThirds = pipe(useRepeats, thirds);
const useToggle = pipe(useRepeats, n => !!(n % 2));
const scale = (windowFrame, screenFrame, nextScreenFrame) => {
const xProp = windowFrame.width / screenFrame.width;
const yProp = windowFrame.height / screenFrame.height;
// proportions of screen start to window start of the window lengths
const xStartProp = (windowFrame.x - screenFrame.x) / screenFrame.width;
const yStartProp = (windowFrame.y - screenFrame.y) / screenFrame.height;
const x = nextScreenFrame.x + nextScreenFrame.width * xStartProp;
const y = nextScreenFrame.y + nextScreenFrame.height * yStartProp;
return {
w: xProp * nextScreenFrame.width,
h: yProp * nextScreenFrame.height,
x,
y
};
};
// v-center to maximise
mod1("m", () => {
const repeats = useRepeats("m");
if (repeats === 0) {
// move to middle third
return move(({ screenFrame, windowFrame }) => ({
x: screenFrame.x + (screenFrame.width * 1) / 3,
y: windowFrame.y,
w: screenFrame.width * (1 / 3),
h: windowFrame.height
}));
}
const toggle = repeats % 2 === 1;
// v-maximise
if (toggle)
return move(({ screenFrame }) => ({
x: screenFrame.x + (screenFrame.width * 1) / 3,
y: screenFrame.y,
w: screenFrame.width * (1 / 3),
h: screenFrame.height
}));
// maximise
return move(({ screenFrame }) => ({
x: screenFrame.x,
y: screenFrame.y,
w: screenFrame.width,
h: screenFrame.height
}));
});
mod1("b", () => {
move(({ screenFrame, screen, windowFrame }) =>
scale(windowFrame, screenFrame, screen.previous().flippedVisibleFrame())
);
});
mod1("e", () => {
move(({ screenFrame, screen, windowFrame }) =>
scale(windowFrame, screenFrame, screen.next().flippedVisibleFrame())
);
});
mod1("h", () => {
const mult = 1 - useThirds("h");
move(({ screenFrame }) => ({
x: screenFrame.x,
y: screenFrame.y,
w: screenFrame.width * mult,
h: screenFrame.height
}));
});
mod1("l", () => {
const mult = useThirds("l");
move(({ screenFrame }) => ({
x: screenFrame.x + mult * screenFrame.width,
y: screenFrame.y,
w: screenFrame.width * (1 - mult),
h: screenFrame.height
}));
});
mod1("j", () => {
const mult = 0.5;
move(({ screenFrame, windowFrame }) => ({
x: windowFrame.x,
y: screenFrame.y + mult * screenFrame.height,
w: windowFrame.width,
h: screenFrame.height * mult
}));
});
mod1("k", () => {
const mult = 0.5;
move(({ screenFrame, windowFrame }) => ({
x: windowFrame.x,
y: screenFrame.y,
w: windowFrame.width,
h: screenFrame.height * mult
}));
});