Checking if clipboard is available on windows
Describe the bug
I am developing a simple password manager in F# and I noticed that after rebooting or taking a screenshot, all of a sudden I can't access the clipboard from my app. When I checked what the issue was, I discovered that the IsSupported field from Clipboard class is false, which results in not being able to access or set data in the clipboard. In the Windows implementation, checking if the clipboard is supported is based on this piece of code:
private const uint CF_UNICODE_TEXT = 13;
public override bool IsSupported { get; } = IsClipboardFormatAvailable (CF_UNICODE_TEXT);
...
[DllImport ("User32.dll", SetLastError = true)]
[return: MarshalAs (UnmanagedType.Bool)]
private static extern bool IsClipboardFormatAvailable (uint format);
So basically, we are checking if the current format of the clipboard is UnicodeText, not if the clipboard is available on the given platform.
So whenever I take a screenshot and then run my app (which uses clipboard functionality from Terminal.Gui) on a Windows device, it stops working as intended.
Also, I think it is worth mentioning that if app already runs and I take a screenshot, everything works as intended.
I tried this simple workaround which seems to work fine, but before submitting a PR, I wanted to confirm that I am not overlooking some edge cases:
private static bool IsClipboardAvailable ()
{
if (OpenClipboard (default))
{
CloseClipboard ();
return true;
}
return false;
}
To Reproduce If you want to reproduce this issue you can try it using my app. Try copying a password to the clipboard after a PC reboot or after taking a screenshot.
I think there is no bug here. From what I understand, you copy text to the clipboard and after the reboot the clipboard is reset and it does not contain any value. The other situation is that you copy text to the clipboard and then take a screenshot and of course the text is no longer on the clipboard but the screenshot is. If you try to do the same with Notepad you also get the same result, because the image is not compatible with text. I haven't really tested it yet, but normally when I use the clipboard it always uses the most recent content, but if it's possible to get a list of the clipboard content so we can select what we want, that's new to me.
From what I understand, you copy text to the clipboard and after the reboot the clipboard is reset and it does not contain any value. The other situation is that you copy text to the clipboard and then take a screenshot and of course the text is no longer on the clipboard but the screenshot is. If you try to do the same with Notepad you also get the same result, because the image is not compatible with text.
You are right. However, what I meant is that, for example, after a PC reboot, when I try to copy text to the clipboard using Clipboard.TrySetClipboardData, it fails as there is no unicode text there so IsClipboardFormatAvailable and IsSupported property both return false. I think the intended behavior when the clipboard is empty is to copy text from Terminal.Gui app to the clipboard. This is the use case that I am facing in my app.
You are right. However, what I meant is that, for example, after a PC reboot, when I try to copy text to the clipboard using
Clipboard.TrySetClipboardData, it fails as there is no unicode text there soIsClipboardFormatAvailableandIsSupportedproperty both return false. I think the intended behavior when the clipboard is empty is to copy text from Terminal.Gui app to the clipboard. This is the use case that I am facing in my app.
So in this case just use the following:
Clipboard.Contents = "your text";
I think he is saying that once printscreen is used before app starts then copy to clipboard fails from then on - not that the clipboard text is being overwritten.
i.e. that the code which checks whether clipboard is working on OS Windows is bugged and is saying there is no clipboard implementation when really there is.
Basically the constructor for WindowsClipboard is asking OS if there is text data available to paste. What it should be doing is asking windows if clipboard functionality exists at all. So should just be:
public WindowsClipboard ()
{
IsSupported = true;
}
What code should probably be
The real issue here is that IsSupported is set once only when the driver is created then used like a way to decide whether the user can copy/paste from that point on:
public WindowsDriver ()
{
WinConsole = new WindowsConsole ();
clipboard = new WindowsClipboard ();
isWindowsTerminal = Environment.GetEnvironmentVariable ("WT_SESSION") != null;
}
Now I got it, sorry. I was doing a print screen after open the Terminal.Gui. Now opening the app after the print screen I confirm now that this is bugged for sure.
I think he is saying that once printscreen is used before app starts then copy to clipboard fails from then on - not that the clipboard text is being overwritten.
Yes, that is what I meant