-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgtk.lua
More file actions
292 lines (215 loc) · 6.84 KB
/
gtk.lua
File metadata and controls
292 lines (215 loc) · 6.84 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
-- LuaX does not handle LuaJIT's lack of a searcher for <module>/init.lua, so we must add it to the path
do
local sep = package.path:match("[/\\]")
local local_init = ("./?/init.lua"):gsub("/", sep)
if not package.path:match("%.[/\\]?[/\\]init%.lua") then
package.path = package.path .. ";" .. local_init
end
end
---@type LuaX
local LuaX = require("src")
local GtkElement = LuaX.GtkElement
local ErrorBoundary = LuaX.ErrorBoundary
local Suspense = LuaX.Suspense
local use_context = LuaX.use_context
local use_suspense = LuaX.use_suspense
local GLibIdleWorkloop = require("src.util.WorkLoop.GLibIdle")
local lgi = require("lgi")
local Gtk = lgi.require("Gtk", "3.0")
local Gio = lgi.Gio
local GLib = lgi.GLib
local use_state = LuaX.use_state
local use_effect = LuaX.use_effect
local min = math.min
-- see docs/Parser.md in regard to the "LuaX." on every Gtk element name
-- Gtk.Notebook has a complicated API, which we'll abstract away using a component
local EasyNotebook = LuaX(function(props)
local labels = props.labels or {}
local notebook, set_notebook = use_state(nil)
local page_count, set_page_count = use_state(0)
use_effect(function()
if not notebook then
return
end
if page_count == 0 then
return
end
local iterations = min(page_count, #labels)
for i = 1, iterations do
local child = notebook:get_nth_page(i - 1)
notebook:set_tab_label_text(child, labels[i])
end
end, { notebook, labels, page_count })
return [[
<LuaX.Gtk.Notebook
-- The LuaX library handles this special property
LuaX::onload={function (w) set_notebook(w) end}
on_page_added={function (w)
set_page_count(w:get_n_pages())
end}
on_page_removed={function (w)
set_page_count(w:get_n_pages())
end}
>
{props.children}
</LuaX.Gtk.Notebook>
]]
end)
local map = function(list, cb)
local ret = {}
for i, item in ipairs(list) do
ret[i] = cb(item)
end
return ret
end
local MessageContext = LuaX.Context("Hello World!")
local Display = LuaX(function()
local message = use_context(MessageContext)
return [[
<LuaX.Gtk.Label>
{message}
</LuaX.Gtk.Label>
]]
end)
local ContextPage = LuaX(function()
local messages = {
{ value = "Message 1!" },
{ value = "Message 2!" },
-- Passing a nil value to MessageContext.Provider results in the default value being returned by use_context
{ value = nil }
}
return [[
<LuaX.Gtk.VBox>
{map(messages, function (message)
return (
<MessageContext.Provider value={message.value} >
<>
<Display />
</>
</MessageContext.Provider>
)
end)}
</LuaX.Gtk.VBox>
]]
end)
local ErrorComponent = LuaX(function()
return [[
<LuaX.Gtk.Button on_clicked={function ()
error("Throw up!")
end}>
I'm going to throw up!
</LuaX.Gtk.Button>
]]
end)
-- If you wish to consume the error with your fallback component, passing the
-- component (as opposed to an instance of it) to ErrorBoundary will create the
-- element, passing the error in props
local ErrorMessage = LuaX(function (props)
local err = props.error
return [[
<LuaX.Gtk.Label>
An error occurred {err and " - " .. err or ""}
</LuaX.Gtk.Label>
]]
end)
local ErrorPage = LuaX(function()
return [[
<LuaX.Gtk.VBox>
<ErrorBoundary fallback={ErrorMessage}>
<ErrorComponent />
</ErrorBoundary>
</LuaX.Gtk.VBox>
]]
end)
local SuspenseComponent = LuaX(function()
-- Because Lua doesn't have a default Promises implementation, use_suspense
-- returns two functions - suspend() defers rendering, and resolve() does
-- the opposite.
local suspend, resolve = use_suspense()
local clicks, set_clicks = use_state(0)
-- We use GLib.timeout_add to create a 1 second delay every time the click
-- count changes.
use_effect(function()
print("Suspend")
suspend()
GLib.timeout_add(0, 1000, function()
print("Resolve")
resolve()
return false
end)
end, { clicks })
return [[
<LuaX.Gtk.Button
on_clicked={function ()
set_clicks(function (clicks)
return clicks + 1
end)
end}
>
Hello World!
</LuaX.Gtk.Button>
]]
end)
local SuspensePage = LuaX(function()
return [[
<LuaX.Gtk.VBox>
<Suspense fallback={<LuaX.Gtk.Spinner LuaX::onload={function (w) w:start() end} />}>
<SuspenseComponent />
</Suspense>
</LuaX.Gtk.VBox>
]]
end)
local App = LuaX(function()
local clicks, set_clicks = use_state(0)
-- Demos will maintain their states because they're all being rendered
-- simultaneously into the EasyNotebook.
return [[
<EasyNotebook
labels={{
"Click counter",
"Contexts",
"Error boundary",
"Suspense"
}}
>
-- page 1: use_state clicking example
<LuaX.Gtk.VBox>
<LuaX.Gtk.Label>
You clicked {clicks} times!
</LuaX.Gtk.Label>
<LuaX.Gtk.Button
on_clicked={function ()
set_clicks(function (clicks)
return clicks + 1
end)
end}
>
Click me!
</LuaX.Gtk.Button>
</LuaX.Gtk.VBox>
-- page 2: contexts
<ContextPage />
-- page 3: error boundary
<ErrorPage />
-- page 4: suspense
<SuspensePage />
</EasyNotebook>
]]
end)
local app = Gtk.Application.new("LuaX.GtkExample", Gio.ApplicationFlags.DEFAULT_FLAGS)
function app:on_startup()
local window = Gtk.ApplicationWindow.new(self)
window:set_title("LuaX demo")
local root = GtkElement.get_root(window)
local elem = LuaX.create_element(App, {})
-- GLibIdleWorkloop provides slightly smoother animations compared to the
-- default blocking WorkLoop, but only functions under a GLib MainLoop
local renderer = LuaX.Renderer(GLibIdleWorkloop)
renderer:render(elem, root)
self:add_window(window)
end
function app:on_activate()
self.active_window:show()
self.active_window:present()
end
return app:run({ arg[0], ... })