trebol-dev 0.0.1 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/app/props.trb ADDED
@@ -0,0 +1,304 @@
1
+ Trebol [
2
+ title: "The properties of a face: one control for each option its style takes, and its handlers"
3
+ exports: [props-code events-code prop-written? event-help type-of-args]
4
+ ]
5
+
6
+ import %model.trb
7
+ import %options.trb
8
+
9
+ ; The panel is laid out anew for each face from what its style takes, so a style that a package
10
+ ; defines shows its options as Trebol's own do. Each control's action calls act, a word of the
11
+ ; app, with the index of its option in the list given back and what to do:
12
+ ; act k 'flag true a flag or a rule with no values on (false: off)
13
+ ; act k 'values "2 40" the values of a keyword, rule or bare value, as typed
14
+ ; act k 'choice "bar" one of a list
15
+ ; act k 'code "[...]" a block, as typed
16
+ ; act k 'custom [w true] a rule a program adds, on or off
17
+ ; act k 'remove none out of the code
18
+ ; act k 'icon 'heart an icon picked
19
+ ; act k 'check "2 x" whether what is typed is what it takes: true, or what is wrong
20
+ ; and the texts, what it follows and its handlers: act 0 'text "x", act 0 'hint "x",
21
+ ; act 0 'is "[...]", act 0 'react "[...]", act 0 'event [on-change "[...]"].
22
+
23
+ event-help: make map! [
24
+ on-back "the browser's Back was pressed"
25
+ on-change "its value changed"
26
+ on-close "it was closed"
27
+ on-dbl-click "clicked twice"
28
+ on-drop "something was dropped on it"
29
+ on-end "it reached its end"
30
+ on-enter "Enter was pressed in it"
31
+ on-expand "a row of it was opened"
32
+ on-find "something was asked for in the palette"
33
+ on-focus "it took the focus"
34
+ on-key "a key was pressed while it has the focus"
35
+ on-leave "the screen is left"
36
+ on-menu "its context menu was asked for"
37
+ on-over "the pointer is over it"
38
+ on-reorder "its children or rows were moved"
39
+ on-search "something was typed to search"
40
+ on-select "it or one of its rows was chosen"
41
+ on-sort "its rows were sorted"
42
+ on-unfocus "it lost the focus"
43
+ ]
44
+
45
+ prop-written?: function ["Whether a face has an option: the part that took it, or none" parts [block!] e [object!]] [
46
+ option-of parts e
47
+ ]
48
+
49
+ type-of-args: function ["What an option takes, as the reference says it: integer! integer!, or one of a list" e [object!]] [
50
+ if find [name label] e/how [return "word"]
51
+ if e/how = 'block [return "block!"]
52
+ out: copy []
53
+ foreach a e/args [
54
+ append out case [
55
+ a = 'text ["text"]
56
+ 'one-of = first a [rejoin ["one of " form either word? a/2 [get a/2] [a/2]]]
57
+ true [form a]
58
+ ]
59
+ ]
60
+ form out
61
+ ]
62
+
63
+ ; the words a block of a property is coloured with: none, the small editor colours Trebol alone
64
+ choices-of: func [a] [either word? a/2 [get a/2] [a/2]]
65
+ one-of-arg: func [e] [all [1 = length? e/args block? first e/args 'one-of = first first e/args first e/args]]
66
+ block-arg?: func [e] [all [1 = length? e/args block? first e/args find first e/args 'block!]]
67
+
68
+ matches?: function [e query [string!]] [
69
+ any [
70
+ empty? query
71
+ find form any [e/kw e/field] query
72
+ all [e/help find e/help query]
73
+ ]
74
+ ]
75
+
76
+ ; a control for entry e, the k-th of the list, given what the face has of it
77
+ control-of: function [g [block!] k [integer!] has vals act [word!] words] [
78
+ e: first g
79
+ ; an option written in more than one way, grid 3 or grid [1 2 1]: typed, as any of them
80
+ if 1 < length? g [
81
+ hint: copy ""
82
+ foreach x g [unless empty? hint [append hint " or "] append hint type-of-args x]
83
+ label: form either set-word? e/field [to word! e/field] [e/field]
84
+ shown: either has [mold/only vals] [""]
85
+ if all [has 1 = length? vals block? first vals] [shown: mold first vals]
86
+ return compose/deep [field (label) (hint) with [value: (shown)] check [(act) (k) 'check value] on-change [(act) (k) 'values value]]
87
+ ]
88
+ label: form any [e/kw if e/how = 'label ['label] either set-word? e/field [to word! e/field] [e/field]]
89
+ help: any [e/help ""]
90
+ shown: either has [mold/only vals] [""]
91
+ switch/default e/how [
92
+ flag [compose/deep [check (label) with [value: (to logic! has)] on-change [(act) (k) 'flag value]]]
93
+ custom [
94
+ out: copy []
95
+ foreach w custom-rule-words [
96
+ on: to logic! all [has find vals w]
97
+ append out compose/deep [check (form w) with [value: (on)] on-change [(act) (k) 'custom reduce [(to lit-word! w) value]]]
98
+ ]
99
+ either empty? out [[text small muted "No program here adds a rule."]] [out]
100
+ ]
101
+ block [compose/deep [
102
+ text small (label)
103
+ widget <trebol-code-small> [] with [value: (either has [mold first vals] [""])] on-change [(act) (k) 'code value]
104
+ ]]
105
+ name label [compose/deep [field (label) "word" with [value: (shown)] check [(act) (k) 'check value] on-change [(act) (k) 'values value]]]
106
+ ] [
107
+ ; rule with no values: on or off
108
+ if all [e/how = 'rule empty? e/args] [
109
+ return compose/deep [check (label) with [value: (to logic! has)] on-change [(act) (k) 'flag value]]
110
+ ]
111
+ if a: one-of-arg e [
112
+ cs: collect [foreach c choices-of a [keep form c]]
113
+ v: either has [form first vals] [none]
114
+ ; joined buttons while they fit the panel, a choice otherwise
115
+ return either all [3 >= length? cs 16 >= length? rejoin cs] [
116
+ compose/deep [text small (label) radio (label) [(cs)] segmented with [value: (v)] on-change [(act) (k) 'choice value]]
117
+ ] [
118
+ compose/deep [choice (label) [(cs)] with [value: (v)] on-change [if value [(act) (k) 'choice value]]]
119
+ ]
120
+ ]
121
+ ; an icon: typed, or picked from those Trebol brings
122
+ if all [1 = length? e/args [issue!] = first e/args] [
123
+ items: collect [foreach w ui-icons [keep to issue! form w keep form w keep w]]
124
+ return compose/deep [
125
+ row middle [
126
+ field (label) "#icon" fill with [value: (shown)] check [(act) (k) 'check value] on-change [(act) (k) 'values value]
127
+ button (either all [has issue? first vals] [first vals] [#search]) "Pick" small menu [(items)] on-menu [(act) (k) 'icon event/picked]
128
+ ]
129
+ ]
130
+ ]
131
+ if block-arg? e [
132
+ return compose/deep [
133
+ text small (label)
134
+ widget <trebol-code-small> [] with [value: (either has [mold first vals] [""])] on-change [(act) (k) 'code value]
135
+ ]
136
+ ]
137
+ compose/deep [field (label) (type-of-args e) with [value: (shown)] check [(act) (k) 'check value] on-change [(act) (k) 'values value]]
138
+ ]
139
+ ]
140
+
141
+ ; a part of the panel: a line, its title, then its controls
142
+ section: func [title [string!] body [block!]] [compose/deep [divider text small muted (title) column gap 2 [(body)]]]
143
+
144
+ props-code: function [
145
+ "The layout of a face's options, and the entries its controls name by index: [code entries]"
146
+ d [object!] f [object!] query [string!] act [word!]
147
+ ] [
148
+ ; a tab or a step: its label, and another after it; its block holds only faces
149
+ if piece? f [
150
+ kind: f/style
151
+ return reduce [compose/deep [
152
+ divider text small muted (either kind = 'tab ["Tab"] ["Step"]) column gap 2 [
153
+ field "label" "what it is called" with [value: (form f/text)] on-change [(act) 0 'piece-label value]
154
+ button #plus (either kind = 'tab ["Add a tab"] ["Add a step"]) small [(act) 0 'add-piece (to lit-word! kind)]
155
+ ]
156
+ ] copy []]
157
+ ]
158
+ if find [tabs steps] f/style [extra: compose/deep [
159
+ button #plus (either f/style = 'tabs ["Add a tab"] ["Add a step"]) small [(act) 0 'add-piece (to lit-word! either f/style = 'tabs ['tab] ['step])]
160
+ ]]
161
+ def: style-def-of d f
162
+ parts: face-parts d f
163
+ r: face-range d f
164
+ ; a view's words come before its faces: neither blocks, texts nor what it follows are among them
165
+ view?: to logic! own-block d f
166
+ entries: copy []
167
+ written: copy []
168
+ groups: copy []
169
+ rows: option-rows def
170
+ foreach [style gs] rows [
171
+ g: copy []
172
+ foreach grp gs [
173
+ e: first grp
174
+ if all [
175
+ any collect [foreach x grp [keep matches? x query]]
176
+ not all [view? e/how = 'block]
177
+ ] [
178
+ append/only entries grp
179
+ k: length? entries
180
+ p: either e/how = 'custom [none] [all [q: group-part parts grp first q]]
181
+ if all [e/how = 'custom] [
182
+ ; every custom rule the face has
183
+ ws: collect [foreach q parts [if all [q/what = 'entry q/e q/e/how = 'custom] [keep q/word]]]
184
+ p: either empty? ws [none] [ws]
185
+ ]
186
+ vals: case [
187
+ block? p [p]
188
+ p [row-values d f p]
189
+ true [none]
190
+ ]
191
+ c: control-of grp k to logic! p vals act none
192
+ help: any [e/help ""]
193
+ either p [
194
+ append written compose/deep [
195
+ row middle [column fill gap 0 [(c) text small muted (help)] button #close "" small [(act) (k) 'remove none]]
196
+ ]
197
+ ] [
198
+ append g compose/deep [column gap 0 [(c) text small muted (help)]]
199
+ ]
200
+ ]
201
+ ]
202
+ unless empty? g [append groups section (rejoin [form style either style = def/name [" · its own"] [""]]) g]
203
+ ]
204
+ code: copy []
205
+ ; what it says and what it follows: every face has them
206
+ t: none h: none
207
+ foreach q parts [
208
+ if q/what = 'text [t: pick r/1 q/from]
209
+ if q/what = 'hint [h: pick r/1 q/from]
210
+ ]
211
+ ; a screen: its name, title and arguments, written before its block
212
+ if all [f/style = 'screen r2: written-range d f] [
213
+ b: r2/1 i: r2/2
214
+ args: either r2/3 = (i + 3) [form pick b i + 2] [""]
215
+ append code compose/deep [
216
+ divider text small muted "Screen" column gap 2 [
217
+ field "screen name" "word" with [value: (form pick b i)] on-change [(act) 0 'screen-name value]
218
+ field "title" "what the window is called" with [value: (form pick b i + 1)] on-change [(act) 0 'screen-title value]
219
+ field "arguments" "words, as id: client/42 gives id 42" with [value: (args)] on-change [(act) 0 'screen-args value]
220
+ button #plus "Add a screen" small [(act) 0 'add-screen none]
221
+ ]
222
+ ]
223
+ ]
224
+ if f/style = 'screens [append code compose/deep [button #plus "Add a screen" small [(act) 0 'add-screen none]]]
225
+ ; the word code names it with: who: field, global for every script
226
+ if all [not view? any [empty? query find "label global word" query]] [
227
+ l: face-label d f
228
+ g: 'global = pick r/1 r/2
229
+ append code compose/deep [
230
+ divider text small muted "Label" column gap 2 [
231
+ row middle [
232
+ field "label" "word" fill with [value: (either l [form l] [""])] on-change [(act) 0 'label value]
233
+ check "global" with [value: (g)] on-change [(act) 0 'global value]
234
+ ]
235
+ text small muted "the word code names it with: who: field; global, for every script"
236
+ ]
237
+ ]
238
+ ]
239
+ if all [not view? any [empty? query find "text hint" query]] [
240
+ append code compose/deep [
241
+ divider text small muted "Text" column gap 2 [
242
+ field "text" "what it says" with [value: (either string? t [t] [""])] on-change [(act) 0 'text value]
243
+ field "hint" "shown while it is empty" with [value: (either string? h [h] [""])] on-change [(act) 0 'hint value]
244
+ ]
245
+ ]
246
+ ]
247
+ if extra [append code extra]
248
+ unless empty? written [append code section "Written · in the code now" written]
249
+ append code groups
250
+ if all [not view? any [empty? query find "is react calc follows" query]] [
251
+ fs: copy []
252
+ foreach kind [is calc react] [
253
+ v: none
254
+ foreach q parts [if all [q/what = 'reaction q/word = kind] [v: pick r/1 q/to]]
255
+ append fs compose/deep [
256
+ text small (form kind)
257
+ widget <trebol-code-small> [] with [value: (either v [mold v] [""])] on-change [(act) 0 (to lit-word! kind) value]
258
+ ]
259
+ ]
260
+ append code compose/deep [divider text small muted "Follows other faces" column gap 2 [(fs) text small muted "is: its value is what the block gives, each time what it reads changes; calc: the same, and it cannot be typed in; react: runs again each time"]]
261
+ ]
262
+ reduce [code entries]
263
+ ]
264
+
265
+ events-code: function [
266
+ "The layout of a face's handlers: one for each event its style fires, written ones with their block"
267
+ d [object!] f [object!] query [string!] act [word!]
268
+ ] [
269
+ if piece? f [return [text muted "A tab or a step has no events of its own: its faces do."]]
270
+ def: style-def-of d f
271
+ parts: face-parts d f
272
+ r: face-range d f
273
+ have: copy []
274
+ foreach q parts [if q/what = 'event [append have reduce [q/word pick r/1 q/to]]]
275
+ evs: either block? def/events [copy def/events] [copy []]
276
+ ; a style told any event fires what it is given: those written, then any of the dialect
277
+ if def/events = 'any [evs: union extract have 2 keys-of event-help]
278
+ ; those it has first
279
+ evs: union intersect evs extract have 2 evs
280
+ code: copy []
281
+ ; what it does when clicked: the block after it; a view's words have none
282
+ if all [not own-block d f any [empty? query find "action click" query]] [
283
+ a: none
284
+ foreach q parts [if q/what = 'action [a: pick r/1 q/from]]
285
+ append code compose/deep [
286
+ row middle spread [text "action" (either a [compose/deep [button #close "" small [(act) 0 'action none]]] [[]])]
287
+ widget <trebol-code-small> [] with [value: (either a [mold a] [""])] on-change [(act) 0 'action value]
288
+ text small muted "the block after it: runs when it is clicked"
289
+ ]
290
+ ]
291
+ foreach ev evs [
292
+ if any [empty? query find form ev query] [
293
+ b: select have ev
294
+ help: any [select event-help ev ""]
295
+ append code compose/deep [
296
+ row middle spread [text (form ev) (either b [compose/deep [button #close "" small [(act) 0 'event reduce [(to lit-word! ev) none]]]] [[]])]
297
+ widget <trebol-code-small> [] with [value: (either b [mold b] [""])] on-change [(act) 0 'event reduce [(to lit-word! ev) value]]
298
+ text small muted (help)
299
+ ]
300
+ ]
301
+ ]
302
+ if empty? code [code: [text muted "No event matches."]]
303
+ code
304
+ ]
package/app/sample.trb ADDED
@@ -0,0 +1,16 @@
1
+ Trebol [title: "A sample for the tests of the model" exports: [sample]]
2
+
3
+ ; what the tests edit; the comments must survive every edit
4
+ sample: {Trebol [title: "Signup"]
5
+
6
+ ; who signs up
7
+ view [
8
+ heading "Join us"
9
+ who: field name "Name" required ; theirs
10
+ field email "Email"
11
+ row [
12
+ button "Save" [save-it]
13
+ button "Back"
14
+ ]
15
+ ]
16
+ }
@@ -0,0 +1,46 @@
1
+ Trebol [
2
+ title: "Rows made up for the preview, so a table, list, chart or board shows what it would; never saved"
3
+ exports: [fill-samples]
4
+ ]
5
+
6
+ ; A face that shows rows has none while it is designed: its rows come when the program runs. The
7
+ ; preview gives it a few made up from what it says it shows, its columns or its lanes, so the
8
+ ; screen looks as it will. They are put in the preview's faces only, never in the text.
9
+
10
+ sample-rows: function [f [object!] n [integer!]] [
11
+ case [
12
+ all [in f 'lanes block? f/lanes not empty? f/lanes f/field] [
13
+ lanes: extract f/lanes 2
14
+ collect [repeat i n [
15
+ m: make map! reduce ['title rejoin ["Card " i]]
16
+ put m f/field pick lanes 1 + ((i - 1) // length? lanes)
17
+ keep m
18
+ ]]
19
+ ]
20
+ all [in f 'columns block? f/columns not empty? f/columns] [
21
+ ws: extract f/columns 2
22
+ collect [repeat i n [
23
+ m: make map! []
24
+ repeat j length? ws [
25
+ put m pick ws j either j = 1 [rejoin [form pick ws 1 " " i]] [10 * i + (7 * j) // 23]
26
+ ]
27
+ keep m
28
+ ]]
29
+ ]
30
+ true [none]
31
+ ]
32
+ ]
33
+
34
+ fill-samples: function [
35
+ "Gives each face in faces (and in theirs) that shows rows and has none a few made up rows"
36
+ faces [block!]
37
+ ] [
38
+ foreach f faces [
39
+ if object? f [
40
+ if all [in f 'data any [none? f/data all [block? f/data empty? f/data]]] [
41
+ if rows: sample-rows f 4 [f/data: rows]
42
+ ]
43
+ if block? get in f 'pane [fill-samples f/pane]
44
+ ]
45
+ ]
46
+ ]