Skip to content

[Android] BlazorWebView: Support fullscreen web content - #40456

Merged
kubaflo merged 4 commits into
dotnet:candidate/11.0.1xx-sr2from
Kebechet:fix-22049-android-blazorwebview-fullscreen
Nov 20, 2026
Merged

kubaflo merged 4 commits into
dotnet:candidate/11.0.1xx-sr2from
Kebechet:fix-22049-android-blazorwebview-fullscreen

Conversation

@Kebechet

@Kebechet Kebechet commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Note

Are you waiting for the changes in this PR to be merged?
It would be very helpful if you could test the resulting artifacts from this PR and let us know in a comment if this change resolves your issue. Thank you!

Target Branch

This PR is for Initial base
New features, API changes, or .NET 12 work (including .NET 12 fixes) main
.NET 11 servicing fixes net11, for review
Older servicing, release integration, infrastructure, or documentation The branch designated for that work

Description of Change

Root cause: on Android, BlazorWebView installs its own BlazorWebChromeClient, and it never overrode OnShowCustomView. The Android WebView only lets web content enter fullscreen when the WebChromeClient overrides OnShowCustomView, so inside a BlazorWebView document.fullscreenEnabled is false, element.requestFullscreen() rejects with TypeError: Fullscreen is not supported, and the fullscreen button of <video> is disabled. The WebView control got fullscreen support in #15472, but that code lives in MauiWebChromeClient, which BlazorWebView does not use.

Fix:

  • The OnShowCustomView/OnHideCustomView code of MauiWebChromeClient moves into an internal WebViewFullScreenPresenter in Core, and both chrome clients delegate to it, so WebView and BlazorWebView enter and leave fullscreen the same way. The view the page puts in fullscreen covers the decor view, the activity turns to landscape and hides the system bars; on exit the view is removed, the orientation is restored and the system bars come back.
  • BlazorWebViewHandler passes the hosting activity to BlazorWebChromeClient, which keeps it in a weak reference as MauiWebChromeClient does.
  • BlazorWebViewHandler.DisconnectHandler calls BlazorWebChromeClient.Disconnect(), which leaves fullscreen if it is showing and refuses to enter it again. The WebView keeps the chrome client until its components are disposed, so the page can still ask for fullscreen in that time, and the handler then detaches the client before destroying the WebView, so Chromium's own OnHideCustomView never arrives. Without this, the overlay, the landscape lock and the hidden system bars would stay on the activity. A request after Disconnect() gets OnCustomViewHidden() and shows nothing.

Changes in the moved code, which also apply to WebView:

  • Fullscreen no longer changes WindowCompat.SetDecorFitsSystemWindows. Leaving fullscreen set it to true, while MauiAppCompatActivity sets it to false. With that policy, one fullscreen round trip moved the app's content down by the status bar height: android.R.id.content went from y=0 to y=128 on an API 34 emulator, and stayed at 0 on Android 16 (API 36). Android has no getter for this policy on API 30+, so the presenter cannot put the app's own value back; it also does not need to change it, because the custom view goes on the decor view, outside the content that the policy pads, and the system bars are hidden while it shows.
  • The status bar, the navigation bar and the caption bar are restored separately (API 30+). Before, Show combined their visibility with OR, so an app with only one of them visible got all of them back after fullscreen. Below API 30 the presenter still assumes all bars were visible, as before.
  • Hide does nothing unless a view is showing. Before, a stray OnHideCustomView reset the orientation to a stale value.
  • Show without an activity, or with one that is finishing, destroyed or has no window attached, calls OnCustomViewHidden() right away, so the page does not stay in a fullscreen state that nothing displays. Before, a missing activity was ignored, and a detached window threw a NullReferenceException reading its insets.

No public API change. WebViewFullScreenPresenter is internal (Core already grants InternalsVisibleTo to Microsoft.AspNetCore.Components.WebView.Maui), and MauiWebChromeClient only loses private fields.

Behaviour a BlazorWebView now inherits from the WebView control: any element the page puts in fullscreen, not only <video>, gets the landscape orientation and the white background the WebView code sets, and the window keeps the BehaviorShowTransientBarsBySwipe system bar behaviour after fullscreen.

Not covered here: WebView still does not leave fullscreen when its handler disconnects, and HybridWebView sets no chrome client, so its content cannot enter fullscreen either.

Tests

