Skip to content

Commit 96b2973

Browse files
committed
feat: new accent color switcher in the preferences page
a little something to take the edge off
1 parent 97cce6d commit 96b2973

7 files changed

Lines changed: 118 additions & 1 deletion

‎data/io.github.vani_tty1.memerist.gschema.xml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@
3131
<description>Preferred color scheme: 'light', 'dark', or 'default'</description>
3232
</key>
3333

34+
<key name="accent-color" type="s">
35+
<default>'default'</default>
36+
<summary>Accent color</summary>
37+
<description>App accent color: 'default' (follow the system), 'blue', 'teal', 'green', 'yellow', 'orange', 'red', 'pink', 'purple' or 'slate'</description>
38+
</key>
39+
3440
<key name="hidden-templates" type="as">
3541
<default>[]</default>
3642
<summary>Hidden built-in templates</summary>

‎src/meme-accent.c‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
}

‎src/meme-accent.h‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#pragma once
2+
3+
#include <glib.h>
4+
5+
G_BEGIN_DECLS
6+
7+
void meme_accent_init (void);
8+
9+
G_END_DECLS

‎src/meme-application.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "meme-window.h"
2525
#include "meme-welcome-dialog.h"
2626
#include "meme-preferences-dialog.h"
27+
#include "meme-accent.h"
2728
#include "config.h"
2829
#include <epoxy/gl.h>
2930
#include "gdk/gdk.h"
@@ -238,6 +239,7 @@ meme_application_startup (GApplication *app)
238239
{
239240

240241
G_APPLICATION_CLASS (meme_application_parent_class)->startup (app);
242+
meme_accent_init ();
241243
g_action_map_add_action_entries (G_ACTION_MAP (app),
242244
app_actions,
243245
G_N_ELEMENTS (app_actions),

‎src/meme-preferences-dialog.c‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,28 @@
2222
#include <adwaita.h>
2323
#include "meme-preferences-dialog.h"
2424

25+
static void
26+
update_reset_sensitivity (GSettings *settings, const char *key, GtkWidget *button)
27+
{
28+
g_autofree char *id = g_settings_get_string (settings, "accent-color");
29+
30+
gtk_widget_set_sensitive (button, g_strcmp0 (id, "default") != 0);
31+
}
32+
33+
static void
34+
on_reset_clicked (GtkButton *button, GSettings *settings)
35+
{
36+
g_settings_reset (settings, "accent-color");
37+
}
38+
2539
void
2640
meme_show_preferences_dialog (GtkWindow *parent)
2741
{
2842
GtkBuilder *builder;
2943
AdwDialog *dialog;
3044
GtkWidget *reduce_quality_row;
45+
GtkWidget *accent_reset_button;
46+
GSimpleActionGroup *actions;
3147
GSettings *settings;
3248

3349
builder = gtk_builder_new_from_resource ("/io/github/vani_tty1/memerist/preferences-dialog.ui");
@@ -40,6 +56,17 @@ meme_show_preferences_dialog (GtkWindow *parent)
4056
reduce_quality_row, "active",
4157
G_SETTINGS_BIND_DEFAULT);
4258

59+
accent_reset_button = GTK_WIDGET (gtk_builder_get_object (builder, "accent_reset_button"));
60+
actions = g_simple_action_group_new ();
61+
g_action_map_add_action (G_ACTION_MAP (actions), g_settings_create_action (settings, "accent-color"));
62+
gtk_widget_insert_action_group (GTK_WIDGET (dialog), "prefs", G_ACTION_GROUP (actions));
63+
g_object_unref (actions);
64+
65+
g_signal_connect_object (settings, "changed::accent-color",
66+
G_CALLBACK (update_reset_sensitivity), accent_reset_button, 0);
67+
g_signal_connect (accent_reset_button, "clicked", G_CALLBACK (on_reset_clicked), settings);
68+
update_reset_sensitivity (settings, "accent-color", accent_reset_button);
69+
4370
g_object_set_data_full (G_OBJECT (dialog), "meme-preferences-settings",
4471
settings, g_object_unref);
4572

‎src/meson.build‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ blueprints = custom_target('blueprints',
1616
myapp_sources = [
1717
'main.c',
1818
'meme-application.c',
19+
'meme-accent.c',
1920
'meme-core.c',
2021
'meme-window.c',
2122
'meme-window-canvas.c',

‎src/template-window.blp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ using Gtk 4.0;
22
using Adw 1;
33

44
Adw.Dialog template_window {
5-
content-width: 480;
5+
content-width: 580;
66
content-height: 640;
77
width-request: 320;
88
height-request: 320;

0 commit comments

Comments
 (0)