Skip to content
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

[Advanced Paste] Repeatedly pasting plain text as JSON produces inconsistent results #34151

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
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
34 changes: 31 additions & 3 deletions src/modules/AdvancedPaste/AdvancedPaste/Helpers/ClipboardHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using System.Threading;
using System.Threading.Tasks;
using ManagedCommon;
using Microsoft.UI.Xaml.Media.Imaging;
using Windows.ApplicationModel.DataTransfer;
using Windows.Storage.Streams;
using Windows.System;
Expand All @@ -21,9 +20,28 @@ internal static void SetClipboardTextContent(string text)

if (!string.IsNullOrEmpty(text))
{
Logger.LogDebug("Text to set to clipboard: " + text);

DataPackage output = new();
output.SetText(text);
Clipboard.SetContentWithOptions(output, null);
bool success = Clipboard.SetContentWithOptions(output, null);
Logger.LogDebug("Setting Clipboard data was success?: " + success);

// Wait 50 ms in a loop until setting the clipboard content has really finished.
while (!Clipboard.GetContent().GetTextAsync().GetAwaiter().GetResult().Contains(text))
{
Thread.Sleep(50);
}

try
{
string clipContent = Clipboard.GetContent().GetTextAsync().GetAwaiter().GetResult();
Logger.LogDebug("Clipboard content for current process: " + clipContent);
}
catch (Exception ex)
{
Logger.LogError("Failed to get clipboard content: ", ex.GetBaseException());
}
GhostVaibhav marked this conversation as resolved.
Show resolved Hide resolved

// TODO(stefan): For some reason Flush() fails from time to time when directly activated via hotkey.
// Calling inside a loop makes it work.
Expand All @@ -39,14 +57,24 @@ internal static void SetClipboardTextContent(string text)
{
Task.Run(() =>
{
Clipboard.Flush();
try
{
Clipboard.Flush();
}
catch (Exception ex)
{
Logger.LogError("Clipboard.Flush() failed. Real reason:", ex.GetBaseException());
throw;
}
}).Wait();

flushed = true;
Logger.LogDebug("Clipboard flushed.");
}
catch (Exception ex)
{
Logger.LogError("Clipboard.Flush() failed", ex);
Thread.Sleep(50);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Collections.ObjectModel;
using System.Globalization;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using AdvancedPaste.Helpers;
using AdvancedPaste.Models;
Expand Down Expand Up @@ -252,6 +253,8 @@ internal void ToPlainTextFunction()
{
Logger.LogTrace();

string originalClipboardData = ClipboardData.GetTextAsync().GetAwaiter().GetResult();

string outputString = MarkdownHelper.PasteAsPlainTextFromClipboard(ClipboardData);

SetClipboardContentAndHideWindow(outputString);
Expand All @@ -260,6 +263,13 @@ internal void ToPlainTextFunction()
{
ClipboardHelper.SendPasteKeyCombination();
}

// Sleeping 250 ms that paste command can be completely executed before resetting the clipboard.
// Otherwise we reset the clipboard to early and past the original content. Or the clipboard is still used while resetting and resetting fails.
Thread.Sleep(250);

// Resetting clipboard content
SetClipboardContentAndHideWindow(originalClipboardData);
}
catch
{
Expand All @@ -272,6 +282,8 @@ internal void ToMarkdownFunction(bool pasteAlways = false)
{
Logger.LogTrace();

string originalClipboardData = ClipboardData.GetTextAsync().GetAwaiter().GetResult();

string outputString = MarkdownHelper.ToMarkdown(ClipboardData);

SetClipboardContentAndHideWindow(outputString);
Expand All @@ -280,6 +292,13 @@ internal void ToMarkdownFunction(bool pasteAlways = false)
{
ClipboardHelper.SendPasteKeyCombination();
}

// Sleeping 250 ms that paste command can be completely executed before resetting the clipboard.
// Otherwise we reset the clipboard to early and past the original content. Or the clipboard is still used while resetting and resetting fails.
Thread.Sleep(250);

// Resetting clipboard content
SetClipboardContentAndHideWindow(originalClipboardData);
}
catch
{
Expand All @@ -292,6 +311,8 @@ internal void ToJsonFunction(bool pasteAlways = false)
{
Logger.LogTrace();

string originalClipboardData = ClipboardData.GetTextAsync().GetAwaiter().GetResult();

string jsonText = JsonHelper.ToJsonFromXmlOrCsv(ClipboardData);

SetClipboardContentAndHideWindow(jsonText);
Expand All @@ -300,6 +321,13 @@ internal void ToJsonFunction(bool pasteAlways = false)
{
ClipboardHelper.SendPasteKeyCombination();
}

// Sleeping 250 ms that paste command can be completely executed before resetting the clipboard.
// Otherwise we reset the clipboard to early and past the original content. Or the clipboard is still used while resetting and resetting fails.
Thread.Sleep(250);

// Resetting clipboard content
SetClipboardContentAndHideWindow(originalClipboardData);
}
catch
{
Expand Down
Loading