Skip to content

Commit

Permalink
Added pasting to infinite canvas from within collection preview
Browse files Browse the repository at this point in the history
  • Loading branch information
d2dyno1 committed Nov 30, 2022
1 parent bdb5c43 commit a22536b
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 113 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using Microsoft.UI.Xaml.Controls;
using System.Collections.Specialized;
using System.Collections;

using System.Linq;
using ClipboardCanvas.Helpers;
using ClipboardCanvas.ViewModels.ContextMenu;

Expand Down Expand Up @@ -36,7 +36,7 @@ private void UpdateMenuFlyoutItems(MenuFlyout menuFlyout, IList<BaseMenuFlyoutIt
return;
}

IEnumerable<MenuFlyoutItemBase> flyoutItems = FlyoutHelpers.GetMenuFlyoutItems(collection);
IEnumerable<MenuFlyoutItemBase> flyoutItems = FlyoutHelpers.GetMenuFlyoutItems(newItems.Cast<BaseMenuFlyoutItemViewModel>().ToList());

menuFlyout.Items.Clear();
foreach (var item in flyoutItems)
Expand Down
55 changes: 27 additions & 28 deletions ClipboardCanvas/CanavsPasteModels/ImagePasteModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,46 +73,45 @@ protected override async Task<SafeWrapperResult> SetDataFromDataPackage(DataPack
async () => await (RandomAccessStreamReference.CreateFromUri(new Uri(url))).OpenReadAsync());
}
}
else if (dataPackage.Contains(StandardDataFormats.Bitmap))
else if (dataPackage.Contains(StandardDataFormats.Bitmap)) // Just image
{
if (dataPackage.Contains(StandardDataFormats.StorageItems)) // File to image
{
SafeWrapper<IReadOnlyList<IStorageItem>> items = await dataPackage.SafeGetStorageItemsAsync();
SafeWrapper<RandomAccessStreamReference> bitmap = await dataPackage.SafeGetBitmapAsync();
SafeWrapper<Uri> uri = await dataPackage.SafeGetUriAsync();

if (!items)
if (uri)
{
customName = Path.GetFileName(uri.Result.LocalPath);
if (!Path.HasExtension(customName))
{
return items;
customName = $"{customName}.png";
}

IsContentAsReference = _wasPastedAsReference;

this.pastedItem = items.Result.First();
return await SetDataFromExistingItem(pastedItem);
}
else // Just image

if (!bitmap)
{
SafeWrapper<RandomAccessStreamReference> bitmap = await dataPackage.SafeGetBitmapAsync();
SafeWrapper<Uri> uri = await dataPackage.SafeGetUriAsync();
return bitmap;
}

if (uri)
{
customName = Path.GetFileName(uri.Result.LocalPath);
if (!Path.HasExtension(customName))
{
customName = $"{customName}.png";
}
}
openedStream = await SafeWrapperRoutines.SafeWrapAsync(
() => bitmap.Result.OpenReadAsync().AsTask());

if (!bitmap)
{
return bitmap;
}
}
else if (dataPackage.Contains(StandardDataFormats.StorageItems)) // File to image
{
SafeWrapper<IReadOnlyList<IStorageItem>> items = await dataPackage.SafeGetStorageItemsAsync();

openedStream = await SafeWrapperRoutines.SafeWrapAsync(
() => bitmap.Result.OpenReadAsync().AsTask());
if (!items)
{
return items;
}

IsContentAsReference = _wasPastedAsReference;

this.pastedItem = items.Result.First();
return await SetDataFromExistingItem(pastedItem);
}

openedStream ??= new SafeWrapper<IRandomAccessStreamWithContentType>(null, ItemIsNotAFileResult);
if (!openedStream)
{
openedStream.Result?.Dispose();
Expand Down
6 changes: 5 additions & 1 deletion ClipboardCanvas/Pages/CollectionPreviewPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:animvis="using:Microsoft.UI.Xaml.Controls.AnimatedVisuals"
xmlns:ap="using:ClipboardCanvas.AttachedProperties"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:gxt="using:ClipboardCanvas.GlobalizationExtensions"
xmlns:local="using:ClipboardCanvas.Pages"
Expand Down Expand Up @@ -130,6 +131,7 @@
Grid.RowSpan="2"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Background="Transparent"
CornerRadius="4">

<Viewbox
Expand All @@ -145,11 +147,13 @@
Background="Transparent" />
</Grid>
</Viewbox>
<Grid.ContextFlyout>
<MenuFlyout ap:FlyoutItemsSourceAttachedProperty.Value="{x:Bind ContextMenuItems, Mode=OneWay}" />
</Grid.ContextFlyout>
</Grid>

<!-- Shadow and Filename -->
<Grid Grid.Row="1">

<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="Auto" />
Expand Down
76 changes: 75 additions & 1 deletion ClipboardCanvas/ViewModels/CollectionPreviewItemViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
using System;
using System.Collections.ObjectModel;
using System.IO;
using System.Threading.Tasks;
using CommunityToolkit.Mvvm.ComponentModel;
using System.Threading;

using Windows.ApplicationModel.DataTransfer;
using Windows.Graphics.Printing3D;
using Windows.Storage;
using ClipboardCanvas.CanvasFileReceivers;
using ClipboardCanvas.Contexts.Operations;
using ClipboardCanvas.DataModels;
using ClipboardCanvas.DataModels.ContentDataModels;
using ClipboardCanvas.Extensions;
using ClipboardCanvas.GlobalizationExtensions;
using ClipboardCanvas.Interfaces.Search;
using ClipboardCanvas.Models;
using ClipboardCanvas.ViewModels.UserControls.Collections;
using ClipboardCanvas.Helpers;
using ClipboardCanvas.ReferenceItems;
using ClipboardCanvas.Services;
using ClipboardCanvas.ViewModels.ContextMenu;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.DependencyInjection;

namespace ClipboardCanvas.ViewModels
{
Expand All @@ -17,6 +31,8 @@ public class CollectionPreviewItemViewModel : ObservableObject, ISearchItem, IDi

private CancellationTokenSource _cancellationTokenSource;

private IUserSettingsService UserSettingsService { get; } = Ioc.Default.GetRequiredService<IUserSettingsService>();

#endregion

#region Public Properties
Expand Down Expand Up @@ -49,6 +65,9 @@ public bool IsCanvasPreviewVisible
set => SetProperty(ref _IsCanvasPreviewVisible, value);
}

public ObservableCollection<BaseMenuFlyoutItemViewModel> ContextMenuItems { get; }


public TwoWayPropertyUpdater<IReadOnlyCanvasPreviewModel> TwoWayReadOnlyCanvasPreview { get; set; }

public IReadOnlyCanvasPreviewModel ReadOnlyCanvasPreviewModel { get; private set; }
Expand All @@ -64,6 +83,7 @@ public bool IsCanvasPreviewVisible
private CollectionPreviewItemViewModel()
{
this._cancellationTokenSource = new CancellationTokenSource();
this.ContextMenuItems = new();

this.TwoWayReadOnlyCanvasPreview = new TwoWayPropertyUpdater<IReadOnlyCanvasPreviewModel>();
this.TwoWayReadOnlyCanvasPreview.OnPropertyValueUpdatedEvent += TwoWayReadOnlyCanvasPreview_OnPropertyValueUpdatedEvent;
Expand Down Expand Up @@ -126,6 +146,7 @@ public static async Task<CollectionPreviewItemViewModel> GetCollectionPreviewIte
CollectionItemViewModel = collectionItemViewModel
};

await viewModel.LoadContextMenuItems();
await viewModel.UpdateDisplayName();

return viewModel;
Expand All @@ -138,6 +159,59 @@ public async Task UpdateDisplayName()

#endregion

#region Private Helpers

private async Task LoadContextMenuItems()
{
if (!ContextMenuItems.IsEmpty())
return;

var sourceItem = await CollectionItemViewModel.SourceItem;
if (sourceItem is IStorageFolder && sourceItem.Name.EndsWith(Constants.FileSystem.INFINITE_CANVAS_EXTENSION))
{
ContextMenuItems.Add(new MenuFlyoutItemViewModel()
{
Command = new AsyncRelayCommand(PasteToInfiniteCanvas),
IconGlyph = "\uE77F",
Text = "PasteFromClipboard".GetLocalized2()
});
}
else
{
ContextMenuItems.Add(new MenuFlyoutItemViewModel()
{
Command = new AsyncRelayCommand(CopyFile),
IconGlyph = "\uE8C8",
Text = "CopyFile".GetLocalized2()
});
}
}

private async Task CopyFile()
{
DataPackage data = new DataPackage();
bool result = await ReadOnlyCanvasPreviewModel.SetDataToDataPackage(data);
ClipboardHelpers.CopyDataPackage(data);
}

private async Task PasteToInfiniteCanvas()
{
var clipboardData = ClipboardHelpers.GetClipboardData();
if (!clipboardData)
return;

var pastedItemContentType = await BaseContentTypeModel.GetContentTypeFromDataPackage(clipboardData);
if (pastedItemContentType is InvalidContentTypeDataModel)
return;

var canvasItem = new InfiniteCanvasItem(CollectionItemViewModel.AssociatedItem);
var infiniteCanvasFileReceiver = new InfiniteCanvasFileReceiver(canvasItem);
var canvasPasteModel = CanvasHelpers.GetPasteModelFromContentType(pastedItemContentType, infiniteCanvasFileReceiver, new StatusCenterOperationReceiver());
await canvasPasteModel.PasteData(clipboardData, UserSettingsService.AlwaysPasteFilesAsReference, default);
}

#endregion

#region IDisposable

public void Dispose()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -465,11 +465,13 @@ private async void InteractableCanvasModel_OnInfiniteCanvasSaveRequestedEvent(ob
Justification = "<Pending>")]
private async void FilesystemChangeWatcher_OnChangeRegisteredEvent(object sender, ChangeRegisteredEventArgs2 e)
{
// TODO: Regression - e.filesystemChangeReader.ReadBatchAsync() throws E_WRONG_THREAD
try
{
await MainWindow.Instance.DispatcherQueue.EnqueueAsync(async () =>
{
if (e.FullPath.EndsWith(".TMP") || e.FullPath.EndsWith("~tmp"))
return;

// Reflect changes
if (FileHelpers.IsPathEqualExtension(e.FullPath,
Constants.FileSystem.INFINITE_CANVAS_CONFIGURATION_FILE_EXTENSION)
Expand Down
Loading

0 comments on commit a22536b

Please sign in to comment.