From a3cb685eb03c256221e8412640fd8ab5a044df9c Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Mon, 14 Oct 2019 21:53:34 +0200 Subject: window_layout: Restore all window positions when monitors change Save all window positions when windows are moved, resized, created, deleted. When the primary monitor changes, restore previously saved window positions for that monitor. This ensures that our windows stay in the same position even after connecting and disconnecting an external monitor. --- window_layout.lua | 40 +++++++++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 11 deletions(-) (limited to 'window_layout.lua') diff --git a/window_layout.lua b/window_layout.lua index 0fbd1e9..408498c 100644 --- a/window_layout.lua +++ b/window_layout.lua @@ -14,22 +14,40 @@ -- along with this program. If not, see . --- Do this when external monitor is connected --- https://www.hammerspoon.org/docs/hs.screen.watcher.html --- hs.screen.primaryScreen():name() == 'G247HL' -local function iterm2() - for _, window in ipairs(hs.application.find('com.googlecode.iterm2'):allWindows()) do - if window:title():match('^2%. ') then - window:setFrame(hs.geometry.rect(-1253.0,23.0,793.0,873.0)) - return +all_window_filter = hs.window.filter.new():setOverrideFilter({ visible = true }) +window_positions = {} + +local function window_positions_save(window, _app_name, event_type) + local screen = hs.screen.primaryScreen():id() + + if event_type == hs.window.filter.windowsChanged then + window_positions[screen] = {} + + for _, window in ipairs(all_window_filter:getWindows()) do + window_positions[screen][window:id()] = window:frame() end + elseif event_type == hs.window.filter.windowMoved then + window_positions[screen][window:id()] = window:frame() end end +local function window_positions_restore() + local screen = hs.screen.primaryScreen():id() -screen_watcher = hs.screen.watcher.new(function() - if hs.screen.primaryScreen():name() == 'G247HL' then - iterm2() + for id, frame in pairs(window_positions[screen]) do + hs.window.get(id):setFrame(frame) end +end + +-- Save window positions on load +window_positions_save(nil, nil, hs.window.filter.windowsChanged) + +all_window_filter + :subscribe(hs.window.filter.windowMoved, window_positions_save) + :subscribe(hs.window.filter.windowsChanged, window_positions_save) + + +screen_watcher = hs.screen.watcher.new(function() + window_positions_restore() end) screen_watcher:start() -- cgit v1.2.3