Title: [250648] trunk/Source/WebKit
Revision
250648
Author
[email protected]
Date
2019-10-03 01:54:51 -0700 (Thu, 03 Oct 2019)

Log Message

[GTK] Don't hardcode swipe navigation gesture style
https://bugs.webkit.org/show_bug.cgi?id=202447

Patch by Alexander Mikhaylenko <[email protected]> on 2019-10-03
Reviewed by Carlos Garcia Campos.

Use GTK foreign drawing to draw swipe gesture dimming and shadow instead of
hardcoded opacity and gradient values.

Have separate elements for dimming and shadow. Additionally, add border
and outline. Dimming layer opacity always matches progress, shadow opacity
is 1 except when the page is almost completely retracted. Border and outline
opacity is always 1. Outline is drawn above the top page. For determining
shadow, border and outline width, 'min-width' property is used.

Each element also has .right or .left style class depending on text direction.

Creating patterns is relatively expensive, so do it once a gesture
starts and clear them once it ends. Style changes are currently not
handled, since it's unlikely anything would change while the gesture
is done. Dimming layer is created with the same width as webview and is
clipped when drawing.

Translate existing dimming and shadow into css as is, add subtle border and
outline. Since it's controlled via css, downstreams can override the style.

Set WebKitWebView css name to 'webkitwebview', and only apply styles for
elements directly preceded by it, otherwise the selectors are too generic.

* PlatformGTK.cmake: Add gtk.css to gresource bundle.
* Resources/gtk/gtk-theme.css: Added.
(webkitwebview > dimming):
(webkitwebview > shadow):
(webkitwebview > shadow.right):
(webkitwebview > shadow.left):
(webkitwebview > border):
(webkitwebview > outline):
* UIProcess/API/gtk/WebKitWebViewBase.cpp:
(webkit_web_view_base_class_init): Set css name to 'webkitwebview'.
* UIProcess/ViewGestureController.h:
* UIProcess/gtk/ViewGestureControllerGtk.cpp:
(WebKit::ViewGestureController::createStyleContext):
(WebKit::createElementPattern):
(WebKit::elementWidth):
(WebKit::ViewGestureController::beginSwipeGesture): Create helper patterns.
(WebKit::ViewGestureController::draw): Rework drawing to use the patterns.
(WebKit::ViewGestureController::removeSwipeSnapshot): Clear the patterns.

Modified Paths

Added Paths

Diff

Modified: trunk/Source/WebKit/ChangeLog (250647 => 250648)


--- trunk/Source/WebKit/ChangeLog	2019-10-03 08:39:53 UTC (rev 250647)
+++ trunk/Source/WebKit/ChangeLog	2019-10-03 08:54:51 UTC (rev 250648)
@@ -1,3 +1,52 @@
+2019-10-03  Alexander Mikhaylenko  <[email protected]>
+
+        [GTK] Don't hardcode swipe navigation gesture style
+        https://bugs.webkit.org/show_bug.cgi?id=202447
+
+        Reviewed by Carlos Garcia Campos.
+
+        Use GTK foreign drawing to draw swipe gesture dimming and shadow instead of
+        hardcoded opacity and gradient values.
+
+        Have separate elements for dimming and shadow. Additionally, add border
+        and outline. Dimming layer opacity always matches progress, shadow opacity
+        is 1 except when the page is almost completely retracted. Border and outline
+        opacity is always 1. Outline is drawn above the top page. For determining
+        shadow, border and outline width, 'min-width' property is used.
+
+        Each element also has .right or .left style class depending on text direction.
+
+        Creating patterns is relatively expensive, so do it once a gesture
+        starts and clear them once it ends. Style changes are currently not
+        handled, since it's unlikely anything would change while the gesture
+        is done. Dimming layer is created with the same width as webview and is
+        clipped when drawing.
+
+        Translate existing dimming and shadow into css as is, add subtle border and
+        outline. Since it's controlled via css, downstreams can override the style.
+
+        Set WebKitWebView css name to 'webkitwebview', and only apply styles for
+        elements directly preceded by it, otherwise the selectors are too generic.
+
+        * PlatformGTK.cmake: Add gtk.css to gresource bundle.
+        * Resources/gtk/gtk-theme.css: Added.
+        (webkitwebview > dimming):
+        (webkitwebview > shadow):
+        (webkitwebview > shadow.right):
+        (webkitwebview > shadow.left):
+        (webkitwebview > border):
+        (webkitwebview > outline):
+        * UIProcess/API/gtk/WebKitWebViewBase.cpp:
+        (webkit_web_view_base_class_init): Set css name to 'webkitwebview'.
+        * UIProcess/ViewGestureController.h:
+        * UIProcess/gtk/ViewGestureControllerGtk.cpp:
+        (WebKit::ViewGestureController::createStyleContext):
+        (WebKit::createElementPattern):
+        (WebKit::elementWidth):
+        (WebKit::ViewGestureController::beginSwipeGesture): Create helper patterns.
+        (WebKit::ViewGestureController::draw): Rework drawing to use the patterns.
+        (WebKit::ViewGestureController::removeSwipeSnapshot): Clear the patterns.
+
 2019-10-03  Carlos Garcia Campos  <[email protected]>
 
         [GTK] Make PSON optional

