[NUI] Enhance clipboard sample code
authorBowon Ryu <bowon.ryu@samsung.com>
Mon, 11 Sep 2023 02:01:42 +0000 (11:01 +0900)
committerJoogabYun <40262755+JoogabYun@users.noreply.github.com>
Mon, 11 Sep 2023 07:30:31 +0000 (16:30 +0900)
- test/NUIClipboard
- test/NUIClipboardDataSelected

Signed-off-by: Bowon Ryu <bowon.ryu@samsung.com>
test/NUIClipboard/Clipboard.cs
test/NUIClipboardDataSelected/ClipboardDataSelected.cs

index 99b1425..f6b91ab 100644 (file)
@@ -8,16 +8,23 @@ namespace NUIClipboard
     class Program : NUIApplication
     {
         const string TAG = "clipboard";
+
+        // Below are the MIME Types used in Tizen World(NUI, EFL).
         const string MIME_TYPE_PLAIN_TEXT = "text/plain;charset=utf-8";
-        const string MIME_TYPE_TEXT_URI = "text/uri-list";
         const string MIME_TYPE_HTML = "application/xhtml+xml";
+        const string MIME_TYPE_TEXT_URI = "text/uri-list";
 
         TextField fieldCopy;
-        TextField fieldPaste;
-
         TextLabel labelType;
         TextLabel labelData;
 
+        Button buttonCopy;
+        Button buttonPaste;
+        Button buttonCopyHtml;
+        Button buttonPasteHtml;
+        Button buttonCopyUri;
+        Button buttonPasteUri;
+
         protected override void OnCreate()
         {
             base.OnCreate();
@@ -26,80 +33,57 @@ namespace NUIClipboard
 
         void Initialize()
         {
-            Window.Instance.WindowSize = new Size(900, 1080);
-            Window.Instance.BackgroundColor = Color.White;
-
-            View mainView = NewView(false);
-            Window.Instance.GetDefaultLayer().Add(mainView);
-
-            TextLabel title = NewTextLabel("Tizen.NUI.Clipboard Test", LayoutParamPolicies.MatchParent);
-            title.HorizontalAlignment = HorizontalAlignment.Center;
-            title.PointSize = 30.0f;
-            mainView.Add(title);
-
-            string descriptionText = "[Description]\n" +
-                                     "* Copy : Copy the text entered in the TextField using the Clipboard's SetData() API.\n" +
-                                     "* This sample uses the 'text/plain;charset=utf-8' MIME type to send and receive data.\n" + 
-                                     "* Paste : Paste the text using the Clipboard's GetData() API.\n" +
-                                     "* Clear : Clear Information.";
-
-            TextLabel description = NewTextLabel(descriptionText, LayoutParamPolicies.MatchParent);
-            description.PointSize = 20.0f;
-            mainView.Add(description);
-
-            View vertView1 = NewView(true);
-            mainView.Add(vertView1);
+            GenerateUI(900, 1080);
 
-            fieldCopy = NewTextField("Enter text to copy", LayoutParamPolicies.MatchParent);
-            vertView1.Add(fieldCopy);
+            // The API used by the application is Clipboard's SetData() and GetData() methods.
+            // Please refer to the code in the button Clicked event below.
 
-            Button buttonCopy = NewButton("Copy");
-            vertView1.Add(buttonCopy);
+            // TEXT type test : "text/plain;charset=utf-8"
             buttonCopy.Clicked += (s, e) =>
             {
                 string data = fieldCopy.Text;
-                Clipboard.Instance.SetData(MIME_TYPE_PLAIN_TEXT, data);
                 Tizen.Log.Info(TAG, $"SetData type:{MIME_TYPE_PLAIN_TEXT}, data:{data}\n");
-            };
-
 
-            View vertView2 = NewView(true);
-            mainView.Add(vertView2);
+                // Set "text/plain;charset=utf-8" type data on the clipboard.
+                Clipboard.Instance.SetData(MIME_TYPE_PLAIN_TEXT, data);
+            };
 
-            Button buttonPaste = NewButton("Paste");
-            vertView2.Add(buttonPaste);
             buttonPaste.Clicked += (s, e) =>
             {
-                Clipboard.Instance.GetData(MIME_TYPE_PLAIN_TEXT, OnClipboardDataReceived);
                 Tizen.Log.Info(TAG, $"GetData request type:{MIME_TYPE_PLAIN_TEXT}\n");
+
+                // Request to receive "text/plain;charset=utf-8" type data from clipboard.
+                // The results can be received through the OnClipboardDataReceived callback.
+                Clipboard.Instance.GetData(MIME_TYPE_PLAIN_TEXT, OnClipboardDataReceived);
             };
 
-            Button buttonCopyClear = NewButton("Clear");
-            vertView2.Add(buttonCopyClear);
-            buttonCopyClear.Clicked += (s, e) =>
+            // HTML type test : "application/xhtml+xml"
+            buttonCopyHtml.Clicked += (s, e) =>
             {
-                fieldCopy.Text = "";
-                labelType.Text = " ";
-                labelData.Text = " ";
+                string data = fieldCopy.Text;
+                Tizen.Log.Info(TAG, $"SetData type:{MIME_TYPE_HTML}, data:{data}\n");
+                Clipboard.Instance.SetData(MIME_TYPE_HTML, data);
             };
 
-            View vertView3 = NewView(true);
-            mainView.Add(vertView3);
-
-            TextLabel pastedType = NewTextLabel("MIME type", 300);
-            vertView3.Add(pastedType);
-
-            labelType = NewTextLabel(" ", LayoutParamPolicies.MatchParent);
-            vertView3.Add(labelType);
-
-            View vertView4 = NewView(true);
-            mainView.Add(vertView4);
+            buttonPasteHtml.Clicked += (s, e) =>
+            {
+                Tizen.Log.Info(TAG, $"GetData request type:{MIME_TYPE_HTML}\n");
+                Clipboard.Instance.GetData(MIME_TYPE_HTML, OnClipboardDataReceived);
+            };
 
-            TextLabel pastedData = NewTextLabel("Data", 300);
-            vertView4.Add(pastedData);
+            // URI type test : "text/uri-list"
+            buttonCopyUri.Clicked += (s, e) =>
+            {
+                string data = fieldCopy.Text;
+                Tizen.Log.Info(TAG, $"SetData type:{MIME_TYPE_TEXT_URI}, data:{data}\n");
+                Clipboard.Instance.SetData(MIME_TYPE_TEXT_URI, data);
+            };
 
-            labelData = NewTextLabel(" ", LayoutParamPolicies.MatchParent);
-            vertView4.Add(labelData);
+            buttonPasteUri.Clicked += (s, e) =>
+            {
+                Tizen.Log.Info(TAG, $"GetData request type:{MIME_TYPE_TEXT_URI}\n");
+                Clipboard.Instance.GetData(MIME_TYPE_TEXT_URI, OnClipboardDataReceived);
+            };
         }
 
         public void OnClipboardDataReceived(bool success, ClipEvent clipEvent)
@@ -117,6 +101,109 @@ namespace NUIClipboard
             labelData.Text = clipEvent.Data;
         }
 
