Skip to content

Commit

Permalink
fixup! Add a floating layer
Browse files Browse the repository at this point in the history
  • Loading branch information
zakj committed Sep 2, 2024
1 parent c026e68 commit f47357b
Showing 1 changed file with 37 additions and 14 deletions.
51 changes: 37 additions & 14 deletions init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ PaperWM.license = "MIT - https://opensource.org/licenses/MIT"
PaperWM.default_hotkeys = {
stop_events = { { "alt", "cmd", "shift" }, "q" },
refresh_windows = { { "alt", "cmd", "shift" }, "r" },
toggle_floating = { { "alt", "cmd", "shift" }, "escape" },
focus_left = { { "alt", "cmd" }, "left" },
focus_right = { { "alt", "cmd" }, "right" },
focus_up = { { "alt", "cmd" }, "up" },
Expand Down Expand Up @@ -217,6 +218,15 @@ local function updateIndexTable(space, column)
end
end

---save the is_floating list to settings
local function persistFloatingList()
local persisted = {}
for k, _ in pairs(is_floating) do
table.insert(persisted, k)
end
hs.settings.set(IsFloatingKey, persisted)
end

local focused_window = nil ---@type Window|nil
local pending_window = nil ---@type Window|nil

Expand All @@ -236,7 +246,15 @@ local function windowEventHandler(window, event, self)
after the window was added ]]
--

if is_floating[window:id()] then return end
if is_floating[window:id()] then
-- this event is only meaningful for floating windows
if event == "windowDestroyed" then
is_floating[window:id()] = nil
persistFloatingList()
end
-- no other events are meaningful for floating windows
return
end

if event == "windowFocused" then
if pending_window and window == pending_window then
Expand Down Expand Up @@ -338,10 +356,15 @@ function PaperWM:start()
ui_watchers = {}
is_floating = {}

-- restore saved is_floating state, filtering for valid windows
local persisted = hs.settings.get(IsFloatingKey) or {}
for _, id in ipairs(persisted) do
is_floating[id] = true
local window = Window.get(id)
if window and self.window_filter:isWindowAllowed(window) then
is_floating[id] = true
end
end
persistFloatingList()

-- populate window list, index table, ui_watchers, and set initial layout
self:refreshWindows()
Expand All @@ -350,7 +373,7 @@ function PaperWM:start()
self.window_filter:subscribe({
WindowFilter.windowFocused, WindowFilter.windowVisible,
WindowFilter.windowNotVisible, WindowFilter.windowFullscreened,
WindowFilter.windowUnfullscreened
WindowFilter.windowUnfullscreened, WindowFilter.windowDestroyed
}, function(window, _, event) windowEventHandler(window, event, self) end)

-- watch for external monitor plug / unplug
Expand Down Expand Up @@ -1209,22 +1232,22 @@ function PaperWM:toggleFloating()
end

local id = window:id()
local space = nil
is_floating[id] = not is_floating[id]
local persisted = {}
for k, v in pairs(is_floating) do
if v then table.insert(persisted, k) end
if is_floating[id] then
is_floating[id] = nil
else
is_floating[id] = true
end
hs.settings.set(IsFloatingKey, persisted)
persistFloatingList()

local space = nil
if is_floating[id] then
space = index_table[id]['space']
self:removeWindow(window, true)
space = self:removeWindow(window, true)
else
self:addWindow(window)
space = index_table[id]['space']
space = self:addWindow(window)
end
if space then
self:tileSpace(space)
end
self:tileSpace(space)
end

---supported window movement actions
Expand Down

0 comments on commit f47357b

Please sign in to comment.