2 * Copyright(c) 2023 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 using System.Collections.Generic;
19 using System.ComponentModel;
20 using System.Diagnostics.CodeAnalysis;
22 using System.Runtime.InteropServices;
24 using Tizen.NUI.BaseComponents;
31 [EditorBrowsable(EditorBrowsableState.Never)]
32 public partial class Clipboard : BaseHandle
35 /// User callback for clipboard event.
38 /// Receives requested data through <see cref="Tizen.NUI.ClipEvent"/>.
40 public delegate void ClipboardCallback(bool success, ClipEvent clipEvent);
42 internal bool hasClipboardDataReceived = false;
43 internal Dictionary<uint, ClipboardCallback> receivedCallbackDictionary = new Dictionary<uint, ClipboardCallback>();
45 private Clipboard() : this(Interop.Clipboard.Get(), true)
47 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
50 private Clipboard(global::System.IntPtr cPtr, bool cMemoryOwn) : base(cPtr, cMemoryOwn)
55 /// Gets the singleton instance of Clipboard.
57 [EditorBrowsable(EditorBrowsableState.Never)]
58 public static Clipboard Instance { get; } = new Clipboard();
61 /// Request set the given data to the clipboard.
63 /// <param name="mimeType">The mime type of the data.</param>
64 /// <param name="data">The data to be set on the clipboard.</param>
65 /// <returns>True if the internal clipboard sending request is successful.</returns>
67 /// The following example demonstrates how to use the SetData.
69 /// string MIME_TYPE_PLAIN_TEXT = "text/plain;charset=utf-8";
70 /// Clipboard.Instance.SetData(MIME_TYPE_PLAIN_TEXT, "Hello Clipboard");
73 [EditorBrowsable(EditorBrowsableState.Never)]
74 public bool SetData(string mimeType, string data)
76 bool setData = Interop.Clipboard.SetData(SwigCPtr, mimeType, data);
77 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
82 /// Request get data of the specified mime type from clipboard<br/>
83 /// and invokes the given callback with the received clipboard data.
85 /// <param name="mimeType">The mime type of data to request.</param>
86 /// <param name="dataReceivedCallback">The callback method to handle the received clipboard data.</param>
88 /// GetData() method is introduced to fetch data of the specified mime type,<br/>
89 /// and it expects a callback function as a parameter.<br/>
90 /// The given callback is invoked with received clipboard data.<br/>
91 /// The callback is designed to be used only once for handling the data.
94 /// The following example demonstrates how to use the GetData and ClipboardCallback.
96 /// string MIME_TYPE_PLAIN_TEXT = "text/plain;charset=utf-8";
97 /// Clipboard.Instance.GetData(MIME_TYPE_PLAIN_TEXT, OnClipboardDataReceived);
99 /// public void OnClipboardDataReceived(bool success, ClipEvent clipEvent)
101 /// if (!success) return;
102 /// string mimeType = clipEvent.MimeType;
103 /// string data = clipEvent.Data;
107 [EditorBrowsable(EditorBrowsableState.Never)]
108 public void GetData(string mimeType, ClipboardCallback dataReceivedCallback)
110 if(!hasClipboardDataReceived)
112 ClipboardDataReceived += OnClipboardDataReceived;
113 hasClipboardDataReceived = true;
116 uint id = Interop.Clipboard.GetData(SwigCPtr, mimeType);
117 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
121 // Calls the failure callback even if the request fails.
122 var clipEvent = new ClipEvent()
124 MimeType = string.Empty,
127 dataReceivedCallback(false, clipEvent);
131 receivedCallbackDictionary[id] = dataReceivedCallback;
135 private void OnClipboardDataReceived(object sender, ClipboardEventArgs e)
137 if (!receivedCallbackDictionary.Any()) return;
140 if (receivedCallbackDictionary.ContainsKey(id))
142 ClipboardCallback callback = receivedCallbackDictionary[id];
143 if (callback != null)
145 callback(e.Success, e.ClipEvent);
147 receivedCallbackDictionary.Remove(id);
154 protected override void Dispose(DisposeTypes type)
161 if (hasClipboardDataReceived)
163 ClipboardDataReceived -= OnClipboardDataReceived;
164 receivedCallbackDictionary.Clear();
169 if (clipboardDataReceivedCallback != null)
171 this.ClipboardDataReceivedSignal().Disconnect(clipboardDataReceivedCallback);
174 if (clipboardDataSelectedCallback != null)
176 this.ClipboardDataSelectedSignal().Disconnect(clipboardDataSelectedCallback);