[NUI] Enhance clipboard sample code
[platform/core/csapi/tizenfx.git] / test / NUIClipboardDataSelected / ClipboardDataSelected.cs
1 using System;
2 using Tizen.NUI;
3 using Tizen.NUI.BaseComponents;
4 using Tizen.NUI.WindowSystem;
5
6 /*
7  * This app has no UI.
8  * After the app is launched, it is hidden and the focus is skipped.
9  * This is a sample app that becomes SecondarySelection and catches the Copy event of another process.
10  * If there is another SecondarySelection already running on the device, this sample will not work properly.
11  */
12
13 namespace NUIClipboardDataSelected
14 {
15     class Program : NUIApplication
16     {
17         const string TAG = "clipboard";
18         const string MIME_TYPE_PLAIN_TEXT = "text/plain;charset=utf-8";
19
20         protected override void OnCreate()
21         {
22             base.OnCreate();
23             Initialize();
24         }
25
26         void Initialize()
27         {
28             Window.Instance.WindowSize = new Size(1, 1);
29             Window.Instance.BackgroundColor = Color.White;
30
31             Tizen.NUI.WindowSystem.Shell.TizenShell tzShell;
32             tzShell = new Tizen.NUI.WindowSystem.Shell.TizenShell();
33             Window.Instance.SetAcceptFocus(false);
34
35             Tizen.NUI.WindowSystem.Shell.KVMService kvmService;
36             // Window that will act as KVM Service.
37             kvmService = new Tizen.NUI.WindowSystem.Shell.KVMService(tzShell, Window.Instance); 
38             kvmService.SetSecondarySelction();
39
40             // This view has nothing to do with this test, just for easy debugging.
41             // If there is a view, it is exposed to the process monitor.
42             View view = NewView();
43             Window.Instance.GetDefaultLayer().Add(view);
44
45             // Register event handler.
46             Clipboard.Instance.DataSelected += OnClipboardDataSelected;
47
48             // Self copy test.
49             CopyTest();
50         }
51
52         // When copy occurs somewhere, this callback is invoked.
53         public void OnClipboardDataSelected(object sender, ClipboardDataSelectedEventArgs e)
54         {
55             // e.MimeType is the MIME type of the copy data that invoked this callback.
56             string selectedType = e.MimeType;
57             Tizen.Log.Info(TAG, $"OnClipboardDataSelected type:{selectedType}\n");
58
59             // Do something here.
60             // For example, MC app can call Clipboard's GetData() with the MIME type of the event argument.
61             Clipboard.Instance.GetData(selectedType, OnClipboardDataReceived);
62         }
63
64         // When call Clipboard's GetData(), the user callback is called.
65         public void OnClipboardDataReceived(bool success, ClipEvent clipEvent)
66         {
67             if (!success)
68             {
69                 Tizen.Log.Error(TAG, $"Data receive fail");
70                 return;
71             }
72
73             Tizen.Log.Info(TAG, $"OnClipboardDataReceived type:{clipEvent.MimeType}, data:{clipEvent.Data}\n");
74         }
75
76         public void CopyTest()
77         {
78             // Self copy test.
79             // * SetData() is called 5 seconds after app execution.
80             // * Observe the Log of OnClipboardDataSelected.
81             // * If the log is output, there is a problem somewhere.
82             // * DataSelected event should not be invoked
83             // * by the SetData() called within the SecondarySelection.
84             Timer timer = new Timer(5000);
85             timer.Tick += (s, e) =>
86             {
87                 string data = "Lorem ipsum dolor sit amet consectetuer";
88                 Tizen.Log.Info(TAG, $"SetData type:{MIME_TYPE_PLAIN_TEXT}, data:{data}\n");
89                 Clipboard.Instance.SetData(MIME_TYPE_PLAIN_TEXT, data);
90                 return false;
91             };
92             timer.Start();
93         }
94
95         public View NewView()
96         {
97             var view = new View()
98             {
99                 Layout = new LinearLayout()
100                 {
101                     LinearOrientation = LinearLayout.Orientation.Vertical,
102                     LinearAlignment = LinearLayout.Alignment.Begin,
103                 },
104                 WidthSpecification = LayoutParamPolicies.MatchParent,
105                 HeightSpecification = LayoutParamPolicies.WrapContent,
106                 BackgroundColor = Color.White,
107             };
108             return view;
109         }
110
111         static void Main(string[] args)
112         {
113             var app = new Program();
114             app.Run(args);
115         }
116     }
117 }