From dea2a05a68f9b39263aac2858e5fee9b7fcff0c8 Mon Sep 17 00:00:00 2001 From: italo-capasso Date: Mon, 9 Dec 2024 17:00:28 -0500 Subject: [PATCH] Cache all primary colors before asking for one. This ensures that the correct primary color will always get updated when asking for a color even when changing the style mid-app-running. --- src/Core/Package.vala | 58 +++++++++++++++++++++++++++++-------------- 1 file changed, 40 insertions(+), 18 deletions(-) diff --git a/src/Core/Package.vala b/src/Core/Package.vala index 020b7b263..69b79e793 100644 --- a/src/Core/Package.vala +++ b/src/Core/Package.vala @@ -390,7 +390,10 @@ public class AppCenterCore.Package : Object { public string? description = null; private string? summary = null; - private string? color_primary = null; + private string? color_primary_light = null; + private string? color_primary_dark = null; + private string? color_primary_unknown = null; + private string? color_primary_fallback = null; private string? color_primary_text = null; private string? payments_key = null; private string? suggested_amount = null; @@ -418,7 +421,10 @@ public class AppCenterCore.Package : Object { name = null; description = null; summary = null; - color_primary = null; + color_primary_light = null; + color_primary_dark = null; + color_primary_unknown = null; + color_primary_fallback = null; color_primary_text = null; payments_key = null; suggested_amount = null; @@ -749,28 +755,44 @@ public class AppCenterCore.Package : Object { } public string? get_color_primary () { - if (color_primary != null) { - return color_primary; + cache_primary_colors (); + + string? color_primary = null; + var gtk_settings = Gtk.Settings.get_default (); + if (color_primary_light != null && !gtk_settings.gtk_application_prefer_dark_theme) { + color_primary = color_primary_light; + } else if (color_primary_dark != null && gtk_settings.gtk_application_prefer_dark_theme) { + color_primary = color_primary_dark; + } else if (color_primary_unknown != null) { + color_primary = color_primary_unknown; } else { - var branding = component.get_branding (); - if (branding != null) { - var gtk_settings = Gtk.Settings.get_default (); - if (gtk_settings.gtk_application_prefer_dark_theme) { - color_primary = branding.get_color (AppStream.ColorKind.PRIMARY, AppStream.ColorSchemeKind.DARK); - } else { - color_primary = branding.get_color (AppStream.ColorKind.PRIMARY, AppStream.ColorSchemeKind.LIGHT); - } + color_primary = color_primary_fallback; + } - if (color_primary == null) { - color_primary = branding.get_color (AppStream.ColorKind.PRIMARY, AppStream.ColorSchemeKind.UNKNOWN); - } + return color_primary; + } + + private void cache_primary_colors () { + var branding = component.get_branding (); + if (branding != null) { + if (color_primary_dark == null) { + color_primary_dark = branding.get_color (AppStream.ColorKind.PRIMARY, + AppStream.ColorSchemeKind.DARK); + } + + if (color_primary_light == null) { + color_primary_light = branding.get_color (AppStream.ColorKind.PRIMARY, + AppStream.ColorSchemeKind.LIGHT); } - if (color_primary == null) { - color_primary = component.get_custom_value ("x-appcenter-color-primary"); + if (color_primary_unknown == null) { + color_primary_unknown = branding.get_color (AppStream.ColorKind.PRIMARY, + AppStream.ColorSchemeKind.UNKNOWN); } + } - return color_primary; + if (color_primary_fallback == null) { + color_primary_fallback = component.get_custom_value ("x-appcenter-color-primary"); } }