hdy-style-manager.c 24.2 KB
Newer Older
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*
 * Copyright (C) 2021 Purism SPC
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 *
 * Author: Alexander Mikhaylenko <alexander.mikhaylenko@puri.sm>
 */

#include "config.h"
#include <glib/gi18n-lib.h>

#include "hdy-style-manager.h"

#include "hdy-settings-private.h"
#include <gtk/gtk.h>

#define SWITCH_DURATION 250

/**
 * HdyColorScheme:
 * @HDY_COLOR_SCHEME_DEFAULT: Inherit the parent color-scheme. When set on the
22
23
 *   [class@StyleManager] returned by [func@StyleManager.get_default], it's
 *   equivalent to `HDY_COLOR_SCHEME_FORCE_LIGHT`.
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
24
25
26
27
28
29
30
 * @HDY_COLOR_SCHEME_FORCE_LIGHT: Always use light appearance.
 * @HDY_COLOR_SCHEME_PREFER_LIGHT: Use light appearance unless the system
 *   prefers dark colors.
 * @HDY_COLOR_SCHEME_PREFER_DARK: Use dark appearance unless the system prefers
 *   light colors.
 * @HDY_COLOR_SCHEME_FORCE_DARK: Always use dark appearance.
 *
31
 * Application color schemes for [property@StyleManager:color-scheme].
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
32
33
34
35
36
 *
 * Since: 1.6
 */

/**
37
 * HdyStyleManager:
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
38
 *
39
 * A class for managing application-wide styling.
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
40
 *
41
42
 * `HdyStyleManager` provides a way to query and influence the application
 * styles, such as whether to use dark or high contrast appearance.
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
43
 *
44
45
46
47
48
 * It allows to set the color scheme via the
 * [property@StyleManager:color-scheme] property, and to query the current
 * appearance, as well as whether a system-wide color scheme preference exists.
 *
 * Important: [property@Gtk.Settings:gtk-application-prefer-dark-theme] should
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
 * not be used together with `HdyStyleManager` and will result in a warning.
 * Color schemes should be used instead.
 *
 * Since: 1.6
 */

struct _HdyStyleManager
{
  GObject parent_instance;

  GdkDisplay *display;
  HdySettings *settings;

  HdyColorScheme color_scheme;
  gboolean dark;

65
  GtkCssProvider *animations_provider;
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
  guint animation_timeout_id;
};

G_DEFINE_TYPE (HdyStyleManager, hdy_style_manager, G_TYPE_OBJECT);

enum {
  PROP_0,
  PROP_DISPLAY,
  PROP_COLOR_SCHEME,
  PROP_SYSTEM_SUPPORTS_COLOR_SCHEMES,
  PROP_DARK,
  PROP_HIGH_CONTRAST,
  LAST_PROP,
};

static GParamSpec *props[LAST_PROP];

static GHashTable *display_style_managers = NULL;
static HdyStyleManager *default_instance = NULL;

86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/* Copied from gtkcssprovider.c */

static gchar *
get_theme_dir (void)
{
  const gchar *var;

  var = g_getenv ("GTK_DATA_PREFIX");
  if (var == NULL)
    var = PREFIX;

  return g_build_filename (var, "share", "themes", NULL);
}

#if (GTK_MINOR_VERSION % 2)
#define MINOR (GTK_MINOR_VERSION + 1)
#else
#define MINOR GTK_MINOR_VERSION
#endif

static gchar *
find_theme_dir (const gchar *dir,
                const gchar *subdir,
                const gchar *name,
                const gchar *variant)
{
  g_autofree gchar *file = NULL;
  g_autofree gchar *base = NULL;
  gchar *path;
  gint i;

  if (variant)
    file = g_strconcat ("gtk-", variant, ".css", NULL);
  else
    file = g_strdup ("gtk.css");

  if (subdir)
    base = g_build_filename (dir, subdir, name, NULL);
  else
    base = g_build_filename (dir, name, NULL);

  for (i = MINOR; i >= 0; i = i - 2) {
    g_autofree gchar *subsubdir = NULL;

    if (i < 14)
      i = 0;

    subsubdir = g_strdup_printf ("gtk-3.%d", i);
    path = g_build_filename (base, subsubdir, file, NULL);

    if (g_file_test (path, G_FILE_TEST_EXISTS))
      break;

    g_free (path);
    path = NULL;
  }

  return path;
}

