Skip to content

Commit

Permalink
update to WebUI 2.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
neroist committed Apr 1, 2023
1 parent 61f72b5 commit bc6d423
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 72 deletions.
70 changes: 20 additions & 50 deletions webui.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Nim wrapper for [WebUI](https://github.com/alifcommunity/webui)
:Author: Jasmine
:WebUI Version: 2.1.0
:WebUI Version: 2.1.1
See: https://neroist.github.io/webui-docs/
]###
Expand Down Expand Up @@ -59,13 +59,22 @@ type
RuntimeDeno
RuntimeNodeJs

# forward declarations, needed for `bind` and `bindAll`
WebUIEvent* = enum
EventConnected = 1
EventMultiConnection
EventUnwantedConnection
EventDisconnected
EventMouseClick
EventNavigation
EventCallback

# forward declarations, needed for `bind`
proc getNumber*(win: Window): int

# vars
var cbs: array[bindings.WEBUI_MAX_ARRAY, array[bindings.WEBUI_MAX_ARRAY, proc (e: Event)]] ## \
## array of binded callbacks.
## Needed for `bind` and `bindAll`
## Needed for `bind`

proc wait*() =
## Run application run until the user closes all
Expand Down Expand Up @@ -309,9 +318,6 @@ proc serverRoot*(winCore: WindowCore): bool =
proc serverPort*(winCore: WindowCore): int =
int winCore.impl.serverPort

proc bindAll*(winCore: WindowCore): bool =
bool winCore.impl.isBindAll

proc url*(winCore: WindowCore): string =
$ winCore.impl.url

Expand Down Expand Up @@ -352,6 +358,9 @@ proc runtime*(winCore: WindowCore): Runtime =
proc detectProcessClose*(winCore: WindowCore): bool =
winCore.impl.detectProcessClose

proc hasEvents*(winCore: WindowCore): bool =
bool winCore.impl.hasEvents

# -------- Window --------

proc newWindow*(): Window =
Expand Down Expand Up @@ -483,48 +492,6 @@ proc `bind`*(win: Window; element: string; `func`: proc (e: Event): bool) =
e.returnBool(res)
)

proc bindAll*(win: Window; `func`: proc (e: Event)) =
## Bind all elements

bindings.bindAll(win.impl, bindHandler)
let wid = win.getNumber()

# bindInterface was going to return zero anyway
#
# C source of `webui_bind_interface`:
# ...
# if(_webui_is_empty(element)) {
# webui_bind_all(win, webui_bind_interface_all_handler);
# webui.cb_interface_all[0] = func;
# return 0;
# }
# ...

cbs[wid][0] = `func`

proc bindAll*(win: Window; `func`: proc (e: Event): string) =
win.bindAll(
proc (e: Event) =
let res = `func`(e)
e.returnString(res)
)

proc bindAll*(win: Window; `func`: proc (e: Event): int) =
win.bindAll(
proc (e: Event) =
let res = `func`(e)
e.returnInt(res)
)

proc bindAll*(win: Window; `func`: proc (e: Event): bool) =
## Bind all elements to `func` automatically pass return value of `func` to Javascript

win.bindAll(
proc (e: Event) =
let res = `func`(e)
e.returnBool(res)
)

proc open*(win: Window; url: string | Uri; browser: Browser = BrowserAny): bool {.discardable.} =
bindings.open(win.impl, cstring $url, cuint ord(browser))

Expand All @@ -544,8 +511,8 @@ proc receive*(win: Window; packet: string; len: int) =
proc send*(win: Window; packet: string; packetsSize: int) =
bindings.windowSend(win.impl, cstring packet, csize_t packetsSize)

proc event*(win: Window; elementId, element: string; data: pointer; dataLen: int) =
bindings.windowEvent(win.impl, cstring elementId, cstring element, data, cuint dataLen)
proc event*(win: Window; elementId, element: string; data: pointer; dataLen: int, eventType: int | WebUIEvent) =
bindings.windowEvent(win.impl, cstring elementId, cstring element, data, cuint dataLen, cint ord eventType)

proc getNumber*(win: Window): int =
int bindings.windowGetNumber(win.impl)
Expand Down Expand Up @@ -612,6 +579,9 @@ proc waitProcess*(win: Window; status: bool) =
proc generateJsBridge*(win: Window): string =
$ bindings.generateJsBridge(win.impl)

proc generateInternalId*(win: Window, element: string) =
bindings.generateInternalId(win.impl, cstring element)

export
bindings.webui,
bindings.WEBUI_VERSION
49 changes: 28 additions & 21 deletions webui/bindings.nim
Original file line number Diff line number Diff line change
Expand Up @@ -84,19 +84,26 @@ else:
{.deadCodeElim: on.}

