dotfiles

My PC Dotfiles & Website

files/noctalia/keyhelp/lib/controller.luau (1733B)


 1local M = {}
 2
 3function M.new(host, alreadyOpen)
 4  local self = { phase = alreadyOpen and "open" or "closed", desired = alreadyOpen or false, generation = 0 }
 5  local function begin()
 6    self.generation = self.generation + 1
 7    local generation = self.generation
 8    self.phase = "pending"
 9    host.detect(self.backend, function(appIds)
10      if generation ~= self.generation or self.phase ~= "pending" or not self.desired then
11        return
12      end
13      self.phase = "opening"
14      host.open({ compositor = self.backend, appIds = appIds })
15    end)
16  end
17
18  function self.toggle(backend)
19    if backend ~= "hyprland" and backend ~= "niri" then
20      return false
21    end
22    self.backend = backend
23    self.desired = not self.desired
24    if self.phase == "closed" and self.desired then
25      begin()
26    elseif self.phase == "pending" and not self.desired then
27      self.generation = self.generation + 1
28      self.phase = "closed"
29    elseif self.phase == "open" and not self.desired then
30      self.phase = "closing"
31      host.close()
32    end
33    return true
34  end
35
36  function self.lifecycle(open)
37    if open then
38      if self.phase ~= "opening" and self.phase ~= "closing" then
39        self.desired = true
40      end
41      self.phase = "open"
42      if not self.desired then
43        self.phase = "closing"
44        host.close()
45      end
46    else
47      local reopen = self.phase == "closing" and self.desired
48      self.generation = self.generation + 1
49      self.phase = "closed"
50      self.desired = reopen
51      if reopen then
52        begin()
53      end
54    end
55  end
56
57  function self.stop()
58    self.generation = self.generation + 1
59    self.desired = false
60    self.phase = "closed"
61  end
62
63  return self
64end
65
66return M