#undef MINOR

static gchar *
find_theme (const gchar *name,
            const gchar *variant)
{
  g_autofree gchar *dir = NULL;
  const gchar *const *dirs;
  gchar *path;
  gint i;

  /* First look in the user's data directory */
  path = find_theme_dir (g_get_user_data_dir (), "themes", name, variant);
  if (path)
    return path;

  /* Next look in the user's home directory */
  path = find_theme_dir (g_get_home_dir (), ".themes", name, variant);
  if (path)
    return path;

  /* Look in system data directories */
  dirs = g_get_system_data_dirs ();
  for (i = 0; dirs[i]; i++) {
    path = find_theme_dir (dirs[i], "themes", name, variant);
    if (path)
      return path;
  }

  /* Finally, try in the default theme directory */
  dir = get_theme_dir ();
  path = find_theme_dir (dir, NULL, name, variant);

  return path;
}

static gboolean
check_theme_exists (const gchar *name,
                    const gchar *variant)
{
  g_autofree gchar *resource_path = NULL;
  g_autofree gchar *path = NULL;

  /* try loading the resource for the theme. This is mostly meant for built-in
   * themes.
   */
  if (variant)
    resource_path = g_strdup_printf ("/org/gtk/libgtk/theme/%s/gtk-%s.css", name, variant);
  else
    resource_path = g_strdup_printf ("/org/gtk/libgtk/theme/%s/gtk.css", name);

  if (g_resources_get_info (resource_path, 0, NULL, NULL, NULL))
    return TRUE;

  /* Next try looking for files in the various theme directories. */
  path = find_theme (name, variant);

  return path != NULL;
}

static gchar *
get_system_theme_name (void)
{
  GdkScreen *screen = gdk_screen_get_default ();
  g_auto (GValue) value = G_VALUE_INIT;

  g_value_init (&value, G_TYPE_STRING);
  if (!gdk_screen_get_setting (screen, "gtk-theme-name", &value))
    return g_strdup ("Adwaita");

  return g_value_dup_string (&value);
}

Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
static void
warn_prefer_dark_theme (HdyStyleManager *self)
{
  g_warning ("Using GtkSettings:gtk-application-prefer-dark-theme together "
             "with HdyStyleManager is unsupported. Please use "
             "HdyStyleManager:color-scheme instead.");
}

static void
unregister_display (GdkDisplay *display)
{
  g_assert (!g_hash_table_contains (display_style_managers, display));

  g_hash_table_remove (display_style_managers, display);
}

static void
register_display (GdkDisplayManager *display_manager,
                  GdkDisplay        *display)
{
  HdyStyleManager *style_manager;

  style_manager = g_object_new (HDY_TYPE_STYLE_MANAGER,
                                "display", display,
                                NULL);

  g_assert (!g_hash_table_contains (display_style_managers, display));

  g_hash_table_insert (display_style_managers, display, style_manager);

  g_signal_connect (display,
                    "closed",
                    G_CALLBACK (unregister_display),
                    NULL);
}

static gboolean
enable_animations_cb (HdyStyleManager *self)
{
  GdkScreen *screen = gdk_display_get_default_screen (self->display);

260
261
  gtk_style_context_remove_provider_for_screen (screen,
                                                GTK_STYLE_PROVIDER (self->animations_provider));
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
262
263
264
265
266
267

  self->animation_timeout_id = 0;

  return G_SOURCE_REMOVE;
}

268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
static void update_stylesheet (HdyStyleManager *self);

