Skip to content

Commit d546ada

Browse files
authored
v2.0.0-stable
1 parent 15dad27 commit d546ada

7 files changed

Lines changed: 3285 additions & 14 deletions

src/NetWebView2Lib.csproj

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
<ErrorReport>prompt</ErrorReport>
2525
<WarningLevel>2</WarningLevel>
2626
<RegisterForComInterop>true</RegisterForComInterop>
27-
<DocumentationFile>bin\Debug\NetWebView2Lib.xml</DocumentationFile>
27+
<DocumentationFile>
28+
</DocumentationFile>
2829
</PropertyGroup>
2930
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
3031
<DebugType>pdbonly</DebugType>
@@ -34,6 +35,8 @@
3435
<ErrorReport>prompt</ErrorReport>
3536
<WarningLevel>4</WarningLevel>
3637
<RegisterForComInterop>true</RegisterForComInterop>
38+
<DocumentationFile>
39+
</DocumentationFile>
3740
</PropertyGroup>
3841
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
3942
<DebugSymbols>true</DebugSymbols>
@@ -77,14 +80,14 @@
7780
<ErrorReport>prompt</ErrorReport>
7881
</PropertyGroup>
7982
<ItemGroup>
80-
<Reference Include="Microsoft.Web.WebView2.Core, Version=1.0.3650.58, Culture=neutral, PublicKeyToken=2a8ab48044d2601e, processorArchitecture=MSIL">
81-
<HintPath>packages\Microsoft.Web.WebView2.1.0.3650.58\lib\net462\Microsoft.Web.WebView2.Core.dll</HintPath>
83+
<Reference Include="Microsoft.Web.WebView2.Core, Version=1.0.2739.15, Culture=neutral, PublicKeyToken=2a8ab48044d2601e, processorArchitecture=MSIL">
84+
<HintPath>packages\Microsoft.Web.WebView2.1.0.2739.15\lib\net462\Microsoft.Web.WebView2.Core.dll</HintPath>
8285
</Reference>
83-
<Reference Include="Microsoft.Web.WebView2.WinForms, Version=1.0.3650.58, Culture=neutral, PublicKeyToken=2a8ab48044d2601e, processorArchitecture=MSIL">
84-
<HintPath>packages\Microsoft.Web.WebView2.1.0.3650.58\lib\net462\Microsoft.Web.WebView2.WinForms.dll</HintPath>
86+
<Reference Include="Microsoft.Web.WebView2.WinForms, Version=1.0.2739.15, Culture=neutral, PublicKeyToken=2a8ab48044d2601e, processorArchitecture=MSIL">
87+
<HintPath>packages\Microsoft.Web.WebView2.1.0.2739.15\lib\net462\Microsoft.Web.WebView2.WinForms.dll</HintPath>
8588
</Reference>
86-
<Reference Include="Microsoft.Web.WebView2.Wpf, Version=1.0.3650.58, Culture=neutral, PublicKeyToken=2a8ab48044d2601e, processorArchitecture=MSIL">
87-
<HintPath>packages\Microsoft.Web.WebView2.1.0.3650.58\lib\net462\Microsoft.Web.WebView2.Wpf.dll</HintPath>
89+
<Reference Include="Microsoft.Web.WebView2.Wpf, Version=1.0.2739.15, Culture=neutral, PublicKeyToken=2a8ab48044d2601e, processorArchitecture=MSIL">
90+
<HintPath>packages\Microsoft.Web.WebView2.1.0.2739.15\lib\net462\Microsoft.Web.WebView2.Wpf.dll</HintPath>
8891
</Reference>
8992
<Reference Include="Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
9093
<HintPath>packages\Newtonsoft.Json.13.0.4\lib\net45\Newtonsoft.Json.dll</HintPath>
@@ -103,12 +106,12 @@
103106
<ItemGroup>
104107
<Compile Include="AssemblyUtils.cs" />
105108
<Compile Include="BasicAuthenticationRequestedEventArgsWrapper.cs" />
106-
<Compile Include="JsonParser.cs" />
109+
<Compile Include="WebView2Parser.cs" />
107110
<Compile Include="ProcessFailedEventArgsWrapper.cs" />
108111
<Compile Include="WebView2AcceleratorKeyPressedEventArgs.cs" />
109-
<Compile Include="WebViewBridge.cs" />
112+
<Compile Include="WebView2Bridge.cs" />
110113
<Compile Include="Properties\AssemblyInfo.cs" />
111-
<Compile Include="WebViewManager.cs" />
114+
<Compile Include="WebView2Manager.cs" />
112115
</ItemGroup>
113116
<ItemGroup>
114117
<None Include="packages.config" />
@@ -120,5 +123,7 @@
120123
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
121124
</PropertyGroup>
122125
<Error Condition="!Exists('packages\Microsoft.Web.WebView2.1.0.3650.58\build\Microsoft.Web.WebView2.targets')" Text="$([System.String]::Format('$(ErrorText)', 'packages\Microsoft.Web.WebView2.1.0.3650.58\build\Microsoft.Web.WebView2.targets'))" />
126+
<Error Condition="!Exists('packages\Microsoft.Web.WebView2.1.0.2739.15\build\Microsoft.Web.WebView2.targets')" Text="$([System.String]::Format('$(ErrorText)', 'packages\Microsoft.Web.WebView2.1.0.2739.15\build\Microsoft.Web.WebView2.targets'))" />
123127
</Target>
128+
<Import Project="packages\Microsoft.Web.WebView2.1.0.2739.15\build\Microsoft.Web.WebView2.targets" Condition="Exists('packages\Microsoft.Web.WebView2.1.0.2739.15\build\Microsoft.Web.WebView2.targets')" />
124129
</Project>

