Skip to content

Commit 9ac9ec8

Browse files
committed
- Fix Preview in WelcomePage
- Adjust BG Color - Fix aspect ratio wallpaper bitmap
1 parent 697fb72 commit 9ac9ec8

File tree

4 files changed

+69
-42
lines changed

4 files changed

+69
-42
lines changed

Flow.Launcher/Helper/WallpaperPathRetrieval.cs

Lines changed: 63 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ namespace Flow.Launcher.Helper;
1313
public static class WallpaperPathRetrieval
1414
{
1515
private static readonly int MAX_CACHE_SIZE = 3;
16-
1716
private static readonly Dictionary<(string, DateTime), ImageBrush> wallpaperCache = new();
1817

1918
public static Brush GetWallpaperBrush()
@@ -27,46 +26,73 @@ public static Brush GetWallpaperBrush()
2726
try
2827
{
2928
var wallpaperPath = Win32Helper.GetWallpaperPath();
30-
if (wallpaperPath is not null && File.Exists(wallpaperPath))
29+
if (string.IsNullOrEmpty(wallpaperPath) || !File.Exists(wallpaperPath))
3130
{
32-
// Since the wallpaper file name can be the same (TranscodedWallpaper),
33-
// we need to add the last modified date to differentiate them
34-
var dateModified = File.GetLastWriteTime(wallpaperPath);
35-
wallpaperCache.TryGetValue((wallpaperPath, dateModified), out var cachedWallpaper);
36-
if (cachedWallpaper != null)
37-
{
38-
return cachedWallpaper;
39-
}
31+
App.API.LogInfo(nameof(WallpaperPathRetrieval), $"Wallpaper path is invalid: {wallpaperPath}");
32+
var wallpaperColor = GetWallpaperColor();
33+
return new SolidColorBrush(wallpaperColor);
34+
}
35+
36+
// Since the wallpaper file name can be the same (TranscodedWallpaper),
37+
// we need to add the last modified date to differentiate them
38+
var dateModified = File.GetLastWriteTime(wallpaperPath);
39+
wallpaperCache.TryGetValue((wallpaperPath, dateModified), out var cachedWallpaper);
40+
if (cachedWallpaper != null)
41+
{
42+
App.API.LogInfo(nameof(WallpaperPathRetrieval), "Using cached wallpaper");
43+
return cachedWallpaper;
44+
}
45+
46+
// We should not dispose the memory stream since the bitmap is still in use
47+
var memStream = new MemoryStream(File.ReadAllBytes(wallpaperPath));
48+
var bitmap = new BitmapImage();
49+
bitmap.BeginInit();
50+
bitmap.StreamSource = memStream;
51+
bitmap.EndInit();
52+
53+
if (bitmap.PixelWidth == 0 || bitmap.PixelHeight == 0)
54+
{
55+
App.API.LogInfo(nameof(WallpaperPathRetrieval), $"Failed to load bitmap: Width={bitmap.PixelWidth}, Height={bitmap.PixelHeight}");
56+
return new SolidColorBrush(Colors.Transparent);
57+
}
58+
59+
var originalWidth = bitmap.PixelWidth;
60+
var originalHeight = bitmap.PixelHeight;
61+
62+
// Calculate the scaling factor to fit the image within 800x600 while preserving aspect ratio
63+
double widthRatio = 800.0 / originalWidth;
64+
double heightRatio = 600.0 / originalHeight;
65+
double scaleFactor = Math.Min(widthRatio, heightRatio);
66+
67+
int decodedPixelWidth = (int)(originalWidth * scaleFactor);
68+
int decodedPixelHeight = (int)(originalHeight * scaleFactor);
4069

41-
// We should not dispose the memory stream since the bitmap is still in use
42-
var memStream = new MemoryStream(File.ReadAllBytes(wallpaperPath));
43-
var bitmap = new BitmapImage();
44-
bitmap.BeginInit();
45-
bitmap.StreamSource = memStream;
46-
bitmap.DecodePixelWidth = 800;
47-
bitmap.DecodePixelHeight = 600;
48-
bitmap.EndInit();
49-
bitmap.Freeze(); // Make the bitmap thread-safe
50-
var wallpaperBrush = new ImageBrush(bitmap) { Stretch = Stretch.UniformToFill };
51-
wallpaperBrush.Freeze(); // Make the brush thread-safe
52-
53-
// Manage cache size
54-
if (wallpaperCache.Count >= MAX_CACHE_SIZE)
70+
// Set DecodePixelWidth and DecodePixelHeight to resize the image while preserving aspect ratio
71+
bitmap = new BitmapImage();
72+
bitmap.BeginInit();
73+
memStream.Seek(0, SeekOrigin.Begin); // Reset stream position
74+
bitmap.StreamSource = memStream;
75+
bitmap.DecodePixelWidth = decodedPixelWidth;
76+
bitmap.DecodePixelHeight = decodedPixelHeight;
77+
bitmap.EndInit();
78+
bitmap.Freeze(); // Make the bitmap thread-safe
79+
var wallpaperBrush = new ImageBrush(bitmap) { Stretch = Stretch.UniformToFill };
80+
wallpaperBrush.Freeze(); // Make the brush thread-safe
81+
82+
// Manage cache size
83+
if (wallpaperCache.Count >= MAX_CACHE_SIZE)
84+
{
85+
// Remove the oldest wallpaper from the cache
86+
var oldestCache = wallpaperCache.Keys.OrderBy(k => k.Item2).FirstOrDefault();
87+
if (oldestCache != default)
5588
{
56-
// Remove the oldest wallpaper from the cache
57-
var oldestCache = wallpaperCache.Keys.OrderBy(k => k.Item2).FirstOrDefault();
58-
if (oldestCache != default)
59-
{
60-
wallpaperCache.Remove(oldestCache);
61-
}
89+
wallpaperCache.Remove(oldestCache);
6290
}
63-
64-
wallpaperCache.Add((wallpaperPath, dateModified), wallpaperBrush);
65-
return wallpaperBrush;
6691
}
6792

68-
var wallpaperColor = GetWallpaperColor();
69-
return new SolidColorBrush(wallpaperColor);
93+
wallpaperCache.Add((wallpaperPath, dateModified), wallpaperBrush);
94+
return wallpaperBrush;
95+
7096
}
7197
catch (Exception ex)
7298
{
@@ -86,8 +112,9 @@ private static Color GetWallpaperColor()
86112
var parts = strResult.Trim().Split(new[] { ' ' }, 3).Select(byte.Parse).ToList();
87113
return Color.FromRgb(parts[0], parts[1], parts[2]);
88114
}
89-
catch
115+
catch (Exception ex)
90116
{
117+
App.API.LogException(nameof(WallpaperPathRetrieval), "Error parsing wallpaper color", ex);
91118
}
92119
}
93120

