Skip to content

Truly independent web browser

UI/Qt: Clip translucent windows that cannot blend on X11

Andreas Kling594a450b857c
diff --git a/Tests/UI/Qt/CMakeLists.txt b/Tests/UI/Qt/CMakeLists.txt
index 0c2a2580053..6a507cb0523 100644
--- a/Tests/UI/Qt/CMakeLists.txt
+++ b/Tests/UI/Qt/CMakeLists.txt
@@ -105,7 +105,11 @@ if (LINUX)
"${LADYBIRD_SOURCE_DIR}/UI/Qt/JavaScriptDialog.cpp"
"${LADYBIRD_SOURCE_DIR}/UI/Qt/PermissionPromptPopover.cpp"
"${LADYBIRD_SOURCE_DIR}/UI/Qt/WindowCloseCoordinator.cpp"
+ "${LADYBIRD_SOURCE_DIR}/UI/Qt/X11Window.cpp"
)
+ find_package(PkgConfig REQUIRED)
+ pkg_check_modules(XCB_ICCCM REQUIRED IMPORTED_TARGET xcb-icccm)
+ target_link_libraries(TestUIQt PRIVATE PkgConfig::XCB_ICCCM)
endif()
target_sources(TestUIQt PRIVATE ${TEST_SOURCES})
diff --git a/UI/Qt/Application.cpp b/UI/Qt/Application.cpp
index b577b0f6017..d21d8090ee7 100644
--- a/UI/Qt/Application.cpp
+++ b/UI/Qt/Application.cpp
@@ -29,6 +29,10 @@
# include <UI/Qt/ScrollbarPreferencesMac.h>
#endif
+#if defined(AK_OS_LINUX)
+# include <UI/Qt/X11Window.h>
+#endif
+
#include <QAction>
#include <QClipboard>
#include <QDesktopServices>
@@ -629,6 +633,7 @@ Core::EventLoop& Application::create_platform_event_loop()
m_application = make<LadybirdQApplication>(arguments());
#if defined(AK_OS_LINUX)
QGuiApplication::setDesktopFileName(QStringLiteral("org.ladybird.Ladybird"));
+ Ladybird::clip_translucent_windows_that_cannot_blend();
#endif
#if !defined(Q_OS_MACOS) && !defined(Q_OS_WIN)
install_orca_scripts();
diff --git a/UI/Qt/Autocomplete.cpp b/UI/Qt/Autocomplete.cpp
index 5a52fbfa7e2..71db01b96f4 100644
--- a/UI/Qt/Autocomplete.cpp
+++ b/UI/Qt/Autocomplete.cpp
@@ -86,11 +86,12 @@ public:
protected:
virtual void paintEvent(QPaintEvent*) override
{
+ auto opaque_pixels_only = ChromeStyle::shows_opaque_pixels_only(*this);
QPainter painter(this);
- painter.setRenderHint(QPainter::Antialiasing, true);
+ painter.setRenderHint(QPainter::Antialiasing, !opaque_pixels_only);
auto const& panel = Theme::panel();
painter.setBrush(panel.surface);
- painter.setPen(QPen(panel.edge, 1));
+ painter.setPen(QPen(opaque_pixels_only ? ChromeStyle::opaque_panel_edge() : panel.edge, 1));
painter.drawRoundedRect(QRectF(rect()).adjusted(0.5, 0.5, -0.5, -0.5), POPUP_CORNER_RADIUS, POPUP_CORNER_RADIUS);
}
};
diff --git a/UI/Qt/CMakeLists.txt b/UI/Qt/CMakeLists.txt
index d3a0e97ad63..e87646ad600 100644
--- a/UI/Qt/CMakeLists.txt
+++ b/UI/Qt/CMakeLists.txt
@@ -264,6 +264,10 @@ if (BUILD_TESTING AND NOT BUILD_BROWSER_TESTS_ONLY)
target_sources(TestPerformanceMonitorWidget PRIVATE SystemAppearanceMac.mm)
target_link_libraries(TestPerformanceMonitorWidget PRIVATE "-framework AppKit")
endif()
+ if (LINUX AND NOT ANDROID)
+ target_sources(TestPerformanceMonitorWidget PRIVATE X11Window.cpp)
+ target_link_libraries(TestPerformanceMonitorWidget PRIVATE PkgConfig::XCB_ICCCM)
+ endif()
set_target_properties(TestPerformanceMonitorWidget PROPERTIES AUTOMOC ON)
target_include_directories(TestPerformanceMonitorWidget PRIVATE ${LADYBIRD_SOURCE_DIR})
target_link_libraries(TestPerformanceMonitorWidget PRIVATE Qt6::Widgets LibWebView LibURL AK)
diff --git a/UI/Qt/ChromeStyle.cpp b/UI/Qt/ChromeStyle.cpp
index 69a4379aa57..72080a1d931 100644
--- a/UI/Qt/ChromeStyle.cpp
+++ b/UI/Qt/ChromeStyle.cpp
@@ -26,6 +26,10 @@
#include <QStyleOption>
#include <QWidget>
+#if defined(AK_OS_LINUX)
+# include <UI/Qt/X11Window.h>
+#endif
+
namespace Ladybird::ChromeStyle {
bool is_dark(QPalette const& palette)
@@ -52,6 +56,22 @@ static QColor over(QColor const& color, QColor const& background)
return mix(background, color, static_cast<double>(color.alphaF()));
}
+QColor opaque_panel_edge()
+{
+ auto const& panel = Theme::panel();
+ return over(panel.edge, panel.surface);
+}
+
+bool shows_opaque_pixels_only(QWidget const& widget)
+{
+#if defined(AK_OS_LINUX)
+ return is_clipped_to_its_opaque_pixels(*widget.window());
+#else
+ (void)widget;
+ return false;
+#endif
+}
+
WindowActivity window_activity(QWidget const& widget)
{
return widget.isActiveWindow() ? WindowActivity::Active : WindowActivity::Inactive;
@@ -1892,7 +1912,7 @@ public:
if (element == PE_PanelMenu) {
auto const& panel = Theme::panel();
painter->save();
- painter->setRenderHint(QPainter::Antialiasing, true);
+ painter->setRenderHint(QPainter::Antialiasing, !widget || !shows_opaque_pixels_only(*widget));
painter->setPen(QPen(over(panel.edge, panel.surface), 1));
painter->setBrush(panel.surface);
painter->drawRoundedRect(QRectF(option->rect).adjusted(0.5, 0.5, -0.5, -0.5), MENU_RADIUS - 0.5, MENU_RADIUS - 0.5);
diff --git a/UI/Qt/ChromeStyle.h b/UI/Qt/ChromeStyle.h
index bc8cadc85c7..68d37dfa579 100644
--- a/UI/Qt/ChromeStyle.h
+++ b/UI/Qt/ChromeStyle.h
@@ -72,6 +72,14 @@ QColor chrome_muted_text(QPalette const&);
QFont chrome_title_font(QFont);
QString style_sheet_color(QColor const&);
+
+// The panel outline painted over the panel, for a panel that cannot be partly clear.
+QColor opaque_panel_edge();
+
+// Whether the widget's window shows only the pixels it paints opaque, so that its edges are painted opaque and without
+// antialiasing.
+bool shows_opaque_pixels_only(QWidget const&);
+
QString application_style_sheet(QPalette const&);
QString toolbar_container_style_sheet(QPalette const&);
QString menu_bar_style_sheet(QPalette const&);
diff --git a/UI/Qt/Popover.cpp b/UI/Qt/Popover.cpp
index ef51075b321..8e3ef567df0 100644
--- a/UI/Qt/Popover.cpp
+++ b/UI/Qt/Popover.cpp
@@ -35,6 +35,10 @@
# include <UI/Qt/MacWindow.h>
#endif
+#if defined(AK_OS_LINUX)
+# include <UI/Qt/X11Window.h>
+#endif
+
namespace Ladybird {
static constexpr int POPOVER_ANCHOR_GAP = 4;
@@ -121,6 +125,18 @@ private:
};
#endif
+#if !defined(AK_OS_MACOS)
+// NB: Where windows do not blend with the desktop, a popover would fade in from black and fade out to black.
+static bool popovers_fade()
+{
+# if defined(AK_OS_LINUX)
+ return windows_blend_with_desktop();
+# else
+ return true;
+# endif
+}
+#endif
+
// A popover fades in quickly as it opens, growing out of its anchor.
void Popover::showEvent(QShowEvent* event)
{
@@ -154,7 +170,8 @@ void Popover::showEvent(QShowEvent* event)
});
}
#else
- fade_in(origin);
+ if (popovers_fade())
+ fade_in(origin);
#endif
if (on_visibility_change)
@@ -249,7 +266,7 @@ void Popover::setVisible(bool visible)
#else
if (visible) {
stop_fading();
- } else if (isVisible() && !m_is_finishing_close) {
+ } else if (isVisible() && !m_is_finishing_close && popovers_fade()) {
fade_out(AfterFadingOut::Hide);
return;
}
@@ -264,7 +281,7 @@ void Popover::setVisible(bool visible)
// click outside it, is already gone.
void Popover::closeEvent(QCloseEvent* event)
{
- if (!event->spontaneous() && isVisible() && !m_is_finishing_close) {
+ if (!event->spontaneous() && isVisible() && !m_is_finishing_close && popovers_fade()) {
event->ignore();
fade_out(AfterFadingOut::Close);
return;
diff --git a/UI/Qt/X11Window.cpp b/UI/Qt/X11Window.cpp
index 97ba93a473b..dc08c18218e 100644
--- a/UI/Qt/X11Window.cpp
+++ b/UI/Qt/X11Window.cpp
@@ -4,23 +4,41 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
+#include <AK/AnyOf.h>
+#include <AK/ByteString.h>
+#include <AK/Math.h>
+#include <AK/Vector.h>
#include <LibGfx/Size.h>
#include <UI/Qt/X11Window.h>
+#include <QBitmap>
+#include <QEvent>
#include <QGuiApplication>
+#include <QImage>
+#include <QPointer>
+#include <QRegion>
+#include <QTimer>
#include <QWidget>
+#include <QWindow>
#include <xcb/xcb_icccm.h>
namespace Ladybird {
-void set_x11_window_aspect_ratio(QWidget& widget, Gfx::IntSize aspect_ratio)
+static xcb_connection_t* x11_connection()
{
auto* x11_application = qGuiApp->nativeInterface<QNativeInterface::QX11Application>();
if (!x11_application)
+ return nullptr;
+ return x11_application->connection();
+}
+
+void set_x11_window_aspect_ratio(QWidget& widget, Gfx::IntSize aspect_ratio)
+{
+ auto* connection = x11_connection();
+ if (!connection)
return;
- auto* connection = x11_application->connection();
auto window = static_cast<xcb_window_t>(widget.winId());
// Qt writes all of the window's size hints whenever it changes the window's geometry, so the aspect ratio joins the
@@ -32,4 +50,185 @@ void set_x11_window_aspect_ratio(QWidget& widget, Gfx::IntSize aspect_ratio)
xcb_flush(connection);
}
+// A compositing manager owns the _NET_WM_CM_S<n> selection of each screen it composites.
+static Vector<xcb_atom_t> const& compositing_manager_selections(xcb_connection_t* connection)
+{
+ static auto const selections = [connection] {
+ Vector<xcb_intern_atom_cookie_t> cookies;
+ auto screen_count = xcb_setup_roots_length(xcb_get_setup(connection));
+ for (int screen = 0; screen < screen_count; ++screen) {
+ auto name = ByteString::formatted("_NET_WM_CM_S{}", screen);
+ cookies.append(xcb_intern_atom(connection, false, name.length(), name.characters()));
+ }
+
+ Vector<xcb_atom_t> atoms;
+ for (auto cookie : cookies) {
+ auto* reply = xcb_intern_atom_reply(connection, cookie, nullptr);
+ if (!reply)
+ continue;
+ atoms.append(reply->atom);
+ free(reply);
+ }
+ return atoms;
+ }();
+ return selections;
+}
+
+bool windows_blend_with_desktop()
+{
+ auto* connection = x11_connection();
+ if (!connection)
+ return true;
+
+ // NB: A compositing manager can start or stop at any time, so this asks the display each time.
+ Vector<xcb_get_selection_owner_cookie_t> cookies;
+ for (auto selection : compositing_manager_selections(connection))
+ cookies.append(xcb_get_selection_owner(connection, selection));
+
+ bool has_compositing_manager = false;
+ for (auto cookie : cookies) {
+ auto* reply = xcb_get_selection_owner_reply(connection, cookie, nullptr);
+ if (!reply)
+ continue;
+ if (reply->owner != XCB_WINDOW_NONE)
+ has_compositing_manager = true;
+ free(reply);
+ }
+ return has_compositing_manager;
+}
+
+static constexpr char const* CLIPPED_TO_ITS_OPAQUE_PIXELS = "ladybird_clipped_to_its_opaque_pixels";
+
+bool is_clipped_to_its_opaque_pixels(QWidget const& window)
+{
+ return window.property(CLIPPED_TO_ITS_OPAQUE_PIXELS).toBool();
+}
+
+// NB: A compositing manager blends top-level windows only. A window embedded in another one, such as the suggestions
+// under the address field, never blends with what lies beneath it.
+static bool window_blends_with_what_lies_beneath(QWidget& window)
+{
+ if (window.windowHandle() && window.windowHandle()->parent())
+ return false;
+ return windows_blend_with_desktop();
+}
+
+static void clip_translucent_window(QWidget& window)
+{
+ if (window_blends_with_what_lies_beneath(window)) {
+ if (is_clipped_to_its_opaque_pixels(window)) {
+ window.clearMask();
+ window.setProperty(CLIPPED_TO_ITS_OPAQUE_PIXELS, false);
+ window.update();
+ }
+ return;
+ }
+
+ if (window.size().isEmpty())
+ return;
+
+ // NB: The window paints differently once clipped, such as with opaque edges, so it is marked before its picture
+ // is taken.
+ if (!is_clipped_to_its_opaque_pixels(window)) {
+ window.setProperty(CLIPPED_TO_ITS_OPAQUE_PIXELS, true);
+ window.update();
+ }
+
+ // NB: The window paints no background of its own, so whatever stays clear in the picture is clear in the window. A
+ // picture of a window leaves out what lies outside its mask, so the mask it has grown out of goes first.
+ window.clearMask();
+ auto device_pixel_ratio = window.devicePixelRatioF();
+ QImage picture(window.size() * device_pixel_ratio, QImage::Format_ARGB32_Premultiplied);
+ picture.setDevicePixelRatio(device_pixel_ratio);
+ picture.fill(Qt::transparent);
+ window.render(&picture, {}, {}, QWidget::DrawChildren);
+
+ // NB: A pixel that is partly clear shows darkened, so only the opaque pixels stay. That leaves out shadows and the
+ // antialiased edges of rounded corners. The mask is in logical pixels, so one stays only if all of the device
+ // pixels it covers are opaque.
+ struct DevicePixels {
+ int start { 0 };
+ int end { 0 };
+ };
+ auto device_pixels_covered = [&](int logical_pixel, int device_size) {
+ return DevicePixels {
+ .start = static_cast<int>(AK::floor(logical_pixel * device_pixel_ratio)),
+ .end = min(static_cast<int>(AK::ceil((logical_pixel + 1) * device_pixel_ratio)), device_size),
+ };
+ };
+ QImage opaque_pixels(window.size(), QImage::Format_ARGB32_Premultiplied);
+ for (int y = 0; y < window.height(); ++y) {
+ auto rows = device_pixels_covered(y, picture.height());
+ auto* mask_pixels = reinterpret_cast<QRgb*>(opaque_pixels.scanLine(y));
+ for (int x = 0; x < window.width(); ++x) {
+ auto columns = device_pixels_covered(x, picture.width());
+ bool is_opaque = rows.start < rows.end && columns.start < columns.end;
+ for (int device_y = rows.start; is_opaque && device_y < rows.end; ++device_y) {
+ auto const* pixels = reinterpret_cast<QRgb const*>(picture.constScanLine(device_y));
+ for (int device_x = columns.start; is_opaque && device_x < columns.end; ++device_x)
+ is_opaque = qAlpha(pixels[device_x]) == 255;
+ }
+ mask_pixels[x] = is_opaque ? 0xff000000 : 0;
+ }
+ }
+ QRegion opaque_region { QBitmap::fromImage(opaque_pixels.createAlphaMask()) };
+
+ // NB: QWidget takes an empty mask to mean no mask at all. A window that paints nothing opaque, such as one that is
+ // only a shadow, gets a mask that lies wholly outside it instead.
+ if (opaque_region.isEmpty())
+ opaque_region = QRect { window.width(), window.height(), 1, 1 };
+
+ window.setMask(opaque_region);
+}
+
+class TranslucentWindowClipper final : public QObject {
+public:
+ AK_ALLOC_WITH_KMALLOC;
+
+ explicit TranslucentWindowClipper(QObject* parent)
+ : QObject(parent)
+ {
+ }
+
+private:
+ virtual bool eventFilter(QObject* object, QEvent* event) override
+ {
+ if (event->type() != QEvent::Show && event->type() != QEvent::Resize)
+ return false;
+ if (!object->isWidgetType())
+ return false;
+
+ auto& widget = static_cast<QWidget&>(*object);
+ if (!widget.isWindow() || !widget.testAttribute(Qt::WA_TranslucentBackground))
+ return false;
+
+ // NB: A window is clipped as it shows, so that it never appears unclipped, and again once its contents have
+ // been laid out for its new size.
+ if (event->type() == QEvent::Show)
+ clip_translucent_window(widget);
+ if (any_of(m_windows_to_clip, [&](auto const& window) { return window == &widget; }))
+ return false;
+ m_windows_to_clip.append(&widget);
+ if (m_windows_to_clip.size() == 1) {
+ QTimer::singleShot(0, this, [this] {
+ auto windows = move(m_windows_to_clip);
+ for (auto& window : windows) {
+ if (window && window->isVisible())
+ clip_translucent_window(*window);
+ }
+ });
+ }
+ return false;
+ }
+
+ Vector<QPointer<QWidget>> m_windows_to_clip;
+};
+
+void clip_translucent_windows_that_cannot_blend()
+{
+ if (!x11_connection())
+ return;
+ qApp->installEventFilter(new TranslucentWindowClipper(qApp));
+}
+
}
diff --git a/UI/Qt/X11Window.h b/UI/Qt/X11Window.h
index 862959ce8f1..e5b5516b1d3 100644
--- a/UI/Qt/X11Window.h
+++ b/UI/Qt/X11Window.h
@@ -15,4 +15,18 @@ namespace Ladybird {
// Does nothing unless the widget's window is on an X11 display.
void set_x11_window_aspect_ratio(QWidget&, Gfx::IntSize);
+// Whether translucent windows blend with what lies beneath them. Only an X11 display without a compositing manager
+// lacks that.
+bool windows_blend_with_desktop();
+
+// A translucent window shows black where it is clear, and darkened where it is partly clear, unless it blends with
+// what lies beneath it. That needs a compositing manager, and even then only a top-level window blends. From then on,
+// each translucent window that cannot blend is cut down to the pixels it paints fully opaque, so a card loses its
+// shadow instead of sitting on a black box. Does nothing unless the application is on an X11 display.
+void clip_translucent_windows_that_cannot_blend();
+
+// Whether a translucent window has been cut down to the pixels it paints opaque. Its partly clear pixels show darkened
+// or not at all, so it paints its edges opaque.
+bool is_clipped_to_its_opaque_pixels(QWidget const& window);
+
}