fbb9eedf97520a5f3c51b9ca1dce1992935997a2
[platform/core/csapi/tizenfx.git] / test / NUITizenGallery / Examples / EditorTest2 / EditorTest2.xaml.cs
1 /*
2  * Copyright(c) 2021 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17 using Tizen.NUI;
18 using Tizen.NUI.BaseComponents;
19 using Tizen.NUI.Components;
20 namespace NUITizenGallery
21 {
22     public partial class EditorTest2Page : ContentPage
23     {
24         public Size2D viewFullSize;
25
26         public EditorTest2Page()
27         {
28             InitializeComponent();
29             viewFullSize = new Size2D(0, 0);
30
31             // Text set to TextEditor
32             editor.Text = "Event result";
33             editor.Margin = new Extents(20, 20, 20, 0);
34
35             // Text set to TextEditor
36             editor2.Text = "Input";
37             editor2.Margin = new Extents(20, 20, 20, 0);
38
39             // TextChanged Event
40             editor2.TextChanged += OnTextEditorTextChanged;
41
42             underline.Margin = new Extents(20, 20, 5, 20);
43             underline2.Margin = new Extents(20, 20, 0, 10);
44
45             // Set focus highlight to underline 
46             editor.FocusGained += (s, e) =>
47             {
48                 underline.BackgroundColor = Color.Cyan;
49             };
50
51             editor.FocusLost += (s, e) =>
52             {
53                 underline.BackgroundColor = Color.Gray;
54             };
55
56             editor2.FocusGained += (s, e) =>
57             {
58                 underline2.BackgroundColor = Color.Cyan;
59             };
60
61             editor2.FocusLost += (s, e) =>
62             {
63                 underline2.BackgroundColor = Color.Gray;
64                 editor.Text = "Editing completed";
65             };
66
67             // View size adjustment from ime state
68             InputMethodContext imeEditor = editor.GetInputMethodContext();
69             imeEditor.StatusChanged += OnImeStatusChanged;
70             InputMethodContext imeEditor2 = editor2.GetInputMethodContext();
71             imeEditor2.StatusChanged += OnImeStatusChanged;
72         }
73
74         private void OnTextEditorTextChanged(object sender, TextEditor.TextChangedEventArgs e)
75         {
76             editor.Text = editor2.Text;
77             editor.VerticalScrollPosition = 9999;
78         }
79
80         private void OnImeStatusChanged(object sender, InputMethodContext.StatusChangedEventArgs e)
81         {
82             if (e.StatusChanged)
83             {
84                 // When the virtual keyboard (IME) is shown, StatusChanged is true
85                 var resizedIME = sender as InputMethodContext;
86                 Rectangle rectangle = resizedIME.GetInputMethodArea();
87
88                 if (rectangle.Height > viewFullSize.Height)
89                 {
90                     viewFullSize = (Size2D)mainView.Size2D.Clone();
91                 }
92                 
93                 int width = viewFullSize.Width;
94                 int height = viewFullSize.Height - rectangle.Height;
95                 mainView.Size2D = new Size2D(width, height);
96             }
97             else
98             {
99                 mainView.Size2D = viewFullSize;
100             }
101
102             // Set bounding box for text decoration
103             // This prevents the cursor handle from leaving the valid area.
104             editor.DecorationBoundingBox = new Rectangle(mainView.Position2D.X, mainView.Position2D.Y, mainView.Size2D.Width, mainView.Size2D.Height);
105             editor2.DecorationBoundingBox = new Rectangle(mainView.Position2D.X, mainView.Position2D.Y, mainView.Size2D.Width, mainView.Size2D.Height);
106         }
107     }
108 }