-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.js
151 lines (121 loc) · 5.28 KB
/
main.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
function windowCoordinatesAlmostEqual(a, b) {
return Math.abs(a - b) < 2;
}
function resizeAndMove(horizontalThird, twoThirds) {
if (!workspace.activeClient.normalWindow && !workspace.activeClient.utility) {
return;
}
var placementArea = workspace.clientArea(KWin.PlacementArea, workspace.activeScreen, workspace.currentDesktop);
var g = workspace.activeClient.geometry;
var numThirds = twoThirds ? 2 : 1;
var thirdWidth = placementArea.width / 3;
var width = numThirds * thirdWidth;
var left;
if (numThirds == 1) {
left = placementArea.x + horizontalThird * thirdWidth;
} else if (numThirds == 2) {
if (horizontalThird == 0) {
left = placementArea.x;
} else if (horizontalThird == 1) {
left = placementArea.x + thirdWidth / 2;
} else if (horizontalThird == 2) {
left = placementArea.x + thirdWidth;
}
}
var horizontalLayoutChanged = (
!windowCoordinatesAlmostEqual(g.x, left) ||
!windowCoordinatesAlmostEqual(g.width, width)
);
g.x = left;
g.width = width;
var top = placementArea.y;
var fullHeight = placementArea.height;
var halfHeight = fullHeight / 2;
var verticalLayouts = [[top, fullHeight], [top, halfHeight], [top + halfHeight, halfHeight]];
var currentVerticalLayout = null;
for (var i = 0; i < verticalLayouts.length; ++i) {
var y = verticalLayouts[i][0];
var h = verticalLayouts[i][1];
if (windowCoordinatesAlmostEqual(g.y, y) && windowCoordinatesAlmostEqual(g.height, h)) {
currentVerticalLayout = i;
break;
}
}
var newVerticalLayout;
if (currentVerticalLayout === null || horizontalLayoutChanged) {
newVerticalLayout = 0;
} else {
newVerticalLayout = (currentVerticalLayout + 1) % verticalLayouts.length;
}
g.y = verticalLayouts[newVerticalLayout][0];
g.height = verticalLayouts[newVerticalLayout][1];
workspace.activeClient.geometry = g;
// TODO: restore size when window is moved with mouse, if it hasn't been resized otherwise
}
// TODO: this is a copy/paste with x<->y and width<->height. Could be less copy/paste.
function resizeAndMoveVertical(verticalThird, twoThirds) {
if (!workspace.activeClient.normalWindow && !workspace.activeClient.utility) {
return;
}
var placementArea = workspace.clientArea(KWin.PlacementArea, workspace.activeScreen, workspace.currentDesktop);
var g = workspace.activeClient.geometry;
var numThirds = twoThirds ? 2 : 1;
var thirdHeight = placementArea.height / 3;
var height = numThirds * thirdHeight;
var top;
if (numThirds == 1) {
top = placementArea.y + verticalThird * thirdHeight;
} else if (numThirds == 2) {
if (verticalThird == 0) {
top = placementArea.y;
} else if (verticalThird == 1) {
top = placementArea.y + thirdHeight / 2;
} else if (verticalThird == 2) {
top = placementArea.y + thirdHeight;
}
}
var verticalLayoutChanged = (
!windowCoordinatesAlmostEqual(g.y, top) ||
!windowCoordinatesAlmostEqual(g.height, height)
);
g.y = top;
g.height = height;
var left = placementArea.x;
var fullWidth = placementArea.width;
var halfWidth = fullWidth / 2;
var horizontalLayouts = [[left, fullWidth], [left, halfWidth], [left + halfWidth, halfWidth]];
var currentHorizontalLayout = null;
for (var i = 0; i < horizontalLayouts.length; ++i) {
var x = horizontalLayouts[i][0];
var h = horizontalLayouts[i][1];
if (windowCoordinatesAlmostEqual(g.x, x) && windowCoordinatesAlmostEqual(g.width, h)) {
currentHorizontalLayout = i;
break;
}
}
var newHorizontalLayout;
if (currentHorizontalLayout === null || verticalLayoutChanged) {
newHorizontalLayout = 0;
} else {
newHorizontalLayout = (currentHorizontalLayout + 1) % horizontalLayouts.length;
}
g.x = horizontalLayouts[newHorizontalLayout][0];
g.width = horizontalLayouts[newHorizontalLayout][1];
workspace.activeClient.geometry = g;
// TODO: restore size when window is moved with mouse, if it hasn't been resized otherwise
}
function shortcut(text, defaultShortcut, func) {
registerShortcut(text, text, defaultShortcut, func);
}
shortcut("Thirds: Window to Left 1/3", "Meta+Left", function () { resizeAndMove(0, false); });
shortcut("Thirds: Window to Horizontal Middle 1/3", "Meta+Down", function () { resizeAndMove(1, false); });
shortcut("Thirds: Window to Right 1/3", "Meta+Right", function () { resizeAndMove(2, false); });
shortcut("Thirds: Window to Left 2/3", "Meta+Ctrl+Left", function () { resizeAndMove(0, true); });
shortcut("Thirds: Window to Horizontal Middle 2/3", "Meta+Ctrl+Down", function () { resizeAndMove(1, true); });
shortcut("Thirds: Window to Right 2/3", "Meta+Ctrl+Right", function () { resizeAndMove(2, true); });
shortcut("Thirds: Window to Top 1/3", "Meta+B", function () { resizeAndMoveVertical(0, false); });
shortcut("Thirds: Window to Vertical Middle 1/3", "Meta+N", function () { resizeAndMoveVertical(1, false); });
shortcut("Thirds: Window to Bottom 1/3", "Meta+M", function () { resizeAndMoveVertical(2, false); });
shortcut("Thirds: Window to Top 2/3", "Meta+Ctrl+B", function () { resizeAndMoveVertical(0, true); });
shortcut("Thirds: Window to Vertical Middle 2/3", "Meta+Ctrl+N", function () { resizeAndMoveVertical(1, true); });
shortcut("Thirds: Window to Bottom 2/3", "Meta+Ctrl+M", function () { resizeAndMoveVertical(2, true); });