comparison infinite_canvas/theme.c @ 276:b55c22cff335

Add interactive infinite canvas prototype
author MrJuneJune <me@mrjunejune.com>
date Mon, 17 Aug 2026 16:57:56 -0700
parents
children 8d560f50ed4c
comparison
equal deleted inserted replaced
274:c9be578316a6 276:b55c22cff335
1 #include "infinite_canvas/theme.h"
2
3 #include <stdlib.h>
4 #include <string.h>
5
6 #if defined(PLATFORM_WEB) || defined(__EMSCRIPTEN__)
7 #include <emscripten/emscripten.h>
8
9 EM_JS(int, Canvas_Web_Prefers_Dark, (), {
10 return window.matchMedia &&
11 window.matchMedia("(prefers-color-scheme: dark)").matches ? 1 : 0;
12 });
13 #elif defined(_WIN32)
14 #include <windows.h>
15 #else
16 #include <stdio.h>
17 #endif
18
19 Canvas_Theme_Mode Canvas_Theme_Mode_From_String(const char *p_value)
20 {
21 if (!p_value || strcmp(p_value, "auto") == 0) return CANVAS_THEME_AUTO;
22 if (strcmp(p_value, "light") == 0) return CANVAS_THEME_LIGHT;
23 if (strcmp(p_value, "dark") == 0) return CANVAS_THEME_DARK;
24 return CANVAS_THEME_AUTO;
25 }
26
27 boolean Canvas_System_Prefers_Dark(void)
28 {
29 #if defined(PLATFORM_WEB) || defined(__EMSCRIPTEN__)
30 return Canvas_Web_Prefers_Dark() ? TRUE : FALSE;
31 #elif defined(_WIN32)
32 DWORD light_theme = 1;
33 DWORD size = sizeof(light_theme);
34 LSTATUS status = RegGetValueA(
35 HKEY_CURRENT_USER,
36 "Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize",
37 "AppsUseLightTheme",
38 RRF_RT_REG_DWORD,
39 NULL,
40 &light_theme,
41 &size);
42 return status == ERROR_SUCCESS && light_theme == 0 ? TRUE : FALSE;
43 #elif defined(__APPLE__)
44 FILE *p_pipe = popen("defaults read -g AppleInterfaceStyle 2>/dev/null", "r");
45 if (!p_pipe) return FALSE;
46 char value[32] = {0};
47 boolean dark = fgets(value, sizeof(value), p_pipe) &&
48 strstr(value, "Dark") ? TRUE : FALSE;
49 pclose(p_pipe);
50 return dark;
51 #else
52 const char *p_gtk_theme = getenv("GTK_THEME");
53 if (p_gtk_theme &&
54 (strstr(p_gtk_theme, "dark") || strstr(p_gtk_theme, "Dark"))) {
55 return TRUE;
56 }
57
58 FILE *p_pipe = popen(
59 "gsettings get org.gnome.desktop.interface color-scheme 2>/dev/null",
60 "r");
61 if (!p_pipe) return FALSE;
62 char value[64] = {0};
63 boolean dark = fgets(value, sizeof(value), p_pipe) &&
64 strstr(value, "prefer-dark") ? TRUE : FALSE;
65 pclose(p_pipe);
66 return dark;
67 #endif
68 }
69
70 void Canvas_Theme_Resolve(Canvas_Theme *p_theme, Canvas_Theme_Mode mode)
71 {
72 Canvas_Theme_Mode resolved = mode;
73 if (resolved == CANVAS_THEME_AUTO) {
74 resolved = Canvas_System_Prefers_Dark() ?
75 CANVAS_THEME_DARK :
76 CANVAS_THEME_LIGHT;
77 }
78
79 *p_theme = (Canvas_Theme){
80 .requested_mode = mode,
81 .resolved_mode = resolved,
82 };
83 if (resolved == CANVAS_THEME_DARK) {
84 p_theme->background = (Color){15, 15, 17, 255};
85 p_theme->surface = (Color){24, 24, 27, 255};
86 p_theme->surface_muted = (Color){39, 39, 42, 255};
87 p_theme->border = (Color){63, 63, 70, 255};
88 p_theme->text = (Color){244, 244, 245, 255};
89 p_theme->text_muted = (Color){161, 161, 170, 255};
90 p_theme->accent = (Color){96, 165, 250, 255};
91 p_theme->accent_soft = (Color){30, 58, 95, 255};
92 p_theme->shadow = (Color){0, 0, 0, 110};
93 p_theme->toast_surface = (Color){24, 24, 27, 255};
94 p_theme->toast_border = (Color){63, 63, 70, 255};
95 } else {
96 p_theme->background = (Color){255, 255, 255, 255};
97 p_theme->surface = (Color){255, 255, 255, 252};
98 p_theme->surface_muted = (Color){244, 247, 251, 255};
99 p_theme->border = (Color){205, 211, 221, 255};
100 p_theme->text = (Color){24, 24, 27, 255};
101 p_theme->text_muted = (Color){113, 113, 122, 255};
102 p_theme->accent = (Color){37, 99, 235, 255};
103 p_theme->accent_soft = (Color){239, 246, 255, 255};
104 p_theme->shadow = (Color){15, 23, 42, 36};
105 p_theme->toast_surface = (Color){255, 255, 255, 255};
106 p_theme->toast_border = (Color){212, 216, 223, 255};
107 }
108 }
109
110 void Canvas_Theme_Toggle(Canvas_Theme *p_theme)
111 {
112 Canvas_Theme_Resolve(
113 p_theme,
114 p_theme->resolved_mode == CANVAS_THEME_DARK ?
115 CANVAS_THEME_LIGHT :
116 CANVAS_THEME_DARK);
117 }
118
119 void Canvas_Theme_Draw_Shadow(
120 const Canvas_Theme *p_theme,
121 Rectangle bounds,
122 float roundness,
123 int32 segments,
124 float zoom,
125 float opacity)
126 {
127 float offset = 6.0f / zoom;
128 float shadow_alpha = ((float)p_theme->shadow.a / 255.0f) * opacity;
129 DrawRectangleRounded(
130 (Rectangle){
131 bounds.x + offset,
132 bounds.y + offset,
133 bounds.width,
134 bounds.height,
135 },
136 roundness,
137 segments,
138 Fade(p_theme->shadow, shadow_alpha));
139 }