mirror of
https://github.com/Flow-Launcher/Flow.Launcher.git
synced 2026-03-11 08:54:32 +00:00
- Fix Preview in WelcomePage
- Adjust BG Color - Fix aspect ratio wallpaper bitmap
This commit is contained in:
parent
697fb7218e
commit
9ac9ec8497
4 changed files with 72 additions and 45 deletions
|
|
@ -13,7 +13,6 @@ namespace Flow.Launcher.Helper;
|
|||
public static class WallpaperPathRetrieval
|
||||
{
|
||||
private static readonly int MAX_CACHE_SIZE = 3;
|
||||
|
||||
private static readonly Dictionary<(string, DateTime), ImageBrush> wallpaperCache = new();
|
||||
|
||||
public static Brush GetWallpaperBrush()
|
||||
|
|
@ -27,46 +26,73 @@ public static class WallpaperPathRetrieval
|
|||
try
|
||||
{
|
||||
var wallpaperPath = Win32Helper.GetWallpaperPath();
|
||||
if (wallpaperPath is not null && File.Exists(wallpaperPath))
|
||||
if (string.IsNullOrEmpty(wallpaperPath) || !File.Exists(wallpaperPath))
|
||||
{
|
||||
// Since the wallpaper file name can be the same (TranscodedWallpaper),
|
||||
// we need to add the last modified date to differentiate them
|
||||
var dateModified = File.GetLastWriteTime(wallpaperPath);
|
||||
wallpaperCache.TryGetValue((wallpaperPath, dateModified), out var cachedWallpaper);
|
||||
if (cachedWallpaper != null)
|
||||
{
|
||||
return cachedWallpaper;
|
||||
}
|
||||
|
||||
// We should not dispose the memory stream since the bitmap is still in use
|
||||
var memStream = new MemoryStream(File.ReadAllBytes(wallpaperPath));
|
||||
var bitmap = new BitmapImage();
|
||||
bitmap.BeginInit();
|
||||
bitmap.StreamSource = memStream;
|
||||
bitmap.DecodePixelWidth = 800;
|
||||
bitmap.DecodePixelHeight = 600;
|
||||
bitmap.EndInit();
|
||||
bitmap.Freeze(); // Make the bitmap thread-safe
|
||||
var wallpaperBrush = new ImageBrush(bitmap) { Stretch = Stretch.UniformToFill };
|
||||
wallpaperBrush.Freeze(); // Make the brush thread-safe
|
||||
|
||||
// Manage cache size
|
||||
if (wallpaperCache.Count >= MAX_CACHE_SIZE)
|
||||
{
|
||||
// Remove the oldest wallpaper from the cache
|
||||
var oldestCache = wallpaperCache.Keys.OrderBy(k => k.Item2).FirstOrDefault();
|
||||
if (oldestCache != default)
|
||||
{
|
||||
wallpaperCache.Remove(oldestCache);
|
||||
}
|
||||
}
|
||||
|
||||
wallpaperCache.Add((wallpaperPath, dateModified), wallpaperBrush);
|
||||
return wallpaperBrush;
|
||||
App.API.LogInfo(nameof(WallpaperPathRetrieval), $"Wallpaper path is invalid: {wallpaperPath}");
|
||||
var wallpaperColor = GetWallpaperColor();
|
||||
return new SolidColorBrush(wallpaperColor);
|
||||
}
|
||||
|
||||
var wallpaperColor = GetWallpaperColor();
|
||||
return new SolidColorBrush(wallpaperColor);
|
||||
// Since the wallpaper file name can be the same (TranscodedWallpaper),
|
||||
// we need to add the last modified date to differentiate them
|
||||
var dateModified = File.GetLastWriteTime(wallpaperPath);
|
||||
wallpaperCache.TryGetValue((wallpaperPath, dateModified), out var cachedWallpaper);
|
||||
if (cachedWallpaper != null)
|
||||
{
|
||||
App.API.LogInfo(nameof(WallpaperPathRetrieval), "Using cached wallpaper");
|
||||
return cachedWallpaper;
|
||||
}
|
||||
|
||||
// We should not dispose the memory stream since the bitmap is still in use
|
||||
var memStream = new MemoryStream(File.ReadAllBytes(wallpaperPath));
|
||||
var bitmap = new BitmapImage();
|
||||
bitmap.BeginInit();
|
||||
bitmap.StreamSource = memStream;
|
||||
bitmap.EndInit();
|
||||
|
||||
if (bitmap.PixelWidth == 0 || bitmap.PixelHeight == 0)
|
||||
{
|
||||
App.API.LogInfo(nameof(WallpaperPathRetrieval), $"Failed to load bitmap: Width={bitmap.PixelWidth}, Height={bitmap.PixelHeight}");
|
||||
return new SolidColorBrush(Colors.Transparent);
|
||||
}
|
||||
|
||||
var originalWidth = bitmap.PixelWidth;
|
||||
var originalHeight = bitmap.PixelHeight;
|
||||
|
||||
// Calculate the scaling factor to fit the image within 800x600 while preserving aspect ratio
|
||||
double widthRatio = 800.0 / originalWidth;
|
||||
double heightRatio = 600.0 / originalHeight;
|
||||
double scaleFactor = Math.Min(widthRatio, heightRatio);
|
||||
|
||||
int decodedPixelWidth = (int)(originalWidth * scaleFactor);
|
||||
int decodedPixelHeight = (int)(originalHeight * scaleFactor);
|
||||
|
||||
// Set DecodePixelWidth and DecodePixelHeight to resize the image while preserving aspect ratio
|
||||
bitmap = new BitmapImage();
|
||||
bitmap.BeginInit();
|
||||
memStream.Seek(0, SeekOrigin.Begin); // Reset stream position
|
||||
bitmap.StreamSource = memStream;
|
||||
bitmap.DecodePixelWidth = decodedPixelWidth;
|
||||
bitmap.DecodePixelHeight = decodedPixelHeight;
|
||||
bitmap.EndInit();
|
||||
bitmap.Freeze(); // Make the bitmap thread-safe
|
||||
var wallpaperBrush = new ImageBrush(bitmap) { Stretch = Stretch.UniformToFill };
|
||||
wallpaperBrush.Freeze(); // Make the brush thread-safe
|
||||
|
||||
// Manage cache size
|
||||
if (wallpaperCache.Count >= MAX_CACHE_SIZE)
|
||||
{
|
||||
// Remove the oldest wallpaper from the cache
|
||||
var oldestCache = wallpaperCache.Keys.OrderBy(k => k.Item2).FirstOrDefault();
|
||||
if (oldestCache != default)
|
||||
{
|
||||
wallpaperCache.Remove(oldestCache);
|
||||
}
|
||||
}
|
||||
|
||||
wallpaperCache.Add((wallpaperPath, dateModified), wallpaperBrush);
|
||||
return wallpaperBrush;
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
|
@ -86,8 +112,9 @@ public static class WallpaperPathRetrieval
|
|||
var parts = strResult.Trim().Split(new[] { ' ' }, 3).Select(byte.Parse).ToList();
|
||||
return Color.FromRgb(parts[0], parts[1], parts[2]);
|
||||
}
|
||||
catch
|
||||
catch (Exception ex)
|
||||
{
|
||||
App.API.LogException(nameof(WallpaperPathRetrieval), "Error parsing wallpaper color", ex);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -110,8 +110,8 @@
|
|||
<Border.Background>
|
||||
<LinearGradientBrush StartPoint="0 0" EndPoint="1 1">
|
||||
<LinearGradientBrush.GradientStops>
|
||||
<GradientStop Offset="0.0" Color="#1494df" />
|
||||
<GradientStop Offset="1.0" Color="#1073bd" />
|
||||
<GradientStop Offset="0.0" Color="#2A4D8C" />
|
||||
<GradientStop Offset="1.0" Color="#1E3160" />
|
||||
</LinearGradientBrush.GradientStops>
|
||||
</LinearGradientBrush>
|
||||
</Border.Background>
|
||||
|
|
|
|||
|
|
@ -60,7 +60,7 @@
|
|||
HorizontalAlignment="Center"
|
||||
VerticalAlignment="Center"
|
||||
Orientation="Horizontal">
|
||||
<Border Width="450" Style="{DynamicResource WindowBorderStyle}">
|
||||
<Border Width="450" Style="{DynamicResource PreviewWindowBorderStyle}">
|
||||
<Border Style="{DynamicResource WindowRadius}">
|
||||
<Grid>
|
||||
<Grid.RowDefinitions>
|
||||
|
|
|
|||
|
|
@ -58,10 +58,10 @@
|
|||
|
||||
<Border Grid.Row="0" HorizontalAlignment="Stretch">
|
||||
<Border.Background>
|
||||
<LinearGradientBrush StartPoint="0 0" EndPoint="1 1">
|
||||
<LinearGradientBrush StartPoint="0 1" EndPoint="0 0">
|
||||
<LinearGradientBrush.GradientStops>
|
||||
<GradientStop Offset="0.0" Color="#7b83eb" />
|
||||
<GradientStop Offset="1.0" Color="#555dc0" />
|
||||
<GradientStop Offset="0.0" Color="#E5F3F7" />
|
||||
<GradientStop Offset="1.0" Color="#FAFAFD" />
|
||||
</LinearGradientBrush.GradientStops>
|
||||
</LinearGradientBrush>
|
||||
</Border.Background>
|
||||
|
|
|
|||
Loading…
Reference in a new issue