ozdenui 0.1.0

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.
Files changed (149) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +14 -0
  3. data/NOTICE +5 -0
  4. data/README.md +242 -0
  5. data/exe/ozdenui +17 -0
  6. data/lib/ozdenui/AGENTS.md +210 -0
  7. data/lib/ozdenui/COMPONENTS.md +3017 -0
  8. data/lib/ozdenui/app.rb +749 -0
  9. data/lib/ozdenui/command.rb +107 -0
  10. data/lib/ozdenui/components.rb +368 -0
  11. data/lib/ozdenui/dialogs.rb +212 -0
  12. data/lib/ozdenui/files.rb +124 -0
  13. data/lib/ozdenui/icon.png +0 -0
  14. data/lib/ozdenui/kit.json +1 -0
  15. data/lib/ozdenui/kit.rb +115 -0
  16. data/lib/ozdenui/look.rb +229 -0
  17. data/lib/ozdenui/objects.rb +652 -0
  18. data/lib/ozdenui/server.rb +270 -0
  19. data/lib/ozdenui/values.rb +273 -0
  20. data/lib/ozdenui/version.rb +15 -0
  21. data/lib/ozdenui/web/basic/LICENSE +14 -0
  22. data/lib/ozdenui/web/basic/basic.min.css +1 -0
  23. data/lib/ozdenui/web/basic/basic.min.js +78 -0
  24. data/lib/ozdenui/web/basic/font/open-sans/LICENSE.txt +206 -0
  25. data/lib/ozdenui/web/basic/font/open-sans/OpenSans-Bold.ttf +0 -0
  26. data/lib/ozdenui/web/basic/font/open-sans/OpenSans-Regular.ttf +0 -0
  27. data/lib/ozdenui/web/basic/scroll-bar.min.js +3 -0
  28. data/lib/ozdenui/web/bridge.js +584 -0
  29. data/lib/ozdenui/web/comp/accordion.js +659 -0
  30. data/lib/ozdenui/web/comp/action-button/add.png +0 -0
  31. data/lib/ozdenui/web/comp/action-button.js +521 -0
  32. data/lib/ozdenui/web/comp/action-tab-bar.js +383 -0
  33. data/lib/ozdenui/web/comp/app-bar.js +237 -0
  34. data/lib/ozdenui/web/comp/badge.js +292 -0
  35. data/lib/ozdenui/web/comp/bottom-bar.js +555 -0
  36. data/lib/ozdenui/web/comp/breadcrumbs.js +524 -0
  37. data/lib/ozdenui/web/comp/canvas.js +652 -0
  38. data/lib/ozdenui/web/comp/chat-view.js +827 -0
  39. data/lib/ozdenui/web/comp/check-box.js +490 -0
  40. data/lib/ozdenui/web/comp/code-editor.js +965 -0
  41. data/lib/ozdenui/web/comp/color-picker.js +636 -0
  42. data/lib/ozdenui/web/comp/context-menu.js +951 -0
  43. data/lib/ozdenui/web/comp/currency-field.js +209 -0
  44. data/lib/ozdenui/web/comp/data-table/arrow.svg +2 -0
  45. data/lib/ozdenui/web/comp/data-table.js +937 -0
  46. data/lib/ozdenui/web/comp/date-picker.js +1055 -0
  47. data/lib/ozdenui/web/comp/dialog.js +992 -0
  48. data/lib/ozdenui/web/comp/email-field.js +65 -0
  49. data/lib/ozdenui/web/comp/file-picker.js +920 -0
  50. data/lib/ozdenui/web/comp/filters.js +807 -0
  51. data/lib/ozdenui/web/comp/form.js +602 -0
  52. data/lib/ozdenui/web/comp/full-screen-select/arrow.svg +2 -0
  53. data/lib/ozdenui/web/comp/full-screen-select/cancel.svg +2 -0
  54. data/lib/ozdenui/web/comp/full-screen-select.js +614 -0
  55. data/lib/ozdenui/web/comp/gauge.js +439 -0
  56. data/lib/ozdenui/web/comp/graph-box.js +3911 -0
  57. data/lib/ozdenui/web/comp/grid-group.js +185 -0
  58. data/lib/ozdenui/web/comp/hold-to-confirm-button/trash-red.png +0 -0
  59. data/lib/ozdenui/web/comp/hold-to-confirm-button/trash.png +0 -0
  60. data/lib/ozdenui/web/comp/hold-to-confirm-button.js +289 -0
  61. data/lib/ozdenui/web/comp/image-view.js +598 -0
  62. data/lib/ozdenui/web/comp/item-list.js +1740 -0
  63. data/lib/ozdenui/web/comp/json-editor.js +3221 -0
  64. data/lib/ozdenui/web/comp/json-view.js +597 -0
  65. data/lib/ozdenui/web/comp/label-editor.js +1140 -0
  66. data/lib/ozdenui/web/comp/left-menu/data-table.png +0 -0
  67. data/lib/ozdenui/web/comp/left-menu/home.png +0 -0
  68. data/lib/ozdenui/web/comp/left-menu/reports.png +0 -0
  69. data/lib/ozdenui/web/comp/left-menu/setting.png +0 -0
  70. data/lib/ozdenui/web/comp/left-menu.js +548 -0
  71. data/lib/ozdenui/web/comp/line-progress-bar.js +343 -0
  72. data/lib/ozdenui/web/comp/loading-screen.js +413 -0
  73. data/lib/ozdenui/web/comp/log-view.js +530 -0
  74. data/lib/ozdenui/web/comp/login-page.js +685 -0
  75. data/lib/ozdenui/web/comp/markdown-view.js +1045 -0
  76. data/lib/ozdenui/web/comp/menu-bar.js +624 -0
  77. data/lib/ozdenui/web/comp/mini-graph-box.js +492 -0
  78. data/lib/ozdenui/web/comp/minimal-accent.js +208 -0
  79. data/lib/ozdenui/web/comp/notice.js +229 -0
  80. data/lib/ozdenui/web/comp/number-field.js +128 -0
  81. data/lib/ozdenui/web/comp/numeric-keyboard.js +298 -0
  82. data/lib/ozdenui/web/comp/page-control.js +762 -0
  83. data/lib/ozdenui/web/comp/page-header.js +191 -0
  84. data/lib/ozdenui/web/comp/password-field/hide-btn.png +0 -0
  85. data/lib/ozdenui/web/comp/password-field/hide-btn2.png +0 -0
  86. data/lib/ozdenui/web/comp/password-field/show-btn.png +0 -0
  87. data/lib/ozdenui/web/comp/password-field/show-btn2.png +0 -0
  88. data/lib/ozdenui/web/comp/password-field.js +192 -0
  89. data/lib/ozdenui/web/comp/period-bar.js +436 -0
  90. data/lib/ozdenui/web/comp/phone-field.js +322 -0
  91. data/lib/ozdenui/web/comp/progress-bar.js +992 -0
  92. data/lib/ozdenui/web/comp/property-panel.js +3639 -0
  93. data/lib/ozdenui/web/comp/radio-button.js +527 -0
  94. data/lib/ozdenui/web/comp/range-slider.js +569 -0
  95. data/lib/ozdenui/web/comp/rich-text-editor.js +1064 -0
  96. data/lib/ozdenui/web/comp/scroll-area.js +308 -0
  97. data/lib/ozdenui/web/comp/scrolling-text.js +297 -0
  98. data/lib/ozdenui/web/comp/search-input/clear-round.svg +1 -0
  99. data/lib/ozdenui/web/comp/search-input/clear.svg +1 -0
  100. data/lib/ozdenui/web/comp/search-input/filter.png +0 -0
  101. data/lib/ozdenui/web/comp/search-input/filter.svg +1 -0
  102. data/lib/ozdenui/web/comp/search-input/search.svg +1 -0
  103. data/lib/ozdenui/web/comp/search-input.js +725 -0
  104. data/lib/ozdenui/web/comp/search-results.js +596 -0
  105. data/lib/ozdenui/web/comp/select-box.js +1862 -0
  106. data/lib/ozdenui/web/comp/settings-row.js +299 -0
  107. data/lib/ozdenui/web/comp/side-panel.js +616 -0
  108. data/lib/ozdenui/web/comp/slider-field.js +952 -0
  109. data/lib/ozdenui/web/comp/slider.js +838 -0
  110. data/lib/ozdenui/web/comp/smart-button/alert-black.png +0 -0
  111. data/lib/ozdenui/web/comp/smart-button/clock.png +0 -0
  112. data/lib/ozdenui/web/comp/smart-button.js +981 -0
  113. data/lib/ozdenui/web/comp/smart-table/clock.png +0 -0
  114. data/lib/ozdenui/web/comp/smart-table/down.png +0 -0
  115. data/lib/ozdenui/web/comp/smart-table/scroll.png +0 -0
  116. data/lib/ozdenui/web/comp/smart-table/sort.png +0 -0
  117. data/lib/ozdenui/web/comp/smart-table/up.png +0 -0
  118. data/lib/ozdenui/web/comp/smart-table.js +1883 -0
  119. data/lib/ozdenui/web/comp/sortable-list.js +1181 -0
  120. data/lib/ozdenui/web/comp/spark-line-box.js +738 -0
  121. data/lib/ozdenui/web/comp/splitter.js +411 -0
  122. data/lib/ozdenui/web/comp/status-bar.js +408 -0
  123. data/lib/ozdenui/web/comp/step-bar.js +345 -0
  124. data/lib/ozdenui/web/comp/stepper.js +753 -0
  125. data/lib/ozdenui/web/comp/tabs.js +1366 -0
  126. data/lib/ozdenui/web/comp/tag-input.js +1107 -0
  127. data/lib/ozdenui/web/comp/tap-effect.js +170 -0
  128. data/lib/ozdenui/web/comp/text-area-field.js +233 -0
  129. data/lib/ozdenui/web/comp/text-field.js +1006 -0
  130. data/lib/ozdenui/web/comp/time-line.js +398 -0
  131. data/lib/ozdenui/web/comp/time-picker.js +1032 -0
  132. data/lib/ozdenui/web/comp/tiny-select/arrow.svg +1 -0
  133. data/lib/ozdenui/web/comp/tiny-select.js +322 -0
  134. data/lib/ozdenui/web/comp/tiny-table.js +283 -0
  135. data/lib/ozdenui/web/comp/toast.js +891 -0
  136. data/lib/ozdenui/web/comp/toggle.js +757 -0
  137. data/lib/ozdenui/web/comp/tool-bar.js +760 -0
  138. data/lib/ozdenui/web/comp/tooltip.js +534 -0
  139. data/lib/ozdenui/web/comp/tree-view.js +1179 -0
  140. data/lib/ozdenui/web/comp/ui-effects.js +1097 -0
  141. data/lib/ozdenui/web/comp/ui-standards.js +403 -0
  142. data/lib/ozdenui/web/comp/url-field.js +190 -0
  143. data/lib/ozdenui/web/comp/web-view.js +588 -0
  144. data/lib/ozdenui/web/components.js +123 -0
  145. data/lib/ozdenui/web/index.html +39 -0
  146. data/lib/ozdenui/window.rb +188 -0
  147. data/lib/ozdenui/window_cocoa.rb +221 -0
  148. data/lib/ozdenui.rb +32 -0
  149. metadata +200 -0
