From ac3c227bb5d12339fee6fb4c243f3a5f67217915 Mon Sep 17 00:00:00 2001 From: Roy Ben-Shabat Date: Wed, 28 Nov 2018 20:16:11 +0200 Subject: Working on machine studio storage ! --- .../Converters/StorageItemToImageConverter.cs | 39 ++++++ .../Helpers/FileIconHelper.cs | 146 +++++++++++++++++++++ .../Tango.MachineStudio.Storage/Images/file.png | Bin 0 -> 1184 bytes .../Tango.MachineStudio.Storage/Images/folder.png | Bin 0 -> 756 bytes .../Tango.MachineStudio.Storage.csproj | 13 +- .../ViewModels/MainViewVM.cs | 79 +++++++++++ .../Views/MainView.xaml | 126 +++++++++++++++++- 7 files changed, 401 insertions(+), 2 deletions(-) create mode 100644 Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Storage/Converters/StorageItemToImageConverter.cs create mode 100644 Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Storage/Helpers/FileIconHelper.cs create mode 100644 Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Storage/Images/file.png create mode 100644 Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Storage/Images/folder.png (limited to 'Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Storage') diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Storage/Converters/StorageItemToImageConverter.cs b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Storage/Converters/StorageItemToImageConverter.cs new file mode 100644 index 000000000..e9d2c8c18 --- /dev/null +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Storage/Converters/StorageItemToImageConverter.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Data; +using Tango.Integration.Storage; +using Tango.SharedUI.Helpers; + +namespace Tango.MachineStudio.Storage.Converters +{ + public class StorageItemToImageConverter : IValueConverter + { + public object Convert(object value, Type targetType, object parameter, CultureInfo culture) + { + StorageItem item = value as StorageItem; + + if (item != null) + { + if (item is StorageFolder) + { + return ResourceHelper.GetImageFromResources("Images/folder.png"); + } + else + { + return Helpers.FileIconHelper.FindIconForFilename(item.Name, true); + } + } + + return null; + } + + public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) + { + throw new NotImplementedException(); + } + } +} diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Storage/Helpers/FileIconHelper.cs b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Storage/Helpers/FileIconHelper.cs new file mode 100644 index 000000000..47dab8e5e --- /dev/null +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Storage/Helpers/FileIconHelper.cs @@ -0,0 +1,146 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.IO; +using System.Runtime.InteropServices; +using System.Windows; +using System.Windows.Interop; +using System.Windows.Media; +using System.Windows.Media.Imaging; + +namespace Tango.MachineStudio.Storage.Helpers +{ + public static class FileIconHelper + { + private static readonly Dictionary _smallIconCache = new Dictionary(); + private static readonly Dictionary _largeIconCache = new Dictionary(); + /// + /// Get an icon for a given filename + /// + /// any filename + /// 16x16 or 32x32 icon + /// null if path is null, otherwise - an icon + public static ImageSource FindIconForFilename(string fileName, bool large) + { + var extension = Path.GetExtension(fileName); + if (extension == null) + return null; + var cache = large ? _largeIconCache : _smallIconCache; + ImageSource icon; + if (cache.TryGetValue(extension, out icon)) + return icon; + icon = IconReader.GetFileIcon(fileName, large ? IconReader.IconSize.Large : IconReader.IconSize.Small, false).ToImageSource(); + cache.Add(extension, icon); + return icon; + } + /// + /// http://stackoverflow.com/a/6580799/1943849 + /// + static ImageSource ToImageSource(this Icon icon) + { + var imageSource = Imaging.CreateBitmapSourceFromHIcon( + icon.Handle, + Int32Rect.Empty, + BitmapSizeOptions.FromEmptyOptions()); + return imageSource; + } + /// + /// Provides static methods to read system icons for both folders and files. + /// + /// + /// IconReader.GetFileIcon("c:\\general.xls"); + /// + static class IconReader + { + /// + /// Options to specify the size of icons to return. + /// + public enum IconSize + { + /// + /// Specify large icon - 32 pixels by 32 pixels. + /// + Large = 0, + /// + /// Specify small icon - 16 pixels by 16 pixels. + /// + Small = 1 + } + /// + /// Returns an icon for a given file - indicated by the name parameter. + /// + /// Pathname for file. + /// Large or small + /// Whether to include the link icon + /// System.Drawing.Icon + public static Icon GetFileIcon(string name, IconSize size, bool linkOverlay) + { + var shfi = new Shell32.Shfileinfo(); + var flags = Shell32.ShgfiIcon | Shell32.ShgfiUsefileattributes; + if (linkOverlay) flags += Shell32.ShgfiLinkoverlay; + /* Check the size specified for return. */ + if (IconSize.Small == size) + flags += Shell32.ShgfiSmallicon; + else + flags += Shell32.ShgfiLargeicon; + Shell32.SHGetFileInfo(name, + Shell32.FileAttributeNormal, + ref shfi, + (uint)Marshal.SizeOf(shfi), + flags); + // Copy (clone) the returned icon to a new object, thus allowing us to clean-up properly + var icon = (Icon)Icon.FromHandle(shfi.hIcon).Clone(); + User32.DestroyIcon(shfi.hIcon); // Cleanup + return icon; + } + } + /// + /// Wraps necessary Shell32.dll structures and functions required to retrieve Icon Handles using SHGetFileInfo. Code + /// courtesy of MSDN Cold Rooster Consulting case study. + /// + static class Shell32 + { + private const int MaxPath = 256; + [StructLayout(LayoutKind.Sequential)] + public struct Shfileinfo + { + private const int Namesize = 80; + public readonly IntPtr hIcon; + private readonly int iIcon; + private readonly uint dwAttributes; + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = MaxPath)] + private readonly string szDisplayName; + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = Namesize)] + private readonly string szTypeName; + }; + public const uint ShgfiIcon = 0x000000100; // get icon + public const uint ShgfiLinkoverlay = 0x000008000; // put a link overlay on icon + public const uint ShgfiLargeicon = 0x000000000; // get large icon + public const uint ShgfiSmallicon = 0x000000001; // get small icon + public const uint ShgfiUsefileattributes = 0x000000010; // use passed dwFileAttribute + public const uint FileAttributeNormal = 0x00000080; + [DllImport("Shell32.dll")] + public static extern IntPtr SHGetFileInfo( + string pszPath, + uint dwFileAttributes, + ref Shfileinfo psfi, + uint cbFileInfo, + uint uFlags + ); + } + /// + /// Wraps necessary functions imported from User32.dll. Code courtesy of MSDN Cold Rooster Consulting example. + /// + static class User32 + { + /// + /// Provides access to function required to delete handle. This method is used internally + /// and is not required to be called separately. + /// + /// Pointer to icon handle. + /// N/A + [DllImport("User32.dll")] + public static extern int DestroyIcon(IntPtr hIcon); + } + } +} diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Storage/Images/file.png b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Storage/Images/file.png new file mode 100644 index 000000000..a8cf88667 Binary files /dev/null and b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Storage/Images/file.png differ diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Storage/Images/folder.png b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Storage/Images/folder.png new file mode 100644 index 000000000..1d32e80bf Binary files /dev/null and b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Storage/Images/folder.png differ diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Storage/Tango.MachineStudio.Storage.csproj b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Storage/Tango.MachineStudio.Storage.csproj index e2f41df43..0ce2a1cb4 100644 --- a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Storage/Tango.MachineStudio.Storage.csproj +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Storage/Tango.MachineStudio.Storage.csproj @@ -43,6 +43,7 @@ + ..\..\..\packages\MahApps.Metro.1.5.0\lib\net45\System.Windows.Interactivity.dll @@ -63,6 +64,8 @@ GlobalVersionInfo.cs + + @@ -103,6 +106,10 @@ {a34ee0f0-649d-41c8-8489-b6f1cc6924ee} Tango.Core + + {4206ac58-3b57-4699-8835-90bf6db01a61} + Tango.Integration + {bc932dbd-7cdb-488c-99e4-f02cf441f55e} Tango.Logging @@ -126,9 +133,13 @@ MSBuild:Compile - + + + + + \ No newline at end of file diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Storage/ViewModels/MainViewVM.cs b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Storage/ViewModels/MainViewVM.cs index d7eec4d36..f28783590 100644 --- a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Storage/ViewModels/MainViewVM.cs +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Storage/ViewModels/MainViewVM.cs @@ -3,15 +3,94 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using Tango.Core.Commands; +using Tango.Integration.ExternalBridge; +using Tango.Integration.Storage; using Tango.MachineStudio.Common; +using Tango.MachineStudio.Common.StudioApplication; namespace Tango.MachineStudio.Storage.ViewModels { public class MainViewVM : StudioViewModel { + private IStudioApplicationManager _applicationManager; + private bool _machine_operator_changed = true; + + private StorageManager _storageManager; + public StorageManager StorageManager + { + get { return _storageManager; } + set { _storageManager = value; RaisePropertyChangedAuto(); } + } + + private StorageItem _selectedStorageItem; + public StorageItem SelectedStorageItem + { + get { return _selectedStorageItem; } + set { _selectedStorageItem = value; RaisePropertyChangedAuto(); } + } + + private String _currentPath; + public String CurrentPath + { + get { return _currentPath; } + set { _currentPath = value; RaisePropertyChangedAuto(); } + } + + public RelayCommand BackCommand { get; set; } + + public RelayCommand RefreshCommand { get; set; } + + public RelayCommand GoCommand { get; set; } + + public MainViewVM(IStudioApplicationManager applicationManager) + { + _applicationManager = applicationManager; + _applicationManager.ConnectedMachineChanged += _applicationManager_ConnectedMachineChanged; + + GoCommand = new RelayCommand(NavigateToCurrentPath); + } + + private void _applicationManager_ConnectedMachineChanged(object sender, IExternalBridgeClient e) + { + _machine_operator_changed = true; + + if (IsVisible) + { + Initialize(); + } + } + public override void OnApplicationReady() { } + + public override void OnNavigatedTo() + { + base.OnNavigatedTo(); + + if (_machine_operator_changed) + { + _machine_operator_changed = false; + Initialize(); + } + } + + private async void NavigateToCurrentPath() + { + await StorageManager.GetFolder(CurrentPath); + } + + private async void Initialize() + { + if (_applicationManager.ConnectedMachine != null) + { + StorageManager = _applicationManager.ConnectedMachine.CreateStorageManager(); + await StorageManager.GetStorageDrive(); + await StorageManager.GetRootFolder(); + CurrentPath = StorageManager.StorageDrive.Root; + } + } } } diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Storage/Views/MainView.xaml b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Storage/Views/MainView.xaml index 0350dc789..46ef31c6b 100644 --- a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Storage/Views/MainView.xaml +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Storage/Views/MainView.xaml @@ -5,10 +5,134 @@ xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:vm="clr-namespace:Tango.MachineStudio.Storage.ViewModels" xmlns:global="clr-namespace:Tango.MachineStudio.Storage" + xmlns:mahApps="http://metro.mahapps.com/winfx/xaml/controls" + xmlns:converters="clr-namespace:Tango.SharedUI.Converters;assembly=Tango.SharedUI" + xmlns:localConverters="clr-namespace:Tango.MachineStudio.Storage.Converters" + xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes" xmlns:local="clr-namespace:Tango.MachineStudio.Storage.Views" mc:Ignorable="d" d:DesignHeight="1080" d:DesignWidth="1920" Background="Transparent" d:DataContext="{d:DesignInstance Type=vm:MainViewVM, IsDesignTimeCreatable=False}" DataContext="{x:Static global:ViewModelLocator.MainViewVM}"> + + + + + + - + + + + + + ACTIONS + + + + + + + + + + + + + + + + + + + STORAGE + + + Root: + + + + Capacity: + + + + Free Space: + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + -- cgit v1.3.1