summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthias Clasen <mclasen@redhat.com>2019-08-29 15:28:18 +0300
committerMatthias Clasen <mclasen@redhat.com>2019-08-30 11:40:53 +0300
commit60b706e76476083a5f63c56d43c7db770c9819ec (patch)
tree72f3d3ef222d9948936d390510b37065dd51767f
parent6905935fd38dd0afed9b708cc6adb048d6424e58 (diff)
downloadgtk+-dark-mode.tar.gz
Add some theme information APIsdark-mode
Add some APIs that help applications adapt to theme changes.
-rw-r--r--docs/reference/gtk/gtk4-sections.txt7
-rw-r--r--gtk/gtkcssprovider.c3
-rw-r--r--gtk/gtkmain.c125
-rw-r--r--gtk/gtkmain.h13
-rw-r--r--gtk/gtkprivate.h3
5 files changed, 151 insertions, 0 deletions
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..cc4839b9fa 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", theme);
+ else if (supported_themes != NULL &&
+ !g_strv_contains (supported_themes, theme))
+ g_info ("Theme %s is untested with this application", 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
@@ -104,6 +104,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
PangoLanguage *gtk_get_default_language (void);
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__ */