-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.lua
205 lines (183 loc) · 5.69 KB
/
init.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
--- === MinimizedWindowsMenu ===
---
--- Menubar menu for showing and switching to minimized windows.
--- Shows windows of the current space only.
local spaces = require("hs._asm.undocumented.spaces")
local inspect = require("hs.inspect")
local window = require("hs.window")
local fnutils = require("hs.fnutils")
local spoons = require("hs.spoons")
local image = require("hs.image")
local menubar = require("hs.menubar")
local obj={}
obj.__index = obj
-- Metadata
obj.name = "MinimizedWindowsMenu"
obj.version = "0.0"
obj.author = "B Viefhues"
obj.homepage = "https://github.com/bviefhues/MinimizedWindowsMenu.spoon"
obj.license = "MIT - https://opensource.org/licenses/MIT"
--- MinimizedWindowsMenu.logger
--- Variable
--- Logger object used within the Spoon. Can be accessed to set
--- the default log level for the messages coming from the Spoon.
obj.log = hs.logger.new("MinimizedWindowsMenu")
-- Internal: MinimizedWindowsMenu.menubar
-- Variable
-- Contains the Spoons hs.menubar.
obj.menubar = nil
-- Internal: MinimizedWindowsMenu.appIconCache
-- Variable
-- Dict for caching app icon per app name.
obj.appIconCache = {}
-- Internal: MinimizedWindowsMenu.menubarIcon
-- Variable
-- The menubar icon.
obj.menubarIcon = [[ASCII:
. . . . . . . . . . . . . . . . . . . .
. . h a # # # # # # # # # # # # a b . .
. h i # # # # # # # # # # # # # # i b .
. g . . . . . . . . . . . . . . . . c .
. # . . . . . . . . . . . . . . . . # .
. # . . . . . . E # # # E F . . . . # .
. # . . . . . . . . . . . # . . . . # .
. # . . . . C # # C D . . # . . . . # .
. # . . . . . . . . # . . # . . . . # .
. g . . A # A B . . # . . F . . . . # .
. . . . . . . # . . D . . . . . . . # .
. 4 # # 3 . . B . . . . . . . . . . c .
. # # # # . . . . . . . . . . . . . d .
. 1 # # 2 . e # # # # # # # # # e d . .
. . . . . . . . . . . . . . . . . . . .
]]
-- Internal: Utility function to debug windows states
function logWindows(text, windows)
obj.log.d(text)
for i, w in ipairs(windows) do
obj.log.d(" ", i,
"ID:"..w:id(),
"V:"..tostring(w:isVisible()):sub(1,1),
"S:"..tostring(w:isStandard()):sub(1,1),
"M:"..tostring(w:isMinimized()):sub(1,1),
"("..w:title():sub(1,25)..")")
end
end
-- Internal: Make window visible by unminimizing, raising and focussing
function obj.makeWindowVisible(modifiers, menuItem)
obj.log.d("> makeWindowVisible",
inspect(modifiers), inspect(menuItem))
menuItem.window:unminimize():raise():focus()
obj.log.d("< makeWindowVisible")
end
-- Internal: Get the application icon for a window
-- Caches the generated app icons, per application name
function obj.iconForWindow(window)
local application = window:application()
obj.log.d("> iconForWindow()", window:title(), application:name())
obj.log.d("Getting cached app icon")
local icon = obj.appIconCache[application:name()]
if not icon then
obj.log.d("Getting icon from imageFromAppBundle()")
icon = image.imageFromAppBundle(application:bundleID())
icon = icon:copy():size({w=16,h=16})
obj.appIconCache[application:name()] = icon
end
obj.log.d("< iconForWindow()")
return icon
end
-- Internal: Generates the menu table for the menu bar.
-- Will be dunamically evaluated each time the menu is displayed.
function obj.menuTable()
obj.log.d("> menuTable")
local menuTable = {}
for _, w in ipairs(window.minimizedWindows()) do
-- filter for windows in current space
if fnutils.contains(w:spaces(), spaces.activeSpace()) then
table.insert(menuTable, {
title = w:title(),
image = obj.iconForWindow(w), -- window app icon
fn = obj.makeWindowVisible,
window = w, -- remember window object to process in fn
})
end
end
if #menuTable == 0 then -- no minimized windows
menuTable = {{
title = "(none)",
disabled = true,
}}
end
-- obj.log.d("< menuTable ->", inspect.inspect(menuTable))
obj.log.d("< menuTable ->", "(...)")
return menuTable
end
-- Internal for the time being, leaving in in case keybeindings added later
-- MinimizedWindowsMenu:bindHotkeys(mapping)
-- Method
-- Binds hotkeys for MinimizedWindowsMenu
--
-- Parameters:
-- * mapping - A table containing hotkey modifier/key details for
-- the following items:
-- *
--
-- Returns:
-- * The MinimizedWindowsMenu object
function obj:bindHotkeys(mapping)
obj.log.d("> bindHotkeys", inspect(mapping))
local def = {
}
spoons.bindHotkeysToSpec(def, mapping)
obj.log.d("< bindHotkeys")
return self
end
--- MinimizedWindowsMenu:start()
--- Method
--- Starts MinimizedWindowsMenu spoon
---
--- Parameters:
--- * None
---
--- Returns:
--- * The MinimizedWindowsMenu object
function obj:start()
obj.log.d("> start")
obj.menubar = menubar.new()
obj.menubar:setIcon(obj.menubarIcon)
obj.menubar:setMenu(obj.menuTable)
obj.log.d("< start")
return self
end
--- MinimizedWindowsMenu:stop()
--- Method
--- Stops MinimizedWindowsMenu spoon
---
--- Parameters:
--- * None
---
--- Returns:
--- * The MinimizedWindowsMenu object
function obj:stop()
obj.log.d("> stop")
if obj.menubar then obj.menubar:delete() end
obj.menubar = nil
obj.log.d("< stop")
return self
end
--- MinimizedWindowsMenu:setLogLevel()
--- Method
--- Set the log level of the spoon logger.
--- Utility method for chaining.
---
--- Parameters:
--- * Log level
---
--- Returns:
--- * The MinimizedWindowsMenu object
function obj:setLogLevel(level)
obj.log.d("> setLogLevel")
obj.log.setLogLevel(level)
obj.log.d("< setLogLevel")
return self
end
return obj