-
Notifications
You must be signed in to change notification settings - Fork 452
Add support for custom clipboard formats #6223
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
minetoblend
wants to merge
29
commits into
ppy:master
Choose a base branch
from
minetoblend:feature/custom-clipboard-formats
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
c32b348
Alter clipboard to support multiple formats
minetoblend 95116ea
Add custom format support to `HeadlessClipboard`
minetoblend cdd0c0c
Add in-memory fallback for custom clipboard format to `MacOSClipboard`
minetoblend 16b00d9
Add in-memory fallback for custom clipboard format to `SDL2Clipboard`
minetoblend d149485
Add support for custom clipboard formats & web custom formats to `Win…
minetoblend 99179b3
Add in-memory fallback for custom clipboard format to `AndroidClipboard`
minetoblend ef661c9
Add comment linking to web custom format explainer
minetoblend 1b7ff7c
Add doc comments to clipboard entry classes
minetoblend 38b11cf
Fix comments in `Clipboard`
minetoblend 5f7d94d
Undo change to how pointers are freed in `WindowsClipboard.setClipboard`
minetoblend 744d72d
Simplify the data structures used to represent multiple clipboard values
minetoblend beb1d98
Always free pointer after registering clipboard format
27efe4c
Refactor logic to create web custom format clipboard entries
ae106b3
Add documentation explaining the clipboard entries created to support…
def378e
Use consistent ordering for method parameters
51b5fbc
Use UTF8 instead of ansi for web custom format clipboard entries
825b952
Rename variable
74a35fa
Clean up `ClipboardData` and `Clipboard`
7e660f0
Rename variables to be consistent between methods
3398cad
Add doc comment explaining the web custom format fallback when readin…
48115c3
Add documentation for `SetData` return value
6fa516d
Fix formatting for `ClipboardData` values in `Clipboard`
ac78808
Handle cases where registering clipboard format fails
c758183
Add logging for cases where calls to win32 clipboard api fail
629ddff
Add logging for cases where calls to win32 clipboard api fail when re…
7e4231a
Add `SetLastError` flag to dll imports
minetoblend 8081b3c
Fix gibberish in doc comment
minetoblend e0fd3fb
Rename `customFormats` to `registeredFormatIdentifiers`
minetoblend 87f243b
Use `TryGetValue` to retrieve values from web custom format map
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,9 @@ | ||
| // 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 Android.Content; | ||
| using NuGet.Packaging; | ||
| using osu.Framework.Platform; | ||
| using SixLabors.ImageSharp; | ||
|
|
||
|
|
@@ -10,6 +12,7 @@ namespace osu.Framework.Android | |
| public class AndroidClipboard : Clipboard | ||
| { | ||
| private readonly ClipboardManager? clipboardManager; | ||
| private readonly Dictionary<string, string> customFormatValues = new Dictionary<string, string>(); | ||
|
|
||
| public AndroidClipboard(AndroidGameView view) | ||
| { | ||
|
|
@@ -18,14 +21,35 @@ public AndroidClipboard(AndroidGameView view) | |
|
|
||
| public override string? GetText() => clipboardManager?.PrimaryClip?.GetItemAt(0)?.Text; | ||
|
|
||
| public override void SetText(string text) | ||
| public override Image<TPixel>? GetImage<TPixel>() => null; | ||
|
|
||
| public override bool SetData(ClipboardData data) | ||
| { | ||
| if (clipboardManager != null) | ||
| clipboardManager.PrimaryClip = ClipData.NewPlainText(null, text); | ||
| } | ||
| if (clipboardManager == null) | ||
| return false; | ||
|
|
||
| public override Image<TPixel>? GetImage<TPixel>() => null; | ||
| customFormatValues.Clear(); | ||
| clipboardManager.PrimaryClip = null; | ||
|
|
||
| if (data.IsEmpty()) | ||
| return false; | ||
|
|
||
| bool success = true; | ||
|
|
||
| if (data.Text != null) | ||
| clipboardManager.PrimaryClip = ClipData.NewPlainText(null, data.Text); | ||
|
|
||
| public override bool SetImage(Image image) => false; | ||
| if (data.Image != null) | ||
| success = false; | ||
|
|
||
| customFormatValues.AddRange(data.CustomFormatValues); | ||
|
|
||
| return success; | ||
| } | ||
|
|
||
| public override string? GetCustom(string format) | ||
| { | ||
| return customFormatValues[format]; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| // 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 concurrent representations of some data to 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> | ||
| /// Contains values to be stored with custom mime type in the clipboard. | ||
| /// Keyed by mime type. | ||
| /// </summary> | ||
| public readonly Dictionary<string, string> CustomFormatValues = new Dictionary<string, string>(); | ||
bdach marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| public ClipboardData() | ||
| { | ||
| Text = null; | ||
| Image = null; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Returns true if no data is present | ||
| /// </summary> | ||
| public bool IsEmpty() | ||
| { | ||
| return Text == null && Image == null && CustomFormatValues.Count == 0; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,8 @@ | ||
| // 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 NuGet.Packaging; | ||
| using SixLabors.ImageSharp; | ||
|
|
||
| namespace osu.Framework.Platform | ||
|
|
@@ -15,21 +17,25 @@ public class HeadlessClipboard : Clipboard | |
| { | ||
| private string? clipboardText; | ||
| private Image? clipboardImage; | ||
| private readonly Dictionary<string, string> customValues = new Dictionary<string, string>(); | ||
|
|
||
| public override string? GetText() => clipboardText; | ||
|
|
||
| public override void SetText(string text) | ||
| public override Image<TPixel>? GetImage<TPixel>() => clipboardImage?.CloneAs<TPixel>(); | ||
|
|
||
| public override string? GetCustom(string mimeType) | ||
| { | ||
| clipboardImage = null; | ||
| clipboardText = text; | ||
| return customValues[mimeType]; | ||
| } | ||
|
|
||
| public override Image<TPixel>? GetImage<TPixel>() => clipboardImage?.CloneAs<TPixel>(); | ||
|
|
||
| public override bool SetImage(Image image) | ||
| public override bool SetData(ClipboardData data) | ||
| { | ||
| clipboardText = null; | ||
| clipboardImage = image; | ||
| clipboardText = data.Text; | ||
| clipboardImage = data.Image; | ||
| customValues.Clear(); | ||
|
|
||
| customValues.AddRange(data.CustomFormatValues); | ||
|
|
||
| return true; | ||
| } | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,28 +1,48 @@ | ||
| // 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 NuGet.Packaging; | ||
| using SDL2; | ||
| using SixLabors.ImageSharp; | ||
|
|
||
| namespace osu.Framework.Platform.SDL2 | ||
| { | ||
| public class SDL2Clipboard : Clipboard | ||
| { | ||
| private readonly Dictionary<string, string> customFormatValues = new Dictionary<string, string>(); | ||
|
|
||
| // SDL cannot differentiate between string.Empty and no text (eg. empty clipboard or an image) | ||
| // doesn't matter as text editors don't really allow copying empty strings. | ||
| // assume that empty text means no text. | ||
| public override string? GetText() => SDL.SDL_HasClipboardText() == SDL.SDL_bool.SDL_TRUE ? SDL.SDL_GetClipboardText() : null; | ||
|
|
||
| public override void SetText(string text) => SDL.SDL_SetClipboardText(text); | ||
|
|
||
| public override Image<TPixel>? GetImage<TPixel>() | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| public override bool SetImage(Image image) | ||
| public override string? GetCustom(string mimeType) | ||
| { | ||
| return customFormatValues[mimeType]; | ||
| } | ||
|
|
||
| public override bool SetData(ClipboardData data) | ||
| { | ||
| return false; | ||
| customFormatValues.Clear(); | ||
|
|
||
| if (data.IsEmpty()) | ||
| return false; | ||
|
|
||
| if (data.Image != null) | ||
| return false; | ||
|
|
||
| if (data.Text != null) | ||
| SDL.SDL_SetClipboardText(data.Text); | ||
|
|
||
| customFormatValues.AddRange(data.CustomFormatValues); | ||
|
|
||
| return true; | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.