diff options
author | Teddy Wing | 2019-10-14 21:53:34 +0200 |
---|---|---|
committer | Teddy Wing | 2019-10-14 21:53:34 +0200 |
commit | a3cb685eb03c256221e8412640fd8ab5a044df9c (patch) | |
tree | 0a66241278eb4c2a29b57750eb1c0d15b1374eef /window_layout.lua | |
parent | 16be80dc3608907223815c5d95bb59e2d3fc046f (diff) | |
download | dothammerspoon-a3cb685eb03c256221e8412640fd8ab5a044df9c.tar.bz2 |
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.
Diffstat (limited to 'window_layout.lua')
-rw-r--r-- | window_layout.lua | 40 |
1 files changed, 29 insertions, 11 deletions
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 <https://www.gnu.org/licenses/>. --- 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() |