+        // UI code
+        public void GenerateUI(int width, int height)
+        {
+            Window.Instance.WindowSize = new Size(width, height);
+            Window.Instance.BackgroundColor = Color.White;
+
+            // Top UI
+            View mainView = NewView(false);
+            Window.Instance.GetDefaultLayer().Add(mainView);
+
+            mainView.Add(NewTitle("Tizen.NUI.Clipboard Test"));
+            mainView.Add(NewPadding(5));
+            mainView.Add(NewInfo(" ▼ Description"));
+
+            string descriptionText = "* <u>Copy</u> : Copy the text entered in the TextField using the Clipboard's <u><i>SetData()</i></u> API.\n" +
+                                     "* <u>Paste</u> : Paste the text using the Clipboard's <u><i>GetData()</i></u> API.\n" +
+                                     "* <u>Clear</u> : Clear Information.\n" +
+                                     "* This sample uses three different <u><i>MIME types(TEXT, HTML, URI)</i></u> to send and receive data.\n" + 
+                                     "* In order to send and receive data, the requested <u><i>MIME type must be the same.</i></u>\n" +
+                                     "* For example, if copy the TEXT type and try to paste the HTML type, may receive a failure callback.";
+
+            mainView.Add(NewInfo(descriptionText, true));
+            mainView.Add(NewPadding(5));
+            mainView.Add(NewInfo(" ▼ Enter the text you want copy into the TextField"));
+
+            View vertView1 = NewView(true);
+            mainView.Add(vertView1);
+
+            fieldCopy = NewTextField("Enter text to copy", LayoutParamPolicies.MatchParent);
+            vertView1.Add(fieldCopy);
+
+            Button buttonCopyClear = NewButton("Clear");
+            vertView1.Add(buttonCopyClear);
+            buttonCopyClear.Clicked += (s, e) =>
+            {
+                fieldCopy.Text = "";
+                labelType.Text = " ";
+                labelData.Text = " ";
+            };
+
+            //Copy & Paste Button
+            mainView.Add(NewPadding(5));
+            mainView.Add(NewInfo(" ▼ Select Copy or Paste with MIME type"));
+
+            // plain text
+            View vertView4 = NewView(true);
+            mainView.Add(vertView4);
+
+            vertView4.Add(NewTextLabel($"MIME type : {MIME_TYPE_PLAIN_TEXT}", LayoutParamPolicies.MatchParent));
+
+            buttonCopy = NewButton("Copy");
+            vertView4.Add(buttonCopy);
+
+            buttonPaste = NewButton("Paste");
+            vertView4.Add(buttonPaste);
+
+            // html
+            View vertView5 = NewView(true);
+            mainView.Add(vertView5);
+
+            vertView5.Add(NewTextLabel($"MIME type : {MIME_TYPE_HTML}", LayoutParamPolicies.MatchParent));
+
+            buttonCopyHtml = NewButton("Copy");
+            vertView5.Add(buttonCopyHtml);
+
+            buttonPasteHtml = NewButton("Paste");
+            vertView5.Add(buttonPasteHtml);
+
+            // text uri
+            View vertView6 = NewView(true);
+            mainView.Add(vertView6);
+
+            vertView6.Add(NewTextLabel($"MIME type : {MIME_TYPE_TEXT_URI}", LayoutParamPolicies.MatchParent));
+
+            buttonCopyUri = NewButton("Copy");
+            vertView6.Add(buttonCopyUri);
+
+            buttonPasteUri = NewButton("Paste");
+            vertView6.Add(buttonPasteUri);
+
+            // Bottom UI
+            mainView.Add(NewPadding(5));
+            mainView.Add(NewInfo(" ▼ Pasted information"));
+
+            // info type
+            View vertView2 = NewView(true);
+            mainView.Add(vertView2);
+
+            vertView2.Add(NewTextLabel("MIME type", 300));
+
+            labelType = NewTextLabel(" ", LayoutParamPolicies.MatchParent);
+            vertView2.Add(labelType);
+
+            // info data
+            View vertView3 = NewView(true);
+            mainView.Add(vertView3);
+
+            vertView3.Add(NewTextLabel("Data", 300));
+
+            labelData = NewTextLabel(" ", LayoutParamPolicies.MatchParent);
+            vertView3.Add(labelData);
+        }
+
         public View NewView(bool horizontal)
         {
             var view = new View()
@@ -125,7 +212,7 @@ namespace NUIClipboard
                 {
                     LinearOrientation = horizontal ? LinearLayout.Orientation.Horizontal : LinearLayout.Orientation.Vertical,
                     LinearAlignment = LinearLayout.Alignment.Begin,
-                    CellPadding = new Size2D(10, 20),
+                    CellPadding = new Size2D(10, 10),
                 },
                 WidthSpecification = LayoutParamPolicies.MatchParent,
                 HeightSpecification = LayoutParamPolicies.WrapContent,
@@ -134,7 +221,17 @@ namespace NUIClipboard
             return view;
         }
 
