Skip to content

Commit 645c94f

Browse files
committed
More handling for Dynamic Theme errors (Addresses #1211) (#1213)
1 parent 8e7b12b commit 645c94f

File tree

2 files changed

+30
-22
lines changed

2 files changed

+30
-22
lines changed

Daybreak.Shared/Services/Themes/GameScreenshotsTheme.cs

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using Daybreak.Shared.Models.ColorPalette;
22
using Daybreak.Shared.Models.Themes;
33
using Daybreak.Shared.Services.Screenshots;
4+
using Microsoft.Extensions.Logging;
5+
using System.Extensions.Core;
46
using System.Windows.Extensions.Services;
57

68
namespace Daybreak.Shared.Services.Themes;
@@ -11,13 +13,15 @@ namespace Daybreak.Shared.Services.Themes;
1113
/// </summary>
1214
public sealed class GameScreenshotsTheme(
1315
IThemeManager themeManager,
14-
IScreenshotService screenshotService)
16+
IScreenshotService screenshotService,
17+
ILogger<GameScreenshotsTheme> logger)
1518
: Theme(ThemeName, AccentColor.Orange, new StaticBackground(string.Empty), LightDarkMode.SystemSynchronized, string.Empty), IApplicationLifetimeService
1619
{
1720
public const string ThemeName = "Dynamic Screenshots";
1821

1922
private readonly IScreenshotService screenshotService = screenshotService;
2023
private readonly IThemeManager themeManager = themeManager;
24+
private readonly ILogger<GameScreenshotsTheme> logger = logger;
2125

2226
private CancellationTokenSource? cancellationTokenSource;
2327

@@ -27,27 +31,35 @@ private async Task PeriodicallyUpdateGameScreenshots(CancellationToken cancellat
2731
* The theme needs to always cycle through entries, so that when the user selects this theme, it's already initialized.
2832
* We rely on ScreenshotService to cache screenshot entries in memory.
2933
*/
34+
var scopedLogger = this.logger.CreateScopedLogger();
3035
while (!cancellationToken.IsCancellationRequested)
3136
{
32-
var entry = await this.screenshotService.GetRandomScreenshot(cancellationToken);
33-
if (entry is null)
37+
try
3438
{
39+
var entry = await this.screenshotService.GetRandomScreenshot(cancellationToken);
40+
if (entry is null)
41+
{
42+
await Task.Delay(TimeSpan.FromSeconds(30), cancellationToken);
43+
continue;
44+
}
45+
46+
this.AccentColor = entry.AccentColor;
47+
this.Background = new StaticBackground(entry.FilePath);
48+
this.Mode = entry.LightDarkMode;
49+
this.Filter = entry.LightDarkMode is LightDarkMode.Light
50+
? "blur(3px) brightness(1.3) sepia(0.2) saturate(1.2)"
51+
: "blur(2px) brightness(1.2) hue-rotate(10deg) saturate(1.1)";
52+
if (this == this.themeManager.CurrentTheme)
53+
{
54+
this.themeManager.ReapplyTheme();
55+
}
56+
3557
await Task.Delay(TimeSpan.FromSeconds(30), cancellationToken);
36-
continue;
3758
}
38-
39-
this.AccentColor = entry.AccentColor;
40-
this.Background = new StaticBackground(entry.FilePath);
41-
this.Mode = entry.LightDarkMode;
42-
this.Filter = entry.LightDarkMode is LightDarkMode.Light
43-
? "blur(3px) brightness(1.3) sepia(0.2) saturate(1.2)"
44-
: "blur(2px) brightness(1.2) hue-rotate(10deg) saturate(1.1)";
45-
if (this == this.themeManager.CurrentTheme)
59+
catch (Exception ex)
4660
{
47-
this.themeManager.ReapplyTheme();
61+
scopedLogger.LogError(ex, "An error occurred while updating game screenshots theme.");
4862
}
49-
50-
await Task.Delay(TimeSpan.FromSeconds(30), cancellationToken);
5163
}
5264
}
5365

Daybreak/Services/Screenshots/ScreenshotService.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,20 +61,16 @@ public sealed class ScreenshotService(
6161

6262
try
6363
{
64-
using var bitmap = GetBitmap(path);
65-
if (bitmap is null)
66-
{
67-
return default;
68-
}
69-
64+
using var fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
65+
using var bitmap = new Bitmap(fileStream);
7066
var dominantColor = ColorThief.GetColor(bitmap, quality: 2);
7167
var closestAccent = GetClosestAccentColor(Color.FromArgb(dominantColor.Color.A, dominantColor.Color.R, dominantColor.Color.G, dominantColor.Color.B));
7268
var entry = new ScreenshotEntry(path, closestAccent, dominantColor.IsDark ? LightDarkMode.Dark : LightDarkMode.Light);
7369
EntryCache[path] = entry;
7470
scopedLogger.LogDebug("Screenshot entry created and cached for path {path}", path);
7571
return entry;
7672
}
77-
catch(Exception e)
73+
catch (Exception e)
7874
{
7975
scopedLogger.LogError(e, "Failed to get screenshot entry from path {path}", path);
8076
return default;

0 commit comments

Comments
 (0)