Skip to content

Truly independent web browser

UI/Qt: Let the page see the mouse around floating surfaces on macOS

Andreas Kling7f4113d3f9db
diff --git a/UI/Qt/MacWindow.h b/UI/Qt/MacWindow.h
index a457788cb32..27d151a7c4c 100644
--- a/UI/Qt/MacWindow.h
+++ b/UI/Qt/MacWindow.h
@@ -6,6 +6,7 @@
#pragma once
+#include <AK/Noncopyable.h>
#include <AK/Optional.h>
#include <AK/Platform.h>
#include <AK/Types.h>
@@ -16,6 +17,7 @@
class QColor;
class QDrag;
+class QMargins;
class QWheelEvent;
class QWidget;
@@ -46,14 +48,31 @@ Optional<AppKitScrollMomentumTransition> take_appkit_scroll_momentum_transition_
void make_appkit_window_first_responder(QWidget&);
// Rounds the corners of a native child widget's view, which shows what is below it through them, and outlines it.
void round_appkit_view_corners(QWidget&, qreal radius, QColor const& border_color);
-// Puts the native view of the first widget right below the second one's, among the views of the second one's superview.
-void stack_appkit_view_below(QWidget&, QWidget& sibling);
// The opacity of a native view embedded in another window, which has no window opacity of its own.
void set_appkit_view_opacity(QWidget&, qreal opacity);
// Clips a native view to rounded bottom corners, for a sheet that hangs from what is above it.
void round_appkit_view_bottom_corners(QWidget&, qreal radius);
// Casts a shadow around a native view. Being outside of the view, the shadow takes no mouse input.
void cast_appkit_view_shadow(QWidget&, qreal corner_radius, qreal blur_radius, qreal y_offset, QColor const&);
+// A shadow right below a native view, in a plain view of its own. It is no Qt window, as Qt on macOS takes a child
+// window to be under the mouse even when it is transparent for input, and keeps the mouse from what is below it.
+class AppKitViewShadow {
+ AK_MAKE_NONCOPYABLE(AppKitViewShadow);
+ AK_MAKE_NONMOVABLE(AppKitViewShadow);
+
+public:
+ AppKitViewShadow();
+ ~AppKitViewShadow();
+
+ // The shadow is clipped to the given room around the view.
+ void follow(QWidget&, QMargins const& room, qreal corner_radius, qreal blur_radius, qreal y_offset, QColor const& shadow_color, QColor const& fill_color);
+ void hide();
+ void set_opacity(qreal);
+
+private:
+ void* m_view { nullptr };
+ void* m_caster { nullptr };
+};
bool start_appkit_window_drag(QWidget&);
Qt::DropAction execute_appkit_tab_drag(QWidget&, QDrag&);
void show_appkit_dictionary_lookup(QWidget&, WebView::DictionaryLookup const&, Gfx::IntPoint);
diff --git a/UI/Qt/MacWindow.mm b/UI/Qt/MacWindow.mm
index 88e8c62f352..3bc5dd64bcc 100644
--- a/UI/Qt/MacWindow.mm
+++ b/UI/Qt/MacWindow.mm
@@ -12,9 +12,11 @@
#include <LibGfx/Point.h>
#include <LibGfx/Size.h>
#include <QAbstractNativeEventFilter>
+#include <QColor>
#include <QCoreApplication>
#include <QDrag>
#include <QGuiApplication>
+#include <QMargins>
#include <QMouseEvent>
#include <QPointer>
#include <QWheelEvent>
@@ -23,6 +25,7 @@
#include <UI/Qt/WebContentView.h>
#import <Cocoa/Cocoa.h>
+#import <QuartzCore/QuartzCore.h>
#import <UI/Qt/DictionaryLookup.h>
#import <dlfcn.h>
#import <objc/runtime.h>
@@ -137,6 +140,18 @@
@end
+@interface LadybirdShadowView : NSView
+@end
+
+@implementation LadybirdShadowView
+
+- (NSView*)hitTest:(NSPoint)point
+{
+ return nil;
+}
+
+@end
+
namespace Ladybird {
static NSEvent* s_latest_window_drag_event;
@@ -635,17 +650,6 @@ void round_appkit_view_corners(QWidget& widget, qreal radius, QColor const& bord
CGColorRelease(color);
}
-void stack_appkit_view_below(QWidget& widget, QWidget& sibling)
-{
- if (QGuiApplication::platformName() != "cocoa")
- return;
- auto* view = reinterpret_cast<NSView*>(widget.winId());
- auto* sibling_view = reinterpret_cast<NSView*>(sibling.winId());
- if (!view || !sibling_view || !sibling_view.superview)
- return;
- [sibling_view.superview addSubview:view positioned:NSWindowBelow relativeTo:sibling_view];
-}
-
void round_appkit_view_bottom_corners(QWidget& widget, qreal radius)
{
if (QGuiApplication::platformName() != "cocoa")
@@ -684,6 +688,76 @@ void cast_appkit_view_shadow(QWidget& widget, qreal corner_radius, qreal blur_ra
CGPathRelease(shadow_path);
}
+AppKitViewShadow::AppKitViewShadow()
+{
+ auto* view = [[LadybirdShadowView alloc] initWithFrame:NSZeroRect];
+ view.wantsLayer = YES;
+ view.layer.masksToBounds = YES;
+ view.hidden = YES;
+ auto* caster = [[CALayer alloc] init];
+ [view.layer addSublayer:caster];
+ m_view = view;
+ m_caster = caster;
+}
+
+AppKitViewShadow::~AppKitViewShadow()
+{
+ auto* view = static_cast<NSView*>(m_view);
+ [view removeFromSuperview];
+ [view release];
+ [static_cast<CALayer*>(m_caster) release];
+}
+
+void AppKitViewShadow::follow(QWidget& widget, QMargins const& room, qreal corner_radius, qreal blur_radius, qreal y_offset, QColor const& shadow_color, QColor const& fill_color)
+{
+ if (QGuiApplication::platformName() != "cocoa")
+ return;
+ auto* surface = reinterpret_cast<NSView*>(widget.winId());
+ if (!surface || !surface.superview)
+ return;
+ auto* view = static_cast<NSView*>(m_view);
+ auto* caster = static_cast<CALayer*>(m_caster);
+
+ [CATransaction begin];
+ [CATransaction setDisableActions:YES];
+
+ auto surface_frame = surface.frame;
+ auto room_at_origin = surface.superview.isFlipped ? room.top() : room.bottom();
+ view.frame = NSMakeRect(surface_frame.origin.x - room.left(), surface_frame.origin.y - room_at_origin,
+ surface_frame.size.width + room.left() + room.right(), surface_frame.size.height + room.top() + room.bottom());
+ [surface.superview addSubview:view positioned:NSWindowBelow relativeTo:surface];
+ view.hidden = NO;
+
+ // NB: This view is not flipped, unlike Qt's, so the bottom of the room comes first and a shadow falls to lower y.
+ auto caster_frame = CGRectMake(room.left(), room.bottom(), surface_frame.size.width, surface_frame.size.height);
+ caster.frame = caster_frame;
+ caster.cornerRadius = corner_radius;
+ auto* fill = CGColorCreateSRGB(fill_color.redF(), fill_color.greenF(), fill_color.blueF(), fill_color.alphaF());
+ caster.backgroundColor = fill;
+ CGColorRelease(fill);
+ auto* shadow = CGColorCreateSRGB(shadow_color.redF(), shadow_color.greenF(), shadow_color.blueF(), shadow_color.alphaF());
+ caster.shadowColor = shadow;
+ CGColorRelease(shadow);
+ caster.shadowOpacity = 1;
+ caster.shadowRadius = blur_radius;
+ caster.shadowOffset = CGSizeMake(0, -y_offset);
+ auto* shadow_path = CGPathCreateWithRoundedRect(CGRectMake(0, 0, caster_frame.size.width, caster_frame.size.height), corner_radius, corner_radius, nullptr);
+ caster.shadowPath = shadow_path;
+ CGPathRelease(shadow_path);
+
+ [CATransaction commit];
+}
+
+void AppKitViewShadow::hide()
+{
+ static_cast<NSView*>(m_view).hidden = YES;
+}
+
+void AppKitViewShadow::set_opacity(qreal opacity)
+{
+ static_cast<NSView*>(m_view).alphaValue = opacity;
+}
+
void set_appkit_view_opacity(QWidget& widget, qreal opacity)
{
if (QGuiApplication::platformName() != "cocoa")
diff --git a/UI/Qt/Popover.cpp b/UI/Qt/Popover.cpp
index 1630a02ebd2..f104e6993df 100644
--- a/UI/Qt/Popover.cpp
+++ b/UI/Qt/Popover.cpp
@@ -6,6 +6,7 @@
#include <AK/StdLibExtras.h>
#include <AK/TemporaryChange.h>
+#include <AK/TypeCasts.h>
#include <UI/Qt/ChromeStyle.h>
#include <UI/Qt/Motion.h>
#include <UI/Qt/Popover.h>
@@ -511,6 +512,7 @@ void cast_popover_shadow(QWidget& card)
static constexpr int SHEET_SHADOW_MARGIN = 100;
+#if !defined(AK_OS_MACOS)
// The shape that casts the shadow. It lies under the surface, so only the shadow around it shows.
class FloatingShadowCaster final : public QWidget {
public:
@@ -548,17 +550,18 @@ private:
qreal m_corner_radius { 0 };
FloatingShadowTone m_tone { FloatingShadowTone::FollowTheme };
};
+#endif
class FloatingShadow final : public QWidget {
public:
AK_ALLOC_WITH_KMALLOC;
- // NB: On macOS, the shadow is a window of its own embedded in the browser window, so it stacks above the page's
- // native view, while letting clicks through to it. Elsewhere the page and the surface are drawn in the window
- // itself, so the shadow is a widget beside the surface.
+ // NB: On macOS, the shadow is a plain view right below the surface's native view, so it stacks above the page's
+ // native view, while letting the mouse through to it, and this widget stays hidden. Elsewhere the page and the
+ // surface are drawn in the window itself, so the shadow is a widget beside the surface.
FloatingShadow(QWidget& surface, qreal corner_radius, FloatingShadowTone tone, FloatingShadowKind kind)
#if defined(AK_OS_MACOS)
- : QWidget(surface.window(), Qt::Tool | Qt::FramelessWindowHint | Qt::NoDropShadowWindowHint | Qt::WindowTransparentForInput)
+ : QWidget(surface.window())
#else
: QWidget(surface.parentWidget())
#endif
@@ -566,16 +569,20 @@ public:
, m_margins(kind == FloatingShadowKind::Sheet
? QMargins(SHEET_SHADOW_MARGIN, 0, SHEET_SHADOW_MARGIN, SHEET_SHADOW_MARGIN)
: POPOVER_SHADOW_MARGINS)
+#if defined(AK_OS_MACOS)
+ , m_corner_radius(corner_radius)
+ , m_tone(tone)
+ , m_kind(kind)
+#endif
{
+#if !defined(AK_OS_MACOS)
setAttribute(Qt::WA_TranslucentBackground);
setAttribute(Qt::WA_ShowWithoutActivating);
setAttribute(Qt::WA_TransparentForMouseEvents);
-#if defined(AK_OS_MACOS)
- setAttribute(Qt::WA_NativeWindow);
-#endif
auto* layout = new QVBoxLayout(this);
layout->setContentsMargins(m_margins);
layout->addWidget(new FloatingShadowCaster(*this, corner_radius, tone, kind));
+#endif
watch_surface_and_its_ancestors();
connect(&surface, &QObject::destroyed, this, &QObject::deleteLater);
@@ -583,6 +590,10 @@ public:
follow_surface();
}
+#if defined(AK_OS_MACOS)
+ void set_opacity(qreal opacity) { m_view_shadow.set_opacity(opacity); }
+#endif
+
private:
virtual bool eventFilter(QObject*, QEvent* event) override
{
@@ -592,6 +603,7 @@ private:
case QEvent::Show:
case QEvent::Hide:
case QEvent::ZOrderChange:
+ case QEvent::PaletteChange:
follow_surface();
break;
case QEvent::ParentChange:
@@ -622,7 +634,11 @@ private:
TemporaryChange following { m_is_following, true };
if (!m_surface->isVisible()) {
+#if defined(AK_OS_MACOS)
+ m_view_shadow.hide();
+#else
hide();
+#endif
return;
}
@@ -631,17 +647,13 @@ private:
if (parentWidget() != m_surface->window())
setParent(m_surface->window(), windowFlags());
- // NB: The shadow is stacked right below the surface's native view, among the views of the surface's native
- // parent, so it is placed in that parent's coordinates.
- auto* host = m_surface->nativeParentWidget();
- if (!host)
- host = m_surface->window();
- (void)winId();
- windowHandle()->setParent(host->windowHandle());
- auto surface_rect = QRect(m_surface->mapTo(host, QPoint()), m_surface->size());
- setGeometry(surface_rect.marginsAdded(m_margins));
- show();
- stack_appkit_view_below(*this, *m_surface);
+ // NB: Core Animation's blur radius is a little over 40% of the CSS one of the design.
+ auto is_dark = m_tone == FloatingShadowTone::Dark || Theme::is_dark();
+ auto fill_color = m_tone == FloatingShadowTone::Dark ? QColor(28, 28, 30) : Theme::panel().surface;
+ if (m_kind == FloatingShadowKind::Sheet)
+ m_view_shadow.follow(*m_surface, m_margins, m_corner_radius, 17, 18, QColor(0, 0, 0, is_dark ? 140 : 56), fill_color);
+ else
+ m_view_shadow.follow(*m_surface, m_margins, m_corner_radius, 15, 14, QColor(0, 0, 0, is_dark ? 115 : 41), fill_color);
#else
if (parentWidget() != m_surface->parentWidget())
setParent(m_surface->parentWidget());
@@ -654,6 +666,12 @@ private:
QPointer<QWidget> m_surface;
QMargins m_margins;
bool m_is_following { false };
+#if defined(AK_OS_MACOS)
+ qreal m_corner_radius { 0 };
+ FloatingShadowTone m_tone { FloatingShadowTone::FollowTheme };
+ FloatingShadowKind m_kind { FloatingShadowKind::Popover };
+ AppKitViewShadow m_view_shadow;
+#endif
};
QWidget& cast_floating_shadow(QWidget& surface, qreal corner_radius, FloatingShadowTone tone, FloatingShadowKind kind)
@@ -664,7 +682,10 @@ QWidget& cast_floating_shadow(QWidget& surface, qreal corner_radius, FloatingSha
void set_floating_window_opacity(QWidget& window, qreal opacity)
{
#if defined(AK_OS_MACOS)
- set_appkit_view_opacity(window, opacity);
+ if (auto* shadow = as_if<FloatingShadow>(window))
+ shadow->set_opacity(opacity);
+ else
+ set_appkit_view_opacity(window, opacity);
#else
// NB: Wayland has no window opacity, and a widget in a window has none either, so those fade by an effect instead.
static auto const has_window_opacity = !QGuiApplication::platformName().startsWith(QStringLiteral("wayland"));