diff --git a/src/modules/launcher/Plugins/Wox.Plugin.Program/Programs/UWP.cs b/src/modules/launcher/Plugins/Wox.Plugin.Program/Programs/UWP.cs index 35917cb312..b41e5c838c 100644 --- a/src/modules/launcher/Plugins/Wox.Plugin.Program/Programs/UWP.cs +++ b/src/modules/launcher/Plugins/Wox.Plugin.Program/Programs/UWP.cs @@ -7,8 +7,6 @@ using System.Runtime.InteropServices; 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; @@ -18,6 +16,8 @@ using Wox.Infrastructure; 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 { @@ -517,11 +517,7 @@ namespace Wox.Plugin.Program.Programs 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; } @@ -541,62 +537,6 @@ namespace Wox.Plugin.Program.Programs } } - 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}"; diff --git a/src/modules/launcher/PowerLauncher.UI/LauncherControl.xaml b/src/modules/launcher/PowerLauncher.UI/LauncherControl.xaml index 359131901c..b335201420 100644 --- a/src/modules/launcher/PowerLauncher.UI/LauncherControl.xaml +++ b/src/modules/launcher/PowerLauncher.UI/LauncherControl.xaml @@ -446,7 +446,7 @@ - + diff --git a/src/modules/launcher/PowerLauncher/Msg.xaml b/src/modules/launcher/PowerLauncher/Msg.xaml deleted file mode 100644 index fe14c40ada..0000000000 --- a/src/modules/launcher/PowerLauncher/Msg.xaml +++ /dev/null @@ -1,41 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - Title - sdfdsf - - - - \ No newline at end of file diff --git a/src/modules/launcher/PowerLauncher/Msg.xaml.cs b/src/modules/launcher/PowerLauncher/Msg.xaml.cs deleted file mode 100644 index e9c504eb98..0000000000 --- a/src/modules/launcher/PowerLauncher/Msg.xaml.cs +++ /dev/null @@ -1,87 +0,0 @@ -using System; -using System.IO; -using System.Windows; -using System.Windows.Forms; -using System.Windows.Input; -using System.Windows.Media.Animation; -using System.Windows.Media.Imaging; -using Wox.Helper; -using Wox.Infrastructure; -using Wox.Infrastructure.Image; - -namespace PowerLauncher -{ - public partial class Msg : Window - { - Storyboard fadeOutStoryboard = new Storyboard(); - private bool closing; - - public Msg() - { - InitializeComponent(); - var screen = Screen.FromPoint(System.Windows.Forms.Cursor.Position); - var dipWorkingArea = WindowsInteropHelper.TransformPixelsToDIP(this, - screen.WorkingArea.Width, - screen.WorkingArea.Height); - Left = dipWorkingArea.X - Width; - Top = dipWorkingArea.Y; - showAnimation.From = dipWorkingArea.Y; - showAnimation.To = dipWorkingArea.Y - Height; - - // Create the fade out storyboard - fadeOutStoryboard.Completed += fadeOutStoryboard_Completed; - DoubleAnimation fadeOutAnimation = new DoubleAnimation(dipWorkingArea.Y - Height, dipWorkingArea.Y, new Duration(TimeSpan.FromSeconds(5))) - { - AccelerationRatio = 0.2 - }; - Storyboard.SetTarget(fadeOutAnimation, this); - Storyboard.SetTargetProperty(fadeOutAnimation, new PropertyPath(TopProperty)); - fadeOutStoryboard.Children.Add(fadeOutAnimation); - - imgClose.Source = ImageLoader.Load(Path.Combine(Wox.Infrastructure.Constant.ProgramDirectory, "Images\\close.png")); - imgClose.MouseUp += imgClose_MouseUp; - } - - void imgClose_MouseUp(object sender, MouseButtonEventArgs e) - { - if (!closing) - { - closing = true; - fadeOutStoryboard.Begin(); - } - } - - private void fadeOutStoryboard_Completed(object sender, EventArgs e) - { - Close(); - } - - public void Show(string title, string subTitle, string iconPath) - { - tbTitle.Text = title; - tbSubTitle.Text = subTitle; - if (string.IsNullOrEmpty(subTitle)) - { - tbSubTitle.Visibility = Visibility.Collapsed; - } - if (!File.Exists(iconPath)) - { - imgIco.Source = ImageLoader.Load(Path.Combine(Wox.Infrastructure.Constant.ProgramDirectory, "Images\\app.png")); - } - else { - imgIco.Source = ImageLoader.Load(iconPath); - } - - Show(); - - Dispatcher.InvokeAsync(async () => - { - if (!closing) - { - closing = true; - await Dispatcher.InvokeAsync(fadeOutStoryboard.Begin); - } - }); - } - } -} diff --git a/src/modules/launcher/Wox.Infrastructure/Image/ImageCache.cs b/src/modules/launcher/Wox.Infrastructure/Image/ImageCache.cs index 5e74a2a380..a47f4d255f 100644 --- a/src/modules/launcher/Wox.Infrastructure/Image/ImageCache.cs +++ b/src/modules/launcher/Wox.Infrastructure/Image/ImageCache.cs @@ -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 { diff --git a/src/modules/launcher/Wox.Infrastructure/Image/ImageHashGenerator.cs b/src/modules/launcher/Wox.Infrastructure/Image/ImageHashGenerator.cs deleted file mode 100644 index 9ace8b74fc..0000000000 --- a/src/modules/launcher/Wox.Infrastructure/Image/ImageHashGenerator.cs +++ /dev/null @@ -1,49 +0,0 @@ -using System; -using System.IO; -using System.Security.Cryptography; -using System.Windows.Media; -using System.Windows.Media.Imaging; - -namespace Wox.Infrastructure.Image -{ - public interface IImageHashGenerator - { - string GetHashFromImage(ImageSource image); - } - public class ImageHashGenerator : IImageHashGenerator - { - public string GetHashFromImage(ImageSource imageSource) - { - if (!(imageSource is BitmapSource image)) - { - return null; - } - - try - { - using (var outStream = new MemoryStream()) - { - // PngBitmapEncoder enc2 = new PngBitmapEncoder(); - // enc2.Frames.Add(BitmapFrame.Create(tt)); - - var enc = new JpegBitmapEncoder(); - var bitmapFrame = BitmapFrame.Create(image); - bitmapFrame.Freeze(); - enc.Frames.Add(bitmapFrame); - enc.Save(outStream); - var byteArray = outStream.GetBuffer(); - using (var sha1 = new SHA1CryptoServiceProvider()) - { - var hash = Convert.ToBase64String(sha1.ComputeHash(byteArray)); - return hash; - } - } - } - catch - { - return null; - } - - } - } -} \ No newline at end of file diff --git a/src/modules/launcher/Wox.Infrastructure/Image/ImageLoader.cs b/src/modules/launcher/Wox.Infrastructure/Image/ImageLoader.cs index 528900ce7c..4c148065eb 100644 --- a/src/modules/launcher/Wox.Infrastructure/Image/ImageLoader.cs +++ b/src/modules/launcher/Wox.Infrastructure/Image/ImageLoader.cs @@ -3,10 +3,10 @@ using System.Collections.Concurrent; 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 { @@ -14,9 +14,6 @@ namespace Wox.Infrastructure.Image { private static readonly ImageCache ImageCache = new ImageCache(); private static BinaryStorage> _storage; - private static readonly ConcurrentDictionary GuidToKey = new ConcurrentDictionary(); - private static IImageHashGenerator _hashGenerator; - private static readonly string[] ImageExtensions = { @@ -33,13 +30,11 @@ namespace Wox.Infrastructure.Image public static void Initialize() { _storage = new BinaryStorage>("Image"); - _hashGenerator = new ImageHashGenerator(); ImageCache.Usage = _storage.TryLoad(new ConcurrentDictionary()); foreach (var icon in new[] { Constant.DefaultIcon, Constant.ErrorIcon }) { ImageSource img = new BitmapImage(new Uri(icon)); - img.Freeze(); ImageCache[icon] = img; } Task.Run(() => @@ -101,7 +96,6 @@ namespace Wox.Infrastructure.Image if (path.StartsWith("data:", StringComparison.OrdinalIgnoreCase)) { var imageSource = new BitmapImage(new Uri(path)); - imageSource.Freeze(); return new ImageResult(imageSource, ImageType.Data); } @@ -156,11 +150,6 @@ namespace Wox.Infrastructure.Image image = ImageCache[Constant.ErrorIcon]; path = Constant.ErrorIcon; } - - if (type != ImageType.Error) - { - image.Freeze(); - } } catch (System.Exception e) { @@ -172,43 +161,22 @@ namespace Wox.Infrastructure.Image 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; } } diff --git a/src/modules/launcher/Wox.Infrastructure/Image/ThumbnailReader.cs b/src/modules/launcher/Wox.Infrastructure/Image/ThumbnailReader.cs index bd65fc7000..88d6d464c4 100644 --- a/src/modules/launcher/Wox.Infrastructure/Image/ThumbnailReader.cs +++ b/src/modules/launcher/Wox.Infrastructure/Image/ThumbnailReader.cs @@ -4,6 +4,9 @@ using System.IO; using System.Windows.Interop; using System.Windows.Media.Imaging; using System.Windows; +using Windows.Storage.Streams; +using BitmapSourceWPF = System.Windows.Media.Imaging.BitmapSource; +using BitmapImageUWP = Windows.UI.Xaml.Media.Imaging.BitmapImage; namespace Wox.Infrastructure.Image { @@ -103,14 +106,36 @@ namespace Wox.Infrastructure.Image public int Height { set { height = value; } } }; + public static BitmapImageUWP ByteToImage(byte[] imageData) + { + using (InMemoryRandomAccessStream ms = new InMemoryRandomAccessStream()) + { + using (DataWriter writer = new DataWriter(ms.GetOutputStreamAt(0))) + { + writer.WriteBytes(imageData); + writer.StoreAsync().GetResults(); + } + BitmapImageUWP image = new BitmapImageUWP(); + image.SetSource(ms); + return image; + } + } - public static BitmapSource GetThumbnail(string fileName, int width, int height, ThumbnailOptions options) + public static BitmapImageUWP GetThumbnail(string fileName, int width, int height, ThumbnailOptions options) { IntPtr hBitmap = GetHBitmap(Path.GetFullPath(fileName), width, height, options); - try { - return Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); + byte[] data; + BitmapSourceWPF bitmapSourceWPF = Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); + PngBitmapEncoder encoder = new PngBitmapEncoder(); + encoder.Frames.Add(BitmapFrame.Create(bitmapSourceWPF)); + using (MemoryStream ms = new MemoryStream()) + { + encoder.Save(ms); + data = ms.ToArray(); + } + return ByteToImage(data); } finally { diff --git a/src/modules/launcher/Wox.Infrastructure/Wox.Infrastructure.csproj b/src/modules/launcher/Wox.Infrastructure/Wox.Infrastructure.csproj index afeaef5c68..692210192e 100644 --- a/src/modules/launcher/Wox.Infrastructure/Wox.Infrastructure.csproj +++ b/src/modules/launcher/Wox.Infrastructure/Wox.Infrastructure.csproj @@ -48,6 +48,7 @@ + diff --git a/src/modules/launcher/Wox.Plugin/Result.cs b/src/modules/launcher/Wox.Plugin/Result.cs index 6e0559d35f..d33163d180 100644 --- a/src/modules/launcher/Wox.Plugin/Result.cs +++ b/src/modules/launcher/Wox.Plugin/Result.cs @@ -1,7 +1,7 @@ using System; using System.Collections.Generic; using System.IO; -using System.Windows.Media; +using Windows.UI.Xaml.Media; namespace Wox.Plugin { diff --git a/src/modules/launcher/Wox.Plugin/Wox.Plugin.csproj b/src/modules/launcher/Wox.Plugin/Wox.Plugin.csproj index 55d01df17f..3ec7d48286 100644 --- a/src/modules/launcher/Wox.Plugin/Wox.Plugin.csproj +++ b/src/modules/launcher/Wox.Plugin/Wox.Plugin.csproj @@ -57,6 +57,7 @@ runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/src/modules/launcher/Wox/Msg.xaml b/src/modules/launcher/Wox/Msg.xaml deleted file mode 100644 index b077be8e07..0000000000 --- a/src/modules/launcher/Wox/Msg.xaml +++ /dev/null @@ -1,41 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - Title - sdfdsf - - - - \ No newline at end of file diff --git a/src/modules/launcher/Wox/Msg.xaml.cs b/src/modules/launcher/Wox/Msg.xaml.cs deleted file mode 100644 index df82edb124..0000000000 --- a/src/modules/launcher/Wox/Msg.xaml.cs +++ /dev/null @@ -1,87 +0,0 @@ -using System; -using System.IO; -using System.Windows; -using System.Windows.Forms; -using System.Windows.Input; -using System.Windows.Media.Animation; -using System.Windows.Media.Imaging; -using Wox.Helper; -using Wox.Infrastructure; -using Wox.Infrastructure.Image; - -namespace Wox -{ - public partial class Msg : Window - { - Storyboard fadeOutStoryboard = new Storyboard(); - private bool closing; - - public Msg() - { - InitializeComponent(); - var screen = Screen.FromPoint(System.Windows.Forms.Cursor.Position); - var dipWorkingArea = WindowsInteropHelper.TransformPixelsToDIP(this, - screen.WorkingArea.Width, - screen.WorkingArea.Height); - Left = dipWorkingArea.X - Width; - Top = dipWorkingArea.Y; - showAnimation.From = dipWorkingArea.Y; - showAnimation.To = dipWorkingArea.Y - Height; - - // Create the fade out storyboard - fadeOutStoryboard.Completed += fadeOutStoryboard_Completed; - DoubleAnimation fadeOutAnimation = new DoubleAnimation(dipWorkingArea.Y - Height, dipWorkingArea.Y, new Duration(TimeSpan.FromSeconds(5))) - { - AccelerationRatio = 0.2 - }; - Storyboard.SetTarget(fadeOutAnimation, this); - Storyboard.SetTargetProperty(fadeOutAnimation, new PropertyPath(TopProperty)); - fadeOutStoryboard.Children.Add(fadeOutAnimation); - - imgClose.Source = ImageLoader.Load(Path.Combine(Infrastructure.Constant.ProgramDirectory, "Images\\close.png")); - imgClose.MouseUp += imgClose_MouseUp; - } - - void imgClose_MouseUp(object sender, MouseButtonEventArgs e) - { - if (!closing) - { - closing = true; - fadeOutStoryboard.Begin(); - } - } - - private void fadeOutStoryboard_Completed(object sender, EventArgs e) - { - Close(); - } - - public void Show(string title, string subTitle, string iconPath) - { - tbTitle.Text = title; - tbSubTitle.Text = subTitle; - if (string.IsNullOrEmpty(subTitle)) - { - tbSubTitle.Visibility = Visibility.Collapsed; - } - if (!File.Exists(iconPath)) - { - imgIco.Source = ImageLoader.Load(Path.Combine(Infrastructure.Constant.ProgramDirectory, "Images\\app.png")); - } - else { - imgIco.Source = ImageLoader.Load(iconPath); - } - - Show(); - - Dispatcher.InvokeAsync(async () => - { - if (!closing) - { - closing = true; - await Dispatcher.InvokeAsync(fadeOutStoryboard.Begin); - } - }); - } - } -} diff --git a/src/modules/launcher/Wox/PublicAPIInstance.cs b/src/modules/launcher/Wox/PublicAPIInstance.cs index a1b2e5119e..10c33b3b36 100644 --- a/src/modules/launcher/Wox/PublicAPIInstance.cs +++ b/src/modules/launcher/Wox/PublicAPIInstance.cs @@ -4,7 +4,6 @@ using System.Linq; using System.Net; using System.Threading.Tasks; using System.Windows; - using Wox.Core.Plugin; using Wox.Core.Resource; using Wox.Helper; @@ -97,12 +96,7 @@ namespace Wox } public void ShowMsg(string title, string subTitle = "", string iconPath = "", bool useMainWindowAsOwner = true) - { - Application.Current.Dispatcher.Invoke(() => - { - var msg = useMainWindowAsOwner ? new Msg { Owner = Application.Current.MainWindow } : new Msg(); - msg.Show(title, subTitle, iconPath); - }); + { } public void StartLoadingBar() diff --git a/src/modules/launcher/Wox/ViewModel/PluginViewModel.cs b/src/modules/launcher/Wox/ViewModel/PluginViewModel.cs index b1ae890938..0a5239a8a0 100644 --- a/src/modules/launcher/Wox/ViewModel/PluginViewModel.cs +++ b/src/modules/launcher/Wox/ViewModel/PluginViewModel.cs @@ -1,8 +1,8 @@ using System.Windows; -using System.Windows.Media; using Wox.Plugin; using Wox.Core.Resource; using Wox.Infrastructure.Image; +using Windows.UI.Xaml.Media; namespace Wox.ViewModel { diff --git a/src/modules/launcher/Wox/ViewModel/ResultViewModel.cs b/src/modules/launcher/Wox/ViewModel/ResultViewModel.cs index 54a340dd24..8daaf4d603 100644 --- a/src/modules/launcher/Wox/ViewModel/ResultViewModel.cs +++ b/src/modules/launcher/Wox/ViewModel/ResultViewModel.cs @@ -1,10 +1,9 @@ using System; -using System.Windows.Media; -using System.Windows.Threading; using Wox.Infrastructure; using Wox.Infrastructure.Image; using Wox.Infrastructure.Logger; using Wox.Plugin; +using Windows.UI.Xaml.Media; namespace Wox.ViewModel