Files
Wino-Mail/Wino.Mail.WinUI/Views/Mail/ComposePage.xaml.cs
T

621 lines
20 KiB
C#
Raw Normal View History

2025-11-15 14:52:01 +01:00
using System;
2025-09-29 11:16:14 +02:00
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Linq;
using System.Threading;
using System.Threading.Tasks;
using CommunityToolkit.Mvvm.Messaging;
using CommunityToolkit.WinUI.Controls;
using EmailValidation;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media.Animation;
2025-09-29 11:16:14 +02:00
using Microsoft.UI.Xaml.Navigation;
using MimeKit;
using Windows.ApplicationModel.DataTransfer;
using Windows.Foundation;
using Windows.Storage;
using Windows.UI.Core.Preview;
using Wino.Core.Domain;
2026-03-08 15:48:11 +01:00
using Wino.Core.Domain.Entities.Mail;
2025-09-29 11:16:14 +02:00
using Wino.Core.Domain.Entities.Shared;
using Wino.Core.Domain.Models.Reader;
using Wino.Mail.ViewModels.Data;
using Wino.Mail.ViewModels.Messages;
2026-03-08 13:21:42 +01:00
using Wino.Mail.WinUI.Controls;
2025-12-26 20:46:48 +01:00
using Wino.Mail.WinUI.Extensions;
using Wino.Mail.WinUI.Interfaces;
using Wino.Mail.WinUI.Models;
2025-09-29 11:16:14 +02:00
using Wino.Messaging.Client.Mails;
using Wino.Messaging.Client.Shell;
using Wino.Views.Abstract;
2025-12-26 20:46:48 +01:00
namespace Wino.Views.Mail;
2025-09-29 11:16:14 +02:00
public sealed partial class ComposePage : ComposePageAbstract,
2026-04-03 11:56:25 +02:00
IAiHtmlActionHost,
IPopoutClient,
IRecipient<ApplicationThemeChanged>
2025-09-29 11:16:14 +02:00
{
2026-04-15 02:12:01 +02:00
private const int InitialFocusRetryCount = 3;
private bool _isPoppedOut;
2026-04-15 02:12:01 +02:00
private bool _isInitialFocusHandled;
public bool SupportsPopOut => !_isPoppedOut;
public event EventHandler<PopOutRequestedEventArgs>? PopOutRequested;
public event EventHandler<PopoutHostActionRequestedEventArgs>? HostActionRequested;
2025-09-29 11:16:14 +02:00
public WebView2 GetWebView() => WebViewEditor.GetUnderlyingWebView();
2026-04-08 15:31:14 +02:00
public Visibility GetAiActionsToggleVisibility(bool isHidden) => isHidden ? Visibility.Collapsed : Visibility.Visible;
public Visibility GetPopOutButtonVisibility() => SupportsPopOut ? Visibility.Visible : Visibility.Collapsed;
2026-04-08 15:31:14 +02:00
public Visibility GetAiActionsPanelVisibility(bool? isChecked, bool isHidden)
=> !isHidden && isChecked == true ? Visibility.Visible : Visibility.Collapsed;
2026-04-03 11:56:25 +02:00
2025-09-29 11:16:14 +02:00
private readonly List<IDisposable> _disposables = [];
public ComposePage()
{
InitializeComponent();
ViewModel.CloseRequested += ViewModel_CloseRequested;
}
public HostedPopoutDescriptor GetPopoutDescriptor()
{
var title = string.IsNullOrWhiteSpace(ViewModel.Subject) ? Translator.Draft : ViewModel.Subject;
var draftId = ViewModel.CurrentMailDraftItem?.MailCopy?.UniqueId.ToString("N") ?? title;
return new HostedPopoutDescriptor(
$"compose-{draftId}",
title,
1180,
860,
760,
600,
nameof(ComposePage));
}
public void OnPopoutStateChanged(bool isPoppedOut)
{
_isPoppedOut = isPoppedOut;
Bindings.Update();
2025-09-29 11:16:14 +02:00
}
2026-03-08 13:21:42 +01:00
public WinoIconGlyph GetEditorThemeIcon(bool isDarkMode) => isDarkMode ? WinoIconGlyph.LightEditor : WinoIconGlyph.DarkEditor;
public string GetEditorThemeToolTip(bool isDarkMode) => isDarkMode ? Translator.Composer_LightTheme : Translator.Composer_DarkTheme;
private void ToggleEditorThemeClicked(object sender, RoutedEventArgs e)
{
WebViewEditor.ToggleEditorTheme();
}
2026-03-08 15:48:11 +01:00
private async void EmailTemplateSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (sender is not ComboBox comboBox || comboBox.SelectedItem is not EmailTemplate template)
return;
await WebViewEditor.RenderHtmlAsync(template.HtmlContent);
comboBox.SelectedItem = null;
}
2025-11-14 18:51:48 +01:00
private async void GlobalFocusManagerGotFocus(object? sender, FocusManagerGotFocusEventArgs e)
2025-09-29 11:16:14 +02:00
{
// In order to delegate cursor to the inner editor for WebView2.
// When the control got focus, we invoke script to focus the editor.
// This is not done on the WebView2 handlers, because somehow it is
// repeatedly focusing itself, even though when it has the focus already.
if (e.NewFocusedElement == WebViewEditor)
{
await WebViewEditor.FocusEditorAsync(true);
}
}
private IDisposable GetSuggestionBoxDisposable(TokenizingTextBox box)
{
return Observable.FromEventPattern<TypedEventHandler<AutoSuggestBox, AutoSuggestBoxTextChangedEventArgs>, AutoSuggestBoxTextChangedEventArgs>(
x => box.TextChanged += x,
x => box.TextChanged -= x)
.Throttle(TimeSpan.FromMilliseconds(120))
2025-11-14 18:51:48 +01:00
.ObserveOn(SynchronizationContext.Current!)
2025-09-29 11:16:14 +02:00
.Subscribe(t =>
{
if (t.EventArgs.Reason == AutoSuggestionBoxTextChangeReason.UserInput)
{
if (t.Sender is AutoSuggestBox senderBox && senderBox.Text.Length >= 2)
{
_ = ViewModel.ContactService.GetAddressInformationAsync(senderBox.Text).ContinueWith(x =>
{
_ = ViewModel.ExecuteUIThread(() =>
{
var addresses = x.Result;
senderBox.ItemsSource = addresses;
});
});
}
}
});
}
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.IsDraggingOverFilesDropZone = 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.IsDraggingOverFilesDropZone = false;
}
private async void OnFileDropGridFileDropped(object sender, DragEventArgs e)
{
try
{
if (e.DataView.Contains(StandardDataFormats.StorageItems))
{
var storageItems = await e.DataView.GetStorageItemsAsync();
var files = storageItems.OfType<StorageFile>();
await AttachFiles(files);
}
}
// State should be reset even when an exception occurs, otherwise the UI will be stuck in a dragging state.
finally
{
ViewModel.IsDraggingOverComposerGrid = false;
ViewModel.IsDraggingOverFilesDropZone = false;
}
}
private void OnImageDropGridDragEnter(object sender, DragEventArgs e)
{
bool isValid = false;
if (e.DataView.Contains(StandardDataFormats.StorageItems))
{
// We can't use async/await here because DragUIOverride becomes inaccessible.
// https://github.com/microsoft/microsoft-ui-xaml/issues/9296
var files = e.DataView.GetStorageItemsAsync().GetAwaiter().GetResult().OfType<StorageFile>();
foreach (var file in files)
{
if (IsValidImageFile(file))
{
isValid = true;
}
}
}
e.AcceptedOperation = isValid ? DataPackageOperation.Copy : DataPackageOperation.None;
if (isValid)
{
ViewModel.IsDraggingOverImagesDropZone = true;
e.DragUIOverride.Caption = Translator.ComposerAttachmentsDragDropAttach_Message;
e.DragUIOverride.IsCaptionVisible = true;
e.DragUIOverride.IsGlyphVisible = true;
e.DragUIOverride.IsContentVisible = true;
}
}
private void OnImageDropGridDragLeave(object sender, DragEventArgs e)
{
ViewModel.IsDraggingOverImagesDropZone = false;
}
private async void OnImageDropGridImageDropped(object sender, DragEventArgs e)
{
try
{
if (e.DataView.Contains(StandardDataFormats.StorageItems))
{
var storageItems = await e.DataView.GetStorageItemsAsync();
var files = storageItems.OfType<StorageFile>();
var imagesInformation = new List<ImageInfo>();
foreach (var file in files)
{
if (IsValidImageFile(file))
{
imagesInformation.Add(new ImageInfo
{
Data = await GetDataURL(file),
Name = file.Name
});
}
}
await WebViewEditor.InsertImagesAsync(imagesInformation);
}
}
// State should be reset even when an exception occurs, otherwise the UI will be stuck in a dragging state.
finally
{
ViewModel.IsDraggingOverComposerGrid = false;
ViewModel.IsDraggingOverImagesDropZone = false;
}
static async Task<string> GetDataURL(StorageFile file)
{
return $"data:image/{file.FileType.Replace(".", "")};base64,{Convert.ToBase64String(await file.ToByteArrayAsync())}";
}
}
private async Task AttachFiles(IEnumerable<StorageFile> files)
{
if (files?.Any() != true) return;
// Convert files to MailAttachmentViewModel.
foreach (var file in files)
{
var sharedFile = await file.ToSharedFileAsync();
ViewModel.IncludedAttachments.Add(new MailAttachmentViewModel(sharedFile));
}
}
private static bool IsValidImageFile(StorageFile file)
{
string[] allowedTypes = [".jpg", ".jpeg", ".png"];
var fileType = file.FileType.ToLower();
return allowedTypes.Contains(fileType);
}
private void DisposeDisposables()
{
if (_disposables.Count != 0)
_disposables.ForEach(a => a.Dispose());
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
FocusManager.GotFocus += GlobalFocusManagerGotFocus;
var webView = GetWebView();
if (webView != null)
{
var anim = ConnectedAnimationService.GetForCurrentView().GetAnimation("WebViewConnectedAnimation");
anim?.TryStart(webView);
}
2025-09-29 11:16:14 +02:00
_disposables.Add(GetSuggestionBoxDisposable(ToBox));
_disposables.Add(GetSuggestionBoxDisposable(CCBox));
_disposables.Add(GetSuggestionBoxDisposable(BccBox));
_disposables.Add(WebViewEditor);
ViewModel.GetHTMLBodyFunction = WebViewEditor.GetHtmlBodyAsync;
2026-04-15 02:12:01 +02:00
ViewModel.RenderHtmlBodyAsyncFunc = RenderComposeHtmlAsync;
2025-09-29 11:16:14 +02:00
}
private void ShowCCBCCClicked(object sender, RoutedEventArgs e)
2025-09-29 11:16:14 +02:00
{
ViewModel.IsCCBCCVisible = true;
2025-09-29 11:16:14 +02:00
}
private void PopOutButton_Click(object sender, RoutedEventArgs e)
2025-09-29 11:16:14 +02:00
{
PopOutRequested?.Invoke(this, PopOutRequestedEventArgs.Default);
}
private void ViewModel_CloseRequested(object? sender, EventArgs e)
{
HostActionRequested?.Invoke(this, new PopoutHostActionRequestedEventArgs(PopoutHostActionKind.CloseHostedInstance));
2025-09-29 11:16:14 +02:00
}
2026-04-03 11:56:25 +02:00
private async void ComposeAiActionsToggleButton_Checked(object sender, RoutedEventArgs e)
{
await ComposeAiActionsPanel.RefreshAvailabilityAsync();
}
2025-09-29 11:16:14 +02:00
private async void TokenItemAdding(TokenizingTextBox sender, TokenItemAddingEventArgs args)
{
// Check is valid email.
if (!EmailValidator.Validate(args.TokenText))
{
args.Cancel = true;
ViewModel.NotifyInvalidEmail(args.TokenText);
return;
}
var deferral = args.GetDeferral();
var addedItem = (sender.Tag?.ToString()) switch
{
"ToBox" => await ViewModel.GetAddressInformationAsync(args.TokenText, ViewModel.ToItems),
"CCBox" => await ViewModel.GetAddressInformationAsync(args.TokenText, ViewModel.CCItems),
"BCCBox" => await ViewModel.GetAddressInformationAsync(args.TokenText, ViewModel.BCCItems),
_ => null
};
if (addedItem == null)
{
args.Cancel = true;
ViewModel.NotifyAddressExists();
}
else
{
args.Item = addedItem;
}
deferral.Complete();
}
void IRecipient<ApplicationThemeChanged>.Receive(ApplicationThemeChanged message)
{
WebViewEditor.IsEditorDarkMode = message.IsUnderlyingThemeDark;
}
public async Task RefreshDraftAsync(MailItemViewModel draftMailItemViewModel)
{
if (draftMailItemViewModel == null || !draftMailItemViewModel.IsDraft) return;
2026-04-15 02:12:01 +02:00
// Reset the initial focus flag for the newly loaded draft.
_isInitialFocusHandled = false;
await ViewModel.RefreshDraftAsync(draftMailItemViewModel);
2026-04-15 02:12:01 +02:00
await ApplyInitialFocusAsync();
}
2025-09-29 11:16:14 +02:00
private void ImportanceClicked(object sender, RoutedEventArgs e)
{
ImportanceFlyout.Hide();
ImportanceSplitButton.IsChecked = true;
2025-11-14 18:51:48 +01:00
if (sender is Button senderButton && senderButton.Tag is MessageImportance importance)
2025-09-29 11:16:14 +02:00
{
2025-11-14 18:51:48 +01:00
ViewModel.SelectedMessageImportance = importance;
2025-12-26 20:46:48 +01:00
if (ImportanceSplitButton.Content is Viewbox viewbox &&
viewbox.Child is SymbolIcon symbolIcon &&
2025-11-14 18:51:48 +01:00
senderButton.Content is SymbolIcon contentIcon)
{
symbolIcon.Symbol = contentIcon.Symbol;
}
2025-09-29 11:16:14 +02:00
}
}
private async void AddressBoxLostFocus(object sender, RoutedEventArgs e)
{
// Automatically add current text as item if it is valid mail address.
if (sender is TokenizingTextBox tokenizingTextBox)
{
if (tokenizingTextBox.Items.LastOrDefault() is not ITokenStringContainer info) return;
var currentText = info.Text;
if (!string.IsNullOrEmpty(currentText) && EmailValidator.Validate(currentText))
{
var addressCollection = tokenizingTextBox.Tag?.ToString() switch
{
"ToBox" => ViewModel.ToItems,
"CCBox" => ViewModel.CCItems,
"BCCBox" => ViewModel.BCCItems,
_ => null
};
2025-11-14 18:51:48 +01:00
AccountContact? addedItem = null;
2025-09-29 11:16:14 +02:00
if (addressCollection != null)
addedItem = await ViewModel.GetAddressInformationAsync(currentText, addressCollection);
// Item has already been added.
if (addedItem == null)
{
tokenizingTextBox.Text = string.Empty;
}
else if (addressCollection != null)
{
addressCollection.Add(addedItem);
tokenizingTextBox.Text = string.Empty;
}
}
}
}
private void ComposerLoaded(object sender, RoutedEventArgs e)
{
2026-04-15 02:12:01 +02:00
if (ShouldFocusRecipients())
{
ToBox.Focus(FocusState.Programmatic);
}
2025-09-29 11:16:14 +02:00
}
private void CCBBCGotFocus(object sender, RoutedEventArgs e)
{
2026-04-15 02:12:01 +02:00
if (ShouldFocusRecipients() && !_isInitialFocusHandled)
2025-09-29 11:16:14 +02:00
{
2026-04-15 02:12:01 +02:00
_isInitialFocusHandled = true;
2025-09-29 11:16:14 +02:00
ToBox.Focus(FocusState.Programmatic);
}
}
protected override async void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
base.OnNavigatingFrom(e);
FocusManager.GotFocus -= GlobalFocusManagerGotFocus;
2026-04-03 11:56:25 +02:00
ComposeAiActionsPanel.CancelPendingOperation();
2025-09-29 11:16:14 +02:00
await ViewModel.UpdateMimeChangesAsync();
ViewModel.RenderHtmlBodyAsyncFunc = null;
2025-09-29 11:16:14 +02:00
DisposeDisposables();
}
2025-11-01 12:10:44 +01:00
2026-04-03 11:56:25 +02:00
public async Task<string?> GetCurrentHtmlAsync(CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
var html = await WebViewEditor.GetHtmlBodyAsync();
cancellationToken.ThrowIfCancellationRequested();
return html;
}
public async Task ApplyHtmlResultAsync(string html, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
await WebViewEditor.RenderHtmlAsync(html);
cancellationToken.ThrowIfCancellationRequested();
}
public Task<string?> TryGetCachedTranslationHtmlAsync(string languageCode, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
return Task.FromResult<string?>(null);
}
public Task SaveCachedTranslationHtmlAsync(string languageCode, string html, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
return Task.CompletedTask;
}
public Task<string?> TryGetCachedSummaryTextAsync(CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
return Task.FromResult<string?>(null);
}
public Task SaveCachedSummaryTextAsync(string summary, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
return Task.CompletedTask;
}
public string GetSuggestedSummaryFileName() => "email-summary.txt";
2025-11-16 00:23:23 +01:00
private void OpenAttachment_Click(object sender, RoutedEventArgs e)
{
if (sender is MenuFlyoutItem item && item.CommandParameter is MailAttachmentViewModel attachment)
{
ViewModel.OpenAttachmentCommand.Execute(attachment);
}
}
private void SaveAttachment_Click(object sender, RoutedEventArgs e)
{
if (sender is MenuFlyoutItem item && item.CommandParameter is MailAttachmentViewModel attachment)
{
ViewModel.SaveAttachmentCommand.Execute(attachment);
}
}
private void RemoveAttachment_Click(object sender, RoutedEventArgs e)
{
if (sender is Button button && button.CommandParameter is MailAttachmentViewModel attachment)
{
ViewModel.RemoveAttachmentCommand.Execute(attachment);
}
}
2025-11-01 12:10:44 +01:00
protected override void RegisterRecipients()
{
base.RegisterRecipients();
WeakReferenceMessenger.Default.Register<ApplicationThemeChanged>(this);
}
protected override void UnregisterRecipients()
{
base.UnregisterRecipients();
WeakReferenceMessenger.Default.Unregister<ApplicationThemeChanged>(this);
}
// TODO: Save mime on closing the app.
2025-09-29 11:16:14 +02:00
private async void OnClose(object sender, SystemNavigationCloseRequestedPreviewEventArgs e)
{
var deferral = e.GetDeferral();
try
{
await ViewModel.UpdateMimeChangesAsync();
}
finally { deferral.Complete(); }
}
2026-04-15 02:12:01 +02:00
private bool ShouldFocusRecipients()
=> !ShouldFocusEditor();
private bool ShouldFocusEditor()
{
var inReplyTo = ViewModel.CurrentMimeMessage?.InReplyTo;
if (string.IsNullOrWhiteSpace(inReplyTo))
{
inReplyTo = ViewModel.CurrentMailDraftItem?.MailCopy?.InReplyTo;
}
if (string.IsNullOrWhiteSpace(inReplyTo) && ViewModel.CurrentMimeMessage?.Headers.Contains(HeaderId.InReplyTo) == true)
{
inReplyTo = ViewModel.CurrentMimeMessage.Headers[HeaderId.InReplyTo];
}
return !string.IsNullOrWhiteSpace(inReplyTo);
}
private async Task ApplyInitialFocusAsync()
{
if (_isInitialFocusHandled)
{
return;
}
_isInitialFocusHandled = true;
for (var attempt = 0; attempt < InitialFocusRetryCount; attempt++)
{
if (ShouldFocusEditor())
{
await WebViewEditor.FocusEditorAsync(true);
if (FocusManager.GetFocusedElement(XamlRoot) is WebView2)
{
return;
}
}
else
{
ToBox.Focus(FocusState.Programmatic);
if (FocusManager.GetFocusedElement(XamlRoot) == ToBox)
{
return;
}
}
await Task.Delay(TimeSpan.FromMilliseconds(50));
}
}
private async Task RenderComposeHtmlAsync(string html)
{
await WebViewEditor.RenderHtmlAsync(html);
await ApplyInitialFocusAsync();
}
2025-09-29 11:16:14 +02:00
}