1 /*
2 * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
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
16 #include "components/ui_dialog.h"
17 #include "common/screen.h"
18 #include "common/typed_text.h"
19 #if ENABLE_DEBUG
20 #include "gfx_utils/graphic_assert.h"
21 #endif
22 #include "gfx_utils/graphic_log.h"
23 #if ENABLE_WINDOW
24 #include "window/window.h"
25 #endif
26
27 #if ENABLE_WINDOW
28 namespace OHOS {
29 class UIDialogLabelButton : public UILabelButton {
30 public:
UIDialogLabelButton(UIDialog * dialog)31 explicit UIDialogLabelButton(UIDialog* dialog)
32 {
33 dialog_ = dialog;
34 }
~UIDialogLabelButton()35 virtual ~UIDialogLabelButton() {}
OnClickEvent(const ClickEvent & event)36 virtual bool OnClickEvent(const ClickEvent& event)
37 {
38 bool ret = true;
39 if (dialog_ != nullptr) {
40 dialog_->DestroyWindow();
41 dialog_->isShowing_ = false;
42 }
43 if (onClickListener_ != nullptr) {
44 ret = onClickListener_->OnClick(*this, event);
45 }
46 return ret;
47 }
48 private:
49 UIDialog* dialog_;
50 };
51
52 class UIDialogClickListener : public UIView::OnClickListener {
53 public:
UIDialogClickListener(UIDialog * dialog)54 explicit UIDialogClickListener(UIDialog* dialog)
55 {
56 dialog_ = dialog;
57 }
~UIDialogClickListener()58 virtual ~UIDialogClickListener() {}
OnClick(UIView & view,const ClickEvent & event)59 bool OnClick(UIView& view, const ClickEvent& event) override
60 {
61 bool ret = true;
62 if ((dialog_ != nullptr) && (dialog_->dialogLayer_ != nullptr) &&
63 !dialog_->dialogLayer_->GetRect().IsContains(event.GetCurrentPos())) {
64 if (dialog_->enableAutoCancel_) {
65 dialog_->DestroyWindow();
66 dialog_->isShowing_ = false;
67 }
68 if (dialog_->onCancelListener_ != nullptr) {
69 ret = dialog_->onCancelListener_->OnClick(view, event);
70 }
71 }
72 return ret;
73 }
74 private:
75 UIDialog* dialog_;
76 };
77
UIDialog()78 UIDialog::UIDialog()
79 : isShowing_(false),
80 enableAutoCancel_(false),
81 buttonNum_(0),
82 title_(nullptr),
83 text_(nullptr),
84 button1_(nullptr),
85 button2_(nullptr),
86 button3_(nullptr),
87 dialogLayer_(nullptr),
88 windowRootView_(nullptr),
89 onCancelListener_(nullptr),
90 dialogClickListener_(nullptr),
91 window_(nullptr),
92 line1_(nullptr),
93 line2_(nullptr),
94 titleText_(nullptr),
95 textText_(nullptr)
96 {
97 uint16_t screenWidth = Screen::GetInstance().GetWidth();
98 uint16_t screenHeight = Screen::GetInstance().GetHeight();
99 // 100: calculate percentage
100 widthMax_ = screenWidth * MAX_WIDTH_PERCENT / 100;
101 // 100: calculate percentage
102 heightMax_ = screenHeight * MAX_HEIGHT_PERCENT / 100;
103 colorType1_ = Color::White();
104 colorType2_ = Color::White();
105 colorType3_ = Color::White();
106 }
107
~UIDialog()108 UIDialog::~UIDialog()
109 {
110 onCancelListener_ = nullptr;
111 if (dialogLayer_ != nullptr) {
112 dialogLayer_->RemoveAll();
113 delete dialogLayer_;
114 dialogLayer_ = nullptr;
115 }
116 if (title_ != nullptr) {
117 delete title_;
118 title_ = nullptr;
119 }
120 if (text_ != nullptr) {
121 delete text_;
122 text_ = nullptr;
123 }
124 if (button1_ != nullptr) {
125 delete button1_;
126 button1_ = nullptr;
127 }
128 if (button2_ != nullptr) {
129 delete button2_;
130 button2_ = nullptr;
131 }
132 if (button3_ != nullptr) {
133 delete button3_;
134 button3_ = nullptr;
135 }
136 if (line1_ != nullptr) {
137 delete line1_;
138 line1_ = nullptr;
139 }
140 if (line2_ != nullptr) {
141 delete line2_;
142 line2_ = nullptr;
143 }
144 if ((windowRootView_ != nullptr) && !RootView::DestroyWindowRootView(windowRootView_)) {
145 windowRootView_ = nullptr;
146 }
147 if (dialogClickListener_ != nullptr) {
148 delete dialogClickListener_;
149 dialogClickListener_ = nullptr;
150 }
151 }
152
SetTitle(const char * title)153 void UIDialog::SetTitle(const char* title)
154 {
155 if (title == nullptr) {
156 return;
157 }
158 titleText_ = title;
159 }
160
SetText(const char * text)161 void UIDialog::SetText(const char* text)
162 {
163 if (text == nullptr) {
164 return;
165 }
166 textText_ = text;
167 }
168
SetButton(DialogButtonType buttonType,const char * text,UIView::OnClickListener * listener)169 void UIDialog::SetButton(DialogButtonType buttonType, const char* text, UIView::OnClickListener* listener)
170 {
171 switch (buttonType) {
172 case DialogButtonType::BUTTON_LEFT:
173 if (button1_ == nullptr) {
174 button1_ = new UIDialogLabelButton(this);
175 if (button1_ == nullptr) {
176 GRAPHIC_LOGE("new UIDialogLabelButton fail");
177 return;
178 }
179 button1_->SetViewId(BUTTON1_ID);
180 AddButton(buttonType, button1_, text, listener);
181 } else {
182 button1_->SetText(text);
183 button1_->SetOnClickListener(listener);
184 }
185 break;
186 case DialogButtonType::BUTTON_MID:
187 if (button2_ == nullptr) {
188 button2_ = new UIDialogLabelButton(this);
189 if (button2_ == nullptr) {
190 GRAPHIC_LOGE("new UIDialogLabelButton fail");
191 return;
192 }
193 button2_->SetViewId(BUTTON2_ID);
194 AddButton(buttonType, button2_, text, listener);
195 } else {
196 button2_->SetText(text);
197 button2_->SetOnClickListener(listener);
198 }
199 break;
200 case DialogButtonType::BUTTON_RIGHT:
201 if (button3_ == nullptr) {
202 button3_ = new UIDialogLabelButton(this);
203 if (button3_ == nullptr) {
204 GRAPHIC_LOGE("new UIDialogLabelButton fail");
205 return;
206 }
207 AddButton(buttonType, button3_, text, listener);
208 } else {
209 button3_->SetText(text);
210 button3_->SetOnClickListener(listener);
211 }
212 break;
213 default:
214 break;
215 }
216 }
217
AddButton(DialogButtonType buttonType,UILabelButton * button,const char * text,UIView::OnClickListener * listener)218 void UIDialog::AddButton(DialogButtonType buttonType,
219 UILabelButton* button,
220 const char* text,
221 UIView::OnClickListener* listener)
222 {
223 buttonNum_++;
224 button->SetText(text);
225 button->SetOnClickListener(listener);
226 button->SetFont(DEFAULT_VECTOR_FONT_FILENAME, BUTTON_FONT_SIZE);
227 button->SetTextColor(Color::Blue());
228 button->SetStyleForState(STYLE_BACKGROUND_COLOR, Color::White().full, UIButton::ButtonState::RELEASED);
229 button->SetStyleForState(STYLE_BACKGROUND_COLOR, Color::Black().full, UIButton::ButtonState::PRESSED);
230 button->SetStyleForState(STYLE_BACKGROUND_OPA, BUTTON_PRESS_OPA, UIButton::ButtonState::PRESSED);
231 button->SetStyleForState(STYLE_BORDER_WIDTH, 0, UIButton::ButtonState::RELEASED);
232 button->SetStyleForState(STYLE_BORDER_WIDTH, 0, UIButton::ButtonState::PRESSED);
233 button->SetHeight(BUTTON_HEIGHT);
234 }
235
Show()236 void UIDialog::Show()
237 {
238 if (isShowing_) {
239 return;
240 }
241 SetTitleLabel();
242 SetTextLabel();
243 InitDialog();
244 AddComponents();
245 MeasureSize();
246 Layout();
247 CreateDialogWindow();
248 window_->Show();
249 isShowing_ = true;
250 }
251
SetTitleLabel()252 void UIDialog::SetTitleLabel()
253 {
254 if (titleText_ == nullptr) {
255 return;
256 }
257 if (title_ == nullptr) {
258 title_ = new UILabel();
259 if (title_ == nullptr) {
260 GRAPHIC_LOGE("new UILabel fail");
261 return;
262 }
263 title_->SetViewId(TITLE_ID);
264 title_->SetFont(DEFAULT_VECTOR_FONT_FILENAME, TITLE_FONT_SIZE);
265 title_->SetAlign(UITextLanguageAlignment::TEXT_ALIGNMENT_LEFT);
266 title_->SetStyle(STYLE_TEXT_COLOR, Color::Black().full);
267 title_->SetStyle(STYLE_TEXT_OPA, TITLE_TEXT_OPA);
268 title_->SetStyle(STYLE_BACKGROUND_COLOR, Color::White().full);
269 }
270 title_->SetText(titleText_);
271 }
272
SetTextLabel()273 void UIDialog::SetTextLabel()
274 {
275 if (textText_ == nullptr) {
276 return;
277 }
278 if (text_ == nullptr) {
279 text_ = new UILabel();
280 if (text_ == nullptr) {
281 GRAPHIC_LOGE("new UILabel fail");
282 return;
283 }
284 text_->SetFont(DEFAULT_VECTOR_FONT_FILENAME, TEXT_FONT_SIZE);
285 text_->SetStyle(STYLE_TEXT_COLOR, Color::Black().full);
286 text_->SetStyle(STYLE_TEXT_OPA, TITLE_TEXT_OPA);
287 text_->SetStyle(STYLE_BACKGROUND_COLOR, Color::White().full);
288 }
289 text_->SetText(textText_);
290 }
291
InitDialog()292 void UIDialog::InitDialog()
293 {
294 if (dialogLayer_ == nullptr) {
295 dialogLayer_ = new UIViewGroup();
296 if (dialogLayer_ == nullptr) {
297 GRAPHIC_LOGE("new UIViewGroup fail");
298 return;
299 }
300 dialogLayer_->SetStyle(STYLE_BACKGROUND_COLOR, Color::White().full);
301 }
302 if (windowRootView_ == nullptr) {
303 windowRootView_ = RootView::GetWindowRootView();
304 windowRootView_->SetWidth(Screen::GetInstance().GetWidth());
305 windowRootView_->SetHeight(Screen::GetInstance().GetHeight());
306 windowRootView_->SetTouchable(true);
307 windowRootView_->Add(dialogLayer_);
308 }
309 if (dialogClickListener_ == nullptr) {
310 dialogClickListener_ = new UIDialogClickListener(this);
311 if (dialogClickListener_ == nullptr) {
312 GRAPHIC_LOGE("new UIDialogClickListener fail");
313 return;
314 }
315 windowRootView_->SetOnClickListener(dialogClickListener_);
316 }
317 }
318
AddComponents()319 void UIDialog::AddComponents()
320 {
321 if (title_ != nullptr) {
322 dialogLayer_->Add(title_);
323 }
324 if (text_ != nullptr) {
325 dialogLayer_->Add(text_);
326 }
327 if (button1_ != nullptr) {
328 dialogLayer_->Add(button1_);
329 }
330 if (button2_ != nullptr) {
331 dialogLayer_->Add(button2_);
332 }
333 if (button3_ != nullptr) {
334 dialogLayer_->Add(button3_);
335 }
336 if (buttonNum_ > 1) {
337 line1_ = new UIView();
338 if (line1_ == nullptr) {
339 GRAPHIC_LOGE("new UIView fail");
340 return;
341 }
342 line1_->SetHeight(LINE_HEIGHT);
343 line1_->SetWidth(LINE_WIDTH);
344 line1_->SetStyle(STYLE_BACKGROUND_COLOR, Color::Black().full);
345 line1_->SetStyle(STYLE_BACKGROUND_OPA, LINE_OPA);
346 dialogLayer_->Add(line1_);
347 }
348 if (buttonNum_ == 3) { // 3: three buttons
349 line2_ = new UIView();
350 if (line2_ == nullptr) {
351 GRAPHIC_LOGE("new UIView fail");
352 return;
353 }
354 line2_->SetHeight(LINE_HEIGHT);
355 line2_->SetWidth(LINE_WIDTH);
356 line2_->SetStyle(STYLE_BACKGROUND_COLOR, Color::Black().full);
357 line2_->SetStyle(STYLE_BACKGROUND_OPA, LINE_OPA);
358 dialogLayer_->Add(line2_);
359 }
360 }
361
MeasureSize()362 void UIDialog::MeasureSize()
363 {
364 uint16_t width = MeasureMaxWidth();
365 uint16_t height = 0;
366
367 if (buttonNum_ > 0) {
368 // 2: there are 2 paddings horizontally
369 uint16_t buttonWidth = (width - 2 * BUTTON_PADDING - (buttonNum_ - 1) * BUTTON_MID_PADDING) / buttonNum_;
370 // 2: there are 2 paddings horizontally
371 width = static_cast<uint32_t>(buttonWidth) * buttonNum_ + (buttonNum_ - 1) * BUTTON_MID_PADDING +
372 2 * BUTTON_PADDING;
373 height += BUTTON_TOTAL_HEIGHT;
374 height += TEXT_BUTTON_PADDING;
375 if (button1_ != nullptr) {
376 button1_->SetWidth(buttonWidth);
377 }
378 if (button2_ != nullptr) {
379 button2_->SetWidth(buttonWidth);
380 }
381 if (button3_ != nullptr) {
382 button3_->SetWidth(buttonWidth);
383 }
384 } else {
385 height += PADDING;
386 }
387
388 if (title_ != nullptr) {
389 title_->SetWidth(width - 2 * PADDING); // 2: there are 2 paddings horizontally
390 height += TITLE_TOTAL_HEIGHT;
391 } else {
392 height += PADDING;
393 }
394
395 if (text_ != nullptr) {
396 text_->SetWidth(width - 2 * PADDING); // 2: there are 2 paddings horizontally
397 uint16_t textHightMax = heightMax_ - height;
398 if (text_->GetTextHeight() < textHightMax) {
399 text_->SetHeight(text_->GetTextHeight());
400 } else {
401 text_->SetHeight(textHightMax);
402 text_->SetLineBreakMode(UILabel::LINE_BREAK_ELLIPSIS);
403 }
404 height += text_->GetHeight();
405 }
406
407 uint16_t dialogHeight = height;
408 dialogLayer_->SetHeight(dialogHeight);
409 dialogLayer_->SetWidth(width);
410 }
411
MeasureMaxWidth()412 uint16_t UIDialog::MeasureMaxWidth()
413 {
414 uint16_t titleWidth = 0;
415 uint16_t textWidth = 0;
416 uint16_t buttonTotalWidth = 0;
417
418 if (title_ != nullptr) {
419 titleWidth = MeasureTitleWidth();
420 titleWidth += 2 * PADDING; // 2: there are 2 paddings horizontally
421 }
422 if (text_ != nullptr) {
423 textWidth = MeasureTextWidth();
424 textWidth += 2 * PADDING; // 2: there are 2 paddings horizontally
425 }
426 if (buttonNum_ > 0) {
427 buttonTotalWidth =
428 static_cast<uint32_t>(MeasureButtonWidth()) * buttonNum_ + (buttonNum_ - 1) * BUTTON_MID_PADDING;
429 buttonTotalWidth += 2 * BUTTON_PADDING; // 2: there are 2 paddings horizontally
430 }
431 return MATH_MAX(titleWidth, MATH_MAX(textWidth, buttonTotalWidth));
432 }
433
MeasureTitleWidth()434 uint16_t UIDialog::MeasureTitleWidth()
435 {
436 uint16_t titleWidth = 0;
437 uint16_t widthMaxNoPadding = widthMax_ - 2 * PADDING; // 2: there are 2 paddings horizontally
438 title_->SetLineBreakMode(UILabel::LINE_BREAK_ADAPT);
439 if (title_->GetTextWidth() > widthMaxNoPadding) {
440 titleWidth = widthMaxNoPadding;
441 title_->SetLineBreakMode(UILabel::LINE_BREAK_ELLIPSIS);
442 } else {
443 titleWidth = title_->GetTextWidth();
444 }
445 title_->SetHeight(title_->GetTextHeight());
446 return titleWidth;
447 }
448
MeasureTextWidth()449 uint16_t UIDialog::MeasureTextWidth()
450 {
451 uint16_t textWidth = 0;
452 uint16_t widthMaxNoPadding = widthMax_ - 2 * PADDING; // 2: there are 2 paddings horizontally
453 if (title_ != nullptr) {
454 text_->SetAlign(UITextLanguageAlignment::TEXT_ALIGNMENT_LEFT);
455 } else {
456 text_->SetAlign(UITextLanguageAlignment::TEXT_ALIGNMENT_CENTER);
457 }
458 text_->SetLineBreakMode(UILabel::LINE_BREAK_ADAPT);
459 textWidth = text_->GetTextWidth();
460 if (text_->GetTextWidth() > widthMaxNoPadding) {
461 text_->SetAlign(UITextLanguageAlignment::TEXT_ALIGNMENT_LEFT);
462 textWidth = widthMaxNoPadding;
463 text_->SetLineBreakMode(UILabel::LINE_BREAK_WRAP);
464 }
465 return textWidth;
466 }
467
MeasureButtonWidth()468 uint16_t UIDialog::MeasureButtonWidth()
469 {
470 if (buttonNum_ == 0) {
471 return 0;
472 }
473
474 uint16_t buttonTextWidth = 0;
475 // 2: there are 2 paddings horizontally
476 uint16_t buttonMaxWidth = (widthMax_ - 2 * BUTTON_PADDING - (buttonNum_ - 1) * BUTTON_MID_PADDING) / buttonNum_;
477
478 if (button1_ != nullptr) {
479 const char* text1 = button1_->GetText();
480 buttonTextWidth =
481 MATH_MAX(buttonTextWidth, TypedText::GetTextSize(text1, button1_->GetFontId(), BUTTON_FONT_SIZE,
482 button1_->GetStyleConst().letterSpace_,
483 button1_->GetStyleConst().lineHeight_, widthMax_, 0).x);
484 }
485 if (button2_ != nullptr) {
486 const char* text2 = button2_->GetText();
487 buttonTextWidth =
488 MATH_MAX(buttonTextWidth, TypedText::GetTextSize(text2, button2_->GetFontId(), BUTTON_FONT_SIZE,
489 button2_->GetStyleConst().letterSpace_,
490 button2_->GetStyleConst().lineHeight_, widthMax_, 0).x);
491 }
492 if (button3_ != nullptr) {
493 const char* text3 = button3_->GetText();
494 buttonTextWidth =
495 MATH_MAX(buttonTextWidth, TypedText::GetTextSize(text3, button3_->GetFontId(), BUTTON_FONT_SIZE,
496 button3_->GetStyleConst().letterSpace_,
497 button3_->GetStyleConst().lineHeight_, widthMax_, 0).x);
498 }
499 return (buttonTextWidth + BUTTON_HEIGHT) > buttonMaxWidth ? buttonMaxWidth : (buttonTextWidth + BUTTON_HEIGHT);
500 }
501
Layout()502 void UIDialog::Layout()
503 {
504 if (title_ != nullptr) {
505 // 2: there are 2 paddings vertically
506 uint8_t padding = (TITLE_TOTAL_HEIGHT - title_->GetHeight()) / 2;
507 title_->LayoutLeftOfParent(PADDING);
508 title_->LayoutTopOfParent(padding);
509 if (text_ != nullptr) {
510 text_->LayoutLeftOfParent(PADDING);
511 text_->LayoutBottomToSibling(TITLE_ID, padding);
512 }
513 } else {
514 if (text_ != nullptr) {
515 text_->LayoutLeftOfParent(PADDING);
516 text_->LayoutTopOfParent(PADDING);
517 }
518 }
519 LayoutButton();
520 }
521
LayoutButton()522 void UIDialog::LayoutButton()
523 {
524 if (button1_ != nullptr) {
525 button1_->LayoutLeftOfParent(BUTTON_PADDING);
526 button1_->LayoutBottomOfParent(BUTTON_PADDING);
527 if (button2_ != nullptr) {
528 button2_->LayoutRightToSibling(BUTTON1_ID, BUTTON_MID_PADDING);
529 button2_->LayoutBottomOfParent(BUTTON_PADDING);
530 }
531 } else if (button2_ != nullptr) {
532 button2_->LayoutLeftOfParent(BUTTON_PADDING);
533 button2_->LayoutBottomOfParent(BUTTON_PADDING);
534 }
535 if (button3_ != nullptr) {
536 button3_->LayoutRightOfParent(BUTTON_PADDING);
537 button3_->LayoutBottomOfParent(BUTTON_PADDING);
538 }
539
540 if (buttonNum_ == 3) { // 3: three buttons
541 line1_->LayoutBottomOfParent(LINE_BOTTOM_PADDING);
542 line1_->LayoutRightToSibling(BUTTON1_ID, LINE_BUTTON_PADDING);
543 line2_->LayoutBottomOfParent(LINE_BOTTOM_PADDING);
544 line2_->LayoutRightToSibling(BUTTON2_ID, LINE_BUTTON_PADDING);
545 } else if (buttonNum_ == 2) { // 2: two buttons
546 if (button1_ != nullptr) {
547 line1_->LayoutBottomOfParent(LINE_BOTTOM_PADDING);
548 line1_->LayoutRightToSibling(BUTTON1_ID, LINE_BUTTON_PADDING);
549 } else {
550 line1_->LayoutBottomOfParent(LINE_BOTTOM_PADDING);
551 line1_->LayoutRightToSibling(BUTTON2_ID, LINE_BUTTON_PADDING);
552 }
553 }
554 }
555
CreateDialogWindow()556 void UIDialog::CreateDialogWindow()
557 {
558 dialogLayer_->LayoutCenterOfParent();
559 Rect rect = dialogLayer_->GetRect();
560 int16_t offset_x = dialogLayer_->GetX();
561 int16_t offset_y = dialogLayer_->GetY();
562
563 windowRootView_->SetPosition(-offset_x, -offset_y);
564 windowRootView_->Invalidate();
565
566 WindowConfig config = {};
567 config.rect = rect;
568 config.rect.SetPosition(offset_x, offset_y);
569 config.isModal = true;
570 window_ = Window::CreateWindow(config);
571 if (window_ != nullptr) {
572 window_->BindRootView(windowRootView_);
573 } else {
574 GRAPHIC_LOGE("Create window false!");
575 }
576 }
577
SetButtonColor(DialogButtonType buttonType,ColorType color)578 void UIDialog::SetButtonColor(DialogButtonType buttonType, ColorType color)
579 {
580 switch (buttonType) {
581 case DialogButtonType::BUTTON_LEFT:
582 if (button1_ != nullptr) {
583 button1_->SetStyleForState(STYLE_BACKGROUND_COLOR, color.full, UIButton::ButtonState::RELEASED);
584 button1_->SetStyleForState(STYLE_BACKGROUND_OPA, color.alpha, UIButton::ButtonState::RELEASED);
585 colorType1_ = color;
586 }
587 break;
588 case DialogButtonType::BUTTON_MID:
589 if (button2_ != nullptr) {
590 button2_->SetStyleForState(STYLE_BACKGROUND_COLOR, color.full, UIButton::ButtonState::RELEASED);
591 button2_->SetStyleForState(STYLE_BACKGROUND_OPA, color.alpha, UIButton::ButtonState::RELEASED);
592 colorType2_ = color;
593 }
594 break;
595 case DialogButtonType::BUTTON_RIGHT:
596 if (button3_ != nullptr) {
597 button3_->SetStyleForState(STYLE_BACKGROUND_COLOR, color.full, UIButton::ButtonState::RELEASED);
598 button3_->SetStyleForState(STYLE_BACKGROUND_COLOR, color.alpha, UIButton::ButtonState::RELEASED);
599 colorType3_ = color;
600 }
601 break;
602 default:
603 break;
604 }
605 }
606
SetOnCancelListener(UIView::OnClickListener * onCancelListener)607 void UIDialog::SetOnCancelListener(UIView::OnClickListener* onCancelListener)
608 {
609 if (onCancelListener == nullptr) {
610 return;
611 }
612 onCancelListener_ = onCancelListener;
613 }
614
EnableAutoCancel(bool enable)615 void UIDialog::EnableAutoCancel(bool enable)
616 {
617 enableAutoCancel_ = enable;
618 }
619
DestroyWindow()620 void UIDialog::DestroyWindow()
621 {
622 if (window_ != nullptr) {
623 Window::DestroyWindow(window_);
624 window_ = nullptr;
625 }
626 }
627
628 #ifdef ENABLE_DEBUG
GetButtonText(DialogButtonType buttonType) const629 const char* UIDialog::GetButtonText(DialogButtonType buttonType) const
630 {
631 switch (buttonType) {
632 case DialogButtonType::BUTTON_LEFT:
633 if (button1_ != nullptr) {
634 return button1_->GetText();
635 }
636 return nullptr;
637 case DialogButtonType::BUTTON_MID:
638 if (button2_ != nullptr) {
639 return button2_->GetText();
640 }
641 return nullptr;
642 case DialogButtonType::BUTTON_RIGHT:
643 if (button3_ != nullptr) {
644 return button3_->GetText();
645 }
646 return nullptr;
647 default:
648 return nullptr;
649 }
650 }
651
GetButtonListener(DialogButtonType buttonType) const652 UIView::OnClickListener* UIDialog::GetButtonListener(DialogButtonType buttonType) const
653 {
654 switch (buttonType) {
655 case DialogButtonType::BUTTON_LEFT:
656 if (button1_ != nullptr) {
657 return button1_->GetOnClickListener();
658 }
659 return nullptr;
660 case DialogButtonType::BUTTON_MID:
661 if (button2_ != nullptr) {
662 return button2_->GetOnClickListener();
663 }
664 return nullptr;
665 case DialogButtonType::BUTTON_RIGHT:
666 if (button3_ != nullptr) {
667 return button3_->GetOnClickListener();
668 }
669 return nullptr;
670 default:
671 return nullptr;
672 }
673 }
674
GetButtonColor(DialogButtonType buttonType) const675 ColorType UIDialog::GetButtonColor(DialogButtonType buttonType) const
676 {
677 switch (buttonType) {
678 case DialogButtonType::BUTTON_LEFT:
679 if (button1_ != nullptr) {
680 return colorType1_;
681 }
682 break;
683 case DialogButtonType::BUTTON_MID:
684 if (button2_ != nullptr) {
685 return colorType2_;
686 }
687 break;
688 case DialogButtonType::BUTTON_RIGHT:
689 if (button3_ != nullptr) {
690 return colorType3_;
691 }
692 break;
693 default:
694 break;
695 }
696 ASSERT(0);
697 }
698 #endif // ENABLE_DEBUG
699 } // namespace OHOS
700 #endif // ENABLE_WINDOW
701