1 /*
2 * Copyright (c) 2021 Bestechnic (Shanghai) Co., Ltd. All rights reserved.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "common/screen.h"
16 #include "components/root_view.h"
17 #include "components/ui_digital_clock.h"
18 #include "components/ui_label.h"
19 #include "components/ui_label_button.h"
20 #include "components/ui_scroll_view.h"
21 #include "components/ui_swipe_view.h"
22 #include "components/ui_view_group.h"
23 #include "graphic_config.h"
24 #include "graphic_timer.h"
25 #include "hilog/log.h"
26 #include "ui_test.h"
27
28 #undef LOG_TAG
29 #define LOG_TAG "ViewDemo"
30
31 using namespace OHOS;
32
DeleteChildren(UIView * view)33 static void DeleteChildren(UIView *view)
34 {
35 if (view == nullptr) {
36 return;
37 }
38 while (view) {
39 UIView *tempView = view;
40 view = view->GetNextSibling();
41 if (tempView->IsViewGroup()) {
42 DeleteChildren(static_cast<UIViewGroup *>(tempView)->GetChildrenHead());
43 }
44 if (tempView->GetParent()) {
45 static_cast<UIViewGroup *>(tempView->GetParent())->Remove(tempView);
46 }
47 delete tempView;
48 }
49 }
50
51 class ViewDemo : public UIView::OnClickListener
52 {
53 public:
GetInstance()54 static ViewDemo *GetInstance()
55 {
56 static ViewDemo instance;
57 return &instance;
58 }
59 void SetUp();
60 UIView *GetView();
61
62 private:
ViewDemo()63 ViewDemo() {}
64 ~ViewDemo();
65
66 void GetView0();
67 void GetView1();
68 void GetView2();
69
70 bool OnClick(UIView &view, const ClickEvent &event) override;
71 void SetUpNav(const char *text, int x, int y, UIViewGroup *parent);
TimerCb(void * arg)72 static void TimerCb(void *arg)
73 {
74 UIDigitalClock *clock = reinterpret_cast<UIDigitalClock *>(arg);
75 if (clock) {
76 clock->IncOneSecond();
77 }
78 }
79 UIScrollView *container_ = nullptr;
80 UIViewGroup *view0_ = nullptr;
81 UIViewGroup *view1_ = nullptr;
82 UIViewGroup *view2_ = nullptr;
83 GraphicTimer *timer_ = nullptr;
84 int g_height = 50;
85 };
86
~ViewDemo()87 ViewDemo::~ViewDemo()
88 {
89 DeleteChildren(container_);
90 container_ = nullptr;
91 view0_ = nullptr;
92 view1_ = nullptr;
93 view2_ = nullptr;
94 if (timer_) {
95 delete timer_;
96 timer_ = nullptr;
97 }
98 }
99
SetUp()100 void ViewDemo::SetUp()
101 {
102 if (container_ != nullptr) {
103 return;
104 }
105 container_ = new UIScrollView();
106 container_->SetPosition(0, 0, Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight());
107 container_->SetYScrollBarVisible(true);
108 container_->SetHorizontalScrollState(false);
109 container_->SetReboundSize(50);
110 }
111
GetView()112 UIView *ViewDemo::GetView()
113 {
114 GetView0();
115 GetView1();
116 GetView2();
117 return container_;
118 }
119
SetUpNav(const char * text,int x,int y,UIViewGroup * parent)120 void ViewDemo::SetUpNav(const char *text, int x, int y, UIViewGroup *parent)
121 {
122 UILabelButton *btn = new UILabelButton();
123 btn->SetText(text);
124 btn->SetPosition(x, y, 50, 50);
125 btn->SetStyle(STYLE_BORDER_RADIUS, 90);
126 parent->Add(btn);
127 }
128
GetView0()129 void ViewDemo::GetView0()
130 {
131 if (view0_ != nullptr || timer_ != nullptr) {
132 return;
133 }
134 view0_ = new UIViewGroup();
135 view0_->SetPosition(0, g_height, Screen::GetInstance().GetWidth(), 200);
136 g_height += view0_->GetHeight();
137 SetUpNav("1", 80, 0, view0_);
138
139 UIDigitalClock *dClock_ = new UIDigitalClock();
140 dClock_->SetPosition(150, 0, 250, 64);
141 dClock_->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 20);
142 dClock_->SetTime24Hour(15, 14, 50);
143 view0_->Add(dClock_);
144 container_->Add(view0_);
145
146 timer_ = new GraphicTimer(1000, TimerCb, dClock_, true);
147 timer_->Start();
148 }
149
GetView1()150 void ViewDemo::GetView1()
151 {
152 if (view1_ != nullptr) {
153 return;
154 }
155 view1_ = new UIViewGroup();
156 view1_->SetPosition(0, g_height, Screen::GetInstance().GetWidth(), 200);
157 g_height += view0_->GetHeight();
158 SetUpNav("2", 80, 0, view1_);
159
160 UILabel *label_ = new UILabel();
161 label_->SetPosition(150, 0, 250, 64);
162 label_->SetText("图形UI组件实现了一套系统级的图形引擎,为应用开发提供UIKit接口,包括了动画、布局、图形转换、事件处理,以及丰富的UI组件。");
163 label_->SetLineBreakMode(UILabel::LINE_BREAK_WRAP);
164 label_->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 20);
165 view1_->Add(label_);
166 container_->Add(view1_);
167 }
168
GetView2()169 void ViewDemo::GetView2()
170 {
171 if (view2_ != nullptr) {
172 return;
173 }
174 view2_ = new UIViewGroup();
175 view2_->SetPosition(0, g_height, Screen::GetInstance().GetWidth(), 200);
176 g_height += view0_->GetHeight();
177 SetUpNav("3", 80, 0, view2_);
178
179 UILabelButton *button_ = new UILabelButton();
180 button_->SetPosition(150, 0, 120, 64);
181 button_->SetText("Play");
182 button_->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 20);
183 button_->SetOnClickListener(this);
184 view2_->Add(button_);
185 container_->Add(view2_);
186 }
187
OnClick(UIView & view,const ClickEvent & event)188 bool ViewDemo::OnClick(UIView &view, const ClickEvent &event)
189 {
190 if (view.GetViewType() != UI_LABEL_BUTTON) {
191 return true;
192 }
193 UILabelButton *button_ = reinterpret_cast<UILabelButton *>(&view);
194 if (strcmp(button_->GetText(), "Play") == 0) {
195 button_->SetText("Pause");
196 } else if (strcmp(button_->GetText(), "Pause") == 0) {
197 button_->SetText("Play");
198 }
199 return true;
200 }
201
202 /****************************************************/
203 class MyScrollView
204 {
205 public:
GetInstance()206 static MyScrollView *GetInstance()
207 {
208 static MyScrollView inst;
209 return &inst;
210 }
211
212 void SetUp();
213 UIView *GetView();
214
215 private:
MyScrollView()216 MyScrollView() {}
217 ~MyScrollView();
218
219 void GetScrollView();
220
221 UIScrollView *container_ = nullptr;
222 };
223
SetUp()224 void MyScrollView::SetUp()
225 {
226 BaseGfxEngine::GetInstance()->SetScreenShape(ScreenShape::CIRCLE);
227
228 if (container_ == nullptr) {
229 container_ = new UIScrollView();
230 }
231
232 container_->SetPosition(0, 0, Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight());
233 container_->SetYScrollBarVisible(true);
234 container_->SetHorizontalScrollState(false);
235 container_->SetReboundSize(50);
236 }
237
~MyScrollView()238 MyScrollView::~MyScrollView()
239 {
240 DeleteChildren(container_);
241 container_ = nullptr;
242 }
243
GetView()244 UIView *MyScrollView::GetView()
245 {
246 GetScrollView();
247 return container_;
248 }
249
GetScrollView()250 void MyScrollView::GetScrollView()
251 {
252 int16_t g_height = 0;
253 UILabelButton *button1 = new UILabelButton();
254 container_->Add(button1);
255 button1->SetPosition(180, 100 + g_height, 100, 100);
256 button1->SetStyle(STYLE_BORDER_RADIUS, 90);
257 button1->SetText("button1");
258
259 UILabelButton *button2 = new UILabelButton();
260 container_->Add(button2);
261 button2->SetPosition(180, 250 + g_height, 100, 100);
262 button2->SetStyle(STYLE_BORDER_RADIUS, 90);
263 button2->SetText("button2");
264
265 UILabelButton *button3 = new UILabelButton();
266 container_->Add(button3);
267 button3->SetPosition(180, 400 + g_height, 100, 100);
268 button3->SetStyle(STYLE_BORDER_RADIUS, 90);
269 button3->SetText("button3");
270
271 UILabelButton *button4 = new UILabelButton();
272 container_->Add(button4);
273 button4->SetPosition(180, 550 + g_height, 100, 100);
274 button4->SetStyle(STYLE_BORDER_RADIUS, 90);
275 button4->SetText("button4");
276
277 UILabelButton *button5 = new UILabelButton();
278 container_->Add(button5);
279 button5->SetPosition(180, 700 + g_height, 100, 100);
280 button5->SetStyle(STYLE_BORDER_RADIUS, 90);
281 button5->SetText("button5");
282 }
283
284 class ViewSwitchDemo : public UIView::OnClickListener
285 {
286 public:
GetInstance()287 static ViewSwitchDemo *GetInstance()
288 {
289 static ViewSwitchDemo instance;
290 return &instance;
291 }
292 void SetUp();
293 UIView *GetView();
294
295 private:
ViewSwitchDemo()296 ViewSwitchDemo() {}
297 ~ViewSwitchDemo();
298
299 void GetView0();
300 void GetView1();
301 void GetView2();
302
303 bool OnClick(UIView &view, const ClickEvent &event) override;
304 void SetUpNav(UIViewGroup *parent);
305
306 UIViewGroup *container_ = nullptr;
307 UIViewGroup *view0_ = nullptr;
308 UIViewGroup *view1_ = nullptr;
309 UIViewGroup *view2_ = nullptr;
310 };
311
~ViewSwitchDemo()312 ViewSwitchDemo::~ViewSwitchDemo()
313 {
314 DeleteChildren(container_);
315 container_ = nullptr;
316 view0_ = nullptr;
317 view1_ = nullptr;
318 view2_ = nullptr;
319 }
320
SetUp()321 void ViewSwitchDemo::SetUp()
322 {
323 if (container_ != nullptr) {
324 return;
325 }
326 container_ = new UIViewGroup();
327 container_->SetPosition(0, 0, Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight());
328 }
329
GetView()330 UIView *ViewSwitchDemo::GetView()
331 {
332 GetView0();
333 container_->Add(view0_);
334 return container_;
335 }
336
SetUpNav(UIViewGroup * container)337 void ViewSwitchDemo::SetUpNav(UIViewGroup *container)
338 {
339 UILabelButton *btn1 = new UILabelButton();
340 btn1->SetPosition(100, 350, 120, 50);
341 btn1->SetText("上一页");
342 btn1->SetOnClickListener(this);
343
344 UILabelButton *btn2 = new UILabelButton();
345 btn2->SetPosition(250, 350, 120, 50);
346 btn2->SetText("下一页");
347 btn2->SetOnClickListener(this);
348
349 container->Add(btn1);
350 container->Add(btn2);
351 }
352
GetView0()353 void ViewSwitchDemo::GetView0()
354 {
355 if (view0_ != nullptr) {
356 return;
357 }
358 view0_ = new UIViewGroup();
359 view0_->SetPosition(0, 0, Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight());
360
361 UILabel *label_ = new UILabel();
362 label_->SetPosition(100, 100, 250, 64);
363 label_->SetText("图形UI组件实现了一套系统级的图形引擎。");
364 label_->SetLineBreakMode(UILabel::LINE_BREAK_WRAP);
365 label_->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 20);
366
367 view0_->Add(label_);
368 SetUpNav(view0_);
369 }
370
GetView1()371 void ViewSwitchDemo::GetView1()
372 {
373 if (view1_ != nullptr) {
374 return;
375 }
376 view1_ = new UIViewGroup();
377 view1_->SetPosition(0, 0, Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight());
378
379 UILabel *label_ = new UILabel();
380 label_->SetPosition(100, 100, 250, 64);
381 label_->SetText("该组件为应用开发提供UIKit接口,包括了动画、布局、图形转换、事件处理,以及丰富的UI组件。"
382 "组件内部直接调用HAL接口,或者使用WMS(Window Manager Service)提供的客户端与硬件交互,以完成事件响应、图像绘制等操作。");
383 label_->SetLineBreakMode(UILabel::LINE_BREAK_WRAP);
384 label_->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 20);
385
386 view1_->Add(label_);
387 SetUpNav(view1_);
388 }
389
GetView2()390 void ViewSwitchDemo::GetView2()
391 {
392 if (view2_ != nullptr) {
393 return;
394 }
395 view2_ = new UIViewGroup();
396 view2_->SetPosition(0, 0, Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight());
397
398 UILabel *label_ = new UILabel();
399 label_->SetPosition(100, 100, 250, 64);
400 label_->SetText("组件分为基础组件和容器组件"
401 "- 基础组件:仅实现组件自身单一功能,比如按钮、文字、图片等;"
402 "- 容器组件:可将其他组件作为自己的子组件,通过组合实现复杂功能。");
403 label_->SetLineBreakMode(UILabel::LINE_BREAK_WRAP);
404 label_->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 20);
405
406 view2_->Add(label_);
407 SetUpNav(view2_);
408 }
409
OnClick(UIView & view,const ClickEvent & event)410 bool ViewSwitchDemo::OnClick(UIView &view, const ClickEvent &event)
411 {
412 if (view.GetViewType() != UI_LABEL_BUTTON) {
413 return true;
414 }
415 UILabelButton *button_ = reinterpret_cast<UILabelButton *>(&view);
416 UIView *parent_ = button_->GetParent();
417 if (parent_ == view0_) {
418 if (strcmp(button_->GetText(), "下一页") == 0) {
419 container_->RemoveAll();
420 GetView1();
421 container_->Add(view1_);
422 container_->Invalidate();
423 }
424 } else if (parent_ == view1_) {
425 if (strcmp(button_->GetText(), "上一页") == 0) {
426 container_->RemoveAll();
427 GetView0();
428 container_->Add(view0_);
429 container_->Invalidate();
430 } else if (strcmp(button_->GetText(), "下一页") == 0) {
431 container_->RemoveAll();
432 GetView2();
433 container_->Add(view2_);
434 container_->Invalidate();
435 }
436 } else if (parent_ == view2_) {
437 if (strcmp(button_->GetText(), "上一页") == 0) {
438 container_->RemoveAll();
439 GetView1();
440 container_->Add(view1_);
441 container_->Invalidate();
442 }
443 }
444 return true;
445 }
446
ViewDemoStart(void)447 void ViewDemoStart(void)
448 {
449 RootView *rootView_ = RootView::GetInstance();
450 rootView_->SetPosition(0, 0, Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight());
451 ViewDemo *view = ViewDemo::GetInstance();
452 // MyScrollView *view = MyScrollView::GetInstance();
453 // ViewSwitchDemo *view = ViewSwitchDemo::GetInstance();
454 view->SetUp();
455 rootView_->Add(view->GetView());
456 rootView_->Invalidate();
457 }
458