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 IconCache = new Dictionary(); 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); /// /// Icon of the asset /// [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; } } }