static gboolean
unblock_theme_name_changed_cb (HdyStyleManager *self)
{
  GdkScreen *screen;
  GtkSettings *gtk_settings;

  screen = gdk_display_get_default_screen (self->display);
  gtk_settings = gtk_settings_get_for_screen (screen);

  g_signal_handlers_unblock_by_func (gtk_settings,
                                     G_CALLBACK (update_stylesheet),
                                     self);

  return G_SOURCE_REMOVE;
}

Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
static void
update_stylesheet (HdyStyleManager *self)
{
  GdkScreen *screen;
  GtkSettings *gtk_settings;

  if (!self->display)
    return;

  screen = gdk_display_get_default_screen (self->display);
  gtk_settings = gtk_settings_get_for_screen (screen);

  g_signal_handlers_block_by_func (gtk_settings,
                                   G_CALLBACK (warn_prefer_dark_theme),
                                   self);
301
302
303
  g_signal_handlers_block_by_func (gtk_settings,
                                   G_CALLBACK (update_stylesheet),
                                   self);
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
304

305
  if (self->animation_timeout_id)
306
307
    g_clear_handle_id (&self->animation_timeout_id, g_source_remove);

308
309
310
  gtk_style_context_add_provider_for_screen (screen,
                                             GTK_STYLE_PROVIDER (self->animations_provider),
                                             10000);
311

Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
312
313
314
315
  g_object_set (gtk_settings,
                "gtk-application-prefer-dark-theme", self->dark,
                NULL);

316
  if (hdy_settings_get_high_contrast (self->settings)) {
317
318
319
320
    g_object_set (gtk_settings,
                  "gtk-theme-name",
                  self->dark ? "HighContrastInverse" : "HighContrast",
                  NULL);
321
322
323
324
325
326
327
328
329
330
331
332
333
  } else {
    g_autofree gchar *theme_name = get_system_theme_name ();
    gboolean override_theme = FALSE;

    if (g_str_has_suffix (theme_name, "-dark")) {
      theme_name[strlen (theme_name) - 5] = '\0';
      override_theme = TRUE;
    }

    if (!check_theme_exists (theme_name, self->dark ? "dark" : NULL))
      g_object_set (gtk_settings, "gtk-theme-name", "Adwaita", NULL);
    else if (override_theme)
      g_object_set (gtk_settings, "gtk-theme-name", theme_name, NULL);
334
335
    else
      gtk_settings_reset_property (gtk_settings, "gtk-theme-name");
336
  }
337

Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
338
339
340
341
  g_signal_handlers_unblock_by_func (gtk_settings,
                                     G_CALLBACK (warn_prefer_dark_theme),
                                     self);

342
343
344
345
  self->animation_timeout_id =
    g_timeout_add (SWITCH_DURATION,
                   G_SOURCE_FUNC (enable_animations_cb),
                   self);
346
347

  g_idle_add (G_SOURCE_FUNC (unblock_theme_name_changed_cb), self);
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
}

static inline gboolean
get_is_dark (HdyStyleManager *self)
{
  HdySystemColorScheme system_scheme = hdy_settings_get_color_scheme (self->settings);

  switch (self->color_scheme) {
  case HDY_COLOR_SCHEME_DEFAULT:
    if (self->display)
      return get_is_dark (default_instance);
    return FALSE;
  case HDY_COLOR_SCHEME_FORCE_LIGHT:
    return FALSE;
  case HDY_COLOR_SCHEME_PREFER_LIGHT:
    return system_scheme == HDY_SYSTEM_COLOR_SCHEME_PREFER_DARK;
  case HDY_COLOR_SCHEME_PREFER_DARK:
    return system_scheme != HDY_SYSTEM_COLOR_SCHEME_PREFER_LIGHT;
  case HDY_COLOR_SCHEME_FORCE_DARK:
    return TRUE;
  default:
    g_assert_not_reached ();
  }
}

static void
update_dark (HdyStyleManager *self)
{
  gboolean dark = get_is_dark (self);

  if (dark == self->dark)
    return;

  self->dark = dark;

  update_stylesheet (self);

  g_object_notify_by_pspec (G_OBJECT (self), props[PROP_DARK]);
}

