f0dc005cd2f0eccbf8ca7d34825fdd7028bdd503
[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
19         protected override void OnCreate()
20         {
21             base.OnCreate();
22             Initialize();
23         }
24
25         void Initialize()
26         {
27             Window.Instance.WindowSize = new Size(1, 1);
28             Window.Instance.BackgroundColor = Color.White;
29
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             // Add a dummy view for easy debugging.
41             View view = NewView();
42             Window.Instance.GetDefaultLayer().Add(view);
43
44             // Register event handler.
45             Clipboard.Instance.DataSelected += OnClipboardDataSelected;
46         }
47
48         // When copy occurs somewhere, this callback is invoked.
49         public void OnClipboardDataSelected(object sender, ClipboardDataSelectedEventArgs e)
50         {
51             // e.MimeType is the MIME type of the copy data that invoked this callback.
52             string selectedType = e.MimeType;
53             Tizen.Log.Info(TAG, $"OnClipboardDataSelected type:{selectedType}\n");
54
55             // Do something here.
56             // For example, MC app can call Clipboard's GetData() with the MIME type of the event argument.
57             Clipboard.Instance.GetData(selectedType, OnClipboardDataReceived);
58         }
59
60         // When call Clipboard's GetData(), the user callback is called.
61         public void OnClipboardDataReceived(bool success, ClipEvent clipEvent)
62         {
63             if (!success)
64             {
65                 Tizen.Log.Error(TAG, $"Data receive fail");
66                 return;
67             }
68
69             Tizen.Log.Info(TAG, $"OnClipboardDataReceived type:{clipEvent.MimeType}, data:{clipEvent.Data}\n");
70         }
71
72         public View NewView()
73         {
74             var view = new View()
75             {
76                 Layout = new LinearLayout()
77                 {
78                     LinearOrientation = LinearLayout.Orientation.Vertical,
79                     LinearAlignment = LinearLayout.Alignment.Begin,
80                 },
81                 WidthSpecification = LayoutParamPolicies.MatchParent,
82                 HeightSpecification = LayoutParamPolicies.WrapContent,
83                 BackgroundColor = Color.White,
84             };
85             return view;
86         }
87
88         static void Main(string[] args)
89         {
90             var app = new Program();
91             app.Run(args);
92         }
93     }
94 }