|
| 1 | +// Copyright (c) Microsoft Corporation |
| 2 | +// The Microsoft Corporation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using FormsClipboard = System.Windows.Forms.Clipboard; |
| 6 | + |
| 7 | +namespace Microsoft.PowerToys.UITest.Next; |
| 8 | + |
| 9 | +/// <summary> |
| 10 | +/// Clipboard helpers that always execute on an STA thread (<see cref="FormsClipboard"/> |
| 11 | +/// requires it). Tolerant — every method swallows clipboard errors and returns a default, |
| 12 | +/// so callers can use them in test <c>finally</c> blocks without worrying about masking |
| 13 | +/// the real failure. |
| 14 | +/// </summary> |
| 15 | +public static class ClipboardHelper |
| 16 | +{ |
| 17 | + /// <summary>Return the current clipboard text, or <see cref="string.Empty"/> if none / on error.</summary> |
| 18 | + public static string GetText() => RunSTA(() => FormsClipboard.ContainsText() ? FormsClipboard.GetText() : string.Empty) ?? string.Empty; |
| 19 | + |
| 20 | + /// <summary>Clear the clipboard. Returns true on success, false on error.</summary> |
| 21 | + public static bool Clear() => RunSTA(() => { FormsClipboard.Clear(); return true; }); |
| 22 | + |
| 23 | + /// <summary>Set the clipboard text. Returns true on success, false on error.</summary> |
| 24 | + public static bool SetText(string value) => RunSTA(() => { FormsClipboard.SetText(value); return true; }); |
| 25 | + |
| 26 | + /// <summary> |
| 27 | + /// Poll the clipboard up to <paramref name="timeoutMS"/> for the first non-empty text |
| 28 | + /// different from <paramref name="ignoredValue"/>. Returns <see cref="string.Empty"/> on |
| 29 | + /// timeout. Use when you've just cleared the clipboard and are waiting for an external |
| 30 | + /// app (e.g. ColorPicker on click) to write into it. |
| 31 | + /// </summary> |
| 32 | + public static string WaitForText(string ignoredValue = "", int timeoutMS = 3_000, int pollIntervalMS = 100) |
| 33 | + { |
| 34 | + var deadline = DateTime.UtcNow + TimeSpan.FromMilliseconds(timeoutMS); |
| 35 | + while (DateTime.UtcNow < deadline) |
| 36 | + { |
| 37 | + var text = GetText(); |
| 38 | + if (!string.IsNullOrEmpty(text) && text != ignoredValue) |
| 39 | + { |
| 40 | + return text; |
| 41 | + } |
| 42 | + |
| 43 | + Thread.Sleep(pollIntervalMS); |
| 44 | + } |
| 45 | + |
| 46 | + return string.Empty; |
| 47 | + } |
| 48 | + |
| 49 | + private static T? RunSTA<T>(Func<T> body) |
| 50 | + { |
| 51 | + T? result = default; |
| 52 | + try |
| 53 | + { |
| 54 | + var thread = new Thread(() => |
| 55 | + { |
| 56 | + try |
| 57 | + { |
| 58 | + result = body(); |
| 59 | + } |
| 60 | + catch |
| 61 | + { |
| 62 | + // Best effort — clipboard can throw under contention (OpenClipboard failures). |
| 63 | + } |
| 64 | + }); |
| 65 | + thread.SetApartmentState(ApartmentState.STA); |
| 66 | + thread.Start(); |
| 67 | + thread.Join(TimeSpan.FromSeconds(5)); |
| 68 | + } |
| 69 | + catch |
| 70 | + { |
| 71 | + } |
| 72 | + |
| 73 | + return result; |
| 74 | + } |
| 75 | +} |
0 commit comments