-        public TextLabel NewTextLabel(string text, int width)
+        public View NewPadding(int height)
+        {
+            var view = new View()
+            {
+                WidthSpecification = LayoutParamPolicies.MatchParent,
+                HeightSpecification = height,
+            };
+            return view;
+        }
+
+        public TextLabel NewTextLabel(string text, int width, bool borderLine = true)
         {
             var label = new TextLabel
             {
@@ -142,13 +239,29 @@ namespace NUIClipboard
                 MultiLine = true,
                 WidthSpecification = width,
                 HeightSpecification = LayoutParamPolicies.WrapContent,
-                PointSize = 25.0f,
+                PointSize = 20.0f,
                 BackgroundColor = Color.White,
-                BorderlineWidth = 1.0f,
+                BorderlineWidth = borderLine ? 1.0f : 0.0f,
+                EnableMarkup = true,
             };
             return label;
         }
 
+        public TextLabel NewInfo(string text, bool borderLine = false)
+        {
+            var info = NewTextLabel(text, LayoutParamPolicies.MatchParent, borderLine);
+            info.PointSize = 16.0f;
+            return info;
+        }
+
+        public TextLabel NewTitle(string text)
+        {
+            var title = NewTextLabel(text, LayoutParamPolicies.MatchParent);
+            title.HorizontalAlignment = HorizontalAlignment.Center;
+            title.PointSize = 25.0f;
+            return title;
+        }
+
         public TextField NewTextField(string placeholderText, int width)
         {
             var field = new TextField
@@ -157,9 +270,10 @@ namespace NUIClipboard
                 PlaceholderTextFocused = placeholderText,
                 WidthSpecification = width,
                 HeightSpecification = LayoutParamPolicies.WrapContent,
-                PointSize = 25.0f,
+                PointSize = 20.0f,
                 BackgroundColor = Color.White,
                 BorderlineWidth = 1.0f,
+                MaxLength = 1000,
             };
             return field;
         }
