|
1 | 1 | // Copyright (c) Files Community |
2 | 2 | // Licensed under the MIT License. |
3 | 3 |
|
| 4 | +using Microsoft.UI.Xaml; |
4 | 5 | using Microsoft.UI.Xaml.Controls; |
| 6 | +using System.Runtime.InteropServices.ComTypes; |
| 7 | +using System.Windows.Input; |
| 8 | +using Vanara.PInvoke; |
| 9 | +using Windows.ApplicationModel.DataTransfer; |
| 10 | +using WinRT; |
5 | 11 |
|
6 | 12 | namespace Files.App.UserControls |
7 | 13 | { |
8 | 14 | public sealed partial class ShelfPane : UserControl |
9 | 15 | { |
10 | 16 | public ShelfPane() |
11 | 17 | { |
| 18 | + // TODO: [Shelf] Remove once view model is connected |
| 19 | + ItemsSource = new ObservableCollection<ShelfItem>(); |
| 20 | + |
12 | 21 | InitializeComponent(); |
13 | 22 | } |
| 23 | + |
| 24 | + private void Shelf_DragOver(object sender, DragEventArgs e) |
| 25 | + { |
| 26 | + if (!FilesystemHelpers.HasDraggedStorageItems(e.DataView)) |
| 27 | + return; |
| 28 | + |
| 29 | + e.Handled = true; |
| 30 | + e.AcceptedOperation = DataPackageOperation.Link; |
| 31 | + } |
| 32 | + |
| 33 | + private async void Shelf_Drop(object sender, DragEventArgs e) |
| 34 | + { |
| 35 | + if (ItemsSource is null) |
| 36 | + return; |
| 37 | + |
| 38 | + // Get items |
| 39 | + var storageService = Ioc.Default.GetRequiredService<IStorageService>(); |
| 40 | + var storageItems = (await FilesystemHelpers.GetDraggedStorageItems(e.DataView)).ToArray(); |
| 41 | + |
| 42 | + // Add to list |
| 43 | + foreach (var item in storageItems) |
| 44 | + { |
| 45 | + var storable = item switch |
| 46 | + { |
| 47 | + StorageFileWithPath => (IStorable?)await storageService.TryGetFileAsync(item.Path), |
| 48 | + StorageFolderWithPath => (IStorable?)await storageService.TryGetFolderAsync(item.Path), |
| 49 | + _ => null |
| 50 | + }; |
| 51 | + |
| 52 | + if (storable is null) |
| 53 | + continue; |
| 54 | + |
| 55 | + var shelfItem = new ShelfItem(storable); |
| 56 | + _ = shelfItem.InitAsync(); |
| 57 | + |
| 58 | + ItemsSource.Add(shelfItem); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + private void ListView_DragItemsStarting(object sender, DragItemsStartingEventArgs e) |
| 63 | + { |
| 64 | + if (ItemsSource is null) |
| 65 | + return; |
| 66 | + |
| 67 | + var shellItemList = SafetyExtensions.IgnoreExceptions(() => ItemsSource.Select(x => new Vanara.Windows.Shell.ShellItem(x.Inner.Id)).ToArray()); |
| 68 | + if (shellItemList?[0].FileSystemPath is not null) |
| 69 | + { |
| 70 | + var iddo = shellItemList[0].Parent?.GetChildrenUIObjects<IDataObject>(HWND.NULL, shellItemList); |
| 71 | + if (iddo is null) |
| 72 | + return; |
| 73 | + |
| 74 | + shellItemList.ForEach(x => x.Dispose()); |
| 75 | + var dataObjectProvider = e.Data.As<Shell32.IDataObjectProvider>(); |
| 76 | + dataObjectProvider.SetDataObject(iddo); |
| 77 | + } |
| 78 | + else |
| 79 | + { |
| 80 | + // Only support IStorageItem capable paths |
| 81 | + var storageItems = ItemsSource.Select(x => VirtualStorageItem.FromPath(x.Inner.Id)); |
| 82 | + e.Data.SetStorageItems(storageItems, false); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + public IList<ShelfItem>? ItemsSource |
| 87 | + { |
| 88 | + get => (IList<ShelfItem>?)GetValue(ItemsSourceProperty); |
| 89 | + set => SetValue(ItemsSourceProperty, value); |
| 90 | + } |
| 91 | + public static readonly DependencyProperty ItemsSourceProperty = |
| 92 | + DependencyProperty.Register(nameof(ItemsSource), typeof(IList<ShelfItem>), typeof(ShelfPane), new PropertyMetadata(null)); |
| 93 | + |
| 94 | + public ICommand? ClearCommand |
| 95 | + { |
| 96 | + get => (ICommand?)GetValue(ClearCommandProperty); |
| 97 | + set => SetValue(ClearCommandProperty, value); |
| 98 | + } |
| 99 | + public static readonly DependencyProperty ClearCommandProperty = |
| 100 | + DependencyProperty.Register(nameof(ClearCommand), typeof(ICommand), typeof(ShelfPane), new PropertyMetadata(null)); |
14 | 101 | } |
15 | 102 | } |
0 commit comments