widget-factory: Add theme switch

Add a submenu to test the builtin themes. This is mainly
for the benefit of the artists working on those themes.
This commit is contained in:
Matthias Clasen 2020-03-26 13:51:59 -04:00
parent eaa20f5cdf
commit 8ab2e31b58
2 changed files with 89 additions and 6 deletions

View File

@ -26,7 +26,7 @@
#include <gtk/gtk.h>
static void
change_theme_state (GSimpleAction *action,
change_dark_state (GSimpleAction *action,
GVariant *state,
gpointer user_data)
{
@ -40,6 +40,57 @@ change_theme_state (GSimpleAction *action,
g_simple_action_set_state (action, state);
}
static char *current_theme;
static gboolean current_dark;
static void
change_theme_state (GSimpleAction *action,
GVariant *state,
gpointer user_data)
{
GtkSettings *settings = gtk_settings_get_default ();
const char *s;
const char *theme;
gboolean prefer_dark = FALSE;
s = g_variant_get_string (state, NULL);
if (strcmp (s, "adwaita") == 0)
{
theme = "Adwaita";
prefer_dark = FALSE;
}
else if (strcmp (s, "adwaita-dark") == 0)
{
theme = "Adwaita";
prefer_dark = TRUE;
}
else if (strcmp (s, "highcontrast") == 0)
{
theme = "HighContrast";
prefer_dark = FALSE;
}
else if (strcmp (s, "highcontrast-inverse") == 0)
{
theme = "HighContrastInverse";
prefer_dark = FALSE;
}
else if (strcmp (s, "current") == 0)
{
theme = current_theme;
prefer_dark = current_dark;
}
else
return;
g_object_set (G_OBJECT (settings),
"gtk-theme-name", theme,
"gtk-application-prefer-dark-theme", prefer_dark,
NULL);
g_simple_action_set_state (action, state);
}
static GtkWidget *page_stack;
static void
@ -1671,7 +1722,8 @@ activate (GApplication *app)
GtkCssProvider *provider;
GMenuModel *model;
static GActionEntry win_entries[] = {
{ "dark", NULL, NULL, "false", change_theme_state },
{ "dark", NULL, NULL, "false", change_dark_state },
{ "theme", NULL, "s", "'current'", change_theme_state },
{ "transition", NULL, NULL, "false", change_transition_state },
{ "search", activate_search, NULL, NULL, NULL },
{ "delete", activate_delete, NULL, NULL, NULL },
@ -1703,6 +1755,11 @@ activate (GApplication *app)
GPermission *permission;
GAction *action;
g_object_get (gtk_settings_get_default (),
"gtk-theme-name", &current_theme,
"gtk-application-prefer-dark-theme", &current_dark,
NULL);
g_type_ensure (my_text_view_get_type ());
provider = gtk_css_provider_new ();

View File

@ -6,10 +6,36 @@
<attribute name="label" translatable="yes">Get Busy</attribute>
<attribute name="action">win.busy</attribute>
</item>
<item>
<attribute name="label" translatable="yes">Dark Theme</attribute>
<attribute name="action">win.dark</attribute>
</item>
<submenu>
<attribute name="label" translatable="yes">Theme</attribute>
<section>
<item>
<attribute name="label" translatable="yes">Current theme</attribute>
<attribute name="action">win.theme</attribute>
<attribute name="target">current</attribute>
</item>
<item>
<attribute name="label" translatable="yes">Adwaita</attribute>
<attribute name="action">win.theme</attribute>
<attribute name="target">adwaita</attribute>
</item>
<item>
<attribute name="label" translatable="yes">Adwaita dark</attribute>
<attribute name="action">win.theme</attribute>
<attribute name="target">adwaita-dark</attribute>
</item>
<item>
<attribute name="label" translatable="yes">High contrast</attribute>
<attribute name="action">win.theme</attribute>
<attribute name="target">highcontrast</attribute>
</item>
<item>
<attribute name="label" translatable="yes">High contrast inverse</attribute>
<attribute name="action">win.theme</attribute>
<attribute name="target">highcontrast-inverse</attribute>
</item>
</section>
</submenu>
<item>
<attribute name="label" translatable="yes">Slide Pages</attribute>
<attribute name="action">win.transition</attribute>