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.
- checksums.yaml +7 -0
- data/LICENSE +14 -0
- data/NOTICE +5 -0
- data/README.md +242 -0
- data/exe/ozdenui +17 -0
- data/lib/ozdenui/AGENTS.md +210 -0
- data/lib/ozdenui/COMPONENTS.md +3017 -0
- data/lib/ozdenui/app.rb +749 -0
- data/lib/ozdenui/command.rb +107 -0
- data/lib/ozdenui/components.rb +368 -0
- data/lib/ozdenui/dialogs.rb +212 -0
- data/lib/ozdenui/files.rb +124 -0
- data/lib/ozdenui/icon.png +0 -0
- data/lib/ozdenui/kit.json +1 -0
- data/lib/ozdenui/kit.rb +115 -0
- data/lib/ozdenui/look.rb +229 -0
- data/lib/ozdenui/objects.rb +652 -0
- data/lib/ozdenui/server.rb +270 -0
- data/lib/ozdenui/values.rb +273 -0
- data/lib/ozdenui/version.rb +15 -0
- data/lib/ozdenui/web/basic/LICENSE +14 -0
- data/lib/ozdenui/web/basic/basic.min.css +1 -0
- data/lib/ozdenui/web/basic/basic.min.js +78 -0
- data/lib/ozdenui/web/basic/font/open-sans/LICENSE.txt +206 -0
- data/lib/ozdenui/web/basic/font/open-sans/OpenSans-Bold.ttf +0 -0
- data/lib/ozdenui/web/basic/font/open-sans/OpenSans-Regular.ttf +0 -0
- data/lib/ozdenui/web/basic/scroll-bar.min.js +3 -0
- data/lib/ozdenui/web/bridge.js +584 -0
- data/lib/ozdenui/web/comp/accordion.js +659 -0
- data/lib/ozdenui/web/comp/action-button/add.png +0 -0
- data/lib/ozdenui/web/comp/action-button.js +521 -0
- data/lib/ozdenui/web/comp/action-tab-bar.js +383 -0
- data/lib/ozdenui/web/comp/app-bar.js +237 -0
- data/lib/ozdenui/web/comp/badge.js +292 -0
- data/lib/ozdenui/web/comp/bottom-bar.js +555 -0
- data/lib/ozdenui/web/comp/breadcrumbs.js +524 -0
- data/lib/ozdenui/web/comp/canvas.js +652 -0
- data/lib/ozdenui/web/comp/chat-view.js +827 -0
- data/lib/ozdenui/web/comp/check-box.js +490 -0
- data/lib/ozdenui/web/comp/code-editor.js +965 -0
- data/lib/ozdenui/web/comp/color-picker.js +636 -0
- data/lib/ozdenui/web/comp/context-menu.js +951 -0
- data/lib/ozdenui/web/comp/currency-field.js +209 -0
- data/lib/ozdenui/web/comp/data-table/arrow.svg +2 -0
- data/lib/ozdenui/web/comp/data-table.js +937 -0
- data/lib/ozdenui/web/comp/date-picker.js +1055 -0
- data/lib/ozdenui/web/comp/dialog.js +992 -0
- data/lib/ozdenui/web/comp/email-field.js +65 -0
- data/lib/ozdenui/web/comp/file-picker.js +920 -0
- data/lib/ozdenui/web/comp/filters.js +807 -0
- data/lib/ozdenui/web/comp/form.js +602 -0
- data/lib/ozdenui/web/comp/full-screen-select/arrow.svg +2 -0
- data/lib/ozdenui/web/comp/full-screen-select/cancel.svg +2 -0
- data/lib/ozdenui/web/comp/full-screen-select.js +614 -0
- data/lib/ozdenui/web/comp/gauge.js +439 -0
- data/lib/ozdenui/web/comp/graph-box.js +3911 -0
- data/lib/ozdenui/web/comp/grid-group.js +185 -0
- data/lib/ozdenui/web/comp/hold-to-confirm-button/trash-red.png +0 -0
- data/lib/ozdenui/web/comp/hold-to-confirm-button/trash.png +0 -0
- data/lib/ozdenui/web/comp/hold-to-confirm-button.js +289 -0
- data/lib/ozdenui/web/comp/image-view.js +598 -0
- data/lib/ozdenui/web/comp/item-list.js +1740 -0
- data/lib/ozdenui/web/comp/json-editor.js +3221 -0
- data/lib/ozdenui/web/comp/json-view.js +597 -0
- data/lib/ozdenui/web/comp/label-editor.js +1140 -0
- data/lib/ozdenui/web/comp/left-menu/data-table.png +0 -0
- data/lib/ozdenui/web/comp/left-menu/home.png +0 -0
- data/lib/ozdenui/web/comp/left-menu/reports.png +0 -0
- data/lib/ozdenui/web/comp/left-menu/setting.png +0 -0
- data/lib/ozdenui/web/comp/left-menu.js +548 -0
- data/lib/ozdenui/web/comp/line-progress-bar.js +343 -0
- data/lib/ozdenui/web/comp/loading-screen.js +413 -0
- data/lib/ozdenui/web/comp/log-view.js +530 -0
- data/lib/ozdenui/web/comp/login-page.js +685 -0
- data/lib/ozdenui/web/comp/markdown-view.js +1045 -0
- data/lib/ozdenui/web/comp/menu-bar.js +624 -0
- data/lib/ozdenui/web/comp/mini-graph-box.js +492 -0
- data/lib/ozdenui/web/comp/minimal-accent.js +208 -0
- data/lib/ozdenui/web/comp/notice.js +229 -0
- data/lib/ozdenui/web/comp/number-field.js +128 -0
- data/lib/ozdenui/web/comp/numeric-keyboard.js +298 -0
- data/lib/ozdenui/web/comp/page-control.js +762 -0
- data/lib/ozdenui/web/comp/page-header.js +191 -0
- data/lib/ozdenui/web/comp/password-field/hide-btn.png +0 -0
- data/lib/ozdenui/web/comp/password-field/hide-btn2.png +0 -0
- data/lib/ozdenui/web/comp/password-field/show-btn.png +0 -0
- data/lib/ozdenui/web/comp/password-field/show-btn2.png +0 -0
- data/lib/ozdenui/web/comp/password-field.js +192 -0
- data/lib/ozdenui/web/comp/period-bar.js +436 -0
- data/lib/ozdenui/web/comp/phone-field.js +322 -0
- data/lib/ozdenui/web/comp/progress-bar.js +992 -0
- data/lib/ozdenui/web/comp/property-panel.js +3639 -0
- data/lib/ozdenui/web/comp/radio-button.js +527 -0
- data/lib/ozdenui/web/comp/range-slider.js +569 -0
- data/lib/ozdenui/web/comp/rich-text-editor.js +1064 -0
- data/lib/ozdenui/web/comp/scroll-area.js +308 -0
- data/lib/ozdenui/web/comp/scrolling-text.js +297 -0
- data/lib/ozdenui/web/comp/search-input/clear-round.svg +1 -0
- data/lib/ozdenui/web/comp/search-input/clear.svg +1 -0
- data/lib/ozdenui/web/comp/search-input/filter.png +0 -0
- data/lib/ozdenui/web/comp/search-input/filter.svg +1 -0
- data/lib/ozdenui/web/comp/search-input/search.svg +1 -0
- data/lib/ozdenui/web/comp/search-input.js +725 -0
- data/lib/ozdenui/web/comp/search-results.js +596 -0
- data/lib/ozdenui/web/comp/select-box.js +1862 -0
- data/lib/ozdenui/web/comp/settings-row.js +299 -0
- data/lib/ozdenui/web/comp/side-panel.js +616 -0
- data/lib/ozdenui/web/comp/slider-field.js +952 -0
- data/lib/ozdenui/web/comp/slider.js +838 -0
- data/lib/ozdenui/web/comp/smart-button/alert-black.png +0 -0
- data/lib/ozdenui/web/comp/smart-button/clock.png +0 -0
- data/lib/ozdenui/web/comp/smart-button.js +981 -0
- data/lib/ozdenui/web/comp/smart-table/clock.png +0 -0
- data/lib/ozdenui/web/comp/smart-table/down.png +0 -0
- data/lib/ozdenui/web/comp/smart-table/scroll.png +0 -0
- data/lib/ozdenui/web/comp/smart-table/sort.png +0 -0
- data/lib/ozdenui/web/comp/smart-table/up.png +0 -0
- data/lib/ozdenui/web/comp/smart-table.js +1883 -0
- data/lib/ozdenui/web/comp/sortable-list.js +1181 -0
- data/lib/ozdenui/web/comp/spark-line-box.js +738 -0
- data/lib/ozdenui/web/comp/splitter.js +411 -0
- data/lib/ozdenui/web/comp/status-bar.js +408 -0
- data/lib/ozdenui/web/comp/step-bar.js +345 -0
- data/lib/ozdenui/web/comp/stepper.js +753 -0
- data/lib/ozdenui/web/comp/tabs.js +1366 -0
- data/lib/ozdenui/web/comp/tag-input.js +1107 -0
- data/lib/ozdenui/web/comp/tap-effect.js +170 -0
- data/lib/ozdenui/web/comp/text-area-field.js +233 -0
- data/lib/ozdenui/web/comp/text-field.js +1006 -0
- data/lib/ozdenui/web/comp/time-line.js +398 -0
- data/lib/ozdenui/web/comp/time-picker.js +1032 -0
- data/lib/ozdenui/web/comp/tiny-select/arrow.svg +1 -0
- data/lib/ozdenui/web/comp/tiny-select.js +322 -0
- data/lib/ozdenui/web/comp/tiny-table.js +283 -0
- data/lib/ozdenui/web/comp/toast.js +891 -0
- data/lib/ozdenui/web/comp/toggle.js +757 -0
- data/lib/ozdenui/web/comp/tool-bar.js +760 -0
- data/lib/ozdenui/web/comp/tooltip.js +534 -0
- data/lib/ozdenui/web/comp/tree-view.js +1179 -0
- data/lib/ozdenui/web/comp/ui-effects.js +1097 -0
- data/lib/ozdenui/web/comp/ui-standards.js +403 -0
- data/lib/ozdenui/web/comp/url-field.js +190 -0
- data/lib/ozdenui/web/comp/web-view.js +588 -0
- data/lib/ozdenui/web/components.js +123 -0
- data/lib/ozdenui/web/index.html +39 -0
- data/lib/ozdenui/window.rb +188 -0
- data/lib/ozdenui/window_cocoa.rb +221 -0
- data/lib/ozdenui.rb +32 -0
- metadata +200 -0
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
# Bismillah
|
|
2
|
+
#
|
|
3
|
+
# server.rb - ozdenui v0.1 (OzdenUI: basic.js for Ruby)
|
|
4
|
+
# The page of the window and its messages, served on this computer only (127.0.0.1, a port of its own).
|
|
5
|
+
#
|
|
6
|
+
# The program sends its messages over an event stream ("commands"); the page answers and tells its events with POST
|
|
7
|
+
# "emit", one request at a time, so they come in the order they happened (web/bridge.js). Only the socket library of
|
|
8
|
+
# Ruby is used: the gem needs no other gem.
|
|
9
|
+
#
|
|
10
|
+
# Started Date: November 2026
|
|
11
|
+
# Webpage: https://bug7a.github.io/ozdenui-ruby/
|
|
12
|
+
#
|
|
13
|
+
# Copyright 2026 Bugra Ozden <bugra.ozden@gmail.com> (https://bug7a.github.io/)
|
|
14
|
+
# Licensed under the Apache License, Version 2.0
|
|
15
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
16
|
+
|
|
17
|
+
require "socket"
|
|
18
|
+
require "securerandom"
|
|
19
|
+
require "json"
|
|
20
|
+
require "cgi"
|
|
21
|
+
|
|
22
|
+
module OzdenUI
|
|
23
|
+
# What the gem is made of. A program uses the methods of OzdenUI; these classes are behind them.
|
|
24
|
+
module Runtime
|
|
25
|
+
# @api private
|
|
26
|
+
class PageGone < StandardError; end
|
|
27
|
+
|
|
28
|
+
# A queue with a pop that gives up after a time (Queue#pop takes a timeout only since Ruby 3.2).
|
|
29
|
+
# @api private
|
|
30
|
+
class Channel
|
|
31
|
+
def initialize
|
|
32
|
+
@items = []
|
|
33
|
+
@mutex = Mutex.new
|
|
34
|
+
@condition = ConditionVariable.new
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def push(item)
|
|
38
|
+
@mutex.synchronize do
|
|
39
|
+
@items << item
|
|
40
|
+
@condition.signal
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def pop(seconds)
|
|
45
|
+
@mutex.synchronize do
|
|
46
|
+
@condition.wait(@mutex, seconds) if @items.empty?
|
|
47
|
+
@items.shift
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# @api private
|
|
53
|
+
class Server
|
|
54
|
+
WEB_FOLDER = File.join(__dir__, "web")
|
|
55
|
+
STREAM_CHECK_SECONDS = 0.25 # this often a silent stream looks whether its page is still there
|
|
56
|
+
|
|
57
|
+
attr_accessor :on_stream # the window of a browser is known to be closed by its stream (window.rb)
|
|
58
|
+
|
|
59
|
+
def initialize(app)
|
|
60
|
+
@app = app
|
|
61
|
+
@listener = TCPServer.new("127.0.0.1", 0)
|
|
62
|
+
# WHY: another program or page of this computer must not reach the page of the window
|
|
63
|
+
@token = SecureRandom.hex(16)
|
|
64
|
+
@prefix = "/#{@token}/"
|
|
65
|
+
@mutex = Mutex.new
|
|
66
|
+
@emit_mutex = Mutex.new # one batch of the page at a time, in order
|
|
67
|
+
@page_id = nil # the page that listens now (a page loaded again is a new one)
|
|
68
|
+
@outgoing = Channel.new # the messages for it
|
|
69
|
+
@waiting = {} # the answers the program waits for
|
|
70
|
+
@next_reply = 0
|
|
71
|
+
@stopped = false
|
|
72
|
+
@thread = Thread.new { accept_connections }
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def address
|
|
76
|
+
"http://127.0.0.1:#{@listener.addr[1]}#{@prefix}index.html"
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def stop
|
|
80
|
+
@mutex.synchronize { @stopped = true }
|
|
81
|
+
fail_waiting
|
|
82
|
+
@listener.close
|
|
83
|
+
rescue IOError
|
|
84
|
+
nil
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
# Sends a message to the page and waits for its answer (the value the page gave).
|
|
88
|
+
def request(message)
|
|
89
|
+
waiter = Queue.new
|
|
90
|
+
number = nil
|
|
91
|
+
outgoing = nil
|
|
92
|
+
@mutex.synchronize do
|
|
93
|
+
raise PageGone, "the page is gone" if @stopped
|
|
94
|
+
number = (@next_reply += 1)
|
|
95
|
+
@waiting[number] = waiter
|
|
96
|
+
outgoing = @outgoing
|
|
97
|
+
end
|
|
98
|
+
begin
|
|
99
|
+
text = JSON.generate(message.merge("n" => number))
|
|
100
|
+
rescue StandardError => error
|
|
101
|
+
@mutex.synchronize { @waiting.delete(number) }
|
|
102
|
+
raise Error, "a value can not be sent to the page: #{error.message}"
|
|
103
|
+
end
|
|
104
|
+
outgoing.push(text)
|
|
105
|
+
reply = waiter.pop
|
|
106
|
+
raise reply if reply.is_a?(Exception)
|
|
107
|
+
reply["value"]
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
private
|
|
111
|
+
|
|
112
|
+
def accept_connections
|
|
113
|
+
loop do
|
|
114
|
+
socket = @listener.accept
|
|
115
|
+
Thread.new(socket) { |connection| serve(connection) }
|
|
116
|
+
end
|
|
117
|
+
rescue IOError, SystemCallError
|
|
118
|
+
nil # the server was stopped
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
def serve(socket)
|
|
122
|
+
socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
|
|
123
|
+
loop do
|
|
124
|
+
line = socket.gets("\r\n")
|
|
125
|
+
break unless line
|
|
126
|
+
method, target, = line.split(" ")
|
|
127
|
+
headers = {}
|
|
128
|
+
while (header = socket.gets("\r\n")) && header != "\r\n"
|
|
129
|
+
name, value = header.split(":", 2)
|
|
130
|
+
headers[name.downcase] = value.to_s.strip
|
|
131
|
+
end
|
|
132
|
+
length = headers["content-length"].to_i
|
|
133
|
+
body = length.positive? ? socket.read(length) : ""
|
|
134
|
+
path, query = target.to_s.split("?", 2)
|
|
135
|
+
unless path.start_with?(@prefix)
|
|
136
|
+
respond(socket, 404, "text/plain", "not found")
|
|
137
|
+
next
|
|
138
|
+
end
|
|
139
|
+
name = path[@prefix.length..-1]
|
|
140
|
+
case name
|
|
141
|
+
when "commands"
|
|
142
|
+
serve_commands(socket, query.to_s)
|
|
143
|
+
break
|
|
144
|
+
when "emit" then serve_emit(socket, method, body)
|
|
145
|
+
when "index.html" then serve_index(socket)
|
|
146
|
+
else serve_file(socket, name)
|
|
147
|
+
end
|
|
148
|
+
end
|
|
149
|
+
rescue IOError, SystemCallError
|
|
150
|
+
nil # the page closed the link
|
|
151
|
+
ensure
|
|
152
|
+
socket.close unless socket.closed?
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
def respond(socket, status, kind, body)
|
|
156
|
+
reason = { 200 => "OK", 204 => "No Content", 400 => "Bad Request", 404 => "Not Found" }[status]
|
|
157
|
+
head = "HTTP/1.1 #{status} #{reason}\r\nContent-Length: #{body.bytesize}\r\n"
|
|
158
|
+
head += "Content-Type: #{kind}\r\n" unless body.empty?
|
|
159
|
+
# WHY no-store: a web view keeps a file of an older version of the gem under the same address of another run
|
|
160
|
+
socket.write(head + "Cache-Control: no-store\r\nConnection: keep-alive\r\n\r\n", body)
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
def escape_html(text)
|
|
164
|
+
CGI.escapeHTML(text.to_s)
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
# The page, with the title of the window (a browser window shows the title of its page).
|
|
168
|
+
def serve_index(socket)
|
|
169
|
+
page = File.read(File.join(WEB_FOLDER, "index.html"), encoding: "UTF-8")
|
|
170
|
+
options = @app.options
|
|
171
|
+
title = "<title>#{escape_html(options[:title])}</title>"
|
|
172
|
+
# The size of the page the program asked for: a browser window fits itself to it before the program starts
|
|
173
|
+
size = %(<meta name="ozdenui-size" content="#{options[:width]}x#{options[:height]}">)
|
|
174
|
+
page = page.sub("<title>ozdenui</title>", "#{title}\n #{size}")
|
|
175
|
+
page = page.sub('<html lang="en">', %(<html lang="#{escape_html(options[:language])}">)) if options[:language]
|
|
176
|
+
respond(socket, 200, "text/html; charset=utf-8", page.b)
|
|
177
|
+
end
|
|
178
|
+
|
|
179
|
+
def serve_file(socket, name)
|
|
180
|
+
relative = CGI.unescape(name.gsub("+", "%2B"))
|
|
181
|
+
path = File.expand_path(relative, WEB_FOLDER)
|
|
182
|
+
is_inside = path.start_with?(WEB_FOLDER + File::SEPARATOR)
|
|
183
|
+
unless is_inside && File.file?(path)
|
|
184
|
+
respond(socket, 404, "text/plain", "not found")
|
|
185
|
+
return
|
|
186
|
+
end
|
|
187
|
+
kind = Values::MIME_TYPES[File.extname(path).downcase] || "application/octet-stream"
|
|
188
|
+
respond(socket, 200, kind, File.binread(path))
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
# The event stream of the page: every message of the program is one "data:" line.
|
|
192
|
+
def serve_commands(socket, query)
|
|
193
|
+
page_id = CGI.parse(query)["page"].first.to_s
|
|
194
|
+
socket.write("HTTP/1.1 200 OK\r\nContent-Type: text/event-stream\r\nCache-Control: no-store\r\nConnection: keep-alive\r\n\r\n")
|
|
195
|
+
outgoing = @mutex.synchronize { channel_of(page_id) }
|
|
196
|
+
@on_stream&.call(true)
|
|
197
|
+
begin
|
|
198
|
+
loop do
|
|
199
|
+
message = outgoing.pop(STREAM_CHECK_SECONDS)
|
|
200
|
+
if message
|
|
201
|
+
socket.write("data: #{message}\n\n")
|
|
202
|
+
elsif page_left?(socket)
|
|
203
|
+
break
|
|
204
|
+
end
|
|
205
|
+
end
|
|
206
|
+
ensure
|
|
207
|
+
@on_stream&.call(false)
|
|
208
|
+
end
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
# WHY: a stream that has nothing to send does not see that its page was closed.
|
|
212
|
+
def page_left?(socket)
|
|
213
|
+
return false unless IO.select([socket], nil, nil, 0)
|
|
214
|
+
socket.read_nonblock(1024)
|
|
215
|
+
false
|
|
216
|
+
rescue IO::WaitReadable
|
|
217
|
+
false
|
|
218
|
+
rescue EOFError, SystemCallError
|
|
219
|
+
true
|
|
220
|
+
end
|
|
221
|
+
|
|
222
|
+
def serve_emit(socket, method, body)
|
|
223
|
+
batch = method == "POST" ? JSON.parse(body, object_class: Data) : nil
|
|
224
|
+
unless batch.is_a?(Array)
|
|
225
|
+
respond(socket, 400, "text/plain", "bad request")
|
|
226
|
+
return
|
|
227
|
+
end
|
|
228
|
+
@emit_mutex.synchronize { batch.each { |message| take(message) } }
|
|
229
|
+
respond(socket, 204, "", "")
|
|
230
|
+
rescue JSON::ParserError
|
|
231
|
+
respond(socket, 400, "text/plain", "bad request")
|
|
232
|
+
end
|
|
233
|
+
|
|
234
|
+
def take(message)
|
|
235
|
+
if message["ready"]
|
|
236
|
+
# A new page: what waited for the old one gets no answer, and its messages are not for the new one.
|
|
237
|
+
@mutex.synchronize { channel_of(message["ready"]["page"].to_s) }
|
|
238
|
+
fail_waiting
|
|
239
|
+
@app.on_page_ready(message["ready"])
|
|
240
|
+
elsif message["reply"]
|
|
241
|
+
waiter = @mutex.synchronize { @waiting.delete(message["reply"]) }
|
|
242
|
+
return unless waiter
|
|
243
|
+
waiter.push(message["error"] ? Error.new(message["error"].to_s) : message)
|
|
244
|
+
else
|
|
245
|
+
@app.on_page_event(message["target"].to_s, message["event"].to_s, message["data"])
|
|
246
|
+
end
|
|
247
|
+
end
|
|
248
|
+
|
|
249
|
+
# The messages of a page, by its name (the lock is held). WHY by its name: the stream of a page may open before or
|
|
250
|
+
# after the server took its "ready"; a channel made new at "ready" was sometimes not the one its stream listened
|
|
251
|
+
# to, and the program waited for ever for an answer.
|
|
252
|
+
def channel_of(page_id)
|
|
253
|
+
if page_id != @page_id
|
|
254
|
+
@page_id = page_id
|
|
255
|
+
@outgoing = Channel.new
|
|
256
|
+
end
|
|
257
|
+
@outgoing
|
|
258
|
+
end
|
|
259
|
+
|
|
260
|
+
def fail_waiting
|
|
261
|
+
waiting = @mutex.synchronize do
|
|
262
|
+
taken = @waiting
|
|
263
|
+
@waiting = {}
|
|
264
|
+
taken
|
|
265
|
+
end
|
|
266
|
+
waiting.each_value { |waiter| waiter.push(PageGone.new("the page is gone")) }
|
|
267
|
+
end
|
|
268
|
+
end
|
|
269
|
+
end
|
|
270
|
+
end
|
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
# Bismillah
|
|
2
|
+
#
|
|
3
|
+
# values.rb - ozdenui v0.1 (OzdenUI: basic.js for Ruby)
|
|
4
|
+
# Names (label_text <-> labelText), the values that go to the page and come from it, the blocks the page calls,
|
|
5
|
+
# the text of a number (a format) and pictures.
|
|
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 "json"
|
|
15
|
+
require "time"
|
|
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
|
+
# What a program did wrong, told at its own line: a wrong value, an object that can not hold others...
|
|
21
|
+
class Error < StandardError; end
|
|
22
|
+
|
|
23
|
+
# A table that came from the page (a row, an item, the detail of an event): a Hash with the keys of the page as
|
|
24
|
+
# texts, read with a text or a symbol: row["name"], row[:name].
|
|
25
|
+
class Data < Hash
|
|
26
|
+
def [](key)
|
|
27
|
+
super(key.is_a?(Symbol) ? key.to_s : key)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def fetch(key, *rest, &block)
|
|
31
|
+
super(key.is_a?(Symbol) ? key.to_s : key, *rest, &block)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def key?(key)
|
|
35
|
+
super(key.is_a?(Symbol) ? key.to_s : key)
|
|
36
|
+
end
|
|
37
|
+
alias has_key? key?
|
|
38
|
+
alias include? key?
|
|
39
|
+
|
|
40
|
+
def dig(key, *rest)
|
|
41
|
+
value = self[key]
|
|
42
|
+
return value if rest.empty? || value.nil?
|
|
43
|
+
value.dig(*rest)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def values_at(*keys)
|
|
47
|
+
keys.map { |key| self[key] }
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# What the page tells of an event of an object: event.key ("Enter"), event.x, event.text (an Input)...
|
|
52
|
+
Event = Struct.new(:name, :key, :code, :shift, :ctrl, :alt, :meta, :x, :y, :text, :detail)
|
|
53
|
+
|
|
54
|
+
# The text of a number, made by the page at once (OzdenUI.number_format).
|
|
55
|
+
NumberFormat = Struct.new(:prefix, :suffix, :thousands, :decimals, :percent)
|
|
56
|
+
|
|
57
|
+
# @api private
|
|
58
|
+
module Values
|
|
59
|
+
LIB_FOLDER = File.expand_path("..", __dir__) + File::SEPARATOR
|
|
60
|
+
FORMAT_PATTERN = /\A(.*?)\{:?(,?)(?:\.(\d+))?([fd%]?)\}(.*)\z/m.freeze
|
|
61
|
+
PICTURE_ENDING = /\.(png|jpe?g|gif|webp|bmp|svg)\z/i.freeze
|
|
62
|
+
SVG_START = /\A\s*(<\?xml.*?\?>\s*)?(<!--.*?-->\s*)*<svg[\s>]/m.freeze
|
|
63
|
+
ADDRESS_PATTERN = /\A(https?:|data:|blob:|file:)/.freeze
|
|
64
|
+
MIME_TYPES = {
|
|
65
|
+
".html" => "text/html; charset=utf-8", ".htm" => "text/html; charset=utf-8", ".js" => "text/javascript; charset=utf-8",
|
|
66
|
+
".css" => "text/css; charset=utf-8", ".json" => "application/json", ".png" => "image/png", ".jpg" => "image/jpeg",
|
|
67
|
+
".jpeg" => "image/jpeg", ".gif" => "image/gif", ".webp" => "image/webp", ".svg" => "image/svg+xml",
|
|
68
|
+
".bmp" => "image/bmp", ".ico" => "image/x-icon", ".ttf" => "font/ttf", ".woff" => "font/woff",
|
|
69
|
+
".woff2" => "font/woff2", ".txt" => "text/plain; charset=utf-8", ".md" => "text/markdown; charset=utf-8",
|
|
70
|
+
".csv" => "text/csv", ".pdf" => "application/pdf", ".mp3" => "audio/mpeg", ".wav" => "audio/wav",
|
|
71
|
+
".mp4" => "video/mp4", ".zip" => "application/zip"
|
|
72
|
+
}.freeze
|
|
73
|
+
|
|
74
|
+
module_function
|
|
75
|
+
|
|
76
|
+
# *** NAMES: label_text (Ruby) <-> labelText (the kit)
|
|
77
|
+
|
|
78
|
+
# A symbol is a Ruby name (:label_text -> "labelText"); a text is a name of the kit as it is ("tab_border").
|
|
79
|
+
def kit_name(name)
|
|
80
|
+
return name if name.is_a?(String)
|
|
81
|
+
text = name.to_s
|
|
82
|
+
text.include?("_") ? text.gsub(/_([a-z0-9])/) { Regexp.last_match(1).upcase } : text
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def ruby_name(name)
|
|
86
|
+
name.to_s.gsub(/([a-z0-9])([A-Z])/) { "#{Regexp.last_match(1)}_#{Regexp.last_match(2).downcase}" }
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
# "onClick" -> "click", "onKeyDown" -> "keydown", :key_down -> "keydown"
|
|
90
|
+
def event_name(name)
|
|
91
|
+
name.to_s.sub(/\Aon(?=[A-Z_])/, "").delete("_").downcase
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def callback_name?(name)
|
|
95
|
+
name.match?(/\Aon[A-Z]/)
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
# style, look, buttonStyle, searchParams...: a param of looks, whose keys are names of the kit.
|
|
99
|
+
def style_name?(name)
|
|
100
|
+
name == "style" || name == "look" || name == "css" || name == "aria" || name.end_with?("Style", "Params")
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
# A param that names a picture file: iconFile, imageFile, checkIconFile...
|
|
104
|
+
def file_param?(name)
|
|
105
|
+
name.end_with?("File")
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def closest_name(name, known)
|
|
109
|
+
best = nil
|
|
110
|
+
best_distance = 3
|
|
111
|
+
lower = name.downcase
|
|
112
|
+
known.each do |candidate|
|
|
113
|
+
distance = edit_distance(lower, candidate.downcase)
|
|
114
|
+
if distance < best_distance || (distance == best_distance && best && candidate < best)
|
|
115
|
+
best = candidate
|
|
116
|
+
best_distance = distance
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
best
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
def edit_distance(first, second)
|
|
123
|
+
previous = (0..second.length).to_a
|
|
124
|
+
(1..first.length).each do |i|
|
|
125
|
+
row = [i]
|
|
126
|
+
(1..second.length).each do |j|
|
|
127
|
+
cost = first[i - 1] == second[j - 1] ? 0 : 1
|
|
128
|
+
row << [previous[j] + 1, row[j - 1] + 1, previous[j - 1] + cost].min
|
|
129
|
+
end
|
|
130
|
+
previous = row
|
|
131
|
+
end
|
|
132
|
+
previous[second.length]
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
# *** THE LINE OF THE PROGRAM: "app.rb:12", the first line of the stack that is not of this gem.
|
|
136
|
+
|
|
137
|
+
def source_line
|
|
138
|
+
place = caller_locations(1, 40).find { |location| program_line?(location) }
|
|
139
|
+
place ? "#{File.basename(place.path)}:#{place.lineno}" : ""
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
def program_line?(location)
|
|
143
|
+
path = location.absolute_path || location.path.to_s
|
|
144
|
+
!path.start_with?(LIB_FOLDER) && !path.start_with?("<internal:")
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
# The lines of the program in the trace of a failed task.
|
|
148
|
+
def program_trace(error)
|
|
149
|
+
(error.backtrace || []).reject { |line| line.start_with?(LIB_FOLDER) || line.start_with?("<internal:") }
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
# *** READING VALUES:
|
|
153
|
+
|
|
154
|
+
# 1, true and a text that is not empty are on (the flags of basic.js are 1 / 0).
|
|
155
|
+
def on?(value)
|
|
156
|
+
case value
|
|
157
|
+
when nil, false then false
|
|
158
|
+
when true then true
|
|
159
|
+
when String then !["", "0", "false"].include?(value)
|
|
160
|
+
when Numeric then !value.zero?
|
|
161
|
+
else true
|
|
162
|
+
end
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
def number_of(value)
|
|
166
|
+
case value
|
|
167
|
+
when Numeric then value
|
|
168
|
+
when true then 1
|
|
169
|
+
when String then Float(value.strip, exception: false) || 0
|
|
170
|
+
else 0
|
|
171
|
+
end
|
|
172
|
+
rescue ArgumentError, TypeError
|
|
173
|
+
0 # WHY: Float(text, exception: false) is of Ruby 2.6; an older one raises
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
def text_of(value)
|
|
177
|
+
return value.to_i.to_s if value.is_a?(Float) && value.finite? && value == value.to_i
|
|
178
|
+
value.to_s
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
# true / false -> 1 / 0, as basic.js takes its flags (a Toggle compares value === 1).
|
|
182
|
+
def flag_as_number(value)
|
|
183
|
+
return 1 if value == true
|
|
184
|
+
return 0 if value == false
|
|
185
|
+
value
|
|
186
|
+
end
|
|
187
|
+
|
|
188
|
+
# *** BLOCKS OF THE PROGRAM, CALLED BY THE PAGE:
|
|
189
|
+
|
|
190
|
+
def callable?(value)
|
|
191
|
+
value.is_a?(Proc) || value.is_a?(Method)
|
|
192
|
+
end
|
|
193
|
+
|
|
194
|
+
def check_handler(handler, where)
|
|
195
|
+
return if handler.nil? || callable?(handler)
|
|
196
|
+
raise Error, "#{where} takes a block: a lambda (-> { ... }, ->(obj) { ... }) or a method (method(:save)), not #{handler.inspect}"
|
|
197
|
+
end
|
|
198
|
+
|
|
199
|
+
# Calls a block of the program with the values of the page. The block takes what it needs: -> { }, ->(obj) { },
|
|
200
|
+
# ->(obj, value) { }: a lambda and a method get as many values as they ask for.
|
|
201
|
+
def call_handler(handler, args)
|
|
202
|
+
return unless handler
|
|
203
|
+
strict = handler.is_a?(Method) || handler.lambda?
|
|
204
|
+
return handler.call(*args) unless strict
|
|
205
|
+
kinds = handler.parameters.map(&:first)
|
|
206
|
+
return handler.call(*args) if kinds.include?(:rest)
|
|
207
|
+
required = kinds.count(:req)
|
|
208
|
+
count = [[args.length, required + kinds.count(:opt)].min, required].max
|
|
209
|
+
given = args.first(count)
|
|
210
|
+
given << nil while given.length < count
|
|
211
|
+
handler.call(*given)
|
|
212
|
+
end
|
|
213
|
+
|
|
214
|
+
# *** THE TEXT OF A NUMBER, made by the page at once: "{:,.2f} TL"
|
|
215
|
+
|
|
216
|
+
def number_format(pattern)
|
|
217
|
+
parts = FORMAT_PATTERN.match(pattern.to_s)
|
|
218
|
+
unless parts
|
|
219
|
+
raise Error, "#{pattern.inspect} is not a format: a text around one place, Ex: \"{:,.2f} TL\", \"{:.0f} kg\", \"{:.1%}\""
|
|
220
|
+
end
|
|
221
|
+
decimals = nil
|
|
222
|
+
decimals = parts[3].to_i if parts[3]
|
|
223
|
+
decimals = 0 if parts[3].nil? && parts[4] == "d"
|
|
224
|
+
NumberFormat.new(parts[1], parts[5], parts[2] == "," ? 1 : 0, decimals, parts[4] == "%" ? 1 : 0)
|
|
225
|
+
end
|
|
226
|
+
|
|
227
|
+
# *** PICTURES:
|
|
228
|
+
|
|
229
|
+
def data_address(content, kind)
|
|
230
|
+
"data:#{kind};base64,#{[content].pack('m0')}"
|
|
231
|
+
end
|
|
232
|
+
|
|
233
|
+
def kind_of_bytes(content)
|
|
234
|
+
head = content.byteslice(0, 16).to_s.b
|
|
235
|
+
return "image/png" if head.start_with?("\x89PNG".b)
|
|
236
|
+
return "image/jpeg" if head.start_with?("\xFF\xD8".b)
|
|
237
|
+
return "image/gif" if head.start_with?("GIF8".b)
|
|
238
|
+
return "image/webp" if head.start_with?("RIFF".b) && head.byteslice(8, 4) == "WEBP".b
|
|
239
|
+
return "image/bmp" if head.start_with?("BM".b)
|
|
240
|
+
"application/octet-stream"
|
|
241
|
+
end
|
|
242
|
+
|
|
243
|
+
def svg_text?(text)
|
|
244
|
+
SVG_START.match?(text.byteslice(0, 2000).to_s.scrub)
|
|
245
|
+
end
|
|
246
|
+
|
|
247
|
+
def bytes?(text)
|
|
248
|
+
text.encoding == Encoding::BINARY || !text.valid_encoding? || text.include?("\0")
|
|
249
|
+
end
|
|
250
|
+
|
|
251
|
+
def picture(source)
|
|
252
|
+
raise Error, "picture() takes a path, the text of an SVG or bytes (File.binread), not #{source.class}" unless source.is_a?(String)
|
|
253
|
+
return data_address(source, "image/svg+xml") if svg_text?(source)
|
|
254
|
+
return data_address(source, kind_of_bytes(source)) if bytes?(source)
|
|
255
|
+
picture_of_path(source)
|
|
256
|
+
end
|
|
257
|
+
|
|
258
|
+
# The address of a file of this computer; a text that is not a file stays as it is (an address, an icon of the kit).
|
|
259
|
+
# WHY a data address: the page comes from the program, not from the disk, so it can not read a file by its path.
|
|
260
|
+
def picture_of_path(path)
|
|
261
|
+
return path if path.empty? || ADDRESS_PATTERN.match?(path) || path.start_with?("comp/")
|
|
262
|
+
return data_address(path, "image/svg+xml") if svg_text?(path)
|
|
263
|
+
content = File.binread(path)
|
|
264
|
+
kind = MIME_TYPES[File.extname(path).downcase]
|
|
265
|
+
kind = kind_of_bytes(content) if kind.nil?
|
|
266
|
+
data_address(content, kind.split(";").first)
|
|
267
|
+
rescue SystemCallError => error
|
|
268
|
+
OzdenUI.warning("the picture #{path} could not be read: #{error.message}") if PICTURE_ENDING.match?(path)
|
|
269
|
+
path
|
|
270
|
+
end
|
|
271
|
+
end
|
|
272
|
+
end
|
|
273
|
+
end
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# Bismillah
|
|
2
|
+
#
|
|
3
|
+
# version.rb - ozdenui v0.1 (OzdenUI: basic.js for Ruby)
|
|
4
|
+
# The version of the gem.
|
|
5
|
+
#
|
|
6
|
+
# Started Date: November 2026
|
|
7
|
+
# Webpage: https://bug7a.github.io/ozdenui-ruby/
|
|
8
|
+
#
|
|
9
|
+
# Copyright 2026 Bugra Ozden <bugra.ozden@gmail.com> (https://bug7a.github.io/)
|
|
10
|
+
# Licensed under the Apache License, Version 2.0
|
|
11
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
12
|
+
|
|
13
|
+
module OzdenUI
|
|
14
|
+
VERSION = "0.1.0"
|
|
15
|
+
end
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
|
|
2
|
+
Copyright 2020-2026 Bugra Ozden <bugra.ozden@gmail.com> (https://bug7a.github.io/)
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
@font-face{font-family:opensans;src:url(font/open-sans/OpenSans-Regular.ttf);font-display:block}@font-face{font-family:opensans-bold;src:url(font/open-sans/OpenSans-Bold.ttf);font-display:block}body{margin:0;padding:0;font-family:opensans;overflow:hidden}.basic_box,.basic_label,.basic_button,.basic_textbox-main,.basic_image{-webkit-tap-highlight-color:rgba(0,0,0,0);touch-action:manipulation;-webkit-font-smoothing:antialiased;-moz-osx-font-smoothing:grayscale;-webkit-touch-callout:none;-webkit-text-size-adjust:auto;user-select:none;-webkit-user-select:none}.basic_box{position:absolute;display:block;overflow-x:hidden;overflow-y:hidden;background-color:whitesmoke;border-radius:0;border:0 solid rgba(0,0,0,.6);font-family:opensans;font-size:16px;text-align:left;color:rgba(0,0,0,.8);padding:0;margin:0;box-sizing:border-box;pointer-events:none}.basic_label{position:absolute;display:block;overflow:hidden;font-family:opensans;font-size:20px;text-align:left;color:rgba(0,0,0,.8);background-color:transparent;border:0 solid rgba(0,0,0,.6);padding:0;box-sizing:border-box;pointer-events:none}.basic_button{position:absolute;display:block;overflow:hidden;font-family:opensans;font-size:20px;text-align:center;color:rgba(0,0,0,.65);border:1px solid rgba(0,0,0,.40);box-shadow:inset 0px -2px 0 0 rgba(0,0,0,.40);margin:0;box-sizing:border-box;border-radius:4px;background-color:#689BD2;background-image:linear-gradient(to bottom,rgba(0,0,0,0),rgba(0,0,0,.02) 60%,rgba(0,0,0,.055));background-position:left top;background-repeat:no-repeat;background-size:100% 100%;pointer-events:auto;cursor:pointer}.basic_button.minimal{border:0 solid rgba(0,0,0,.40);box-shadow:none;background-image:none}.basic_button:focus{outline:none}.basic_button:disabled{opacity:.6!important;cursor:default}.basic_textbox-main{position:absolute;display:block}.basic_textbox{position:absolute;font-family:opensans;font-size:20px;text-align:left;color:#4A4A4A;background-color:#FFFFFF;left:0;right:0;border:1px solid #4A4A4A;width:270px;height:50px;padding:12px 15px;margin:0;box-sizing:border-box;border-radius:4px;background-image:linear-gradient(to top,rgba(0,0,0,0),rgba(0,0,0,.02) 60%,rgba(0,0,0,.055));background-position:left top;background-repeat:no-repeat;background-size:100% 100%;pointer-events:auto}.basic_textbox.minimal{border:0 solid #4A4A4A;background-image:none}.basic_textbox:focus{outline:none;padding:12px 14px;background-color:#FFFFFF;border:2px solid #689BD2}.basic_textbox.minimal:focus{outline:none;padding:12px 15px;border:0 solid steelblue}.basic_textbox:disabled{opacity:.6!important}.basic_textbox-title{position:absolute;background-color:transparent;font-family:opensans;font-size:16px;text-align:left;color:#4A4A4A;height:28px;top:-28px;left:0;padding:2px;padding-left:0;border:0;border-top-left-radius:6px;border-top-right-radius:6px;border-bottom-left-radius:0;border-bottom-right-radius:0;background-image:none}.basic_image{position:absolute;display:block;overflow:hidden;background-color:transparent;border:0 solid rgba(0,0,0,.6);padding:0;margin:0;box-sizing:border-box;border-radius:0;pointer-events:none}.basic_box::-webkit-scrollbar{width:0;height:0}.basic_box::-webkit-scrollbar-thumb{background:rgba(0,0,0,.15);border-radius:6px;border:2px solid rgba(0,0,0,0);background-clip:padding-box}.basic_box::-webkit-scrollbar-thumb:hover{background:rgba(0,0,0,.2);border-radius:6px;border:2px solid rgba(0,0,0,0);background-clip:padding-box}.basic_box::-webkit-scrollbar-thumb:active{background:rgba(0,0,0,.25);border-radius:6px;border:2px solid rgba(0,0,0,0);background-clip:padding-box}.basic_box::-webkit-scrollbar-track{background:transparent;border-radius:0;background-clip:padding-box}.basic_box::-webkit-scrollbar-corner{background-color:transparent}
|