Device tests in BlazorWebViewTests.FullScreen.Android.cs:

  • WebContentReportsFullScreenAsEnabled reads document.fullscreenEnabled from a running BlazorWebView.
  • ShowCustomViewCoversActivityUntilHidden drives the WebView's chrome client. After OnShowCustomView the custom view is a child of the activity's decor view and the orientation is landscape. After OnHideCustomView the view is removed, the orientation is restored and OnCustomViewHidden was called once.
  • DisconnectingHandlerLeavesFullScreen shows a custom view and disconnects the handler; the view is removed, the orientation is restored and OnCustomViewHidden was called once.
  • ShowCustomViewAfterDisconnectIsRejected disconnects the handler, then asks the still attached chrome client for fullscreen; nothing is shown and OnCustomViewHidden is called once.
  • ShowCustomViewWithoutActivityReportsTheViewHidden and ShowCustomViewWithoutAWindowOnScreenReportsTheViewHidden: a chrome client with no activity, or with an activity that has no window, reports the view hidden and keeps no showing state.
  • HidingAgainAfterLeavingFullScreenChangesNothing leaves fullscreen, changes the orientation, calls OnHideCustomView again and checks the orientation and the callback count do not change.
  • LeavingFullScreenKeepsTheActivityEdgeToEdge gives the activity the edge-to-edge policy of MauiAppCompatActivity, enters and leaves fullscreen, and checks the content has not moved.
  • LeavingFullScreenKeepsAHiddenStatusBarHidden (API 30+) hides the status bar, enters and leaves fullscreen, and checks the status bar is still hidden and the navigation bar is as it was.

The tests that show a custom view put the activity back in a finally (overlay, orientation, system bar behaviour) and wait for the rotation and bar animations to finish. They run against the test runner's activity rather than Platform.CurrentActivity, because the external navigation tests start activities that finish themselves, and the current activity can still point at one of those.

All MauiBlazorWebView.DeviceTests, run with xharness on a Pixel 6 (Android 16, API 36) and an API 34 emulator (Google APIs, x86_64), on this branch's base net11 (d1bb681):

Run Pixel 6, API 36 Emulator, API 34
Without the fix (only the first two tests existed then) 70 run: 64 passed, 4 skipped, 2 failed, both new tests (fullscreenEnabled is false; the custom view never reaches the decor view) not run
Without the DisconnectHandler change 71 run: 66 passed, 4 skipped, 1 failed (DisconnectingHandlerLeavesFullScreen: the overlay stays on the decor view) not run
All the tests above, with the product code as of 0d6eaf5 (before the review follow-up) 77 run: 70 passed, 4 skipped, 3 failed: ShowCustomViewAfterDisconnectIsRejected (the view lands on the decor view), LeavingFullScreenKeepsAHiddenStatusBarHidden (the status bar comes back), ShowCustomViewWithoutAWindowOnScreenReportsTheViewHidden (NullPointerException) 77 run: 69 passed, 4 skipped, 4 failed: the same three, and LeavingFullScreenKeepsTheActivityEdgeToEdge (the content moves from y=0 to y=128)
This branch, at its head commit (93871d5) 77 run: 73 passed, 4 skipped, 0 failed 77 run: 73 passed, 4 skipped, 0 failed (the same files, run just before the commit message was amended)

HidingAgainAfterLeavingFullScreenChangesNothing and ShowCustomViewWithoutActivityReportsTheViewHidden cover guards that were already in 0d6eaf5; each fails when its guard is removed (the orientation is reset to Unspecified; the callback count is 0).

API 19 to 29 were not run.

By hand, in a template Blazor Hybrid app on MAUI 10.0.130 with a copy of this code installed from app code as the BlazorWebView's chrome client: a tap that calls requestFullscreen() on a <video> resolves, the element goes fullscreen in landscape with the system bars hidden, and Back leaves fullscreen with the page layout as it was. The video file itself did not load on that network, so this checked the fullscreen handling, not playback. That check predates the review follow-up.

What NOT to Do (for future agents)

  • ❌ Don't copy the fullscreen code into BlazorWebChromeClient. The two controls would drift apart; share WebViewFullScreenPresenter.
  • ❌ Don't test fullscreen by calling requestFullscreen() through EvaluateJavascript, as the device tests run script. That gives the page no user activation, so the call rejects. document.fullscreenEnabled does not depend on one.
  • ❌ Don't call SetDecorFitsSystemWindows in the presenter. Android cannot report the app's value on API 30+, so whatever the presenter sets on exit overrides the app's layout policy.
  • ❌ Don't use Platform.CurrentActivity in these device tests. After the external navigation tests it can be an activity that finished in OnCreate and has no window.

