-
Notifications
You must be signed in to change notification settings - Fork 22
/
chooserToolbar.lua
176 lines (165 loc) · 7.42 KB
/
chooserToolbar.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
local chooser = require("hs.chooser")
local toolbar = require("hs.webview.toolbar")
local canvas = require("hs.canvas")
local inspect = require("hs.inspect")
local stext = require("hs.styledtext")
local module = {}
local list1, list2, list3 = {}, {}, {}
for i = 1, 10, 1 do
table.insert(list1, { text = string.char(96 + i) } )
table.insert(list2, { text = tostring(i) })
table.insert(list3, { text = string.char(64 + i) })
end
local changeChooserEntries = function(list, bar, parent, item)
parent:choices(list)
parent:query("")
bar:selectedItem(item)
end
local toolbarItems = {
{
id = "choice1",
selectable = true,
label = "Lower Case",
image = canvas.new{ h = 50, w = 50 }:appendElements{
{
type = "rectangle",
strokeColor = { white = 1 },
fillColor = { red = .5 },
}, {
-- vertical centering is hard without putting the text into a local variable, finding its bounding box
-- with hs.canvas:minimumTextSize, then calculating the proper frame based on canvas dimensions... this
-- is "close enough" for this example...
frame = { h = 50, w = 50, x = 0, y = -6 },
text = stext.new("a", {
font = { name = ".AppleSystemUIFont", size = 50 },
paragraphStyle = { alignment = "center" }
}),
type = "text",
}
}:imageFromCanvas(),
fn = function(...) changeChooserEntries(list1, ...) end,
},
{
id = "choice2",
selectable = true,
label = "Numeric",
image = canvas.new{ h = 50, w = 50 }:appendElements{
{
type = "rectangle",
strokeColor = { white = 1 },
fillColor = { green = .5 },
}, {
frame = { h = 50, w = 50, x = 0, y = -6 },
text = stext.new("1", {
font = { name = ".AppleSystemUIFont", size = 50 },
paragraphStyle = { alignment = "center" }
}),
type = "text",
}
}:imageFromCanvas(),
fn = function(...) changeChooserEntries(list2, ...) end,
},
{
id = "choice3",
selectable = true,
label = "Upper Case",
image = canvas.new{ h = 50, w = 50 }:appendElements{
{
type = "rectangle",
strokeColor = { white = 1 },
fillColor = { blue = .5 },
}, {
frame = { h = 50, w = 50, x = 0, y = -6 },
text = stext.new("A", {
font = { name = ".AppleSystemUIFont", size = 50 },
paragraphStyle = { alignment = "center" }
}),
type = "text",
}
}:imageFromCanvas(),
fn = function(...) changeChooserEntries(list3, ...) end,
},
{
id = "hide",
selectable = false,
label = "Hide",
image = canvas.new{ h = 50, w = 50 }:appendElements{
{
frame = { h = 50, w = 50, x = 0, y = -6 },
text = stext.new("🚫", {
font = { name = ".AppleSystemUIFont", size = 50 },
paragraphStyle = { alignment = "center" }
}),
type = "text",
}
}:imageFromCanvas(),
fn = function(bar, parent, item) bar:visible(false) end,
},
{
id = "remove",
selectable = false,
label = "Remove",
image = canvas.new{ h = 50, w = 50 }:appendElements{
{
frame = { h = 50, w = 50, x = 0, y = -6 },
text = stext.new("☠", {
font = { name = ".AppleSystemUIFont", size = 50 },
paragraphStyle = { alignment = "center" }
}),
type = "text",
}
}:imageFromCanvas(),
fn = function(bar, parent, item) parent:attachedToolbar(nil) end,
},
{
id = "titleBar",
selectable = false,
label = "Adjust",
image = canvas.new{ h = 50, w = 50 }:appendElements{
{
frame = { h = 50, w = 50, x = 0, y = -6 },
text = stext.new("🔼", {
font = { name = ".AppleSystemUIFont", size = 50 },
paragraphStyle = { alignment = "center" }
}),
type = "text",
}
}:imageFromCanvas(),
fn = function(bar, parent, item)
bar:inTitleBar(not bar:inTitleBar())
if bar:inTitleBar() then
textToDisplay = "🔽"
else
textToDisplay = "🔼"
end
bar:modifyItem{
id = "titleBar",
image = canvas.new{ h = 50, w = 50 }:appendElements{
{
frame = { h = 50, w = 50, x = 0, y = -6 },
text = stext.new(textToDisplay, {
font = { name = ".AppleSystemUIFont", size = 50 },
paragraphStyle = { alignment = "center" }
}),
type = "text",
}
}:imageFromCanvas(),
}
end,
},
}
local _toolbar = toolbar.new("chooserToolbarTest")
:addItems(toolbarItems)
:canCustomize(true)
:setCallback(function(...)
print("+++ Oops! You better assign me something to do!")
end)
local _chooser = chooser.new(function(...)
print("Chooser results = " .. inspect(table.pack(...), { newline = " ", indent = "" }))
end):attachedToolbar(_toolbar)
changeChooserEntries(list1, _toolbar, _chooser, "choice1")
module._toolbar = _toolbar
module._chooser = _chooser
module.show = function() _chooser:show() end
module.attach = function() _chooser:attachedToolbar(_toolbar) end
return module