mirror of
https://github.com/Flow-Launcher/Flow.Launcher.git
synced 2026-03-11 08:54:32 +00:00
port image
This commit is contained in:
parent
d788de8b90
commit
dc4e1c4b75
11 changed files with 177 additions and 151 deletions
|
|
@ -48,6 +48,7 @@
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Avalonia" Version="11.0.7" />
|
||||||
<PackageReference Include="Ben.Demystifier" Version="0.4.1" />
|
<PackageReference Include="Ben.Demystifier" Version="0.4.1" />
|
||||||
<PackageReference Include="Fody" Version="6.5.5">
|
<PackageReference Include="Fody" Version="6.5.5">
|
||||||
<PrivateAssets>all</PrivateAssets>
|
<PrivateAssets>all</PrivateAssets>
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Windows.Media;
|
using System.Windows.Media;
|
||||||
|
using Avalonia.Media.Imaging;
|
||||||
|
|
||||||
namespace Flow.Launcher.Infrastructure.Image
|
namespace Flow.Launcher.Infrastructure.Image
|
||||||
{
|
{
|
||||||
|
|
@ -12,9 +13,9 @@ namespace Flow.Launcher.Infrastructure.Image
|
||||||
{
|
{
|
||||||
|
|
||||||
public int usage;
|
public int usage;
|
||||||
public ImageSource imageSource;
|
public Bitmap imageSource;
|
||||||
|
|
||||||
public ImageUsage(int usage, ImageSource image)
|
public ImageUsage(int usage, Bitmap image)
|
||||||
{
|
{
|
||||||
this.usage = usage;
|
this.usage = usage;
|
||||||
imageSource = image;
|
imageSource = image;
|
||||||
|
|
@ -36,7 +37,7 @@ namespace Flow.Launcher.Infrastructure.Image
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public ImageSource this[string path, bool isFullImage = false]
|
public Bitmap this[string path, bool isFullImage = false]
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
|
|
@ -86,7 +87,7 @@ namespace Flow.Launcher.Infrastructure.Image
|
||||||
return key is not null && Data.ContainsKey((key, isFullImage)) && Data[(key, isFullImage)].imageSource != null;
|
return key is not null && Data.ContainsKey((key, isFullImage)) && Data[(key, isFullImage)].imageSource != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool TryGetValue(string key, bool isFullImage, out ImageSource image)
|
public bool TryGetValue(string key, bool isFullImage, out Bitmap image)
|
||||||
{
|
{
|
||||||
if (key is not null)
|
if (key is not null)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
49
Flow.Launcher.Infrastructure/Image/ImageHelper.cs
Normal file
49
Flow.Launcher.Infrastructure/Image/ImageHelper.cs
Normal file
|
|
@ -0,0 +1,49 @@
|
||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
using System.Net.Http;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Avalonia.Media.Imaging;
|
||||||
|
using Avalonia.Platform;
|
||||||
|
using Flow.Launcher.Infrastructure.Logger;
|
||||||
|
|
||||||
|
namespace Flow.Launcher.Infrastructure.Image
|
||||||
|
{
|
||||||
|
public static class ImageHelper
|
||||||
|
{
|
||||||
|
public static Bitmap LoadFromResource(Uri resourceUri)
|
||||||
|
{
|
||||||
|
return new Bitmap(AssetLoader.Open(resourceUri));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async Task<Bitmap?> LoadFromFile(string path, int? width)
|
||||||
|
{
|
||||||
|
if (width is null)
|
||||||
|
{
|
||||||
|
return new Bitmap(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
await using var stream = File.OpenRead(path);
|
||||||
|
MemoryStream memoryStream = new MemoryStream();
|
||||||
|
await stream.CopyToAsync(memoryStream);
|
||||||
|
memoryStream.Position = 0;
|
||||||
|
return Bitmap.DecodeToWidth(memoryStream, width.Value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async Task<Bitmap?> LoadFromWeb(Uri url)
|
||||||
|
{
|
||||||
|
using var httpClient = new HttpClient();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var response = await httpClient.GetAsync(url);
|
||||||
|
response.EnsureSuccessStatusCode();
|
||||||
|
var data = await response.Content.ReadAsByteArrayAsync();
|
||||||
|
return new Bitmap(new MemoryStream(data));
|
||||||
|
}
|
||||||
|
catch (HttpRequestException ex)
|
||||||
|
{
|
||||||
|
Log.Error($"An error occurred while downloading image '{url}' : {ex.Message}");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -7,6 +7,7 @@ using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System.Windows.Media;
|
using System.Windows.Media;
|
||||||
using System.Windows.Media.Imaging;
|
using System.Windows.Media.Imaging;
|
||||||
|
using Avalonia.Media.Imaging;
|
||||||
using Flow.Launcher.Infrastructure.Logger;
|
using Flow.Launcher.Infrastructure.Logger;
|
||||||
using Flow.Launcher.Infrastructure.Storage;
|
using Flow.Launcher.Infrastructure.Storage;
|
||||||
using static Flow.Launcher.Infrastructure.Http.Http;
|
using static Flow.Launcher.Infrastructure.Http.Http;
|
||||||
|
|
@ -21,8 +22,8 @@ namespace Flow.Launcher.Infrastructure.Image
|
||||||
private static readonly ConcurrentDictionary<string, string> GuidToKey = new();
|
private static readonly ConcurrentDictionary<string, string> GuidToKey = new();
|
||||||
private static IImageHashGenerator _hashGenerator;
|
private static IImageHashGenerator _hashGenerator;
|
||||||
private static readonly bool EnableImageHash = true;
|
private static readonly bool EnableImageHash = true;
|
||||||
public static ImageSource MissingImage { get; } = new BitmapImage(new Uri(Constant.MissingImgIcon));
|
public static Bitmap MissingImage { get; } = new Bitmap(Constant.MissingImgIcon);
|
||||||
public static ImageSource LoadingImage { get; } = new BitmapImage(new Uri(Constant.LoadingImgIcon));
|
public static Bitmap LoadingImage { get; } = new Bitmap(Constant.LoadingImgIcon);
|
||||||
public const int SmallIconSize = 64;
|
public const int SmallIconSize = 64;
|
||||||
public const int FullIconSize = 256;
|
public const int FullIconSize = 256;
|
||||||
|
|
||||||
|
|
@ -40,8 +41,7 @@ namespace Flow.Launcher.Infrastructure.Image
|
||||||
|
|
||||||
foreach (var icon in new[] { Constant.DefaultIcon, Constant.MissingImgIcon })
|
foreach (var icon in new[] { Constant.DefaultIcon, Constant.MissingImgIcon })
|
||||||
{
|
{
|
||||||
ImageSource img = new BitmapImage(new Uri(icon));
|
var img = new Bitmap(icon);
|
||||||
img.Freeze();
|
|
||||||
ImageCache[icon, false] = img;
|
ImageCache[icon, false] = img;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -93,14 +93,14 @@ namespace Flow.Launcher.Infrastructure.Image
|
||||||
|
|
||||||
private class ImageResult
|
private class ImageResult
|
||||||
{
|
{
|
||||||
public ImageResult(ImageSource imageSource, ImageType imageType)
|
public ImageResult(Bitmap image, ImageType imageType)
|
||||||
{
|
{
|
||||||
ImageSource = imageSource;
|
Image = image;
|
||||||
ImageType = imageType;
|
ImageType = imageType;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ImageType ImageType { get; }
|
public ImageType ImageType { get; }
|
||||||
public ImageSource ImageSource { get; }
|
public Bitmap Image { get; }
|
||||||
}
|
}
|
||||||
|
|
||||||
private enum ImageType
|
private enum ImageType
|
||||||
|
|
@ -138,28 +138,28 @@ namespace Flow.Launcher.Infrastructure.Image
|
||||||
return new ImageResult(image, ImageType.ImageFile);
|
return new ImageResult(image, ImageType.ImageFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (path.StartsWith("data:", StringComparison.OrdinalIgnoreCase))
|
// if (path.StartsWith("data:", StringComparison.OrdinalIgnoreCase))
|
||||||
{
|
// {
|
||||||
var imageSource = new BitmapImage(new Uri(path));
|
// var imageSource = new BitmapImage(new Uri(path));
|
||||||
imageSource.Freeze();
|
// imageSource.Freeze();
|
||||||
return new ImageResult(imageSource, ImageType.Data);
|
// return new ImageResult(imageSource, ImageType.Data);
|
||||||
}
|
// }
|
||||||
|
|
||||||
imageResult = await Task.Run(() => GetThumbnailResult(ref path, loadFullImage));
|
imageResult = await Task.Run(async () => await GetThumbnailResult(path, loadFullImage));
|
||||||
}
|
}
|
||||||
catch (System.Exception e)
|
catch (System.Exception e)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
// Get thumbnail may fail for certain images on the first try, retry again has proven to work
|
// Get thumbnail may fail for certain images on the first try, retry again has proven to work
|
||||||
imageResult = GetThumbnailResult(ref path, loadFullImage);
|
imageResult = await GetThumbnailResult(path, loadFullImage);
|
||||||
}
|
}
|
||||||
catch (System.Exception e2)
|
catch (System.Exception e2)
|
||||||
{
|
{
|
||||||
Log.Exception($"|ImageLoader.Load|Failed to get thumbnail for {path} on first try", e);
|
Log.Exception($"|ImageLoader.Load|Failed to get thumbnail for {path} on first try", e);
|
||||||
Log.Exception($"|ImageLoader.Load|Failed to get thumbnail for {path} on second try", e2);
|
Log.Exception($"|ImageLoader.Load|Failed to get thumbnail for {path} on second try", e2);
|
||||||
|
|
||||||
ImageSource image = ImageCache[Constant.MissingImgIcon, false];
|
var image = ImageCache[Constant.MissingImgIcon, false];
|
||||||
ImageCache[path, false] = image;
|
ImageCache[path, false] = image;
|
||||||
imageResult = new ImageResult(image, ImageType.Error);
|
imageResult = new ImageResult(image, ImageType.Error);
|
||||||
}
|
}
|
||||||
|
|
@ -168,32 +168,29 @@ namespace Flow.Launcher.Infrastructure.Image
|
||||||
return imageResult;
|
return imageResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static async Task<BitmapImage> LoadRemoteImageAsync(bool loadFullImage, Uri uriResult)
|
private static async Task<Bitmap> LoadRemoteImageAsync(bool loadFullImage, Uri uriResult)
|
||||||
{
|
{
|
||||||
// Download image from url
|
// Download image from url
|
||||||
await using var resp = await GetStreamAsync(uriResult);
|
await using var resp = await GetStreamAsync(uriResult);
|
||||||
await using var buffer = new MemoryStream();
|
await using var buffer = new MemoryStream();
|
||||||
await resp.CopyToAsync(buffer);
|
await resp.CopyToAsync(buffer);
|
||||||
buffer.Seek(0, SeekOrigin.Begin);
|
buffer.Seek(0, SeekOrigin.Begin);
|
||||||
var image = new BitmapImage();
|
Bitmap image;
|
||||||
image.BeginInit();
|
|
||||||
image.CacheOption = BitmapCacheOption.OnLoad;
|
|
||||||
if (!loadFullImage)
|
if (!loadFullImage)
|
||||||
{
|
{
|
||||||
image.DecodePixelHeight = SmallIconSize;
|
image = Bitmap.DecodeToWidth(buffer, SmallIconSize);
|
||||||
image.DecodePixelWidth = SmallIconSize;
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
image = new Bitmap(buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
image.StreamSource = buffer;
|
|
||||||
image.EndInit();
|
|
||||||
image.StreamSource = null;
|
|
||||||
image.Freeze();
|
|
||||||
return image;
|
return image;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static ImageResult GetThumbnailResult(ref string path, bool loadFullImage = false)
|
private static async ValueTask<ImageResult> GetThumbnailResult(string path, bool loadFullImage = false)
|
||||||
{
|
{
|
||||||
ImageSource image;
|
Bitmap image;
|
||||||
ImageType type = ImageType.Error;
|
ImageType type = ImageType.Error;
|
||||||
|
|
||||||
if (Directory.Exists(path))
|
if (Directory.Exists(path))
|
||||||
|
|
@ -214,7 +211,7 @@ namespace Flow.Launcher.Infrastructure.Image
|
||||||
type = ImageType.ImageFile;
|
type = ImageType.ImageFile;
|
||||||
if (loadFullImage)
|
if (loadFullImage)
|
||||||
{
|
{
|
||||||
image = LoadFullImage(path);
|
image = new Bitmap(path);
|
||||||
type = ImageType.FullImageFile;
|
type = ImageType.FullImageFile;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|
@ -224,7 +221,7 @@ namespace Flow.Launcher.Infrastructure.Image
|
||||||
* be the case in many situations while testing.
|
* be the case in many situations while testing.
|
||||||
* - Solution: explicitly pass the ThumbnailOnly flag
|
* - Solution: explicitly pass the ThumbnailOnly flag
|
||||||
*/
|
*/
|
||||||
image = GetThumbnail(path, ThumbnailOptions.ThumbnailOnly);
|
image = await ImageHelper.LoadFromFile(path, SmallIconSize);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|
@ -239,15 +236,11 @@ namespace Flow.Launcher.Infrastructure.Image
|
||||||
path = Constant.MissingImgIcon;
|
path = Constant.MissingImgIcon;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (type != ImageType.Error)
|
|
||||||
{
|
|
||||||
image.Freeze();
|
|
||||||
}
|
|
||||||
|
|
||||||
return new ImageResult(image, type);
|
return new ImageResult(image, type);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static BitmapSource GetThumbnail(string path, ThumbnailOptions option = ThumbnailOptions.ThumbnailOnly,
|
private static Bitmap GetThumbnail(string path, ThumbnailOptions option = ThumbnailOptions.ThumbnailOnly,
|
||||||
int size = SmallIconSize)
|
int size = SmallIconSize)
|
||||||
{
|
{
|
||||||
return WindowsThumbnailProvider.GetThumbnail(
|
return WindowsThumbnailProvider.GetThumbnail(
|
||||||
|
|
@ -262,34 +255,34 @@ namespace Flow.Launcher.Infrastructure.Image
|
||||||
return ImageCache.ContainsKey(path, loadFullImage);
|
return ImageCache.ContainsKey(path, loadFullImage);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static bool TryGetValue(string path, bool loadFullImage, out ImageSource image)
|
public static bool TryGetValue(string path, bool loadFullImage, out Bitmap image)
|
||||||
{
|
{
|
||||||
return ImageCache.TryGetValue(path, loadFullImage, out image);
|
return ImageCache.TryGetValue(path, loadFullImage, out image);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async ValueTask<ImageSource> LoadAsync(string path, bool loadFullImage = false)
|
public static async ValueTask<Bitmap> LoadAsync(string path, bool loadFullImage = false)
|
||||||
{
|
{
|
||||||
var imageResult = await LoadInternalAsync(path, loadFullImage);
|
var imageResult = await LoadInternalAsync(path, loadFullImage);
|
||||||
|
|
||||||
var img = imageResult.ImageSource;
|
var img = imageResult.Image;
|
||||||
if (imageResult.ImageType != ImageType.Error && imageResult.ImageType != ImageType.Cache)
|
if (imageResult.ImageType != ImageType.Error && imageResult.ImageType != ImageType.Cache)
|
||||||
{
|
{
|
||||||
// we need to get image hash
|
// we need to get image hash
|
||||||
string hash = EnableImageHash ? _hashGenerator.GetHashFromImage(img) : null;
|
// string hash = EnableImageHash ? _hashGenerator.GetHashFromImage(img) : null;
|
||||||
if (hash != null)
|
// if (hash != null)
|
||||||
{
|
// {
|
||||||
if (GuidToKey.TryGetValue(hash, out string key))
|
// if (GuidToKey.TryGetValue(hash, out string key))
|
||||||
{
|
// {
|
||||||
// image already exists
|
// // image already exists
|
||||||
img = ImageCache[key, loadFullImage] ?? img;
|
// img = ImageCache[key, loadFullImage] ?? img;
|
||||||
}
|
// }
|
||||||
else
|
// else
|
||||||
{
|
// {
|
||||||
// new guid
|
// // new guid
|
||||||
|
//
|
||||||
GuidToKey[hash] = path;
|
// GuidToKey[hash] = path;
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
// update cache
|
// update cache
|
||||||
ImageCache[path, loadFullImage] = img;
|
ImageCache[path, loadFullImage] = img;
|
||||||
|
|
@ -297,42 +290,5 @@ namespace Flow.Launcher.Infrastructure.Image
|
||||||
|
|
||||||
return img;
|
return img;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static BitmapImage LoadFullImage(string path)
|
|
||||||
{
|
|
||||||
BitmapImage image = new BitmapImage();
|
|
||||||
image.BeginInit();
|
|
||||||
image.CacheOption = BitmapCacheOption.OnLoad;
|
|
||||||
image.UriSource = new Uri(path);
|
|
||||||
image.CreateOptions = BitmapCreateOptions.IgnoreColorProfile;
|
|
||||||
image.EndInit();
|
|
||||||
|
|
||||||
if (image.PixelWidth > 320)
|
|
||||||
{
|
|
||||||
BitmapImage resizedWidth = new BitmapImage();
|
|
||||||
resizedWidth.BeginInit();
|
|
||||||
resizedWidth.CacheOption = BitmapCacheOption.OnLoad;
|
|
||||||
resizedWidth.UriSource = new Uri(path);
|
|
||||||
resizedWidth.CreateOptions = BitmapCreateOptions.IgnoreColorProfile;
|
|
||||||
resizedWidth.DecodePixelWidth = 320;
|
|
||||||
resizedWidth.EndInit();
|
|
||||||
|
|
||||||
if (resizedWidth.PixelHeight > 320)
|
|
||||||
{
|
|
||||||
BitmapImage resizedHeight = new BitmapImage();
|
|
||||||
resizedHeight.BeginInit();
|
|
||||||
resizedHeight.CacheOption = BitmapCacheOption.OnLoad;
|
|
||||||
resizedHeight.UriSource = new Uri(path);
|
|
||||||
resizedHeight.CreateOptions = BitmapCreateOptions.IgnoreColorProfile;
|
|
||||||
resizedHeight.DecodePixelHeight = 320;
|
|
||||||
resizedHeight.EndInit();
|
|
||||||
return resizedHeight;
|
|
||||||
}
|
|
||||||
|
|
||||||
return resizedWidth;
|
|
||||||
}
|
|
||||||
|
|
||||||
return image;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,12 @@
|
||||||
using System;
|
using System;
|
||||||
|
using System.Drawing;
|
||||||
|
using System.Drawing.Imaging;
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Windows.Interop;
|
using System.Windows.Interop;
|
||||||
using System.Windows.Media.Imaging;
|
using System.Windows.Media.Imaging;
|
||||||
using System.Windows;
|
using System.Windows;
|
||||||
|
using Bitmap = Avalonia.Media.Imaging.Bitmap;
|
||||||
|
|
||||||
namespace Flow.Launcher.Infrastructure.Image
|
namespace Flow.Launcher.Infrastructure.Image
|
||||||
{
|
{
|
||||||
|
|
@ -41,8 +44,8 @@ namespace Flow.Launcher.Infrastructure.Image
|
||||||
internal interface IShellItem
|
internal interface IShellItem
|
||||||
{
|
{
|
||||||
void BindToHandler(IntPtr pbc,
|
void BindToHandler(IntPtr pbc,
|
||||||
[MarshalAs(UnmanagedType.LPStruct)]Guid bhid,
|
[MarshalAs(UnmanagedType.LPStruct)] Guid bhid,
|
||||||
[MarshalAs(UnmanagedType.LPStruct)]Guid riid,
|
[MarshalAs(UnmanagedType.LPStruct)] Guid riid,
|
||||||
out IntPtr ppv);
|
out IntPtr ppv);
|
||||||
|
|
||||||
void GetParent(out IShellItem ppsi);
|
void GetParent(out IShellItem ppsi);
|
||||||
|
|
@ -88,9 +91,9 @@ namespace Flow.Launcher.Infrastructure.Image
|
||||||
{
|
{
|
||||||
[PreserveSig]
|
[PreserveSig]
|
||||||
HResult GetImage(
|
HResult GetImage(
|
||||||
[In, MarshalAs(UnmanagedType.Struct)] NativeSize size,
|
[In, MarshalAs(UnmanagedType.Struct)] NativeSize size,
|
||||||
[In] ThumbnailOptions flags,
|
[In] ThumbnailOptions flags,
|
||||||
[Out] out IntPtr phbm);
|
[Out] out IntPtr phbm);
|
||||||
}
|
}
|
||||||
|
|
||||||
[StructLayout(LayoutKind.Sequential)]
|
[StructLayout(LayoutKind.Sequential)]
|
||||||
|
|
@ -104,21 +107,34 @@ namespace Flow.Launcher.Infrastructure.Image
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
public static BitmapSource GetThumbnail(string fileName, int width, int height, ThumbnailOptions options)
|
public static Bitmap GetThumbnail(string fileName, int width, int height, ThumbnailOptions options)
|
||||||
{
|
{
|
||||||
IntPtr hBitmap = GetHBitmap(Path.GetFullPath(fileName), width, height, options);
|
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
return Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
|
var icon = Icon.ExtractAssociatedIcon(fileName);
|
||||||
|
if (icon == null)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
var bitmapTmp = icon.ToBitmap();
|
||||||
|
var bitmapdata = bitmapTmp.LockBits(new Rectangle(0, 0, bitmapTmp.Width, bitmapTmp.Height),
|
||||||
|
ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
|
||||||
|
var bitmap1 = new Bitmap(Avalonia.Platform.PixelFormat.Bgra8888, Avalonia.Platform.AlphaFormat.Unpremul,
|
||||||
|
bitmapdata.Scan0,
|
||||||
|
new Avalonia.PixelSize(bitmapdata.Width, bitmapdata.Height),
|
||||||
|
new Avalonia.Vector(96, 96),
|
||||||
|
bitmapdata.Stride);
|
||||||
|
bitmapTmp.UnlockBits(bitmapdata);
|
||||||
|
bitmapTmp.Dispose();
|
||||||
|
return bitmap1;
|
||||||
}
|
}
|
||||||
finally
|
catch (System.Exception e)
|
||||||
{
|
{
|
||||||
// delete HBitmap to avoid memory leaks
|
return null;
|
||||||
DeleteObject(hBitmap);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static IntPtr GetHBitmap(string fileName, int width, int height, ThumbnailOptions options)
|
private static IntPtr GetHBitmap(string fileName, int width, int height, ThumbnailOptions options)
|
||||||
{
|
{
|
||||||
IShellItem nativeShellItem;
|
IShellItem nativeShellItem;
|
||||||
|
|
@ -128,11 +144,7 @@ namespace Flow.Launcher.Infrastructure.Image
|
||||||
if (retCode != 0)
|
if (retCode != 0)
|
||||||
throw Marshal.GetExceptionForHR(retCode);
|
throw Marshal.GetExceptionForHR(retCode);
|
||||||
|
|
||||||
NativeSize nativeSize = new NativeSize
|
NativeSize nativeSize = new NativeSize { Width = width, Height = height };
|
||||||
{
|
|
||||||
Width = width,
|
|
||||||
Height = height
|
|
||||||
};
|
|
||||||
|
|
||||||
IntPtr hBitmap;
|
IntPtr hBitmap;
|
||||||
HResult hr = ((IShellItemImageFactory)nativeShellItem).GetImage(nativeSize, options, out hBitmap);
|
HResult hr = ((IShellItemImageFactory)nativeShellItem).GetImage(nativeSize, options, out hBitmap);
|
||||||
|
|
@ -140,14 +152,16 @@ namespace Flow.Launcher.Infrastructure.Image
|
||||||
// if extracting image thumbnail and failed, extract shell icon
|
// if extracting image thumbnail and failed, extract shell icon
|
||||||
if (options == ThumbnailOptions.ThumbnailOnly && hr == HResult.ExtractionFailed)
|
if (options == ThumbnailOptions.ThumbnailOnly && hr == HResult.ExtractionFailed)
|
||||||
{
|
{
|
||||||
hr = ((IShellItemImageFactory) nativeShellItem).GetImage(nativeSize, ThumbnailOptions.IconOnly, out hBitmap);
|
hr = ((IShellItemImageFactory)nativeShellItem).GetImage(nativeSize, ThumbnailOptions.IconOnly,
|
||||||
|
out hBitmap);
|
||||||
}
|
}
|
||||||
|
|
||||||
Marshal.ReleaseComObject(nativeShellItem);
|
Marshal.ReleaseComObject(nativeShellItem);
|
||||||
|
|
||||||
if (hr == HResult.Ok) return hBitmap;
|
if (hr == HResult.Ok) return hBitmap;
|
||||||
|
|
||||||
throw new COMException($"Error while extracting thumbnail for {fileName}", Marshal.GetExceptionForHR((int)hr));
|
throw new COMException($"Error while extracting thumbnail for {fileName}",
|
||||||
|
Marshal.GetExceptionForHR((int)hr));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -29,22 +29,24 @@ namespace Flow.Launcher
|
||||||
|
|
||||||
// Create the fade out storyboard
|
// Create the fade out storyboard
|
||||||
fadeOutStoryboard.Completed += fadeOutStoryboard_Completed;
|
fadeOutStoryboard.Completed += fadeOutStoryboard_Completed;
|
||||||
DoubleAnimation fadeOutAnimation = new DoubleAnimation(dipWorkingArea.Y - Height, dipWorkingArea.Y, new Duration(TimeSpan.FromSeconds(5)))
|
DoubleAnimation fadeOutAnimation =
|
||||||
{
|
new DoubleAnimation(dipWorkingArea.Y - Height, dipWorkingArea.Y, new Duration(TimeSpan.FromSeconds(5)))
|
||||||
AccelerationRatio = 0.2
|
{
|
||||||
};
|
AccelerationRatio = 0.2
|
||||||
|
};
|
||||||
Storyboard.SetTarget(fadeOutAnimation, this);
|
Storyboard.SetTarget(fadeOutAnimation, this);
|
||||||
Storyboard.SetTargetProperty(fadeOutAnimation, new PropertyPath(TopProperty));
|
Storyboard.SetTargetProperty(fadeOutAnimation, new PropertyPath(TopProperty));
|
||||||
fadeOutStoryboard.Children.Add(fadeOutAnimation);
|
fadeOutStoryboard.Children.Add(fadeOutAnimation);
|
||||||
|
|
||||||
_ = LoadImageAsync();
|
_ = LoadImageAsync();
|
||||||
|
|
||||||
imgClose.MouseUp += imgClose_MouseUp;
|
imgClose.MouseUp += imgClose_MouseUp;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async System.Threading.Tasks.Task LoadImageAsync()
|
private async System.Threading.Tasks.Task LoadImageAsync()
|
||||||
{
|
{
|
||||||
imgClose.Source = await ImageLoader.LoadAsync(Path.Combine(Infrastructure.Constant.ProgramDirectory, "Images\\close.png"));
|
imgClose.Source =
|
||||||
|
default; //await ImageLoader.LoadAsync(Path.Combine(Infrastructure.Constant.ProgramDirectory, "Images\\close.png"));
|
||||||
}
|
}
|
||||||
|
|
||||||
void imgClose_MouseUp(object sender, MouseButtonEventArgs e)
|
void imgClose_MouseUp(object sender, MouseButtonEventArgs e)
|
||||||
|
|
@ -69,26 +71,26 @@ namespace Flow.Launcher
|
||||||
{
|
{
|
||||||
tbSubTitle.Visibility = Visibility.Collapsed;
|
tbSubTitle.Visibility = Visibility.Collapsed;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!File.Exists(iconPath))
|
if (!File.Exists(iconPath))
|
||||||
{
|
{
|
||||||
imgIco.Source = await ImageLoader.LoadAsync(Path.Combine(Constant.ProgramDirectory, "Images\\app.png"));
|
// imgIco.Source = await ImageLoader.LoadAsync(Path.Combine(Constant.ProgramDirectory, "Images\\app.png"));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
imgIco.Source = await ImageLoader.LoadAsync(iconPath);
|
// imgIco.Source = await ImageLoader.LoadAsync(iconPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
Show();
|
Show();
|
||||||
|
|
||||||
await Dispatcher.InvokeAsync(async () =>
|
await Dispatcher.InvokeAsync(async () =>
|
||||||
{
|
{
|
||||||
if (!closing)
|
if (!closing)
|
||||||
{
|
{
|
||||||
closing = true;
|
closing = true;
|
||||||
await Dispatcher.InvokeAsync(fadeOutStoryboard.Begin);
|
await Dispatcher.InvokeAsync(fadeOutStoryboard.Begin);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -12,12 +12,12 @@
|
||||||
SelectedIndex="{Binding SelectedIndex, Mode=TwoWay}"
|
SelectedIndex="{Binding SelectedIndex, Mode=TwoWay}"
|
||||||
SelectedItem="{Binding SelectedItem, Mode=TwoWay}"
|
SelectedItem="{Binding SelectedItem, Mode=TwoWay}"
|
||||||
SelectionMode="AlwaysSelected"
|
SelectionMode="AlwaysSelected"
|
||||||
AutoScrollToSelectedItem="True"
|
AutoScrollToSelectedItem="False"
|
||||||
IsVisible="{Binding Visibility}"
|
IsVisible="{Binding Visibility}"
|
||||||
MaxHeight="{Binding MaxHeight}">
|
MaxHeight="{Binding MaxHeight}">
|
||||||
<ListBox.ItemTemplate>
|
<ListBox.ItemTemplate>
|
||||||
<DataTemplate>
|
<DataTemplate>
|
||||||
<Button HorizontalAlignment="Stretch" Height="52">
|
<Button HorizontalAlignment="Stretch" Height="40">
|
||||||
<Button.Template>
|
<Button.Template>
|
||||||
<ControlTemplate>
|
<ControlTemplate>
|
||||||
<ContentPresenter Content="{TemplateBinding Button.Content}" />
|
<ContentPresenter Content="{TemplateBinding Button.Content}" />
|
||||||
|
|
|
||||||
|
|
@ -29,15 +29,15 @@ namespace Flow.Launcher.ViewModel
|
||||||
|
|
||||||
private async void LoadIconAsync()
|
private async void LoadIconAsync()
|
||||||
{
|
{
|
||||||
Image = await ImageLoader.LoadAsync(PluginPair.Metadata.IcoPath);
|
Image = null; // await ImageLoader.LoadAsync(PluginPair.Metadata.IcoPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ImageSource Image
|
public ImageSource Image
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
if (_image == ImageLoader.MissingImage)
|
// if (_image == ImageLoader.MissingImage)
|
||||||
LoadIconAsync();
|
// LoadIconAsync();
|
||||||
|
|
||||||
return _image;
|
return _image;
|
||||||
}
|
}
|
||||||
|
|
@ -69,7 +69,8 @@ namespace Flow.Launcher.ViewModel
|
||||||
? new Control()
|
? new Control()
|
||||||
: settingProvider.CreateSettingPanel()
|
: settingProvider.CreateSettingPanel()
|
||||||
: null;
|
: null;
|
||||||
private ImageSource _image = ImageLoader.MissingImage;
|
|
||||||
|
private ImageSource _image = default;// ImageLoader.MissingImage;
|
||||||
|
|
||||||
public Visibility ActionKeywordsVisibility => PluginPair.Metadata.ActionKeywords.Count == 1 ? Visibility.Visible : Visibility.Collapsed;
|
public Visibility ActionKeywordsVisibility => PluginPair.Metadata.ActionKeywords.Count == 1 ? Visibility.Visible : Visibility.Collapsed;
|
||||||
public string InitilizaTime => PluginPair.Metadata.InitTime + "ms";
|
public string InitilizaTime => PluginPair.Metadata.InitTime + "ms";
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,7 @@ using Flow.Launcher.Plugin;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Drawing.Text;
|
using System.Drawing.Text;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using Avalonia.Media.Imaging;
|
||||||
|
|
||||||
namespace Flow.Launcher.ViewModel
|
namespace Flow.Launcher.ViewModel
|
||||||
{
|
{
|
||||||
|
|
@ -146,10 +147,10 @@ namespace Flow.Launcher.ViewModel
|
||||||
private volatile bool ImageLoaded;
|
private volatile bool ImageLoaded;
|
||||||
private volatile bool PreviewImageLoaded;
|
private volatile bool PreviewImageLoaded;
|
||||||
|
|
||||||
private ImageSource image = ImageLoader.LoadingImage;
|
private Bitmap image = default; //ImageLoader.LoadingImage;
|
||||||
private ImageSource previewImage = ImageLoader.LoadingImage;
|
private Bitmap previewImage = default; //ImageLoader.LoadingImage;
|
||||||
|
|
||||||
public ImageSource Image
|
public Bitmap Image
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
|
|
@ -164,7 +165,7 @@ namespace Flow.Launcher.ViewModel
|
||||||
private set => image = value;
|
private set => image = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ImageSource PreviewImage
|
public Bitmap PreviewImage
|
||||||
{
|
{
|
||||||
get => previewImage;
|
get => previewImage;
|
||||||
private set => previewImage = value;
|
private set => previewImage = value;
|
||||||
|
|
@ -177,15 +178,15 @@ namespace Flow.Launcher.ViewModel
|
||||||
|
|
||||||
public GlyphInfo Glyph { get; set; }
|
public GlyphInfo Glyph { get; set; }
|
||||||
|
|
||||||
private async Task<ImageSource> LoadImageInternalAsync(string imagePath, Result.IconDelegate icon,
|
private async Task<Bitmap> LoadImageInternalAsync(string imagePath, Result.IconDelegate icon,
|
||||||
bool loadFullImage)
|
bool loadFullImage)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(imagePath) && icon != null)
|
if (string.IsNullOrEmpty(imagePath) && icon != null)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var image = icon();
|
// var image = icon();
|
||||||
return image;
|
return default; // image;
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
|
|
@ -195,14 +196,14 @@ namespace Flow.Launcher.ViewModel
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return await ImageLoader.LoadAsync(imagePath, loadFullImage).ConfigureAwait(false);
|
return default; // await ImageLoader.LoadAsync(imagePath, loadFullImage).ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task LoadImageAsync()
|
private async Task LoadImageAsync()
|
||||||
{
|
{
|
||||||
var imagePath = Result.IcoPath;
|
var imagePath = Result.IcoPath;
|
||||||
var iconDelegate = Result.Icon;
|
var iconDelegate = Result.Icon;
|
||||||
if (ImageLoader.TryGetValue(imagePath, false, out ImageSource img))
|
if (ImageLoader.TryGetValue(imagePath, false, out var img))
|
||||||
{
|
{
|
||||||
image = img;
|
image = img;
|
||||||
}
|
}
|
||||||
|
|
@ -217,7 +218,7 @@ namespace Flow.Launcher.ViewModel
|
||||||
{
|
{
|
||||||
var imagePath = Result.Preview.PreviewImagePath ?? Result.IcoPath;
|
var imagePath = Result.Preview.PreviewImagePath ?? Result.IcoPath;
|
||||||
var iconDelegate = Result.Preview.PreviewDelegate ?? Result.Icon;
|
var iconDelegate = Result.Preview.PreviewDelegate ?? Result.Icon;
|
||||||
if (ImageLoader.TryGetValue(imagePath, true, out ImageSource img))
|
if (ImageLoader.TryGetValue(imagePath, true, out var img))
|
||||||
{
|
{
|
||||||
previewImage = img;
|
previewImage = img;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -42,7 +42,7 @@ namespace Flow.Launcher.Plugin.WebSearch
|
||||||
|
|
||||||
_viewModel.SetupCustomImagesDirectory();
|
_viewModel.SetupCustomImagesDirectory();
|
||||||
|
|
||||||
imgPreviewIcon.Source = await _viewModel.LoadPreviewIconAsync(_searchSource.IconPath);
|
// imgPreviewIcon.Source = await _viewModel.LoadPreviewIconAsync(_searchSource.IconPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnCancelButtonClick(object sender, RoutedEventArgs e)
|
private void OnCancelButtonClick(object sender, RoutedEventArgs e)
|
||||||
|
|
@ -140,7 +140,7 @@ namespace Flow.Launcher.Plugin.WebSearch
|
||||||
if (_viewModel.ShouldProvideHint(selectedNewIconImageFullPath))
|
if (_viewModel.ShouldProvideHint(selectedNewIconImageFullPath))
|
||||||
MessageBox.Show(_api.GetTranslation("flowlauncher_plugin_websearch_iconpath_hint"));
|
MessageBox.Show(_api.GetTranslation("flowlauncher_plugin_websearch_iconpath_hint"));
|
||||||
|
|
||||||
imgPreviewIcon.Source = await _viewModel.LoadPreviewIconAsync(selectedNewIconImageFullPath);
|
// imgPreviewIcon.Source = await _viewModel.LoadPreviewIconAsync(selectedNewIconImageFullPath);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ using System.Threading.Tasks;
|
||||||
using System.Windows;
|
using System.Windows;
|
||||||
#pragma warning restore IDE0005
|
#pragma warning restore IDE0005
|
||||||
using System.Windows.Media;
|
using System.Windows.Media;
|
||||||
|
using Avalonia.Media.Imaging;
|
||||||
|
|
||||||
namespace Flow.Launcher.Plugin.WebSearch
|
namespace Flow.Launcher.Plugin.WebSearch
|
||||||
{
|
{
|
||||||
|
|
@ -59,7 +60,7 @@ namespace Flow.Launcher.Plugin.WebSearch
|
||||||
return Directory.GetParent(fullPathToSelectedImage).ToString() == Main.DefaultImagesDirectory;
|
return Directory.GetParent(fullPathToSelectedImage).ToString() == Main.DefaultImagesDirectory;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal async ValueTask<ImageSource> LoadPreviewIconAsync(string pathToPreviewIconImage)
|
internal async ValueTask<Bitmap> LoadPreviewIconAsync(string pathToPreviewIconImage)
|
||||||
{
|
{
|
||||||
return await ImageLoader.LoadAsync(pathToPreviewIconImage);
|
return await ImageLoader.LoadAsync(pathToPreviewIconImage);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue