Skip to content

Instantly share code, notes, and snippets.

@colindean
Created June 10, 2014 02:52
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 colindean/3fa219643462c1604f16 to your computer and use it in GitHub Desktop.
Save colindean/3fa219643462c1604f16 to your computer and use it in GitHub Desktop.
Zephyros/Phoenix/Hydra configurations
// originally from https://gist.github.com/sdegutis/7756583
var mash = ["cmd", "alt", "ctrl"];
var mashShift = ["cmd", "alt", "shift"];
var MARGIN_X = 5;
var MARGIN_Y = 5;
var GRID_WIDTH = 3;
Window.prototype.getGrid = function() {
var winFrame = this.frame();
var screenRect = this.screen().frameWithoutDockOrMenu();
var thirdScreenWidth = screenRect.width / GRID_WIDTH;
var halfScreenHeight = screenRect.height / 2;
return {
x: Math.round((winFrame.x - screenRect.x) / thirdScreenWidth),
y: Math.round((winFrame.y - screenRect.y) / halfScreenHeight),
w: Math.max(1, Math.round(winFrame.width / thirdScreenWidth)),
h: Math.max(1, Math.round(winFrame.height / halfScreenHeight))
};
};
Window.prototype.setGrid = function(grid, screen) {
var screenRect = screen.frameWithoutDockOrMenu();
var thirdScreenWidth = screenRect.width / GRID_WIDTH;
var halfScreenHeight = screenRect.height / 2;
var newFrame = {
x: (grid.x * thirdScreenWidth) + screenRect.x,
y: (grid.y * halfScreenHeight) + screenRect.y,
width: grid.w * thirdScreenWidth,
height: grid.h * halfScreenHeight
};
newFrame.x += MARGIN_X;
newFrame.y += MARGIN_Y;
newFrame.width -= (MARGIN_X * 2.0);
newFrame.height -= (MARGIN_Y * 2.0);
this.setFrame(newFrame);
}
Window.prototype.snapToGrid = function() {
if (this.isNormalWindow()) {
this.setGrid(this.getGrid(), this.screen());
}
}
function changeGridWidth(by) {
GRID_WIDTH = Math.max(1, GRID_WIDTH + by);
api.alert("grid is now " + GRID_WIDTH + " tiles wide", 1);
_.each(Window.visibleWindows(), function(win) { win.snapToGrid(); });
}
api.bind('D', mash, function() { api.launch("Dictionary"); });
api.bind(';', mash, function() { Window.focusedWindow().snapToGrid(); });
api.bind("'", mash, function() { _.each(Window.visibleWindows(), function(win) { win.snapToGrid(); }); });
api.bind('=', mash, function() { changeGridWidth(+1); });
api.bind('-', mash, function() { changeGridWidth(-1); });
api.bind('H', mashShift, function() { Window.focusedWindow().focusWindowLeft(); });
api.bind('L', mashShift, function() { Window.focusedWindow().focusWindowRight(); });
api.bind('K', mashShift, function() { Window.focusedWindow().focusWindowUp(); });
api.bind('J', mashShift, function() { Window.focusedWindow().focusWindowDown(); });
api.bind('M', mash, function() {
var win = Window.focusedWindow();
var f = {x: 0, y: 0, w: GRID_WIDTH, h: 2};
win.setGrid(f, win.screen());
return true;
});
api.bind('N', mash, function() {
var win = Window.focusedWindow();
win.setGrid(win.getGrid(), win.screen().nextScreen());
return true;
});
api.bind('P', mash, function() {
var win = Window.focusedWindow();
win.setGrid(win.getGrid(), win.screen().previousScreen());
return true;
});
api.bind('H', mash, function() {
var win = Window.focusedWindow();
var f = win.getGrid();
f.x = Math.max(f.x - 1, 0);
win.setGrid(f, win.screen());
return true;
});
api.bind('L', mash, function() {
var win = Window.focusedWindow();
var f = win.getGrid();
f.x = Math.min(f.x + 1, GRID_WIDTH - f.w);
win.setGrid(f, win.screen());
return true;
});
api.bind('O', mash, function() {
var win = Window.focusedWindow();
var f = win.getGrid();
f.w = Math.min(f.w + 1, GRID_WIDTH - f.x);
win.setGrid(f, win.screen());
return true;
});
api.bind('I', mash, function() {
var win = Window.focusedWindow();
var f = win.getGrid();
f.w = Math.max(f.w - 1, 1);
win.setGrid(f, win.screen());
return true;
});
api.bind('J', mash, function() {
var win = Window.focusedWindow();
var f = win.getGrid();
f.y = 1;
f.h = 1;
win.setGrid(f, win.screen());
return true;
});
api.bind('K', mash, function() {
var win = Window.focusedWindow();
var f = win.getGrid();
f.y = 0;
f.h = 1;
win.setGrid(f, win.screen());
return true;
});
api.bind('U', mash, function() {
var win = Window.focusedWindow();
var f = win.getGrid();
f.y = 0;
f.h = 2;
win.setGrid(f, win.screen());
return true;
});
require '/Applications/Zephyros.app/Contents/Resources/libs/zephyros.rb'
# require '~/Dropbox/projects/zephyros/libs/zephyros.rb'
mash = ["cmd", "alt", "ctrl"]
mash_shift = ["cmd", "alt", "shift"]
$window_grid_width = 4
API.update_settings({:alert_should_animate => false,
:alert_default_delay => 0.5})
API.bind('D', mash) { `open -a Dictionary` }
API.bind('X', mash) do
actions = {
'Zephyros' => lambda { `open /Users/sdegutis/projects/Zephyros/Zephyros.xcodeproj` },
'Zephyros README' => lambda { `open -aemacs /Users/sdegutis/projects/Zephyros/README.md` },
'Open email' => lambda { 2.times {|i| `open https://mail.google.com/mail/u/#{i}/#inbox` } },
'Show clipboard' => lambda { API.alert API.clipboard_contents, 3 },
}
action_names = actions.keys
API.choose_from action_names, 'Do Something', 5, 20 do |i|
actions[action_names[i]].call if i
end
end
class Window
def snap_to_grid
self.set_grid get_grid, nil if normal_window?
end
def maximize_with_margins
f = self.screen.frame_without_dock_or_menu
f.inset! $window_grid_margin_x, $window_grid_margin_y
self.frame = f
end
end
def change_grid_width(by)
$window_grid_width = [1, $window_grid_width + by].max
API.alert "grid is now #{$window_grid_width} tiles wide"
API.visible_windows.each(&:snap_to_grid)
end
API.bind(';', mash) { API.focused_window.snap_to_grid }
API.bind("'", mash) { API.visible_windows.map(&:snap_to_grid) }
API.bind('=', mash) { change_grid_width +1 }
API.bind('-', mash) { change_grid_width -1 }
API.bind('H', mash_shift) { API.focused_window.focus_window_left }
API.bind('L', mash_shift) { API.focused_window.focus_window_right }
API.bind('K', mash_shift) { API.focused_window.focus_window_up }
API.bind('J', mash_shift) { API.focused_window.focus_window_down }
API.bind 'N', mash do
win = API.focused_window
win.set_grid win.get_grid, win.screen.next_screen
end
API.bind 'P', mash do
win = API.focused_window
win.set_grid win.get_grid, win.screen.previous_screen
end
API.bind 'M', mash do
win = API.focused_window
win.maximize_with_margins
end
API.bind 'H', mash do
win = API.focused_window
r = win.get_grid
r.x = [r.x - 1, 0].max
win.set_grid r, nil
end
API.bind 'L', mash do
win = API.focused_window
r = win.get_grid
r.x = [r.x + 1, $window_grid_width - r.w].min
win.set_grid r, nil
end
API.bind 'O', mash do
win = API.focused_window
r = win.get_grid
r.w = [r.w + 1, $window_grid_width - r.x].min
win.set_grid r, nil
end
API.bind 'I', mash do
win = API.focused_window
r = win.get_grid
r.w = [r.w - 1, 1].max
win.set_grid r, nil
end
API.bind 'J', mash do
win = API.focused_window
r = win.get_grid
r.y = 1
r.h = 1
win.set_grid r, nil
end
API.bind 'K', mash do
win = API.focused_window
r = win.get_grid
r.y = 0
r.h = 1
win.set_grid r, nil
end
API.bind 'U', mash do
win = API.focused_window
r = win.get_grid
r.y = 0
r.h = 2
win.set_grid r, nil
end
def snap_new_window_to_grid(win)
case win.app.title
when 'Google Chrome'
win.maximize_with_margins if win.normal_window?
when 'Emacs'
win.snap_to_grid
end
end
# API.listen 'window_created' do |win|
# snap_new_window_to_grid win
# end
# API.listen 'app_launched' do |app|
# app.visible_windows.each { |win| snap_new_window_to_grid win }
# end
wait_on_callbacks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment