Skip to content

Commit

Permalink
Added WebView2 wrapper and threadsafe postWebMessageAsJson (#1941)
Browse files Browse the repository at this point in the history
  • Loading branch information
DocMoebiuz authored Feb 8, 2025
1 parent 4202cf6 commit fc39181
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 6 deletions.
8 changes: 5 additions & 3 deletions MobiFlight/BrowserMessages/Publisher/PostMessagePublisher.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
using Microsoft.Web.WebView2.Core;
using Newtonsoft.Json;
using System;
using System.Threading;

namespace MobiFlight.BrowserMessages.Publisher
{
public class PostMessagePublisher : IMessagePublisher
{
private readonly CoreWebView2 _webView;
private readonly ThreadSafeWebView2 _webView;
private Action<object> _onMessageReceived;

public PostMessagePublisher(CoreWebView2 webView)
public PostMessagePublisher(ThreadSafeWebView2 webView)
{
_webView = webView;
_webView.WebMessageReceived += WebView_WebMessageReceived;
Expand All @@ -21,7 +22,8 @@ public void Publish<TEvent>(TEvent eventToPublish)
{
var message = new Message<TEvent>() { key = eventToPublish.GetType().Name, payload = eventToPublish };
var jsonMessage = JsonConvert.SerializeObject(message);
_webView.PostWebMessageAsJson(jsonMessage);
// Ensure the call is made on the UI thread
_webView.PostWebMessageAsJsonThreadSafe(jsonMessage);
}
}

Expand Down
20 changes: 20 additions & 0 deletions MobiFlight/ThreadSafeWebView2.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Microsoft.Web.WebView2.WinForms;
using System;

namespace MobiFlight
{
public class ThreadSafeWebView2 : WebView2
{
public void PostWebMessageAsJsonThreadSafe(string jsonMessage)
{
if (this.InvokeRequired)
{
this.Invoke(new Action(() => CoreWebView2.PostWebMessageAsJson(jsonMessage)));
}
else
{
CoreWebView2.PostWebMessageAsJson(jsonMessage);
}
}
}
}
3 changes: 3 additions & 0 deletions MobiFlightConnector.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,9 @@
<Compile Include="MobiFlight\OutputConfig\SimConnectValue.cs" />
<Compile Include="HubHop\Msfs2020HubhopPresetList.cs" />
<Compile Include="MobiFlight\PortDetails.cs" />
<Compile Include="MobiFlight\ThreadSafeWebView2.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="SimConnectMSFS\SimConnectCacheInterface.cs" />
<Compile Include="SimConnectMSFS\WasmModuleClient.cs" />
<Compile Include="SimConnectMSFS\SimConnectDefinitions.cs" />
Expand Down
4 changes: 2 additions & 2 deletions UI/Panels/FrontendPanel.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion UI/Panels/FrontendPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ async void InitializeAsync()
FrontendWebView.CoreWebView2.WebMessageReceived += CoreWebView2_WebMessageReceived;
FrontendWebView.CoreWebView2.DOMContentLoaded += CoreWebView2_DOMContentLoaded;

MessageExchange.Instance.SetPublisher(new PostMessagePublisher(FrontendWebView.CoreWebView2));
MessageExchange.Instance.SetPublisher(new PostMessagePublisher(FrontendWebView));
}

private void CoreWebView2_DOMContentLoaded(object sender, CoreWebView2DOMContentLoadedEventArgs e)
Expand Down

0 comments on commit fc39181

Please sign in to comment.