forked from czottmann/homedir
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy path.phoenix.js
183 lines (144 loc) · 3.94 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
175
176
177
178
179
180
181
182
183
// This is my configuration for Phoenix <https://github.com/sdegutis/Phoenix>, an awesome
// and super-lightweight OS X window manager that can be configured and scripted through
// the insanity that is Javascript. Heavily recommended. :)
//
// With my admittedly limited Javascript skills, I'm extending the built-in Phoenix classes
// a little to allow for slightly more expressive window configuration (most importantly,
// by being able to pass screen ratios instead of pixel rects, and controlling specific
// apps just a little bit easier.)
//
// Feedback/forks/improvements highly appreciated!
//
// -- [email protected]
// twitter.com/hmans
//
var mash = ["cmd", "alt", "ctrl"];
var padding = 4;
api.bind('1', ['ctrl'], function() {
App.focus("Google Chrome");
});
api.bind('2', ['ctrl'], function() {
App.focus("Atom");
});
api.bind('3', ['ctrl'], function() {
App.focus("Terminal");
});
api.bind('space', mash, function() {
Window.focusedWindow().toFullScreen();
});
api.bind('up', mash, function() {
Window.focusedWindow().toTopHalf();
});
api.bind('down', mash, function() {
Window.focusedWindow().toBottomHalf();
});
api.bind('left', mash, function() {
Window.focusedWindow().toLeftHalf();
});
api.bind('right', mash, function() {
Window.focusedWindow().toRightHalf();
});
api.bind('1', mash, function() {
api.alert("Layout 1", 0.5);
forApp("Google Chrome", function(win) {
win.toRightHalf();
});
forApp("Terminal", function(win) {
win.toGrid(0, 0.7, 0.5, 0.3);
});
forApp("Sublime Text", function(win) {
win.toGrid(0, 0, 0.5, 0.7);
});
forApp("Atom", function(win) {
win.toGrid(0, 0, 0.5, 0.7);
});
forApp("GitX", function(win) {
win.toGrid(0.2, 0.2, 0.6, 0.6);
});
});
api.bind('2', mash, function() {
api.alert("Layout 2", 0.5);
forApp("Terminal", function(win) {
win.toRightHalf();
})
forApp("Sublime Text", function(win) {
win.toLeftHalf();
})
forApp("Atom", function(win) {
win.toLeftHalf();
})
});
// Let's extend the Phoenix classes a little.
//
var lastFrames = {};
Window.prototype.calculateGrid = function(x, y, width, height) {
var screen = this.screen().frameWithoutDockOrMenu();
return {
x: Math.round(x * screen.width) + padding + screen.x,
y: Math.round(y * screen.height) + padding + screen.y,
width: Math.round(width * screen.width) - 2*padding,
height: Math.round(height * screen.height) - 2*padding
};
}
Window.prototype.toGrid = function(x, y, width, height) {
var rect = this.calculateGrid(x, y, width, height);
this.setFrame(rect);
return this;
}
Window.prototype.toFullScreen = function() {
var fullFrame = this.calculateGrid(0, 0, 1, 1);
if (!_.isEqual(this.frame(), fullFrame)) {
this.rememberFrame();
return this.toGrid(0, 0, 1, 1);
} else if (lastFrames[this]) {
this.setFrame(lastFrames[this]);
this.forgetFrame();
}
}
Window.prototype.toTopHalf = function() {
return this.toGrid(0, 0, 1, 0.5);
}
Window.prototype.toBottomHalf = function() {
return this.toGrid(0, 0.5, 1, 0.5);
}
Window.prototype.toLeftHalf = function() {
return this.toGrid(0, 0, 0.5, 1);
}
Window.prototype.toRightHalf = function() {
return this.toGrid(0.5, 0, 0.5, 1);
}
Window.prototype.rememberFrame = function() {
lastFrames[this] = this.frame();
}
Window.prototype.forgetFrame = function() {
delete lastFrames[this];
}
App.byTitle = function(title) {
var apps = this.runningApps();
for (i = 0; i < apps.length; i++) {
var app = apps[i];
if (app.title() == title) {
app.show();
return app;
}
}
}
App.prototype.focus = function() {
if (win = this.firstWindow()) {
win.focusWindow();
}
}
App.focus = function(name) {
if (app = App.byTitle(name)) {
app.focus();
}
}
App.prototype.firstWindow = function() {
return this.visibleWindows()[0];
}
function forApp(name, f) {
var app = App.byTitle(name);
if (app) {
_.each(app.visibleWindows(), f);
}
}