static void
notify_high_contrast_cb (HdyStyleManager *self)
{
  update_stylesheet (self);

  g_object_notify_by_pspec (G_OBJECT (self), props[PROP_HIGH_CONTRAST]);
}

static void
hdy_style_manager_constructed (GObject *object)
{
  HdyStyleManager *self = HDY_STYLE_MANAGER (object);

  G_OBJECT_CLASS (hdy_style_manager_parent_class)->constructed (object);

  if (self->display) {
    GdkScreen *screen = gdk_display_get_default_screen (self->display);
    GtkSettings *settings = gtk_settings_get_for_screen (screen);
    gboolean prefer_dark_theme;

    g_object_get (settings,
                  "gtk-application-prefer-dark-theme", &prefer_dark_theme,
                  NULL);

    if (prefer_dark_theme)
      warn_prefer_dark_theme (self);

    g_signal_connect_object (settings,
                             "notify::gtk-application-prefer-dark-theme",
                             G_CALLBACK (warn_prefer_dark_theme),
                             self,
                             G_CONNECT_SWAPPED);
420
421
422
423
424
    g_signal_connect_object (settings,
                             "notify::gtk-theme-name",
                             G_CALLBACK (update_stylesheet),
                             self,
                             G_CONNECT_SWAPPED);
425
426
427
428
429
430

    self->animations_provider = gtk_css_provider_new ();
    gtk_css_provider_load_from_data (self->animations_provider,
                                     "* { transition: none; }",
                                     -1,
                                     NULL);
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
  }

  self->settings = hdy_settings_get_default ();

  g_signal_connect_object (self->settings,
                           "notify::color-scheme",
                           G_CALLBACK (update_dark),
                           self,
                           G_CONNECT_SWAPPED);
  g_signal_connect_object (self->settings,
                           "notify::high-contrast",
                           G_CALLBACK (notify_high_contrast_cb),
                           self,
                           G_CONNECT_SWAPPED);

  update_dark (self);
  update_stylesheet (self);
}

static void
hdy_style_manager_dispose (GObject *object)
{
  HdyStyleManager *self = HDY_STYLE_MANAGER (object);

  g_clear_handle_id (&self->animation_timeout_id, g_source_remove);
456
  g_clear_object (&self->animations_provider);
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527

  G_OBJECT_CLASS (hdy_style_manager_parent_class)->dispose (object);
}

static void
hdy_style_manager_get_property (GObject    *object,
                                guint       prop_id,
                                GValue     *value,
                                GParamSpec *pspec)
{
  HdyStyleManager *self = HDY_STYLE_MANAGER (object);

  switch (prop_id) {
  case PROP_DISPLAY:
    g_value_set_object (value, hdy_style_manager_get_display (self));
    break;

  case PROP_COLOR_SCHEME:
    g_value_set_enum (value, hdy_style_manager_get_color_scheme (self));
    break;

  case PROP_SYSTEM_SUPPORTS_COLOR_SCHEMES:
    g_value_set_boolean (value, hdy_style_manager_get_system_supports_color_schemes (self));
    break;

  case PROP_DARK:
    g_value_set_boolean (value, hdy_style_manager_get_dark (self));
    break;

    case PROP_HIGH_CONTRAST:
    g_value_set_boolean (value, hdy_style_manager_get_high_contrast (self));
    break;

  default:
    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
  }
}

static void
hdy_style_manager_set_property (GObject      *object,
                                guint         prop_id,
                                const GValue *value,
                                GParamSpec   *pspec)
{
  HdyStyleManager *self = HDY_STYLE_MANAGER (object);

  switch (prop_id) {
  case PROP_DISPLAY:
    self->display = g_value_get_object (value);
    break;

  case PROP_COLOR_SCHEME:
    hdy_style_manager_set_color_scheme (self, g_value_get_enum (value));
    break;

  default:
    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
  }
}

