Truly independent web browser
UI/Qt: Give the web view rounded corners on macOS
Andreas Klingcf26179096a7
diff --git a/UI/Qt/ChromeLayout.h b/UI/Qt/ChromeLayout.hindex 17050f5b577..53050f074f8 100644--- a/UI/Qt/ChromeLayout.h+++ b/UI/Qt/ChromeLayout.h@@ -33,7 +33,13 @@ static constexpr BrowserChromeLayoutPolicy browser_chrome_layout_policy()return {.expanded_sidebar_width = 232,.collapsed_sidebar_width = 52,+#ifdef AK_OS_MACOS+ // NB: Leaves an even space around the location field, as the rounded web view below it has no border of its own+ // to absorb an odd one.+ .toolbar_height = 40,+#else.toolbar_height = 41,+#endif};}diff --git a/UI/Qt/ChromeStyle.cpp b/UI/Qt/ChromeStyle.cppindex 6b7975058df..c2bca6638d8 100644--- a/UI/Qt/ChromeStyle.cpp+++ b/UI/Qt/ChromeStyle.cpp@@ -471,7 +471,15 @@ QWidget#LadybirdNavigationToolbar QToolButton#LadybirdCloseWindowButton[pressedOcolor: {4};background: transparent;}}-)",+)"+#if defined(AK_OS_MACOS)+ R"(+QWidget#LadybirdToolbarContainer {{+ border-bottom: 0;+}}+)"+#endif+ ,background, surface_hover, surface_pressed, separator, text, disabled_text, close_hover, close_text,window_controls_separator, badge_surface, badge_border, badge_surface_hover, badge_surface_pressed);}@@ -1331,7 +1339,23 @@ QToolButton#LadybirdCloseWindowButton[pressedOutside="true"] {{color: {4};background: transparent;}}-)",+)"+#if defined(AK_OS_MACOS)+ R"(+QWidget#LadybirdVerticalTabBar {{+ border-right-color: transparent;+}}++QWidget#LadybirdVerticalTabBar[verticalTabsPosition="right"] {{+ border-left-color: transparent;+}}++QWidget#LadybirdVerticalTabsContentSeparator {{+ background: transparent;+}}+)"+#endif+ ,background, hover, pressed, control_border, text, close_hover, close_text, sidebar_separator, sidebar_separator_hover);}diff --git a/UI/Qt/Tab.cpp b/UI/Qt/Tab.cppindex dadf7c34fde..9c54746bd97 100644--- a/UI/Qt/Tab.cpp+++ b/UI/Qt/Tab.cpp@@ -253,6 +253,9 @@ QToolButton* Tab::create_navigation_history_toolbar_button(QWidget& parent, QAct}static constexpr int TOOLBAR_HORIZONTAL_MARGIN = 12;+#ifdef AK_OS_MACOS+static constexpr int WEB_VIEW_INSET = 6;+#endifstatic constexpr int TOOLBAR_VERTICAL_MARGIN = 2;static constexpr int TOOLBAR_SIDEBAR_TOGGLE_NAVIGATION_GAP = 8;static constexpr int TOOLBAR_LOCATION_EDIT_SIDE_GAP = 32;@@ -663,7 +666,14 @@ Tab::Tab(BrowserWindow* window, Optional<WebView::CanonicalTraversable&> traverstoolbar_container_layout->addWidget(m_toolbar);toolbar_container_layout->addWidget(m_bookmarks_bar_container);+#ifdef AK_OS_MACOS+ m_view_layout = new QHBoxLayout;+ m_view_layout->addWidget(m_view);+ update_web_view_frame(true, {});+ tab_layout->addLayout(m_view_layout);+#elsetab_layout->addWidget(m_view);+#endiftab_layout->addWidget(m_find_in_page);update_bookmarks_bar_visibility();@@ -1347,6 +1357,24 @@ void Tab::set_vertical_tabs_position(WebView::VerticalTabsPosition position)m_toolbar->layout()->invalidate();}+#ifdef AK_OS_MACOS+void Tab::update_web_view_frame(bool chrome_visible, Optional<WebView::VerticalTabsPosition> sidebar_position)+{+ // With the browser chrome hidden, the page fills the window edge to edge.+ if (!chrome_visible) {+ m_view_layout->setContentsMargins(0, 0, 0, 0);+ m_view->set_has_rounded_corners(false);+ return;+ }++ // Keep the web view's rounded corners clear of the window edges. The sidebar has its own padding.+ auto left = sidebar_position == WebView::VerticalTabsPosition::Left ? 0 : WEB_VIEW_INSET;+ auto right = sidebar_position == WebView::VerticalTabsPosition::Right ? 0 : WEB_VIEW_INSET;+ m_view_layout->setContentsMargins(left, 0, right, WEB_VIEW_INSET);+ m_view->set_has_rounded_corners(true);+}+#endif+void Tab::set_toolbar_container_in_tab_layout(bool in_tab_layout){auto* tab_layout = static_cast<QBoxLayout*>(layout());@@ -1374,7 +1402,14 @@ void Tab::set_toolbar_window_controls_visible(bool visible)m_toolbar_window_controls_separator->setVisible(has_custom_window_controls);m_toolbar_window_controls->setVisible(has_native_window_controls || has_custom_window_controls);- m_toolbar->layout()->setContentsMargins(TOOLBAR_HORIZONTAL_MARGIN, TOOLBAR_VERTICAL_MARGIN, has_custom_window_controls ? TOOLBAR_WINDOW_CONTROLS_RIGHT_MARGIN : TOOLBAR_HORIZONTAL_MARGIN, TOOLBAR_VERTICAL_MARGIN);+ auto top_margin = TOOLBAR_VERTICAL_MARGIN;+#ifdef AK_OS_MACOS+ // NB: A toolbar holding the window controls is the top of the window, whose outline covers its first point.+ auto window_outline_width = visible ? 1 : 0;+ top_margin += window_outline_width;+ m_toolbar->setFixedHeight(browser_chrome_layout_policy().toolbar_height + window_outline_width);+#endif+ m_toolbar->layout()->setContentsMargins(TOOLBAR_HORIZONTAL_MARGIN, top_margin, has_custom_window_controls ? TOOLBAR_WINDOW_CONTROLS_RIGHT_MARGIN : TOOLBAR_HORIZONTAL_MARGIN, TOOLBAR_VERTICAL_MARGIN);m_toolbar->layout()->invalidate();}@@ -1574,6 +1609,15 @@ void Tab::set_screen_wake_lock_state(Web::ScreenWakeLockState wake_lock_state)}}+#ifdef AK_OS_MACOS+void Tab::paintEvent(QPaintEvent*)+{+ // The inset around the web view should match the chrome surrounding it.+ QPainter painter(this);+ painter.fillRect(rect(), ChromeStyle::chrome_background(window()->palette()));+}+#endif+void Tab::resizeEvent(QResizeEvent* event){QWidget::resizeEvent(event);diff --git a/UI/Qt/Tab.h b/UI/Qt/Tab.hindex 30106e2f383..ed36aea6951 100644--- a/UI/Qt/Tab.h+++ b/UI/Qt/Tab.h@@ -116,6 +116,9 @@ public:void set_vertical_tabs_enabled(bool);void set_vertical_tabs_position(WebView::VerticalTabsPosition);+#ifdef AK_OS_MACOS+ void update_web_view_frame(bool chrome_visible, Optional<WebView::VerticalTabsPosition> sidebar_position);+#endifvoid set_window(BrowserWindow&);void set_toolbar_container_in_tab_layout(bool);void set_toolbar_window_controls_visible(bool);@@ -141,6 +144,9 @@ signals:private:void location_edit_return_pressed(String, Optional<URL::URL>, WebView::OmniboxDestinationKind);+#ifdef AK_OS_MACOS+ virtual void paintEvent(QPaintEvent*) override;+#endifvirtual void resizeEvent(QResizeEvent*) override;virtual void hideEvent(QHideEvent*) override;virtual bool event(QEvent*) override;@@ -226,6 +232,9 @@ private:bool m_is_updating_chrome_style { false };bool m_vertical_tabs_enabled { false };WebView::VerticalTabsPosition m_vertical_tabs_position { WebView::VerticalTabsPosition::Left };+#ifdef AK_OS_MACOS+ QHBoxLayout* m_view_layout { nullptr };+#endifint m_loading_animation_frame { 0 };QMenu* m_context_menu { nullptr };diff --git a/UI/Qt/TabBar.cpp b/UI/Qt/TabBar.cppindex 41d511bce18..c697b45c845 100644--- a/UI/Qt/TabBar.cpp+++ b/UI/Qt/TabBar.cpp@@ -2046,6 +2046,9 @@ void TabWidget::insert_tab(int index, Tab* widget, QString const& label)widget->set_vertical_tabs_enabled(m_vertical_tabs_enabled);widget->set_vertical_tabs_position(m_vertical_tabs_position);+#ifdef AK_OS_MACOS+ update_web_view_frame(*widget);+#endifupdate_toolbar_placement();update_tab_layout();@@ -2799,6 +2802,10 @@ void TabWidget::update_tab_chrome_visibility()m_toolbar_container->setVisible(m_tab_bar_visible);m_vertical_tab_bar_column->setVisible(m_tab_bar_visible && !is_horizontal);update_vertical_tabs_content_separator();+#ifdef AK_OS_MACOS+ for (int index = 0; index < m_stacked_widget->count(); ++index)+ update_web_view_frame(*tab(index));+#endifauto show_window_controls = m_window_controls_visible&& m_tab_bar_visible@@ -2806,6 +2813,16 @@ void TabWidget::update_tab_chrome_visibility()m_window_controls->setVisible(show_window_controls);}+#ifdef AK_OS_MACOS+void TabWidget::update_web_view_frame(Tab& tab)+{+ Optional<WebView::VerticalTabsPosition> sidebar_position;+ if (m_tab_bar_visible && m_tab_bar->tab_layout() != TabLayout::Horizontal)+ sidebar_position = m_vertical_tabs_position;+ tab.update_web_view_frame(m_tab_bar_visible, sidebar_position);+}+#endif+void TabWidget::recreate_icons(){m_tab_bar->recreate_icons();diff --git a/UI/Qt/TabBar.h b/UI/Qt/TabBar.hindex a617c9b52f8..f2bfd37b907 100644--- a/UI/Qt/TabBar.h+++ b/UI/Qt/TabBar.h@@ -255,6 +255,9 @@ private:void update_vertical_tabs_button_layout();void update_tab_layout();void update_tab_chrome_visibility();+#ifdef AK_OS_MACOS+ void update_web_view_frame(Tab&);+#endifvoid recreate_icons();void update_chrome_style();void update_vertical_tabs_overlay_geometry();diff --git a/UI/Qt/WebContentView.cpp b/UI/Qt/WebContentView.cppindex fbea95c88e6..1aef9c1b7df 100644--- a/UI/Qt/WebContentView.cpp+++ b/UI/Qt/WebContentView.cpp@@ -34,6 +34,7 @@#include <LibWebView/WebCompatReport.h>#include <LibWebView/WebContentClient.h>#include <UI/Qt/Application.h>+#include <UI/Qt/ChromeStyle.h>#include <UI/Qt/CrashReportReviewWidget.h>#include <UI/Qt/ReportReview.h>#include <UI/Qt/ScreenshotSelectionOverlay.h>@@ -111,7 +112,7 @@ WebContentView::WebContentView(QWidget* window, Optional<WebView::CanonicalTravesetParent(window);setApi(QRhiWidget::Api::Direct3D11);#elif defined(LADYBIRD_QT_USE_IOSURFACE_LAYER)- // The page is shown by a layer of this widget's own native view; Qt neither erases nor paints the area under it.+ // The page is shown by a layer of this widget's own native view; Qt only paints the chrome behind its corners.setAttribute(Qt::WA_DontCreateNativeAncestors);setAttribute(Qt::WA_NativeWindow);setAttribute(Qt::WA_OpaquePaintEvent);@@ -1141,6 +1142,15 @@ void WebContentView::paintEvent(QPaintEvent*)}#endif+#ifdef LADYBIRD_QT_USE_IOSURFACE_LAYER+void WebContentView::paintEvent(QPaintEvent*)+{+ // The page layer has rounded corners, so the area outside them should match the chrome around this view.+ QPainter painter(this);+ painter.fillRect(rect(), ChromeStyle::chrome_background(window()->palette()));+}+#endif+// The page as the view currently shows it, in device pixels.QImage WebContentView::visible_snapshot() const{diff --git a/UI/Qt/WebContentView.h b/UI/Qt/WebContentView.hindex d508df40ef3..5ab4a31f1a8 100644--- a/UI/Qt/WebContentView.h+++ b/UI/Qt/WebContentView.h@@ -119,7 +119,7 @@ public:WebContentView(QWidget* window, Optional<WebView::CanonicalTraversable&> traversable = {}, WebContentViewInitialState initial_state = {});virtual ~WebContentView() override;-#if !defined(LADYBIRD_QT_USE_RHI_WIDGET) && !defined(LADYBIRD_QT_USE_IOSURFACE_LAYER)+#if !defined(LADYBIRD_QT_USE_RHI_WIDGET)virtual void paintEvent(QPaintEvent*) override;#endifvirtual void resizeEvent(QResizeEvent*) override;@@ -167,6 +167,10 @@ public:void update_palette(PaletteMode = PaletteMode::Default);Optional<QPixmap> tab_preview_pixmap(QSize const& maximum_size) const;+#ifdef LADYBIRD_QT_USE_IOSURFACE_LAYER+ void set_has_rounded_corners(bool);+#endif+WebView::AccessibilityTreeManager const* accessibility_tree_manager() const { return m_accessibility_manager.ptr(); }#if !defined(Q_OS_MACOS) && !defined(Q_OS_WIN)@@ -308,6 +312,7 @@ private:void destroy_iosurface_layer();void* m_iosurface_layer { nullptr };+ bool m_has_rounded_corners { false };#endif#ifdef LADYBIRD_QT_USE_RHI_WIDGETdiff --git a/UI/Qt/WebContentViewMac.mm b/UI/Qt/WebContentViewMac.mmindex 345e0c24417..063da12cd85 100644--- a/UI/Qt/WebContentViewMac.mm+++ b/UI/Qt/WebContentViewMac.mm@@ -16,11 +16,20 @@namespace Ladybird {+static constexpr CGFloat WEB_VIEW_CORNER_RADIUS = 10;+static CALayer* as_layer(void* layer){return static_cast<CALayer*>(layer);}+void WebContentView::set_has_rounded_corners(bool has_rounded_corners)+{+ m_has_rounded_corners = has_rounded_corners;+ if (m_iosurface_layer)+ as_layer(m_iosurface_layer).cornerRadius = has_rounded_corners ? WEB_VIEW_CORNER_RADIUS : 0;+}+bool WebContentView::ensure_iosurface_layer_attached_to_native_view(){auto* window_handle = windowHandle();@@ -36,8 +45,11 @@ bool WebContentView::ensure_iosurface_layer_attached_to_native_view()if (!m_iosurface_layer) {auto* layer = [[CALayer alloc] init];- layer.opaque = YES;+ // NB: The chrome painted behind the rounded corners shows through, so the layer is not opaque.+ layer.opaque = NO;layer.masksToBounds = YES;+ layer.cornerRadius = m_has_rounded_corners ? WEB_VIEW_CORNER_RADIUS : 0;+ layer.cornerCurve = kCACornerCurveContinuous;// Without this every new frame would cross-fade in and geometry changes would slide.layer.actions = @ {@"contents" : NSNull.null,@@ -48,6 +60,7 @@ bool WebContentView::ensure_iosurface_layer_attached_to_native_view()@"position" : NSNull.null,@"hidden" : NSNull.null,@"backgroundColor" : NSNull.null,+ @"cornerRadius" : NSNull.null,};m_iosurface_layer = layer;}