[GTK] Automatically adjust font size when gtk-xft-dpi changes
authorcommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 15 Nov 2017 12:06:43 +0000 (12:06 +0000)
committercommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 15 Nov 2017 12:06:43 +0000 (12:06 +0000)
https://bugs.webkit.org/show_bug.cgi?id=142673

Some follow-up fixes for the previous patch.

Patch by Gabriel Ivascu <givascu@igalia.com> on 2017-11-15
Reviewed by Carlos Garcia Campos.

Source/WebCore:

* platform/PlatformScreen.h:
* platform/gtk/PlatformScreenGtk.cpp:
(WebCore::screenDPIObserverHandlersMap):
(WebCore::gtkXftDPIChangedCallback):
(WebCore::setScreenDPIObserverHandler):
* platform/wpe/PlatformScreenWPE.cpp:
(WebCore::setScreenDPIObserverHandler):

Source/WebKit:

* UIProcess/API/glib/WebKitSettings.cpp:
(webKitSettingsDispose):
(webKitSettingsConstructed):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@224872 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebCore/ChangeLog
Source/WebCore/platform/PlatformScreen.h
Source/WebCore/platform/gtk/PlatformScreenGtk.cpp
Source/WebCore/platform/wpe/PlatformScreenWPE.cpp
Source/WebKit/ChangeLog
Source/WebKit/UIProcess/API/glib/WebKitSettings.cpp

index 9a9d2d7..ec67f52 100644 (file)
@@ -1,3 +1,20 @@
+2017-11-15  Gabriel Ivascu  <givascu@igalia.com>
+
+        [GTK] Automatically adjust font size when gtk-xft-dpi changes
+        https://bugs.webkit.org/show_bug.cgi?id=142673
+
+        Some follow-up fixes for the previous patch.
+
+        Reviewed by Carlos Garcia Campos.
+
+        * platform/PlatformScreen.h:
+        * platform/gtk/PlatformScreenGtk.cpp:
+        (WebCore::screenDPIObserverHandlersMap):
+        (WebCore::gtkXftDPIChangedCallback):
+        (WebCore::setScreenDPIObserverHandler):
+        * platform/wpe/PlatformScreenWPE.cpp:
+        (WebCore::setScreenDPIObserverHandler):
+
 2017-11-14  Nan Wang  <n_wang@apple.com>
 
         AX: AOM: Implement AccessibleNode class and support label and role attributes
index 24237da..f90915e 100644 (file)
@@ -64,7 +64,7 @@ bool screenIsMonochrome(Widget*);
 bool screenHasInvertedColors();
 #if USE(GLIB)
 double screenDPI();
-void setScreenDPIObserverHandler(Function<void()>&&);
+void setScreenDPIObserverHandler(Function<void()>&&, void*);
 #endif
 
 FloatRect screenRect(Widget*);
index 3f2836c..395dfe5 100644 (file)
@@ -40,7 +40,8 @@
 
 #include <cmath>
 #include <gtk/gtk.h>
-#include <wtf/Optional.h>
+#include <wtf/HashMap.h>
+#include <wtf/NeverDestroyed.h>
 
 namespace WebCore {
 
@@ -113,30 +114,38 @@ double screenDPI()
     return cachedDpi;
 }
 
-static std::optional<Function<void()>> screenDPIObserverHandler;
+static WTF::HashMap<void*, Function<void()>>& screenDPIObserverHandlersMap()
+{
+    static WTF::NeverDestroyed<WTF::HashMap<void*, Function<void()>>> handlersMap;
+    return handlersMap;
+}
 
 static void gtkXftDPIChangedCallback()
 {
-    if (screenDPIObserverHandler)
-        (*screenDPIObserverHandler)();
+    for (const auto& keyValuePair : screenDPIObserverHandlersMap())
+        keyValuePair.value();
 }
 
