Skip to content

Instantly share code, notes, and snippets.

@sandro
Last active February 11, 2021 19:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sandro/1224b973b9445814b368fbc01bf93a18 to your computer and use it in GitHub Desktop.
Save sandro/1224b973b9445814b368fbc01bf93a18 to your computer and use it in GitHub Desktop.
phoenix config
function log(obj) {
Phoenix.log(JSON.stringify(obj));
}
var simpleMover = new function() {
var self = this;
var enabled = false;
var modal = Modal.build({
text: "Move: Up,Down,Left,Right\nGrow/Shrink: Cmd+Up/Down/Left/Right\nRaise windows: w\nRefocus: g\nExit: escape",
origin: function() {
return {x: 0, y: 200}
}
});
var keys = [];
var hintKeys = [];
var hintModals = [];
var exitKey = new Key("escape", [], function() {
self.disable();
});
var availableHintKeys = ["0","1","2","3","4","5","6","7","8","9","q","e","r","t","y","u","i","o","p"];
var closerID;
exitKey.disable();
var kk = function (f) {
return function() {
if (self.enabled()) {
self.initCloser();
var screen = Screen.main().flippedFrame();
var w = Window.focused();
if (w) {
var widthSlice = 200;
var heightSlice = 200;
f(screen, w, w.frame(), widthSlice, heightSlice);
}
}
}
}
this.enabled = function() {
return enabled;
}
this.initCloser = function() {
clearTimeout(closerID);
closerID = setTimeout(this.disable, 5000);
}
this.enable = function() {
if (self.enabled()) {
return;
}
enabled = true;
exitKey.enable();
self.enableKeys();
modal.show();
this.initCloser();
}
this.disable = function() {
if (!self.enabled()) {
return;
}
enabled = false;
exitKey.disable();
self.disableKeys();
modal.close();
self.killHints();
}
this.each = function(list, f) {
for (var i = 0; i < list.length; i++) {
f(list[i], i);
}
}
this.allKeys = function(f) {
self.each(keys, f);
}
this.enableKeys = function() {
self.allKeys(function(k) { k.enable() });
}
this.disableKeys = function() {
self.allKeys(function(k) { k.disable() });
}
this.killHints = function() {
self.each(hintKeys, function(k) {
k.disable();
});
hintKeys = [];
self.each(hintModals, function(m) {
m.close();
});
hintModals = [];
}
var hintWindows = function(screen, sortedWindows, keyFunc) {
for (var i = 0; i < sortedWindows.length; i++) {
var win = sortedWindows[i];
var app = win.app();
var wf = win.frame();
var hint = availableHintKeys[i];
var modal = Modal.build({
icon: app.icon(),
text: hint,
duration: 10,
origin: function() { return {x: wf.x, y: (screen.height - wf.y) - i*100 } }
});
hintModals.push(modal);
modal.show();
var hintKey = new Key(hint, [], function(selectedWindow) {
return function() {
keyFunc(selectedWindow);
self.disable();
}
}(win));
hintKeys.push(hintKey);
}
}
var raiseAppWindows = function(screen, w, wf, widthSlice, heightSlice) {
self.killHints();
var app = w.app();
var windows = app.windows({visible: true});
var focusedIndex = 0;
self.each(windows, function(w, i) {
if (w.isMain()) {
focusedIndex = i;
}
});
windows.splice(focusedIndex, 1);
var sorted = windows.sort(function(a,b) {
var ay = a.frame().y;
var by = b.frame().y;
if (ay < by) {
return -1;
}
else if (ay > by) {
return 1;
} else {
return 0;
}
});
sorted.splice(0, 0, w);
hintWindows(screen, sorted, function(selectedWindow) {
selectedWindow.raise();
w.focus();
});
}
var refocusAnyWindow = function(screen, w, wf, widthSlice, heightSlice) {
self.killHints();
var windows = Window.recent();
hintWindows(screen, windows, function(selectedWindow) {
selectedWindow.raise();
w.focus();
});
}
var setUpKeys = function() {
var key = new Key("left", [], kk(function(screen, w, wf, widthSlice) {
w.setTopLeft({x: wf.x - widthSlice, y: wf.y});
}));
keys.push(key);
key = new Key("right", [], kk(function(screen, w, wf, widthSlice) {
w.setTopLeft({x: wf.x + widthSlice, y: wf.y});
}));
keys.push(key);
key = new Key("up", [], kk(function(screen, w, wf, widthSlice, heightSlice) {
w.setTopLeft({x: wf.x, y: wf.y - heightSlice});
}));
keys.push(key);
key = new Key("down", [], kk(function(screen, w, wf, widthSlice, heightSlice) {
w.setTopLeft({x: wf.x, y: wf.y + heightSlice});
}));
keys.push(key);
key = new Key("left", ["cmd"], kk(function(screen, w, wf, widthSlice, heightSlice) {
w.setSize({width: wf.width - widthSlice, height: wf.height});
}));
keys.push(key);
key = new Key("right", ["cmd"], kk(function(screen, w, wf, widthSlice, heightSlice) {
w.setSize({width: wf.width + widthSlice, height: wf.height});
}));
keys.push(key);
key = new Key("up", ["cmd"], kk(function(screen, w, wf, widthSlice, heightSlice) {
w.setSize({width: wf.width, height: wf.height - heightSlice});
}));
keys.push(key);
key = new Key("down", ["cmd"], kk(function(screen, w, wf, widthSlice, heightSlice) {
w.setSize({width: wf.width, height: wf.height + heightSlice});
}));
keys.push(key);
key = new Key("w", [], kk(raiseAppWindows));
keys.push(key);
key = new Key("g", [], kk(refocusAnyWindow));
keys.push(key);
self.disableKeys();
}
this.init = function() {
setUpKeys();
}
}
simpleMover.init();
var keyHandler = new function() {
var self = this;
var keyPresses = {};
var cycles = 3;
var keyPressTimeout = 2000;
var keyPressTimeoutId = -1;
var wrap = function(key, func) {
return function() {
var num = self.keyPressed(key);
// var screen = Screen.main().flippedFrame();
var w = Window.focused();
var screen = w.screen().flippedFrame();
func(screen, w, num);
}
}
this.clearKeyPresses = function() {
keyPresses = {};
}
this.keyPressed = function(key) {
clearTimeout(keyPressTimeoutId);
keyPressTimeoutId = setTimeout(self.clearKeyPresses, keyPressTimeout)
keyNum = keyPresses[key] || 0;
if (keyNum >= cycles) {
keyNum = 0;
}
// self.clearKeyPresses();
keyPresses[key] = keyNum + 1;
return keyPresses[key];
}
this.on = function(key, modifiers, func) {
Key.on(key, modifiers, wrap(key, func))
}
}
function manualAdjust() {
simpleMover.enable();
}
function center(screen, w) {
var x = screen.x + (screen.width * 0.15);
var width = screen.width * 0.7;
w.setFrame({x: x, y: screen.y, width: width, height: screen.height});
}
function fullWidthHeight(screen, w) {
w.setFrame({x: screen.x, y: screen.y, width: screen.width, height: screen.height});
}
function leftSide(screen, w, num) {
first = {x: screen.x, width: screen.width * leftProportion};
second = {x: first.x, width: first.width - 200};
third = {x: first.x, width: first.width - 500};
// w.setFrame({x: screen.x, y: screen.y, width: screen.width * leftProportion, height: screen.height});
sizes = [first,second,third]
size = sizes[num-1]
w.setFrame({x: size.x, y: screen.y, width: size.width, height: screen.height});
}
function rightSide(screen, w, num) {
first = {x: screen.x + (screen.width * leftProportion), width: screen.width * rightProportion};
second = {x: first.x - 200, width: first.width + 200};
third = {x: first.x + 100, width: first.width - 100};
if (num == 1) {
w.setFrame({x: first.x, y: screen.y, width: first.width, height: screen.height});
} else if (num == 2) {
w.setFrame({x: second.x, y: screen.y, width: second.width, height: screen.height});
} else if (num == 3) {
w.setFrame({x: third.x, y: screen.y, width: third.width, height: screen.height});
}
}
function topSide(screen, w) {
w.setFrame({x: screen.x, y: screen.y, width: screen.width, height: screen.height * topProportion});
}
function bottomSide(screen, w) {
w.setFrame({x: screen.x, y: screen.y + (screen.height * topProportion), width: screen.width, height: screen.height * bottomProportion});
}
Phoenix.set({
openAtLogin: true
});
var leftProportion = 0.53;
var rightProportion = 1.0 - leftProportion;
var topProportion = 0.55;
var bottomProportion = 1.0 - topProportion;
var modifiers = ["ctrl", "alt", "cmd"];
keyHandler.on("c", modifiers, center);
keyHandler.on("m", modifiers, fullWidthHeight);
keyHandler.on("left", modifiers, leftSide);
keyHandler.on("right", modifiers, rightSide);
keyHandler.on("up", modifiers, topSide);
keyHandler.on("down", modifiers, bottomSide);
keyHandler.on("f", modifiers, manualAdjust);
//Key.on("b", [], function() {
// var w = Window.focused();
// w.setFullScreen(!w.isFullScreen());
//});
//
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment