Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 27 additions & 15 deletions Daybreak.Shared/Services/Themes/GameScreenshotsTheme.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Daybreak.Shared.Models.ColorPalette;
using Daybreak.Shared.Models.Themes;
using Daybreak.Shared.Services.Screenshots;
using Microsoft.Extensions.Logging;
using System.Extensions.Core;
using System.Windows.Extensions.Services;

namespace Daybreak.Shared.Services.Themes;
Expand All @@ -11,13 +13,15 @@ namespace Daybreak.Shared.Services.Themes;
/// </summary>
public sealed class GameScreenshotsTheme(
IThemeManager themeManager,
IScreenshotService screenshotService)
IScreenshotService screenshotService,
ILogger<GameScreenshotsTheme> logger)
: Theme(ThemeName, AccentColor.Orange, new StaticBackground(string.Empty), LightDarkMode.SystemSynchronized, string.Empty), IApplicationLifetimeService
{
public const string ThemeName = "Dynamic Screenshots";

private readonly IScreenshotService screenshotService = screenshotService;
private readonly IThemeManager themeManager = themeManager;
private readonly ILogger<GameScreenshotsTheme> logger = logger;

private CancellationTokenSource? cancellationTokenSource;

Expand All @@ -27,27 +31,35 @@ private async Task PeriodicallyUpdateGameScreenshots(CancellationToken cancellat
* The theme needs to always cycle through entries, so that when the user selects this theme, it's already initialized.
* We rely on ScreenshotService to cache screenshot entries in memory.
*/
var scopedLogger = this.logger.CreateScopedLogger();
while (!cancellationToken.IsCancellationRequested)
{
var entry = await this.screenshotService.GetRandomScreenshot(cancellationToken);
if (entry is null)
try
{
var entry = await this.screenshotService.GetRandomScreenshot(cancellationToken);
if (entry is null)
{
await Task.Delay(TimeSpan.FromSeconds(30), cancellationToken);
continue;
}

this.AccentColor = entry.AccentColor;
this.Background = new StaticBackground(entry.FilePath);
this.Mode = entry.LightDarkMode;
this.Filter = entry.LightDarkMode is LightDarkMode.Light
? "blur(3px) brightness(1.3) sepia(0.2) saturate(1.2)"
: "blur(2px) brightness(1.2) hue-rotate(10deg) saturate(1.1)";
if (this == this.themeManager.CurrentTheme)
{
this.themeManager.ReapplyTheme();
}

await Task.Delay(TimeSpan.FromSeconds(30), cancellationToken);
continue;
}

this.AccentColor = entry.AccentColor;
this.Background = new StaticBackground(entry.FilePath);
this.Mode = entry.LightDarkMode;
this.Filter = entry.LightDarkMode is LightDarkMode.Light
? "blur(3px) brightness(1.3) sepia(0.2) saturate(1.2)"
: "blur(2px) brightness(1.2) hue-rotate(10deg) saturate(1.1)";
if (this == this.themeManager.CurrentTheme)
catch (Exception ex)
{
this.themeManager.ReapplyTheme();
scopedLogger.LogError(ex, "An error occurred while updating game screenshots theme.");
}

await Task.Delay(TimeSpan.FromSeconds(30), cancellationToken);
}
}

Expand Down
10 changes: 3 additions & 7 deletions Daybreak/Services/Screenshots/ScreenshotService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,16 @@ public sealed class ScreenshotService(

try
{
using var bitmap = GetBitmap(path);
if (bitmap is null)
{
return default;
}

using var fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
using var bitmap = new Bitmap(fileStream);
var dominantColor = ColorThief.GetColor(bitmap, quality: 2);
var closestAccent = GetClosestAccentColor(Color.FromArgb(dominantColor.Color.A, dominantColor.Color.R, dominantColor.Color.G, dominantColor.Color.B));
var entry = new ScreenshotEntry(path, closestAccent, dominantColor.IsDark ? LightDarkMode.Dark : LightDarkMode.Light);
EntryCache[path] = entry;
scopedLogger.LogDebug("Screenshot entry created and cached for path {path}", path);
return entry;
}
catch(Exception e)
catch (Exception e)
{
scopedLogger.LogError(e, "Failed to get screenshot entry from path {path}", path);
return default;
Expand Down
Loading