-void setScreenDPIObserverHandler(Function<void()>&& handler)
+void setScreenDPIObserverHandler(Function<void()>&& handler, void* context)
 {
     static GtkSettings* gtkSettings = gtk_settings_get_default();
     static unsigned long gtkXftDpiChangedHandlerID = 0;
 
-    if (!handler) {
-        if (gtkSettings && gtkXftDpiChangedHandlerID) {
-            g_signal_handler_disconnect(gtkSettings, gtkXftDpiChangedHandlerID);
-            gtkXftDpiChangedHandlerID = 0;
-        }
+    if (!gtkSettings)
         return;
-    }
 
-    screenDPIObserverHandler = WTFMove(handler);
-    if (gtkSettings && !gtkXftDpiChangedHandlerID)
-        gtkXftDpiChangedHandlerID = g_signal_connect(gtkSettings, "notify::gtk-xft-dpi", G_CALLBACK(gtkXftDPIChangedCallback), nullptr);
+    if (handler)
+        screenDPIObserverHandlersMap().set(context, WTFMove(handler));
+    else
+        screenDPIObserverHandlersMap().remove(context);
+
+    if (!screenDPIObserverHandlersMap().isEmpty()) {
+        if (!gtkXftDpiChangedHandlerID)
+            gtkXftDpiChangedHandlerID = g_signal_connect(gtkSettings, "notify::gtk-xft-dpi", G_CALLBACK(gtkXftDPIChangedCallback), nullptr);
+    } else if (gtkXftDpiChangedHandlerID) {
+        g_signal_handler_disconnect(gtkSettings, gtkXftDpiChangedHandlerID);
+        gtkXftDpiChangedHandlerID = 0;
+    }
 }
 
 static GdkScreen* getScreen(GtkWidget* widget)
index b4b59dd..a2d079b 100644 (file)
@@ -60,7 +60,7 @@ double screenDPI()
     return 96;
 }
 
-void setScreenDPIObserverHandler(Function<void()>&&)
+void setScreenDPIObserverHandler(Function<void()>&&, void*)
 {
     notImplemented();
 }
index 7dea6e8..6e86c90 100644 (file)
@@ -1,3 +1,16 @@
+2017-11-15  Gabriel Ivascu  <givascu@igalia.com>
+
+        [GTK] Automatically adjust font size when gtk-xft-dpi changes
+        https://bugs.webkit.org/show_bug.cgi?id=142673
+
+        Some follow-up fixes for the previous patch.
+
+        Reviewed by Carlos Garcia Campos.
+
+        * UIProcess/API/glib/WebKitSettings.cpp:
+        (webKitSettingsDispose):
+        (webKitSettingsConstructed):
+
 2017-11-14  Nan Wang  <n_wang@apple.com>
 
         AX: AOM: Implement AccessibleNode class and support label and role attributes
index ca2934d..5f40ea3 100644 (file)
@@ -161,7 +161,7 @@ enum {
 
 static void webKitSettingsDispose(GObject* object)
 {
-    WebCore::setScreenDPIObserverHandler(nullptr);
+    WebCore::setScreenDPIObserverHandler(nullptr, object);
     G_OBJECT_CLASS(webkit_settings_parent_class)->dispose(object);
 }
 
@@ -180,17 +180,15 @@ static void webKitSettingsConstructed(GObject* object)
             return;
 
         auto scalingFactor = newScreenDpi / settings->priv->screenDpi;
-        auto prevFontSize = settings->priv->preferences->defaultFontSize();
-        auto prevMonospaceFontSize = settings->priv->preferences->defaultFixedFontSize();
-
-        settings->priv->preferences->setDefaultFontSize(std::round(prevFontSize * scalingFactor));
-        g_object_notify(G_OBJECT(settings), "default-font-size");
-
-        settings->priv->preferences->setDefaultFixedFontSize(std::round(prevMonospaceFontSize * scalingFactor));
-        g_object_notify(G_OBJECT(settings), "default-monospace-font-size");
-
+        auto fontSize = settings->priv->preferences->defaultFontSize();
+        auto monospaceFontSize = settings->priv->preferences->defaultFixedFontSize();
         settings->priv->screenDpi = newScreenDpi;
-    });
+
+        g_object_freeze_notify(G_OBJECT(settings));
+        webkit_settings_set_default_font_size(settings, std::round(fontSize * scalingFactor));
+        webkit_settings_set_default_monospace_font_size(settings, std::round(monospaceFontSize * scalingFactor));
+        g_object_thaw_notify(G_OBJECT(settings));
+    }, object);
 }
 
 static void webKitSettingsSetProperty(GObject* object, guint propId, const GValue* value, GParamSpec* paramSpec)