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,107 @@
1
+ # Bismillah
2
+ #
3
+ # command.rb - ozdenui v0.1 (OzdenUI: basic.js for Ruby)
4
+ # The ozdenui command of the gem: the rules of OzdenUI for the AI assistant of a project (agents), and the params of a
5
+ # component in the terminal (doc).
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
+ require_relative "version"
15
+
16
+ module OzdenUICommand
17
+ LIB = __dir__
18
+ START_MARK = "<!-- ozdenui-ruby: start (written by `ozdenui agents`; the next run writes this part again) -->"
19
+ END_MARK = "<!-- ozdenui-ruby: end -->"
20
+ MESSAGES = {
21
+ made: "%s: made, with the rules of OzdenUI for Ruby %s.",
22
+ added: "%s: the rules of OzdenUI for Ruby %s are added at its end; your lines are as they were.",
23
+ updated: "%s: the rules of OzdenUI for Ruby are updated to %s; your lines are as they were.",
24
+ same: "%s: already has the rules of OzdenUI for Ruby %s."
25
+ }.freeze
26
+ HELP = <<~TEXT
27
+ ozdenui %s (OzdenUI for Ruby)
28
+
29
+ ozdenui agents The rules of OzdenUI for an AI assistant, into AGENTS.md of this folder.
30
+ ozdenui agents CLAUDE.md Into another file.
31
+ ozdenui agents --print Only prints them.
32
+ ozdenui doc The names of the components.
33
+ ozdenui doc SelectBox The params of a component, with their defaults.
34
+
35
+ OZDENUI_LOOK=app.png ruby app.rb Runs your program, saves a picture of its window as app.png and ends.
36
+ TEXT
37
+
38
+ module_function
39
+
40
+ def rules
41
+ File.read(File.join(LIB, "AGENTS.md"), encoding: "UTF-8").strip
42
+ end
43
+
44
+ # The text of the file with the rules between the marks: in the place of the old ones, or at its end.
45
+ def with_rules(old_text)
46
+ block = "#{START_MARK}\n#{rules}\n#{END_MARK}\n"
47
+ start = old_text.index(START_MARK)
48
+ finish = old_text.index(END_MARK)
49
+ return old_text[0...start] + block + old_text[(finish + END_MARK.length)..-1].sub(/\A\n+/, "") if start && finish && finish > start
50
+ return block if old_text.strip.empty?
51
+ old_text.sub(/\n+\z/, "") + "\n\n" + block
52
+ end
53
+
54
+ # Writes the rules into the file; it returns what it did.
55
+ def write_rules(target)
56
+ old_text = File.exist?(target) ? File.read(target, encoding: "UTF-8") : ""
57
+ new_text = with_rules(old_text)
58
+ return :same if new_text == old_text
59
+ File.write(target, new_text)
60
+ return :made if old_text.strip.empty?
61
+ old_text.include?(START_MARK) ? :updated : :added
62
+ end
63
+
64
+ def sections
65
+ text = File.read(File.join(LIB, "COMPONENTS.md"), encoding: "UTF-8")
66
+ text.split(/^## /).drop(1).map { |part| [part[/\A\S+/], "## " + part.rstrip] }.to_h
67
+ end
68
+
69
+ def doc(name)
70
+ all = sections
71
+ if name.nil?
72
+ puts all.keys.each_slice(6).map { |row| row.join(" ") }, "", "ozdenui doc <name>: its params"
73
+ return 0
74
+ end
75
+ found = all.keys.find { |key| key.downcase == name.downcase.delete("_") }
76
+ unless found
77
+ close = all.keys.select { |key| key.downcase.include?(name.downcase) }
78
+ warn "No component #{name}.#{close.empty? ? '' : " Did you mean: #{close.join(', ')}?"} (ozdenui doc: every name)"
79
+ return 1
80
+ end
81
+ puts all[found]
82
+ 0
83
+ end
84
+
85
+ def run(words)
86
+ case words.first
87
+ when "doc" then doc(words[1])
88
+ when "agents"
89
+ if words[1] == "--print"
90
+ puts rules
91
+ return 0
92
+ end
93
+ target = words[1] || "AGENTS.md"
94
+ puts format(MESSAGES[write_rules(target)], target, OzdenUI::VERSION)
95
+ 0
96
+ when "version", "--version", "-v"
97
+ puts OzdenUI::VERSION
98
+ 0
99
+ else
100
+ puts format(HELP, OzdenUI::VERSION)
101
+ words.empty? ? 0 : 2
102
+ end
103
+ rescue SystemCallError => error
104
+ warn error.message
105
+ 1
106
+ end
107
+ end
@@ -0,0 +1,368 @@
1
+ # Bismillah
2
+ #
3
+ # components.rb - ozdenui v0.1 (OzdenUI: basic.js for Ruby)
4
+ # The components of the kit: every one is a method of its name (SmartButton(...), SelectBox(...)), made from the table
5
+ # of kit.rb. What a table can not say is here: the look of the window, the areas that hold objects, a param changed
6
+ # later with the method of the kit.
7
+ #
8
+ # Started Date: November 2026
9
+ # Webpage: https://bug7a.github.io/ozdenui-ruby/
10
+ #
11
+ # Copyright 2026 Bugra Ozden <bugra.ozden@gmail.com> (https://bug7a.github.io/)
12
+ # Licensed under the Apache License, Version 2.0
13
+ # SPDX-License-Identifier: Apache-2.0
14
+
15
+ module OzdenUI
16
+ # What the gem is made of. A program uses the methods of OzdenUI; these classes are behind them.
17
+ module Runtime
18
+ # @api private
19
+ module Maker
20
+ # A light package the code names, and its dark twin: a dark window takes the twin.
21
+ DARK_TWINS = {
22
+ "ItemList" => { "gallery" => "galleryDark" },
23
+ "SmartButton" => { "minimalDanger" => "minimalDangerDark" }
24
+ }.freeze
25
+ DARK_TONES = %w[cyan orange yellow green blue ink].push(App::ACCENT_STYLE_NAME).freeze
26
+ # The callback of a component that only keeps its state in the program (the page sends it with every call).
27
+ KEEP_STATE = proc {}
28
+
29
+ module_function
30
+
31
+ def component(type_name, args, props, content)
32
+ app = OzdenUI.current_app
33
+ app.check(type_name)
34
+ spec = Kit[type_name] || Spec.new(type_name, {})
35
+ values, extra = read(type_name, args, props)
36
+ obj = UIObject.new(app, type_name, :component, spec)
37
+ if content && !spec.container
38
+ hint = spec.container_hint ? "objects go into it with #{spec.container_hint}" : "it holds no other objects"
39
+ raise Error, "#{type_name}(...) { ... }: a #{type_name} takes no block: #{hint}"
40
+ end
41
+
42
+ parent_id = spec.on_page? ? "page" : app.container_id
43
+ parent = app.objects[parent_id]
44
+ if !spec.own_place? && !spec.on_page? && parent && parent.free_place?
45
+ values["left"] = 0 unless values.key?("left")
46
+ values["top"] = 0 unless values.key?("top")
47
+ end
48
+ take_fill(values, parent) if extra["fill"]
49
+ values["style"] = unframed_style(values["style"]) if extra["unframed"]
50
+ # WHY: on_enter is not a param of the kit: the Enter key of the field
51
+ on_enter = values.delete("onEnter") if spec.focus_part
52
+ obj.warn_about_unknown_names(values)
53
+ spec.aliases.each do |short, long|
54
+ values[long] = values.delete(short) if values.key?(short) && !spec.params.include?(short)
55
+ end
56
+ is_window_look = take_window_look(app, spec, values)
57
+ spec.defaults.each do |key, value|
58
+ next if key == "styleName" && is_window_look # WHY: the look of a light window; the window chose another one
59
+ values[key] = value unless values.key?(key)
60
+ end
61
+ if type_name == "FilePicker" && values["onOpenDialog"].nil?
62
+ values["onOpenDialog"] = Files.method(:open_dialog_of_picker) # WHY: the dialog of the page gives no path
63
+ end
64
+ # WHY: the state of the component comes back with these callbacks
65
+ spec.sync.each { |name| values[name] = KEEP_STATE if values[name].nil? }
66
+ values.each do |key, value|
67
+ Values.check_handler(value, "#{type_name}(#{Values.ruby_name(key)}: ...)") if Values.callback_name?(key)
68
+ end
69
+ size_into_style(values) if spec.size_in_style?
70
+ spec.state_defaults.each { |key, value| obj.kept_values[key] = value }
71
+ obj.keep_values(values)
72
+ if type_name == "Tabs" && obj.kept_values["value"].nil?
73
+ obj.kept_values["value"] = first_key(values["tabs"]) # the kit selects the first tab
74
+ end
75
+ app.objects[obj.id] = obj
76
+ command = { "op" => "create", "id" => obj.id, "type" => type_name, "args" => [], "props" => obj.page_values(values),
77
+ "parent" => parent_id, "src" => Values.source_line }
78
+ command["state"] = spec.state unless spec.state.empty?
79
+ app.send_command(command)
80
+ obj.set_dom_handler("onEnter", on_enter) if on_enter
81
+ obj.add(&content) if content
82
+ obj
83
+ end
84
+
85
+ # The key of the first item of a list of the kit: its "key", else its "text", else the item itself.
86
+ def first_key(list)
87
+ first = list.is_a?(Array) ? list.first : nil
88
+ return first unless first.is_a?(Hash)
89
+ first[:key] || first["key"] || first[:text] || first["text"]
90
+ end
91
+
92
+ def kit_keys(table)
93
+ table.is_a?(Hash) ? table.map { |key, value| [Values.kit_name(key), value] }.to_h : {}
94
+ end
95
+
96
+ # The style of a component without its frame: no border and no rounded corners on its box. The keys of the code win.
97
+ def unframed_style(style)
98
+ result = kit_keys(style)
99
+ result["box"] = { "border" => 0, "round" => 0 }.merge(kit_keys(result["box"]))
100
+ result
101
+ end
102
+
103
+ # WHY: a SmartTable reads its size from its style; a width given as a param would change only its box.
104
+ def size_into_style(values)
105
+ style = kit_keys(values["style"])
106
+ %w[width height].each { |name| style[name] = values[name] if values.key?(name) && !style.key?(name) }
107
+ # WHY: with a height of its own the rows keep their height and their count follows the table; without it the 16
108
+ # rows of the kit share the height, each a few px high in a small table
109
+ values["fitLineCount"] = 1 if values.key?("height") && !values.key?("fitLineCount")
110
+ values["style"] = style unless style.empty?
111
+ end
112
+
113
+ # A minimal window: the minimal look; a dark window: the dark look; unless the code chose one. It is true when the
114
+ # window chose the look.
115
+ def take_window_look(app, spec, values)
116
+ take_window_tone(app, values) if spec.name == "SmartButton"
117
+ if values["styleName"].is_a?(String)
118
+ twin = (DARK_TWINS[spec.name] || {})[values["styleName"]]
119
+ values["styleName"] = twin if app.dark? && twin
120
+ return false
121
+ end
122
+ if spec.theme_param?
123
+ theme = app.minimal? ? (app.dark? ? "minimalDark" : "minimal") : (app.dark? ? "dark" : nil)
124
+ values["theme"] = theme if theme && !values.key?("theme")
125
+ return false
126
+ end
127
+ if app.minimal? && spec.minimal?
128
+ name = app.options[:accent] ? App::ACCENT_STYLE_NAME : "minimal"
129
+ values["styleName"] = app.dark? ? "#{name}Dark" : name
130
+ return true
131
+ end
132
+ if app.dark? && spec.dark_style
133
+ values["styleName"] = spec.dark_style
134
+ return true
135
+ end
136
+ false
137
+ end
138
+
139
+ # The tone of a SmartButton in this window: "accent" is the main action in the color of the window, and a dark
140
+ # window takes the dark twin of a tone.
141
+ def take_window_tone(app, values)
142
+ tone = values["tone"]
143
+ return if !tone.is_a?(String) || (values.key?("styleName") && tone != "accent")
144
+ if tone == "accent"
145
+ tone = if app.options[:accent] then App::ACCENT_STYLE_NAME
146
+ elsif app.minimal? then "ink"
147
+ else "cyan"
148
+ end
149
+ end
150
+ tone += "Dark" if app.dark? && DARK_TONES.include?(tone)
151
+ values["tone"] = tone
152
+ end
153
+ end
154
+
155
+ class UIObject
156
+ # *** THE AREAS OF THE COMPONENTS THAT HOLD OBJECTS:
157
+
158
+ # The area of a tab of a Tabs, by the key of the tab; the objects made in the block go into it.
159
+ #
160
+ # tabs.panel("general") { Label(text: "General") }
161
+ def panel(key, &content)
162
+ must_be("Tabs", "panel")
163
+ key = key.to_s
164
+ unless @parts["panel:#{key}"]
165
+ # WHY once: it makes the wrapper of the panel, and a second call would clear it
166
+ send_call("setPanelContent", [key, { "__bpNoop" => 1 }])
167
+ @parts["panel:#{key}"] = part({ "call" => "getPanel", "args" => [key] }, "wrapper")
168
+ end
169
+ fill_part(@parts["panel:#{key}"], content)
170
+ end
171
+
172
+ # The content of a section of an Accordion, by its key.
173
+ def section(key, &content)
174
+ must_be("Accordion", "section")
175
+ fill_part(part({ "call" => "getContent", "args" => [key.to_s] }), content)
176
+ end
177
+
178
+ # The first area of a Splitter (the left or the top one).
179
+ def first(&content)
180
+ must_be("Splitter", "first")
181
+ fill_part(part("first"), content)
182
+ end
183
+
184
+ # The second area of a Splitter.
185
+ def second(&content)
186
+ must_be("Splitter", "second")
187
+ fill_part(part("second"), content)
188
+ end
189
+
190
+ # The area of a SidePanel: its objects stand one under the other.
191
+ def body(&content)
192
+ must_be("SidePanel", "body")
193
+ fill_part(part({ "call" => "getBody" }), content)
194
+ end
195
+
196
+ # Gives an object a cell of its own in a GridGroup (from 0), or a span:
197
+ #
198
+ # grid.place(wide, column_span: 2) # also column:, row:, row_span:, align:
199
+ #
200
+ # Inside the block of the grid, the grid itself is what the block gets: GridGroup(columns: 4) { |grid| ... }
201
+ def place(item, **where)
202
+ must_be("GridGroup", "place")
203
+ send_call("place", [item, where.map { |key, value| [Values.kit_name(key), value] }.to_h])
204
+ item
205
+ end
206
+
207
+ # Shows the warning of a field (TextField and its kin) when its text is not right, and is true when it is right: a
208
+ # required field that is empty, an e-mail that is not one...
209
+ def valid?
210
+ unless @kit_spec && @kit_spec.focus_part
211
+ raise Error, "valid? is of a field (TextField, EmailField...), not of a #{@type_name}"
212
+ end
213
+ send_call("checkInputValue", [])
214
+ Values.number_of(measure(:status)).zero?
215
+ end
216
+
217
+ # Lines into a LogView from any thread: log.writer(:error).puts("the file is missing"); $stdout = log.writer
218
+ # The level is "info", "warn", "error", "debug" or "success".
219
+ def writer(level = :info)
220
+ must_be("LogView", "writer")
221
+ LineWriter.new(self, level.to_s)
222
+ end
223
+
224
+ # @api private
225
+ def set_component_value(name, value)
226
+ spec = @kit_spec
227
+ name = spec.aliases[name] if spec.aliases[name] && !spec.params.include?(name)
228
+ if name == "onEnter" && spec.focus_part
229
+ Values.check_handler(value, "on_enter")
230
+ set_dom_handler("onEnter", value)
231
+ return true
232
+ end
233
+ if Values.callback_name?(name)
234
+ Values.check_handler(value, Values.ruby_name(name))
235
+ value ||= Maker::KEEP_STATE # WHY: a nil would go to the page as null, and the component calls it
236
+ @app.send_command("op" => "set", "id" => @id, "props" => { name => page_value(name, value) }, "src" => Values.source_line)
237
+ return true
238
+ end
239
+ if name == "styleName" && spec.methods.include?("setTheme")
240
+ # The look of one object while the program runs (a dark switch of its own, without a new page)
241
+ keep_values(name => value)
242
+ send_call("setTheme", [value.to_s])
243
+ return true
244
+ end
245
+ method, silent = spec.setters[name]
246
+ unless method
247
+ if %w[styleName style].include?(name)
248
+ @app.warn_at("#{@type_name}: #{Values.ruby_name(name)} is the look the component is made with; it does not change later")
249
+ end
250
+ return false
251
+ end
252
+ keep_values(name => value)
253
+ uncheck_others_of_group
254
+ args = [page_value(name, value)]
255
+ args << 1 if silent
256
+ command = { "op" => "call", "id" => @id, "method" => method, "args" => args, "src" => Values.source_line }
257
+ # WHY: the method may change the state too (a slider keeps its value inside its range)
258
+ command["refresh"] = 1 unless spec.state.empty?
259
+ @app.send_command(command)
260
+ true
261
+ end
262
+
263
+ # WHY: the page tells only of the radio the user pressed; the one that was checked before would stay checked in
264
+ # the program.
265
+ # @api private
266
+ def uncheck_others_of_group
267
+ return unless @type_name == "RadioButton" && Values.on?(@kept_values["checked"])
268
+ @app.objects.each_value do |other|
269
+ next if other.equal?(self) || other.type_name != "RadioButton" || other.kept_values["group"] != @kept_values["group"]
270
+ other.kept_values["checked"] = 0
271
+ end
272
+ end
273
+
274
+ private
275
+
276
+ def must_be(type_name, method)
277
+ raise Error, "#{method} is of a #{type_name}, not of a #{@type_name}" unless @type_name == type_name
278
+ end
279
+
280
+ def fill_part(part, content)
281
+ part.add(&content) if content
282
+ part
283
+ end
284
+ end
285
+
286
+ # @api private
287
+ class LineWriter
288
+ def initialize(obj, level)
289
+ @obj = obj
290
+ @level = level
291
+ end
292
+
293
+ def write(*texts)
294
+ text = texts.join
295
+ lines = text.chomp.split("\n", -1)
296
+ OzdenUI.ui { lines.each { |line| @obj.call(@level, line) } }
297
+ text.bytesize
298
+ end
299
+
300
+ def puts(*texts)
301
+ write(texts.empty? ? "\n" : texts.flatten.map { |text| text.to_s.chomp + "\n" }.join)
302
+ nil
303
+ end
304
+
305
+ def print(*texts)
306
+ write(*texts.map(&:to_s))
307
+ nil
308
+ end
309
+
310
+ def <<(text)
311
+ write(text.to_s)
312
+ self
313
+ end
314
+
315
+ def flush
316
+ self
317
+ end
318
+
319
+ def sync
320
+ true
321
+ end
322
+
323
+ def sync=(value); end
324
+ end
325
+ end
326
+
327
+ module_function
328
+
329
+ # Makes a component of the kit by its name, with its params: component("Gauge", value: 72, width: 200). Every
330
+ # component is also a method of its name: Gauge(value: 72, width: 200).
331
+ def component(type_name, *args, **props, &content)
332
+ Runtime::Maker.component(type_name.to_s, args, props, content)
333
+ end
334
+
335
+ Runtime::Kit.component_names.each do |name|
336
+ define_method(name) { |*args, **props, &content| Runtime::Maker.component(name, args, props, content) }
337
+ end
338
+
339
+ # The value: of the checked RadioButton of a group ("default" when no group: was given), or nil.
340
+ def radio_value(group = "default")
341
+ OzdenUI.current_app.objects.each_value do |obj|
342
+ next unless obj.type_name == "RadioButton" && obj.checked?
343
+ return obj.value if (obj.kept_values["group"] || "default").to_s == group.to_s
344
+ end
345
+ nil
346
+ end
347
+
348
+ # Shows a small bubble with a text at an object, for a while (milliseconds; 0: until hint.remove).
349
+ # side: "top" (default), "bottom", "left", "right". A bubble that comes under the mouse by itself is a Tooltip.
350
+ def show_hint(target, text, milliseconds = 2500, side: "top")
351
+ app = OzdenUI.current_app
352
+ app.check("show_hint")
353
+ look = { "color" => "#FFFFFF", "textColor" => "rgba(0, 0, 0, 0.85)", "border" => 1,
354
+ "borderColor" => "rgba(0, 0, 0, 0.12)", "shadow" => "0 10px 30px rgba(0, 0, 0, 0.12)" }
355
+ if app.dark?
356
+ # WHY a frame: the bubble of the kit has no line and no shadow, so it is not seen on a card of its own color
357
+ look = { "color" => "#2A2A29", "textColor" => Runtime::UIObject::DARK_TEXT, "border" => 1,
358
+ "borderColor" => "rgba(255, 255, 255, 0.14)", "shadow" => "0 10px 30px rgba(0, 0, 0, 0.4)" }
359
+ end
360
+ bubble = Runtime::UIObject.new(app, "Part", :part)
361
+ app.objects[bubble.id] = bubble
362
+ app.call_static("Tooltip", "show", [{ "__bpObject" => target.id }, text.to_s, look, side.to_s], keep: bubble.id)
363
+ if milliseconds.positive?
364
+ OzdenUI.after(milliseconds) { bubble.remove if app.objects[bubble.id] }
365
+ end
366
+ bubble
367
+ end
368
+ end
@@ -0,0 +1,212 @@
1
+ # Bismillah
2
+ #
3
+ # dialogs.rb - ozdenui v0.1 (OzdenUI: basic.js for Ruby)
4
+ # Dialogs that return their answer (the window keeps working while the code waits), toasts, and the file dialogs of
5
+ # the system.
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
+ require "open3"
15
+ require "rbconfig"
16
+
17
+ module OzdenUI
18
+ # What the gem is made of. A program uses the methods of OzdenUI; these classes are behind them.
19
+ module Runtime
20
+ # @api private
21
+ module Dialogs
22
+ module_function
23
+
24
+ def show(method, params)
25
+ app = OzdenUI.current_app
26
+ app.check("#{method} dialog")
27
+ look = {}
28
+ Maker.take_window_look(app, Kit["Dialog"], look)
29
+ params["styleName"] = look["styleName"] if look["styleName"]
30
+ # A text that was not given: the text of the kit
31
+ params.delete_if { |key, value| value.nil? || (value == "" && !%w[messageText value].include?(key)) }
32
+ app.call_static("Dialog", method, [params], wait: true)
33
+ end
34
+
35
+ def toast(kind, message, title)
36
+ app = OzdenUI.current_app
37
+ theme = app.theme
38
+ if app.minimal?
39
+ theme = app.options[:accent] ? App::ACCENT_STYLE_NAME : "minimal"
40
+ theme += "Dark" if app.dark?
41
+ end
42
+ params = { "messageText" => message.to_s, "type" => kind.to_s, "theme" => theme }
43
+ params["titleText"] = title.to_s if title
44
+ app.call_static("Toast", "show", [params])
45
+ nil
46
+ end
47
+
48
+ # *** THE FILE DIALOGS OF THE SYSTEM. They return real paths; none when cancelled.
49
+
50
+ def system_name
51
+ host = RbConfig::CONFIG["host_os"]
52
+ return :mac if host =~ /darwin/
53
+ return :windows if host =~ /mswin|mingw|cygwin/
54
+ :linux
55
+ end
56
+
57
+ def file_dialog(kind, title: nil, directory: nil, file_name: nil, types: nil)
58
+ app = OzdenUI.app
59
+ app.flush if app && app.app_thread? # what the program drew before the dialog is on the page
60
+ types = Array(types).map { |ending| ending.to_s.sub(/\A\./, "") }
61
+ command = case system_name
62
+ when :mac then ["osascript", "-e", apple_script(kind, title, directory, file_name, types)]
63
+ when :windows then ["powershell", "-NoProfile", "-STA", "-Command", power_shell(kind, title, directory, file_name, types)]
64
+ else zenity(kind, title, directory, file_name, types)
65
+ end
66
+ output, = Open3.capture2(*command, err: File::NULL)
67
+ output.force_encoding("UTF-8").split("\n").map(&:strip).reject(&:empty?)
68
+ rescue SystemCallError => error
69
+ $stderr.puts "ozdenui: the file dialog of the system could not open: #{error.message}"
70
+ []
71
+ end
72
+
73
+ def quoted(text)
74
+ %("#{text.to_s.gsub('\\') { '\\\\' }.gsub('"') { '\\"' }}")
75
+ end
76
+
77
+ def apple_script(kind, title, directory, file_name, types)
78
+ extra = +""
79
+ extra << " with prompt #{quoted(title)}" if title
80
+ extra << " default location POSIX file #{quoted(File.expand_path(directory))}" if directory
81
+ of_type = types.empty? ? "" : " of type {#{types.map { |ending| quoted(ending) }.join(', ')}}"
82
+ choose = case kind
83
+ when :open then "set chosen to {choose file#{of_type}#{extra}}"
84
+ when :open_many then "set chosen to choose file#{of_type}#{extra} with multiple selections allowed"
85
+ when :save then "set chosen to {choose file name#{extra}#{file_name ? " default name #{quoted(file_name)}" : ''}}"
86
+ else "set chosen to {choose folder#{extra}}"
87
+ end
88
+ # WHY the front application: the dialog belongs to the window of the program, so it opens in front of it
89
+ "tell application (path to frontmost application as text)\n#{choose}\nend tell\n" \
90
+ "set out to \"\"\nrepeat with one in chosen\nset out to out & POSIX path of one & linefeed\nend repeat\nout"
91
+ end
92
+
93
+ def power_shell(kind, title, directory, file_name, types)
94
+ single = ->(text) { "'#{text.to_s.gsub("'", "''")}'" }
95
+ script = +"Add-Type -AssemblyName System.Windows.Forms; [Console]::OutputEncoding = [Text.Encoding]::UTF8; "
96
+ if kind == :folder
97
+ script << "$d = New-Object System.Windows.Forms.FolderBrowserDialog; "
98
+ script << "$d.Description = #{single.call(title)}; " if title
99
+ script << "$d.SelectedPath = #{single.call(directory)}; " if directory
100
+ return script + "if ($d.ShowDialog() -eq 'OK') { $d.SelectedPath }"
101
+ end
102
+ script << "$d = New-Object System.Windows.Forms.#{kind == :save ? 'SaveFileDialog' : 'OpenFileDialog'}; "
103
+ script << "$d.Title = #{single.call(title)}; " if title
104
+ script << "$d.InitialDirectory = #{single.call(directory)}; " if directory
105
+ script << "$d.FileName = #{single.call(file_name)}; " if file_name
106
+ unless types.empty?
107
+ list = types.map { |ending| "*.#{ending}" }.join(";")
108
+ script << "$d.Filter = #{single.call("Files (#{list})|#{list}|All files (*.*)|*.*")}; "
109
+ end
110
+ script << "$d.Multiselect = $true; " if kind == :open_many
111
+ script + "if ($d.ShowDialog() -eq 'OK') { $d.FileNames }"
112
+ end
113
+
114
+ def zenity(kind, title, directory, file_name, types)
115
+ args = ["zenity", "--file-selection"]
116
+ args << "--title=#{title}" if title
117
+ args.push("--multiple", "--separator=\n") if kind == :open_many
118
+ args.push("--save", "--confirm-overwrite") if kind == :save
119
+ args << "--directory" if kind == :folder
120
+ start = directory.to_s
121
+ start += "/" unless start.empty? || start.end_with?("/")
122
+ args << "--filename=#{start}#{file_name}" unless "#{start}#{file_name}".empty?
123
+ args << "--file-filter=#{types.map { |ending| "*.#{ending}" }.join(' ')}" unless types.empty?
124
+ args
125
+ end
126
+ end
127
+ end
128
+
129
+ module_function
130
+
131
+ # Shows a message and waits until it is closed.
132
+ def alert(message, title: nil, confirm_text: nil)
133
+ Runtime::Dialogs.show("alert", "titleText" => title, "messageText" => message.to_s, "buttonText" => confirm_text)
134
+ nil
135
+ end
136
+
137
+ # Asks a yes / no question: delete_all if confirm("Delete the list?", danger: true)
138
+ # danger: the yes button is red (a delete).
139
+ def confirm(message, title: "Are you sure?", confirm_text: nil, cancel_text: nil, danger: false)
140
+ Runtime::Values.on?(Runtime::Dialogs.show("confirm", "titleText" => title, "messageText" => message.to_s, "confirmText" => confirm_text,
141
+ "cancelText" => cancel_text, "danger" => danger ? 1 : 0))
142
+ end
143
+
144
+ # Asks for a text; nil when the dialog was cancelled: name = prompt("The name of the list:")
145
+ # value: the text in the field at the start; field_text: the title of the field; required: an empty answer is not taken.
146
+ def prompt(message, title: nil, value: "", placeholder: nil, field_text: nil, required: false, confirm_text: nil, cancel_text: nil)
147
+ answer = Runtime::Dialogs.show("prompt", "titleText" => title, "messageText" => message.to_s, "value" => value.to_s,
148
+ "placeholder" => placeholder, "fieldText" => field_text, "required" => required ? 1 : 0,
149
+ "confirmText" => confirm_text, "cancelText" => cancel_text)
150
+ answer.nil? ? nil : Runtime::Values.text_of(answer)
151
+ end
152
+
153
+ # Shows a message with buttons of the program and returns the text of the pressed one (nil: closed with Escape or a
154
+ # click outside). The first button is the main one.
155
+ #
156
+ # case ask("Save the changes?", "Save", "Don't save", "Cancel") ...
157
+ def ask(message, *buttons, title: nil)
158
+ app = OzdenUI.current_app
159
+ app.check("ask")
160
+ buttons = ["OK"] if buttons.empty?
161
+ list = buttons.each_with_index.map { |text, index| { "text" => text.to_s, "key" => text.to_s, "type" => index.zero? ? "primary" : "secondary" } }
162
+ pending = Runtime::Answer.new
163
+ props = { message_text: message.to_s, buttons: list, on_close: ->(_dialog, reason, key) { pending.give(reason == "button" ? key : nil) } }
164
+ props[:title_text] = title.to_s if title
165
+ dialog = Runtime::Maker.component("Dialog", [], props, nil)
166
+ # WHY by the id of the dialog: when the page can not make it, its answer is nil (the code would wait for ever)
167
+ app.wait_for_answer(dialog.id, pending)
168
+ end
169
+
170
+ # A small message for a while, that never waits: toast("Saved."), toast("No file.", type: :error, title: "Open")
171
+ # type: :success (default), :error, :warning, :info.
172
+ def toast(message, type: :success, title: nil)
173
+ Runtime::Dialogs.toast(type, message, title)
174
+ end
175
+
176
+ def toast_success(message, title: nil)
177
+ Runtime::Dialogs.toast(:success, message, title)
178
+ end
179
+
180
+ def toast_error(message, title: nil)
181
+ Runtime::Dialogs.toast(:error, message, title)
182
+ end
183
+
184
+ def toast_warning(message, title: nil)
185
+ Runtime::Dialogs.toast(:warning, message, title)
186
+ end
187
+
188
+ def toast_info(message, title: nil)
189
+ Runtime::Dialogs.toast(:info, message, title)
190
+ end
191
+
192
+ # The path of the file the user chose in the file dialog of the system (nil: cancelled).
193
+ # title:, directory: the folder it opens in, types: the endings it shows (%w[png jpg]).
194
+ def open_file(**options)
195
+ Runtime::Dialogs.file_dialog(:open, **options).first
196
+ end
197
+
198
+ # As open_file, with more than one file: a list of paths (empty: cancelled).
199
+ def open_files(**options)
200
+ Runtime::Dialogs.file_dialog(:open_many, **options)
201
+ end
202
+
203
+ # The path to save to (nil: cancelled). The file is not made: the program writes it. file_name: the name it offers.
204
+ def save_file(**options)
205
+ Runtime::Dialogs.file_dialog(:save, **options).first
206
+ end
207
+
208
+ # The path of the folder the user chose (nil: cancelled).
209
+ def choose_folder(**options)
210
+ Runtime::Dialogs.file_dialog(:folder, **options).first
211
+ end
212
+ end