[gnome-builder/wip/gtk4-port: 385/736] libide/sourceview: move style-scheme helpers




commit e052ab19e63f8243414e06a6193241d9974e023d
Author: Christian Hergert <chergert redhat com>
Date:   Mon Apr 4 16:00:40 2022 -0700

    libide/sourceview: move style-scheme helpers
    
    These can be in libide-sourceview for now, since they don't have to rely
    on anything specific to IdeApplication and friends.

 src/libide/gui/ide-application-settings.c       |   4 +-
 src/libide/gui/ide-recoloring.c                 |  84 +-----------
 src/libide/gui/meson.build                      |   1 +
 src/libide/meson.build                          |   2 +-
 src/libide/sourceview/ide-source-style-scheme.c | 170 ++++++++++++++++++++++++
 src/libide/sourceview/ide-source-style-scheme.h |  40 ++++++
 src/libide/sourceview/ide-source-view.h         |   4 +
 src/libide/sourceview/libide-sourceview.h       |   1 +
 src/libide/sourceview/meson.build               |   2 +
 9 files changed, 225 insertions(+), 83 deletions(-)
---
diff --git a/src/libide/gui/ide-application-settings.c b/src/libide/gui/ide-application-settings.c
index 0ba7a54f6..9bd5437c9 100644
--- a/src/libide/gui/ide-application-settings.c
+++ b/src/libide/gui/ide-application-settings.c
@@ -22,6 +22,8 @@
 
 #include "config.h"
 
+#include <libide-sourceview.h>
+
 #include "ide-application-private.h"
 #include "ide-recoloring-private.h"
 
@@ -200,7 +202,7 @@ ide_application_get_style_scheme (IdeApplication *self)
     variant = "light";
 
   style_scheme = gtk_source_style_scheme_manager_get_scheme (style_scheme_manager, style_scheme_id);
-  style_scheme = _ide_source_style_scheme_get_variant (style_scheme, variant);
+  style_scheme = ide_source_style_scheme_get_variant (style_scheme, variant);
 
   return gtk_source_style_scheme_get_id (style_scheme);
 }
diff --git a/src/libide/gui/ide-recoloring.c b/src/libide/gui/ide-recoloring.c
index ff2bbd485..bef1dc2f9 100644
--- a/src/libide/gui/ide-recoloring.c
+++ b/src/libide/gui/ide-recoloring.c
@@ -22,6 +22,8 @@
 
 #include <math.h>
 
+#include <libide-sourceview.h>
+
 #include "ide-recoloring-private.h"
 
 #define SHARED_CSS \
@@ -183,36 +185,6 @@ premix_colors (GdkRGBA       *dest,
 }
 #endif
 
-gboolean
-_ide_source_style_scheme_is_dark (GtkSourceStyleScheme *scheme)
-{
-  const char *id = gtk_source_style_scheme_get_id (scheme);
-  const char *variant = gtk_source_style_scheme_get_metadata (scheme, "variant");
-  GdkRGBA text_bg;
-
-  if (g_strcmp0 (variant, "light") == 0)
-    return FALSE;
-  else if (g_strcmp0 (variant, "dark") == 0)
-    return TRUE;
-  else if (strstr (id, "-dark") != NULL)
-    return TRUE;
-
-  if (get_background (scheme, "text", &text_bg))
-    {
-      /* http://alienryderflex.com/hsp.html */
-      double r = text_bg.red * 255.0;
-      double g = text_bg.green * 255.0;
-      double b = text_bg.blue * 255.0;
-      double hsp = sqrt (0.299 * (r * r) +
-                         0.587 * (g * g) +
-                         0.114 * (b * b));
-
-      return hsp <= 127.5;
-    }
-
-  return FALSE;
-}
-
 char *
 _ide_recoloring_generate_css (GtkSourceStyleScheme *style_scheme)
 {
@@ -238,7 +210,7 @@ _ide_recoloring_generate_css (GtkSourceStyleScheme *style_scheme)
     return NULL;
 
   name = gtk_source_style_scheme_get_name (style_scheme);
-  is_dark = _ide_source_style_scheme_is_dark (style_scheme);
+  is_dark = ide_source_style_scheme_is_dark (style_scheme);
   alt = is_dark ? &white : &black;
 
   str = g_string_new (SHARED_CSS);
@@ -316,53 +288,3 @@ _ide_recoloring_generate_css (GtkSourceStyleScheme *style_scheme)
 
   return g_string_free (str, FALSE);
 }
