Browser vuoto: correzioni dopo la revisione (#52)

La revisione di c49df4c ha trovato alcuni punti deboli. Si correggono qui.

- Fuori vista la pagina torna al 100%. Lo zoom calcolato per uno schermo
  piccolo restava anche per le sessioni del motore, e le pagine vedevano un
  devicePixelRatio diverso (regola 4). Il rapporto si ricalcola solo quando
  la finestra viene sullo schermo.
- Pulsante nella barra delle applicazioni. WPF lo cambia nascondendo la
  finestra e mostrandola di nuovo (verificato con un aggancio a
  WM_WINDOWPOSCHANGED). Ora il pulsante cambia solo mentre la finestra è
  fuori vista. Sullo schermo la finestra chiede di ridisegnarsi
  (RedrawWindow), senza nulla che la pagina veda. Una prova con WebView2
  vero controlla che la finestra non si nasconda mai sullo schermo.
- Ridotta a icona troppo a lungo, la finestra si ripristina coperta (DWM) e
  senza prendere il fuoco, e si sposta fuori vista prima di scoprirsi. Prima
  ricompariva per un attimo dove stava. Spostare la posizione di ripristino
  non basta: Windows la riporta su uno schermo (provato).
- Dopo un riavvio del processo GPU i bordi della pagina cambiano davvero: un
  pixel in meno, poi quelli veri. Gli stessi bordi di nuovo potevano non
  servire a nulla.
- «Pagina iniziale», sulla barra di Windows e nella vista remota, passa da
  BrowserConnectionService.OpenStartPageAsync. Aspetta la fine della
  sessione del motore e il turno della pagina, come l'apertura dal riposo,
  e agisce solo mentre Alberto guida.
- Vista remota.
  - Dopo un riavvio dei fotogrammi la vista aspetta il primo fotogramma per
    mezzo secondo prima dell'immagine ferma: Chromium lo manda solo dopo la
    risposta allo start, e prima quasi ogni riavvio costava una cattura.
  - L'immagine ferma si scarta se nel frattempo arriva un fotogramma vero.
  - Il silenzio si conta dall'avvio.
  - Un riavvio del browser durante la chiusura non lascia eccezioni non
    osservate.
- Un POST vuoto senza Content-Length (a pezzi, HTTP/2) non dà più 400 su
  begin, cancel e browser/show (ApiHttp.OptionalBodyAsync).
- La pagina Browser toglie ?accesso=1 dall'indirizzo. Ricaricarla riapriva
  la pagina di accesso.
- RemoteViewFramesTests aspetta fino a 20 s invece di 5. Controlla che
  l'immagine arrivi, non quanto in fretta; il tempo va nell'uscita della
  prova.

Aggiornati PROTOCOLLO.md, la guida (Primo collegamento, Installazione
Windows) e docs/decisioni.md con le righe di #52.

Prove nuove:
- AFrameSoonAfterARestartNeedsNoStillPicture
- PaginaInizialeWaitsForThePageTurnAndActsOnlyWhileAlbertoIsAtTheBrowser
- ABodyWithoutALengthIsReadAndAnEmptyOneIsNoBody
- nella prova della finestra vera: nessun nascondere sullo schermo e pagina
  al 100% fuori vista

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-16 13:28:08 +01:00
1 parent c49df4ca7b
commit 654076d280
25 files changed
+505 -86

No files matched your search

+82 -8
View File
@@ -9,6 +9,7 @@ using Febe.Browser.Hosting;
using Febe.Browser.WebView2;
using Febe.Core.Abstractions;
using Febe.Desktop.Core;
using Febe.Desktop.Native;
using Microsoft.Web.WebView2.Core;
namespace Febe.Desktop.Browser;
@@ -18,8 +19,11 @@ 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>
/// <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);
/// <param name="OpenStartPage">
/// What «Pagina iniziale» does (issue #52): the connection opens the site's home when connected, the login page otherwise,
/// once the engine's session ended and with the page's turn. Null: the button does nothing.
/// </param>
internal sealed record WebView2HostOptions(BrowserHosting Hosting, string AdditionalBrowserArguments, ImageSource? Icon, Action MarkUsed, Func<CancellationToken, Task>? OpenStartPage = null);
/// <summary>What the host does when a process of WebView2 fails (issue #52).</summary>
internal enum ProcessFailureResponse
@@ -33,7 +37,7 @@ internal enum ProcessFailureResponse
/// <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>
/// <summary>The GPU process restarted: the page is resized by a pixel and back, or the window may stay blank or black.</summary>
Redraw,
}
@@ -179,8 +183,10 @@ internal sealed class WebView2Host : IBrowserHost
// 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.
// On screen, perhaps shrunk to fit it: WebView2 gets its bounds again and learns where its window is now, and paints
// again (WPF hid and showed the window, out of view, to give it its taskbar button).
UpdateBounds();
Repaint();
try
{
_controller?.MoveFocus(CoreWebView2MoveFocusReason.Programmatic);
@@ -345,7 +351,7 @@ 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.HomeRequested += (_, _) => OpenStartPage();
window.DpiChanged += (_, _) => UpdateBounds();
window.PageAreaChanged += (_, _) => UpdateBounds();
@@ -508,9 +514,10 @@ internal sealed class WebView2Host : IBrowserHost
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);
// A windowed WebView can stay blank or black after the GPU process restarted until its size changes: the
// same bounds again may be no change at all, so one pixel less and then the real ones (never
// IsVisible = false, S2), once this handler has returned.
_ = _ui.InvokeAsync(Redraw, DispatcherPriority.Background);
break;
default:
if (kind == CoreWebView2ProcessFailedKind.RenderProcessUnresponsive)
@@ -576,6 +583,73 @@ internal sealed class WebView2Host : IBrowserHost
}
}
/// <summary>
/// «Pagina iniziale»: Alberto's own navigation (rule 7), done by the connection, which first waits for the engine's
/// session to end and takes the page's turn (issue #52).
/// </summary>
private void OpenStartPage()
{
if (_settings.OpenStartPage is not { } open || _core is null)
{
return;
}
TakeLease();
_ = OpenStartPageQuietlyAsync(open);
}
private async Task OpenStartPageQuietlyAsync(Func<CancellationToken, Task> open)
{
try
{
await open(CancellationToken.None).ConfigureAwait(true);
}
catch (Exception ex) when (ex is not OutOfMemoryException)
{
_log.Warn("browser", $"«Pagina iniziale» non aperta ({ex.GetType().Name}).");
}
}
/// <summary>After a GPU restart: a real change of size (one pixel less), then the real bounds and a repaint.</summary>
private void Redraw()
{
if (_controller is not { } controller || _window is not { } window)
{
return;
}
try
{
var bounds = window.PageBounds;
if (bounds.Height > 1)
{
controller.Bounds = bounds with { Height = bounds.Height - 1 };
}
}
catch (Exception ex) when (ex is COMException or InvalidOperationException)
{
// The browser process is gone: the next start sizes the new WebView.
return;
}
_ = _ui.InvokeAsync(
() =>
{
UpdateBounds();
Repaint();
},
DispatcherPriority.Background);
}
/// <summary>Asks the window and WebView2's child windows to paint again; nothing the page can see changes.</summary>
private void Repaint()
{
if (_window is { } window)
{
Win32.RedrawWindow(window.Handle, IntPtr.Zero, IntPtr.Zero, Win32.RdwInvalidate | Win32.RdwAllChildren | Win32.RdwUpdateNow);
}
}
private void UpdateBounds()
{
if (_controller is not { } controller || _window is not { } window)