Skip to content

Commit 9312863

Browse files
authored
v2.2.1-alpha
1 parent 094fbc7 commit 9312863

4 files changed

Lines changed: 132 additions & 46 deletions

File tree

src/Core/WebView2Manager.Actions.cs

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -168,21 +168,33 @@ public void Print()
168168
});
169169
}
170170

171+
private bool _isLocked = false;
172+
171173
public void SetLockState(bool lockState)
172174
{
175+
if (_isLocked == lockState) return; // Skip if already in this state
176+
173177
InvokeOnUiThread(() => {
174178
if (_webView?.CoreWebView2 != null) {
175179
var s = _webView.CoreWebView2.Settings;
176-
s.AreDefaultContextMenusEnabled = !lockState;
177-
s.AreDevToolsEnabled = !lockState;
178-
s.IsZoomControlEnabled = !lockState;
179-
s.IsBuiltInErrorPageEnabled = !lockState;
180-
s.AreDefaultScriptDialogsEnabled = !lockState;
181-
s.AreBrowserAcceleratorKeysEnabled = !lockState;
182-
s.IsStatusBarEnabled = !lockState;
180+
Log($"SetLockState: {lockState} (Interception Mode)");
181+
182+
// PERMANENT ENGINE SETTINGS (to ensure they are ALWAYS available and snappy)
183+
s.AreDefaultContextMenusEnabled = true;
184+
s.AreDevToolsEnabled = true;
185+
s.AreBrowserAcceleratorKeysEnabled = true;
186+
187+
// Volatile settings that we still toggle at engine level
188+
s.IsZoomControlEnabled = lockState ? false : _isZoomControlEnabled;
189+
s.IsBuiltInErrorPageEnabled = lockState ? false : _isBuiltInErrorPageEnabled;
190+
s.AreDefaultScriptDialogsEnabled = lockState ? false : _areDefaultScriptDialogsEnabled;
191+
s.IsStatusBarEnabled = lockState ? false : _isStatusBarEnabled;
183192
}
184-
_areBrowserPopupsAllowed = !lockState;
185-
_contextMenuEnabled = !lockState;
193+
194+
// Set the internal flag. Our event handlers (Events.cs) will check this
195+
// to decide whether to Allow or Block F12/Right-Click.
196+
_isLocked = lockState;
197+
Log($"SetLockState: Results -> LockState={_isLocked}");
186198
});
187199
}
188200

@@ -377,12 +389,6 @@ public void RemoveExtension(string extensionId)
377389
#endregion
378390

