Browser mai bianco né muto: pagina iniziale, finestra che resta, fotogrammi che ripartono (#52)
Il browser di Febe a volte era bianco o non mostrava nulla. Le cause erano
diverse e si correggono insieme.
- Pagina iniziale. Se il browser è a riposo su about:blank e Alberto lo apre
(«Usa il browser», «Apri il browser»), Febe lo porta sulla home del sito
quando è collegato, altrimenti sulla pagina di accesso. È un'azione di
Alberto, sotto il suo permesso di guida, non del motore: Febe aspetta che
la sessione finisca e prende il turno della pagina.
Il nuovo pulsante «Pagina iniziale» sta nella barra della finestra di
Windows e nella vista remota. Su about:blank la barra dice «Il browser è a
riposo su una pagina vuota». «Indietro» non torna più sulla pagina di
riposo.
- Disconnessione. «Apri il browser» nella striscia rossa apre la pagina di
accesso. Con --accesso-di-prova il primo controllo all'avvio non segnala
più una disconnessione finta.
- Windows: la finestra resta.
- Lasciare una pagina di Febe non nasconde più la finestra «Browser di
Febe» e non la rimette a riposo: finisce solo il permesso tenuto dalla
pagina (cancel con pageLeft, IConnectionService.ReleaseAsync).
- Sullo schermo la finestra ha il suo pulsante nella barra delle
applicazioni, così ridurla a icona non la fa sparire. Dopo 2 minuti
ridotta a icona torna fuori vista (S2).
- Se lo schermo è troppo piccolo, la finestra si restringe all'area di
lavoro e la pagina si rimpicciolisce con ZoomFactor, con le stesse misure
CSS.
- I bordi della pagina si riapplicano, con NotifyParentWindowPositionChanged,
quando la finestra si mostra, si sposta o riparte il processo GPU.
- Una pagina che non risponde mentre Alberto guida resta com'è.
- IBrowserWindow.ShowAsync lascia il posto a
IConnectionService.ShowBrowserAsync.
- Docker: fotogrammi che ripartono. La vista remota ha un watchdog. Riavvia
i fotogrammi dopo una pagina nuova caricata senza fotogrammi e dopo circa
2 s di silenzio. L'attesa raddoppia fino a 30 s finché la pagina resta
ferma. Se il riavvio non porta nulla, la vista riceve un'immagine ferma
(Page.captureScreenshot, già fra i comandi ammessi).
RemoteViewSession ha ora un registro vero: avvii, riavvii e silenzi vanno
a livello Info, categoria «vista», senza indirizzi.
Se dopo 8 s non arriva nessuna immagine, l'interfaccia mostra «Nessuna
immagine dal browser» con «Riprova».
Nessun comando CDP nuovo e nessuna dipendenza nuova. Aggiornati PROTOCOLLO.md
e la guida (Primo collegamento, Attività e arresti, Installazione Windows,
Problemi comuni).
Prove nuove:
- ScreencastWatchdogTests, RemoteViewFramesTests (Chromium vero, solo CI
Linux)
- Home e Indietro in RemoteViewStartTests
- RemoteViewSocketTests
- ConnectionApiTests
- BrowserStartPageTests
- BrowserWindowDecisionTests e BrowserWindowTests (WebView2 vero)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
65c0c4470e
commit
c49df4ca7b
49 files changed
+2056
-177
No files matched your search
@@ -3,21 +3,40 @@ using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Interop;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Threading;
|
||||
|
||||
namespace Febe.Desktop.Browser;
|
||||
|
||||
/// <summary>
|
||||
/// The window «Browser di Febe» (spec §4.1, §12): the page of Instagram at a fixed size, always the same, and below it a
|
||||
/// thin bar with «Indietro», «Ricarica» and the address without its query string. WebView2 draws into the upper area
|
||||
/// (as a child window, or as a DirectComposition visual for S2). When Alberto does not use it the window stays open
|
||||
/// but out of view, never hidden nor minimised: WebView2 would stop painting (S2). Closing it only puts it out of view.
|
||||
/// thin bar with «Indietro», «Ricarica», «Pagina iniziale» and the address without its query string. WebView2 draws
|
||||
/// into the upper area (as a child window, or as a DirectComposition visual for S2). When Alberto does not use it the
|
||||
/// window stays open but out of view, never hidden: WebView2 would stop painting (S2). Closing it only puts it out of
|
||||
/// view.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// Issue #52. On screen it has a button in the taskbar, so that minimising it does not make it vanish; minimised it
|
||||
/// keeps Alberto at the wheel, but never for long (S2): after <see cref="MinimizedLimit"/> it is restored and put out of
|
||||
/// view as if he had closed it. When the page and its bar do not fit in the work area of the screen (a small screen, a
|
||||
/// large text scale) the window shrinks to fit and the page is shown zoomed out (<see cref="Fit"/>), always with the
|
||||
/// same size in CSS pixels. While the page is the empty rest page the bar says so.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
internal sealed class BrowserWindow : Window
|
||||
{
|
||||
/// <summary>Height of the bar under the page, in device-independent pixels.</summary>
|
||||
public const double BarHeight = 40;
|
||||
|
||||
/// <summary>The smallest zoom the page is shown at when the screen is small.</summary>
|
||||
public const double SmallestFit = 0.4;
|
||||
|
||||
/// <summary>What the bar says while the page is the empty rest page.</summary>
|
||||
public const string RestNotice = "Il browser è a riposo su una pagina vuota: premi «Pagina iniziale».";
|
||||
|
||||
private readonly TextBlock _address;
|
||||
private readonly Border _page;
|
||||
private readonly DispatcherTimer _minimized;
|
||||
private bool _allowClose;
|
||||
|
||||
public BrowserWindow(int pageWidth, int pageHeight, ImageSource? icon)
|
||||
@@ -31,8 +50,9 @@ internal sealed class BrowserWindow : Window
|
||||
WindowStartupLocation = WindowStartupLocation.Manual;
|
||||
ShowActivated = false;
|
||||
|
||||
// No taskbar button: out of view the window must not be reachable by accident (a click on a button that shows
|
||||
// Out of view no taskbar button: the window must not be reachable by accident (a click on a button that shows
|
||||
// nothing). It comes to the front from the interface: «Apri il browser», «Portalo in primo piano», the login.
|
||||
// On screen it has one (MoveOnScreen), so a minimised window can be brought back.
|
||||
ShowInTaskbar = false;
|
||||
Background = new SolidColorBrush(Color.FromRgb(0xF4, 0xF5, 0xF7));
|
||||
|
||||
@@ -40,6 +60,8 @@ internal sealed class BrowserWindow : Window
|
||||
back.Click += (_, _) => BackRequested?.Invoke(this, EventArgs.Empty);
|
||||
var reload = BarButton("Ricarica", "Ricarica la pagina");
|
||||
reload.Click += (_, _) => ReloadRequested?.Invoke(this, EventArgs.Empty);
|
||||
var home = BarButton("Pagina iniziale", "Apre la pagina iniziale del sito, o la pagina di accesso se non sei collegato");
|
||||
home.Click += (_, _) => HomeRequested?.Invoke(this, EventArgs.Empty);
|
||||
_address = new TextBlock
|
||||
{
|
||||
VerticalAlignment = VerticalAlignment.Center,
|
||||
@@ -57,24 +79,30 @@ internal sealed class BrowserWindow : Window
|
||||
Foreground = new SolidColorBrush(Color.FromRgb(0x5B, 0x63, 0x6E)),
|
||||
FontFamily = new FontFamily("Segoe UI"),
|
||||
FontSize = 12,
|
||||
TextTrimming = TextTrimming.CharacterEllipsis,
|
||||
};
|
||||
|
||||
var bar = new DockPanel { Height = BarHeight, LastChildFill = true, Background = new SolidColorBrush(Color.FromRgb(0xFF, 0xFF, 0xFF)) };
|
||||
DockPanel.SetDock(back, Dock.Left);
|
||||
DockPanel.SetDock(reload, Dock.Left);
|
||||
DockPanel.SetDock(home, Dock.Left);
|
||||
DockPanel.SetDock(hint, Dock.Right);
|
||||
bar.Children.Add(back);
|
||||
bar.Children.Add(reload);
|
||||
bar.Children.Add(home);
|
||||
bar.Children.Add(hint);
|
||||
bar.Children.Add(_address);
|
||||
|
||||
// The page area: WebView2 covers it (its bounds are computed from PageWidth/PageHeight and the DPI).
|
||||
var page = new Border { Width = pageWidth, Height = pageHeight, Background = Brushes.White };
|
||||
// The page area: WebView2 covers it (its bounds are computed from PageWidth/PageHeight, the fit and the DPI).
|
||||
_page = new Border { Width = pageWidth, Height = pageHeight, Background = Brushes.White };
|
||||
var layout = new DockPanel { LastChildFill = true };
|
||||
DockPanel.SetDock(bar, Dock.Bottom);
|
||||
layout.Children.Add(bar);
|
||||
layout.Children.Add(page);
|
||||
layout.Children.Add(_page);
|
||||
Content = layout;
|
||||
|
||||
_minimized = new DispatcherTimer(DispatcherPriority.Normal, Dispatcher);
|
||||
_minimized.Tick += (_, _) => MinimizedTooLong();
|
||||
}
|
||||
|
||||
/// <summary>Width of the page in device-independent pixels (CSS pixels of the page at zoom 100%).</summary>
|
||||
@@ -82,15 +110,30 @@ internal sealed class BrowserWindow : Window
|
||||
|
||||
public int PageHeight { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The zoom the page is shown at: 1, or less when the page and its bar do not fit in the work area of the screen.
|
||||
/// The page keeps its size in CSS pixels whatever the fit (the host zooms WebView2 out by the same factor).
|
||||
/// </summary>
|
||||
public double Fit { get; private set; } = 1;
|
||||
|
||||
/// <summary>How long the window may stay minimised before it is put out of view as «ho finito» (S2).</summary>
|
||||
public TimeSpan MinimizedLimit { get; set; } = TimeSpan.FromMinutes(2);
|
||||
|
||||
/// <summary>Raised by «Indietro».</summary>
|
||||
public event EventHandler? BackRequested;
|
||||
|
||||
/// <summary>Raised by «Ricarica».</summary>
|
||||
public event EventHandler? ReloadRequested;
|
||||
|
||||
/// <summary>Raised when Alberto closes or minimises the window: the owner puts it out of view.</summary>
|
||||
/// <summary>Raised by «Pagina iniziale».</summary>
|
||||
public event EventHandler? HomeRequested;
|
||||
|
||||
/// <summary>Raised when Alberto closes the window, or leaves it minimised too long: the owner puts it out of view.</summary>
|
||||
public event EventHandler? DoneRequested;
|
||||
|
||||
/// <summary>Raised when the page area changed size (a new fit): the owner sizes WebView2 again.</summary>
|
||||
public event EventHandler? PageAreaChanged;
|
||||
|
||||
/// <summary>The window handle (created on first use).</summary>
|
||||
public IntPtr Handle => new WindowInteropHelper(this).EnsureHandle();
|
||||
|
||||
@@ -98,32 +141,68 @@ internal sealed class BrowserWindow : Window
|
||||
public double Scale => VisualTreeHelper.GetDpi(this).DpiScaleX;
|
||||
|
||||
/// <summary>Where WebView2 draws, in physical pixels of the client area.</summary>
|
||||
public System.Drawing.Rectangle PageBounds => new(0, 0, (int)Math.Round(PageWidth * Scale), (int)Math.Round(PageHeight * Scale));
|
||||
public System.Drawing.Rectangle PageBounds => new(0, 0, (int)Math.Round(PageWidth * Fit * Scale), (int)Math.Round(PageHeight * Fit * Scale));
|
||||
|
||||
/// <summary>True while the window is on a screen, false while it is parked out of view.</summary>
|
||||
public bool IsOnScreen =>
|
||||
Left < SystemParameters.VirtualScreenLeft + SystemParameters.VirtualScreenWidth
|
||||
&& Left + ActualWidth > SystemParameters.VirtualScreenLeft;
|
||||
|
||||
/// <summary>Shows the address of the page, without query string or fragment (they may carry tokens).</summary>
|
||||
public void ShowAddress(string? address) => _address.Text = address ?? string.Empty;
|
||||
/// <summary>The text the bar shows now (the address, or <see cref="RestNotice"/>).</summary>
|
||||
public string AddressText => _address.Text;
|
||||
|
||||
/// <summary>Centres the window on the work area of the primary screen.</summary>
|
||||
/// <summary>
|
||||
/// The zoom at which a page of <paramref name="pageWidth"/>×<paramref name="pageHeight"/> plus its bar and the
|
||||
/// window's frame fits in an area of <paramref name="areaWidth"/>×<paramref name="areaHeight"/> (all in
|
||||
/// device-independent pixels): 1 when it fits, never below <see cref="SmallestFit"/>.
|
||||
/// </summary>
|
||||
public static double FitFor(double areaWidth, double areaHeight, double pageWidth, double pageHeight, double frameWidth, double frameHeight)
|
||||
{
|
||||
if (pageWidth <= 0 || pageHeight <= 0)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
var byWidth = (areaWidth - frameWidth) / pageWidth;
|
||||
var byHeight = (areaHeight - frameHeight - BarHeight) / pageHeight;
|
||||
var fit = Math.Min(1, Math.Min(byWidth, byHeight));
|
||||
return Math.Max(SmallestFit, Math.Floor(fit * 100) / 100);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Shows the address of the page, without query string or fragment (they may carry tokens); on the empty rest page,
|
||||
/// <see cref="RestNotice"/>.
|
||||
/// </summary>
|
||||
public void ShowAddress(string? address) =>
|
||||
_address.Text = address is null or "" or "about:blank" ? RestNotice : address;
|
||||
|
||||
/// <summary>
|
||||
/// Centres the window on the work area of the primary screen, shrunk to fit it when needed, with its taskbar button.
|
||||
/// </summary>
|
||||
public void MoveOnScreen()
|
||||
{
|
||||
var area = SystemParameters.WorkArea;
|
||||
var width = ActualWidth > 0 ? ActualWidth : PageWidth;
|
||||
var height = ActualHeight > 0 ? ActualHeight : PageHeight + BarHeight;
|
||||
FitTo(area);
|
||||
ShowInTaskbar = true;
|
||||
var width = ActualWidth > 0 ? ActualWidth : PageWidth * Fit;
|
||||
var height = ActualHeight > 0 ? ActualHeight : (PageHeight * Fit) + BarHeight;
|
||||
Left = area.Left + Math.Max(0, (area.Width - width) / 2);
|
||||
Top = area.Top + Math.Max(0, (area.Height - height) / 2);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Parks the window just right of every screen: open, «visible» for Windows and for WebView2, out of Alberto's view
|
||||
/// (S2). Never <c>Hide()</c> and never minimised.
|
||||
/// (S2), without its taskbar button. Never <c>Hide()</c>, never left minimised.
|
||||
/// </summary>
|
||||
public void MoveOffScreen()
|
||||
{
|
||||
_minimized.Stop();
|
||||
if (WindowState == WindowState.Minimized)
|
||||
{
|
||||
WindowState = WindowState.Normal;
|
||||
}
|
||||
|
||||
ShowInTaskbar = false;
|
||||
Left = SystemParameters.VirtualScreenLeft + SystemParameters.VirtualScreenWidth + 64;
|
||||
Top = SystemParameters.VirtualScreenTop;
|
||||
}
|
||||
@@ -132,6 +211,7 @@ internal sealed class BrowserWindow : Window
|
||||
public void CloseForGood()
|
||||
{
|
||||
_allowClose = true;
|
||||
_minimized.Stop();
|
||||
Close();
|
||||
}
|
||||
|
||||
@@ -151,14 +231,48 @@ internal sealed class BrowserWindow : Window
|
||||
{
|
||||
if (WindowState == WindowState.Minimized)
|
||||
{
|
||||
// A minimised window stops painting (S2): back to normal, out of view.
|
||||
WindowState = WindowState.Normal;
|
||||
DoneRequested?.Invoke(this, EventArgs.Empty);
|
||||
// Minimised Alberto still has it (its taskbar button brings it back), but WebView2 stops painting (S2):
|
||||
// left there too long it counts as closed.
|
||||
_minimized.Interval = MinimizedLimit;
|
||||
_minimized.Stop();
|
||||
_minimized.Start();
|
||||
}
|
||||
else
|
||||
{
|
||||
_minimized.Stop();
|
||||
}
|
||||
|
||||
base.OnStateChanged(e);
|
||||
}
|
||||
|
||||
private void MinimizedTooLong()
|
||||
{
|
||||
_minimized.Stop();
|
||||
if (WindowState == WindowState.Minimized)
|
||||
{
|
||||
WindowState = WindowState.Normal;
|
||||
DoneRequested?.Invoke(this, EventArgs.Empty);
|
||||
}
|
||||
}
|
||||
|
||||
private void FitTo(Rect area)
|
||||
{
|
||||
// The frame of the window (title bar and borders) around the page and its bar.
|
||||
var frame = SystemParameters.WindowNonClientFrameThickness;
|
||||
var resize = SystemParameters.WindowResizeBorderThickness;
|
||||
var fit = FitFor(area.Width, area.Height, PageWidth, PageHeight, frame.Left + frame.Right + resize.Left + resize.Right, frame.Top + frame.Bottom + resize.Top + resize.Bottom);
|
||||
if (Math.Abs(fit - Fit) < 0.001)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Fit = fit;
|
||||
_page.Width = PageWidth * fit;
|
||||
_page.Height = PageHeight * fit;
|
||||
UpdateLayout();
|
||||
PageAreaChanged?.Invoke(this, EventArgs.Empty);
|
||||
}
|
||||
|
||||
private static Button BarButton(string text, string tip) => new()
|
||||
{
|
||||
Content = text,
|
||||
|
||||
@@ -52,7 +52,7 @@ internal sealed class WebView2Browser : IFebeBrowser
|
||||
_options = new BrowserOptions(ProfileDirectory, settings.Width, settings.Height);
|
||||
Host = new WebView2Host(
|
||||
ui,
|
||||
new WebView2HostOptions(settings.Hosting, settings.AdditionalBrowserArguments, icon, () => context.Tracker.MarkUsed(context.Clock.Now)),
|
||||
new WebView2HostOptions(settings.Hosting, settings.AdditionalBrowserArguments, icon, () => context.Tracker.MarkUsed(context.Clock.Now), context.CurrentStartPage),
|
||||
context.Log);
|
||||
Host.BrowserProcessFailed += (_, _) => RequestRestart(null);
|
||||
}
|
||||
|
||||
@@ -18,7 +18,24 @@ namespace Febe.Desktop.Browser;
|
||||
/// <param name="AdditionalBrowserArguments">The switches of S2 (<see cref="BrowserArguments.ForWebView2"/>).</param>
|
||||
/// <param name="Icon">The icon of the window.</param>
|
||||
/// <param name="MarkUsed">Called when Alberto uses the browser and when he is done with it («Riprendi»).</param>
|
||||
internal sealed record WebView2HostOptions(BrowserHosting Hosting, string AdditionalBrowserArguments, ImageSource? Icon, Action MarkUsed);
|
||||
/// <param name="StartPage">Where «Pagina iniziale» goes: the site's home when connected, the login page otherwise (issue #52).</param>
|
||||
internal sealed record WebView2HostOptions(BrowserHosting Hosting, string AdditionalBrowserArguments, ImageSource? Icon, Action MarkUsed, Func<string>? StartPage = null);
|
||||
|
||||
/// <summary>What the host does when a process of WebView2 fails (issue #52).</summary>
|
||||
internal enum ProcessFailureResponse
|
||||
{
|
||||
/// <summary>Nothing: WebView2 recovers by itself (an unresponsive page while Alberto looks at it, a helper process).</summary>
|
||||
None,
|
||||
|
||||
/// <summary>The whole browser is gone: the owner starts it again.</summary>
|
||||
Restart,
|
||||
|
||||
/// <summary>The page crashed (or hangs while nobody looks): back to the rest page (spec §6.1).</summary>
|
||||
Rest,
|
||||
|
||||
/// <summary>The GPU process restarted: the bounds are given again, or the window may stay blank or black.</summary>
|
||||
Redraw,
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The browser host of the Windows edition (spec §5, §6.1, §12): WebView2 in the window «Browser di Febe», with its own
|
||||
@@ -36,6 +53,9 @@ internal sealed record WebView2HostOptions(BrowserHosting Hosting, string Additi
|
||||
/// «Ho finito»; using and closing it marks the browser as used for «Riprendi».</item>
|
||||
/// <item>Stopping closes the WebView and waits for its browser process to exit, so the session is on disk (S3);
|
||||
/// <see cref="RestartAsync"/> («Scollega») does the same before deleting the profile.</item>
|
||||
/// <item>Issue #52: the bounds (and the zoom of a window shrunk to fit the screen) are given again, and WebView2 told of
|
||||
/// the window's new place, whenever the window moves on or off screen and after the GPU process restarted; a page that
|
||||
/// hangs while Alberto looks at it is left to recover instead of being replaced by the rest page.</item>
|
||||
/// <item>Threads: every WebView2 object (environment, controller, <c>CoreWebView2</c>, its settings and DevTools
|
||||
/// receivers) and the window are touched only on the UI thread, through its dispatcher; WebView2 throws on any other
|
||||
/// thread. Callers on other threads (the composition, the engine) get only plain values captured on the UI thread
|
||||
@@ -147,12 +167,20 @@ internal sealed class WebView2Host : IBrowserHost
|
||||
}
|
||||
|
||||
window.MoveOnScreen();
|
||||
if (window.WindowState == System.Windows.WindowState.Minimized)
|
||||
{
|
||||
window.WindowState = System.Windows.WindowState.Normal;
|
||||
}
|
||||
|
||||
window.Activate();
|
||||
window.Topmost = true;
|
||||
window.Topmost = false;
|
||||
|
||||
// The lease first: once the window is on screen the engine must be still, whatever happens to the focus.
|
||||
TakeLease();
|
||||
|
||||
// On screen, perhaps shrunk to fit it: WebView2 gets its bounds again and learns where its window is now.
|
||||
UpdateBounds();
|
||||
try
|
||||
{
|
||||
_controller?.MoveFocus(CoreWebView2MoveFocusReason.Programmatic);
|
||||
@@ -169,9 +197,24 @@ internal sealed class WebView2Host : IBrowserHost
|
||||
{
|
||||
_window?.MoveOffScreen();
|
||||
ReleaseLease();
|
||||
NotifyPositionChanged();
|
||||
return Task.CompletedTask;
|
||||
});
|
||||
|
||||
/// <summary>
|
||||
/// What a failed process of WebView2 asks of the host (issue #52). A page that does not answer while Alberto is at
|
||||
/// the window is his to wait for (WebView2 recovers, or he reloads): replacing it with the rest page would turn it
|
||||
/// white under his eyes. Nobody looking, it goes to rest as a crashed page does.
|
||||
/// </summary>
|
||||
internal static ProcessFailureResponse ResponseTo(CoreWebView2ProcessFailedKind kind, bool userIsDriving) => kind switch
|
||||
{
|
||||
CoreWebView2ProcessFailedKind.BrowserProcessExited => ProcessFailureResponse.Restart,
|
||||
CoreWebView2ProcessFailedKind.RenderProcessExited => ProcessFailureResponse.Rest,
|
||||
CoreWebView2ProcessFailedKind.RenderProcessUnresponsive => userIsDriving ? ProcessFailureResponse.None : ProcessFailureResponse.Rest,
|
||||
CoreWebView2ProcessFailedKind.GpuProcessExited => ProcessFailureResponse.Redraw,
|
||||
_ => ProcessFailureResponse.None,
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Closes the WebView, waits for its browser process to exit (the profile is free and written), runs
|
||||
/// <paramref name="whileStopped"/> — «Scollega» deletes the profile there — and starts it again. <see cref="Cdp"/>
|
||||
@@ -302,7 +345,14 @@ internal sealed class WebView2Host : IBrowserHost
|
||||
window.DoneRequested += (_, _) => Done();
|
||||
window.BackRequested += (_, _) => UserNavigation(core => core.GoBack(), core => core.CanGoBack);
|
||||
window.ReloadRequested += (_, _) => UserNavigation(core => core.Reload(), _ => true);
|
||||
window.HomeRequested += (_, _) => UserNavigation(core => core.Navigate(_settings.StartPage!()), _ => _settings.StartPage is not null);
|
||||
window.DpiChanged += (_, _) => UpdateBounds();
|
||||
window.PageAreaChanged += (_, _) => UpdateBounds();
|
||||
|
||||
// The raw controller does not follow its parent by itself: popups (lists of a <select>, menus, suggestions)
|
||||
// would open where the window was, out of view.
|
||||
window.LocationChanged += (_, _) => NotifyPositionChanged();
|
||||
window.ShowAddress(null);
|
||||
_window = window;
|
||||
}
|
||||
|
||||
@@ -336,6 +386,7 @@ internal sealed class WebView2Host : IBrowserHost
|
||||
|
||||
controller.BoundsMode = CoreWebView2BoundsMode.UseRawPixels;
|
||||
controller.Bounds = _window.PageBounds;
|
||||
controller.ZoomFactor = _window.Fit;
|
||||
|
||||
// WebView2 stops painting when IsVisible is false (S2): it stays true for the whole life of the browser.
|
||||
controller.IsVisible = true;
|
||||
@@ -436,24 +487,38 @@ internal sealed class WebView2Host : IBrowserHost
|
||||
return;
|
||||
}
|
||||
|
||||
if (kind == CoreWebView2ProcessFailedKind.BrowserProcessExited)
|
||||
switch (ResponseTo(kind, UserIsDriving))
|
||||
{
|
||||
// The whole browser is gone: commands fail from now on, and the owner starts it again (same profile), as
|
||||
// the supervisor does with Chromium — once this handler has returned (threading model of WebView2).
|
||||
_session.Detach();
|
||||
_ = _ui.InvokeAsync(() => BrowserProcessFailed?.Invoke(this, EventArgs.Empty), DispatcherPriority.Background);
|
||||
}
|
||||
else if (kind is CoreWebView2ProcessFailedKind.RenderProcessExited or CoreWebView2ProcessFailedKind.RenderProcessUnresponsive)
|
||||
{
|
||||
// The page crashed: back to the rest page (spec §6.1); a running recipe ends on its own errors.
|
||||
try
|
||||
{
|
||||
(sender as CoreWebView2)?.Navigate(CdpPageExtensions.RestUrl);
|
||||
}
|
||||
catch (Exception ex) when (ex is COMException or InvalidOperationException)
|
||||
{
|
||||
// The browser went with the page: its own ProcessFailed follows.
|
||||
}
|
||||
case ProcessFailureResponse.Restart:
|
||||
// The whole browser is gone: commands fail from now on, and the owner starts it again (same profile), as
|
||||
// the supervisor does with Chromium — once this handler has returned (threading model of WebView2).
|
||||
_session.Detach();
|
||||
_ = _ui.InvokeAsync(() => BrowserProcessFailed?.Invoke(this, EventArgs.Empty), DispatcherPriority.Background);
|
||||
break;
|
||||
case ProcessFailureResponse.Rest:
|
||||
// The page crashed: back to the rest page (spec §6.1); a running recipe ends on its own errors.
|
||||
try
|
||||
{
|
||||
(sender as CoreWebView2)?.Navigate(CdpPageExtensions.RestUrl);
|
||||
}
|
||||
catch (Exception ex) when (ex is COMException or InvalidOperationException)
|
||||
{
|
||||
// The browser went with the page: its own ProcessFailed follows.
|
||||
}
|
||||
|
||||
break;
|
||||
case ProcessFailureResponse.Redraw:
|
||||
// A windowed WebView can stay blank or black after the GPU process restarted until its bounds change:
|
||||
// they are given again (never IsVisible = false, S2), once this handler has returned.
|
||||
_ = _ui.InvokeAsync(UpdateBounds, DispatcherPriority.Background);
|
||||
break;
|
||||
default:
|
||||
if (kind == CoreWebView2ProcessFailedKind.RenderProcessUnresponsive)
|
||||
{
|
||||
_log.Info("browser", "WebView2: la pagina non risponde mentre usi il browser; Febe la lascia com'è (Ricarica, se non si riprende).");
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -481,11 +546,12 @@ internal sealed class WebView2Host : IBrowserHost
|
||||
TakeLease();
|
||||
}
|
||||
|
||||
/// <summary>Alberto closed or minimised the window: done, out of view.</summary>
|
||||
/// <summary>Alberto closed the window (or left it minimised too long): done, out of view.</summary>
|
||||
private void Done()
|
||||
{
|
||||
_window?.MoveOffScreen();
|
||||
ReleaseLease();
|
||||
NotifyPositionChanged();
|
||||
}
|
||||
|
||||
private void UserNavigation(Action<CoreWebView2> action, Func<CoreWebView2, bool> allowed)
|
||||
@@ -520,6 +586,10 @@ internal sealed class WebView2Host : IBrowserHost
|
||||
try
|
||||
{
|
||||
controller.Bounds = window.PageBounds;
|
||||
|
||||
// Shrunk to fit a small screen, the page is zoomed out by as much: its size in CSS pixels stays the same.
|
||||
controller.ZoomFactor = window.Fit;
|
||||
controller.NotifyParentWindowPositionChanged();
|
||||
}
|
||||
catch (Exception ex) when (ex is COMException or InvalidOperationException)
|
||||
{
|
||||
@@ -527,6 +597,18 @@ internal sealed class WebView2Host : IBrowserHost
|
||||
}
|
||||
}
|
||||
|
||||
private void NotifyPositionChanged()
|
||||
{
|
||||
try
|
||||
{
|
||||
_controller?.NotifyParentWindowPositionChanged();
|
||||
}
|
||||
catch (Exception ex) when (ex is COMException or InvalidOperationException)
|
||||
{
|
||||
// The browser process is gone: the next start places the new WebView.
|
||||
}
|
||||
}
|
||||
|
||||
private void TakeLease()
|
||||
{
|
||||
if (_lease is null)
|
||||
|
||||
Reference in new issue
Block a user