-
-GtkSourceStyleScheme *
-_ide_source_style_scheme_get_variant (GtkSourceStyleScheme *scheme,
-                                      const char           *variant)
-{
-  GtkSourceStyleSchemeManager *style_scheme_manager;
-  GtkSourceStyleScheme *ret;
-  g_autoptr(GString) str = NULL;
-  g_autofree char *key = NULL;
-  const char *mapping;
-
-  g_return_val_if_fail (GTK_SOURCE_IS_STYLE_SCHEME (scheme), NULL);
-  g_return_val_if_fail (g_strcmp0 (variant, "light") == 0 ||
-                        g_strcmp0 (variant, "dark") == 0, NULL);
-
-  style_scheme_manager = gtk_source_style_scheme_manager_get_default ();
-
-  /* If the scheme provides "light-variant" or "dark-variant" metadata,
-   * we will prefer those if the variant is available.
-   */
-  key = g_strdup_printf ("%s-variant", variant);
-  if ((mapping = gtk_source_style_scheme_get_metadata (scheme, key)))
-    {
-      if ((ret = gtk_source_style_scheme_manager_get_scheme (style_scheme_manager, mapping)))
-        return ret;
-    }
-
-  /* Try to find a match by replacing -light/-dark with @variant */
-  str = g_string_new (gtk_source_style_scheme_get_id (scheme));
-
-  if (g_str_has_suffix (str->str, "-light"))
-    g_string_truncate (str, str->len - strlen ("-light"));
-  else if (g_str_has_suffix (str->str, "-dark"))
-    g_string_truncate (str, str->len - strlen ("-dark"));
-
-  g_string_append_printf (str, "-%s", variant);
-
-  /* Look for "Foo-variant" directly */
-  if ((ret = gtk_source_style_scheme_manager_get_scheme (style_scheme_manager, str->str)))
-    return ret;
-
-  /* Look for "Foo" */
-  g_string_truncate (str, str->len - strlen (variant) - 1);
-  if ((ret = gtk_source_style_scheme_manager_get_scheme (style_scheme_manager, str->str)))
-    return ret;
-
-  /* Fallback to what we were provided */
-  return ret;
-}
-
diff --git a/src/libide/gui/meson.build b/src/libide/gui/meson.build
index 38e95e9e8..77689b2f3 100644
--- a/src/libide/gui/meson.build
+++ b/src/libide/gui/meson.build
@@ -162,6 +162,7 @@ libide_gui_deps = [
   libide_plugins_dep,
   libide_projects_dep,
   libide_search_dep,
+  libide_sourceview_dep,
 ]
 
 if get_option('webkit').enabled()
diff --git a/src/libide/meson.build b/src/libide/meson.build
index 8a4679a2a..0b5d296ea 100644
--- a/src/libide/meson.build
+++ b/src/libide/meson.build
@@ -15,9 +15,9 @@ subdir('search')
 subdir('projects')
 subdir('foundry')
 subdir('debugger')
+subdir('sourceview')
 subdir('gui')
 subdir('terminal')
-subdir('sourceview')
 subdir('editor')
 subdir('lsp')
 subdir('greeter')
