From 57e31c1dfb16651d3e8c113ada9d22e816893f78 Mon Sep 17 00:00:00 2001 From: Aleh Khantsevich Date: Wed, 22 May 2024 02:10:14 +0200 Subject: [PATCH] Added attachments drag & drop support --- .../Translations/en_US/resources.json | 2 + Wino.Core.Domain/Translator.Designer.cs | 10 +++ Wino.Mail.ViewModels/ComposePageViewModel.cs | 6 ++ Wino.Mail/Views/ComposePage.xaml | 48 +++++++++++++- Wino.Mail/Views/ComposePage.xaml.cs | 62 ++++++++++++++++--- 5 files changed, 116 insertions(+), 12 deletions(-) diff --git a/Wino.Core.Domain/Translations/en_US/resources.json b/Wino.Core.Domain/Translations/en_US/resources.json index c3341e98..2359418a 100644 --- a/Wino.Core.Domain/Translations/en_US/resources.json +++ b/Wino.Core.Domain/Translations/en_US/resources.json @@ -46,6 +46,8 @@ "ClipboardTextCopied_Title": "Copied", "ClipboardTextCopyFailed_Message": "Failed to copy {0} to clipboard.", "ComposerToPlaceholder": "click enter to input addresses", + "ComposerAttachmentsDropZone_Message": "Drop your files here", + "ComposerAttachmentsDragDropAttach_Message": "Attach", "CustomThemeBuilder_AccentColorDescription": "Set custom accent color if you wish. Not selecting a color will use your Windows accent color.", "CustomThemeBuilder_AccentColorTitle": "Accent color", "CustomThemeBuilder_PickColor": "Pick", diff --git a/Wino.Core.Domain/Translator.Designer.cs b/Wino.Core.Domain/Translator.Designer.cs index c244e57f..ff5289a7 100644 --- a/Wino.Core.Domain/Translator.Designer.cs +++ b/Wino.Core.Domain/Translator.Designer.cs @@ -253,6 +253,16 @@ namespace Wino.Core.Domain /// public static string ComposerToPlaceholder => Resources.GetTranslatedString(@"ComposerToPlaceholder"); + /// + /// Drop your files here + /// + public static string ComposerAttachmentsDropZone_Message => Resources.GetTranslatedString(@"ComposerAttachmentsDropZone_Message"); + + /// + /// Attach + /// + public static string ComposerAttachmentsDragDropAttach_Message => Resources.GetTranslatedString(@"ComposerAttachmentsDragDropAttach_Message"); + /// /// Set custom accent color if you wish. Not selecting a color will use your Windows accent color. /// diff --git a/Wino.Mail.ViewModels/ComposePageViewModel.cs b/Wino.Mail.ViewModels/ComposePageViewModel.cs index e2e30b34..3d7c03b4 100644 --- a/Wino.Mail.ViewModels/ComposePageViewModel.cs +++ b/Wino.Mail.ViewModels/ComposePageViewModel.cs @@ -64,6 +64,12 @@ namespace Wino.Mail.ViewModels [ObservableProperty] private MailAccount composingAccount; + [ObservableProperty] + private bool isDraggingOverComposerGrid; + + [ObservableProperty] + private bool isDraggingOverDropZone; + public ObservableCollection IncludedAttachments { get; set; } = new ObservableCollection(); public ObservableCollection Accounts { get; set; } = new ObservableCollection(); diff --git a/Wino.Mail/Views/ComposePage.xaml b/Wino.Mail/Views/ComposePage.xaml index 432683ce..8f947c61 100644 --- a/Wino.Mail/Views/ComposePage.xaml +++ b/Wino.Mail/Views/ComposePage.xaml @@ -107,7 +107,10 @@ - + Transparent @@ -620,9 +623,36 @@ + - - + + + + + + + + + + + + + + + + + + diff --git a/Wino.Mail/Views/ComposePage.xaml.cs b/Wino.Mail/Views/ComposePage.xaml.cs index 921cd096..33a67689 100644 --- a/Wino.Mail/Views/ComposePage.xaml.cs +++ b/Wino.Mail/Views/ComposePage.xaml.cs @@ -13,13 +13,16 @@ using Microsoft.UI.Xaml.Controls; using Microsoft.Web.WebView2.Core; using MimeKit; using Newtonsoft.Json; +using Windows.ApplicationModel.DataTransfer; using Windows.Foundation; +using Windows.Storage; using Windows.Storage.Pickers; using Windows.UI.ViewManagement.Core; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Media.Animation; using Windows.UI.Xaml.Navigation; +using Wino.Core.Domain; using Wino.Core.Domain.Entities; using Wino.Core.Domain.Enums; using Wino.Core.Domain.Interfaces; @@ -105,20 +108,61 @@ namespace Wino.Views picker.FileTypeFilter.Add("*"); var files = await picker.PickMultipleFilesAsync(); - if (files == null) return; + await AttachFiles(files); + } + + private void OnComposeGridDragOver(object sender, DragEventArgs e) + { + ViewModel.IsDraggingOverComposerGrid = true; + } + + private void OnComposeGridDragLeave(object sender, DragEventArgs e) + { + ViewModel.IsDraggingOverComposerGrid = false; + } + + private void OnFileDropGridDragOver(object sender, DragEventArgs e) + { + ViewModel.IsDraggingOverDropZone = true; + + e.AcceptedOperation = DataPackageOperation.Copy; + e.DragUIOverride.Caption = Translator.ComposerAttachmentsDragDropAttach_Message; + e.DragUIOverride.IsCaptionVisible = true; + e.DragUIOverride.IsGlyphVisible = true; + e.DragUIOverride.IsContentVisible = true; + } + + private void OnFileDropGridDragLeave(object sender, DragEventArgs e) + { + ViewModel.IsDraggingOverDropZone = false; + } + + private async void OnFileDropGridFileDropped(object sender, DragEventArgs e) + { + if (e.DataView.Contains(StandardDataFormats.StorageItems)) + { + var storageItems = await e.DataView.GetStorageItemsAsync(); + var files = storageItems.OfType(); + + await AttachFiles(files); + } + + ViewModel.IsDraggingOverComposerGrid = false; + ViewModel.IsDraggingOverDropZone = false; + } + + private async Task AttachFiles(IEnumerable files) + { + if (files?.Any() != true) return; // Convert files to MailAttachmentViewModel. - - if (files.Any()) + foreach (var file in files) { - foreach (var file in files) + if (!ViewModel.IncludedAttachments.Any(a => a.FileName == file.Path)) { - if (!ViewModel.IncludedAttachments.Any(a => a.FileName == file.Path)) - { - var attachmentViewModel = await file.ToAttachmentViewModelAsync(); + var attachmentViewModel = await file.ToAttachmentViewModelAsync(); - ViewModel.IncludedAttachments.Add(attachmentViewModel); - } + ViewModel.IncludedAttachments.Add(attachmentViewModel); } } }