using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using Tango.Core.Commands; namespace Tango.Explorer { public class ExplorerControl : Control { private bool _changing_current_path; public String CurrentPath { get { return (String)GetValue(CurrentPathProperty); } set { SetValue(CurrentPathProperty, value); } } public static readonly DependencyProperty CurrentPathProperty = DependencyProperty.Register("CurrentPath", typeof(String), typeof(ExplorerControl), new PropertyMetadata(null, (d, e) => (d as ExplorerControl).OnCurrentPathChanged())); public ExplorerFolderItem CurrentFolder { get { return (ExplorerFolderItem)GetValue(CurrentFolderProperty); } set { SetValue(CurrentFolderProperty, value); } } public static readonly DependencyProperty CurrentFolderProperty = DependencyProperty.Register("CurrentFolder", typeof(ExplorerFolderItem), typeof(ExplorerControl), new PropertyMetadata(null, (d, e) => (d as ExplorerControl).OnCurrentFolderChanged())); public ExplorerItem SelectedItem { get { return (ExplorerItem)GetValue(SelectedItemProperty); } set { SetValue(SelectedItemProperty, value); } } public static readonly DependencyProperty SelectedItemProperty = DependencyProperty.Register("SelectedItem", typeof(ExplorerItem), typeof(ExplorerControl), new PropertyMetadata(null, (d, e) => (d as ExplorerControl).OnSelectedItemChanged())); public RelayCommand BackCommand { get { return (RelayCommand)GetValue(BackCommandProperty); } set { SetValue(BackCommandProperty, value); } } public static readonly DependencyProperty BackCommandProperty = DependencyProperty.Register("BackCommand", typeof(RelayCommand), typeof(ExplorerControl), new PropertyMetadata(null)); public RelayCommand FileSelectedCommand { get { return (RelayCommand)GetValue(FileSelectedCommandProperty); } set { SetValue(FileSelectedCommandProperty, value); } } public static readonly DependencyProperty FileSelectedCommandProperty = DependencyProperty.Register("FileSelectedCommand", typeof(RelayCommand), typeof(ExplorerControl), new PropertyMetadata(null)); public String Filter { get { return (String)GetValue(FilterProperty); } set { SetValue(FilterProperty, value); } } public static readonly DependencyProperty FilterProperty = DependencyProperty.Register("Filter", typeof(String), typeof(ExplorerControl), new PropertyMetadata(null)); public bool EnableFileSelection { get { return (bool)GetValue(EnableFileSelectionProperty); } set { SetValue(EnableFileSelectionProperty, value); } } public static readonly DependencyProperty EnableFileSelectionProperty = DependencyProperty.Register("EnableFileSelection", typeof(bool), typeof(ExplorerControl), new PropertyMetadata(true)); static ExplorerControl() { DefaultStyleKeyProperty.OverrideMetadata(typeof(ExplorerControl), new FrameworkPropertyMetadata(typeof(ExplorerControl))); } public ExplorerControl() { BackCommand = new RelayCommand(NavigateBack); } private void OnCurrentPathChanged() { if (_changing_current_path) return; _changing_current_path = true; if (CurrentPath == null) { CurrentFolder = null; } else if (Directory.Exists(CurrentPath)) { CurrentFolder = ExplorerFolderItem.LoadFromPath(CurrentPath, Filter); } _changing_current_path = false; } private void OnCurrentFolderChanged() { if (_changing_current_path) return; _changing_current_path = true; CurrentPath = CurrentFolder.Path; _changing_current_path = false; } private void OnSelectedItemChanged() { if (SelectedItem != null) { if (SelectedItem is ExplorerFolderItem) { var folder = SelectedItem as ExplorerFolderItem; folder = ExplorerFolderItem.LoadFromPath(folder.Path, Filter); CurrentFolder = folder; SelectedItem = null; } else if (SelectedItem is ExplorerFileItem && EnableFileSelection) { FileSelectedCommand?.Execute(SelectedItem); } } } public void NavigateBack() { if (CurrentFolder != null) { var parentPath = CurrentFolder.GetParentPath(); if (parentPath != null) { CurrentFolder = ExplorerFolderItem.LoadFromPath(parentPath, Filter); } } } } }