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 int16_t width = 50;
123 int16_t height = 50;
124 int64_t value = 90;
125 UILabelButton *btn = new UILabelButton();
126 btn->SetText(text);
127 btn->SetPosition(x, y, width, width);
128 btn->SetStyle(STYLE_BORDER_RADIUS, value);
129 parent->Add(btn);
130 }
131
GetView0()132 void ViewDemo::GetView0()
133 {
134 if (view0_ != nullptr || timer_ != nullptr) {
135 return;
136 }
137 view0_ = new UIViewGroup();
138 view0_->SetPosition(0, g_height, Screen::GetInstance().GetWidth(), 200);
139 g_height += view0_->GetHeight();
140 SetUpNav("1", 80, 0, view0_);
141
142 UIDigitalClock *dClock_ = new UIDigitalClock();
143 dClock_->SetPosition(150, 0, 250, 64);
144 dClock_->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 20);
145 dClock_->SetTime24Hour(15, 14, 50);
146 view0_->Add(dClock_);
147 container_->Add(view0_);
148
149 timer_ = new GraphicTimer(1000, TimerCb, dClock_, true);
150 timer_->Start();
151 }
152
GetView1()153 void ViewDemo::GetView1()
154 {
155 if (view1_ != nullptr) {
156 return;
157 }
158 view1_ = new UIViewGroup();
159 view1_->SetPosition(0, g_height, Screen::GetInstance().GetWidth(), 200);
160 g_height += view0_->GetHeight();
161 SetUpNav("2", 80, 0, view1_);
162
163 UILabel *label_ = new UILabel();
164 label_->SetPosition(150, 0, 250, 64);
165 label_->SetText("图形UI组件实现了一套系统级的图形引擎,为应用开发提供UIKit接口,包括了动画、布局、图形转换、事件处理,以及丰富的UI组件。");
166 label_->SetLineBreakMode(UILabel::LINE_BREAK_WRAP);
167 label_->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 20);
168 view1_->Add(label_);
169 container_->Add(view1_);
170 }
171
GetView2()172 void ViewDemo::GetView2()
173 {
174 int16_t x = 0;
175 int16_t y = 0;
176 int16_t width = 0;
177 int16_t height = 200;
178 int8_t size = 20;
179 if (view2_ != nullptr) {
180 return;
181 }
182 view2_ = new UIViewGroup();
183 view2_->SetPosition(x, g_height, Screen::GetInstance().GetWidth(), height);
184 g_height += view0_->GetHeight();
185 x = 80;
186 SetUpNav("3", x, y, view2_);
187
188 UILabelButton *button_ = new UILabelButton();
189 x = 150;
190 width = 120;
191 height = 64;
192 button_->SetPosition(x, y, width, height);
193 button_->SetText("Play");
194 button_->SetFont(DEFAULT_VECTOR_FONT_FILENAME, size);
195 button_->SetOnClickListener(this);
196 view2_->Add(button_);
197 container_->Add(view2_);
198 }
199
OnClick(UIView & view,const ClickEvent & event)200 bool ViewDemo::OnClick(UIView &view, const ClickEvent &event)
201 {
202 if (view.GetViewType() != UI_LABEL_BUTTON) {
203 return true;
204 }
205 UILabelButton *button_ = reinterpret_cast<UILabelButton *>(&view);
206 if (strcmp(button_->GetText(), "Play") == 0) {
207 button_->SetText("Pause");
208 } else if (strcmp(button_->GetText(), "Pause") == 0) {
209 button_->SetText("Play");
210 }
211 return true;
212 }
213
214 /****************************************************/
215 class MyScrollView
216 {
217 public:
GetInstance()218 static MyScrollView *GetInstance()
219 {
220 static MyScrollView inst;
221 return &inst;
222 }
223
224 void SetUp();
225 UIView *GetView();
226
227 private:
MyScrollView()228 MyScrollView() {}
229 ~MyScrollView();
230
231 void GetScrollView();
232
233 UIScrollView *container_ = nullptr;
234 };
235
SetUp()236 void MyScrollView::SetUp()
237 {
238 int16_t size = 0;
239 BaseGfxEngine::GetInstance()->SetScreenShape(ScreenShape::CIRCLE);
240
241 if (container_ == nullptr) {
242 container_ = new UIScrollView();
243 }
244
245 container_->SetPosition(0, 0, Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight());
246 container_->SetYScrollBarVisible(true);
247 container_->SetHorizontalScrollState(false);
248 container_->SetReboundSize(size);
249 }
250
~MyScrollView()251 MyScrollView::~MyScrollView()
252 {
253 DeleteChildren(container_);
254 container_ = nullptr;
255 }
256
GetView()257 UIView *MyScrollView::GetView()
258 {
259 GetScrollView();
260 return container_;
261 }
262
GetScrollView()263 void MyScrollView::GetScrollView()
264 {
265 int16_t g_height = 0;
266 int16_t x = 0;
267 int16_t y = 0;
268 int16_t width = 0;
269 int16_t height = 0;
270 int8_t value = 90;
271 UILabelButton *button1 = new UILabelButton();
272 container_->Add(button1);
273 x = 180;
274 y = 100 + g_height;
275 width = 100;
276 height = 100;
277 button1->SetPosition(x, y, width, height);
278 button1->SetStyle(STYLE_BORDER_RADIUS, value);
279 button1->SetText("button1");
280
281 UILabelButton *button2 = new UILabelButton();
282 container_->Add(button2);
283 x = 180;
284 y = 250 + g_height;
285 button2->SetPosition(x, y, width, height);
286 button2->SetStyle(STYLE_BORDER_RADIUS, value);
287 button2->SetText("button2");
288 y = 400 + g_height;
289 UILabelButton *button3 = new UILabelButton();
290 container_->Add(button3);
291 button3->SetPosition(x, y, width, height);
292 button3->SetStyle(STYLE_BORDER_RADIUS, value);
293 button3->SetText("button3");
294
295 UILabelButton *button4 = new UILabelButton();
296 container_->Add(button4);
297 y = 550 + g_height;
298 button4->SetPosition(x, y, width, height);
299 button4->SetStyle(STYLE_BORDER_RADIUS, value);
300 button4->SetText("button4");
301
302 UILabelButton *button5 = new UILabelButton();
303 container_->Add(button5);
304 y = 700 + g_height;
305 button5->SetPosition(x, y, width, height);
306 button5->SetStyle(STYLE_BORDER_RADIUS, value);
307 button5->SetText("button5");
308 }
309
310 class ViewSwitchDemo : public UIView::OnClickListener
311 {
312 public:
GetInstance()313 static ViewSwitchDemo *GetInstance()
314 {
315 static ViewSwitchDemo instance;
316 return &instance;
317 }
318 void SetUp();
319 UIView *GetView();
320
321 private:
ViewSwitchDemo()322 ViewSwitchDemo() {}
323 ~ViewSwitchDemo();
324
325 void GetView0();
326 void GetView1();
327 void GetView2();
328
329 bool OnClick(UIView &view, const ClickEvent &event) override;
330 void SetUpNav(UIViewGroup *parent);
331
332 UIViewGroup *container_ = nullptr;
333 UIViewGroup *view0_ = nullptr;
334 UIViewGroup *view1_ = nullptr;
335 UIViewGroup *view2_ = nullptr;
336 };
337
~ViewSwitchDemo()338 ViewSwitchDemo::~ViewSwitchDemo()
339 {
340 DeleteChildren(container_);
341 container_ = nullptr;
342 view0_ = nullptr;
343 view1_ = nullptr;
344 view2_ = nullptr;
345 }
346
SetUp()347 void ViewSwitchDemo::SetUp()
348 {
349 int x = 0;
350 int y = 0;
351 if (container_ != nullptr) {
352 return;
353 }
354 container_ = new UIViewGroup();
355 container_->SetPosition(x, y, Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight());
356 }
357
GetView()358 UIView *ViewSwitchDemo::GetView()
359 {
360 GetView0();
361 container_->Add(view0_);
362 return container_;
363 }
364
SetUpNav(UIViewGroup * container)365 void ViewSwitchDemo::SetUpNav(UIViewGroup *container)
366 {
367 int16_t x = 100;
368 int16_t y = 350;
369 int16_t width = 120;
370 int16_t height = 50;
371 UILabelButton *btn1 = new UILabelButton();
372 btn1->SetPosition(x, y, width, height);
373 btn1->SetText("上一页");
374 btn1->SetOnClickListener(this);
375
376 UILabelButton *btn2 = new UILabelButton();
377 x = 250;
378 btn2->SetPosition(x, y, width, height);
379 btn2->SetText("下一页");
380 btn2->SetOnClickListener(this);
381
382 container->Add(btn1);
383 container->Add(btn2);
384 }
385
GetView0()386 void ViewSwitchDemo::GetView0()
387 {
388 int16_t x = 0;
389 int16_t y = 0;
390 int16_t width = 0;
391 int16_t height = 0;
392 int8_t size = 20;
393 if (view0_ != nullptr) {
394 return;
395 }
396 view0_ = new UIViewGroup();
397 view0_->SetPosition(x, y, Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight());
398
399 UILabel *label_ = new UILabel();
400 label_->SetPosition(x, y, width, height);
401 label_->SetText("图形UI组件实现了一套系统级的图形引擎。");
402 label_->SetLineBreakMode(UILabel::LINE_BREAK_WRAP);
403 label_->SetFont(DEFAULT_VECTOR_FONT_FILENAME, size);
404
405 view0_->Add(label_);
406 SetUpNav(view0_);
407 }
408
GetView1()409 void ViewSwitchDemo::GetView1()
410 {
411 int16_t x = 0;
412 int16_t y = 0;
413 int16_t width = 0;
414 int16_t height = 0;
415 uint8_t size = 20;
416 if (view1_ != nullptr) {
417 return;
418 }
419 view1_ = new UIViewGroup();
420 view1_->SetPosition(x, y, Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight());
421
422 UILabel *label_ = new UILabel();
423 x = 100;
424 y = 100;
425 width = 250;
426 height = 64;
427 label_->SetPosition(x, y, width, height);
428 label_->SetText("该组件为应用开发提供UIKit接口,包括了动画、布局、图形转换、事件处理,以及丰富的UI组件。"
429 "组件内部直接调用HAL接口,或者使用WMS(Window Manager Service)提供的客户端与硬件交互,以完成事件响应、图像绘制等操作。");
430 label_->SetLineBreakMode(UILabel::LINE_BREAK_WRAP);
431 label_->SetFont(DEFAULT_VECTOR_FONT_FILENAME, size);
432
433 view1_->Add(label_);
434 SetUpNav(view1_);
435 }
436
GetView2()437 void ViewSwitchDemo::GetView2()
438 {
439 int16_t x = 0;
440 int16_t y = 0;
441 int16_t width = 0;
442 int16_t height = 0;
443 int16_t size = 20;
444 if (view2_ != nullptr) {
445 return;
446 }
447 view2_ = new UIViewGroup();
448 view2_->SetPosition(x, y, Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight());
449
450 UILabel *label_ = new UILabel();
451 x = 100;
452 y = 100;
453 width = 250;
454 height = 64;
455 label_->SetPosition(x, y, width, height);
456 label_->SetText("组件分为基础组件和容器组件"
457 "- 基础组件:仅实现组件自身单一功能,比如按钮、文字、图片等;"
458 "- 容器组件:可将其他组件作为自己的子组件,通过组合实现复杂功能。");
459 label_->SetLineBreakMode(UILabel::LINE_BREAK_WRAP);
460 label_->SetFont(DEFAULT_VECTOR_FONT_FILENAME, size);
461
462 view2_->Add(label_);
463 SetUpNav(view2_);
464 }
465
OnClick(UIView & view,const ClickEvent & event)466 bool ViewSwitchDemo::OnClick(UIView &view, const ClickEvent &event)
467 {
468 if (view.GetViewType() != UI_LABEL_BUTTON) {
469 return true;
470 }
471 UILabelButton *button_ = reinterpret_cast<UILabelButton *>(&view);
472 UIView *parent_ = button_->GetParent();
473 if (parent_ == view0_) {
474 if (strcmp(button_->GetText(), "下一页") == 0) {
475 container_->RemoveAll();
476 GetView1();
477 container_->Add(view1_);
478 container_->Invalidate();
479 }
480 } else if (parent_ == view1_) {
481 if (strcmp(button_->GetText(), "上一页") == 0) {
482 container_->RemoveAll();
483 GetView0();
484 container_->Add(view0_);
485 container_->Invalidate();
486 } else if (strcmp(button_->GetText(), "下一页") == 0) {
487 container_->RemoveAll();
488 GetView2();
489 container_->Add(view2_);
490 container_->Invalidate();
491 }
492 } else if (parent_ == view2_) {
493 if (strcmp(button_->GetText(), "上一页") == 0) {
494 container_->RemoveAll();
495 GetView1();
496 container_->Add(view1_);
497 container_->Invalidate();
498 }
499 }
500 return true;
501 }
502
ViewDemoStart(void)503 void ViewDemoStart(void)
504 {
505 int16_t x = 0;
506 int16_t y = 0;
507 RootView *rootView_ = RootView::GetInstance();
508 rootView_->SetPosition(x, y, Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight());
509 ViewDemo *view = ViewDemo::GetInstance();
510 // MyScrollView *view = MyScrollView::GetInstance();
511 // ViewSwitchDemo *view = ViewSwitchDemo::GetInstance();
512 view->SetUp();
513 rootView_->Add(view->GetView());
514 rootView_->Invalidate();
515 }
516