379391
#region 16. UNIFIED SETTINGS (PROPERTIES)
380-
public bool AreDevToolsEnabled
381-
{
382-
get => RunOnUiThread(() => _webView?.CoreWebView2?.Settings?.AreDevToolsEnabled ?? false);
383-
set => InvokeOnUiThread(() => { if (_webView?.CoreWebView2?.Settings != null) _webView.CoreWebView2.Settings.AreDevToolsEnabled = value; });
384-
}
385-
386392
public bool AreBrowserPopupsAllowed
387393
{
388394
get => _areBrowserPopupsAllowed;
@@ -403,20 +409,20 @@ public bool Verbose
403409

404410
public bool AreDefaultScriptDialogsEnabled
405411
{
406-
get => RunOnUiThread(() => _webView?.CoreWebView2?.Settings?.AreDefaultScriptDialogsEnabled ?? true);
407-
set => InvokeOnUiThread(() => { if (_webView?.CoreWebView2?.Settings != null) _webView.CoreWebView2.Settings.AreDefaultScriptDialogsEnabled = value; });
412+
get => _areDefaultScriptDialogsEnabled;
413+
set { _areDefaultScriptDialogsEnabled = value; InvokeOnUiThread(() => { if (_webView?.CoreWebView2?.Settings != null) _webView.CoreWebView2.Settings.AreDefaultScriptDialogsEnabled = value; }); }
408414
}
409415

410416
public bool AreBrowserAcceleratorKeysEnabled
411417
{
412-
get => RunOnUiThread(() => _webView?.CoreWebView2?.Settings?.AreBrowserAcceleratorKeysEnabled ?? true);
413-
set => InvokeOnUiThread(() => { if (_webView?.CoreWebView2?.Settings != null) _webView.CoreWebView2.Settings.AreBrowserAcceleratorKeysEnabled = value; });
418+
get => _areBrowserAcceleratorKeysEnabled;
419+
set { _areBrowserAcceleratorKeysEnabled = value; InvokeOnUiThread(() => { if (_webView?.CoreWebView2?.Settings != null) _webView.CoreWebView2.Settings.AreBrowserAcceleratorKeysEnabled = value; }); }
414420
}
415421

416422
public bool IsStatusBarEnabled
417423
{
418-
get => RunOnUiThread(() => _webView?.CoreWebView2?.Settings?.IsStatusBarEnabled ?? true);
419-
set => SetStatusBarEnabled(value);
424+
get => _isStatusBarEnabled;
425+
set { _isStatusBarEnabled = value; InvokeOnUiThread(() => { if (_webView?.CoreWebView2?.Settings != null) _webView.CoreWebView2.Settings.IsStatusBarEnabled = value; }); }
420426
}
421427

422428
public double ZoomFactor
@@ -433,13 +439,19 @@ public string BackColor
433439
catch { _webView.DefaultBackgroundColor = Color.White; }
434440
});
435441
}
436-
442+
437443
public bool AreHostObjectsAllowed
438444
{
439445
get => RunOnUiThread(() => _webView?.CoreWebView2?.Settings?.AreHostObjectsAllowed ?? true);
440446
set => InvokeOnUiThread(() => { if (_webView?.CoreWebView2?.Settings != null) _webView.CoreWebView2.Settings.AreHostObjectsAllowed = value; });
441447
}
442448

