dotfiles/modules/darwin/hammerspoon/Spoons/MoveWindow.spoon/init.lua

82 lines
2.4 KiB
Lua
Raw Normal View History

2022-04-25 14:42:01 +00:00
--- === Move Window ===
local obj = {}
obj.__index = obj
-- Metadata
obj.name = "MoveWindow"
obj.version = "0.1"
obj.license = "MIT - https://opensource.org/licenses/MIT"
function obj:init()
hs.window.animationDuration = 0.1
2022-07-06 14:35:00 +00:00
dofile(hs.spoons.resourcePath("worklayout.lua"))()
2022-04-25 14:42:01 +00:00
-- bind hotkey
hs.hotkey.bind({ "alt", "ctrl", "cmd" }, "n", function()
-- get the focused window
local win = hs.window.focusedWindow()
-- get the screen where the focused window is displayed, a.k.a. current screen
local screen = win:screen()
2022-05-13 18:46:46 +00:00
-- local nextScreen = screen:next()
2022-04-25 14:42:01 +00:00
-- compute the unitRect of the focused window relative to the current screen
-- and move the window to the next screen setting the same unitRect
2022-05-13 18:46:46 +00:00
win:moveToScreen(screen:next(), true, true, 0)
end)
hs.hotkey.bind({ "alt", "ctrl", "cmd" }, "b", function()
local win = hs.window.focusedWindow()
local screen = win:screen()
win:moveToScreen(screen:previous(), true, true, 0)
2022-04-25 14:42:01 +00:00
end)
2022-04-29 14:50:29 +00:00
2023-04-03 15:51:04 +00:00
-- Maximize
2022-04-29 14:50:29 +00:00
hs.hotkey.bind({ "alt", "ctrl", "cmd" }, "m", function()
-- get the focused window
local win = hs.window.focusedWindow()
2022-07-06 14:35:00 +00:00
local frame = win:frame()
2022-04-29 14:50:29 +00:00
-- maximize if possible
2022-07-06 14:35:00 +00:00
local max = win:screen():fullFrame()
frame.x = max.x
frame.y = max.y
frame.w = max.w
frame.h = max.h
win:setFrame(frame)
-- -- first maximize to grid
-- hs.grid.maximizeWindow(win)
-- -- then spam maximize
-- for i = 1, 8 do
-- win:maximize()
-- end
2022-04-29 14:50:29 +00:00
end)
2023-04-03 15:51:04 +00:00
-- Half-maximize (right)
hs.hotkey.bind({ "alt", "ctrl", "cmd" }, "o", function()
-- get the focused window
local win = hs.window.focusedWindow()
local frame = win:frame()
-- maximize if possible
local max = win:screen():fullFrame()
frame.x = (max.x * 2 + max.w) / 2
2023-04-03 15:51:04 +00:00
frame.y = max.y
frame.w = max.w / 2
frame.h = max.h
win:setFrame(frame)
end)
-- Half-maximize (left)
hs.hotkey.bind({ "alt", "ctrl", "cmd" }, "u", function()
-- get the focused window
local win = hs.window.focusedWindow()
local frame = win:frame()
-- maximize if possible
local max = win:screen():fullFrame()
frame.x = max.x
frame.y = max.y
frame.w = max.w / 2
frame.h = max.h
win:setFrame(frame)
end)
2022-04-25 14:42:01 +00:00
end
return obj