| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using DCL; |
| | 4 | | using DCL.Helpers; |
| | 5 | |
|
| | 6 | | public class Clipboard : IClipboard |
| | 7 | | { |
| 630 | 8 | | private readonly Queue<Promise<string>> promises = new Queue<Promise<string>>(); |
| | 9 | | private readonly IClipboardHandler handler = null; |
| | 10 | |
|
| | 11 | | /// <summary> |
| | 12 | | /// Create a platform specific instance of Clipboard |
| | 13 | | /// </summary> |
| | 14 | | /// <returns>Clipboard instance</returns> |
| | 15 | | public static Clipboard Create() |
| | 16 | | { |
| | 17 | | #if UNITY_WEBGL && !UNITY_EDITOR |
| | 18 | | return new Clipboard(ClipboardWebGL.i); |
| | 19 | | #else |
| 629 | 20 | | return new Clipboard(new ClipboardStandalone()); |
| | 21 | | #endif |
| | 22 | | } |
| | 23 | |
|
| | 24 | | /// <summary> |
| | 25 | | /// Push a string value to the clipboard |
| | 26 | | /// </summary> |
| | 27 | | /// <param name="text">string to store</param> |
| 0 | 28 | | public void WriteText(string text) { handler?.RequestWriteText(text); } |
| | 29 | |
|
| | 30 | | /// <summary> |
| | 31 | | /// Request the string stored at the clipboard |
| | 32 | | /// </summary> |
| | 33 | | /// <returns>Promise of the string value stored at clipboard</returns> |
| | 34 | | [Obsolete("Firefox not supported")] |
| | 35 | | public Promise<string> ReadText() |
| | 36 | | { |
| 2 | 37 | | Promise<string> promise = new Promise<string>(); |
| 2 | 38 | | promises.Enqueue(promise); |
| 2 | 39 | | handler?.RequestGetText(); |
| 2 | 40 | | return promise; |
| | 41 | | } |
| | 42 | |
|
| 630 | 43 | | public Clipboard(IClipboardHandler handler) |
| | 44 | | { |
| 630 | 45 | | this.handler = handler; |
| 630 | 46 | | handler.Initialize(OnReadText); |
| 630 | 47 | | } |
| | 48 | |
|
| | 49 | | private void OnReadText(string text, bool error) |
| | 50 | | { |
| 4 | 51 | | while (promises.Count > 0) |
| | 52 | | { |
| 2 | 53 | | var promise = promises.Dequeue(); |
| 2 | 54 | | if (error) |
| | 55 | | { |
| 1 | 56 | | promise.Reject(text); |
| 1 | 57 | | } |
| | 58 | | else |
| | 59 | | { |
| 1 | 60 | | promise.Resolve(text); |
| | 61 | | } |
| | 62 | | } |
| 2 | 63 | | } |
| | 64 | |
|
| | 65 | | public void Dispose() |
| | 66 | | { |
| 629 | 67 | | } |
| | 68 | |
|
| | 69 | | public void Initialize() |
| | 70 | | { |
| 629 | 71 | | } |
| | 72 | | } |