449+
public bool AreDevToolsEnabled
450+
{
451+
get => _areDevToolsEnabled;
452+
set { _areDevToolsEnabled = value; InvokeOnUiThread(() => { if (_webView?.CoreWebView2?.Settings != null) _webView.CoreWebView2.Settings.AreDevToolsEnabled = value; }); }
453+
}
454+
443455
public int Anchor
444456
{
445457
get => RunOnUiThread(() => (int)_webView.Anchor);
@@ -469,8 +481,8 @@ public bool IsZoomControlEnabled
469481

470482
public bool IsBuiltInErrorPageEnabled
471483
{
472-
get => RunOnUiThread(() => _webView?.CoreWebView2?.Settings?.IsBuiltInErrorPageEnabled ?? true);
473-
set => InvokeOnUiThread(() => { if (_webView?.CoreWebView2?.Settings != null) _webView.CoreWebView2.Settings.IsBuiltInErrorPageEnabled = value; });
484+
get => _isBuiltInErrorPageEnabled;
485+
set { _isBuiltInErrorPageEnabled = value; InvokeOnUiThread(() => { if (_webView?.CoreWebView2?.Settings != null) _webView.CoreWebView2.Settings.IsBuiltInErrorPageEnabled = value; }); }
474486
}
475487
#endregion
476488

src/Core/WebView2Manager.Events.cs

Lines changed: 74 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,39 @@ private void RegisterEvents()
9595
// Context Menu Event
9696
_webView.CoreWebView2.ContextMenuRequested += async (sender, args) =>
9797
{
98+
// FAST PATH: If context menus are enabled and the user isn't listening for details,
99+
// just let the browser handle it immediately.
100+
// 1. LOCK CHECK: If manager is locked (e.g. during Nav), block immediately
101+
if (_isLocked)
102+
{
103+
args.Handled = true;
104+
return;
105+
}
106+
107+
// 2. FAST PATH: If context menus are enabled and no custom listeners, allow default immediately.
108+
// This ensures "Inspect" and other standard items appear without lag.
109+
if (_contextMenuEnabled && OnContextMenuRequested == null && OnContextMenu == null)
110+
{
111+
args.Handled = false;
112+
return;
113+
}
114+
115+
// Log the request for custom processing
98116
Log($"ContextMenuRequested: Location={args.Location.X}x{args.Location.Y}, Target={args.ContextMenuTarget.Kind}");
117+
118+
// 3. SLOW PATH (Custom Handlers or Disabled Prefs)
119+
// Set handled state based on user preference
99120
args.Handled = !_contextMenuEnabled;
100121

122+
// If handled (user preference is disabled), we don't need to do more
123+
if (args.Handled) return;
124+
125+
// Only proceed with script and deferral if someone is actually listening
126+
var deferral = args.GetDeferral();
127+
101128
try
102129
{
130+
// To get the tagName, we need to query the DOM
103131
string script = "document.elementFromPoint(" + args.Location.X + "," + args.Location.Y + ").closest('table') ? 'TABLE' : document.elementFromPoint(" + args.Location.X + "," + args.Location.Y + ").tagName";
104132
string tagNameResult = await _webView.CoreWebView2.ExecuteScriptAsync(script);
105133

@@ -112,24 +140,28 @@ private void RegisterEvents()
112140

113141
OnContextMenuRequested?.Invoke(this, FormatHandle(_parentHandle), lnk, args.Location.X, args.Location.Y, sel);
114142

115-
string cleanSrc = src.Replace("\"", "\\\"");
116-
string cleanLnk = lnk.Replace("\"", "\\\"");
117-
string cleanSel = sel.Replace("\"", "\\\"").Replace("\r", "").Replace("\n", "\\n");
118-
119-
string json = "{" +
120-
"\"x\":" + args.Location.X + "," +
121-
"\"y\":" + args.Location.Y + "," +
122-
"\"kind\":\"" + k + "\"," +
123-
"\"tagName\":\"" + tagName + "\"," +
124-
"\"src\":\"" + cleanSrc + "\"," +
125-
"\"link\":\"" + cleanLnk + "\"," +
126-
"\"selection\":\"" + cleanSel + "\"" +
127-
"}";
128-
129-
OnContextMenu?.Invoke(this, FormatHandle(_parentHandle), "JSON:" + json);
143+
if (OnContextMenu != null)
144+
{
145+
string cleanSrc = src.Replace("\"", "\\\"");
146+
string cleanLnk = lnk.Replace("\"", "\\\"");
147+
string cleanSel = sel.Replace("\"", "\\\"").Replace("\r", "").Replace("\n", "\\n");
148+
149+
string json = "{" +
150+
"\"x\":" + args.Location.X + "," +
151+
"\"y\":" + args.Location.Y + "," +
152+
"\"kind\":\"" + k + "\"," +
153+
"\"tagName\":\"" + tagName + "\"," +
154+
"\"src\":\"" + cleanSrc + "\"," +
155+
"\"link\":\"" + cleanLnk + "\"," +
156+
"\"selection\":\"" + cleanSel + "\"" +
157+
"}";
158+
159+
OnContextMenu?.Invoke(this, FormatHandle(_parentHandle), "JSON:" + json);
160+
}
130161
});
131162
}
132163
catch (Exception ex) { Debug.WriteLine("ContextMenu Error: " + ex.Message); }
164+
finally { deferral.Complete(); }
133165
};
134166

