[gtk/dark-mode] Add some theme information APIs
- From: Matthias Clasen <matthiasc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtk/dark-mode] Add some theme information APIs
- Date: Fri, 30 Aug 2019 07:49:22 +0000 (UTC)
commit 407916184ea5c8096e02b5be9d8a1fd5f7fa1df7
Author: Matthias Clasen <mclasen redhat com>
Date: Thu Aug 29 15:28:18 2019 +0300
Add some theme information APIs
Add some APIs that help applications adapt to
theme changes.
docs/reference/gtk/gtk4-sections.txt | 7 ++
gtk/gtkcssprovider.c | 3 +
gtk/gtkmain.c | 125 +++++++++++++++++++++++++++++++++++
gtk/gtkmain.h | 13 ++++
gtk/gtkprivate.h | 3 +
5 files changed, 151 insertions(+)
---
diff --git a/docs/reference/gtk/gtk4-sections.txt b/docs/reference/gtk/gtk4-sections.txt
index b72906d7ab..c60eb657cc 100644
--- a/docs/reference/gtk/gtk4-sections.txt
+++ b/docs/reference/gtk/gtk4-sections.txt
@@ -4815,6 +4815,13 @@ gtk_get_event_target
gtk_get_event_target_with_type
gtk_propagate_event
+<SUBSECTION>
+gtk_set_supported_themes
+gtk_set_unsupported_themes
+gtk_set_prefer_dark_theme
+gtk_get_current_theme
+gtk_theme_is_dark
+
<SUBSECTION Private>
gtk_init_abi_check
gtk_init_check_abi_check
diff --git a/gtk/gtkcssprovider.c b/gtk/gtkcssprovider.c
index 4943be01f1..c3bd4355f3 100644
--- a/gtk/gtkcssprovider.c
+++ b/gtk/gtkcssprovider.c
@@ -1390,6 +1390,7 @@ gtk_css_provider_load_named (GtkCssProvider *provider,
{
gtk_css_provider_load_from_resource (provider, resource_path);
g_free (resource_path);
+ gtk_set_current_theme (name, variant);
return;
}
g_free (resource_path);
@@ -1417,6 +1418,8 @@ gtk_css_provider_load_named (GtkCssProvider *provider,
priv->path = dir;
g_free (path);
+
+ gtk_set_current_theme (name, variant);
}
else
{
diff --git a/gtk/gtkmain.c b/gtk/gtkmain.c
index e3e592595d..ed9356b54e 100644
--- a/gtk/gtkmain.c
+++ b/gtk/gtkmain.c
@@ -2677,3 +2677,128 @@ gtk_propagate_event (GtkWidget *widget,
gtk_propagate_event_internal (widget, event, topmost);
}
+
+static char **supported_themes;
+static char **unsupported_themes;
+static char *current_theme_name;
+
+/**
+ * gtk_set_supported_themes:
+ * @themes: a %NULL-terminated array of theme names
+ *
+ * Sets a list of theme names that are supported
+ * by the application. You should mark a theme as supported
+ * if the application has been tested with this theme.
+ *
+ * Marking a theme as supported is mainly informational;
+ * GTK may warn the user if a theme is not supported.
+ */
+void
+gtk_set_supported_themes (const char **themes)
+{
+ g_strfreev (supported_themes);
+ supported_themes = g_strdupv (themes);
+}
+
+/**
+ * gtk_set_unsupported_themes:
+ * @themes: a %NULL-terminated array of theme names
+ *
+ * Sets a list of theme names that are unsupported
+ * by the application. You should mark a theme as unsupported
+ * if the application is known to have problems with this
+ * theme.
+ *
+ * Marking a theme as unsupported is mainly informational;
+ * GTK may warn the user if a theme is unsupported.
+ */
+void
+gtk_set_unsupported_themes (const char **themes)
+{
+ g_strfreev (unsupported_themes);
+ unsupported_themes = g_strdupv (themes);
+}
+
+/**
+ * gtk_set_prefer_dark_theme:
+ * @prefer_dark: whether a dark theme variant is preferred
+ * by the application
+ *
+ * A convenience wrapper for the #GtkSettings:gtk-application-prefer-dark
+ * setting.
+ */
+void
+gtk_set_prefer_dark_theme (gboolean prefer_dark)
+{
+ GtkSettings *settings;
+
+ settings = gtk_settings_get_default ();
+
+ g_object_set (settings, "gtk-application-prefer-dark", prefer_dark, NULL);
+}
+
+/**
+ * gtk_get_current_theme:
+ *
+ * Gets the name of the theme that the application is using.
+ *
+ * This may be different from the #GtkSettings:gtk-theme-name
+ * setting. If the theme is a dark variant, the "-dark" suffix
+ * will be included in the name.
+ *
+ * If you use this function, you probably want to listen for
+ * changes of the #GtkSettings:gtk-theme-name and #GtkSettings:gtk-application-prefer-dark
+ * properties.
+ *
+ * Returns: the name of theme that is used
+ */
+const char *
+gtk_get_current_theme (void)
+{
+ return current_theme_name;
+}
+
+void
+gtk_set_current_theme (const char *name,
+ const char *variant)
+{
+ char *theme;
+
+ if (variant)
+ theme = g_strconcat (name, "-", variant, NULL);
+ else
+ theme = g_strdup (name);
+
+ if (unsupported_themes != NULL &&
+ g_strv_contains (unsupported_themes, theme))
+ g_warning ("Theme %s is unsupported with this application\n", theme);
+ else if (supported_themes != NULL &&
+ !g_strv_contains (supported_themes, theme))
+ g_warning ("Theme %s is untested with this application\n", theme);
+
+ g_free (current_theme_name);
+ current_theme_name = theme;
+}
+
+/**
+ * gtk_theme_is_dark:
+ *
+ * Returns whether the current theme is a dark theme.
+ *
+ * This information can be used to adapt custom drawing
+ * to light or dark surroundings.
+ *
+ * If you use this function, you probably want to listen for
+ * changes of the #GtkSettings:gtk-theme-name and #GtkSettings:gtk-application-prefer-dark
+ * properties.
+ *
+ * Returns: %TRUE if the current theme is dark
+ */
+gboolean
+gtk_theme_is_dark (void)
+{
+ if (current_theme_name)
+ return g_str_has_suffix (current_theme_name, "-dark");
+
+ return FALSE;
+}
diff --git a/gtk/gtkmain.h b/gtk/gtkmain.h
index 81c1e574ad..9ef8296f57 100644
--- a/gtk/gtkmain.h
+++ b/gtk/gtkmain.h
@@ -103,6 +103,19 @@ gboolean gtk_init_check_abi_check (int num_checks,
#endif
+GDK_AVAILABLE_IN_ALL
+void gtk_set_supported_themes (const char **themes);
+GDK_AVAILABLE_IN_ALL
+void gtk_set_unsupported_themes (const char **themes);
+
+GDK_AVAILABLE_IN_ALL
+void gtk_set_prefer_dark_theme (gboolean prefer_dark);
+GDK_AVAILABLE_IN_ALL
+const char * gtk_get_current_theme (void);
+GDK_AVAILABLE_IN_ALL
+gboolean gtk_theme_is_dark (void);
+
+
GDK_AVAILABLE_IN_ALL
void gtk_disable_setlocale (void);
GDK_AVAILABLE_IN_ALL
diff --git a/gtk/gtkprivate.h b/gtk/gtkprivate.h
index 81f9068693..b2c92ad4b0 100644
--- a/gtk/gtkprivate.h
+++ b/gtk/gtkprivate.h
@@ -137,6 +137,9 @@ gboolean gtk_get_any_display_debug_flag_set (void);
#endif /* G_ENABLE_DEBUG */
+void gtk_set_current_theme (const char *name,
+ const char *variant);
+
G_END_DECLS
#endif /* __GTK_PRIVATE_H__ */
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]