diff --git a/src/libide/sourceview/ide-source-style-scheme.c b/src/libide/sourceview/ide-source-style-scheme.c
new file mode 100644
index 000000000..431e3860a
--- /dev/null
+++ b/src/libide/sourceview/ide-source-style-scheme.c
@@ -0,0 +1,170 @@
+/* ide-source-style-scheme.c
+ *
+ * Copyright 2022 Christian Hergert <chergert redhat com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#define G_LOG_DOMAIN "ide-source-style-scheme"
+
+#include "config.h"
+
+#include <math.h>
+
+#include "ide-source-style-scheme.h"
+
+enum {
+  FOREGROUND,
+  BACKGROUND,
+};
+
+static gboolean
+get_color (GtkSourceStyleScheme *scheme,
+           const char           *style_name,
+           GdkRGBA              *color,
+           int                   kind)
+{
+  GtkSourceStyle *style;
+  g_autofree char *fg = NULL;
+  g_autofree char *bg = NULL;
+  gboolean fg_set = FALSE;
+  gboolean bg_set = FALSE;
+
+  g_assert (GTK_SOURCE_IS_STYLE_SCHEME (scheme));
+  g_assert (style_name != NULL);
+
+  if (!(style = gtk_source_style_scheme_get_style (scheme, style_name)))
+    return FALSE;
+
+  g_object_get (style,
+                "foreground", &fg,
+                "foreground-set", &fg_set,
+                "background", &bg,
+                "background-set", &bg_set,
+                NULL);
+
+  if (kind == FOREGROUND && fg && fg_set)
+    gdk_rgba_parse (color, fg);
+  else if (kind == BACKGROUND && bg && bg_set)
+    gdk_rgba_parse (color, bg);
+  else
+    return FALSE;
+
+  return color->alpha >= .1;
+}
+
+static inline gboolean
+get_background (GtkSourceStyleScheme *scheme,
+                const char           *style_name,
+                GdkRGBA              *bg)
+{
+  return get_color (scheme, style_name, bg, BACKGROUND);
+}
+
+gboolean
+ide_source_style_scheme_is_dark (GtkSourceStyleScheme *scheme)
+{
+  const char *id;
+  const char *variant;
+  GdkRGBA text_bg;
+
+  g_return_val_if_fail (GTK_SOURCE_IS_STYLE_SCHEME (scheme), FALSE);
+
+  id = gtk_source_style_scheme_get_id (scheme);
+  variant = gtk_source_style_scheme_get_metadata (scheme, "variant");
+
+  if (g_strcmp0 (variant, "light") == 0)
+    return FALSE;
+  else if (g_strcmp0 (variant, "dark") == 0)
+    return TRUE;
+  else if (strstr (id, "-dark") != NULL)
+    return TRUE;
+
+  if (get_background (scheme, "text", &text_bg))
+    {
+      /* http://alienryderflex.com/hsp.html */
+      double r = text_bg.red * 255.0;
+      double g = text_bg.green * 255.0;
+      double b = text_bg.blue * 255.0;
+      double hsp = sqrt (0.299 * (r * r) +
+                         0.587 * (g * g) +
+                         0.114 * (b * b));
+
+      return hsp <= 127.5;
+    }
+
+  return FALSE;
+}
+
+/**
+ * ide_source_style_scheme_get_variant:
+ * @scheme: a #GtkSourceStyleScheme
+ * @variant: the alternative variant
+ *
+ * Gets an alternate for a style scheme if one exists. Otherwise
+ * @scheme is returned.
+ *
+ * Returns: (transfer none) (not nullable): a #GtkSourceStyleScheme
+ */
+GtkSourceStyleScheme *
+ide_source_style_scheme_get_variant (GtkSourceStyleScheme *scheme,
+                                     const char           *variant)
+{
+  GtkSourceStyleSchemeManager *style_scheme_manager;
+  GtkSourceStyleScheme *ret;
+  g_autoptr(GString) str = NULL;
+  g_autofree char *key = NULL;
+  const char *mapping;
+
+  g_return_val_if_fail (GTK_SOURCE_IS_STYLE_SCHEME (scheme), NULL);
+  g_return_val_if_fail (g_strcmp0 (variant, "light") == 0 ||
+                        g_strcmp0 (variant, "dark") == 0, NULL);
+
+  style_scheme_manager = gtk_source_style_scheme_manager_get_default ();
+
+  /* If the scheme provides "light-variant" or "dark-variant" metadata,
+   * we will prefer those if the variant is available.
+   */
+  key = g_strdup_printf ("%s-variant", variant);
+  if ((mapping = gtk_source_style_scheme_get_metadata (scheme, key)))
+    {
+      if ((ret = gtk_source_style_scheme_manager_get_scheme (style_scheme_manager, mapping)))
+        return ret;
+    }
+
+  /* Try to find a match by replacing -light/-dark with @variant */
+  str = g_string_new (gtk_source_style_scheme_get_id (scheme));
+
+  if (g_str_has_suffix (str->str, "-light"))
+    g_string_truncate (str, str->len - strlen ("-light"));
+  else if (g_str_has_suffix (str->str, "-dark"))
+    g_string_truncate (str, str->len - strlen ("-dark"));
+
+  g_string_append_printf (str, "-%s", variant);
+
+  /* Look for "Foo-variant" directly */
+  if ((ret = gtk_source_style_scheme_manager_get_scheme (style_scheme_manager, str->str)))
+    return ret;
+
+  /* Look for "Foo" */
+  g_string_truncate (str, str->len - strlen (variant) - 1);
+  if ((ret = gtk_source_style_scheme_manager_get_scheme (style_scheme_manager, str->str)))
+    return ret;
+
+  /* Fallback to what we were provided */
+  return ret;
+}
+
diff --git a/src/libide/sourceview/ide-source-style-scheme.h b/src/libide/sourceview/ide-source-style-scheme.h
new file mode 100644
index 000000000..c5e1abff9
--- /dev/null
+++ b/src/libide/sourceview/ide-source-style-scheme.h
@@ -0,0 +1,40 @@
+/* ide-source-style-scheme.h
+ *
+ * Copyright 2022 Christian Hergert <chergert redhat com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#pragma once
+
+#if !defined (IDE_SOURCEVIEW_INSIDE) && !defined (IDE_SOURCEVIEW_COMPILATION)
+# error "Only <libide-sourceview.h> can be included directly."
+#endif
+
+#include <gtksourceview/gtksource.h>
+
+#include <libide-core.h>
+
+G_BEGIN_DECLS
+
+IDE_AVAILABLE_IN_ALL
+gboolean              ide_source_style_scheme_is_dark     (GtkSourceStyleScheme *scheme);
+IDE_AVAILABLE_IN_ALL
+GtkSourceStyleScheme *ide_source_style_scheme_get_variant (GtkSourceStyleScheme *scheme,
+                                                           const char           *variant);
+
+
+G_END_DECLS
diff --git a/src/libide/sourceview/ide-source-view.h b/src/libide/sourceview/ide-source-view.h
index b235b6b85..e21f744da 100644
--- a/src/libide/sourceview/ide-source-view.h
+++ b/src/libide/sourceview/ide-source-view.h
@@ -20,6 +20,10 @@
 
 #pragma once
 
+#if !defined (IDE_SOURCEVIEW_INSIDE) && !defined (IDE_SOURCEVIEW_COMPILATION)
+# error "Only <libide-sourceview.h> can be included directly."
+#endif
+
 #include <libide-core.h>
 
 #include <gtksourceview/gtksource.h>
diff --git a/src/libide/sourceview/libide-sourceview.h b/src/libide/sourceview/libide-sourceview.h
index 597c2e60e..13ed96cdb 100644
--- a/src/libide/sourceview/libide-sourceview.h
+++ b/src/libide/sourceview/libide-sourceview.h
@@ -27,6 +27,7 @@ G_BEGIN_DECLS
 #define IDE_SOURCEVIEW_INSIDE
 
 #include "ide-line-change-gutter-renderer.h"
+#include "ide-source-style-scheme.h"
 #include "ide-source-view.h"
 #include "ide-text-util.h"
 
diff --git a/src/libide/sourceview/meson.build b/src/libide/sourceview/meson.build
index 957dbc985..af7bd60c6 100644
--- a/src/libide/sourceview/meson.build
+++ b/src/libide/sourceview/meson.build
@@ -15,6 +15,7 @@ libide_sourceview_private_headers = [
 
 libide_sourceview_public_headers = [
   'ide-line-change-gutter-renderer.h',
+  'ide-source-style-scheme.h',
   'ide-source-view.h',
   'ide-text-util.h',
   'libide-sourceview.h',
@@ -35,6 +36,7 @@ libide_sourceview_private_sources = [
 
 libide_sourceview_public_sources = [
   'ide-line-change-gutter-renderer.c',
+  'ide-source-style-scheme.c',
   'ide-source-view.c',
   'ide-text-util.c',
 ]


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]