pidgin/pidginstylecontext.c

Sat, 30 Jan 2021 22:33:58 -0600

author
Gary Kramlich <grim@reaperworld.com>
date
Sat, 30 Jan 2021 22:33:58 -0600
changeset 40749
b7ee9469eccc
parent 40724
b317bb7479e5
permissions
-rw-r--r--

g_task_return_* refs a task so we need to make sure we remove our reference when we're done with the task.

Testing Done:
Compiled and connected to bonjour and xmpp locally.

Reviewed at https://reviews.imfreedom.org/r/470/

38699
e7b5011e4728 Namespace the contrast stuff and move it to it's own file/namespace
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
1 /*
40592
559881c7bf92 Rename gtkstyle.[ch] to pidginstyle.[ch] and fix up the dark mode check.
Gary Kramlich <grim@reaperworld.com>
parents: 39277
diff changeset
2 * Pidgin - Internet Messenger
559881c7bf92 Rename gtkstyle.[ch] to pidginstyle.[ch] and fix up the dark mode check.
Gary Kramlich <grim@reaperworld.com>
parents: 39277
diff changeset
3 * Copyright (C) Pidgin Developers <devel@pidgin.im>
38699
e7b5011e4728 Namespace the contrast stuff and move it to it's own file/namespace
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
4 *
e7b5011e4728 Namespace the contrast stuff and move it to it's own file/namespace
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
5 * Pidgin is the legal property of its developers, whose names are too numerous
e7b5011e4728 Namespace the contrast stuff and move it to it's own file/namespace
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
6 * to list here. Please refer to the COPYRIGHT file distributed with this
e7b5011e4728 Namespace the contrast stuff and move it to it's own file/namespace
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
7 * source distribution.
e7b5011e4728 Namespace the contrast stuff and move it to it's own file/namespace
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
8 *
e7b5011e4728 Namespace the contrast stuff and move it to it's own file/namespace
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
9 * This program is free software; you can redistribute it and/or modify
e7b5011e4728 Namespace the contrast stuff and move it to it's own file/namespace
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
10 * under the terms of the GNU General Public License as published by
e7b5011e4728 Namespace the contrast stuff and move it to it's own file/namespace
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
11 * the Free Software Foundation; either version 2 of the License, or
e7b5011e4728 Namespace the contrast stuff and move it to it's own file/namespace
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
12 * (at your option) any later version.
e7b5011e4728 Namespace the contrast stuff and move it to it's own file/namespace
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
13 *
e7b5011e4728 Namespace the contrast stuff and move it to it's own file/namespace
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
14 * This program is distributed in the hope that it will be useful,
e7b5011e4728 Namespace the contrast stuff and move it to it's own file/namespace
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
e7b5011e4728 Namespace the contrast stuff and move it to it's own file/namespace
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
e7b5011e4728 Namespace the contrast stuff and move it to it's own file/namespace
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
17 * GNU General Public License for more details.
e7b5011e4728 Namespace the contrast stuff and move it to it's own file/namespace
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
18 *
e7b5011e4728 Namespace the contrast stuff and move it to it's own file/namespace
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
19 * You should have received a copy of the GNU General Public License
40592
559881c7bf92 Rename gtkstyle.[ch] to pidginstyle.[ch] and fix up the dark mode check.
Gary Kramlich <grim@reaperworld.com>
parents: 39277
diff changeset
20 * along with this library; if not, see <https://www.gnu.org/licenses/>.
38699
e7b5011e4728 Namespace the contrast stuff and move it to it's own file/namespace
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
21 */
40592
559881c7bf92 Rename gtkstyle.[ch] to pidginstyle.[ch] and fix up the dark mode check.
Gary Kramlich <grim@reaperworld.com>
parents: 39277
diff changeset
22
40695
df2674f2d982 Rename the pidgin_style_* api to pidgin_style_context_*
Gary Kramlich <grim@reaperworld.com>
parents: 40592
diff changeset
23 #include "pidgin/pidginstylecontext.h"
38699
e7b5011e4728 Namespace the contrast stuff and move it to it's own file/namespace
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
24
e7b5011e4728 Namespace the contrast stuff and move it to it's own file/namespace
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
25 /* Assume light mode */
40592
559881c7bf92 Rename gtkstyle.[ch] to pidginstyle.[ch] and fix up the dark mode check.
Gary Kramlich <grim@reaperworld.com>
parents: 39277
diff changeset
26 static gboolean dark_mode_have_cache = FALSE;
559881c7bf92 Rename gtkstyle.[ch] to pidginstyle.[ch] and fix up the dark mode check.
Gary Kramlich <grim@reaperworld.com>
parents: 39277
diff changeset
27 static gboolean dark_mode_cached_value = FALSE;
38699
e7b5011e4728 Namespace the contrast stuff and move it to it's own file/namespace
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
28
40695
df2674f2d982 Rename the pidgin_style_* api to pidgin_style_context_*
Gary Kramlich <grim@reaperworld.com>
parents: 40592
diff changeset
29 /******************************************************************************
df2674f2d982 Rename the pidgin_style_* api to pidgin_style_context_*
Gary Kramlich <grim@reaperworld.com>
parents: 40592
diff changeset
30 * Public API
df2674f2d982 Rename the pidgin_style_* api to pidgin_style_context_*
Gary Kramlich <grim@reaperworld.com>
parents: 40592
diff changeset
31 *****************************************************************************/
40713
766bbe6061ec Greatly simplify the nick color generation and properly detect dark themes.
Gary Kramlich <grim@reaperworld.com>
parents: 40695
diff changeset
32 void
766bbe6061ec Greatly simplify the nick color generation and properly detect dark themes.
Gary Kramlich <grim@reaperworld.com>
parents: 40695
diff changeset
33 pidgin_style_context_get_background_color(GdkRGBA *color) {
766bbe6061ec Greatly simplify the nick color generation and properly detect dark themes.
Gary Kramlich <grim@reaperworld.com>
parents: 40695
diff changeset
34 /* This value will leak, we could put a shutdown function in but right now
766bbe6061ec Greatly simplify the nick color generation and properly detect dark themes.
Gary Kramlich <grim@reaperworld.com>
parents: 40695
diff changeset
35 * that seems like a bit much for a few bytes.
766bbe6061ec Greatly simplify the nick color generation and properly detect dark themes.
Gary Kramlich <grim@reaperworld.com>
parents: 40695
diff changeset
36 */
766bbe6061ec Greatly simplify the nick color generation and properly detect dark themes.
Gary Kramlich <grim@reaperworld.com>
parents: 40695
diff changeset
37 static GdkRGBA *background = NULL;
766bbe6061ec Greatly simplify the nick color generation and properly detect dark themes.
Gary Kramlich <grim@reaperworld.com>
parents: 40695
diff changeset
38
766bbe6061ec Greatly simplify the nick color generation and properly detect dark themes.
Gary Kramlich <grim@reaperworld.com>
parents: 40695
diff changeset
39 if(g_once_init_enter(&background)) {
766bbe6061ec Greatly simplify the nick color generation and properly detect dark themes.
Gary Kramlich <grim@reaperworld.com>
parents: 40695
diff changeset
40 GdkRGBA *bg = NULL;
766bbe6061ec Greatly simplify the nick color generation and properly detect dark themes.
Gary Kramlich <grim@reaperworld.com>
parents: 40695
diff changeset
41 GtkStyleContext *context = NULL;
766bbe6061ec Greatly simplify the nick color generation and properly detect dark themes.
Gary Kramlich <grim@reaperworld.com>
parents: 40695
diff changeset
42 GtkWidget *window = NULL;
766bbe6061ec Greatly simplify the nick color generation and properly detect dark themes.
Gary Kramlich <grim@reaperworld.com>
parents: 40695
diff changeset
43
766bbe6061ec Greatly simplify the nick color generation and properly detect dark themes.
Gary Kramlich <grim@reaperworld.com>
parents: 40695
diff changeset
44 /* We create a window to get its background color from its style
766bbe6061ec Greatly simplify the nick color generation and properly detect dark themes.
Gary Kramlich <grim@reaperworld.com>
parents: 40695
diff changeset
45 * context. This _is_ doable without creating a window, but you still
766bbe6061ec Greatly simplify the nick color generation and properly detect dark themes.
Gary Kramlich <grim@reaperworld.com>
parents: 40695
diff changeset
46 * need the window class and about four times as much code, so that's
766bbe6061ec Greatly simplify the nick color generation and properly detect dark themes.
Gary Kramlich <grim@reaperworld.com>
parents: 40695
diff changeset
47 * why we do it this way.
766bbe6061ec Greatly simplify the nick color generation and properly detect dark themes.
Gary Kramlich <grim@reaperworld.com>
parents: 40695
diff changeset
48 */
766bbe6061ec Greatly simplify the nick color generation and properly detect dark themes.
Gary Kramlich <grim@reaperworld.com>
parents: 40695
diff changeset
49 window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
766bbe6061ec Greatly simplify the nick color generation and properly detect dark themes.
Gary Kramlich <grim@reaperworld.com>
parents: 40695
diff changeset
50 context = gtk_widget_get_style_context(window);
766bbe6061ec Greatly simplify the nick color generation and properly detect dark themes.
Gary Kramlich <grim@reaperworld.com>
parents: 40695
diff changeset
51
766bbe6061ec Greatly simplify the nick color generation and properly detect dark themes.
Gary Kramlich <grim@reaperworld.com>
parents: 40695
diff changeset
52 gtk_style_context_get(context, GTK_STATE_FLAG_NORMAL,
766bbe6061ec Greatly simplify the nick color generation and properly detect dark themes.
Gary Kramlich <grim@reaperworld.com>
parents: 40695
diff changeset
53 GTK_STYLE_PROPERTY_BACKGROUND_COLOR, &bg,
766bbe6061ec Greatly simplify the nick color generation and properly detect dark themes.
Gary Kramlich <grim@reaperworld.com>
parents: 40695
diff changeset
54 NULL);
40724
b317bb7479e5 Fix some assertions from GTK
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents: 40713
diff changeset
55 gtk_widget_destroy(window);
40713
766bbe6061ec Greatly simplify the nick color generation and properly detect dark themes.
Gary Kramlich <grim@reaperworld.com>
parents: 40695
diff changeset
56
766bbe6061ec Greatly simplify the nick color generation and properly detect dark themes.
Gary Kramlich <grim@reaperworld.com>
parents: 40695
diff changeset
57 g_once_init_leave(&background, bg);
766bbe6061ec Greatly simplify the nick color generation and properly detect dark themes.
Gary Kramlich <grim@reaperworld.com>
parents: 40695
diff changeset
58 }
766bbe6061ec Greatly simplify the nick color generation and properly detect dark themes.
Gary Kramlich <grim@reaperworld.com>
parents: 40695
diff changeset
59
766bbe6061ec Greatly simplify the nick color generation and properly detect dark themes.
Gary Kramlich <grim@reaperworld.com>
parents: 40695
diff changeset
60 *color = *background;
766bbe6061ec Greatly simplify the nick color generation and properly detect dark themes.
Gary Kramlich <grim@reaperworld.com>
parents: 40695
diff changeset
61 }
766bbe6061ec Greatly simplify the nick color generation and properly detect dark themes.
Gary Kramlich <grim@reaperworld.com>
parents: 40695
diff changeset
62
38699
e7b5011e4728 Namespace the contrast stuff and move it to it's own file/namespace
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
63 gboolean
40713
766bbe6061ec Greatly simplify the nick color generation and properly detect dark themes.
Gary Kramlich <grim@reaperworld.com>
parents: 40695
diff changeset
64 pidgin_style_context_is_dark(void) {
40695
df2674f2d982 Rename the pidgin_style_* api to pidgin_style_context_*
Gary Kramlich <grim@reaperworld.com>
parents: 40592
diff changeset
65 GdkRGBA bg;
40592
559881c7bf92 Rename gtkstyle.[ch] to pidginstyle.[ch] and fix up the dark mode check.
Gary Kramlich <grim@reaperworld.com>
parents: 39277
diff changeset
66 gdouble luminance = 0.0;
38699
e7b5011e4728 Namespace the contrast stuff and move it to it's own file/namespace
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
67
40713
766bbe6061ec Greatly simplify the nick color generation and properly detect dark themes.
Gary Kramlich <grim@reaperworld.com>
parents: 40695
diff changeset
68 if(dark_mode_have_cache) {
766bbe6061ec Greatly simplify the nick color generation and properly detect dark themes.
Gary Kramlich <grim@reaperworld.com>
parents: 40695
diff changeset
69 return dark_mode_cached_value;
38699
e7b5011e4728 Namespace the contrast stuff and move it to it's own file/namespace
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
70 }
e7b5011e4728 Namespace the contrast stuff and move it to it's own file/namespace
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
71
40713
766bbe6061ec Greatly simplify the nick color generation and properly detect dark themes.
Gary Kramlich <grim@reaperworld.com>
parents: 40695
diff changeset
72 pidgin_style_context_get_background_color(&bg);
38699
e7b5011e4728 Namespace the contrast stuff and move it to it's own file/namespace
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
73
40713
766bbe6061ec Greatly simplify the nick color generation and properly detect dark themes.
Gary Kramlich <grim@reaperworld.com>
parents: 40695
diff changeset
74 /* 709 coefficients from https://en.wikipedia.org/wiki/Luma_(video) */
766bbe6061ec Greatly simplify the nick color generation and properly detect dark themes.
Gary Kramlich <grim@reaperworld.com>
parents: 40695
diff changeset
75 luminance = (0.2126 * bg.red) + (0.7152 * bg.green) + (0.0722 * bg.blue);
38699
e7b5011e4728 Namespace the contrast stuff and move it to it's own file/namespace
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
76
40713
766bbe6061ec Greatly simplify the nick color generation and properly detect dark themes.
Gary Kramlich <grim@reaperworld.com>
parents: 40695
diff changeset
77 dark_mode_cached_value = (luminance < 0.5);
40592
559881c7bf92 Rename gtkstyle.[ch] to pidginstyle.[ch] and fix up the dark mode check.
Gary Kramlich <grim@reaperworld.com>
parents: 39277
diff changeset
78 dark_mode_have_cache = TRUE;
559881c7bf92 Rename gtkstyle.[ch] to pidginstyle.[ch] and fix up the dark mode check.
Gary Kramlich <grim@reaperworld.com>
parents: 39277
diff changeset
79
559881c7bf92 Rename gtkstyle.[ch] to pidginstyle.[ch] and fix up the dark mode check.
Gary Kramlich <grim@reaperworld.com>
parents: 39277
diff changeset
80 return dark_mode_cached_value;
38699
e7b5011e4728 Namespace the contrast stuff and move it to it's own file/namespace
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
81 }

mercurial