diff options
| author | Shlomo Hecht <shlomo@twine-s.com> | 2020-03-18 10:03:11 +0200 |
|---|---|---|
| committer | Shlomo Hecht <shlomo@twine-s.com> | 2020-03-18 10:03:11 +0200 |
| commit | ada2ce25bd36b6f7b3c8aa01039cc9611b22e55c (patch) | |
| tree | 0684b4e94a807188d2d92f46c33ed36b724692bc /Software/Visual_Studio/Tango.FileSystem/FileItem.cs | |
| parent | 2cd94c78ab9de58fc1f8525e69ab5fa563d0ff75 (diff) | |
| parent | cb7fff096f2fe6812184a286290eaad193c7c2df (diff) | |
| download | Tango-ada2ce25bd36b6f7b3c8aa01039cc9611b22e55c.tar.gz Tango-ada2ce25bd36b6f7b3c8aa01039cc9611b22e55c.zip | |
Merge branch 'master' of https://twinetfs.visualstudio.com/Tango/_git/Tango
Diffstat (limited to 'Software/Visual_Studio/Tango.FileSystem/FileItem.cs')
| -rw-r--r-- | Software/Visual_Studio/Tango.FileSystem/FileItem.cs | 132 |
1 files changed, 132 insertions, 0 deletions
diff --git a/Software/Visual_Studio/Tango.FileSystem/FileItem.cs b/Software/Visual_Studio/Tango.FileSystem/FileItem.cs new file mode 100644 index 000000000..cbc90ce06 --- /dev/null +++ b/Software/Visual_Studio/Tango.FileSystem/FileItem.cs @@ -0,0 +1,132 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Interop; +using System.Windows.Media; +using System.Windows.Media.Imaging; +using Tango.Core.IO; + +namespace Tango.FileSystem +{ + public class FileItem : FileSystemItem + { + private static Dictionary<String, BitmapSource> iconCache; + private static Dictionary<String, BitmapSource> smallIconCache; + private static Dictionary<String, String> typeDescriptionCache; + + static FileItem() + { + iconCache = new Dictionary<string, BitmapSource>(); + smallIconCache = new Dictionary<string, BitmapSource>(); + typeDescriptionCache = new Dictionary<string, string>(); + } + + public FileItem() + { + Type = FileSystemItemType.File; + } + + public String Extension + { + get { return System.IO.Path.GetExtension(Path); } + } + + private BitmapSource _icon; + public BitmapSource Icon + { + get + { + if (_icon == null) + { + _icon = GetFileIcon(); + } + + return _icon; + } + } + + private BitmapSource _smallIcon; + public BitmapSource SmallIcon + { + get + { + if (_smallIcon == null) + { + _smallIcon = GetSmallFileIcon(); + } + + return _smallIcon; + } + } + + public override string Description + { + get + { + if (typeDescriptionCache.ContainsKey(Extension)) + { + return typeDescriptionCache[Extension]; + } + else + { + var tempFile = TemporaryManager.Default.CreateFile(Extension); + var shellFile = Microsoft.WindowsAPICodePack.Shell.ShellFile.FromFilePath(tempFile); + var text = shellFile.Properties.System.ItemTypeText.Value.ToStringSafe(); + shellFile.Dispose(); + tempFile.Delete(); + typeDescriptionCache.Add(Extension, text); + return text; + } + } + } + + protected BitmapSource GetFileIcon() + { + if (iconCache.ContainsKey(Extension)) + { + return iconCache[Extension]; + } + else + { + var tempFile = TemporaryManager.Default.CreateFile(Extension); + var shellFile = Microsoft.WindowsAPICodePack.Shell.ShellFile.FromFilePath(tempFile); + var source = shellFile.Thumbnail.MediumBitmapSource; + shellFile.Dispose(); + tempFile.Delete(); + iconCache.Add(Extension, source); + return source; + } + } + + private BitmapSource GetSmallFileIcon() + { + if (smallIconCache.ContainsKey(Extension)) + { + return smallIconCache[Extension]; + } + else + { + var tempFile = TemporaryManager.Default.CreateFile(Extension); + var shellFile = Microsoft.WindowsAPICodePack.Shell.ShellFile.FromFilePath(tempFile); + var source = shellFile.Thumbnail.SmallBitmapSource; + shellFile.Dispose(); + tempFile.Delete(); + smallIconCache.Add(Extension, source); + return source; + } + } + + private BitmapSource IconToBitmapSource(Icon icon) + { + var imageSource = Imaging.CreateBitmapSourceFromHIcon( + icon.Handle, + Int32Rect.Empty, + BitmapSizeOptions.FromEmptyOptions()); + return imageSource; + } + } +} |
