Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix callback issue #4

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
29 changes: 16 additions & 13 deletions source/webview/package.d
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ enum WindowSizeHint
Fixed
}

struct CallbackContext(T)
{
T func;
void* data = null;
}

struct WebView
{
private webview_t webview;
Expand All @@ -57,21 +63,19 @@ struct WebView
webview_terminate(this.webview);
}

void dispatch(DispatchCallback fn, void* arg)
void dispatch(CallbackContext!(DispatchCallback)* ctx)
{
struct RegisterCallback
{
static WebView _this;
static DispatchCallback Callback;
extern(C) static void call(webview_t w, void* arg)
{
_this.webview = w;
Callback(_this, arg);
CallbackContext!(DispatchCallback)* cb = cast(CallbackContext!(DispatchCallback)*)arg;
WebView webview;
webview.webview = w;
cb.func(webview, cb.data);
}
};
RegisterCallback._this = this;
RegisterCallback.Callback = fn;
webview_dispatch(this.webview, &RegisterCallback.call, arg);
webview_dispatch(this.webview, &RegisterCallback.call, ctx);
}

void* getWindow()
Expand Down Expand Up @@ -109,18 +113,17 @@ struct WebView
webview_eval(this.webview, js.toStringz);
}

void bind(string name, BindCallback fn, void* arg)
void bind(string name, CallbackContext!(BindCallback)* ctx)
{
struct RegisterCallback
{
static BindCallback Callback;
extern(C) static void call(const(char)* seq, const(char)* req, void* arg)
{
Callback(cast(string)(seq.fromStringz), cast(string)(req.fromStringz), arg);
CallbackContext!(BindCallback)* cb = cast(CallbackContext!(BindCallback)*)arg;
cb.func(cast(string)(seq.fromStringz), cast(string)(req.fromStringz), cb.data);
}
};
RegisterCallback.Callback = fn;
webview_bind(this.webview, name.toStringz, &RegisterCallback.call, arg);
webview_bind(this.webview, name.toStringz, &RegisterCallback.call, ctx);
}

void unbind(string name)
Expand Down