135167
// Ad Blocking
@@ -318,6 +350,30 @@ private void RegisterEvents()
318350
{
319351
controller.AcceleratorKeyPressed += (s, e) =>
320352
{
353+
// 1. LOCK CHECK: If manager is locked, block F12/shortcuts
354+
if (_isLocked)
355+
{
356+
e.Handled = true;
357+
return;
358+
}
359+
360+
// 2. PREFERENCE CHECK: If F12 and DevTools are disabled by user, block it.
361+
if (e.VirtualKey == 123 && !_areDevToolsEnabled)
362+
{
363+
e.Handled = true;
364+
return;
365+
}
366+
367+
// 3. FAST PATH: If DevTools are enabled and F12 is pressed, allow it immediately.
368+
if (_areDevToolsEnabled && e.VirtualKey == 123)
369+
{
370+
if (string.IsNullOrEmpty(_blockedVirtualKeys) || !_blockedVirtualKeys.Split(',').Any(k => k.Trim() == "123"))
371+
{
372+
e.Handled = false;
373+
return;
374+
}
375+
}
376+
321377
var eventArgs = new WebView2AcceleratorKeyPressedEventArgs(e, this);
322378
if (!string.IsNullOrEmpty(_blockedVirtualKeys))
323379
{
@@ -328,7 +384,10 @@ private void RegisterEvents()
328384
eventArgs.Block();
329385
}
330386
}
387+
388+
// Only invoke if someone is listening or if it's not a standard system key
331389
OnAcceleratorKeyPressed?.Invoke(this, FormatHandle(_parentHandle), eventArgs);
390+
332391
if (eventArgs.IsCurrentlyHandled) e.Handled = true;
333392
};
334393
}

src/Core/WebView2Manager.Main.cs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ public partial class WebView2Manager : IWebViewActions
4141
private bool _httpStatusCodeDocumentOnly = true;
4242
private bool _isDownloadHandledOverride = false;
4343
private bool _isZoomControlEnabled = true;
44+
private bool _areDevToolsEnabled = true;
45+
private bool _areBrowserAcceleratorKeysEnabled = true;
46+
private bool _isStatusBarEnabled = true;
47+
private bool _isBuiltInErrorPageEnabled = true;
48+
private bool _areDefaultScriptDialogsEnabled = true;
4449
private string _failureReportFolderPath = "";
4550

4651
private int _offsetX = 0;
@@ -282,11 +287,21 @@ await _webView.CoreWebView2.AddScriptToExecuteOnDocumentCreatedAsync(@"
282287
/// </summary>
283288
private void ConfigureSettings()
284289
{
290+
if (_webView?.CoreWebView2 == null) return;
285291
var settings = _webView.CoreWebView2.Settings;
286292
settings.IsWebMessageEnabled = true; // Enable Web Messages
287-
settings.AreDevToolsEnabled = true; // Enable DevTools by default
288-
settings.AreDefaultContextMenusEnabled = true; // Keep TRUE to ensure the event fires
289-
settings.IsZoomControlEnabled = _isZoomControlEnabled; // Apply custom zoom setting
293+
294+
// PERMANENT ENGINE SETTINGS (Managed via Events.cs interception)
295+
settings.AreDevToolsEnabled = true;
296+
settings.AreDefaultContextMenusEnabled = true;
297+
settings.AreBrowserAcceleratorKeysEnabled = true;
298+
299+
// VOLATILE USER PREFERENCES (Still safe to toggle at engine level)
300+
settings.IsZoomControlEnabled = _isZoomControlEnabled;
301+
settings.IsStatusBarEnabled = _isStatusBarEnabled;
302+
settings.IsBuiltInErrorPageEnabled = _isBuiltInErrorPageEnabled;
303+
settings.AreDefaultScriptDialogsEnabled = _areDefaultScriptDialogsEnabled;
304+
290305
_webView.DefaultBackgroundColor = Color.Transparent;
291306
}
292307
#endregion

src/Properties/AssemblyInfo.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Reflection;
1+
using System.Reflection;
22
using System.Runtime.InteropServices;
33

44
// General Information about an assembly is controlled through the following
@@ -28,6 +28,6 @@
2828
// Build Number
2929
// Revision
3030
//
31-
[assembly: AssemblyVersion("2.2.0.0")]
32-
[assembly: AssemblyFileVersion("2.2.0.0")]
33-
[assembly: AssemblyInformationalVersion("2.2.0-alpha")]
31+
[assembly: AssemblyVersion("2.2.1.0")]
32+
[assembly: AssemblyFileVersion("2.2.1.0")]
33+
[assembly: AssemblyInformationalVersion("2.2.1-alpha")]

0 commit comments

Comments
 (0)