Skip to content

Commit

Permalink
Search result thumbnail for PowerLauncher (#2124)
Browse files Browse the repository at this point in the history
* Replace WPF Imaging library with UWP

* Removed UWP and WPF namespace conflicting files from Wox

* Removed Image hashing as it wasn't used anywhere

* Updated formatting
  • Loading branch information
dsrivastavv authored Apr 16, 2020
1 parent 37a551e commit 367cb41
Show file tree
Hide file tree
Showing 16 changed files with 43 additions and 421 deletions.
66 changes: 3 additions & 63 deletions src/modules/launcher/Plugins/Wox.Plugin.Program/Programs/UWP.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
using System.Security.Principal;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Xml.Linq;
using Windows.ApplicationModel;
using Windows.Management.Deployment;
Expand All @@ -18,6 +16,8 @@
using Wox.Plugin.Program.Logger;
using IStream = AppxPackaing.IStream;
using Rect = System.Windows.Rect;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml.Media;

namespace Wox.Plugin.Program.Programs
{
Expand Down Expand Up @@ -517,11 +517,7 @@ internal string LogoPathFromUri(string uri)
public ImageSource Logo()
{
var logo = ImageFromPath(LogoPath);
var plated = PlatedImage(logo);

// todo magic! temp fix for cross thread object
plated.Freeze();
return plated;
return logo;
}


Expand All @@ -541,62 +537,6 @@ private BitmapImage ImageFromPath(string path)
}
}

private ImageSource PlatedImage(BitmapImage image)
{
if (!string.IsNullOrEmpty(BackgroundColor) && BackgroundColor != "transparent")
{
var width = image.Width;
var height = image.Height;
var x = 0;
var y = 0;

var group = new DrawingGroup();

var converted = ColorConverter.ConvertFromString(BackgroundColor);
if (converted != null)
{
var color = (Color)converted;
var brush = new SolidColorBrush(color);
var pen = new Pen(brush, 1);
var backgroundArea = new Rect(0, 0, width, width);
var rectabgle = new RectangleGeometry(backgroundArea);
var rectDrawing = new GeometryDrawing(brush, pen, rectabgle);
group.Children.Add(rectDrawing);

var imageArea = new Rect(x, y, image.Width, image.Height);
var imageDrawing = new ImageDrawing(image, imageArea);
group.Children.Add(imageDrawing);

// http://stackoverflow.com/questions/6676072/get-system-drawing-bitmap-of-a-wpf-area-using-visualbrush
var visual = new DrawingVisual();
var context = visual.RenderOpen();
context.DrawDrawing(group);
context.Close();
const int dpiScale100 = 96;
var bitmap = new RenderTargetBitmap(
Convert.ToInt32(width), Convert.ToInt32(height),
dpiScale100, dpiScale100,
PixelFormats.Pbgra32
);
bitmap.Render(visual);
return bitmap;
}
else
{
ProgramLogger.LogException($"|UWP|PlatedImage|{Package.Location}" +
$"|Unable to convert background string {BackgroundColor} " +
$"to color for {Package.Location}", new InvalidOperationException());

return new BitmapImage(new Uri(Constant.ErrorIcon));
}
}
else
{
// todo use windows theme as background
return image;
}
}