static void
hdy_style_manager_class_init (HdyStyleManagerClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->constructed = hdy_style_manager_constructed;
  object_class->dispose = hdy_style_manager_dispose;
  object_class->get_property = hdy_style_manager_get_property;
  object_class->set_property = hdy_style_manager_set_property;

  /**
528
   * HdyStyleManager:display: (attributes org.gtk.Property.get=hdy_style_manager_get_display)
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
529
530
531
   *
   * The display the style manager is associated with.
   *
532
533
   * The display will be `NULL` for the style manager returned by
   * [func@StyleManager.get_default].
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
534
535
536
537
538
539
540
541
542
543
544
   *
   * Since: 1.6
   */
  props[PROP_DISPLAY] =
    g_param_spec_object ("display",
                         "Display",
                         "Display",
                         GDK_TYPE_DISPLAY,
                         G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);

  /**
545
   * HdyStyleManager:color-scheme: (attributes org.gtk.Property.get=hdy_style_manager_get_color_scheme org.gtk.Property.set=hdy_style_manager_set_color_scheme)
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
546
547
548
549
   *
   * The requested application color scheme.
   *
   * The effective appearance will be decided based on the application color
550
551
552
   * scheme and the system preferred color scheme. The
   * [property@StyleManager:dark] property can be used to query the current
   * effective appearance.
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
553
   *
554
   * The `HDY_COLOR_SCHEME_PREFER_LIGHT` color scheme results in the application
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
555
556
557
   * using light appearance unless the system prefers dark colors. This is the
   * default value.
   *
558
   * The `HDY_COLOR_SCHEME_PREFER_DARK` color scheme results in the application
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
559
560
561
562
   * using dark appearance, but can still switch to the light appearance if the
   * system can prefers it, for example, when the high contrast preference is
   * enabled.
   *
563
   * The `HDY_COLOR_SCHEME_FORCE_LIGHT` and `HDY_COLOR_SCHEME_FORCE_DARK` values
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
564
565
566
567
   * ignore the system preference entirely, they are useful if the application
   * wants to match its UI to its content or to provide a separate color scheme
   * switcher.
   *
568
569
   * If a per-[class@Gdk.Display] style manager has its color scheme set to
   * `HDY_COLOR_SCHEME_DEFAULT`, it will inherit the color scheme from the
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
570
571
   * default style manager.
   *
572
573
   * For the default style manager, `HDY_COLOR_SCHEME_DEFAULT` is equivalent to
   * `HDY_COLOR_SCHEME_FORCE_LIGHT`.
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
574
   *
575
576
   * The [property@StyleManager:system-supports-color-schemes] property can be
   * used to check if the current environment provides a color scheme
577
   * preference.
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
578
579
580
581
582
583
584
585
586
587
588
589
   *
   * Since: 1.6
   */
  props[PROP_COLOR_SCHEME] =
    g_param_spec_enum ("color-scheme",
                       _("Color Scheme"),
                       _("The current color scheme"),
                       HDY_TYPE_COLOR_SCHEME,
                       HDY_COLOR_SCHEME_DEFAULT,
                       G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY);

  /**
590
   * HdyStyleManager:system-supports-color-schemes: (attributes org.gtk.Property.get=hdy_style_manager_get_system_supports_color_schemes)
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
591
592
593
594
595
   *
   * Whether the system supports color schemes.
   *
   * This property can be used to check if the current environment provides a
   * color scheme preference. For example, applications might want to show a
596
   * separate appearance switcher if it's set to `FALSE`.
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
597
598
599
   *
   * It's only set at startup and cannot change its value later.
   *
600
   * See [property@StyleManager:color-scheme].
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
601
602
603
604
605
606
607
608
609
610
611
   *
   * Since: 1.6
   */
  props[PROP_SYSTEM_SUPPORTS_COLOR_SCHEMES] =
    g_param_spec_boolean ("system-supports-color-schemes",
                          _("System supports color schemes"),
                          _("Whether the system supports color schemes"),
                          FALSE,
                          G_PARAM_READABLE);

  /**
612
   * HdyStyleManager:dark: (attributes org.gtk.Property.get=hdy_style_manager_get_dark)
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
613
614
615
616
   *
   * Whether the application is using dark appearance.
   *
   * This property can be used to query the current appearance, as requested via
617
   * [property@StyleManager:color-scheme].
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
618
619
620
621
622
623
624
625
626
627
628
   *
   * Since: 1.6
   */
  props[PROP_DARK] =
    g_param_spec_boolean ("dark",
                          _("Dark"),
                          _("Whether the application is using dark appearance"),
                          FALSE,
                          G_PARAM_READABLE);

  /**
629
   * HdyStyleManager:high-contrast: (attributes org.gtk.Property.get=hdy_style_manager_get_high_contrast)
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
   *
   * Whether the application is using high contrast appearance.
   *
   * This cannot be overridden by applications.
   *
   * Since: 1.6
   */
  props[PROP_HIGH_CONTRAST] =
    g_param_spec_boolean ("high-contrast",
                          _("High Contrast"),
                          _("Whether the application is using high contrast appearance"),
                          FALSE,
                          G_PARAM_READABLE);

  g_object_class_install_properties (object_class, LAST_PROP, props);
}

static void
hdy_style_manager_init (HdyStyleManager *self)
{
  self->color_scheme = HDY_COLOR_SCHEME_DEFAULT;
}

static void
hdy_style_manager_ensure (void)
{
  GdkDisplayManager *display_manager = gdk_display_manager_get ();
  g_autoptr (GSList) displays = NULL;
  GSList *l;

  if (display_style_managers)
    return;

  default_instance = g_object_new (HDY_TYPE_STYLE_MANAGER, NULL);
  display_style_managers = g_hash_table_new_full (g_direct_hash,
                                                  g_direct_equal,
                                                  NULL,
                                                  g_object_unref);

  displays = gdk_display_manager_list_displays (display_manager);

  for (l = displays; l; l = l->next)
    register_display (display_manager, l->data);

  g_signal_connect (display_manager,
                    "display-opened",
                    G_CALLBACK (register_display),
                    NULL);
}

/**
 * hdy_style_manager_get_default:
 *
683
 * Gets the default [class@StyleManager] instance.
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
684
 *
685
686
 * It manages all [class@Gdk.Display] instances unless the style manager for
 * that display has an override.
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
687
 *
688
 * See [func@StyleManager.get_for_display].
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
 *
 * Returns: (transfer none): the default style manager
 *
 * Since: 1.6
 */
HdyStyleManager *
hdy_style_manager_get_default (void)
{
  if (!default_instance)
    hdy_style_manager_ensure ();

  return default_instance;
}

/**
 * hdy_style_manager_get_for_display:
705
 * @display: a display
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
706
 *
707
 * Gets the [class@StyleManager] instance managing @display.
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
708
709
710
711
 *
 * It can be used to override styles for that specific display instead of the
 * whole application.
 *
712
 * Most applications should use [func@StyleManager.get_default] instead.
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
 *
 * Returns: (transfer none): the style manager for @display
 *
 * Since: 1.6
 */
HdyStyleManager *
hdy_style_manager_get_for_display (GdkDisplay *display)
{
  g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);

  if (!display_style_managers)
    hdy_style_manager_ensure ();

  g_return_val_if_fail (g_hash_table_contains (display_style_managers, display), NULL);

  return g_hash_table_lookup (display_style_managers, display);
}

/**
732
733
 * hdy_style_manager_get_display: (attributes org.gtk.Method.get_property=display)
 * @self: a style manager
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
734
735
736
 *
 * Gets the display the style manager is associated with.
 *
737
738
 * The display will be `NULL` for the style manager returned by
 * [func@StyleManager.get_default].
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
739
740
741
742
743
744
745
746
747
748
749
750
751
752
 *
 * Returns: (transfer none): (nullable): the display
 *
 * Since: 1.6
 */
GdkDisplay *
hdy_style_manager_get_display (HdyStyleManager *self)
{
  g_return_val_if_fail (HDY_IS_STYLE_MANAGER (self), NULL);

  return self->display;
}

/**
753
754
 * hdy_style_manager_get_color_scheme: (attributes org.gtk.Method.get_property=color-scheme)
 * @self: a style manager
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
 *
 * Gets the requested application color scheme.
 *
 * Returns: the color scheme
 *
 * Since: 1.6
 */
HdyColorScheme
hdy_style_manager_get_color_scheme (HdyStyleManager *self)
{
  g_return_val_if_fail (HDY_IS_STYLE_MANAGER (self), HDY_COLOR_SCHEME_DEFAULT);

  return self->color_scheme;
}

/**
771
772
 * hdy_style_manager_set_color_scheme: (attributes org.gtk.Method.set_property=color-scheme)
 * @self: a style manager
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
773
774
775
776
777
 * @color_scheme: the color scheme
 *
 * Sets the requested application color scheme.
 *
 * The effective appearance will be decided based on the application color
778
779
780
 * scheme and the system preferred color scheme. The
 * [property@StyleManager:dark] property can be used to query the current
 * effective appearance.
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
 *
 * Since: 1.6
 */
void
hdy_style_manager_set_color_scheme (HdyStyleManager *self,
                                    HdyColorScheme   color_scheme)
{
  g_return_if_fail (HDY_IS_STYLE_MANAGER (self));

  if (color_scheme == self->color_scheme)
    return;

  self->color_scheme = color_scheme;

  g_object_freeze_notify (G_OBJECT (self));

  update_dark (self);

  g_object_notify_by_pspec (G_OBJECT (self), props[PROP_COLOR_SCHEME]);

  g_object_thaw_notify (G_OBJECT (self));

  if (!self->display) {
    GHashTableIter iter;
    HdyStyleManager *manager;

    g_hash_table_iter_init (&iter, display_style_managers);

    while (g_hash_table_iter_next (&iter, NULL, (gpointer) &manager))
      if (manager->color_scheme == HDY_COLOR_SCHEME_DEFAULT)
        update_dark (manager);
  }
}

/**
816
817
 * hdy_style_manager_get_system_supports_color_schemes: (attributes org.gtk.Method.get_property=system-supports-color-schemes)
 * @self: a style manager
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
 *
 * Gets whether the system supports color schemes.
 *
 * Returns: whether the system supports color schemes
 *
 * Since: 1.6
 */
gboolean
hdy_style_manager_get_system_supports_color_schemes (HdyStyleManager *self)
{
  g_return_val_if_fail (HDY_IS_STYLE_MANAGER (self), FALSE);

  return hdy_settings_has_color_scheme (self->settings);
}

/**
834
835
 * hdy_style_manager_get_dark: (attributes org.gtk.Method.get_property=dark)
 * @self: a style manager
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
 *
 * Gets whether the application is using dark appearance.
 *
 * Returns: whether the application is using dark appearance
 *
 * Since: 1.6
 */
gboolean
hdy_style_manager_get_high_contrast (HdyStyleManager *self)
{
  g_return_val_if_fail (HDY_IS_STYLE_MANAGER (self), FALSE);

  return hdy_settings_get_high_contrast (self->settings);
}

/**
852
853
 * hdy_style_manager_get_high_contrast: (attributes org.gtk.Method.get_property=high-contrast)
 * @self: a style manager
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
854
855
856
857
858
859
860
861
862
863
864
865
866
867
 *
 * Gets whether the application is using high contrast appearance.
 *
 * Returns: whether the application is using high contrast appearance
 *
 * Since: 1.6
 */
gboolean
hdy_style_manager_get_dark (HdyStyleManager *self)
{
  g_return_val_if_fail (HDY_IS_STYLE_MANAGER (self), FALSE);

  return self->dark;
}