const
WEBUI_VERSION* = "2.1.0" ## Version
WEBUI_HEADER_SIGNATURE* = 0xFF ## All packets should start with this 8bit
WEBUI_HEADER_JS* = 0xFE ## Javascript result in frontend
WEBUI_HEADER_CLICK* = 0xFD ## Click event
WEBUI_HEADER_SWITCH* = 0xFC ## Frontend refresh
WEBUI_HEADER_CLOSE* = 0xFB ## Close window
WEBUI_HEADER_CALL_FUNC* = 0xFA ## Call a backend function
WEBUI_MAX_ARRAY* = (1024) ## Max num of threads, servers, windows, pointers...
WEBUI_MIN_PORT* = (10000) ## Minimum socket port
WEBUI_MAX_PORT* = (65500) ## Should be less than 65535
WEBUI_MAX_BUF* = (1024000) ## 1024 Kb max dynamic memory allocation
WEBUI_DEFAULT_PATH* = "." ## Default root path
WEBUI_DEF_TIMEOUT* = (8) ## Default startup timeout in seconds
WEBUI_VERSION* = "2.1.1" ## Version
WEBUI_HEADER_SIGNATURE* = 0xFF ## All packets should start with this 8bit
WEBUI_HEADER_JS* = 0xFE ## Javascript result in frontend
WEBUI_HEADER_CLICK* = 0xFD ## Click event
WEBUI_HEADER_SWITCH* = 0xFC ## Frontend refresh
WEBUI_HEADER_CLOSE* = 0xFB ## Close window
WEBUI_HEADER_CALL_FUNC* = 0xFA ## Call a backend function
WEBUI_MAX_ARRAY* = (1024) ## Max num of threads, servers, windows, pointers...
WEBUI_MIN_PORT* = (10000) ## Minimum socket port
WEBUI_MAX_PORT* = (65500) ## Should be less than 65535
WEBUI_MAX_BUF* = (1024000) ## 1024 Kb max dynamic memory allocation
WEBUI_DEFAULT_PATH* = "." ## Default root path
WEBUI_DEF_TIMEOUT* = (8) ## Default startup timeout in seconds
WEBUI_EVENT_CONNECTED* = (1) ## Window connected
WEBUI_EVENT_MULTI_CONNECTION* = (2) ## Multi clients connected
WEBUI_EVENT_UNWANTED_CONNECTION* = (3) ## Unwanted client connected
WEBUI_EVENT_DISCONNECTED* = (4) ## Window disconnected
WEBUI_EVENT_MOUSE_CLICK* = (5) ## Mouse Click
WEBUI_EVENT_NAVIGATION* = (6) ## The window URL changed
WEBUI_EVENT_CALLBACK* = (7) ## Function call

# -- Types -------------------------

Expand All @@ -113,9 +120,7 @@ type
multiAccess*: bool
serverRoot*: bool
serverPort*: cuint
isBindAll*: bool
url*: cstring
cbAll*: array[1, proc (e: Event)]
html*: cstring
htmlCpy*: cstring
icon*: cstring
Expand All @@ -126,6 +131,7 @@ type
connections*: cuint
runtime*: cuint
detectProcessClose*: bool
hasEvents*: bool
#when defined(windows):
# serverThread: Handle
#else:
Expand All @@ -142,6 +148,7 @@ type
window*: ptr Window
data*: pointer
response*: pointer
`type`*: cint

JavascriptResult* {.bycopy.} = object
error*: bool
Expand All @@ -158,6 +165,8 @@ type
internalId*: cstring
elementName*: cstring
data*: pointer
dataLen*: cuint
eventType*: cint

CmdAsync* {.bycopy.} = object
win*: ptr Window
Expand Down Expand Up @@ -214,9 +223,6 @@ type
cb_interface*: array[WEBUI_MAX_ARRAY, proc (elementId: cuint;
windowId: cuint; elementName: cstring; window: ptr Window;
data: cstring; response: cstringArray) {.cdecl.}]
cb_interface_all*: array[1, proc(elementId: cuint; windowId: cuint;
elementName: cstring; window: ptr Window; data: cstring;
response: cstringArray) {.cdecl.}]
executablePath*: cstring
ptrList*: array[WEBUI_MAX_ARRAY, pointer]
ptrPosition*: cuint
Expand Down Expand Up @@ -248,8 +254,6 @@ proc script*(win: ptr Window; script: ptr Script) {.cdecl,
proc `bind`*(win: ptr Window; element: cstring; `func`: proc (
e: ptr Event) {.cdecl.}): cuint {.
cdecl, importc: "webui_bind", webui.}
proc bindAll*(win: ptr Window; `func`: proc (e: ptr Event) {.cdecl.}) {.cdecl,
importc: "webui_bind_all", webui.}
proc open*(win: ptr Window; url: cstring; browser: cuint): bool {.cdecl,
importc: "webui_open", webui.}
proc scriptCleanup*(script: ptr Script) {.cdecl,
Expand Down Expand Up @@ -309,7 +313,7 @@ proc windowReceive*(win: ptr Window; packet: cstring; len: csize_t) {.cdecl,
proc windowSend*(win: ptr Window; packet: cstring;
packetsSize: csize_t) {.cdecl, importc: "_webui_window_send", webui.}
proc windowEvent*(win: ptr Window; elementId: cstring; element: cstring;
data: pointer; dataLen: cuint) {.cdecl,
data: pointer; dataLen: cuint, eventType: cint) {.cdecl,
importc: "_webui_window_event", webui.}
proc windowGetNumber*(win: ptr Window): cuint {.cdecl,
importc: "_webui_window_get_number", webui.}
Expand Down Expand Up @@ -388,3 +392,6 @@ proc freeAllMem*() {.cdecl, importc: "_webui_free_all_mem", webui.}

proc showWindow*(win: ptr Window; html: cstring; browser: cuint): bool {.cdecl,
importc: "_webui_show_window", webui.}
proc generateInternalId*(win: ptr Window; element: cstring): cstring {.cdecl,
importc: "_webui_generate_internal_id", webui.}

2 changes: 1 addition & 1 deletion webui/webui
Submodule webui updated from cf7b7f to 9a3861

0 comments on commit bc6d423

Please sign in to comment.