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 /** 17 * @addtogroup UI_Components 18 * @{ 19 * 20 * @brief Defines UI components such as buttons, texts, images, lists, and progress bars. 21 * 22 * @since 1.0 23 * @version 1.0 24 */ 25 26 /** 27 * @file ui_dialog.h 28 * 29 * @brief Declares the UIDialog class that defines the dialog box component. 30 * 31 * @since 1.0 32 * @version 1.0 33 */ 34 35 #ifndef GRAPHIC_LITE_UI_DIALOG_H 36 #define GRAPHIC_LITE_UI_DIALOG_H 37 38 #include "components/root_view.h" 39 #include "components/ui_label.h" 40 #include "components/ui_label_button.h" 41 42 #if ENABLE_WINDOW 43 namespace OHOS { 44 /** 45 * @brief Represents a dialog box. 46 * 47 * A dialog box displays information for user input. 48 * After a button is clicked in the dialog box, the corresponding callback event is triggered. 49 * 50 * @since 1.0 51 * @version 1.0 52 */ 53 class UIDialogClickListener; 54 class UIDialog : public HeapBase { 55 public: 56 /** 57 * @brief A constructor used to create a <b>UIDialog</b> instance. 58 * 59 * @since 1.0 60 * @version 1.0 61 */ 62 UIDialog(); 63 64 /** 65 * @brief A destructor used to delete the <b>UIDialog</b> instance. 66 * 67 * @since 1.0 68 * @version 1.0 69 */ 70 virtual ~UIDialog(); 71 72 /** 73 * @brief Enumerates buttons in a dialog box. 74 * 75 * @since 1.0 76 * @version 1.0 77 */ 78 enum class DialogButtonType { 79 /** Left button */ 80 BUTTON_LEFT, 81 /** Middle button */ 82 BUTTON_MID, 83 /** Right button */ 84 BUTTON_RIGHT, 85 }; 86 87 /** 88 * @brief Sets the title for this dialog box. 89 * 90 * @param title Indicates the pointer to the title. 91 * @since 1.0 92 * @version 1.0 93 */ 94 void SetTitle(const char* title); 95 96 /** 97 * @brief Sets the text for this dialog box. 98 * 99 * @param text Indicates the pointer to the text. 100 * @since 1.0 101 * @version 1.0 102 */ 103 void SetText(const char* text); 104 105 /** 106 * @brief Sets a button for this dialog box. 107 * 108 * @param buttonType Indicates the button position. 109 * @param text Indicates the pointer to the button text. 110 * @param listener Indicates the pointer to the listener registered for the button. 111 * @since 1.0 112 * @version 1.0 113 */ 114 void SetButton(DialogButtonType buttonType, const char* text, UIView::OnClickListener* listener); 115 116 /** 117 * @brief Shows this dialog box. 118 * 119 * @since 5.0 120 * @version 3.0 121 */ 122 void Show(); 123 124 /** 125 * @brief Sets the button color. 126 * 127 * @param buttonType Indicates the button type. 128 * @param color Indicates the button color to set. 129 * @since 5.0 130 * @version 3.0 131 */ 132 void SetButtonColor(DialogButtonType buttonType, ColorType color); 133 134 /** 135 * @brief Sets a listener for monitoring click events occurring outside this dialog box. 136 * 137 * @param onCancelListener Indicates the pointer to the listener to set. 138 * @since 5.0 139 * @version 3.0 140 */ 141 void SetOnCancelListener(UIView::OnClickListener* onCancelListener); 142 143 /** 144 * @brief Sets whether to close this dialog box when click events occur outside it. 145 * 146 * @param enable Specifies whether to close this dialog box when click events occur outside it. 147 * @since 5.0 148 * @version 3.0 149 */ 150 void EnableAutoCancel(bool enable); 151 152 #ifdef ENABLE_DEBUG GetTitle()153 const char* GetTitle() const 154 { 155 return titleText_; 156 } 157 GetText()158 const char* GetText() const 159 { 160 return textText_; 161 } 162 163 const char* GetButtonText(DialogButtonType buttonType) const; 164 165 UIView::OnClickListener* GetButtonListener(DialogButtonType buttonType) const; 166 167 ColorType GetButtonColor(DialogButtonType buttonType) const; 168 GetOnCancelListener()169 UIView::OnClickListener* GetOnCancelListener() const 170 { 171 return onCancelListener_; 172 } 173 GetEnableAutoCancel()174 bool GetEnableAutoCancel() const 175 { 176 return enableAutoCancel_; 177 } 178 #endif 179 180 private: 181 const static uint8_t BUTTON_HEIGHT = 40; 182 const static uint8_t BUTTON_TOTAL_HEIGHT = 56; 183 const static uint8_t PADDING = 24; 184 const static uint8_t BUTTON_MID_PADDING = 10; 185 const static uint8_t BUTTON_PADDING = 16; 186 const static uint8_t BUTTON_PRESS_OPA = 26; // 10% opacity 187 const static uint8_t TEXT_BUTTON_PADDING = 8; 188 const static uint8_t TEXT_FONT_SIZE = 16; 189 const static uint8_t TITLE_FONT_SIZE = 20; 190 const static uint8_t TITLE_TOTAL_HEIGHT = 56; 191 const static uint8_t BUTTON_FONT_SIZE = 16; 192 const static uint16_t MAX_WIDTH_PERCENT = 60; 193 const static uint16_t MAX_HEIGHT_PERCENT = 90; 194 const static uint8_t TITLE_TEXT_OPA = 230; // 90% opacity 195 const static uint8_t LINE_BUTTON_PADDING = 4; 196 const static uint8_t LINE_WIDTH = 2; 197 const static uint8_t LINE_HEIGHT = 24; 198 const static uint8_t LINE_BOTTOM_PADDING = 24; 199 const static uint8_t LINE_OPA = 51; // 20% opacity 200 const char* TITLE_ID = "dialogTitle"; 201 const char* BUTTON1_ID = "dialogButton1"; 202 const char* BUTTON2_ID = "dialogButton2"; 203 204 void AddButton(DialogButtonType buttonType, UILabelButton* button, const char* text, 205 UIView::OnClickListener* listener); 206 void InitDialog(); 207 void SetTitleLabel(); 208 void SetTextLabel(); 209 void AddComponents(); 210 void MeasureSize(); 211 uint16_t MeasureMaxWidth(); 212 uint16_t MeasureTitleWidth(); 213 uint16_t MeasureTextWidth(); 214 uint16_t MeasureButtonWidth(); 215 void Layout(); 216 void LayoutButton(); 217 void CreateDialogWindow(); 218 void DestroyWindow(); 219 220 bool isShowing_; 221 bool enableAutoCancel_; 222 uint8_t buttonNum_; 223 UILabel* title_; 224 UILabel* text_; 225 UILabelButton* button1_; 226 UILabelButton* button2_; 227 UILabelButton* button3_; 228 UIViewGroup* dialogLayer_; 229 uint16_t widthMax_; 230 uint16_t heightMax_; 231 RootView* windowRootView_; 232 UIView::OnClickListener* onCancelListener_; 233 UIDialogClickListener* dialogClickListener_; 234 Window* window_; 235 UIView* line1_; 236 UIView* line2_; 237 const char* titleText_; 238 const char* textText_; 239 ColorType colorType1_; 240 ColorType colorType2_; 241 ColorType colorType3_; 242 243 friend class UIDialogLabelButton; 244 friend class UIDialogClickListener; 245 }; 246 } 247 #endif // GRAPHIC_LITE_UI_DIALOG_H 248 #endif // ENABLE_WINDOW 249