@@ -0,0 +1,652 @@
1
+ # Bismillah
2
+ #
3
+ # objects.rb - ozdenui v0.1 (OzdenUI: basic.js for Ruby)
4
+ # UIObject (every object of the window is one: it keeps an id and the values it was given, and sends commands to the
5
+ # page, where the real basic.js object lives) and the basic objects: Box, Label, Button, Input, Icon, VGroup, HGroup.
6
+ #
7
+ # Started Date: November 2026
8
+ # Webpage: https://bug7a.github.io/ozdenui-ruby/
9
+ #
10
+ # Copyright 2026 Bugra Ozden <bugra.ozden@gmail.com> (https://bug7a.github.io/)
11
+ # Licensed under the Apache License, Version 2.0
12
+ # SPDX-License-Identifier: Apache-2.0
13
+
14
+ module OzdenUI
15
+ # What the gem is made of. A program uses the methods of OzdenUI; these classes are behind them.
16
+ module Runtime
17
+ # Every object of the window: a basic object, a group or a component.
18
+ #
19
+ # label.text = "Saved" # a value by its name (the names of the kit, in snake_case)
20
+ # label.set(text: "Saved", text_color: "green")
21
+ # field.text, box.checked?, list.selected_keys # what was given, or what the page told: no question to the page
22
+ # list.add_item(key: "a", text: "A") # a method of the kit
23
+ # keys = list.get_selected_keys # a method of the kit that answers (get..., is..., has...)
24
+ class UIObject
25
+ DARK_TEXT = "rgba(255, 255, 255, 0.86)"
26
+ DARK_VALUES = {
27
+ "Box" => { "color" => "#232322", "textColor" => DARK_TEXT },
28
+ "Label" => { "textColor" => DARK_TEXT },
29
+ "Input" => { "color" => "#2A2A29", "textColor" => DARK_TEXT, "borderColor" => "rgba(255, 255, 255, 0.2)" }
30
+ }.freeze
31
+ # WHY the basis and the minimum: a flex item is as large as its content unless it may shrink (no scroll bar came)
32
+ FILL_IN_COLUMN = [{ "width" => "100%", "height" => "auto", "grow" => 1 }, { "flexBasis" => "0", "minHeight" => "0" }].freeze
33
+ FILL_IN_ROW = [{ "width" => "auto", "height" => "100%", "grow" => 1 }, { "flexBasis" => "0", "minWidth" => "0" }].freeze
34
+ POSITION_NAMES = %w[left top width height].freeze
35
+ ASKING_METHOD = /\A(get|is|has)[A-Z]/.freeze
36
+
37
+ # @api private
38
+ attr_reader :id, :object_kind, :kit_spec, :kept_values
39
+
40
+ # The name of the object in the kit: "Label", "SmartButton".
41
+ attr_reader :type_name
42
+
43
+ # @api private
44
+ def initialize(app, type_name, kind, spec = nil)
45
+ @app = app
46
+ @id = app.new_id
47
+ @type_name = type_name
48
+ @object_kind = kind # :basic, :box, :group, :part, :page, :component
49
+ @kit_spec = spec
50
+ @kept_values = {} # the values given, by their kit names
51
+ @handlers = Hash.new { |hash, key| hash[key] = [] }
52
+ @listened = {}
53
+ @prop_handlers = {} # event -> the remover of the handler given as a value (on_click:)
54
+ @param_callbacks = {} # param -> the ids of the blocks given to it (also in its items)
55
+ @parts = {}
56
+ end
57
+
58
+ # @api private
59
+ def become_page(width, height)
60
+ @id = "page"
61
+ @kept_values["width"] = width
62
+ @kept_values["height"] = height
63
+ @listened["resize"] = true
64
+ @app.objects["page"] = self
65
+ # WHY always: the width and the height of the page come with an event of the page; without a handler of the
66
+ # program they would keep the size of the start
67
+ @app.send_command("op" => "listen", "id" => "page", "event" => "resize")
68
+ end
69
+
70
+ def row?
71
+ @type_name == "HGroup"
72
+ end
73
+
74
+ # The page or a Box: its objects have a place of their own (in a group, the group places them).
75
+ # @api private
76
+ def free_place?
77
+ @object_kind == :page || @object_kind == :box
78
+ end
79
+
80
+ def to_s
81
+ "<#{@type_name} #{@id}>"
82
+ end
83
+ alias inspect to_s
84
+
85
+ # The object in JavaScript, for evaluate: evaluate("#{label.js}.elem.offsetWidth")
86
+ def js
87
+ "__bp.objectOf('#{@id}')"
88
+ end
89
+
90
+ # *** VALUES:
91
+
92
+ # Changes values by their names: label.set(text: "Hi", font_size: 20). On a component, a param it has a method for
93
+ # in the kit (setUnitText) is changed with that method: the component draws it again, and its callback is not
94
+ # called (only a change of the user calls it).
95
+ def set(given = {}, **props)
96
+ @app.check("#{@type_name}.set")
97
+ values = {}
98
+ given.merge(props).each do |name, value|
99
+ key = Values.kit_name(name)
100
+ raise Error, "fill: is given when the object is made" if key == "fill"
101
+ values[key] = value unless take_special_value(key, value)
102
+ end
103
+ return self if values.empty?
104
+ warn_about_unknown_names(values)
105
+ keep_values(values)
106
+ @app.send_command("op" => "set", "id" => @id, "props" => page_values(values), "src" => Values.source_line)
107
+ self
108
+ end
109
+
110
+ # A value the object was given, or the state the page told (the checked of a CheckBox), by its name. A value that
111
+ # was never given is nil: obj.measure(:width) asks the page.
112
+ def get(name)
113
+ @kept_values[Values.kit_name(name)]
114
+ end
115
+ alias [] get
116
+
117
+ def []=(name, value)
118
+ set(name => value)
119
+ end
120
+
121
+ # The text of the object: of a Label, of an Input or a field as it is on the page now, of a button.
122
+ def text
123
+ %w[text html value].each { |key| return Values.text_of(@kept_values[key]) if @kept_values.key?(key) }
124
+ ""
125
+ end
126
+
127
+ def text=(text)
128
+ set("text" => text.to_s)
129
+ end
130
+
131
+ # The value of a component: of a SelectBox, a Slider, a Toggle, a DatePicker... (nil: none).
132
+ def value
133
+ @kept_values["value"]
134
+ end
135
+
136
+ # Changes the value of a component; its on_change is not called (only a change of the user calls it).
137
+ def value=(value)
138
+ set("value" => value)
139
+ end
140
+
141
+ # true for a checked CheckBox or RadioButton, and for a Toggle that is on.
142
+ def checked?
143
+ Values.on?(@kept_values.key?("checked") ? @kept_values["checked"] : @kept_values["value"])
144
+ end
145
+ alias checked checked?
146
+ alias on? checked?
147
+
148
+ # The width and the height of the page are current (the page tells them at every resize); of another object they
149
+ # are the values it was given (a size of "auto" or "100%": measure(:width)).
150
+ def width
151
+ @kept_values["width"]
152
+ end
153
+
154
+ def height
155
+ @kept_values["height"]
156
+ end
157
+
158
+ # A hidden object keeps its place in the program, not on the page.
159
+ def show
160
+ set("visible" => true)
161
+ end
162
+
163
+ def hide
164
+ set("visible" => false)
165
+ end
166
+
167
+ # A value as basic.js reads it on the page now (a size of "auto", the scrollTop of a box...). It is a question to
168
+ # the page: keep it out of loops.
169
+ def measure(name)
170
+ @app.check("measure")
171
+ @app.read(@id, page_key(Values.kit_name(name)))
172
+ rescue PageGone, Error => error
173
+ @app.warn_at("#{@type_name}.measure(#{name.inspect}): #{error.message}")
174
+ nil
175
+ end
176
+
177
+ # Calls a method of the object on the page: box.call(:set_motion, "left 0.3s"). Its result is not waited for.
178
+ # A method of the kit is also a method of the object: list.add_item(item).
179
+ def call(method, *args)
180
+ send_call(Values.kit_name(method), args)
181
+ self
182
+ end
183
+
184
+ # Calls a method of the object on the page and returns its result: table.ask(:get_selected_data). It is a question
185
+ # to the page: keep it out of loops.
186
+ def ask(method, *args)
187
+ @app.check("ask")
188
+ page_args = args.map { |value| to_page(value, nil) }
189
+ @app.ask(@id, Values.kit_name(method), page_args)
190
+ rescue PageGone, Error => error
191
+ @app.warn_at("#{@type_name}.ask(#{method.inspect}): #{error.message}")
192
+ nil
193
+ end
194
+
195
+ # Puts the keyboard into the object (an Input, a field).
196
+ def focus
197
+ return part(@kit_spec.focus_part).call(:focus) && self if @kit_spec && @kit_spec.focus_part
198
+ call(:focus)
199
+ end
200
+
201
+ # Removes the object and every object inside it, for good. To hide it for a while: obj.hide
202
+ def remove
203
+ @app.check("#{@type_name}.remove")
204
+ raise Error, "the page can not be removed: close the window with close" if @object_kind == :page
205
+ @app.send_command("op" => "remove", "id" => @id, "src" => Values.source_line)
206
+ nil
207
+ end
208
+
209
+ # *** EVENTS (of the page: click, input, keydown, mouseenter...):
210
+
211
+ # Listens to an event of the object: button.on(:click) { ... }, input.on(:key_down) { |input, event| ... }.
212
+ # The block gets the object and the event (event.key, event.x...). It returns a lambda that removes the handler.
213
+ # The page listens to its window: page.on(:key_down) { ... }, page.on(:resize) { ... }.
214
+ def on(event, handler = nil, &block)
215
+ handler ||= block
216
+ @app.check("#{@type_name}.on")
217
+ Values.check_handler(handler, "on(#{event.inspect})")
218
+ raise Error, "on(#{event.inspect}) takes a block" unless handler
219
+ name = Values.event_name(event)
220
+ @handlers[name] << handler
221
+ unless @listened[name]
222
+ @listened[name] = true
223
+ @app.send_command("op" => "listen", "id" => @id, "event" => name, "src" => Values.source_line)
224
+ end
225
+ -> { @handlers[name].delete_if { |candidate| candidate.equal?(handler) } }
226
+ end
227
+
228
+ # @api private
229
+ def receive_event(name, data)
230
+ @kept_values["text"] = data["text"] if data["text"].is_a?(String)
231
+ take_state(data["state"]) if data["state"].is_a?(Hash)
232
+ handlers = @handlers[name]
233
+ return if handlers.empty?
234
+ event = Event.new(name, data["key"], data["code"], data["shift"], data["ctrl"], data["alt"], data["meta"],
235
+ data["x"], data["y"], data["text"], @app.ruby_value(data["detail"]))
236
+ handlers.dup.each { |handler| Values.call_handler(handler, [self, event]) }
237
+ end
238
+
239
+ # The state the page sent with an event: the values the program reads without a question (checked, value...).
240
+ # @api private
241
+ def take_state(state)
242
+ state.each do |key, value|
243
+ value = @app.ruby_value(value)
244
+ @kept_values[key] = value
245
+ @kept_values[@kit_spec.aliases[key]] = value if @kit_spec && @kit_spec.aliases[key] # the "text" of a field is its "value"
246
+ end
247
+ uncheck_others_of_group
248
+ end
249
+
250
+ # *** A BOX HOLDS OBJECTS:
251
+
252
+ # Makes the objects of the block inside this box or group, also later (in an event, a timer):
253
+ #
254
+ # list.add { Label(text: "One more") }
255
+ #
256
+ # Only a Box, a group and the areas of the components hold objects (a ScrollArea, the panel of a Tabs...).
257
+ def add
258
+ @app.check("#{@type_name}.add")
259
+ raise Error, "add takes a block: box.add { Label(text: \"...\") }" unless block_given?
260
+ target = container_object
261
+ @app.containers.push(target.id)
262
+ begin
263
+ yield target
264
+ ensure
265
+ @app.containers.pop
266
+ end
267
+ self
268
+ end
269
+
270
+ # A box inside a component, to put objects into or to call: field.part(:input).call(:select). A step of the path
271
+ # is the name of a property, a place in a list (0) or a call of a method: { call: "getPanel", args: ["details"] }.
272
+ # The same path gives the same object.
273
+ def part(*path)
274
+ @app.check("#{@type_name}.part")
275
+ steps = path.map { |step| step.is_a?(Symbol) ? Values.kit_name(step) : step }
276
+ key = steps.inspect
277
+ known = @parts[key]
278
+ return known if known && @app.objects[known.id]
279
+ part = UIObject.new(@app, "Part", :part)
280
+ @app.objects[part.id] = part
281
+ @parts[key] = part
282
+ @app.send_command("op" => "part", "id" => part.id, "of" => @id, "path" => to_page(steps, nil), "src" => Values.source_line)
283
+ part
284
+ end
285
+
286
+ # The box that takes the objects: the object itself, or a box inside a component (the content of a ScrollArea).
287
+ # @api private
288
+ def container_object
289
+ return self if %i[box group part page].include?(@object_kind)
290
+ if @object_kind == :component
291
+ return self if @kit_spec.container == "self"
292
+ return part(@kit_spec.container) if @kit_spec.container
293
+ end
294
+ hint = "only a Box, a group or an area of a component can. Make the object next to it, in the same box."
295
+ hint = @kit_spec.container_hint if @kit_spec && @kit_spec.container_hint
296
+ raise Error, "a #{@type_name} can not hold other objects: #{hint}"
297
+ end
298
+
299
+ # *** NAMES THAT ARE NOT METHODS OF THIS CLASS: a value (label.font_size, label.font_size = 20, box.enabled?) or a
300
+ # method of the kit (list.add_item(item), table.get_selected_data).
301
+
302
+ def method_missing(name, *args, &block)
303
+ text = name.to_s
304
+ super if text.start_with?("to_") # WHY: Ruby asks an object for to_ary, to_str... to know what it is
305
+ return set(text.chomp("=").to_sym => args.first) if text.end_with?("=") && args.length == 1
306
+ return Values.on?(@kept_values[Values.kit_name(text.chomp("?").to_sym)]) if text.end_with?("?") && args.empty?
307
+ key = Values.kit_name(name)
308
+ return @kept_values[key] if args.empty? && @kept_values.key?(key)
309
+ if kit_method?(key)
310
+ return ask(key, *args) if ASKING_METHOD.match?(key)
311
+ return call(key, *args)
312
+ end
313
+ names = known_names
314
+ return nil if args.empty? && (names.nil? || names.include?(key)) # a value that was not given
315
+ guess = Values.closest_name(key, (names || []) + kit_methods)
316
+ hint = guess ? " (did you mean #{Values.ruby_name(guess)}?)" : ""
317
+ raise NoMethodError, "#{name} is not a value or a method of #{@type_name}#{hint}"
318
+ end
319
+
320
+ def respond_to_missing?(name, include_private = false)
321
+ return super if name.to_s.start_with?("to_")
322
+ key = Values.kit_name(name.to_s.chomp("=").chomp("?").to_sym)
323
+ names = known_names
324
+ @kept_values.key?(key) || kit_method?(key) || names.nil? || names.include?(key) || super
325
+ end
326
+
327
+ # @api private
328
+ def known_names
329
+ case @object_kind
330
+ when :component then @kit_spec.known_names
331
+ when :part then nil # what it is, is known only on the page
332
+ when :group then Kit::GROUP_NAMES
333
+ else Kit::OBJECT_PROPS
334
+ end
335
+ end
336
+
337
+ # @api private
338
+ def warn_about_unknown_names(values)
339
+ known = known_names
340
+ return unless known
341
+ values.each_key do |name|
342
+ next if known.include?(name)
343
+ guess = Values.closest_name(name, known)
344
+ hint = guess ? " (did you mean #{Values.ruby_name(guess)}?)" : ""
345
+ @app.warn_at("#{@type_name}: #{Values.ruby_name(name)} is not a value of #{@type_name}#{hint}")
346
+ end
347
+ end
348
+
349
+ # The values given (not the blocks), kept to be read back without a question to the page.
350
+ # @api private
351
+ def keep_values(values)
352
+ values.each do |key, value|
353
+ next if Values.callable?(value)
354
+ if plain_text?
355
+ # WHY: after one of them, the other one is not what the object shows
356
+ @kept_values.delete("html") if key == "text"
357
+ @kept_values.delete("text") if key == "html"
358
+ end
359
+ @kept_values[key] = value
360
+ next unless @kit_spec
361
+ # A value with two names (the "text" of a field is its "value") is read by both.
362
+ @kit_spec.aliases.each do |short, long|
363
+ @kept_values[short] = value if key == long
364
+ @kept_values[long] = value if key == short
365
+ end
366
+ end
367
+ end
368
+
369
+ # The values as they go to the page. The blocks given to a param replace the ones given to it before.
370
+ # @api private
371
+ def page_values(values)
372
+ result = {}
373
+ values.each { |key, value| result[page_key(key)] = page_value(key, value) }
374
+ result
375
+ end
376
+
377
+ # @api private
378
+ def page_value(key, value)
379
+ value = value.to_s if value.is_a?(Symbol)
380
+ value = Values.flag_as_number(value) unless data_param?(key)
381
+ value = Values.picture_of_path(value) if value.is_a?(String) && Values.file_param?(key)
382
+ value = Values.number_format(value) if value.is_a?(String) && @kit_spec && @kit_spec.format_param?(key)
383
+ ids = []
384
+ converted = @app.to_page(value, @id, ids, Values.style_name?(key))
385
+ replace_callbacks(key, ids)
386
+ if converted.is_a?(Hash) && converted["__bpCallback"] && @kit_spec && @kit_spec.whole_args[key]
387
+ converted["wholeArgs"] = @kit_spec.whole_args[key]
388
+ end
389
+ converted
390
+ end
391
+
392
+ # @api private
393
+ def to_page(value, ids)
394
+ @app.to_page(value, @id, ids)
395
+ end
396
+
397
+ # @api private
398
+ def send_call(method, args, refresh: false, slot: nil)
399
+ @app.check("#{@type_name}.#{Values.ruby_name(method)}")
400
+ if @kit_spec && !@kit_spec.methods.empty? && !kit_method?(method)
401
+ guess = Values.closest_name(method, @kit_spec.methods)
402
+ hint = guess ? " (did you mean #{Values.ruby_name(guess)}?)" : ""
403
+ @app.warn_at("#{@type_name}: #{Values.ruby_name(method)} is not a method of #{@type_name}#{hint}")
404
+ end
405
+ ids = []
406
+ page_args = args.map { |value| to_page(value, ids) }
407
+ replace_callbacks(slot, ids) if slot
408
+ command = { "op" => "call", "id" => @id, "method" => method, "args" => page_args, "src" => Values.source_line }
409
+ # WHY: a method of the kit may change what the program reads without a question (set_selected, clear_selection)
410
+ command["refresh"] = 1 if refresh || (@kit_spec && !@kit_spec.state.empty?)
411
+ @app.send_command(command)
412
+ end
413
+
414
+ # The handler given as a value (on_click: block, obj.on_click = block): a new one replaces it, nil takes it away;
415
+ # the handlers of on() stay.
416
+ # @api private
417
+ def set_dom_handler(name, handler)
418
+ remover = @prop_handlers.delete(name)
419
+ remover&.call
420
+ return unless handler
421
+ if name == "onEnter"
422
+ # The Enter key of an Input
423
+ @prop_handlers[name] = on("keydown") do |obj, event|
424
+ Values.call_handler(handler, [obj, event]) if event.key == "Enter"
425
+ end
426
+ return
427
+ end
428
+ @prop_handlers[name] = on(Values.event_name(name), handler)
429
+ end
430
+
431
+ # WHY a method and not a value: an Icon loads its file with load(); a value named picture would do nothing.
432
+ # @api private
433
+ def set_picture(source)
434
+ @kept_values["picture"] = source
435
+ call(:load, Values.picture(source))
436
+ end
437
+
438
+ private
439
+
440
+ def plain_text?
441
+ @type_name == "Label" || @type_name == "Button"
442
+ end
443
+
444
+ # WHY plainText: the text of basic.js is HTML, so a typed "<b>" would be bold and a "<img onerror=...>" would run.
445
+ # Here text is the text as it is written; html is the HTML (a Label and a Button of basic.js take it as their text).
446
+ def page_key(key)
447
+ return "plainText" if plain_text? && key == "text"
448
+ return "text" if plain_text? && key == "html"
449
+ key
450
+ end
451
+
452
+ def data_param?(key)
453
+ @kit_spec ? @kit_spec.data_param?(key) : key == "value"
454
+ end
455
+
456
+ def kit_methods
457
+ (@kit_spec ? @kit_spec.methods : []) + Kit::OBJECT_METHODS
458
+ end
459
+
460
+ def kit_method?(name)
461
+ (@kit_spec && @kit_spec.methods.include?(name)) || Kit::OBJECT_METHODS.include?(name) || (@object_kind == :part && name.match?(/\A[a-z]\w*\z/))
462
+ end
463
+
464
+ # WHY: every obj.on_change = block would keep a new block for ever, and so would every new list of items whose
465
+ # items have an onClick (with what each block holds).
466
+ def replace_callbacks(slot, ids)
467
+ (@param_callbacks.delete(slot) || []).each { |id| @app.callbacks.delete(id) }
468
+ @param_callbacks[slot] = ids unless ids.empty?
469
+ end
470
+
471
+ # A value that is not a plain value of the page: an event, the picture of an Icon, a param with a method.
472
+ def take_special_value(name, value)
473
+ return set_component_value(name, value) if @object_kind == :component
474
+ if Values.callback_name?(name)
475
+ Values.check_handler(value, Values.ruby_name(name))
476
+ set_dom_handler(name, value)
477
+ return true
478
+ end
479
+ if name == "picture" && @type_name == "Icon"
480
+ set_picture(value)
481
+ return true
482
+ end
483
+ false
484
+ end
485
+ end
486
+
487
+ # @api private
488
+ class App
489
+ # A value as it goes to the page (JSON). A block becomes a callback of the owner ({ __bpCallback: id }), an object
490
+ # its id. ids takes the ids of the callbacks made. kit_keys: the keys of a table are names of the kit (a style):
491
+ # a symbol in snake_case is written as the kit writes it.
492
+ def to_page(value, owner_id, ids, kit_keys = false)
493
+ case value
494
+ when nil, true, false, String, Integer then value
495
+ when Float then value.finite? ? value : nil
496
+ when Symbol then value.to_s
497
+ when UIObject then { "__bpObject" => value.id }
498
+ when NumberFormat
499
+ { "__bpFormat" => { "prefix" => value.prefix, "suffix" => value.suffix, "thousands" => value.thousands,
500
+ "decimals" => value.decimals, "percent" => value.percent } }
501
+ when Proc, Method
502
+ id = register_callback(value, owner_id)
503
+ ids&.push(id)
504
+ { "__bpCallback" => id }
505
+ when Hash
506
+ result = {}
507
+ value.each do |key, item|
508
+ name = kit_keys ? Values.kit_name(key) : key.to_s
509
+ result[name] = to_page(item, owner_id, ids, kit_keys)
510
+ end
511
+ result
512
+ when Array then value.map { |item| to_page(item, owner_id, ids, kit_keys) }
513
+ when Time then value.iso8601
514
+ when Struct then to_page(value.to_h, owner_id, ids, kit_keys)
515
+ else
516
+ return value.iso8601 if value.respond_to?(:iso8601) # a Date, a DateTime
517
+ return to_page(value.to_h, owner_id, ids, kit_keys) if value.respond_to?(:to_h)
518
+ return value.to_f if value.is_a?(Numeric) # a Rational, a BigDecimal
519
+ value.to_s
520
+ end
521
+ end
522
+ end
523
+
524
+ # @api private
525
+ module Maker
526
+ module_function
527
+
528
+ # The values of a call: Box(10, 20, 200, 100, color: "white") -> { "left" => 10, ..., "color" => "white" }, by the
529
+ # names of the kit, and what is not a value of the page (fill:, unframed:).
530
+ def read(type_name, args, props)
531
+ # WHY: Ruby 2.6 gives a table with text keys ("tab_border" => 1) as the last value, not as a named one
532
+ props = args.pop.merge(props) if args.last.is_a?(Hash)
533
+ if args.length > 4 || args.any? { |value| !value.is_a?(Numeric) && !value.is_a?(String) }
534
+ raise Error, "#{type_name}(#{args.map(&:inspect).join(', ')}): the values without a name are left, top, width, height; " \
535
+ "the others have names: #{type_name}(text: \"...\")"
536
+ end
537
+ values = {}
538
+ args.each_with_index { |value, index| values[UIObject::POSITION_NAMES[index]] = value }
539
+ extra = {}
540
+ props.each do |name, value|
541
+ key = Values.kit_name(name)
542
+ if %w[fill unframed].include?(key)
543
+ extra[key] = Values.on?(value)
544
+ elsif !value.nil? || Values.callback_name?(key)
545
+ values[key] = value.is_a?(Symbol) ? value.to_s : value
546
+ end
547
+ end
548
+ [values, extra]
549
+ end
550
+
551
+ def take_fill(values, parent)
552
+ props, css = parent && parent.row? ? UIObject::FILL_IN_ROW : UIObject::FILL_IN_COLUMN
553
+ props.each { |key, value| values[key] = value unless values.key?(key) }
554
+ own = values["css"].is_a?(Hash) ? values["css"].map { |key, value| [Values.kit_name(key), value] }.to_h : {}
555
+ values["css"] = css.merge(own)
556
+ end
557
+
558
+ def basic(type_name, kind, args, props, content)
559
+ app = OzdenUI.current_app
560
+ app.check(type_name)
561
+ values, extra = read(type_name, args, props)
562
+ obj = UIObject.new(app, type_name, kind)
563
+ parent = app.objects[app.container_id]
564
+ if parent && parent.free_place?
565
+ # WHY: basic.js puts an object without a place at -1000, -1000 (out of sight until it is placed). Outside a
566
+ # group it gets the top left corner: the commands go together, so nothing is seen early.
567
+ values["left"] = 0 unless values.key?("left")
568
+ values["top"] = 0 unless values.key?("top")
569
+ end
570
+ take_fill(values, parent) if extra["fill"]
571
+ handlers = {}
572
+ values.keys.each do |name|
573
+ next unless Values.callback_name?(name)
574
+ Values.check_handler(values[name], "#{type_name}(#{Values.ruby_name(name)}: ...)")
575
+ handlers[name] = values.delete(name)
576
+ end
577
+ picture = values.delete("picture") if type_name == "Icon"
578
+ obj.warn_about_unknown_names(values)
579
+ if kind == :group && !Values.on?(values["scrollY"]) && !Values.on?(values["scrollX"]) && !values.key?("clipContent")
580
+ # WHY: a group of basic.js cuts a button that grows, a focus ring or a shadow at its edge, so nearly every
581
+ # group of a program would say clip_content: false.
582
+ values["clipContent"] = 0
583
+ end
584
+ if app.dark? && kind != :group
585
+ (UIObject::DARK_VALUES[type_name] || {}).each { |key, value| values[key] = value unless values.key?(key) }
586
+ end
587
+ obj.keep_values(values)
588
+ app.objects[obj.id] = obj
589
+ app.send_command("op" => "create", "id" => obj.id, "type" => type_name, "args" => [], "props" => obj.page_values(values),
590
+ "parent" => app.container_id, "src" => Values.source_line)
591
+ if type_name == "Input"
592
+ # The page always tells the text of an Input
593
+ obj.on("input") {}
594
+ end
595
+ handlers.each { |name, handler| obj.set_dom_handler(name, handler) }
596
+ obj.set_picture(picture) if picture
597
+ obj.add(&content) if content
598
+ obj
599
+ end
600
+ end
601
+ end
602
+
603
+ module_function
604
+
605
+ # A rectangle that holds other objects: the objects made in its block go into it, each with a place of its own.
606
+ #
607
+ # Box(20, 20, 200, 120, color: "white", round: 8) do
608
+ # Label(12, 12, text: "In the box")
609
+ # end
610
+ def Box(*args, **props, &content)
611
+ Runtime::Maker.basic("Box", :box, args, props, content)
612
+ end
613
+
614
+ # Holds objects one under the other (flexbox). A group is 100% x 100% by default; hug: true wraps its objects,
615
+ # fill: true takes the room the other objects of its group leave. Unlike in basic.js, a group does not cut what goes
616
+ # out of it (clip_content: true cuts).
617
+ def VGroup(*args, **props, &content)
618
+ Runtime::Maker.basic("VGroup", :group, args, props, content)
619
+ end
620
+
621
+ # Holds objects side by side (flexbox). See VGroup.
622
+ def HGroup(*args, **props, &content)
623
+ Runtime::Maker.basic("HGroup", :group, args, props, content)
624
+ end
625
+
626
+ # A text. text: is shown as it is written; html: takes HTML.
627
+ def Label(*args, **props)
628
+ Runtime::Maker.basic("Label", :basic, args, props, nil)
629
+ end
630
+
631
+ # The plain button of basic.js. SmartButton is the modern one.
632
+ def Button(*args, **props)
633
+ Runtime::Maker.basic("Button", :basic, args, props, nil)
634
+ end
635
+
636
+ # A plain text box: input.text is always the text on the page. TextField is the modern one.
637
+ def Input(*args, **props)
638
+ Runtime::Maker.basic("Input", :basic, args, props, nil)
639
+ end
640
+
641
+ # A picture: Icon(width: 48, height: 48, picture: "photo.png"). ImageView is the one with a zoom.
642
+ def Icon(*args, **props)
643
+ Runtime::Maker.basic("Icon", :basic, args, props, nil)
644
+ end
645
+
646
+ # The box that takes the objects made now: inside the block of a group, the group itself.
647
+ def current
648
+ app = OzdenUI.current_app
649
+ app.check("current")
650
+ app.objects[app.container_id]
651
+ end
652
+ end