Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
c32b348
Alter clipboard to support multiple formats
minetoblend Mar 23, 2024
95116ea
Add custom format support to `HeadlessClipboard`
minetoblend Mar 23, 2024
cdd0c0c
Add in-memory fallback for custom clipboard format to `MacOSClipboard`
minetoblend Mar 23, 2024
16b00d9
Add in-memory fallback for custom clipboard format to `SDL2Clipboard`
minetoblend Mar 23, 2024
d149485
Add support for custom clipboard formats & web custom formats to `Win…
minetoblend Mar 23, 2024
99179b3
Add in-memory fallback for custom clipboard format to `AndroidClipboard`
minetoblend Mar 23, 2024
ef661c9
Add comment linking to web custom format explainer
minetoblend Mar 23, 2024
1b7ff7c
Add doc comments to clipboard entry classes
minetoblend Mar 23, 2024
38b11cf
Fix comments in `Clipboard`
minetoblend Mar 23, 2024
5f7d94d
Undo change to how pointers are freed in `WindowsClipboard.setClipboard`
minetoblend Mar 24, 2024
744d72d
Simplify the data structures used to represent multiple clipboard values
minetoblend Mar 24, 2024
beb1d98
Always free pointer after registering clipboard format
Mar 26, 2024
27efe4c
Refactor logic to create web custom format clipboard entries
Mar 26, 2024
ae106b3
Add documentation explaining the clipboard entries created to support…
Mar 26, 2024
def378e
Use consistent ordering for method parameters
Mar 26, 2024
51b5fbc
Use UTF8 instead of ansi for web custom format clipboard entries
Mar 26, 2024
825b952
Rename variable
Mar 26, 2024
74a35fa
Clean up `ClipboardData` and `Clipboard`
Mar 26, 2024
7e660f0
Rename variables to be consistent between methods
Mar 26, 2024
3398cad
Add doc comment explaining the web custom format fallback when readin…
Mar 26, 2024
48115c3
Add documentation for `SetData` return value
Mar 26, 2024
6fa516d
Fix formatting for `ClipboardData` values in `Clipboard`
Mar 26, 2024
ac78808
Handle cases where registering clipboard format fails
Mar 26, 2024
c758183
Add logging for cases where calls to win32 clipboard api fail
Mar 26, 2024
629ddff
Add logging for cases where calls to win32 clipboard api fail when re…
Mar 26, 2024
7e4231a
Add `SetLastError` flag to dll imports
minetoblend Mar 28, 2024
8081b3c
Fix gibberish in doc comment
minetoblend Mar 28, 2024
e0fd3fb
Rename `customFormats` to `registeredFormatIdentifiers`
minetoblend Mar 28, 2024
87f243b
Use `TryGetValue` to retrieve values from web custom format map
Mar 29, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Simplify the data structures used to represent multiple clipboard values
  • Loading branch information
minetoblend committed Mar 24, 2024
commit 744d72db533a7271fb959e879a867c69ea567883
25 changes: 8 additions & 17 deletions osu.Framework.Android/AndroidClipboard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Collections.Generic;
using Android.Content;
using NuGet.Packaging;
using osu.Framework.Platform;
using SixLabors.ImageSharp;

Expand All @@ -22,36 +23,26 @@ public AndroidClipboard(AndroidGameView view)

public override Image<TPixel>? GetImage<TPixel>() => null;

public override bool SetData(params ClipboardEntry[] entries)
public override bool SetData(ClipboardData data)
{
if (clipboardManager == null)
return false;

customFormatValues.Clear();
clipboardManager.PrimaryClip = null;

if (entries.Length == 0)
if (data.IsEmpty())
return false;

bool success = true;

foreach (var entry in entries)
{
switch (entry)
{
case ClipboardTextEntry textEntry:
clipboardManager.PrimaryClip = ClipData.NewPlainText(null, textEntry.Value);
break;
if (data.Text != null)
clipboardManager.PrimaryClip = ClipData.NewPlainText(null, data.Text);

case ClipboardCustomEntry customEntry:
customFormatValues[customEntry.Format] = customFormatValues[customEntry.Value];
break;
if (data.Image != null)
success = false;

default:
success = false;
break;
}
}
customFormatValues.AddRange(data.CustomFormatValues);

return success;
}
Expand Down
21 changes: 14 additions & 7 deletions osu.Framework/Platform/Clipboard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ public abstract class Clipboard
public void SetText(string text)
{
SetData(
new ClipboardTextEntry(text)
new ClipboardData
{
Text = text
}
);
}

Expand All @@ -41,7 +44,10 @@ public void SetText(string text)
public bool SetImage(Image image)
{
return SetData(
new ClipboardImageEntry(image)
new ClipboardData
{
Image = image
}
);
}

Expand All @@ -58,15 +64,16 @@ public bool SetImage(Image image)
/// <param name="text">Text to copy to the clipboard</param>
public void SetCustom(string format, string text)
{
SetData(
new ClipboardCustomEntry(format, text)
);
var data = new ClipboardData();
data.AddCustom(format, text);

SetData(data);
}

/// <summary>
/// Copy multiple values to the clipboard
/// </summary>
/// <param name="entries">Entries to copy the clipboard</param>
public abstract bool SetData(params ClipboardEntry[] entries);
/// <param name="data">Data to copy the clipboard</param>
public abstract bool SetData(ClipboardData data);
}
}
20 changes: 0 additions & 20 deletions osu.Framework/Platform/ClipboardCustomEntry.cs

This file was deleted.

53 changes: 53 additions & 0 deletions osu.Framework/Platform/ClipboardData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using System.Collections.Generic;
using SixLabors.ImageSharp;

namespace osu.Framework.Platform
{
/// <summary>
/// Holds multiple values that can be copied to the clipboard
/// </summary>
public struct ClipboardData
{
/// <summary>
/// Text to be stored in the default plaintext format in the clipboard.
/// </summary>
public string? Text;

/// <summary>
/// Image to be stored in the default image format in the clipboard.
/// </summary>
public Image? Image;

/// <summary>
/// Values to be stored as custom formats on the clipboard.
/// </summary>
public readonly Dictionary<string, string> CustomFormatValues = new Dictionary<string, string>();

public ClipboardData()
{
Text = null;
Image = null;
}

/// <summary>
/// Adds a clipboard entry with a custom format
/// </summary>
/// <param name="format">format of the clipboard entry</param>
/// <param name="value">value of the clipboard entry</param>
public void AddCustom(string format, string value)
{
CustomFormatValues[format] = value;
}

/// <summary>
/// Returns true if no data is present
/// </summary>
public bool IsEmpty()
{
return Text == null && Image == null && CustomFormatValues.Count == 0;
}
}
}
12 changes: 0 additions & 12 deletions osu.Framework/Platform/ClipboardEntry.cs

This file was deleted.

20 changes: 0 additions & 20 deletions osu.Framework/Platform/ClipboardImageEntry.cs

This file was deleted.

18 changes: 0 additions & 18 deletions osu.Framework/Platform/ClipboardTextEntry.cs

This file was deleted.

25 changes: 5 additions & 20 deletions osu.Framework/Platform/HeadlessClipboard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// See the LICENCE file in the repository root for full licence text.

using System.Collections.Generic;
using NuGet.Packaging;
using SixLabors.ImageSharp;

namespace osu.Framework.Platform
Expand All @@ -27,29 +28,13 @@ public class HeadlessClipboard : Clipboard
return customValues[format];
}

public override bool SetData(params ClipboardEntry[] entries)
public override bool SetData(ClipboardData data)
{
clipboardText = null;
clipboardImage = null;
clipboardText = data.Text;
clipboardImage = data.Image;
customValues.Clear();

foreach (var entry in entries)
{
switch (entry)
{
case ClipboardTextEntry textEntry:
clipboardText = textEntry.Value;
break;

case ClipboardImageEntry imageEntry:
clipboardImage = imageEntry.Value;
break;

case ClipboardCustomEntry customEntry:
customValues[customEntry.Format] = customEntry.Value;
break;
}
}
customValues.AddRange(data.CustomFormatValues);

return true;
}
Expand Down
27 changes: 10 additions & 17 deletions osu.Framework/Platform/MacOS/MacOSClipboard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using NuGet.Packaging;
using osu.Framework.Platform.MacOS.Native;
using SixLabors.ImageSharp;

Expand Down Expand Up @@ -36,30 +37,22 @@ private IntPtr getFromPasteboard(IntPtr @class)
return objects?.Length > 0 ? objects[0] : IntPtr.Zero;
}

public override bool SetData(params ClipboardEntry[] entries)
public override bool SetData(ClipboardData data)
{
generalPasteboard.ClearContents();
customFormatValues.Clear();

foreach (var entry in entries)
{
switch (entry)
{
case ClipboardTextEntry textEntry:
setToPasteboard(Cocoa.ToNSString(textEntry.Value));
break;
bool success = true;

case ClipboardImageEntry imageEntry:
setToPasteboard(Cocoa.ToNSImage(imageEntry.Value));
break;
if (data.Text != null)
success &= setToPasteboard(Cocoa.ToNSString(data.Text));

case ClipboardCustomEntry customEntry:
customFormatValues[customEntry.Format] = customEntry.Value;
break;
}
}
if (data.Image != null)
success &= setToPasteboard(Cocoa.ToNSImage(data.Image));

return true;
customFormatValues.AddRange(data.CustomFormatValues);

return success;
}

private bool setToPasteboard(IntPtr handle)
Expand Down
30 changes: 11 additions & 19 deletions osu.Framework/Platform/SDL2/SDL2Clipboard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// See the LICENCE file in the repository root for full licence text.

using System.Collections.Generic;
using System.Linq;
using NuGet.Packaging;
using SDL2;
using SixLabors.ImageSharp;

Expand All @@ -27,28 +27,20 @@ public class SDL2Clipboard : Clipboard
return customFormatValues[format];
}

public override bool SetData(params ClipboardEntry[] entries)
public override bool SetData(ClipboardData data)
{
customFormatValues.Clear();

if (entries.Any(e => e is ClipboardImageEntry))
{
if (data.IsEmpty())
return false;
}

foreach (var entry in entries)
{
switch (entry)
{
case ClipboardTextEntry textEntry:
SDL.SDL_SetClipboardText(textEntry.Value);
break;

case ClipboardCustomEntry customEntry:
customFormatValues[customEntry.Format] = customEntry.Value;
break;
}
}

if (data.Image != null)
return false;

if (data.Text != null)
SDL.SDL_SetClipboardText(data.Text);

customFormatValues.AddRange(data.CustomFormatValues);

return true;
}
Expand Down
Loading