- /*
- * Copyright (c) 2023 XXIV
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all
- * copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
- module webview;
-
- import std.string : toStringz, fromStringz;
- import webview.raw;
-
- alias WebViewVersionInfo = webview_version_info_t;
-
- alias DispatchCallback = extern(C) void function (WebView w, void* arg);
-
- alias BindCallback = extern(C) void function (string seq, string req, void* arg);
-
- enum WindowSizeHint
- {
- None,
- Min,
- Max,
- Fixed
- }
-
- struct CallbackContext(T)
- {
- T func;
- void* data = null;
- }
-
- struct WebView
- {
- private webview_t webview;
-
- this(bool debug_, void* window)
- {
- webview = webview_create(cast(int)debug_, window);
- }
-
- void run()
- {
- webview_run(this.webview);
- }
-
- void terminate()
- {
- webview_terminate(this.webview);
- }
-
- void dispatch(CallbackContext!(DispatchCallback)* ctx)
- {
- struct RegisterCallback
- {
- extern(C) static void call(webview_t w, void* arg)
- {
- CallbackContext!(DispatchCallback)* cb = cast(CallbackContext!(DispatchCallback)*)arg;
- WebView webview;
- webview.webview = w;
- cb.func(webview, cb.data);
- }
- };
- webview_dispatch(this.webview, &RegisterCallback.call, ctx);
- }
-
- void* getWindow()
- {
- return webview_get_window(this.webview);
- }
-
- void setTitle(string title)
- {
- webview_set_title(this.webview, title.toStringz);
- }
-
- void setSize(int width, int height, WindowSizeHint hint)
- {
- webview_set_size(this.webview, width, height, cast(int)hint);
- }
-
- void navigate(string url)
- {
- webview_navigate(this.webview, url.toStringz);
- }
-
- void setHtml(string html)
- {
- webview_set_html(this.webview, html.toStringz);
- }
-
- void init(string js)
- {
- webview_init(this.webview, js.toStringz);
- }
-
- void eval(string js)
- {
- webview_eval(this.webview, js.toStringz);
- }
-
- void bind(string name, CallbackContext!(BindCallback)* ctx)
- {
- struct RegisterCallback
- {
- extern(C) static void call(const(char)* seq, const(char)* req, void* arg)
- {
- CallbackContext!(BindCallback)* cb = cast(CallbackContext!(BindCallback)*)arg;
- cb.func(cast(string)(seq.fromStringz), cast(string)(req.fromStringz), cb.data);
- }
- };
- webview_bind(this.webview, name.toStringz, &RegisterCallback.call, ctx);
- }
-
- void unbind(string name)
- {
- webview_unbind(this.webview, name.toStringz);
- }
-
- void ret(string seq, int status, string result)
- {
- webview_return(this.webview, seq.toStringz, status, result.toStringz);
- }
-
- const(WebViewVersionInfo)* ver()
- {
- return webview_version();
- }
-
- ~this()
- {
- webview_destroy(this.webview);
- }
- }