[NUI][TEST] add widget sample for sending message
[platform/core/csapi/tizenfx.git] / test / Tizen.NUI.WidgetViewTest / 1.SendInfo / Tizen.NUI.WidgetViewTest / SimpleWidgetViewApp.cs
1 /*
2  * Copyright (c) 2022 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 System;
18 using System.Collections.Generic;
19 using Tizen.NUI;
20 using Tizen.NUI.BaseComponents;
21 using Tizen.Applications;
22 using Tizen.Applications.Messages;
23
24 namespace WidgetApplicationTemplate
25 {
26     class Program : NUIApplication
27     {
28         string widgetAppId = "Tizen.NUI.WidgetTest";
29         string rcvPort = "my_widget_port";
30
31         protected override void OnCreate()
32         {
33             base.OnCreate();
34             Initialize();
35         }
36         void Initialize()
37         {
38             Window window = GetDefaultWindow();
39
40             window.KeyEvent += OnKeyEvent;
41             window.TouchEvent += OnTouchEvent;
42             
43             rootView = new View();
44             rootView.BackgroundColor = Color.White;
45             rootView.Size = Window.Instance.Size;
46             rootView.PivotPoint = PivotPoint.Center;
47             window.GetDefaultLayer().Add(rootView);
48
49             TextLabel sampleLabel = new TextLabel("Widget Viewer ");
50             sampleLabel.FontFamily = "SamsungOneUI 500";
51             sampleLabel.PointSize = 8;
52             sampleLabel.TextColor = Color.Black;
53             sampleLabel.SizeWidth = 300;
54             sampleLabel.PivotPoint = PivotPoint.Center;
55             rootView.Add(sampleLabel);
56
57             Bundle bundle = new Bundle();
58             bundle.AddItem("COUNT", "1");
59             String encodedBundle = bundle.Encode();
60
61             widgetWidth = 500;
62             widgetHeight = 500;
63             mWidgetView = WidgetViewManager.Instance.AddWidget("class1@Tizen.NUI.WidgetTest", encodedBundle, widgetWidth, widgetHeight, 0.0f);
64             mWidgetView.Position = new Position(100,100);
65             window.GetDefaultLayer().Add(mWidgetView);
66
67             //mWidgetView.WidgetContentUpdated += OnWidgetContentUpdatedCB;
68
69             mWidgetView2 = WidgetViewManager.Instance.AddWidget("class2@Tizen.NUI.WidgetTest", encodedBundle, widgetWidth, widgetHeight, 0.0f);
70             mWidgetView2.Position = new Position(100, widgetHeight + 110);
71             window.GetDefaultLayer().Add(mWidgetView2);
72
73             // Send message using port
74             _msgPort = new MessagePort(rcvPort, false);
75             Tizen.Log.Info("NUI", "MessagePort Create: " + _msgPort.PortName + "Trusted: " + _msgPort.Trusted);
76             _msgPort.Listen();
77         }
78         public void OnKeyEvent(object sender, Window.KeyEventArgs e)
79         {
80             if (e.Key.State == Key.StateType.Down )
81             {
82                 Tizen.Log.Info("NUI", "OnKeyEvent(View-Window) : " + e.Key.KeyPressedName + "\n");
83                 if (e.Key.KeyPressedName == "1")
84                 {
85                     widgetWidth += 200;
86                     widgetHeight += 200;
87                     if(widgetWidth > 1000 || widgetHeight > 1000)
88                     {
89                         widgetWidth = 200;
90                         widgetHeight = 200;
91                     }
92                     mWidgetView.Size2D = new Size2D(widgetWidth, widgetHeight);
93
94                     // Send the WidgetView's width to Widget
95                     var msg = new Bundle();
96                     msg.AddItem("message", "WidgetView's message >> width:" + widgetWidth);
97                     _msgPort.Send(msg, widgetAppId, rcvPort);
98                 }
99
100             }
101         }
102         private void OnTouchEvent(object source, Window.TouchEventArgs e)
103         {
104         }
105
106 /*
107         private void OnWidgetContentUpdatedCB(object sender, WidgetView.WidgetViewEventArgs e)
108         {
109             String encodedBundle = e.WidgetView.ContentInfo;
110             Tizen.Log.Info("NUI", "tscholb : OnWidgetContentUpdatedCB : " + encodedBundle + "\n");
111             Bundle bundle = Bundle.Decode(encodedBundle);
112             string outString;
113             if (bundle.TryGetItem("COUNT", out outString))
114             {
115                 Tizen.Log.Info("NUI", "OnWidgetContentUpdatedCB(2) : " + outString + "\n");
116             }
117
118         }
119 */
120
121         static void Main(string[] args)
122         {
123             var app = new Program();
124             app.Run(args);
125         }
126
127         private static MessagePort _msgPort;
128         private View rootView;
129         WidgetView mWidgetView;
130         WidgetView mWidgetView2;
131         int widgetWidth;
132         int widgetHeight;
133     }
134 }
135
136