Flow.Launcher/Resources/Pages/WelcomePage1.xaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@
110110
<Border.Background>
111111
<LinearGradientBrush StartPoint="0 0" EndPoint="1 1">
112112
<LinearGradientBrush.GradientStops>
113-
<GradientStop Offset="0.0" Color="#1494df" />
114-
<GradientStop Offset="1.0" Color="#1073bd" />
113+
<GradientStop Offset="0.0" Color="#2A4D8C" />
114+
<GradientStop Offset="1.0" Color="#1E3160" />
115115
</LinearGradientBrush.GradientStops>
116116
</LinearGradientBrush>
117117
</Border.Background>

Flow.Launcher/Resources/Pages/WelcomePage2.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
HorizontalAlignment="Center"
6161
VerticalAlignment="Center"
6262
Orientation="Horizontal">
63-
<Border Width="450" Style="{DynamicResource WindowBorderStyle}">
63+
<Border Width="450" Style="{DynamicResource PreviewWindowBorderStyle}">
6464
<Border Style="{DynamicResource WindowRadius}">
6565
<Grid>
6666
<Grid.RowDefinitions>

Flow.Launcher/Resources/Pages/WelcomePage5.xaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@
5858

5959
<Border Grid.Row="0" HorizontalAlignment="Stretch">
6060
<Border.Background>
61-
<LinearGradientBrush StartPoint="0 0" EndPoint="1 1">
61+
<LinearGradientBrush StartPoint="0 1" EndPoint="0 0">
6262
<LinearGradientBrush.GradientStops>
63-
<GradientStop Offset="0.0" Color="#7b83eb" />
64-
<GradientStop Offset="1.0" Color="#555dc0" />
63+
<GradientStop Offset="0.0" Color="#E5F3F7" />
64+
<GradientStop Offset="1.0" Color="#FAFAFD" />
6565
</LinearGradientBrush.GradientStops>
6666
</LinearGradientBrush>
6767
</Border.Background>

0 commit comments

Comments
 (0)