Skip to content

Instantly share code, notes, and snippets.

@lewiscowper
Forked from pirfalt/.phoenix.js
Created February 19, 2014 12:06
Show Gist options
  • Save lewiscowper/9090662 to your computer and use it in GitHub Desktop.
Save lewiscowper/9090662 to your computer and use it in GitHub Desktop.
// HELPERS
// Sometimes it's easier to work with edges instead of point + size
// {x, y, height, width} => {top, bottom, left right}
function frameToRect(frame) {
return {
left: frame.x,
right: frame.x + frame.width,
top: frame.y,
bottom: frame.y + frame.height
}
}
// {top, bottom, left right} => {x, y, height, width}
function rectToFrame(rect) {
return {
x: rect.left,
y: rect.top,
width: rect.right - rect.left,
height: rect.bottom - rect.top
}
}
// Cycle args for the function, if called repeatedly
// cycleCalls(fn, [ [args1...], [args2...], ... ])
var lastCall = null
function cycleCalls(fn, argsList) {
var argIndex = 0, identifier = {}
return function () {
if (lastCall !== identifier || ++argIndex >= argsList.length) argIndex = 0
lastCall = identifier
fn.apply(this, argsList[argIndex])
}
}
// ACTIONS
// toAnything(a,b). Sets the focusWindow size to screensize * a/b
function toLeft(fillCols, maxCols) {
var win = Window.focusedWindow()
var rect = frameToRect(win.frame())
rect.left = 0
rect.right = win.screen().frameWithoutDockOrMenu().width / maxCols * fillCols
win.setFrame(rectToFrame(rect), win.screen())
}
function toRight(fillCols, maxCols) {
var win = Window.focusedWindow()
var rect = frameToRect(win.frame())
var screenFrame = win.screen().frameWithoutDockOrMenu()
rect.left = screenFrame.width - screenFrame.width / maxCols * fillCols
rect.right = screenFrame.width
win.setFrame(rectToFrame(rect), win.screen())
}
function toTop(fillRows, maxRows) {
var win = Window.focusedWindow()
var winFrame = win.frame()
var screenFrame = win.screen().frameWithoutDockOrMenu()
var targetHeight = screenFrame.height / maxRows * fillRows
winFrame.y = screenFrame.y
winFrame.height = targetHeight
win.setFrame(winFrame, win.screen())
}
function toBottom(fillRows, maxRows) {
var win = Window.focusedWindow()
var winFrame = win.frame()
var screenFrame = win.screen().frameWithoutDockOrMenu()
var targetHeight = screenFrame.height / maxRows * fillRows
winFrame.y = screenFrame.y + screenFrame.height - targetHeight
winFrame.height = targetHeight
win.setFrame(winFrame, win.screen())
}
// BINDS
var opts = ['ctrl', 'cmd']
api.bind('h', opts, cycleCalls(
toLeft,
[
[1,2],
[1,3],
[2,3],
[1,1]
]
))
api.bind('l', opts, cycleCalls(
toRight,
[
[1,2],
[1,3],
[2,3],
[1,1]
]
))
api.bind('k', opts, cycleCalls(
toTop,
[
[1,2],
[1,1],
[2,3]
]
))
api.bind('j', opts, cycleCalls(
toBottom,
[
[1,2],
[1,1],
[1,3]
]
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment