Skip to content

Instantly share code, notes, and snippets.

@slumos
Created November 26, 2014 00:41
Show Gist options
  • Save slumos/cb7cc62efd3a22adcb1d to your computer and use it in GitHub Desktop.
Save slumos/cb7cc62efd3a22adcb1d to your computer and use it in GitHub Desktop.
~/.mjolnir/init.lua
local app = require "mjolnir.application"
local hotkey = require "mjolnir.hotkey"
local window = require "mjolnir.window"
local fnutils = require "mjolnir.fnutils"
local geom = require "mjolnir.geometry"
local screen = require "mjolnir.screen"
local alert = require "mjolnir.alert"
local MOD = {"cmd", "ctrl"}
-- Move and resize window to the left or right half of the screen.
-- If window is at the edge and there is another screen in that
-- direction, throw the window to that screen instead.
function move_or_throw(target_window, direction)
local current_screen = target_window:screen()
local window_frame = target_window:frame()
local screen_frame = current_screen:frame()
if string.match(direction, "left") then
local left_screen = current_screen:towest()
if left_screen and at_left_edge(window_frame, screen_frame) then
make_right_half(target_window, left_screen)
else
make_left_half(target_window, current_screen)
end
elseif string.match(direction, "right") then
local right_screen = current_screen:toeast()
if right_screen and at_right_edge(window_frame, screen_frame) then
make_left_half(target_window, right_screen)
else
make_right_half(target_window, current_screen)
end
end
end
hotkey.bind(MOD, "left", function()
move_or_throw(window.focusedwindow(), "left")
end)
hotkey.bind(MOD, "right", function()
move_or_throw(window.focusedwindow(), "right")
end)
-- For my laptop screen, I prefer to use full-screen windows when
-- mobile, but maximized windows when plugged into my desk.
hotkey.bind(MOD, "up", function()
if #screen.allscreens() > 1 then
window.focusedwindow():maximize()
else
window.focusedwindow():setfullscreen(true)
end
end)
-- hotkey.bind(MOD, "R", function()
-- mjolnir.reload()
-- alert.show("Mjolnir config reloaded")
-- end)
-- hotkey.bind(MOD, "M", function()
-- mjolnir.openconsole()
-- end)
function make_left_half(target_window, target_screen)
move_to_screen_unit(target_window, target_screen, geom.rect(0, 0, 0.5, 1))
end
function make_right_half(target_window, target_screen)
move_to_screen_unit(target_window, target_screen, geom.rect(0.5, 0, 0.5, 1))
end
function move_to_screen_unit(target_window, target_screen, target_unit)
local screen_frame = target_screen:frame()
target_window:setframe({x = screen_frame.x + (target_unit.x * screen_frame.w),
y = screen_frame.y + (target_unit.y * screen_frame.h),
w = target_unit.w * screen_frame.w,
h = target_unit.h * screen_frame.h})
end
-- Is the inner rectangle at the edge of the outer? With fuzziness
-- for windows (Emacs) that don't like to be right up against the edge
-- of a screen.
local EDGE_FUZZ = 5
function at_left_edge(inner_frame, outer_frame)
return inner_frame.x - outer_frame.x - EDGE_FUZZ <= 0
end
function at_right_edge(inner_frame, outer_frame)
inner_edge = inner_frame.x + inner_frame.w
outer_edge = outer_frame.x + outer_frame.w
return outer_edge - inner_edge - EDGE_FUZZ <= 0
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment