Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions tests/bindEx.nim
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
discard """
action: "compile"
"""
import webview
import strutils
import os
Expand Down Expand Up @@ -26,10 +29,9 @@ const indexHTML = """
</body>
</html>
"""

let fn="$1/xxx.html"%[getTempDir()]
writeFile(fn, indexHTML)
var w = newWebView("Simple window demo2", "file://" & fn)
let htmlFile = getTempDir() / "xxx.html"
writeFile(htmlFile, indexHTML)
var w = newWebView("Simple window demo2", "file://" & htmlFile)
var fullScreen = true
w.bindProcs("api"):
proc open() = echo w.dialogOpen()
Expand All @@ -47,4 +49,4 @@ w.bindProcs("api"):
# w.setFullscreen()
w.run()
w.exit()
removeFile(fn)
removeFile(htmlFile)
3 changes: 3 additions & 0 deletions tests/minimal.nim
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
discard """
action: "compile"
"""
import webview

open("Minimal webview example", "https://www.bing.com")
3 changes: 3 additions & 0 deletions tests/scopeTest.nim
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
discard """
action: "compile"
"""
import macros

block:
Expand Down
64 changes: 40 additions & 24 deletions webview.nim
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,13 @@ proc eval*(w: Webview; js: cstring): cint {.importc: "webview_eval", header: "we
proc injectCss*(w: Webview; css: cstring): cint {.importc: "webview_inject_css", header: "webview.h".}
proc setTitle*(w: Webview; title: cstring) {.importc: "webview_set_title", header: "webview.h".}
proc setColor*(w: Webview; r,g,b,a: uint8) {.importc: "webview_set_color", header: "webview.h".}
proc setIcon*(w: Webview; icon: cstring) {.importc: "webview_set_icon", header: "webview.h".}
proc setFullscreen*(w: Webview; fullscreen: cint) {.importc: "webview_set_fullscreen", header: "webview.h".}
proc dialog*(w: Webview; dlgtype: DialogType; flags: cint; title: cstring; arg: cstring; result: cstring; resultsz: csize) {.
importc: "webview_dialog", header: "webview.h".}
proc setMinSize*(w: Webview; width, height: cint) {.importc: "webview_minsize", header: "webview.h".}
proc setMaxSize*(w: Webview; width, height: cint) {.importc: "webview_maxsize", header: "webview.h".}
proc focus*(w: Webview) {.importc: "webview_focus", header: "webview.h".}
proc dialog*(w: Webview; dlgtype: DialogType; flags: cint; title: cstring; arg: cstring;
result: cstring; resultsz: csize_t; filter: cstring) {.importc: "webview_dialog", header: "webview.h".}
proc dispatch(w: Webview; fn: pointer; arg: pointer) {.importc: "webview_dispatch", header: "webview.h".}
proc terminate*(w: Webview) {.importc: "webview_terminate", header: "webview.h".}
proc exit*(w: Webview) {.importc: "webview_exit", header: "webview.h".}
Expand Down Expand Up @@ -132,14 +136,14 @@ proc dispatch*(w: Webview, fn: DispatchFn) =
dispatchTable[idx] = fn
dispatch(w, generalDispatchProc, cast[pointer](idx))

proc dialog*(w :Webview, dlgType: DialogType, dlgFlag: int, title, arg: string): string =
proc dialog*(w :Webview, dlgType: DialogType, dlgFlag: int, title, arg: string, filter: string = ""): string =
## dialog() opens a system dialog of the given type and title. String
## argument can be provided for certain dialogs, such as alert boxes. For
## alert boxes argument is a message inside the dialog box.
let maxPath = 4096
let resultPtr = cast[cstring](alloc0(maxPath))
defer: dealloc(resultPtr)
w.dialog(dlgType, dlgFlag.cint, title.cstring, arg.cstring, resultPtr, maxPath.csize)
w.dialog(dlgType, dlgFlag.cint, title.cstring, arg.cstring, resultPtr, maxPath.csize_t, filter.cstring)
return $resultPtr

proc msg*(w: Webview, title, msg: string) =
Expand All @@ -158,15 +162,15 @@ proc error*(w: Webview, title, msg: string) =
## Show one error box
discard w.dialog(dtAlert, dFlagError, title, msg)

proc dialogOpen*(w: Webview, title="Open File", flag=dFlagFile): string =
proc dialogOpen*(w: Webview, title="Open File", flag=dFlagFile, filter=""): string =
## Opens a dialog that requests filenames from the user. Returns ""
## if the user closed the dialog without selecting a file.
return w.dialog(dtOpen, flag, title, "")
return w.dialog(dtOpen, flag, title, "", filter=filter)

proc dialogSave*(w: Webview, title="Save File", flag=dFlagFile): string =
proc dialogSave*(w: Webview, title="Save File", flag=dFlagFile, filter=""): string =
## Opens a dialog that requests a filename to save to from the user.
## Returns "" if the user closed the dialog without selecting a file.
return w.dialog(dtSave, flag, title, "")
return w.dialog(dtSave, flag, title, "", filter=filter)

proc setFullscreen*(w: Webview, fullscree=true): bool =
## setFullscreen according to `fullscree` and return `fullscree`
Expand All @@ -190,7 +194,7 @@ proc open*(title="WebView", url="", width=640, height=480, resizable=true):int {
## (Linux/MacOS).
webview(title.cstring, url.cstring, width.cint, height.cint, (if resizable: 1 else: 0).cint)

const
const
jsTemplate = """
if (typeof $2 === 'undefined') {
$2 = {};
Expand All @@ -203,18 +207,7 @@ $2.$1 = function(arg) {
);
};
"""
jsTemplateOnlyArg = """
if (typeof $2 === 'undefined') {
$2 = {};
}
$2.$1 = function(arg) {
window.external.invoke(
JSON.stringify(
{scope: "$2", name: "$1", args: JSON.stringify(arg)}
)
);
};
"""
jsTemplateOnlyArg = jsTemplate
jsTemplateNoArg = """
if (typeof $2 === 'undefined') {
$2 = {};
Expand All @@ -228,6 +221,23 @@ $2.$1 = function() {
};
"""

## On webkit2gtk > 2.31, "external" is not part of "window".
## Add following workaround for gtk:
var jsGtkWorkaround = """
if (window.external == undefined) {
window.external = {
invoke: function(x) {
window.webkit.messageHandlers.external.postMessage(x);
}
};
}
"""

proc applyGtkWorkaround(w: Webview) =
if not jsGtkWorkaround.isEmptyOrWhitespace:
discard w.eval(jsGtkWorkaround)
jsGtkWorkaround = ""

proc bindProc*[P, R](w: Webview, scope, name: string, p: (proc(param: P): R)) =
proc hook(hookParam: string): string =
var
Expand All @@ -245,7 +255,9 @@ proc bindProc*[P, R](w: Webview, scope, name: string, p: (proc(param: P): R)) =
discard hasKeyOrPut(eps[w], scope, newTable[string, CallHook]())
eps[w][scope][name] = hook
# TODO eval jscode
w.dispatch(proc() = discard w.eval(jsTemplate%[name, scope]))
w.dispatch(proc() =
w.applyGtkWorkaround()
discard w.eval(jsTemplate%[name, scope]))

proc bindProcNoArg*(w: Webview, scope, name: string, p: proc()) =
## ugly hack or macro will fail
Expand All @@ -256,7 +268,9 @@ proc bindProcNoArg*(w: Webview, scope, name: string, p: proc()) =
discard hasKeyOrPut(eps[w], scope, newTable[string, CallHook]())
eps[w][scope][name] = hook
# TODO eval jscode
w.dispatch(proc() = discard w.eval(jsTemplateNoArg%[name, scope]))
w.dispatch(proc() =
w.applyGtkWorkaround()
discard w.eval(jsTemplateNoArg%[name, scope]))

proc bindProc*[P](w: Webview, scope, name: string, p: proc(arg:P)) =
proc hook(hookParam: string): string =
Expand All @@ -273,7 +287,9 @@ proc bindProc*[P](w: Webview, scope, name: string, p: proc(arg:P)) =
discard hasKeyOrPut(eps[w], scope, newTable[string, CallHook]())
eps[w][scope][name] = hook
# TODO eval jscode
w.dispatch(proc() = discard w.eval(jsTemplateOnlyArg%[name, scope]))
w.dispatch(proc() =
w.applyGtkWorkaround()
discard w.eval(jsTemplateOnlyArg%[name, scope]))

macro bindProcs*(w: Webview, scope: string, n: untyped): untyped =
## bind procs like:
Expand Down
8 changes: 4 additions & 4 deletions webview.nimble
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ skipDirs = @["tests"]
requires "nim >= 0.17.2"

task test, "a simple test case":
exec "nim c -r tests/bindEx.nim"
exec "testament pattern \"tests/*.nim\""

task docs, "generate doc":
exec "nim doc2 -o:docs/webview.html webview.nim"

task sync, "update webview.h":
exec "wget -O webview/webview.h https://raw.githubusercontent.com/zserge/webview/master/webview.h"
exec "wget -O webview/webview.go https://raw.githubusercontent.com/zserge/webview/master/webview.go"
exec "wget -O webview/README.md https://raw.githubusercontent.com/zserge/webview/master/README.md"
exec "wget -O webview/webview.h https://raw.githubusercontent.com/wailsapp/wails/master/lib/renderer/webview/webview.h"
exec "wget -O webview/webview.go https://raw.githubusercontent.com/wailsapp/wails/master/lib/renderer/webview/webview.go"
exec "wget -O webview/LICENSE https://raw.githubusercontent.com/wailsapp/wails/master/lib/renderer/webview/LICENSE"

task clean, "clean tmp files":
exec "rm -rf nimcache"
Expand Down
2 changes: 2 additions & 0 deletions webview/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
Latest Webview version copied from https://github.com/wailsapp/wails/tree/master/lib/renderer/webview as the official Webview changed from C to C++ and also removed existing functionality - for example Dialogs, setColor or setFullscreen.

# webview

[![Join the chat at https://gitter.im/zserge/webview](https://badges.gitter.im/zserge/webview.svg)](https://gitter.im/zserge/webview?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
Expand Down
Loading