/* * Copyright(c) 2023 Samsung Electronics Co., Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ using System; using System.ComponentModel; using System.Runtime.InteropServices; namespace Tizen.NUI { /// /// This specifies clipboard event data. /// [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1815: Override equals and operator equals on value types")] [EditorBrowsable(EditorBrowsableState.Never)] public struct ClipEvent { /// /// The mime type of clipboard event data. /// public string MimeType { get; set; } /// /// The clipboard event data to receive. /// public string Data { get; set; } } /// /// ClipboardDataSelectedEventArgs is a class to record clipboard selected event arguments which will be sent to user.
/// This is to catch data selection event. ///
[EditorBrowsable(EditorBrowsableState.Never)] public class ClipboardDataSelectedEventArgs : EventArgs { /// /// The mime type of clipboard selected data. /// public string MimeType { get; set; } } /// /// ClipboardEventArgs is a class to record clipboard event arguments which will be sent to user.
/// This is for internal use only. ///
internal class ClipboardEventArgs : EventArgs { /// /// True if data receive is successful. /// public bool Success { get; set; } /// /// The data request id to identify the response by request. /// public uint Id { get; set; } /// /// Clipboard event data. /// public ClipEvent ClipEvent { get; set; } } /// /// Clipboard event. /// public partial class Clipboard { private EventHandler clipboardDataReceivedEventHandler; private ClipboardDataReceivedCallback clipboardDataReceivedCallback; private EventHandler clipboardDataSelectedEventHandler; private ClipboardDataSelectedCallback clipboardDataSelectedCallback; [UnmanagedFunctionPointer(CallingConvention.Cdecl)] private delegate void ClipboardDataReceivedCallback(uint id, string mimeType, string data); [UnmanagedFunctionPointer(CallingConvention.Cdecl)] private delegate void ClipboardDataSelectedCallback(string mimeType); /// /// The DataSelected event is emitted when a copy event occurs somewhere.
/// In order for this event to operate normally,
/// the process using this event must be Secondary Selection. ///
/// /// The following example demonstrates how to use the DataSelected. /// /// kvmService.SetSecondarySelection(); // precondition /// /// Clipboard.Instance.DataSelected += (s, e) => /// { /// string selectedType = e.MimeType; /// }; /// /// [EditorBrowsable(EditorBrowsableState.Never)] public event EventHandler DataSelected { add { if (clipboardDataSelectedEventHandler == null) { clipboardDataSelectedCallback = (OnClipboardDataSelected); ClipboardDataSelectedSignal().Connect(clipboardDataSelectedCallback); } clipboardDataSelectedEventHandler += value; } remove { clipboardDataSelectedEventHandler -= value; if (clipboardDataSelectedEventHandler == null && ClipboardDataSelectedSignal().Empty() == false) { ClipboardDataSelectedSignal().Disconnect(clipboardDataSelectedCallback); } } } private event EventHandler ClipboardDataReceived { add { if (clipboardDataReceivedEventHandler == null) { clipboardDataReceivedCallback = (OnClipboardDataReceived); ClipboardDataReceivedSignal().Connect(clipboardDataReceivedCallback); } clipboardDataReceivedEventHandler += value; } remove { clipboardDataReceivedEventHandler -= value; if (clipboardDataReceivedEventHandler == null && ClipboardDataReceivedSignal().Empty() == false) { ClipboardDataReceivedSignal().Disconnect(clipboardDataReceivedCallback); } } } internal ClipboardSignal ClipboardDataSelectedSignal() { var ret = new ClipboardSignal(Interop.Clipboard.ClipboardDataSelectedSignal(SwigCPtr), false); if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); return ret; } private void OnClipboardDataSelected(string mimeType) { var e = new ClipboardDataSelectedEventArgs(); e.MimeType = mimeType; clipboardDataSelectedEventHandler?.Invoke(this, e); } internal ClipboardSignal ClipboardDataReceivedSignal() { var ret = new ClipboardSignal(Interop.Clipboard.ClipboardDataReceivedSignal(SwigCPtr), false); if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); return ret; } private void OnClipboardDataReceived(uint id, string mimeType, string data) { var e = new ClipboardEventArgs(); var clipEvent = new ClipEvent() { MimeType = mimeType, Data = data, }; e.ClipEvent = clipEvent; e.Id = id; e.Success = (String.IsNullOrEmpty(e.ClipEvent.MimeType) && String.IsNullOrEmpty(e.ClipEvent.Data)) ? false : true; clipboardDataReceivedEventHandler?.Invoke(this, e); } } }