Skip to content

Instantly share code, notes, and snippets.

@kerbyfc
Created January 11, 2016 06:45
Show Gist options
  • Save kerbyfc/9469394f2af53c56a9e1 to your computer and use it in GitHub Desktop.
Save kerbyfc/9469394f2af53c56a9e1 to your computer and use it in GitHub Desktop.
Phoenix configuration script
keys = []
###*
* Output information via notification center
* @param {Any} obj - any object
* @param {Number} duration = 5 - duration in seconds
###
log = (obj, duration = 5) ->
if obj?
msg = switch
when obj instanceof Error
obj.message
when typeof obj is 'object'
JSON.stringify(obj)
else
new String(obj)
Phoenix.notify msg
###*
* Wrap function with try & catch construction
* @param {Function} implementation
* @return {Function} wrapped function
###
fn = (implementation) ->
->
try
implementation arguments...
catch e
log e
###*
* Current modal window
* @type {Null|Modal}
###
cmodal = null
###*
* Show modal window with name of just focused window.
* It helps with visual detection of current window.
* @param {Window} win - current window
* @param {String} name - window application name
###
showHintForWindow = fn (win, name) ->
cmodal?.close()
cmodal = new Modal()
cmodal.message = name
frame = win.frame()
mframe = cmodal.frame()
x = frame.x + frame.width / 2 - mframe.width / 2
y = win.screen().visibleFrameInRectangle().height - (frame.y + frame.height / 2) - mframe.height / 2
cmodal.origin = {x: x, y: y}
cmodal.duration = 0.3
cmodal.show()
###*
* Focus application by name
* @param {String} name
###
focusApp = (name) ->
if not app = App.get(name)
app = App.launch(name)
if app
windows = _.filter Window.visibleWindowsInOrder(), (win) ->
win.app().name() is name
win = if app.isActive()
_.last(windows)
else
_.first(windows)
win.focus()
showHintForWindow(win, name)
###*
* Move window to the proper screen by screen number
* @param {Window} win
* @param {Number} screenNumber
###
moveWindowToScreen = fn (win, screenNumber) ->
if screen = Screen.screens()[screenNumber - 1]
frame = win.frame();
oldScreenRect = win.screen().visibleFrameInRectangle()
newScreenRect = screen.visibleFrameInRectangle()
xRatio = newScreenRect.width / oldScreenRect.width
yRatio = newScreenRect.height / oldScreenRect.height
win.setFrame
x: (Math.round(frame.x - oldScreenRect.x) * xRatio) + newScreenRect.x
y: (Math.round(frame.y - oldScreenRect.y) * yRatio) + newScreenRect.y
width: Math.round(frame.width * xRatio)
height: Math.round(frame.height * yRatio)
###*
* Move current window to the proper screen by screen number
* @param {Number} screenNumber
###
moveCurrentWindowToScreen = fn (screenNumber) ->
moveWindowToScreen Window.focusedWindow(), screenNumber
# ------------------------------------------------------------------------------
# Application switch hotkeys
keys.push Phoenix.bind 'g', [ 'ctrl', 'cmd' ], ->
focusApp "Google Chrome"
keys.push Phoenix.bind 'v', [ 'ctrl', 'cmd' ], ->
focusApp "Atom"
keys.push Phoenix.bind 's', [ 'ctrl', 'cmd' ], ->
focusApp "Skype"
keys.push Phoenix.bind 'e', [ 'ctrl', 'cmd' ], ->
focusApp "Mail"
keys.push Phoenix.bind 't', [ 'ctrl', 'cmd' ], ->
focusApp "iTerm"
keys.push Phoenix.bind 'f', [ 'ctrl', 'cmd' ], ->
App.launch "Finder"
focusApp "Finder"
# ------------------------------------------------------------------------------
# Application move hotkeys
keys.push Phoenix.bind '1', [ 'ctrl', 'cmd' ], fn ->
moveCurrentWindowToScreen 1
keys.push Phoenix.bind '2', [ 'ctrl', 'cmd' ], fn ->
moveCurrentWindowToScreen 2
keys.push Phoenix.bind '3', [ 'ctrl', 'cmd' ], fn ->
moveCurrentWindowToScreen 3
# ------------------------------------------------------------------------------
# Application resize hotkeys
keys.push Phoenix.bind 'return', ['ctrl', 'cmd'], ->
Window.focusedWindow().maximize()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment