Skip to content

Commit

Permalink
Cache all primary colors before asking for one.
Browse files Browse the repository at this point in the history
This ensures that the correct primary color will always get updated when asking for a color even when changing the style mid-app-running.
  • Loading branch information
italo-capasso committed Dec 9, 2024
1 parent bee3d53 commit dea2a05
Showing 1 changed file with 40 additions and 18 deletions.
58 changes: 40 additions & 18 deletions src/Core/Package.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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");
}
}

Expand Down

0 comments on commit dea2a05

Please sign in to comment.