Branch: refs/heads/main
Home: https://github.com/WebKit/WebKit
Commit: 39b1eedf5498b3e06a762a64f834f5f6de6efb6d
https://github.com/WebKit/WebKit/commit/39b1eedf5498b3e06a762a64f834f5f6de6efb6d
Author: Aditya Keerthi <[email protected]>
Date: 2025-10-14 (Tue, 14 Oct 2025)
Changed paths:
M Source/WebKit/Platform/spi/mac/AppKitSPI.h
M Source/WebKit/UIProcess/API/Cocoa/WKWebView.mm
M Tools/TestWebKitAPI/Tests/TestWebKitAPI/mac/AppKitSPI.h
M Tools/TestWebKitAPI/Tests/WebKitCocoa/ScreenTime.mm
Log Message:
-----------
[Screen Time] Element fullscreen displays a blank view in macOS SwiftUI apps
https://bugs.webkit.org/show_bug.cgi?id=300660
rdar://157888558
Reviewed by Abrar Rahman Protyasha and Tim Horton.
With the introduction of Screen Time in WebKit, `WKWebView` now contains an
`STWebpageView` subview. `STWebpageView` contains an `NSRemoteView`, which uses
Auto Layout to size itself. The addition of this view is the first time
`WKWebView`
has Auto Layout use (that is not from autoresizing mask constaints) in its
hierarchy.
SwiftUI apps have long used `WKWebView` via `NSViewRepresentable`, relying on
SwiftUI's "view hosting" implementation. Even the recently added `WebView` API
relies on view hosting under the hood. SwiftUI's view hosting implementation
takes control of the hosted view's layout. More specifically, SwiftUI checks
`-[NSView _wantsConstraintBasedLayout]` on the hosted view (in this case, a
`WKWebView`). If true, SwiftUI sets `translatesAutoresizingMaskIntoConstraints`
to false, and adds constraints to size the hosted view to match its container
(referred to as the view host in SwiftUI).
With the addition of `STWebpageView` into the `WKWebView` hierarchy,
`-[NSView _wantsConstraintBasedLayout]` now evaluates to true. This is because
`_wantsConstraintBasedLayout` recursively checks subviews to see if they are
using Auto Layout. Consequently, with Screen Time enablement, all `WKWebView`s
in a SwiftUI context have `translatesAutoresizingMaskIntoConstraints` set to
false.
Finally, WebKit's fullscreen implementation works by removing the `WKWebView`
out of its original hierarchy and adding to a WebKit-owned fullscreen window.
As part of this implementation, all constraints are removed from the web view,
and restored on fullscreen exit. Since all constraints are removed in
fullscreen,
and the `WKWebView` has translatesAutoresizingMaskIntoConstraints` set to false
as described above, there is nothing to size the web view. Consequently, a blank
view is shown in fullscreen.
Simply forcing `translatesAutoresizingMaskIntoConstraints` to true does not fix
the issue, as SwiftUI still takes control of the frame of the hosted view. That
approach yields a partial fix where the web view now has a defined size, but the
size is still incorrect. Other fixes could involve SwiftUI, including only
setting `translatesAutoresizingMaskIntoConstraints` to false if the hosted view
directly has non-TAMIC constraints, and avoiding updating the frame if the
hosted
view is no longer a descendant of the hosting view. However, these fixes were
deemed too risky at this time. Finally, an ideal solution would be to
rearchitect
WebKit's fullscreen implementation to avoid ripping the web view out of its
original view hierarchy.
To fix, mark the Screen Time view as hosting an Auto Layout engine prior to
adding it to the `WKWebView`'s hierarchy. This results in `-[NSView
_wantsConstraintBasedLayout]`
on the `WKWebView` itself returning false, since descendants that host their own
engine are skipped. Ultimately, this prevents SwiftUI from setting
`translatesAutoresizingMaskIntoConstraints` to false when the `WKWebView`
contains
the Screen Time view.
* Source/WebKit/Platform/spi/mac/AppKitSPI.h:
* Source/WebKit/UIProcess/API/Cocoa/WKWebView.mm:
(-[WKWebView _installScreenTimeWebpageControllerIfNeeded]):
* Tools/TestWebKitAPI/Tests/TestWebKitAPI/mac/AppKitSPI.h:
* Tools/TestWebKitAPI/Tests/WebKitCocoa/ScreenTime.mm:
(TEST(ScreenTime, FullscreenWithSystemBlockingViewInHierarchy)):
Canonical link: https://commits.webkit.org/301481@main
To unsubscribe from these emails, change your notification settings at
https://github.com/WebKit/WebKit/settings/notifications
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes