crownguipkg/webview

Types

ExternalInvokeCb = proc (w: Webview; arg: string)
External CallBack Proc
WebviewObj {...}{.importc: "struct webview", header: "/Users/bung/nim_works/crowngui/src/crownguipkg/webview.h",
             bycopy.} = object
  url* {...}{.importc: "url".}: cstring ## Current URL
  title* {...}{.importc: "title".}: cstring ## Window Title
  width* {...}{.importc: "width".}: cint ## Window Width
  height* {...}{.importc: "height".}: cint ## Window Height
  resizable* {...}{.importc: "resizable".}: cint ## `true` to Resize the Window, `false` for Fixed size Window
  debug* {...}{.importc: "debug".}: cint ## Debug is `true` when not build for Release
  invokeCb {...}{.importc: "external_invoke_cb".}: pointer ## Callback proc js:window.external.invoke
  priv {...}{.importc: "priv".}: WebviewPrivObj
  userdata {...}{.importc: "userdata".}: pointer
WebView Type
Webview = ptr WebviewObj
DispatchFn = proc ()

Consts

dataUriHtmlHeader = :anonymous
fileLocalHeader = "file:///"
Use Local File as URL

Procs

proc externalInvokeCB=(w: Webview; callback: ExternalInvokeCb) {...}{.inline,
    raises: [Exception], tags: [RootEffect].}
Set the external invoke callback for webview, for Advanced users only
proc dispatch(w: Webview; fn: DispatchFn) {...}{.inline,
    raises: [Exception, KeyError], tags: [RootEffect].}
Explicitly force dispatch a function, for advanced users only
proc bindProc[P, R](w: Webview; scope, name: string; p: (proc (param: P): R)) {...}{.
    used.}
Do NOT use directly, see bindProcs macro.
proc bindProcNoArg(w: Webview; scope, name: string; p: proc ()) {...}{.used,
    raises: [KeyError, Exception, ValueError], tags: [RootEffect].}
Do NOT use directly, see bindProcs macro.
proc bindProc[P](w: Webview; scope, name: string; p: proc (arg: P)) {...}{.used.}
Do NOT use directly, see bindProcs macro.
proc run(w: Webview; quitProc: proc () {...}{.noconv.};
         controlCProc: proc () {...}{.noconv.}; autoClose: static[bool] = true) {...}{.
    inline.}
run starts the main UI loop until the user closes the window. Same as run but with extras.
  • quitProc is a function to run at exit, needs {.noconv.} pragma.
  • controlCProc is a function to run at CTRL+C, needs {.noconv.} pragma.
  • autoClose set to true to automatically run exit() at exit.
proc newWebView(path: static[string] = ""; title = ""; width: Positive = 1000;
                height: Positive = 700; resizable: static[bool] = true;
                debug: static[bool] = not false;
                callback: ExternalInvokeCb = nil;
                skipTaskbar: static[bool] = false;
                windowBorders: static[bool] = true; focus: static[bool] = false;
                keepOnTop: static[bool] = false;
                minimized: static[bool] = false; cssPath: static[string] = "";
                trayIcon: static[cstring] = ""; fullscreen: static[bool] = false): Webview
Create a new Window with given attributes, all arguments are optional.
  • path is the URL or Full Path to 1 HTML file, index of the Web GUI App.
  • title is the Title of the Window.
  • width is the Width of the Window.
  • height is the Height of the Window.
  • resizable set to true to allow Resize of the Window, defaults to true.
  • debug Debug mode, Debug is true when not built for Release.
  • skipTaskbar if set to true the Window will not be visible on the desktop Taskbar.
  • windowBorders if set to false the Window will have no Borders, no Close button, no Minimize button.
  • focus if set to true the Window will force Focus.
  • keepOnTop if set to true the Window will keep on top of all other windows on the desktop.
  • minimized if set the true the Window will be Minimized, Iconified.
  • cssPath Full Path or URL of a CSS file to use as Style, defaults to "dark.css" for Dark theme, can be "light.css" for Light theme.
  • trayIcon Path to a local PNG Image Icon file.
  • fullscreen if set to true the Window will be forced Fullscreen.
  • If --light-theme on commandLineParams() then it will use Light Theme automatically.
  • CSS is embedded, if your app is used Offline, it will display Ok.
  • For templates that do CSS, remember that CSS must be injected after DOM Ready.
  • Is up to the developer to guarantee access to the HTML URL or File of the GUI.

Funcs

func js(w: Webview; javascript: cstring): cint {...}{.importc: "webview_eval",
    header: "/Users/bung/nim_works/crowngui/src/crownguipkg/webview.h",
    discardable.}