src/Properties/AssemblyInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,4 @@
3131
//
3232
[assembly: AssemblyVersion("2.0.0.0")]
3333
[assembly: AssemblyFileVersion("2.0.0.0")]
34-
[assembly: AssemblyInformationalVersion("2.0.0-beta.3")]
34+
[assembly: AssemblyInformationalVersion("2.0.0-stable")]

src/WebView2AcceleratorKeyPressedEventArgs.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public interface IWebView2AcceleratorKeyPressedEventArgs
3737
public class WebView2AcceleratorKeyPressedEventArgs : IWebView2AcceleratorKeyPressedEventArgs
3838
{
3939
private readonly CoreWebView2AcceleratorKeyPressedEventArgs _args;
40-
private readonly WebViewManager _manager;
40+
private readonly WebView2Manager _manager;
4141

4242
public uint VirtualKey { get; }
4343
public int KeyEventLParam { get; }
@@ -71,7 +71,7 @@ public void Block()
7171
try { _args.Handled = true; } catch { }
7272
}
7373

74-
public WebView2AcceleratorKeyPressedEventArgs(CoreWebView2AcceleratorKeyPressedEventArgs args, WebViewManager manager)
74+
public WebView2AcceleratorKeyPressedEventArgs(CoreWebView2AcceleratorKeyPressedEventArgs args, WebView2Manager manager)
7575
{
7676
_args = args;
7777
_manager = manager;

src/WebView2Bridge.cs

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
using System;
2+
using System.Runtime.InteropServices;
3+
using System.Threading;
4+
5+
// --- Version 2.0.0-beta.3 ---
6+
// Breaking Change: Sender-Aware Events (Issue #52)
7+
8+
namespace NetWebView2Lib
9+
{
10+
/// <summary>
11+
/// Delegate for detecting when messages are received from JavaScript.
12+
/// v2.0.0: Now includes sender object and parent window handle.
13+
/// </summary>
14+
/// <param name="sender">The WebViewManager instance that owns this bridge.</param>
15+
/// <param name="parentHandle">The parent window handle (Advanced Window Description).</param>
16+
/// <param name="message">The message content.</param>
17+
[ComVisible(true)]
18+
public delegate void OnMessageReceivedDelegate(object sender, string parentHandle, string message);
19+
20+
/// <summary>
21+
/// Event interface for receiving messages from JavaScript via AutoIt.
22+
/// v2.0.0: Sender-Aware pattern.
23+
/// </summary>
24+
[Guid("3E4F5A6B-7C8D-9E0F-1A2B-3C4D5E6F7A8B")]
25+
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
26+
[ComVisible(true)]
27+
public interface IBridgeEvents
28+
{
29+
/// <summary>
30+
/// Triggered when a message is received from JavaScript.
31+
/// </summary>
32+
/// <param name="sender">The WebViewManager instance.</param>
33+
/// <param name="parentHandle">The parent window handle.</param>
34+
/// <param name="message">The message content.</param>
35+
[DispId(1)]
36+
void OnMessageReceived(object sender, string parentHandle, string message);
37+
}
38+
39+
/// <summary>
40+
/// Action interface for sending messages from JavaScript to AutoIt.
41+
/// </summary>
42+
[Guid("2D3E4F5A-6A7A-4A9B-8C7D-2E3F4A5B6C7D")]
43+
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
44+
[ComVisible(true)]
45+
public interface IBridgeActions
46+
{
47+
/// <summary>
48+
/// Send a message to AutoIt.
49+
/// </summary>
50+
/// <param name="message">The message to send.</param>
51+
[DispId(1)]
52+
void RaiseMessage(string message);
53+
54+
/// <summary>Gets the version of the DLL.</summary>
55+
[DispId(2)] string Version { get; }
56+
}
57+
58+
/// <summary>
59+
/// Implementation of the bridge between WebView2 JavaScript and AutoIt.
60+
/// v2.0.0: Supports Sender-Aware event pattern.
61+
/// </summary>
62+
[Guid("8F9A0B1C-2D3E-4F5A-6B7C-8D9E0F1A2B3C")]
63+
[ClassInterface(ClassInterfaceType.None)]
64+
[ComSourceInterfaces(typeof(IBridgeEvents))]
65+
[ComVisible(true)]
66+
[ProgId("NetWebView2Lib.WebView2Bridge")]
67+
public class WebView2Bridge : IBridgeActions
68+
{
69+
/// <summary>
70+
/// Event fired when a message is received from JavaScript.
71+
/// </summary>
72+
public event OnMessageReceivedDelegate OnMessageReceived;
73+
74+
private SynchronizationContext _syncContext;
75+
private readonly System.Diagnostics.Stopwatch _throttleStopwatch = System.Diagnostics.Stopwatch.StartNew();
76+
private long _lastMessageTicks = 0;
77+
private const long ThrottlingIntervalTicks = TimeSpan.TicksPerSecond / 50; // max 50 calls/sec
78+
79+
// v2.0.0: Parent context for Sender-Aware events
80+
private object _sender;
81+
private string _parentHandle;
82+
83+
/// <summary>
84+
/// Initializes a new instance of the WebView2Bridge class.
85+
/// </summary>
86+
public WebView2Bridge()
87+
{
88+
_syncContext = SynchronizationContext.Current ?? new SynchronizationContext();
89+
}
90+
91+
/// <summary>
92+
/// Sets the parent context for Sender-Aware events.
93+
/// Must be called immediately after construction.
94+
/// </summary>
95+
public void SetParentContext(object sender, object hwnd)
96+
{
97+
_sender = sender;
98+
99+
if (hwnd is IntPtr ptr)
100+
{
101+
_parentHandle = "[HANDLE:0x" + ptr.ToString("X").PadLeft(IntPtr.Size * 2, '0') + "]";
102+
}
103+
else
104+
{
105+
_parentHandle = hwnd?.ToString() ?? "[HANDLE:0x" + IntPtr.Zero.ToString("X").PadLeft(IntPtr.Size * 2, '0') + "]";
106+
}
107+
}
108+
109+
/// <summary>
110+
/// Updates the SynchronizationContext used for raising events.
111+
/// </summary>
112+
/// <param name="context">The new context.</param>
113+
public void SetSyncContext(SynchronizationContext context)
114+
{
115+
_syncContext = context;
116+
}
117+
118+
/// <summary>
119+
/// Send a message from JavaScript to AutoIt.
120+
/// v2.0.0: Now passes sender and parentHandle to the event.
121+
/// </summary>
122+
/// <param name="message">The message content.</param>
123+
public void RaiseMessage(string message)
124+
{
125+
if (OnMessageReceived != null)
126+
{
127+
// Throttling: Max 50 messages per second (Law #2: Performance)
128+
long currentTicks = _throttleStopwatch.ElapsedTicks;
129+
if (currentTicks - _lastMessageTicks < ThrottlingIntervalTicks) return;
130+
_lastMessageTicks = currentTicks;
131+
132+
// v2.0.0: Pass sender and parentHandle for multi-instance support
133+
_syncContext?.Post(_ => OnMessageReceived?.Invoke(_sender, _parentHandle, message), null);
134+
}
135+
}
136+
137+
/// <summary>Gets the version of the DLL.</summary>
138+
public string Version => AssemblyUtils.GetVersion();
139+
}
140+
141+
/// <summary>
142+
/// Compatibility Layer for WebViewBridge (Legacy name).
143+
/// Inherits all functionality from WebView2Bridge.
144+
/// </summary>
145+
[Guid("1A2B3C4D-5E6F-4A8B-9C0D-1E2F3A4B5C6D")] // OLD GUID
146+
[ClassInterface(ClassInterfaceType.None)]
147+
[ComSourceInterfaces(typeof(IBridgeEvents))]
148+
[ComVisible(true)]
149+
[ProgId("NetWebView2Lib.WebViewBridge")] // Explicit OLD ProgID
150+
public class WebViewBridge : WebView2Bridge, IBridgeActions
151+
{
152+
// Inherits everything
153+
}
154+
}

0 commit comments

Comments
 (0)