using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Collections.Specialized; 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; using Tango.Touch.Controls; namespace Tango.Explorer { public class ExplorerControl : Control { private bool _changing_current_path; private TouchListBox _listBox; 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 IEnumerable SelectedItems { get { return (IEnumerable)GetValue(SelectedItemsProperty); } set { SetValue(SelectedItemsProperty, value); } } public static readonly DependencyProperty SelectedItemsProperty = DependencyProperty.Register("SelectedItems", typeof(IEnumerable), typeof(ExplorerControl), new PropertyMetadata(null, (d, e) => (d as ExplorerControl).OnSelectedItemsChanged())); 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, (d, e) => (d as ExplorerControl).OnCurrentPathChanged())); 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)); public bool EnableMultiSelect { get { return (bool)GetValue(EnableMultiSelectProperty); } set { SetValue(EnableMultiSelectProperty, value); } } public static readonly DependencyProperty EnableMultiSelectProperty = DependencyProperty.Register("EnableMultiSelect", typeof(bool), typeof(ExplorerControl), new PropertyMetadata(false)); public bool IsMultiSelecting { get { return (bool)GetValue(IsMultiSelectingProperty); } set { SetValue(IsMultiSelectingProperty, value); } } public static readonly DependencyProperty IsMultiSelectingProperty = DependencyProperty.Register("IsMultiSelecting", typeof(bool), typeof(ExplorerControl), new PropertyMetadata(false)); static ExplorerControl() { DefaultStyleKeyProperty.OverrideMetadata(typeof(ExplorerControl), new FrameworkPropertyMetadata(typeof(ExplorerControl))); } public ExplorerControl() { BackCommand = new RelayCommand(NavigateBack); } public override void OnApplyTemplate() { base.OnApplyTemplate(); _listBox = GetTemplateChild("PART_ListBox") as TouchListBox; } private void OnCurrentPathChanged() { if (_changing_current_path) return; if (_listBox != null) { _listBox.IsMultiSelecting = false; } _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 && _listBox != 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 && !_listBox.IsMultiSelecting) { FileSelectedCommand?.Execute(SelectedItem); } } } private void OnSelectedItemsChanged() { if (SelectedItems != null) { if (SelectedItems is INotifyCollectionChanged) { (SelectedItems as INotifyCollectionChanged).CollectionChanged -= ExplorerControl_CollectionChanged; (SelectedItems as INotifyCollectionChanged).CollectionChanged += ExplorerControl_CollectionChanged; } PreventMultiExtensionSelection(); } } private void ExplorerControl_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { PreventMultiExtensionSelection(); } private void PreventMultiExtensionSelection() { if (_listBox != null && SelectedItems != null) { if (_listBox.IsMultiSelecting && _listBox.SelectedItems != null && _listBox.SelectedItems.Count > 1) { var firstItem = _listBox.SelectedItems.OfType().FirstOrDefault(); if (firstItem != null) { foreach (var item in _listBox.SelectedItems.OfType().ToList()) { if (item.Extension.ToLower() != firstItem.Extension.ToLower()) { var listBoxItem = _listBox.GetItems().FirstOrDefault(x => x.DataContext == item); if (listBoxItem != null) { listBoxItem.IsSelected = false; _listBox.SelectedItems.Remove(item); } } } } } } } public void NavigateBack() { if (CurrentFolder != null) { var parentPath = CurrentFolder.GetParentPath(); if (parentPath != null) { CurrentFolder = ExplorerFolderItem.LoadFromPath(parentPath, Filter); } } } } }