Created
December 1, 2014 14:51
-
-
Save freetwix/d6f4cbe5695ee04029f4 to your computer and use it in GitHub Desktop.
phoenix.js starter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Key combinations | |
var mash = ['ctrl', 'alt', 'cmd']; | |
Screen.prototype.frameSomehowFixed = function() { | |
return _.extend( | |
this.frameWithoutDockOrMenu(), | |
_.pick(this.frameIncludingDockAndMenu(), ['width','x']) | |
); | |
} | |
Window.prototype.toGrid = function(x, y, width, height) { | |
var screen = this.screen().frameSomehowFixed(); | |
var padding = 0; | |
this.setFrame({ | |
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) | |
}); | |
return this; | |
} | |
Window.prototype.moveToScreen = function(screen) { | |
var currentFrame = this.frame(); | |
var oldScreenRect = this.screen().frameSomehowFixed(); | |
var newScreenRect = screen.frameSomehowFixed(); | |
var xRatio = newScreenRect.width / oldScreenRect.width; | |
var yRatio = newScreenRect.height / oldScreenRect.height; | |
var relativeX = Math.abs(oldScreenRect.x - currentFrame.x); | |
var relativeY = Math.abs(oldScreenRect.y - currentFrame.y); | |
this.setFrame({ | |
x: Math.round(relativeX * xRatio) + newScreenRect.x, | |
y: Math.round(relativeY * yRatio) + newScreenRect.y, | |
width: Math.round(currentFrame.width * xRatio), | |
height: Math.round(currentFrame.height * yRatio) | |
}); | |
}; | |
Window.prototype.toLeft = function() { | |
this.toGrid(0.0, 0.0, 0.5, 1.0); | |
return this; | |
} | |
Window.prototype.toRight = function() { | |
this.toGrid(0.5, 0.0, 0.5, 1.0); | |
return this; | |
} | |
Window.prototype.toTop = function() { | |
this.toGrid(0.0, 0.0, 1.0, 0.5); | |
return this; | |
} | |
Window.prototype.toBottom = function() { | |
this.toGrid(0.0, 0.5, 1.0, 0.5); | |
return this; | |
} | |
Window.prototype.toCentered45 = function() { | |
this.toGrid(0.1, 0.0, 0.8, 1.0); | |
return this; | |
} | |
Window.prototype.toFullScreen = function() { | |
this.toGrid(0.0, 0.0, 1.0, 1.0); | |
return this; | |
} | |
var previousSizes = {}; | |
Window.prototype.toggleFullscreen = function() { | |
if (previousSizes[this]) { | |
this.setFrame(previousSizes[this]); | |
delete previousSizes[this]; | |
} else { | |
previousSizes[this] = this.frame(); | |
this.toFullScreen(); | |
} | |
return this; | |
} | |
function fwindow() { | |
return Window.focusedWindow(); | |
} | |
// Screen placement movement | |
api.bind('left', mash, function() { | |
fwindow().toLeft(); | |
}); | |
api.bind('right', mash, function() { | |
fwindow().toRight(); | |
}); | |
api.bind('up', mash, function() { | |
fwindow().toTop(); | |
}); | |
api.bind('down', mash, function() { | |
fwindow().toBottom(); | |
}); | |
api.bind('.', mash, function() { | |
fwindow().toCentered45(); | |
}); | |
api.bind('space', mash, function() { | |
fwindow().toggleFullscreen(); | |
}); | |
// Sets | |
api.bind('n', mash, function() { fwindow().moveToScreen(fwindow().screen().nextScreen()); }); | |
api.bind('p', mash, function() { fwindow().moveToScreen(fwindow().screen().previousScreen()); }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment