Skip to content

Commit

Permalink
Clipboard updates: Add image to clipboard and fix pasting and showing…
Browse files Browse the repository at this point in the history
… status when image is from clipboard
  • Loading branch information
Ruben2776 committed Jan 6, 2025
1 parent 8b30fd2 commit bfbe0f6
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 21 deletions.
8 changes: 7 additions & 1 deletion src/PicView.Avalonia.MacOS/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Media.Imaging;
using Avalonia.Threading;
using PicView.Avalonia.ColorManagement;
using PicView.Avalonia.Interfaces;
Expand Down Expand Up @@ -399,11 +400,16 @@ public bool CutFile(string path)
return false;
}

public Task CopyImageToClipboard()
public Task CopyImageToClipboard(Bitmap bitmap)
{
return Task.CompletedTask;
}

public Task<Bitmap?> GetImageFromClipboard()
{
return null;
}

public Task<bool> ExtractWithLocalSoftwareAsync(string path, string tempDirectory)
{
// TODO: Implement ExtractWithLocalSoftwareAsync
Expand Down
11 changes: 9 additions & 2 deletions src/PicView.Avalonia.Win32/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Markup.Xaml;
using Avalonia.Media.Imaging;
using Avalonia.Threading;
using Clowd.Clipboard;
using PicView.Avalonia.ColorManagement;
using PicView.Avalonia.Interfaces;
using PicView.Avalonia.Navigation;
Expand Down Expand Up @@ -439,9 +441,14 @@ public bool CutFile(string path)
return Win32Clipboard.CopyFileToClipboard(true, path);
}

public Task CopyImageToClipboard()
public async Task CopyImageToClipboard(Bitmap bitmap)
{
return Task.CompletedTask;
await ClipboardAvalonia.SetImageAsync(bitmap).ConfigureAwait(false);
}

public async Task<Bitmap?> GetImageFromClipboard()
{
return await ClipboardAvalonia.GetImageAsync().ConfigureAwait(false);
}

public async Task<bool> ExtractWithLocalSoftwareAsync(string path, string tempDirectory)
Expand Down
1 change: 1 addition & 0 deletions src/PicView.Avalonia.Win32/PicView.Avalonia.Win32.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
<ItemGroup>
<PackageReference Include="Avalonia.Desktop" Version="11.2.3" />
<PackageReference Include="Avalonia.Themes.Simple" Version="11.2.3" />
<PackageReference Include="Clowd.Clipboard.Avalonia" Version="1.1.4" />
</ItemGroup>

<ItemGroup>
Expand Down
47 changes: 39 additions & 8 deletions src/PicView.Avalonia/Clipboard/ClipboardHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,31 @@ public static async Task CopyFileToClipboard(string? file, MainViewModel vm)

public static async Task CopyImageToClipboard(MainViewModel vm)
{
// TODO: Implement CopyImageToClipboard
if (Application.Current?.ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime desktop)
{
return;
}
var clipboard = desktop.MainWindow.Clipboard;

if (vm.ImageSource is not Bitmap bitmap)
{
return;
}
await clipboard.ClearAsync();

// Handle for Windows
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
await Task.WhenAll(vm.PlatformService.CopyImageToClipboard(bitmap), CopyAnimation());
return;
}

using var ms = new MemoryStream();
bitmap.Save(ms);

var dataObject = new DataObject();
dataObject.Set("image/png", ms.ToArray());
await Task.WhenAll(clipboard.SetDataObjectAsync(dataObject), CopyAnimation());
}

public static async Task CopyBase64ToClipboard(string path, MainViewModel vm)
Expand Down Expand Up @@ -129,13 +153,6 @@ public static async Task Paste(MainViewModel vm)
return;
}
var clipboard = desktop.MainWindow.Clipboard;
var text = await clipboard.GetTextAsync();
if (text is not null)
{
await NavigationHelper.LoadPicFromStringAsync(text, vm).ConfigureAwait(false);
return;
}

var files = await clipboard.GetDataAsync(DataFormats.Files);
if (files is not null)
{
Expand Down Expand Up @@ -229,6 +246,20 @@ public static async Task Paste(MainViewModel vm)
UpdateImage.SetSingleImage(bitmap, imageType, name, vm);
return;
}

var text = await clipboard.GetTextAsync();
if (!string.IsNullOrWhiteSpace(text))
{
await NavigationHelper.LoadPicFromStringAsync(text, vm).ConfigureAwait(false);
return;
}
// Handle for Windows
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
bitmap = await vm.PlatformService.GetImageFromClipboard();
UpdateImage.SetSingleImage(bitmap, imageType, name, vm);
return;
}
return;

async Task<Bitmap?> GetBitmapFromBytes(string format)
Expand Down
8 changes: 6 additions & 2 deletions src/PicView.Avalonia/Interfaces/IPlatformSpecificService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace PicView.Avalonia.Interfaces;
using Avalonia.Media.Imaging;

namespace PicView.Avalonia.Interfaces;

public interface IPlatformSpecificService
{
Expand Down Expand Up @@ -43,7 +45,9 @@ public interface IPlatformSpecificService

bool CutFile(string path);

Task CopyImageToClipboard();
Task CopyImageToClipboard(Bitmap bitmap);

Task<Bitmap?> GetImageFromClipboard();

Task<bool> ExtractWithLocalSoftwareAsync(string path, string tempDirectory);

Expand Down
6 changes: 3 additions & 3 deletions src/PicView.Avalonia/Navigation/UpdateImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,9 @@ public static void SetSingleImage(object source, ImageType imageType, string nam
}

var singeImageWindowTitles = ImageTitleFormatter.GenerateTitleForSingleImage(width, height, name, 1);
vm.WindowTitle = singeImageWindowTitles.BaseTitle;
vm.Title = singeImageWindowTitles.TitleWithAppName;
vm.TitleTooltip = singeImageWindowTitles.TitleWithAppName;
vm.WindowTitle = singeImageWindowTitles.TitleWithAppName;
vm.Title = singeImageWindowTitles.BaseTitle;
vm.TitleTooltip = singeImageWindowTitles.BaseTitle;
vm.GalleryMargin = new Thickness(0, 0, 0, 0);

vm.PlatformService.StopTaskbarProgress();
Expand Down
6 changes: 1 addition & 5 deletions src/PicView.Avalonia/Views/MainView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -856,11 +856,7 @@
</MenuItem.Icon>

<!-- Copy image -->
<MenuItem
Command="{CompiledBinding CopyImageCommand}"
Header="{CompiledBinding CopyImage,
Mode=OneWay}"
IsEnabled="False">
<MenuItem Command="{CompiledBinding CopyImageCommand}" Header="{CompiledBinding CopyImage, Mode=OneWay}">
<MenuItem.Icon>
<Path
Data="M384 336H192c-8.8 0-16-7.2-16-16V64c0-8.8 7.2-16 16-16l140.1 0L400 115.9V320c0 8.8-7.2 16-16 16zM192 384H384c35.3 0 64-28.7 64-64V115.9c0-12.7-5.1-24.9-14.1-33.9L366.1 14.1c-9-9-21.2-14.1-33.9-14.1H192c-35.3 0-64 28.7-64 64V320c0 35.3 28.7 64 64 64zM64 128c-35.3 0-64 28.7-64 64V448c0 35.3 28.7 64 64 64H256c35.3 0 64-28.7 64-64V416H272v32c0 8.8-7.2 16-16 16H64c-8.8 0-16-7.2-16-16V192c0-8.8 7.2-16 16-16H96V128H64z"
Expand Down

0 comments on commit bfbe0f6

Please sign in to comment.