forked from TypicalFence/Amethyst
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubset.js
87 lines (84 loc) · 3.54 KB
/
subset.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
function layout() {
return {
name: "Subset",
initialState: {
ids: []
},
commands: {
command3: {
description: "Add window to subset",
updateState: (state, focusedWindowID) => {
const ids = state.ids;
if (!!focusedWindowID) {
const index = ids.indexOf(focusedWindowID);
if (index === -1) {
ids.push(focusedWindowID);
}
}
return { ...state, ids };
}
},
command4: {
description: "Remove window from subset",
updateState: (state, focusedWindowID) => {
const ids = state.ids;
if (!!focusedWindowID) {
const index = ids.indexOf(focusedWindowID);
if (index > -1) {
ids.splice(index, 1);
}
}
return { ...state, ids };
}
}
},
getFrameAssignments: (windows, screenFrame, state) => {
const mainPaneCount = state.ids.length;
const secondaryPaneCount = Math.max(windows.length - mainPaneCount, 0);
const hasSecondaryPane = secondaryPaneCount > 0;
const mainPaneWindowWidth = hasSecondaryPane ? screenFrame.width / 2 : screenFrame.width;
const mainPaneWindowHeight = Math.round(screenFrame.height / mainPaneCount);
const secondaryPaneWindowHeight = Math.round(hasSecondaryPane ? (screenFrame.height / secondaryPaneCount) : 0);
let mainIndex = 0;
let secondaryIndex = 0;
return windows.reduce((frames, window) => {
const isMain = state.ids.includes(window.id);
let frame;
if (isMain) {
frame = {
x: screenFrame.x,
y: screenFrame.y + mainPaneWindowHeight * mainIndex,
width: mainPaneWindowWidth,
height: mainPaneWindowHeight
};
mainIndex++;
} else {
frame = {
x: screenFrame.x + screenFrame.width / 2,
y: screenFrame.y + secondaryPaneWindowHeight * secondaryIndex,
width: screenFrame.width / 2,
height: secondaryPaneWindowHeight
};
secondaryIndex++;
}
return { ...frames, [window.id]: frame };
}, {});
},
updateWithChange: (change, state) => {
switch (change.change) {
case "window_swap":
if (state.ids.includes(change.windowID) && !state.ids.includes(change.otherWindowID)) {
const index = state.ids.indexOf(change.windowID);
state.ids.splice(index, 1);
state.ids.push(change.otherWindowID);
} else if (state.ids.includes(change.otherWindowID) && !state.ids.includes(change.windowID)) {
const index = state.ids.indexOf(change.otherWindowID);
state.ids.splice(index, 1);
state.ids.push(change.windowID);
}
break;
}
return state;
}
};
}