Modified: trunk/Source/WebKit/PlatformGTK.cmake (250647 => 250648)


--- trunk/Source/WebKit/PlatformGTK.cmake	2019-10-03 08:39:53 UTC (rev 250647)
+++ trunk/Source/WebKit/PlatformGTK.cmake	2019-10-03 08:54:51 UTC (rev 250648)
@@ -511,6 +511,7 @@
 WEBKIT_BUILD_INSPECTOR_GRESOURCES(${DERIVED_SOURCES_WEBKIT2GTK_DIR})
 
 set(WebKitResources
+    "        <file alias=\"css/gtk-theme.css\">gtk-theme.css</file>\n"
     "        <file alias=\"images/missingImage\">missingImage.png</file>\n"
     "        <file alias=\"images/missingImage@2x\">[email protected]</file>\n"
     "        <file alias=\"images/panIcon\">panIcon.png</file>\n"
@@ -536,7 +537,7 @@
 add_custom_command(
     OUTPUT ${DERIVED_SOURCES_WEBKIT2GTK_DIR}/WebKitResourcesGResourceBundle.c
     DEPENDS ${DERIVED_SOURCES_WEBKIT2GTK_DIR}/WebKitResourcesGResourceBundle.xml
-    COMMAND glib-compile-resources --generate --sourcedir=${CMAKE_SOURCE_DIR}/Source/WebCore/Resources --sourcedir=${CMAKE_SOURCE_DIR}/Source/WebCore/platform/audio/resources --target=${DERIVED_SOURCES_WEBKIT2GTK_DIR}/WebKitResourcesGResourceBundle.c ${DERIVED_SOURCES_WEBKIT2GTK_DIR}/WebKitResourcesGResourceBundle.xml
+    COMMAND glib-compile-resources --generate --sourcedir=${CMAKE_SOURCE_DIR}/Source/WebCore/Resources --sourcedir=${CMAKE_SOURCE_DIR}/Source/WebCore/platform/audio/resources --sourcedir=${CMAKE_SOURCE_DIR}/Source/WebKit/Resources/gtk --target=${DERIVED_SOURCES_WEBKIT2GTK_DIR}/WebKitResourcesGResourceBundle.c ${DERIVED_SOURCES_WEBKIT2GTK_DIR}/WebKitResourcesGResourceBundle.xml
     VERBATIM
 )
 

Added: trunk/Source/WebKit/Resources/gtk/gtk-theme.css (0 => 250648)


--- trunk/Source/WebKit/Resources/gtk/gtk-theme.css	                        (rev 0)
+++ trunk/Source/WebKit/Resources/gtk/gtk-theme.css	2019-10-03 08:54:51 UTC (rev 250648)
@@ -0,0 +1,41 @@
+webkitwebview > dimming {
+  background: alpha(#000, 0.12);
+}
+
+webkitwebview > shadow {
+  min-width: 56px;
+}
+
+webkitwebview > shadow.right {
+  background-image:
+    linear-gradient(to left,
+                    alpha(#000, 0.05),
+                    alpha(#000, 0.01) 40px,
+                    alpha(#000, 0) 56px),
+    linear-gradient(to left,
+                    alpha(#000, 0.03),
+                    alpha(#000, 0.01) 7px,
+                    alpha(#000, 0) 24px);
+}
+
+webkitwebview > shadow.left {
+  background-image:
+    linear-gradient(to right,
+                    alpha(#000, 0.05),
+                    alpha(#000, 0.01) 40px,
+                    alpha(#000, 0) 56px),
+    linear-gradient(to right,
+                    alpha(#000, 0.03),
+                    alpha(#000, 0.01) 7px,
+                    alpha(#000, 0) 24px);
+}
+
+webkitwebview > border {
+  min-width: 1px;
+  background: alpha(#000, 0.05);
+}
+
+webkitwebview > outline {
+  min-width: 1px;
+  background: alpha(#fff, 0.05);
+}

Modified: trunk/Source/WebKit/UIProcess/API/gtk/WebKitWebViewBase.cpp (250647 => 250648)


--- trunk/Source/WebKit/UIProcess/API/gtk/WebKitWebViewBase.cpp	2019-10-03 08:39:53 UTC (rev 250647)
+++ trunk/Source/WebKit/UIProcess/API/gtk/WebKitWebViewBase.cpp	2019-10-03 08:54:51 UTC (rev 250648)
@@ -1444,6 +1444,8 @@
     // Usually starting a context triggers InitializeWebKit2, but in case
     // we create a view without asking before for a default_context we get a crash.
     WebKit::InitializeWebKit2();
+
+    gtk_widget_class_set_css_name(widgetClass, "webkitwebview");
 }
 
 WebKitWebViewBase* webkitWebViewBaseCreate(const API::PageConfiguration& configuration)

Modified: trunk/Source/WebKit/UIProcess/ViewGestureController.h (250647 => 250648)


--- trunk/Source/WebKit/UIProcess/ViewGestureController.h	2019-10-03 08:39:53 UTC (rev 250647)
+++ trunk/Source/WebKit/UIProcess/ViewGestureController.h	2019-10-03 08:54:51 UTC (rev 250648)
@@ -42,6 +42,7 @@
 #if PLATFORM(GTK)
 #include <WebCore/CairoUtilities.h>
 #include <gtk/gtk.h>
+#include <wtf/glib/GRefPtr.h>
 #endif
 
 #if PLATFORM(COCOA)
@@ -305,6 +306,10 @@
     };
 #endif
 
+#if PLATFORM(GTK)
+    GRefPtr<GtkStyleContext> createStyleContext(const char*);
+#endif
+
     WebPageProxy& m_webPageProxy;
     ViewGestureType m_activeGestureType { ViewGestureType::None };
 
@@ -410,6 +415,14 @@
     SwipeProgressTracker m_swipeProgressTracker;
 
     RefPtr<cairo_pattern_t> m_currentSwipeSnapshotPattern;
+    RefPtr<cairo_pattern_t> m_swipeDimmingPattern;
+    RefPtr<cairo_pattern_t> m_swipeShadowPattern;
+    RefPtr<cairo_pattern_t> m_swipeBorderPattern;
+    RefPtr<cairo_pattern_t> m_swipeOutlinePattern;
+    int m_swipeShadowSize;
+    int m_swipeBorderSize;
+    int m_swipeOutlineSize;
+    GRefPtr<GtkCssProvider> m_cssProvider;
 
     bool m_isSimulatedSwipe { false };
 #endif

Modified: trunk/Source/WebKit/UIProcess/gtk/ViewGestureControllerGtk.cpp (250647 => 250648)


--- trunk/Source/WebKit/UIProcess/gtk/ViewGestureControllerGtk.cpp	2019-10-03 08:39:53 UTC (rev 250647)
+++ trunk/Source/WebKit/UIProcess/gtk/ViewGestureControllerGtk.cpp	2019-10-03 08:54:51 UTC (rev 250648)
@@ -28,6 +28,7 @@
 
 #include "DrawingAreaProxy.h"
 #include "WebBackForwardList.h"
+#include <WebCore/GRefPtrGtk.h>
 
 namespace WebKit {
 using namespace WebCore;
@@ -46,12 +47,6 @@
 static const double swipeCancelArea = 0.5;
 static const double swipeCancelVelocityThreshold = 0.001;
 
-static const double swipeOverlayShadowOpacity = 0.06;
-static const double swipeOverlayDimmingOpacity = 0.12;
-static const double swipeOverlayShadowWidth = 81;
-static const double swipeOverlayShadowGradientOffsets[] = { 0, 0.03125, 0.0625, 0.0938, 0.125, 0.1875, 0.25, 0.375, 0.4375, 0.5, 0.5625, 0.625, 0.6875, 0.75, 0.875, 1. };
-static const double swipeOverlayShadowGradientAlpha[] = { 1, 0.99, 0.98, 0.95, 0.92, 0.82, 0.71, 0.46, 0.35, 0.25, 0.17, 0.11, 0.07, 0.04, 0.01, 0. };
-
 static bool isEventStop(GdkEventScroll* event)
 {
     return gdk_event_is_scroll_stop_event(reinterpret_cast<GdkEvent*>(event));
@@ -283,6 +278,45 @@
     m_viewGestureController.endSwipeGesture(m_targetItem.get(), m_cancelled);
 }
 
+GRefPtr<GtkStyleContext> ViewGestureController::createStyleContext(const char* name)
+{
+    bool isRTL = m_webPageProxy.userInterfaceLayoutDirection() == WebCore::UserInterfaceLayoutDirection::RTL;
+    GtkWidget* widget = m_webPageProxy.viewWidget();
+
+    GRefPtr<GtkWidgetPath> path = adoptGRef(gtk_widget_path_copy(gtk_widget_get_path(widget)));
+
+    int position = gtk_widget_path_append_type(path.get(), GTK_TYPE_WIDGET);
+    gtk_widget_path_iter_set_object_name(path.get(), position, name);
+    gtk_widget_path_iter_add_class(path.get(), position, isRTL ? "left" : "right");
+
+    GtkStyleContext* context = gtk_style_context_new();
+    gtk_style_context_set_path(context, path.get());
+    gtk_style_context_set_parent(context, gtk_widget_get_style_context(widget));
+
+    gtk_style_context_add_provider(context, GTK_STYLE_PROVIDER(m_cssProvider.get()), GTK_STYLE_PROVIDER_PRIORITY_FALLBACK);
+
+    return adoptGRef(context);
+}
+
+static RefPtr<cairo_pattern_t> createElementPattern(GtkStyleContext* context, int width, int height)
+{
+    RefPtr<cairo_surface_t> surface = adoptRef(cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height));
+    RefPtr<cairo_t> cr = adoptRef(cairo_create(surface.get()));
+
+    gtk_render_background(context, cr.get(), 0, 0, width, height);
+    gtk_render_frame(context, cr.get(), 0, 0, width, height);
+
+    return adoptRef(cairo_pattern_create_for_surface(surface.get()));
+}
+
+static int elementWidth(GtkStyleContext* context)
+{
+    int width;
+    gtk_style_context_get(context, gtk_style_context_get_state(context), "min-width", &width, nullptr);
+
+    return width;
+}
+
 void ViewGestureController::beginSwipeGesture(WebBackForwardListItem* targetItem, SwipeDirection direction)
 {
     ASSERT(targetItem);
@@ -318,6 +352,31 @@
 
     if (!m_currentSwipeSnapshotPattern)
         m_currentSwipeSnapshotPattern = adoptRef(cairo_pattern_create_rgb(1, 1, 1));
+
+    auto size = m_webPageProxy.drawingArea()->size();
+
+    if (!m_cssProvider) {
+        m_cssProvider = adoptGRef(gtk_css_provider_new());
+        gtk_css_provider_load_from_resource(m_cssProvider.get(), "/org/webkitgtk/resources/css/gtk-theme.css");
+    }
+
+    GRefPtr<GtkStyleContext> context = createStyleContext("dimming");
+    m_swipeDimmingPattern = createElementPattern(context.get(), size.width(), size.height());
+
+    context = createStyleContext("shadow");
+    m_swipeShadowSize = elementWidth(context.get());
+    if (m_swipeShadowSize)
+        m_swipeShadowPattern = createElementPattern(context.get(), m_swipeShadowSize, size.height());
+
+    context = createStyleContext("border");
+    m_swipeBorderSize = elementWidth(context.get());
+    if (m_swipeBorderSize)
+        m_swipeBorderPattern = createElementPattern(context.get(), m_swipeBorderSize, size.height());
+
+    context = createStyleContext("outline");
+    m_swipeOutlineSize = elementWidth(context.get());
+    if (m_swipeOutlineSize)
+        m_swipeOutlinePattern = createElementPattern(context.get(), m_swipeOutlineSize, size.height());
 }
 
 void ViewGestureController::handleSwipeGesture(WebBackForwardListItem*, double, SwipeDirection)
@@ -342,8 +401,9 @@
     bool isRTL = m_webPageProxy.userInterfaceLayoutDirection() == WebCore::UserInterfaceLayoutDirection::RTL;
     float progress = m_swipeProgressTracker.progress();
 
-    double width = m_webPageProxy.drawingArea()->size().width();
-    double height = m_webPageProxy.drawingArea()->size().height();
+    auto size = m_webPageProxy.drawingArea()->size();
+    int width = size.width();
+    int height = size.height();
 
     double swipingLayerOffset = (swipingLeft ? 0 : width) + floor(width * progress);
 
@@ -352,19 +412,11 @@
         dimmingProgress = 1 - dimmingProgress;
 
     double remainingSwipeDistance = dimmingProgress * width;
-    double shadowFadeDistance = swipeOverlayShadowWidth;
 
-    double shadowOpacity = swipeOverlayShadowOpacity;
-    if (remainingSwipeDistance < shadowFadeDistance)
-        shadowOpacity = (remainingSwipeDistance / shadowFadeDistance) * swipeOverlayShadowOpacity;
+    double shadowOpacity = 1;
+    if (remainingSwipeDistance < m_swipeShadowSize)
+        shadowOpacity = remainingSwipeDistance / m_swipeShadowSize;
 
-    RefPtr<cairo_pattern_t> shadowPattern = adoptRef(cairo_pattern_create_linear(0, 0, swipeOverlayShadowWidth, 0));
-    for (int i = 0; i < 16; i++) {
-        double offset = swipeOverlayShadowGradientOffsets[i];
-        double alpha = swipeOverlayShadowGradientAlpha[i] * shadowOpacity;
-        cairo_pattern_add_color_stop_rgba(shadowPattern.get(), offset, 0, 0, 0, alpha);
-    }
-
     cairo_save(cr);
 
     if (isRTL)
@@ -374,19 +426,38 @@
     cairo_set_source(cr, swipingBack ? m_currentSwipeSnapshotPattern.get() : pageGroup);
     cairo_fill_preserve(cr);
 
-    cairo_set_source_rgba(cr, 0, 0, 0, dimmingProgress * swipeOverlayDimmingOpacity);
-    cairo_fill(cr);
+    cairo_save(cr);
+    cairo_clip(cr);
+    cairo_set_source(cr, m_swipeDimmingPattern.get());
+    cairo_paint_with_alpha(cr, dimmingProgress);
+    cairo_restore(cr);
 
     cairo_translate(cr, swipingLayerOffset, 0);
 
     if (progress) {
-        cairo_save(cr);
-        if (!isRTL)
-            cairo_scale(cr, -1, 1);
-        cairo_rectangle(cr, 0, 0, swipeOverlayShadowWidth, height);
-        cairo_set_source(cr, shadowPattern.get());
-        cairo_fill(cr);
-        cairo_restore(cr);
+        if (m_swipeShadowPattern) {
+            cairo_save(cr);
+            if (!isRTL)
+                cairo_translate(cr, -m_swipeShadowSize, 0);
+
+            cairo_rectangle(cr, 0, 0, m_swipeShadowSize, height);
+            cairo_clip(cr);
+            cairo_set_source(cr, m_swipeShadowPattern.get());
+            cairo_paint_with_alpha(cr, shadowOpacity);
+            cairo_restore(cr);
+        }
+
+        if (m_swipeBorderPattern) {
+            cairo_save(cr);
+            if (!isRTL)
+                cairo_translate(cr, -m_swipeBorderSize, 0);
+
+            cairo_rectangle(cr, 0, 0, m_swipeBorderSize, height);
+            cairo_set_source(cr, m_swipeBorderPattern.get());
+            cairo_fill(cr);
+
+            cairo_restore(cr);
+        }
     }
 
     if (isRTL) {
@@ -397,6 +468,19 @@
     cairo_set_source(cr, swipingBack ? pageGroup : m_currentSwipeSnapshotPattern.get());
     cairo_fill(cr);
 
+    if (progress && m_swipeOutlinePattern) {
+        cairo_save(cr);
+
+        if (isRTL)
+            cairo_translate(cr, width - m_swipeOutlineSize, 0);
+
+        cairo_rectangle(cr, 0, 0, m_swipeOutlineSize, height);
+        cairo_set_source(cr, m_swipeOutlinePattern.get());
+        cairo_fill(cr);
+
+        cairo_restore(cr);
+    }
+
     cairo_restore(cr);
 }
 
@@ -410,6 +494,10 @@
         return;
 
     m_currentSwipeSnapshotPattern = nullptr;
+    m_swipeDimmingPattern = nullptr;
+    m_swipeShadowPattern = nullptr;
+    m_swipeBorderPattern = nullptr;
+    m_swipeOutlinePattern = nullptr;
 
     m_currentSwipeSnapshot = nullptr;
 
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to