Skip to content

Truly independent web browser

UI/Qt: Let the page layer cover the web view before its first frame

Andreas Kling51810c437714
diff --git a/UI/Qt/WebContentView.cpp b/UI/Qt/WebContentView.cpp
index 4de1110634e..f3e8524bee4 100644
--- a/UI/Qt/WebContentView.cpp
+++ b/UI/Qt/WebContentView.cpp
@@ -1211,7 +1211,7 @@ void WebContentView::paintEvent(QPaintEvent*)
// Qt still sends paint events to a view that paints on screen, which has no paint engine to paint with.
if (testAttribute(Qt::WA_PaintOnScreen))
return;
- // The page layer covers the view, except before its first frame, which shows the chrome's color.
+ // Qt paints the view only while an overlay hides the page layer, and the chrome color lies behind the overlay.
QPainter painter(this);
painter.fillRect(rect(), ChromeStyle::chrome_background(window()->palette()));
}
diff --git a/UI/Qt/WebContentViewMac.mm b/UI/Qt/WebContentViewMac.mm
index 4929b12b19e..7044205b689 100644
--- a/UI/Qt/WebContentViewMac.mm
+++ b/UI/Qt/WebContentViewMac.mm
@@ -6,6 +6,7 @@
#include <AK/ScopeGuard.h>
#include <LibGfx/SharedImageBuffer.h>
+#include <UI/Qt/ChromeStyle.h>
#include <UI/Qt/WebContentView.h>
#include <QChildEvent>
@@ -70,9 +71,13 @@ void WebContentView::present_current_paintable_as_layer_contents()
{
if (!ensure_iosurface_layer_attached_to_native_view())
return;
- ScopeGuard update_painting_for_new_contents = [this] { update_whether_qt_paints_view(); };
-
auto* layer = as_layer(m_iosurface_layer);
+ ScopeGuard update_for_new_contents = [this, layer, had_contents = layer.contents != nil] {
+ if ((layer.contents != nil) != had_contents)
+ update_iosurface_layer_background_color();
+ update_whether_qt_paints_view();
+ };
+
layer.contentsScale = device_pixel_ratio();
auto paintable = current_paintable();
@@ -157,9 +162,12 @@ void WebContentView::update_iosurface_layer_background_color()
{
if (!m_iosurface_layer)
return;
- auto color = page_background_color();
- auto* background_color = CGColorCreateSRGB(color.red() / 255.0, color.green() / 255.0, color.blue() / 255.0, 1.0);
- as_layer(m_iosurface_layer).backgroundColor = background_color;
+ auto* layer = as_layer(m_iosurface_layer);
+ // Before its first frame, the view shows the chrome's color.
+ auto page_color = page_background_color();
+ auto color = layer.contents != nil ? QColor(page_color.red(), page_color.green(), page_color.blue()) : ChromeStyle::chrome_background(window()->palette());
+ auto* background_color = CGColorCreateSRGB(color.redF(), color.greenF(), color.blueF(), 1.0);
+ layer.backgroundColor = background_color;
CGColorRelease(background_color);
}
@@ -172,12 +180,14 @@ void WebContentView::detach_iosurface_layer_from_native_view()
// While the page layer covers the view, the view paints on screen, so Qt neither paints it into the window's backing
// store nor copies it into a backing store of the view's own native view. All of that would lie under the layer, and
-// on every step of a window resize it is a fill and a copy of the whole view into freshly allocated surfaces. Qt paints
-// the view before the layer has a frame, and while an overlay, which Qt paints into the view, hides the layer.
+// on every step of a window resize it is a fill and a copy of the whole view into freshly allocated surfaces. Once made,
+// that backing store of the native view stays for as long as the view, so Qt must not paint the view before it has a
+// frame either: until then the layer shows the color Qt would paint. Qt paints the view only while the layer is not
+// attached yet, and while an overlay, which Qt paints into the view, hides the layer.
void WebContentView::update_whether_qt_paints_view()
{
auto* layer = as_layer(m_iosurface_layer);
- bool page_layer_covers_view = layer.superlayer != nil && !layer.hidden && layer.contents != nil;
+ bool page_layer_covers_view = layer.superlayer != nil && !layer.hidden;
if (testAttribute(Qt::WA_PaintOnScreen) == page_layer_covers_view)
return;
setAttribute(Qt::WA_PaintOnScreen, page_layer_covers_view);