Skip to content

Commit

Permalink
Filter screenshots by style and evironment.
Browse files Browse the repository at this point in the history
This use XDG_SESSION_DESKTOP and use either LIGHT or DARK ColorSchemeKind according to gtk_application_prefer_dark_theme to filter screenshots.
  • Loading branch information
italo-capasso committed Dec 18, 2024
1 parent efb5ad2 commit 540356c
Showing 1 changed file with 63 additions and 16 deletions.
79 changes: 63 additions & 16 deletions src/Views/AppInfoView.vala
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,69 @@ public class AppCenter.Views.AppInfoView : Adw.NavigationPage {
}
}

screenshots = package_component.get_screenshots_all ();
bool has_matching_environment = false;
bool has_matching_style = false;
var desktop_environment = Environment.get_variable ("XDG_SESSION_DESKTOP");
var prefer_dark_style = Gtk.Settings.get_default ().gtk_application_prefer_dark_theme;
var desktop_style = prefer_dark_style
? AppStream.ColorSchemeKind.DARK.to_string ()
: AppStream.ColorSchemeKind.LIGHT.to_string ();

package_component.sort_screenshots (desktop_environment,
desktop_style.to_string (),
false);

var all_screenshots = package_component.get_screenshots_all ();

// This first pass is to gather if we have matching style and matching
// desktop environments, this is useful if we need to fall back to any
// screnshot if none of the conditions are fullfiled
all_screenshots.foreach ((screenshot) => {
var environment_id = screenshot.get_environment ();
if (environment_id != null) {
var environment_split = environment_id.split (":", 2);
var screenshot_environment = environment_split[0];
var screenshot_style = environment_split[1]
?? AppStream.ColorSchemeKind.LIGHT.to_string ();

if (screenshot_environment == desktop_environment) {
has_matching_environment = true;
}

if (screenshot_style == desktop_style) {
has_matching_style = true;
}
}
});

screenshots = new GenericArray<AppStream.Screenshot> ();

all_screenshots.foreach ((screenshot) => {
var environment_id = screenshot.get_environment ();
if (environment_id == null) {
screenshots.add (screenshot);
return;
}

var environment_split = environment_id.split (":", 2);
var screenshot_environment = environment_split[0];
var screenshot_style = environment_split[1]
?? AppStream.ColorSchemeKind.LIGHT.to_string ();

var same_environment = screenshot_environment == desktop_environment;
var same_style = screenshot_style == desktop_style;

if (same_environment && same_style) {
screenshots.add (screenshot);
} else if (same_environment && !same_style && !has_matching_style) {
screenshots.add (screenshot);
} else if (!same_environment && same_style && !has_matching_environment) {
screenshots.add (screenshot);
} else if (!has_matching_environment && !has_matching_style) {
screenshots.add (screenshot);
return;
}
});

if (screenshots.length > 0) {
screenshot_carousel = new Adw.Carousel () {
Expand Down Expand Up @@ -942,22 +1004,7 @@ public class AppCenter.Views.AppInfoView : Adw.NavigationPage {
var scale = get_scale_factor ();
var min_screenshot_width = MAX_WIDTH * scale;

var prefer_dark_theme = Gtk.Settings.get_default ().gtk_application_prefer_dark_theme;
screenshots.foreach ((screenshot) => {
var environment_id = screenshot.get_environment ();
if (environment_id != null) {
var environment_split = environment_id.split (":", 2);
if (prefer_dark_theme && environment_split.length != 2) {
return;
}

var color_scheme = AppStream.ColorSchemeKind.from_string (environment_split[1]);
if ((prefer_dark_theme && color_scheme != AppStream.ColorSchemeKind.DARK) ||
(!prefer_dark_theme && color_scheme == AppStream.ColorSchemeKind.DARK)) {
return;
}
}

AppStream.Image? best_image = null;
screenshot.get_images ().foreach ((image) => {
// Image is better than no image
Expand Down

0 comments on commit 540356c

Please sign in to comment.