hammerspoon/modules/windows.lua
谭鹏(深圳联友科技有限公司创新产品群数字营销产品中心) 768e9ad516 new: 大量修改
2024-02-27 15:18:15 +08:00

179 lines
5.0 KiB
Lua

-- window management
local application = require "hs.application"
local hotkey = require "hs.hotkey"
local window = require "hs.window"
local layout = require "hs.layout"
local grid = require "hs.grid"
local hints = require "hs.hints"
local screen = require "hs.screen"
local alert = require "hs.alert"
local fnutils = require "hs.fnutils"
local geometry = require "hs.geometry"
local mouse = require "hs.mouse"
local MOFIFER_KEY_HALF = f19
-- default 0.2
window.animationDuration = 0.1
-- left half
hotkey.bind(MOFIFER_KEY_HALF, "Left", function()
if window.focusedWindow() then
window.focusedWindow():moveToUnit(layout.left50)
else
alert.show("No active window")
end
end)
-- right half
hotkey.bind(MOFIFER_KEY_HALF, "Right", function()
window.focusedWindow():moveToUnit(layout.right50)
end)
-- top half
hotkey.bind(MOFIFER_KEY_HALF, "Up", function()
window.focusedWindow():moveToUnit'[0,0,100,50]'
end)
-- bottom half
hotkey.bind(MOFIFER_KEY_HALF, "Down", function()
window.focusedWindow():moveToUnit'[0,50,100,100]'
end)
-- left top quarter
hotkey.bind(MOFIFER_KEY_HALF, "[", function()
window.focusedWindow():moveToUnit'[0,0,50,50]'
end)
-- right bottom quarter
hotkey.bind(MOFIFER_KEY_HALF, "'", function()
window.focusedWindow():moveToUnit'[50,50,100,100]'
end)
-- right top quarter
hotkey.bind(MOFIFER_KEY_HALF, "]", function()
window.focusedWindow():moveToUnit'[50,0,100,50]'
end)
-- left bottom quarter
hotkey.bind(MOFIFER_KEY_HALF, ";", function()
window.focusedWindow():moveToUnit'[0,50,50,100]'
end)
-- full screen
hotkey.bind(hyper, 'F', function()
window.focusedWindow():toggleFullScreen()
end)
-- center window
hotkey.bind(hyper, 'C', function()
window.focusedWindow():centerOnScreen()
end)
-- maximize window
hotkey.bind(MOFIFER_KEY_HALF, 'return', function() window.focusedWindow(): maximize() end)
-- defines for window maximize toggler
local frameCache = {}
-- toggle a window between its normal size, and being maximized
function toggle_maximize()
local win = window.focusedWindow()
if frameCache[win:id()] then
win:setFrame(frameCache[win:id()])
frameCache[win:id()] = nil
else
frameCache[win:id()] = win:frame()
win:maximize()
end
end
-- display a keyboard hint for switching focus to each window
-- hotkey.bind(hyperShift, '/', function()
-- hints.windowHints()
-- -- Display current application window
-- -- hints.windowHints(hs.window.focusedWindow():application():allWindows())
-- end)
-- switch active window
-- hotkey.bind(hyperShift, "H", function()
-- window.switcher.nextWindow()
-- end)
-- move cursor to previous/next monitor
-- hotkey.bind(hyperCtrl, "Right", function ()
-- focusScreen(window.focusedWindow():screen():previous())
-- end)
hotkey.bind(f19, "`", function ()
focusScreen(window.focusedWindow():screen():next())
end)
--Predicate that checks if a window belongs to a screen
function isInScreen(screen, win)
return win:screen() == screen
end
function focusScreen(screen)
--Get windows within screen, ordered from front to back.
--If no windows exist, bring focus to desktop. Otherwise, set focus on
--front-most application window.
local windows = fnutils.filter(
window.orderedWindows(),
fnutils.partial(isInScreen, screen))
local windowToFocus = #windows > 0 and windows[1] or window.desktop()
if(windowToFocus ~=nil ) then
windowToFocus:focus()
end
-- move cursor to center of screen
local pt = geometry.rectMidPoint(screen:fullFrame())
mouse.setAbsolutePosition(pt)
end
-- maximized active window and move to selected monitor
moveto = function(win, n)
local screens = screen.allScreens()
if n > #screens then
alert.show("Only " .. #screens .. " monitors ")
else
local toWin = screen.allScreens()[n]:name()
alert.show("Move " .. win:application():name() .. " to " .. toWin)
layout.apply({{nil, win:title(), toWin, layout.maximized, nil, nil}})
end
end
-- cycled move active window to another screen
hs.hotkey.bind( f19, 'tab', 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()
-- compute the unitRect of the focused window relative to the current screen
-- and move the window to the next screen setting the same unitRect
win:move(win:frame():toUnitRect(screen:frame()), screen:next(), true, 0)
end)
-- -- move active window to monitor 1 and maximize the window
-- hotkey.bind(f19, "1", function()
-- local win = window.focusedWindow()
-- moveto(win, 1)
-- end)
-- hotkey.bind(f19, "2", function()
-- local win = window.focusedWindow()
-- moveto(win, 2)
-- end)
-- hotkey.bind(f19, "3", function()
-- local win = window.focusedWindow()
-- moveto(win, 3)
-- end)
-- -- move active window to previous/next monitor
-- hotkey.bind(MOFIFER_KEY_HALF, '-', function()
-- window.focusedWindow():moveOneScreenWest():maximize()
-- end)
-- hotkey.bind(MOFIFER_KEY_HALF, '=', function()
-- window.focusedWindow():moveOneScreenEast():maximize()
-- end)