delete cover art when close

This commit is contained in:
Paddy Xu
2018-01-25 23:56:15 +02:00
parent d2fdcd240b
commit 5b0da969af
2 changed files with 28 additions and 7 deletions

View File

@@ -19,12 +19,14 @@ using System;
using System.ComponentModel; using System.ComponentModel;
using System.Diagnostics; using System.Diagnostics;
using System.Drawing; using System.Drawing;
using System.IO;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows; using System.Windows;
using System.Windows.Controls; using System.Windows.Controls;
using System.Windows.Input; using System.Windows.Input;
using System.Windows.Media.Animation; using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using Meta.Vlc; using Meta.Vlc;
using Meta.Vlc.Interop.Media; using Meta.Vlc.Interop.Media;
using QuickLook.Annotations; using QuickLook.Annotations;
@@ -39,8 +41,9 @@ namespace QuickLook.Plugin.VideoViewer
public partial class ViewerPanel : UserControl, IDisposable, INotifyPropertyChanged public partial class ViewerPanel : UserControl, IDisposable, INotifyPropertyChanged
{ {
private readonly ContextObject _context; private readonly ContextObject _context;
private BitmapSource _coverArt;
private Uri _coverArt; private string _coverArtPath = string.Empty;
private bool _hasAudio; private bool _hasAudio;
private bool _hasEnded; private bool _hasEnded;
private bool _hasVideo; private bool _hasVideo;
@@ -147,12 +150,12 @@ namespace QuickLook.Plugin.VideoViewer
} }
} }
public Uri CoverArt public BitmapSource CoverArt
{ {
get => _coverArt; get => _coverArt;
private set private set
{ {
if (value == _coverArt) return; if (ReferenceEquals(value, _coverArt)) return;
if (value == null) return; if (value == null) return;
_coverArt = value; _coverArt = value;
OnPropertyChanged(); OnPropertyChanged();
@@ -172,6 +175,10 @@ namespace QuickLook.Plugin.VideoViewer
mediaElement?.Dispose(); mediaElement?.Dispose();
mediaElement = null; mediaElement = null;
}); });
// delete cached cover art
if (!string.IsNullOrWhiteSpace(_coverArtPath))
File.Delete(_coverArtPath);
} }
catch (Exception e) catch (Exception e)
{ {
@@ -245,9 +252,13 @@ namespace QuickLook.Plugin.VideoViewer
if (HasVideo) if (HasVideo)
return; return;
var path = mediaElement.VlcMediaPlayer.Media.GetMeta(MetaDataType.ArtworkUrl); var artUri = mediaElement.VlcMediaPlayer.Media.GetMeta(MetaDataType.ArtworkUrl);
if (!string.IsNullOrEmpty(path)) if (!string.IsNullOrEmpty(artUri))
CoverArt = new Uri(path); {
var u = new Uri(artUri);
_coverArtPath = u.LocalPath;
CoverArt = u.LoadBitmapImage();
}
metaTitle.Text = mediaElement.VlcMediaPlayer.Media.GetMeta(MetaDataType.Title); metaTitle.Text = mediaElement.VlcMediaPlayer.Media.GetMeta(MetaDataType.Title);
metaArtists.Text = mediaElement.VlcMediaPlayer.Media.GetMeta(MetaDataType.Artist); metaArtists.Text = mediaElement.VlcMediaPlayer.Media.GetMeta(MetaDataType.Artist);
@@ -270,7 +281,7 @@ namespace QuickLook.Plugin.VideoViewer
return; return;
var dark = false; var dark = false;
using (var bitmap = new Bitmap(CoverArt.LocalPath)) using (var bitmap = new Bitmap(CoverArt.ToBitmap()))
{ {
dark = bitmap.IsDarkImage(); dark = bitmap.IsDarkImage();
} }

View File

@@ -135,5 +135,15 @@ namespace QuickLook.ExtensionMethods
return darks > 0.65 * sampleCount; return darks > 0.65 * sampleCount;
} }
public static BitmapImage LoadBitmapImage(this Uri source)
{
var bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.UriSource = source;
bitmap.EndInit();
return bitmap;
}
} }
} }