public override string ToString()
{
return $"{DisplayName}: {Description}";
Expand Down
2 changes: 1 addition & 1 deletion src/modules/launcher/PowerLauncher.UI/LauncherControl.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@
<RowDefinition Height="*"/>
</Grid.RowDefinitions>

<Image x:Name="AppIcon" Height="36" Margin="8,0,0,0" Grid.RowSpan="2" HorizontalAlignment="Left" Source="{Binding Result.IcoPath}" />
<Image x:Name="AppIcon" Height="36" Margin="8,0,0,0" Grid.RowSpan="2" HorizontalAlignment="Left" Source="{Binding Image}" />
<TextBlock x:Name="Title" Grid.Column="1" Text="{Binding Result.Title}" FontWeight="SemiBold" FontSize="20" VerticalAlignment="Bottom"/>
<TextBlock x:Name="Path" Grid.Column="1" Text= "{Binding Result.SubTitle}" Grid.Row="1" Opacity="0.6" VerticalAlignment="Top"/>
<StackPanel Orientation="Horizontal" Opacity="0" HorizontalAlignment="Right" Grid.RowSpan="2" Grid.Column="1">
Expand Down
41 changes: 0 additions & 41 deletions src/modules/launcher/PowerLauncher/Msg.xaml

This file was deleted.

87 changes: 0 additions & 87 deletions src/modules/launcher/PowerLauncher/Msg.xaml.cs

This file was deleted.

3 changes: 1 addition & 2 deletions src/modules/launcher/Wox.Infrastructure/Image/ImageCache.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Media;
using Windows.UI.Xaml.Media;

namespace Wox.Infrastructure.Image
{
Expand Down

This file was deleted.

40 changes: 4 additions & 36 deletions src/modules/launcher/Wox.Infrastructure/Image/ImageLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,17 @@
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Wox.Infrastructure.Logger;
using Wox.Infrastructure.Storage;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml.Media;

namespace Wox.Infrastructure.Image
{
public static class ImageLoader
{
private static readonly ImageCache ImageCache = new ImageCache();
private static BinaryStorage<ConcurrentDictionary<string, int>> _storage;
private static readonly ConcurrentDictionary<string, string> GuidToKey = new ConcurrentDictionary<string, string>();
private static IImageHashGenerator _hashGenerator;


private static readonly string[] ImageExtensions =
{
Expand All @@ -33,13 +30,11 @@ public static class ImageLoader
public static void Initialize()
{
_storage = new BinaryStorage<ConcurrentDictionary<string, int>>("Image");
_hashGenerator = new ImageHashGenerator();
ImageCache.Usage = _storage.TryLoad(new ConcurrentDictionary<string, int>());

foreach (var icon in new[] { Constant.DefaultIcon, Constant.ErrorIcon })
{
ImageSource img = new BitmapImage(new Uri(icon));
img.Freeze();
ImageCache[icon] = img;
}
Task.Run(() =>
Expand Down Expand Up @@ -101,7 +96,6 @@ private static ImageResult LoadInternal(string path, bool loadFullImage = false)
if (path.StartsWith("data:", StringComparison.OrdinalIgnoreCase))
{
var imageSource = new BitmapImage(new Uri(path));
imageSource.Freeze();
return new ImageResult(imageSource, ImageType.Data);
}

Expand Down Expand Up @@ -156,11 +150,6 @@ private static ImageResult LoadInternal(string path, bool loadFullImage = false)
image = ImageCache[Constant.ErrorIcon];
path = Constant.ErrorIcon;
}

if (type != ImageType.Error)
{
image.Freeze();
}
}
catch (System.Exception e)
{
Expand All @@ -172,43 +161,22 @@ private static ImageResult LoadInternal(string path, bool loadFullImage = false)
return new ImageResult(image, type);
}

private static bool EnableImageHash = true;

public static ImageSource Load(string path, bool loadFullImage = false)
{
var imageResult = LoadInternal(path, loadFullImage);

var img = imageResult.ImageSource;
if (imageResult.ImageType != ImageType.Error && imageResult.ImageType != ImageType.Cache)
{ // we need to get image hash
string hash = EnableImageHash ? _hashGenerator.GetHashFromImage(img) : null;
if (hash != null)
{
if (GuidToKey.TryGetValue(hash, out string key))
{ // image already exists
img = ImageCache[key];
}
else
{ // new guid
GuidToKey[hash] = path;
}
}

{
// update cache
ImageCache[path] = 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.EndInit();
BitmapImage image = new BitmapImage(new Uri(path));
return image;
}
}
Expand Down
Loading

0 comments on commit 367cb41

Please sign in to comment.