Skip to content

Commit

Permalink
Add binding for dispatch
Browse files Browse the repository at this point in the history
  • Loading branch information
geaz committed May 17, 2020
1 parent 2f107bd commit 116591c
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 7 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.6.0] - 2020-05-17
### Add
- Add binding for webview_dispatch
- New webview version

## [0.5.5] - 2020-05-05
### Add
- Link interception for image links
Expand Down
2 changes: 1 addition & 1 deletion examples/Minimal/Minimal.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="SharpWebview" Version="0.5.5" />
<PackageReference Include="SharpWebview" Version="0.6.0" />
</ItemGroup>
<ItemGroup>
<None Update="icon.ico">
Expand Down
Binary file modified libs/webview.dll
Binary file not shown.
22 changes: 18 additions & 4 deletions src/SharpWebview/Bindings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ public enum RPCResult
Error = 1,
}

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
internal delegate void DispatchFunction(
IntPtr webview,
IntPtr args);

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
internal delegate void CallBackFunction(
[MarshalAs(UnmanagedType.LPStr)] string id,
Expand Down Expand Up @@ -155,6 +160,19 @@ internal static class Bindings
[DllImport(DllFile, CallingConvention = CallingConvention.Cdecl)]
internal extern static void webview_eval(IntPtr webview, [MarshalAs(UnmanagedType.LPStr)] string js);

/// <summary>
/// Posts a function to be executed on the main thread. You normally do not need
/// to call this function, unless you want to tweak the native window.
///
/// Binding for:
/// WEBVIEW_API void webview_dispatch(webview_t w, void (*fn)(webview_t w, void *arg), void *arg);
/// </summary>
/// <param name="webview">The webview to dispatch the function to</param>
/// <param name="dispatchFunction">The function to execute on the webview thread</param>
/// <param name="args">Paramters to pass to the dispatched function</param>
[DllImport(DllFile, CallingConvention = CallingConvention.Cdecl)]
internal extern static void webview_dispatch(IntPtr webview, DispatchFunction dispatchFunction, IntPtr args);

/// <summary>
/// Binds a native C callback so that it will appear under the given name as a
/// global JavaScript function. Internally it uses webview_init(). Callback
Expand Down Expand Up @@ -196,10 +214,6 @@ internal extern static void webview_return(IntPtr webview,

/*
Not mapped:
// Posts a function to be executed on the main thread. You normally do not need
// to call this function, unless you want to tweak the native window.
WEBVIEW_API void
webview_dispatch(webview_t w, void (*fn)(webview_t w, void *arg), void *arg);
// Returns a native window handle pointer. When using GTK backend the pointer
// is GtkWindow pointer, when using Cocoa backend the pointer is NSWindow
Expand Down
2 changes: 1 addition & 1 deletion src/SharpWebview/SharpWebview.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<Owners>Gerrit 'Geaz' Gazic</Owners>
<Description>C# bindings for zserge/webview - Batteries included</Description>
<PackageId>SharpWebview</PackageId>
<PackageVersion>0.5.5</PackageVersion>
<PackageVersion>0.6.0</PackageVersion>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageProjectUrl>https://github.com/geaz/sharpWebview</PackageProjectUrl>
</PropertyGroup>
Expand Down
14 changes: 13 additions & 1 deletion src/SharpWebview/Webview.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using SharpWebview.Content;
using System.Diagnostics;
using System.Collections.Generic;
using System.Runtime.InteropServices;

namespace SharpWebview
{
Expand Down Expand Up @@ -96,7 +97,7 @@ public Webview Navigate(IWebviewContent webviewContent)
/// <returns>The webview object for a fluent api.</returns>
public Webview Bind(string name, Action<string, string> callback)
{
var callbackInstance = new CallBackFunction((id, req, arg) => { callback(id, req); });
var callbackInstance = new CallBackFunction((id, req, _) => callback(id, req));
callbacks.Add(callbackInstance); // Pin the callback for the GC

Bindings.webview_bind(_nativeWebview, name, callbackInstance, IntPtr.Zero);
Expand Down Expand Up @@ -135,6 +136,16 @@ public void Evaluate(string javascript)
Bindings.webview_eval(_nativeWebview, javascript);
}

/// <summary>
/// Posts a function to be executed on the main thread of the webview.
/// </summary>
/// <param name="dispatchFunc">The function to call on the main thread</param>
public void Dispatch(Action dispatchFunc)
{
var dispatchFuncInstance = new DispatchFunction((_, __) => dispatchFunc());
Bindings.webview_dispatch(_nativeWebview, dispatchFuncInstance, IntPtr.Zero);
}

/// <summary>
/// Disposes the current webview.
/// </summary>
Expand All @@ -148,6 +159,7 @@ protected virtual void Dispose(bool disposing)
{
if (!_disposed)
{
Bindings.webview_terminate(_nativeWebview);
Bindings.webview_destroy(_nativeWebview);
_disposed = true;
}
Expand Down

0 comments on commit 116591c

Please sign in to comment.