JD2022-TU1/main/tools/modules/JD.AssetBrowser/ViewModel/AssetViewModel.cs

99 lines
3 KiB
C#

using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using JD.Asset;
using JD.Filters;
using Plume.WPF.Mvvm;
namespace JD.AssetBrowser.ViewModel
{
public partial class AssetViewModel : BindableBase
{
private static readonly Dictionary<string, ImageSource> IconCache = new Dictionary<string, ImageSource>();
private IAsset _model;
private string _name;
[Filterable(false)]
public IAsset Model
{
get => _model;
set
{
_model = value;
Name = value.Name;
}
}
public string Name
{
get => _name;
set => SetProperty(ref _name, value);
}
[QuickFilterable(true)]
public string RelativePath => Model != null ? Model.Path.RelativeUri : "";
public string ParentFolder => Model != null ? Model.Path.Parent.RelativeUri : "";
public string AbsoluteDirPath => Model != null ? System.IO.Path.GetDirectoryName(Model.Path) : "";
[QuickFilterable(true)]
public string AbsolutePath => Model != null ? Model.Path.Absolute : "";
public bool IsFolder => Model.AssetType == AssetType.Directory;
public bool IsFile => Model.AssetType == AssetType.File;
public int DependencyCount => Model.Dependencies.Count;
public int ReferencedByCount => Model.ReferencedBy.Count;
public string Type => Model.GetType().Name;
public string Extension => Path.GetExtension(AbsolutePath);
/// <summary>
/// Icon of the asset
/// </summary>
[Filterable(false)]
public ImageSource Icon
{
get
{
var extension = System.IO.Path.GetExtension(Model.Path);
if (extension != null && IconCache.ContainsKey(extension))
{
return IconCache[extension];
}
if (System.IO.File.Exists(Model.Path.Absolute))
{
using (Icon sysicon = System.Drawing.Icon.ExtractAssociatedIcon(Model.Path.Absolute))
{
if (sysicon != null)
{
var icon = Imaging.CreateBitmapSourceFromHIcon(sysicon.Handle, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
if (extension != null) IconCache.Add(extension, icon);
return icon;
}
}
}
return null;
}
}
public void RefreshDependencies()
{
OnPropertyChanged(nameof(DependencyCount));
OnPropertyChanged(nameof(ReferencedByCount));
}
public override string ToString()
{
return RelativePath;
}
}
}