-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathstructbondmodule.jl
479 lines (383 loc) · 14.6 KB
/
structbondmodule.jl
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
### A Pluto.jl notebook ###
# v0.19.40
#> custom_attrs = ["hide-enabled"]
using Markdown
using InteractiveUtils
# This Pluto notebook uses @bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of @bind gives bound variables a default value (instead of an error).
macro bind(def, element)
quote
local iv = try Base.loaded_modules[Base.PkgId(Base.UUID("6e696c72-6542-2067-7265-42206c756150"), "AbstractPlutoDingetjes")].Bonds.initial_value catch; b -> missing; end
local el = $(esc(element))
global $(esc(def)) = Core.applicable(Base.get, el) ? Base.get(el) : iv(el)
el
end
end
# ╔═╡ 8db82e94-5c81-4c52-9228-7e22395fb68f
begin
using PlutoDevMacros
end
# ╔═╡ 949ac1ef-c502-4e28-81ff-f99b0d19aa03
@frompackage "../.." begin
using ^.StructBondModule
using ^: ExtendedTableOfContents, Editable
using >.HypertextLiteral
using >.PlutoUI
end
# ╔═╡ 9d4455af-96f1-46d7-a4f3-434495b11c8a
md"""
# Packages
"""
# ╔═╡ 707175a9-d356-43cf-8038-620ebc401c93
ExtendedTableOfContents()
# ╔═╡ cbfc6eec-8991-4a9c-ada0-295c8052854d
# html"""
# <style>
# main {
# margin-right: 350px !important;
# }
# </style>
# """
# ╔═╡ 4843983c-df64-4b94-8634-7a10d9423a70
md"""
# StructBond
"""
# ╔═╡ a8995224-83c6-4f82-b5a5-87a6f86fc7a0
md"""
Generating automatic widgets for custom structs is quite straightforward with the aid of the `@fielddata` macro and the `StructBond` type.
The top-left arrow can be used to show-hide the field elements (useful inside a `BondTable`, shown below), while the green toggle on the top-right can be used to disable temporarily the synch between the JS widgets and the bond values in Pluto.
"""
# ╔═╡ 9e3601f5-efc2-44e9-83d8-5b65ce7e9ccf
begin
"""
struct ASD
"""
Base.@kwdef struct ASD
a::Int
b::Int
c::String
"This field is _special_"
d::String
e::Int
end
@typeasfield String = TextField() # String fields with no struct-specific default will use this
@fielddata ASD begin
a = (md"Markdown description, including ``LaTeX``", Slider(1:10))
b = (@htl("<span>Field with <b>HTML</b> description</span>"), Scrubbable(1:10))
c = ("Normal String Description", TextField())
# d has no ASD-specific custom bond, so it will use the field docstring as description and the default String widget as widget
e = Slider(20:25) # No description, defaults to the fieldname as no docstring is present
end
asd_bond = @bind asd StructBond(ASD; description = "Custom Description")
end
# ╔═╡ cdc0a7ad-a3ac-4270-b707-35b1939da276
asd
# ╔═╡ fe891b4e-f440-4823-b43b-72572f6a6c12
md"""
## Default Type Widgets
"""
# ╔═╡ 86a80228-f495-43e8-b1d4-c93b7b52c8d8
begin
@kwdef struct MAH
a::Int
end
@kwdef struct BOH
mah::MAH
end
# This will make the default widget for an Int a Slider
@typeasfield Int = Slider(1:10)
# This will make the default widget for fields of type ASD a popout that wraps a StructBond{ASD}
@popoutasfield MAH
@bind boh StructBond(BOH)
end
# ╔═╡ 2358f686-1950-40f9-9d5c-dac2d98f4c24
boh
# ╔═╡ 49516374-f625-4a84-ac5c-f92497d45025
md"""
# @NTBond
"""
# ╔═╡ 8cc53cd2-9114-4067-ab0b-37fd8cd79240
md"""
Sometimes custom structs are not needed and it would be useful to just use the same nice bond structure of `StructBond` to simply create arbitrary NamedTuples.
This is possible with the convenience macro `@NTBond` which can be called as shown below to create a nice display for an interactive bond creating an arbitrary NamedTuple.
The macro simply create a `StructBond` wrapping the desired NamedTuple type.
"""
# ╔═╡ 0db51d39-7c05-4e00-b951-7fe776a8e0f9
nt_bond = @bind nt @NTBond "My Fancy NTuple" begin
a = ("Description", Slider(1:10))
b = (md"**Bold** field", Slider(1:10))
c = Slider(1:10) # No description, defaults to the name of the field
end
# ╔═╡ e2b79a58-e66e-4d40-8673-418823753b38
nt
# ╔═╡ 9d23382c-cf35-4b20-a46c-0f4e2de17fc7
md"""
# @BondsList
"""
# ╔═╡ 959acb40-1fd6-43f5-a1a6-73a6ceaae1d7
md"""
In some cases, one does not want to have a single bond wrapping either a Structure or a NamedTuple because single independent bonds are more convenient.
`@BondsList` is a convenience macro to create an object of type `BondsList` which simply allow to add a description to separate bonds and group them all together in a table-like format equivalent to those of `StructBond`.
!!! note
Unlike `StructBond`, a BondsList is already composed of bond created with `@bind` and it just groups them up with a description. The output of `@BondsList` is not supposed to be bound to a variable using `@bind`.\
The bonds grouped in a BondsList still act and update independently from one another.
See the example below for understanding the synthax. The header of a BondsList is shown in an orange background to easily differentiate it from `StructBond`.
"""
# ╔═╡ 4d611425-c8e3-4bc3-912b-8bc0465363bc
bl = @BondsList "My Group of Bonds" let tv = PlutoUI.Experimental.transformed_value
# We use transformed_value to translate GHz to Hz in the bound variable `freq`
"Frequency [GHz]" = @bind freq tv(x -> x * 1e9, Editable(20; suffix = " GHz"))
md"Altitude ``h`` [m]" = @bind alt Scrubbable(100:10:200)
end
# ╔═╡ a5490a6b-dc11-42b7-87e0-d38870fc55e4
freq
# ╔═╡ 705e30fe-77a3-4b06-8b99-1807290edffb
alt
# ╔═╡ 2ee7f26d-f383-4e7e-a69a-8fb72717467c
md"""
# Popout
"""
# ╔═╡ 79beba88-932f-4147-b3a0-d821ef1bc1e2
md"""
The structures above can also be used nested within one another. To facilitate accessing nested structures, one can use the `Popout` type.
In its simple form, you can give an instance of a StructBond, a bond wrapping a StructBond or a BondsList as input to Popout to create a table that is hidden behind a popup window.
If an instance present, but you want a custom type for which you have defined custom bonds and descriptions with `@fielddata` to appear as popout, you can use the function `popoutwrap(TYPE)` to generate a small icon which hides a popup containing the `StructBond` of the provided type `TYPE`.
The StructBond table appears on hover upon the icon, can be made fixed by clicking on the icon and can then be moved around or resized.
A double click on the header of the popout hides it again:
"""
# ╔═╡ 63d6c2df-a411-407c-af11-4f5d09fbb322
begin
Base.@kwdef struct LOL
a::Int
b::Int
end
@fielddata LOL begin
a = (md"Wonder!", Slider(1:10))
b = (@htl("<span>Field B</span>"), Scrubbable(1:10))
end
end
# ╔═╡ 633029af-8cac-426e-b35c-c9fb3938b784
@bind lol_pop popoutwrap(LOL)
# ╔═╡ 065bad73-5fa8-4496-ba33-9e66940b5806
lol_pop
# ╔═╡ 2073f11e-8196-4ebc-922f-5fa589e91797
md"""
The ability to also wrap pre-existing bonds around StructBonds is convenient for organizing the various bonds one have in a `BondsList` or `BondTable`
As an example, one can create a `BondsList` containing the two `StructBond` bonds generated at the beginning of this notebook with the follwing code.
"""
# ╔═╡ 811cf78b-e870-45bb-9173-89a3b3d495f5
blc = @BondsList "Popout Container" begin
"Structure ASD" = Popout(asd_bond)
"NamedTuple" = Popout(nt_bond)
end
# ╔═╡ e7d67662-77b3-482a-b032-8db4afbc01a6
asd
# ╔═╡ 996b4085-114c-48f4-9f90-8e637f29c06a
nt
# ╔═╡ 3a066bf4-3466-469e-90d0-6b14be3ed8d5
md"""
# BondTable
"""
# ╔═╡ 3213d977-7b65-43b0-a881-10fcc2523f14
md"""
The final convenience structure provided by this module is the `BondTable`. It can be created to group a list of bonds in a floating table that stays on the left side of the notebook (similar to the TableOfContents of PlutoUI) and can be moved around and resized or hidden for convenience.
The BondTable is intended to be used either with bonds containing `StructBond` or with `BondsList`. Future types with similar structure will also be added.
Here is an example of a bondtable containing all the examples of this notebook.
"""
# ╔═╡ 903fee67-6b23-41dc-a03d-f1040b696be6
BondTable([
asd_bond,
nt_bond,
bl,
blc
]; description = "My Bonds")
# ╔═╡ 08d711c0-e2cc-4444-94ca-0c4c3cfe901f
nt
# ╔═╡ 03e9d75e-e6c9-4199-933b-2be306daf978
asd
# ╔═╡ 26d11600-2827-4e08-9195-109c8a8bddc3
freq
# ╔═╡ be2a7820-e194-4410-b00d-a9332b234ad6
alt
# ╔═╡ 00000000-0000-0000-0000-000000000001
PLUTO_PROJECT_TOML_CONTENTS = """
[deps]
PlutoDevMacros = "a0499f29-c39b-4c5c-807c-88074221b949"
[compat]
PlutoDevMacros = "~0.7.0"
"""
# ╔═╡ 00000000-0000-0000-0000-000000000002
PLUTO_MANIFEST_TOML_CONTENTS = """
# This file is machine-generated - editing it directly is not advised
julia_version = "1.10.2"
manifest_format = "2.0"
project_hash = "9088728c15a23247dcd792960c847dd5035deb53"
[[deps.AbstractPlutoDingetjes]]
deps = ["Pkg"]
git-tree-sha1 = "0f748c81756f2e5e6854298f11ad8b2dfae6911a"
uuid = "6e696c72-6542-2067-7265-42206c756150"
version = "1.3.0"
[[deps.ArgTools]]
uuid = "0dad84c5-d112-42e6-8d28-ef12dabb789f"
version = "1.1.1"
[[deps.Artifacts]]
uuid = "56f22d72-fd6d-98f1-02f0-08ddc0907c33"
[[deps.Base64]]
uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
[[deps.Dates]]
deps = ["Printf"]
uuid = "ade2ca70-3891-5945-98fb-dc099432e06a"
[[deps.DocStringExtensions]]
deps = ["LibGit2"]
git-tree-sha1 = "2fb1e02f2b635d0845df5d7c167fec4dd739b00d"
uuid = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
version = "0.9.3"
[[deps.Downloads]]
deps = ["ArgTools", "FileWatching", "LibCURL", "NetworkOptions"]
uuid = "f43a241f-c20a-4ad4-852c-f6b1247861c6"
version = "1.6.0"
[[deps.FileWatching]]
uuid = "7b1f6079-737a-58dc-b8bc-7a2ca5c1b5ee"
[[deps.HypertextLiteral]]
deps = ["Tricks"]
git-tree-sha1 = "7134810b1afce04bbc1045ca1985fbe81ce17653"
uuid = "ac1192a8-f4b3-4bfe-ba22-af5b92cd3ab2"
version = "0.9.5"
[[deps.InteractiveUtils]]
deps = ["Markdown"]
uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
[[deps.LibCURL]]
deps = ["LibCURL_jll", "MozillaCACerts_jll"]
uuid = "b27032c2-a3e7-50c8-80cd-2d36dbcbfd21"
version = "0.6.4"
[[deps.LibCURL_jll]]
deps = ["Artifacts", "LibSSH2_jll", "Libdl", "MbedTLS_jll", "Zlib_jll", "nghttp2_jll"]
uuid = "deac9b47-8bc7-5906-a0fe-35ac56dc84c0"
version = "8.4.0+0"
[[deps.LibGit2]]
deps = ["Base64", "LibGit2_jll", "NetworkOptions", "Printf", "SHA"]
uuid = "76f85450-5226-5b5a-8eaa-529ad045b433"
[[deps.LibGit2_jll]]
deps = ["Artifacts", "LibSSH2_jll", "Libdl", "MbedTLS_jll"]
uuid = "e37daf67-58a4-590a-8e99-b0245dd2ffc5"
version = "1.6.4+0"
[[deps.LibSSH2_jll]]
deps = ["Artifacts", "Libdl", "MbedTLS_jll"]
uuid = "29816b5a-b9ab-546f-933c-edad1886dfa8"
version = "1.11.0+1"
[[deps.Libdl]]
uuid = "8f399da3-3557-5675-b5ff-fb832c97cbdb"
[[deps.Logging]]
uuid = "56ddb016-857b-54e1-b83d-db4d58db5568"
[[deps.MacroTools]]
deps = ["Markdown", "Random"]
git-tree-sha1 = "2fa9ee3e63fd3a4f7a9a4f4744a52f4856de82df"
uuid = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
version = "0.5.13"
[[deps.Markdown]]
deps = ["Base64"]
uuid = "d6f4376e-aef5-505a-96c1-9c027394607a"
[[deps.MbedTLS_jll]]
deps = ["Artifacts", "Libdl"]
uuid = "c8ffd9c3-330d-5841-b78e-0817d7145fa1"
version = "2.28.2+1"
[[deps.MozillaCACerts_jll]]
uuid = "14a3606d-f60d-562e-9121-12d972cd8159"
version = "2023.1.10"
[[deps.NetworkOptions]]
uuid = "ca575930-c2e3-43a9-ace4-1e988b2c1908"
version = "1.2.0"
[[deps.Pkg]]
deps = ["Artifacts", "Dates", "Downloads", "FileWatching", "LibGit2", "Libdl", "Logging", "Markdown", "Printf", "REPL", "Random", "SHA", "Serialization", "TOML", "Tar", "UUIDs", "p7zip_jll"]
uuid = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
version = "1.10.0"
[[deps.PlutoDevMacros]]
deps = ["AbstractPlutoDingetjes", "DocStringExtensions", "HypertextLiteral", "InteractiveUtils", "MacroTools", "Markdown", "Pkg", "Random", "TOML"]
git-tree-sha1 = "2944f76ac8c11c913a620da0a6b035e2fadf94c1"
uuid = "a0499f29-c39b-4c5c-807c-88074221b949"
version = "0.7.2"
[[deps.Printf]]
deps = ["Unicode"]
uuid = "de0858da-6303-5e67-8744-51eddeeeb8d7"
[[deps.REPL]]
deps = ["InteractiveUtils", "Markdown", "Sockets", "Unicode"]
uuid = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb"
[[deps.Random]]
deps = ["SHA"]
uuid = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
[[deps.SHA]]
uuid = "ea8e919c-243c-51af-8825-aaa63cd721ce"
version = "0.7.0"
[[deps.Serialization]]
uuid = "9e88b42a-f829-5b0c-bbe9-9e923198166b"
[[deps.Sockets]]
uuid = "6462fe0b-24de-5631-8697-dd941f90decc"
[[deps.TOML]]
deps = ["Dates"]
uuid = "fa267f1f-6049-4f14-aa54-33bafae1ed76"
version = "1.0.3"
[[deps.Tar]]
deps = ["ArgTools", "SHA"]
uuid = "a4e569a6-e804-4fa4-b0f3-eef7a1d5b13e"
version = "1.10.0"
[[deps.Tricks]]
git-tree-sha1 = "eae1bb484cd63b36999ee58be2de6c178105112f"
uuid = "410a4b4d-49e4-4fbc-ab6d-cb71b17b3775"
version = "0.1.8"
[[deps.UUIDs]]
deps = ["Random", "SHA"]
uuid = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"
[[deps.Unicode]]
uuid = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5"
[[deps.Zlib_jll]]
deps = ["Libdl"]
uuid = "83775a58-1f1d-513f-b197-d71354ab007a"
version = "1.2.13+1"
[[deps.nghttp2_jll]]
deps = ["Artifacts", "Libdl"]
uuid = "8e850ede-7688-5339-a07c-302acd2aaf8d"
version = "1.52.0+1"
[[deps.p7zip_jll]]
deps = ["Artifacts", "Libdl"]
uuid = "3f19e933-33d8-53b3-aaab-bd5110c3b7a0"
version = "17.4.0+2"
"""
# ╔═╡ Cell order:
# ╟─9d4455af-96f1-46d7-a4f3-434495b11c8a
# ╠═8db82e94-5c81-4c52-9228-7e22395fb68f
# ╠═949ac1ef-c502-4e28-81ff-f99b0d19aa03
# ╠═707175a9-d356-43cf-8038-620ebc401c93
# ╠═cbfc6eec-8991-4a9c-ada0-295c8052854d
# ╟─4843983c-df64-4b94-8634-7a10d9423a70
# ╟─a8995224-83c6-4f82-b5a5-87a6f86fc7a0
# ╠═9e3601f5-efc2-44e9-83d8-5b65ce7e9ccf
# ╠═cdc0a7ad-a3ac-4270-b707-35b1939da276
# ╟─fe891b4e-f440-4823-b43b-72572f6a6c12
# ╠═86a80228-f495-43e8-b1d4-c93b7b52c8d8
# ╠═2358f686-1950-40f9-9d5c-dac2d98f4c24
# ╟─49516374-f625-4a84-ac5c-f92497d45025
# ╟─8cc53cd2-9114-4067-ab0b-37fd8cd79240
# ╠═0db51d39-7c05-4e00-b951-7fe776a8e0f9
# ╠═e2b79a58-e66e-4d40-8673-418823753b38
# ╟─9d23382c-cf35-4b20-a46c-0f4e2de17fc7
# ╟─959acb40-1fd6-43f5-a1a6-73a6ceaae1d7
# ╠═4d611425-c8e3-4bc3-912b-8bc0465363bc
# ╠═a5490a6b-dc11-42b7-87e0-d38870fc55e4
# ╠═705e30fe-77a3-4b06-8b99-1807290edffb
# ╟─2ee7f26d-f383-4e7e-a69a-8fb72717467c
# ╟─79beba88-932f-4147-b3a0-d821ef1bc1e2
# ╠═63d6c2df-a411-407c-af11-4f5d09fbb322
# ╠═633029af-8cac-426e-b35c-c9fb3938b784
# ╠═065bad73-5fa8-4496-ba33-9e66940b5806
# ╟─2073f11e-8196-4ebc-922f-5fa589e91797
# ╠═811cf78b-e870-45bb-9173-89a3b3d495f5
# ╠═e7d67662-77b3-482a-b032-8db4afbc01a6
# ╠═996b4085-114c-48f4-9f90-8e637f29c06a
# ╟─3a066bf4-3466-469e-90d0-6b14be3ed8d5
# ╟─3213d977-7b65-43b0-a881-10fcc2523f14
# ╠═903fee67-6b23-41dc-a03d-f1040b696be6
# ╠═08d711c0-e2cc-4444-94ca-0c4c3cfe901f
# ╠═03e9d75e-e6c9-4199-933b-2be306daf978
# ╠═26d11600-2827-4e08-9195-109c8a8bddc3
# ╠═be2a7820-e194-4410-b00d-a9332b234ad6
# ╟─00000000-0000-0000-0000-000000000001
# ╟─00000000-0000-0000-0000-000000000002