Skip to content

Commit 242a7d3

Browse files
authored
Merge pull request #33 from webui-dev/neroist-patch-2
Add/update Nim documentation
2 parents a2de3df + 20ad1f3 commit 242a7d3

File tree

1 file changed

+154
-40
lines changed

1 file changed

+154
-40
lines changed

server/docs/2.5/README.md

Lines changed: 154 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -300,8 +300,8 @@ func main() {
300300
```nim
301301
import webui
302302
303-
let win = newWindow()
304-
win.show("<html><script src=\"webui.js\"></script> Hello World from Nim! </html>")
303+
let window = newWindow()
304+
window.show("""<html><script src="/webui.js"></script> Hello World from Nim! </html>""")
305305
wait()
306306
```
307307
[More Nim Examples](https://github.com/webui-dev/nim-webui/tree/main/examples).
@@ -428,7 +428,10 @@ myWindow := webui.NewWindow()
428428
#### **Nim**
429429
<!-- ---------- -->
430430
```nim
431-
let myWindow = newWindow()
431+
let window = newWindow()
432+
433+
# later...
434+
window.show("index.html")
432435
```
433436
<!-- ---------- -->
434437
#### **V**
@@ -555,7 +558,12 @@ int main() {
555558
#### **Nim**
556559
<!-- ---------- -->
557560
```nim
558-
// In development...
561+
let
562+
windowId = 1
563+
window = newWindow(windowId)
564+
565+
# later...
566+
window.show("index.html")
559567
```
560568
<!-- ---------- -->
561569
#### **V**
@@ -676,7 +684,12 @@ int main() {
676684
#### **Nim**
677685
<!-- ---------- -->
678686
```nim
679-
// In development...
687+
let
688+
windowId = getNewWindowId()
689+
window = newWindow(windowId)
690+
691+
# later...
692+
window.show("index.html")
680693
```
681694
<!-- ---------- -->
682695
#### **V**
@@ -842,9 +855,25 @@ webui.Bind(win, "MyID", my_function)
842855
#### **Nim**
843856
<!-- ---------- -->
844857
```nim
845-
myWindow.bind("MyID") do (e: Event):
846-
# <button id="MyID">Hello</button> gets clicked!
847-
echo "Binding element ", e.element, "!"
858+
# `bind` accepts three arguments:
859+
# * window - The window to bind the function onto
860+
# * element - The HTML element / JavaScript object to bind the function `func` onto
861+
# * `func` - The callback proc -- the function to bind to `element`.
862+
863+
# there are two ways to use `bind` -- you may either pass a proc into it or use `do` notation
864+
865+
# --- do notation. binds anon func to `callback`
866+
window.bind("callback") do (e: Event):
867+
# do stuff here
868+
echo "called!"
869+
870+
# --- create proc and pass it to `bind`
871+
proc events(e: Event) =
872+
# do stuff here
873+
echo "caught event ", e.eventType
874+
875+
# bind proc to all events
876+
window.bind("", events)
848877
```
849878
<!-- ---------- -->
850879
#### **V**
@@ -1044,9 +1073,33 @@ webui.Bind(win, "", events)
10441073
#### **Nim**
10451074
<!-- ---------- -->
10461075
```nim
1047-
# Empty ID means bind all events on all elements
1048-
myWindow.bind("") do (e: Event):
1049-
echo "Hi!, You clicked on ", e.element, " element"
1076+
#[
1077+
This is the `WebuiEvent` enum:
1078+
1079+
WebuiEvent* = enum
1080+
weDisconnected ## 0. Window disconnection event
1081+
weConnected ## 1. Window connection event
1082+
weMultiConnection ## 2. New window connection event
1083+
weUnwantedConnection ## 3. New unwanted window connection event
1084+
weMouseClick ## 4. Mouse click event
1085+
weNavigation ## 5. Window navigation event
1086+
weCallback ## 6. Function call event
1087+
1088+
It is held in the `eventType` property of the `Event` object, and describes the
1089+
type of the event
1090+
]#
1091+
1092+
# empty `element` means bind all events on all elements
1093+
window.bind("") do (e: Event):
1094+
case e.eventType
1095+
of weConnected:
1096+
echo "Connected."
1097+
of weDisconnected:
1098+
echo "Disconnected."
1099+
of weMouseClick:
1100+
echo "Click."
1101+
of weEventNavigation:
1102+
echo "Starting navigation to: ", e.data
10501103
```
10511104
<!-- ---------- -->
10521105
#### **V**
@@ -1242,7 +1295,29 @@ int main() {
12421295
#### **Nim**
12431296
<!-- ---------- -->
12441297
```nim
1245-
// In development...
1298+
#[
1299+
This is the `WebuiBrowser` enum:
1300+
1301+
type
1302+
WebuiBrowser* = enum
1303+
wbNoBrowser ## 0. No web browser
1304+
wbAny ## 1. Default recommended web browser
1305+
wbChrome ## 2. Google Chrome
1306+
wbFirefox ## 3. Mozilla Firefox
1307+
wbEdge ## 4. Microsoft Edge
1308+
wbSafari ## 5. Apple Safari
1309+
wbChromium ## 6. The Chromium Project
1310+
wbOpera ## 7. Opera Browser
1311+
wbBrave ## 8. The Brave Browser
1312+
wbVivaldi ## 9. The Vivaldi Browser
1313+
wbEpic ## 10. The Epic Browser
1314+
wbYandex ## 11. The Yandex Browser
1315+
wbChromiumBased ## 12. Any Chromium based browser
1316+
wbWebview ## 13. Webview (not a web browser)
1317+
]#
1318+
1319+
# `getBestBrowser` will return an item from the `WebuiBrowser` enum
1320+
let bestBrowser = window.getBestBrowser()
12461321
```
12471322
<!-- ---------- -->
12481323
#### **V**
@@ -1402,9 +1477,14 @@ myWindow.Show("https://mydomain.com")
14021477
#### **Nim**
14031478
<!-- ---------- -->
14041479
```nim
1405-
myWindow.show("<html><script src=\"/webui.js\"> ... </html>")
1406-
myWindow.show("file.html")
1407-
myWindow.show("https://mydomain.com")
1480+
# `show` accepts 2 (or 3) parameters:
1481+
# * window - The window to show content in
1482+
# * content - The content to show in `window`, may be a file or some HTML code
1483+
# * browser - The browser to open the window in (OPTIONAL)
1484+
1485+
window.show("<html><script src=\"/webui.js\"> ... </html>")
1486+
window.show("file.html")
1487+
window.show("https://mydomain.com")
14081488
```
14091489
<!-- ---------- -->
14101490
#### **V**
@@ -1576,7 +1656,11 @@ myWindow.ShowBrowser("<html><script src=\"/webui.js\"> ... </html>", webui.Chrom
15761656
#### **Nim**
15771657
<!-- ---------- -->
15781658
```nim
1579-
myWindow.show("<html><script src=\"/webui.js\"> ... </html>", Browsers.Chrome)
1659+
# pass a single web browser that you wish to used
1660+
window.show("index.html", wbChrome)
1661+
1662+
# you may also pass a set or openArray if you want to use a specific set of web browsers
1663+
window.show("index.html", {wbWebview, wbChrome, wbEdge})
15801664
```
15811665
<!-- ---------- -->
15821666
#### **V**
@@ -1706,7 +1790,7 @@ int main() {
17061790
#### **Nim**
17071791
<!-- ---------- -->
17081792
```nim
1709-
// In development...
1793+
window.showWv("index.html")
17101794
```
17111795
<!-- ---------- -->
17121796
#### **V**
@@ -1828,7 +1912,7 @@ int main() {
18281912
#### **Nim**
18291913
<!-- ---------- -->
18301914
```nim
1831-
// In development...
1915+
window.kiosk = true
18321916
```
18331917
<!-- ---------- -->
18341918
#### **V**
@@ -1968,9 +2052,11 @@ webui.Wait()
19682052
# Show the windows...
19692053
19702054
# Wait until all windows get closed
1971-
# or when calling exit()
2055+
# or when calling `exit()`
19722056
wait()
19732057
```
2058+
2059+
Full Nim example: <https://github.com/webui-dev/nim-webui/tree/main/examples/minimal.nim>
19742060
<!-- ---------- -->
19752061
#### **V**
19762062
<!-- ---------- -->
@@ -2108,7 +2194,7 @@ webui.Close(win)
21082194
#### **Nim**
21092195
<!-- ---------- -->
21102196
```nim
2111-
myWindow.close()
2197+
window.close()
21122198
```
21132199
<!-- ---------- -->
21142200
#### **V**
@@ -2227,7 +2313,7 @@ int main() {
22272313
#### **Nim**
22282314
<!-- ---------- -->
22292315
```nim
2230-
// In development...
2316+
window.destroy()
22312317
```
22322318
<!-- ---------- -->
22332319
#### **V**
@@ -2464,7 +2550,7 @@ int main() {
24642550
#### **Nim**
24652551
<!-- ---------- -->
24662552
```nim
2467-
// In development...
2553+
window.rootFolder = "/home/Foo/Bar/"
24682554
```
24692555
<!-- ---------- -->
24702556
#### **V**
@@ -2587,7 +2673,7 @@ int main() {
25872673
#### **Nim**
25882674
<!-- ---------- -->
25892675
```nim
2590-
// In development...
2676+
setDefaultRootFolder("/home/Foo/Bar/")
25912677
```
25922678
<!-- ---------- -->
25932679
#### **V**
@@ -2723,7 +2809,7 @@ Full C++ Example: https://github.com/webui-dev/webui/tree/main/examples/C%2B%2B/
27232809
```nim
27242810
var count: int
27252811
2726-
myWindow.setFileHandler() do (filename: string) -> string:
2812+
window.setFileHandler() do (filename: string) -> string:
27272813
echo "File: ", filename
27282814
27292815
case filename
@@ -2748,10 +2834,12 @@ myWindow.setFileHandler() do (filename: string) -> string:
27482834
</html>
27492835
"""
27502836
2751-
# By default, this function returns an empty string
2752-
# returning an empty string will make WebUI look for
2837+
# By default, this function returns an empty string.
2838+
# Returning an empty string will make WebUI look for
27532839
# the requested file locally
27542840
```
2841+
2842+
Full Nim example: <https://github.com/webui-dev/nim-webui/tree/main/examples/serve_folder/serve_folder.nim>
27552843
<!-- ---------- -->
27562844
#### **V**
27572845
<!-- ---------- -->
@@ -2966,7 +3054,7 @@ if win.IsShown() {
29663054
#### **Nim**
29673055
<!-- ---------- -->
29683056
```nim
2969-
if myWindow.shown():
3057+
if window.shown:
29703058
echo "A window is running..."
29713059
else:
29723060
echo "No window is running."
@@ -3108,7 +3196,14 @@ webui_set_timeout(10);
31083196
#### **Nim**
31093197
<!-- ---------- -->
31103198
```nim
3111-
setTimeout(10);
3199+
# `setTimeout` accepts a single parameter:
3200+
# * timeout - The maximum time in seconds to wait for browser to start.
3201+
# Set to 0 to wait forever.
3202+
3203+
setTimeout(30)
3204+
3205+
window.show("index.html")
3206+
wait()
31123207
```
31133208
<!-- ---------- -->
31143209
#### **V**
@@ -3255,18 +3350,20 @@ win.set_icon(myIcon, myIconType)
32553350
```nim
32563351
# SVG Icon
32573352
let
3258-
myIcon = "<svg>...</svg>"
3259-
myIconType = "image/svg+xml"
3353+
icon = "<svg>...</svg>"
3354+
mimeType = "image/svg+xml"
32603355
3261-
# PNG Icon
3356+
# --- PNG Icon
32623357
# let
3263-
# myIcon = "data:image/..."
3264-
# myIconType = "image/png"
3358+
# mimeType = "data:image/..."
3359+
# mimeType = "image/png"
32653360
3266-
# When the web browser ask for `favicon.ico`, WebUI will
3267-
# send a redirection to `favicon.svg`, the body will be `myIcon`
3268-
# and the mime-type will be `myIconType`
3269-
myWindow.setIcon(myIcon, myIconType)
3361+
# `setIcon` accepts 3 arguments:
3362+
# * window - The window to set the icon for
3363+
# * icon - The icon as a string
3364+
# * mime - The MIME type of the icon
3365+
3366+
window.setIcon(icon, mimeType)
32703367
```
32713368
<!-- ---------- -->
32723369
#### **V**
@@ -3414,7 +3511,11 @@ int main() {
34143511
#### **Nim**
34153512
<!-- ---------- -->
34163513
```nim
3417-
// In development...
3514+
# `encode` accepts a single argument:
3515+
# * str - The string to encode into Base64.
3516+
# Note: the string is nil-terminated
3517+
3518+
let base64 = encode("Foo Bar")
34183519
```
34193520
<!-- ---------- -->
34203521
#### **V**
@@ -3672,7 +3773,14 @@ int main() {
36723773
#### **Nim**
36733774
<!-- ---------- -->
36743775
```nim
3675-
// In development...
3776+
from webui/bindings import nil
3777+
3778+
let buffer = bindings.malloc(csize_t 1024)
3779+
3780+
# `free` accepts a single argument:
3781+
# * `ptr` - The buffer to be freed
3782+
3783+
bindings.free(buffer)
36763784
```
36773785
<!-- ---------- -->
36783786
#### **V**
@@ -3799,7 +3907,13 @@ int main() {
37993907
#### **Nim**
38003908
<!-- ---------- -->
38013909
```nim
3802-
// In development...
3910+
from webui/bindings import nil
3911+
3912+
# `malloc` accepts a single argument:
3913+
# * size - The size of the memory in bytes
3914+
3915+
let buffer = bindings.malloc(csize_t 1024)
3916+
bindings.free(buffer)
38033917
```
38043918
<!-- ---------- -->
38053919
#### **V**

0 commit comments

Comments
 (0)