Issues Fixed

Fixes #22049

kubaflo and others added 3 commits November 19, 2026 19:25
…r/DragLeave (dotnet#40441)

<!-- Please let the below note in for people that find this PR -->
> [!NOTE]
> Are you waiting for the changes in this PR to be merged?
> It would be very helpful if you could [test the resulting
artifacts](https://github.com/dotnet/maui/wiki/Testing-PR-Builds) from
this PR and let us know in a comment if this change resolves your issue.
Thank you!

<!--
Open .NET 11 fix PRs against net11 for review; target main for
next-major features.
After approval, a maintainer manually retargets the fix PR to the active
inflight
branch and merges it directly there. Do not merge individual fixes into
net11.
Bots must not retarget or merge. Candidate integration into net11 and
release/*
changes are separate, explicitly coordinated release work.
-->

### Target Branch

| This PR is for | Initial base |
| --- | --- |
| New features, API changes, or .NET 12 work (including .NET 12 fixes) |
`main` |
| .NET 11 servicing fixes | `net11`, for review |
| Older servicing, release integration, infrastructure, or documentation
| The branch designated for that work |

- [x] I selected the applicable base branch using the [contribution
guide](https://github.com/dotnet/maui/blob/main/.github/CONTRIBUTING.md#choose-your-pr-base-before-starting).

For .NET 11 fixes, only a maintainer retargets the approved PR to the
active
inflight lane and merges it after CI validates that final base. Do not
merge
individual fixes directly into `net11`; agents must not retarget or
merge.

### Description of Change

On iOS, `DragEventArgs.GetPosition()` can throw a
`NullReferenceException` inside a `DragOver` or `DragLeave` handler. The
exception crashes the app.

**CI evidence.** The `DropEventCoordinates` UI test crashed the HostApp
on both attempts on the iOS CoreCLR lane of SR2 candidate dotnet#40116
(maui-pr-uitests
[1690394](https://dev.azure.com/dnceng-public/public/_build/results?buildId=1690394),
iOS latest Editor..Fr job). The job's `.ips` reports show SIGABRT from
`xamarin_process_managed_exception` under `-[UIDropInteraction
_dragDestinationGestureStateChanged:]`. The logarchive shows the managed
exception:

```
System.NullReferenceException
  at DragAndDropDelegate.CalculatePosition(IElement, WeakReference, WeakReference)
  at DragAndDropBetweenLayouts.OnDragOver(...)        // HostApp calls e.GetPosition(...)
  at DragAndDropDelegate.<HandleDragOver>...
  at DragAndDropDelegate.SendEventArgs(...)
  at DragAndDropDelegate.HandleDragOver(...)
  at DragAndDropDelegate.SessionDidUpdate(...)
```

**Root cause.**
- `SessionDidUpdate` and `SessionDidExit` pass
`session.LocalDragSession`, a transient managed wrapper of the native
local drag session, into `HandleDragOver` and `HandleDragLeave`.
- Those methods put only a `WeakReference` to it in the `GetPosition`
callback, and nothing else on the path keeps it alive:
`PlatformDragEventArgs` holds the outer `IUIDropSession`, not the local
drag session.
- A GC while the user's handler is running can therefore collect the
wrapper. `CalculatePosition` then calls `LocationInView` on a null
session.
- Separately, `HandleDragOver` built a weak reference even when
`LocalDragSession` is null (a drag from another app), so `GetPosition`
always threw in that case. `HandleDragLeave` and `HandleDrop` already
return null there.

The weak references date from 2023, and the code is identical on
`net11`, the candidate and `main`. This is a latent bug that the CoreCLR
lane's GC timing exposed, not a candidate regression.

**Fix** (`DragAndDropDelegate.cs`, iOS/Mac Catalyst):
- `HandleDragOver` and `HandleDragLeave` call `GC.KeepAlive(session)`
after dispatching to the recognizers. The session stays alive while
handlers can synchronously call `GetPosition`, and the event args still
don't hold it strongly.
- `HandleDragOver` uses a null `sessionRef` when there is no local drag
session, matching `HandleDragLeave` and `HandleDrop`.
- `CalculatePosition` returns `null` when the session reference is
missing or its target has already been collected, instead of
dereferencing null. This covers a `GetPosition` call made after the
handler returns.

**Tests** (`DragAndDropTests.iOS.cs`, `Gesture` category):
- `DragPositionIsAvailableAfterGarbageCollectionDuringHandler(dragLeave:
false/true)`: a fake drop session returns a fresh local drag session
wrapper each time, the handler forces a full GC and then calls
`GetPosition`, and the test asserts the fake session's location.
- `DragOverPositionIsNullWithoutLocalDragSession`: with no local drag
session, `GetPosition` in `DragOver` returns `null`.

**Validation** (iOS 18.5 simulator, Release, CoreCLR, the same runtime
as the failing CI lane):

| Command | Result |
| --- | --- |
| `pwsh .github/skills/run-device-tests/scripts/Run-DeviceTests.ps1
-Project Controls -Platform ios -TestFilter "Category=Gesture"
-DeviceUdid <iOS 18.5 sim> -SkipXcodeVersionCheck` with only the new
tests (no fix) | **Failed**: 45 passed, 3 failed. All three new cases
throw `NullReferenceException` at
`DragAndDropDelegate.CalculatePosition`, the same frame as the CI crash.
|
| Same command with the fix | **Passed**: 48 passed, 0 failed |
| `dotnet format whitespace --folder --include <changed files>
--verify-no-changes` | Passed |

The UI test itself was not rerun locally, because its crash depends on
when a GC happens.

### Issues Fixed

No existing issue; found while investigating SR2 candidate dotnet#40116 CI.
Searched for related issues and PRs and found no duplicate.
dotnet#28416/dotnet#28417 is an unrelated Mac Catalyst `DragAndDropDelegate` NRE.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
BlazorWebChromeClient never overrode OnShowCustomView, so the Android WebView
reported document.fullscreenEnabled as false: requestFullscreen() rejected and
the fullscreen button of <video> and of embedded players (YouTube) was disabled.

MauiWebChromeClient already implements fullscreen for the WebView control. Its
OnShowCustomView/OnHideCustomView code moves unchanged into an internal
WebViewFullScreenPresenter that both chrome clients use, so the two controls
enter and leave fullscreen the same way.

Fixes dotnet#22049
The handler detaches the chrome client before it destroys the WebView, so
Chromium's own OnHideCustomView never arrived and the fullscreen overlay, the
landscape lock and the hidden system bars stayed on the activity.

The shared presenter also ignores a hide without a shown view, which used to
reset the orientation to a stale value, and tells the page right away when
there is no activity to show the view in. The fullscreen device tests restore
the test activity in a finally block.
@github-actions

Copy link
Copy Markdown
Contributor

🚀 Dogfood this PR with:

⚠️ WARNING: Do not do this without first carefully reviewing the code of this PR to satisfy yourself it is safe.

curl -fsSL https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.sh | bash -s -- 40456

Or

  • Run remotely in PowerShell:
iex "& { $(irm https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.ps1) } 40456"

@azure-pipelines

This comment has been minimized.

@dotnet-policy-service dotnet-policy-service Bot added the community ✨ Community Contribution label Nov 19, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Hey there @Kebechet! Thank you so much for your PR! Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

@github-actions github-actions Bot added area-blazor Blazor Hybrid / Desktop, BlazorWebView platform/android labels Nov 19, 2026
@kubaflo

This comment has been minimized.

@github-actions github-actions Bot added the s/agent-review-in-progress AI review is currently running for this PR label Nov 19, 2026
@MauiBot

MauiBot commented Nov 19, 2026

Copy link
Copy Markdown
Collaborator

PR Test Gate

@Kebechet — existing-test verification; Setup did not capture a tested commit.

Result INCONCLUSIVE Platform Android


🧪 Gate analysis — click to expand

⚠ Inconclusive. Setup, build, or execution did not produce a conclusive result. This does not prove the fix is incorrect.

No interpretable test-group summary was produced. No additional test execution is inferred.

Setup did not capture a verified PR head; no commit is claimed as tested.

No immutable without-fix baseline was prepared.

@MauiBot MauiBot removed the s/agent-review-in-progress AI review is currently running for this PR label Nov 19, 2026

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expert Review — 5 findings

Reviewed commit 0d6eaf5.

See inline comments for details.

…creen

Review follow-up for the shared fullscreen presenter and the
BlazorWebView chrome client:

- Fullscreen no longer calls WindowCompat.SetDecorFitsSystemWindows.
  Leaving fullscreen set it to true while MauiAppCompatActivity sets
  false, so the content moved down by the status bar height after each
  fullscreen (128 px on an API 34 emulator; not on API 36, which keeps
  apps edge to edge). Android has no getter for it on API 30+, and the
  custom view sits on the decor view, outside the content it pads.
- On API 30+ the status bar, the navigation bar and the caption bar are
  restored separately; before, an app with one of them hidden got all of
  them back. Below API 30 all bars are still assumed visible.
- Show reports the view hidden when the activity is finishing, destroyed
  or has no attached window, instead of throwing or covering a window
  that is not on screen.
- BlazorWebChromeClient.Disconnect (was ExitFullScreen) also rejects
  later OnShowCustomView calls: the WebView keeps the client until its
  components are disposed, and nothing would hide a view shown then.
- Device tests for the disconnect race, the bar state, the layout
  policy, the missing window and the two guards added earlier. They run
  against the test runner's activity, because the external navigation
  tests leave Platform.CurrentActivity on activities that finished in
  OnCreate.
@kubaflo

This comment has been minimized.

@github-actions github-actions Bot added the s/agent-review-in-progress AI review is currently running for this PR label Nov 20, 2026
@MauiBot

MauiBot commented Nov 20, 2026

Copy link
Copy Markdown
Collaborator

PR Test Gate

@Kebechet — existing-test verification; Setup did not capture a tested commit.

Result INCONCLUSIVE Platform Android


🧪 Gate analysis — click to expand

⚠ Inconclusive. Setup, build, or execution did not produce a conclusive result. This does not prove the fix is incorrect.

No interpretable test-group summary was produced. No additional test execution is inferred.

Setup did not capture a verified PR head; no commit is claimed as tested.

No immutable without-fix baseline was prepared.

@MauiBot MauiBot removed the s/agent-review-in-progress AI review is currently running for this PR label Nov 20, 2026

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expert Review — 3 findings

Reviewed commit 93871d5.

See inline comments for details.


// Store current state
_windowInsetsController = windowInsetsController;
_systemBarsToRestore = WindowInsetsCompat.Type.SystemBars(); // Assume visible by default

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[major] Logic and Correctness Verification - On Android 24-29, a Blazor app with a hidden status bar and visible navigation bar gets its status bar shown after a video fullscreen round trip. This branch unconditionally records SystemBars(), and Hide shows that entire mask, including bars that were already hidden. Separate-bar restoration resolves the earlier finding only on API 30+; the defect remains newly reachable through BlazorWebView on older supported versions. Capture and restore the pre-fullscreen visibility on this branch too, and cover it without the API-30-only test skip.

await WebViewHelpers.WaitForWebViewReady(platformWebView);

var activityState = new ActivityStateBeforeFullScreen(activity);
var chromeClient = platformWebView.WebChromeClient;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[moderate] Android Platform Specifics - WebView.WebChromeClient's getter requires API 26, but these tests run without a version guard and the device-test project supports API 24. On Android 7.0/7.1 (API 24/25), ShowCustomViewCoversActivityUntilHidden, DisconnectingHandlerLeavesFullScreen and ShowCustomViewAfterDisconnectIsRejected fail retrieving the client before exercising fullscreen. Obtain the installed client through an API-24-compatible test hook/cached reference, or explicitly guard these tests for API 26+, as Core WebViewHandler does for this getter.

Assert.Equal<object>(activity.Window!.DecorView, customView.Parent);
Assert.Equal(ScreenOrientation.Landscape, activity.RequestedOrientation);

chromeClient.OnHideCustomView();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[moderate] Regression Prevention and Test Coverage - Assert system-bar visibility while fullscreen is showing, before immediately hiding it. This test checks only parentage and orientation; the other bar test checks visibility only after exit. Removing the presenter's system-bar Hide calls would leave these assertions unchanged even though entering fullscreen from an activity with visible bars would leave those bars over the video. Establish visible bars, enter fullscreen, wait for their visibility to become false and assert it before testing restoration.

@kubaflo
kubaflo merged commit 952a4ae into dotnet:candidate/11.0.1xx-sr2 Nov 20, 2026
39 of 42 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-blazor Blazor Hybrid / Desktop, BlazorWebView community ✨ Community Contribution platform/android

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants