1 /* 2 * Copyright (c) 2022-2023 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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_DIALOG_DIALOG_PATTERN_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_DIALOG_DIALOG_PATTERN_H 18 19 #include <cstdint> 20 21 #include "base/geometry/ng/offset_t.h" 22 #include "base/geometry/ng/size_t.h" 23 #include "base/memory/ace_type.h" 24 #include "base/memory/referenced.h" 25 #include "core/components/dialog/dialog_properties.h" 26 #include "core/components/dialog/dialog_theme.h" 27 #include "core/components_ng/pattern/dialog/dialog_event_hub.h" 28 #include "core/components_ng/pattern/dialog/dialog_accessibility_property.h" 29 #include "core/components_ng/pattern/dialog/dialog_layout_algorithm.h" 30 #include "core/components_ng/pattern/dialog/dialog_layout_property.h" 31 #include "core/components_ng/pattern/overlay/popup_base_pattern.h" 32 33 namespace OHOS::Ace::NG { 34 class DialogPattern : public PopupBasePattern { 35 DECLARE_ACE_TYPE(DialogPattern, PopupBasePattern); 36 37 public: DialogPattern(const RefPtr<DialogTheme> & dialogTheme,const RefPtr<UINode> & customNode)38 DialogPattern(const RefPtr<DialogTheme>& dialogTheme, const RefPtr<UINode>& customNode) 39 : dialogTheme_(dialogTheme), customNode_(customNode) 40 {} 41 ~DialogPattern() override = default; 42 IsAtomicNode()43 bool IsAtomicNode() const override 44 { 45 return false; 46 } 47 CreateLayoutProperty()48 RefPtr<LayoutProperty> CreateLayoutProperty() override 49 { 50 return AceType::MakeRefPtr<DialogLayoutProperty>(); 51 } 52 CreateLayoutAlgorithm()53 RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override 54 { 55 return AceType::MakeRefPtr<DialogLayoutAlgorithm>(); 56 } 57 CreateEventHub()58 RefPtr<EventHub> CreateEventHub() override 59 { 60 return MakeRefPtr<DialogEventHub>(); 61 } 62 CreateAccessibilityProperty()63 RefPtr<AccessibilityProperty> CreateAccessibilityProperty() override 64 { 65 return MakeRefPtr<DialogAccessibilityProperty>(); 66 } 67 GetFocusPattern()68 FocusPattern GetFocusPattern() const override 69 { 70 return { FocusType::SCOPE, true }; 71 } 72 73 void BuildChild(const DialogProperties& dialogProperties); 74 75 void ToJsonValue(std::unique_ptr<JsonValue>& json) const override; 76 GetTitle()77 const std::string& GetTitle() 78 { 79 return title_; 80 } 81 GetSubtitle()82 const std::string& GetSubtitle() 83 { 84 return subtitle_; 85 } 86 GetMessage()87 const std::string& GetMessage() 88 { 89 return message_; 90 } 91 GetCustomNode()92 const RefPtr<UINode>& GetCustomNode() 93 { 94 return customNode_; 95 } 96 SetOpenAnimation(const std::optional<AnimationOption> & openAnimation)97 void SetOpenAnimation(const std::optional<AnimationOption>& openAnimation) 98 { 99 openAnimation_ = openAnimation; 100 } GetOpenAnimation()101 std::optional<AnimationOption> GetOpenAnimation() const 102 { 103 return openAnimation_; 104 } 105 SetCloseAnimation(const std::optional<AnimationOption> & closeAnimation)106 void SetCloseAnimation(const std::optional<AnimationOption>& closeAnimation) 107 { 108 closeAnimation_ = closeAnimation; 109 } GetCloseAnimation()110 std::optional<AnimationOption> GetCloseAnimation() const 111 { 112 return closeAnimation_; 113 } 114 SetDialogProperties(const DialogProperties & param)115 void SetDialogProperties(const DialogProperties& param) 116 { 117 dialogProperties_ = param; 118 } 119 GetDialogProperties()120 const DialogProperties& GetDialogProperties() const 121 { 122 return dialogProperties_; 123 } 124 125 void OnColorConfigurationUpdate() override; 126 127 private: ShouldAvoidKeyboard()128 bool ShouldAvoidKeyboard() const override 129 { 130 return false; 131 } 132 void OnModifyDone() override; 133 134 void InitClickEvent(const RefPtr<GestureEventHub>& gestureHub); 135 void HandleClick(const GestureEvent& info); 136 void RegisterOnKeyEvent(const RefPtr<FocusHub>& focusHub); 137 bool OnKeyEvent(const KeyEvent& event); 138 139 void PopDialog(int32_t buttonIdx); 140 141 // set render context properties of content frame 142 void UpdateContentRenderContext(const RefPtr<FrameNode>& contentNode, const DialogProperties& props); 143 RefPtr<FrameNode> BuildMainTitle(const DialogProperties& dialogProperties); 144 RefPtr<FrameNode> BuildSubTitle(const DialogProperties& dialogProperties); 145 void ParseButtonFontColorAndBgColor( 146 const ButtonInfo& params, std::string& textColor, std::optional<Color>& bgColor); 147 void SetButtonTextOpacity(const RefPtr<FrameNode>& textNode, bool enabled); 148 void SetButtonEnabled(const RefPtr<FrameNode>& buttonNode, bool enabled); 149 RefPtr<FrameNode> BuildTitle(const DialogProperties& dialogProperties); 150 RefPtr<FrameNode> BuildContent(const DialogProperties& dialogProperties); 151 RefPtr<FrameNode> CreateDialogScroll(const DialogProperties& dialogProps); 152 153 void UpdateDialogButtonProperty(RefPtr<FrameNode>& buttonNode, int32_t index, bool isVertical, int32_t length); 154 RefPtr<FrameNode> BuildButtons(const std::vector<ButtonInfo>& buttons, const DialogButtonDirection& direction); 155 void AddButtonAndDivider( 156 const std::vector<ButtonInfo>& buttons, const RefPtr<NG::FrameNode>& container, bool isVertical); 157 RefPtr<FrameNode> CreateDivider( 158 const Dimension& dividerLength, const Dimension& dividerWidth, const Color& color, const Dimension& space); 159 RefPtr<FrameNode> CreateButton( 160 const ButtonInfo& params, int32_t index, bool isCancel = false, bool isVertical = false, int32_t length = 0); 161 RefPtr<FrameNode> CreateButtonText(const std::string& text, const std::string& colorStr); 162 // to close dialog when button is clicked 163 void BindCloseCallBack(const RefPtr<GestureEventHub>& hub, int32_t buttonIdx); 164 // build ActionSheet items 165 RefPtr<FrameNode> BuildSheet(const std::vector<ActionSheetInfo>& sheets); 166 RefPtr<FrameNode> BuildSheetItem(const ActionSheetInfo& item); 167 RefPtr<FrameNode> BuildSheetInfoTitle(const std::string& title); 168 RefPtr<FrameNode> BuildSheetInfoIcon(const std::string& icon); 169 // build actionMenu 170 RefPtr<FrameNode> BuildMenu(const std::vector<ButtonInfo>& buttons); 171 172 RefPtr<DialogTheme> dialogTheme_; 173 RefPtr<UINode> customNode_; 174 RefPtr<ClickEvent> onClick_; 175 176 std::optional<AnimationOption> openAnimation_; 177 std::optional<AnimationOption> closeAnimation_; 178 179 // XTS inspector values 180 std::string message_; 181 std::string title_; 182 std::string subtitle_; 183 184 DialogProperties dialogProperties_; 185 RefPtr<FrameNode> menuNode_; 186 bool isFirstDefaultFocus_ = true; 187 188 ACE_DISALLOW_COPY_AND_MOVE(DialogPattern); 189 }; 190 } // namespace OHOS::Ace::NG 191 192 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_DIALOG_DIALOG_PATTERN_H 193