Evaluate a JavaScript cstring, runs the javascript string on the window
func css(w: Webview; css: cstring): cint {...}{.importc: "webview_inject_css",
    header: "/Users/bung/nim_works/crowngui/src/crownguipkg/webview.h",
    discardable.}
Set a CSS cstring, inject the CSS on the Window
func setTitle(w: Webview; title: cstring) {...}{.importc: "webview_set_title",
    header: "/Users/bung/nim_works/crowngui/src/crownguipkg/webview.h".}
Set Title of window
func setColor(w: Webview; red, green, blue, alpha: uint8) {...}{.
    importc: "webview_set_color",
    header: "/Users/bung/nim_works/crowngui/src/crownguipkg/webview.h".}
Set background color of the Window
func setFullscreen(w: Webview; fullscreen: bool) {...}{.
    importc: "webview_set_fullscreen",
    header: "/Users/bung/nim_works/crowngui/src/crownguipkg/webview.h".}
Set fullscreen
func jsDebug(format: cstring) {...}{.varargs, importc: "webview_debug", header: "/Users/bung/nim_works/crowngui/src/crownguipkg/webview.h".}
console.debug() directly inside the JavaScript context.
func jsLog(s: cstring) {...}{.importc: "webview_print_log", header: "/Users/bung/nim_works/crowngui/src/crownguipkg/webview.h".}
console.log() directly inside the JavaScript context.
func setUrl(w: Webview; url: cstring) {...}{.importc: "webview_launch_external_URL", header: "/Users/bung/nim_works/crowngui/src/crownguipkg/webview.h".}
Set the current URL
func setIconify(w: Webview; mustBeIconified: bool) {...}{.
    importc: "webview_set_iconify",
    header: "/Users/bung/nim_works/crowngui/src/crownguipkg/webview.h".}
Set window to be Minimized Iconified
func setBorderless(w: Webview; decorated: bool) {...}{.inline, raises: [], tags: [].}
Use a window without borders, no close nor minimize buttons.
func setSkipTaskbar(w: Webview; hint: bool) {...}{.inline, raises: [], tags: [].}
Do not show the window on the Taskbar
func setSize(w: Webview; width: Positive; height: Positive) {...}{.inline,
    raises: [], tags: [].}
Resize the window to given size
func setFocus(w: Webview) {...}{.inline, raises: [], tags: [].}
Force focus on the window
func setOnTop(w: Webview; mustBeOnTop: bool) {...}{.inline, raises: [], tags: [].}
Force window to be on top of all other windows
func setClipboard(w: Webview; text: cstring) {...}{.inline, raises: [], tags: [].}
Set a text cstring on the Clipboard, text must not be empty string
func setTrayIcon(w: Webview; path, tooltip: cstring; visible = true) {...}{.inline,
    raises: [], tags: [].}
Set a TrayIcon on the corner of the desktop. path is full path to a PNG image icon. Only shows an icon.
func run(w: Webview) {...}{.inline, raises: [], tags: [].}
run starts the main UI loop until the user closes the window or exit() is called.
func exit(w: Webview) {...}{.inline, raises: [], tags: [].}
Explicitly Terminate, close, exit, quit.

Macros

macro bindProcs(w: Webview; scope: string; n: untyped): untyped
  • Functions must be proc or func; No template nor macro.
  • Functions must NOT have return Type, must NOT return anything, use the API.
  • To pass return data to the Frontend use the JavaScript API and WebGui API.
  • Functions do NOT need the * Star to work. Functions must NOT have Pragmas.

You can bind functions with the signature like:

proc functionName[T, U](argumentString: T): U
proc functionName[T](argumentString: T)
proc functionName()

Then you can call the function in JavaScript side, like this:

scope.functionName(argumentString)

Example:

let app = newWebView()
app.bindProcs("api"):
  proc changeTitle(title: string) = app.setTitle(title) ## You can call code on the right-side,
  proc changeCss(stylesh: string) = app.css(stylesh)    ## from JavaScript Web Frontend GUI,
  proc injectJs(jsScript: string) = app.js(jsScript)    ## by the function name on the left-side.
  ## (JS) JavaScript Frontend <-- = --> Nim Backend (Native Code, C Speed)

The only limitation is 1 string argument only, but you can just use JSON.

Templates

template msg(w: Webview; title, msg: string)
Show one message box
template info(w: Webview; title, msg: string)
Show one alert box
template warn(w: Webview; title, msg: string)
Show one warn box
template error(w: Webview; title, msg: string)
Show one error box
template dialogOpen(w: Webview; title = ""): string
Opens a dialog that requests filenames from the user. Returns "" if the user closed the dialog without selecting a file.
template dialogSave(w: Webview; title = ""): 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.
template dialogOpenDir(w: Webview; title = ""): string
Opens a dialog that requests a Directory from the user.