@@ -169,9 +283,10 @@ namespace NUIClipboard
             var button = new Button(NewButtonStyle())
             {
                 Text = text,
-                PointSize = 25.0f,
+                PointSize = 20.0f,
                 WidthSpecification = 200,
                 HeightSpecification = LayoutParamPolicies.WrapContent,
+                ItemHorizontalAlignment = HorizontalAlignment.Center,
             };
             return button;
         }
index f0dc005..80ff348 100644 (file)
@@ -15,6 +15,7 @@ namespace NUIClipboardDataSelected
     class Program : NUIApplication
     {
         const string TAG = "clipboard";
+        const string MIME_TYPE_PLAIN_TEXT = "text/plain;charset=utf-8";
 
         protected override void OnCreate()
         {
@@ -27,22 +28,25 @@ namespace NUIClipboardDataSelected
             Window.Instance.WindowSize = new Size(1, 1);
             Window.Instance.BackgroundColor = Color.White;
 
-
             Tizen.NUI.WindowSystem.Shell.TizenShell tzShell;
             tzShell = new Tizen.NUI.WindowSystem.Shell.TizenShell();
             Window.Instance.SetAcceptFocus(false);
 
             Tizen.NUI.WindowSystem.Shell.KVMService kvmService;
-            // window that will act as KVM Service.
+            // Window that will act as KVM Service.
             kvmService = new Tizen.NUI.WindowSystem.Shell.KVMService(tzShell, Window.Instance); 
             kvmService.SetSecondarySelction();
 
-            // Add a dummy view for easy debugging.
+            // This view has nothing to do with this test, just for easy debugging.
+            // If there is a view, it is exposed to the process monitor.
             View view = NewView();
             Window.Instance.GetDefaultLayer().Add(view);
 
             // Register event handler.
             Clipboard.Instance.DataSelected += OnClipboardDataSelected;
+
+            // Self copy test.
+            CopyTest();
         }
 
         // When copy occurs somewhere, this callback is invoked.
@@ -69,6 +73,25 @@ namespace NUIClipboardDataSelected
             Tizen.Log.Info(TAG, $"OnClipboardDataReceived type:{clipEvent.MimeType}, data:{clipEvent.Data}\n");
         }
 
+        public void CopyTest()
+        {
+            // Self copy test.
+            // * SetData() is called 5 seconds after app execution.
+            // * Observe the Log of OnClipboardDataSelected.
+            // * If the log is output, there is a problem somewhere.
+            // * DataSelected event should not be invoked
+            // * by the SetData() called within the SecondarySelection.
+            Timer timer = new Timer(5000);
+            timer.Tick += (s, e) =>
+            {
+                string data = "Lorem ipsum dolor sit amet consectetuer";
+                Tizen.Log.Info(TAG, $"SetData type:{MIME_TYPE_PLAIN_TEXT}, data:{data}\n");
+                Clipboard.Instance.SetData(MIME_TYPE_PLAIN_TEXT, data);
+                return false;
+            };
+            timer.Start();
+        }
+
         public View NewView()
         {
             var view = new View()