|
| 1 | +#include "meme-accent.h" |
| 2 | +#include <adwaita.h> |
| 3 | + |
| 4 | +typedef struct { |
| 5 | + const char *id; |
| 6 | + const char *bg; |
| 7 | + const char *light; |
| 8 | + const char *dark; |
| 9 | +} AccentColor; |
| 10 | + |
| 11 | +static const AccentColor accent_colors[] = { |
| 12 | + { "blue", "#3584e4", "#0461be", "#78aeed" }, |
| 13 | + { "teal", "#2190a4", "#007184", "#7bdff4" }, |
| 14 | + { "green", "#3a944a", "#15772e", "#8de6b1" }, |
| 15 | + { "yellow", "#c88800", "#905400", "#f8e45c" }, |
| 16 | + { "orange", "#ed5b00", "#c64600", "#ffa348" }, |
| 17 | + { "red", "#e62d42", "#c01c28", "#ff7b63" }, |
| 18 | + { "pink", "#d56199", "#a2326c", "#ffa0d0" }, |
| 19 | + { "purple", "#9141ac", "#7a2f94", "#dc8add" }, |
| 20 | + { "slate", "#6f8396", "#4f6376", "#c0d5ea" }, |
| 21 | +}; |
| 22 | + |
| 23 | +static GSettings *settings; |
| 24 | +static GtkCssProvider *provider; |
| 25 | + |
| 26 | +static void |
| 27 | +apply_accent (void) |
| 28 | +{ |
| 29 | + g_autofree char *id = g_settings_get_string (settings, "accent-color"); |
| 30 | + gboolean dark = adw_style_manager_get_dark (adw_style_manager_get_default ()); |
| 31 | + g_autofree char *css = NULL; |
| 32 | + guint i; |
| 33 | + |
| 34 | + for (i = 0; i < G_N_ELEMENTS (accent_colors); i++) |
| 35 | + { |
| 36 | + const AccentColor *c = &accent_colors[i]; |
| 37 | + |
| 38 | + if (g_strcmp0 (c->id, id) != 0) |
| 39 | + continue; |
| 40 | + |
| 41 | + css = g_strdup_printf ("@define-color accent_bg_color %s;\n" |
| 42 | + "@define-color accent_fg_color #ffffff;\n" |
| 43 | + "@define-color accent_color %s;\n", |
| 44 | + c->bg, dark ? c->dark : c->light); |
| 45 | + break; |
| 46 | + } |
| 47 | + |
| 48 | + gtk_css_provider_load_from_string (provider, css ? css : ""); |
| 49 | +} |
| 50 | + |
| 51 | +static void |
| 52 | +on_accent_changed (void) |
| 53 | +{ |
| 54 | + apply_accent (); |
| 55 | +} |
| 56 | + |
| 57 | +void |
| 58 | +meme_accent_init (void) |
| 59 | +{ |
| 60 | + if (provider != NULL) |
| 61 | + return; |
| 62 | + |
| 63 | + settings = g_settings_new ("io.github.vani_tty1.memerist"); |
| 64 | + provider = gtk_css_provider_new (); |
| 65 | + gtk_style_context_add_provider_for_display (gdk_display_get_default (), |
| 66 | + GTK_STYLE_PROVIDER (provider), |
| 67 | + GTK_STYLE_PROVIDER_PRIORITY_APPLICATION + 1); |
| 68 | + g_signal_connect (settings, "changed::accent-color", G_CALLBACK (on_accent_changed), NULL); |
| 69 | + g_signal_connect (adw_style_manager_get_default (), "notify::dark", G_CALLBACK (on_accent_changed), NULL); |
| 70 | + |
| 71 | + apply_accent (); |
| 72 | +} |
0 commit comments