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 #include <functional> 21 22 #include "base/geometry/ng/offset_t.h" 23 #include "base/geometry/ng/size_t.h" 24 #include "base/memory/ace_type.h" 25 #include "base/memory/referenced.h" 26 #include "core/common/autofill/auto_fill_trigger_state_holder.h" 27 #include "core/components/dialog/dialog_properties.h" 28 #include "core/components/dialog/dialog_theme.h" 29 #include "core/components_ng/manager/focus/focus_view.h" 30 #include "core/components_ng/pattern/dialog/dialog_event_hub.h" 31 #include "core/components_ng/pattern/dialog/dialog_accessibility_property.h" 32 #include "core/components_ng/pattern/dialog/dialog_layout_algorithm.h" 33 #include "core/components_ng/pattern/dialog/dialog_layout_property.h" 34 #include "core/components_ng/pattern/overlay/popup_base_pattern.h" 35 36 namespace OHOS::Ace::NG { 37 class InspectorFilter; 38 39 enum class DialogContentNode { 40 TITLE = 0, 41 SUBTITLE, 42 MESSAGE, 43 SHEET, 44 BORDERWIDTH, 45 }; 46 enum class DialogDismissReason { 47 DIALOG_PRESS_BACK = 0, 48 DIALOG_TOUCH_OUTSIDE, 49 DIALOG_CLOSE_BUTTON, 50 }; 51 class DialogPattern : public PopupBasePattern, public FocusView, public AutoFillTriggerStateHolder { 52 DECLARE_ACE_TYPE(DialogPattern, PopupBasePattern, FocusView, AutoFillTriggerStateHolder); 53 54 public: DialogPattern(const RefPtr<DialogTheme> & dialogTheme,const RefPtr<UINode> & customNode)55 DialogPattern(const RefPtr<DialogTheme>& dialogTheme, const RefPtr<UINode>& customNode) 56 : dialogTheme_(dialogTheme), customNode_(customNode) 57 {} 58 ~DialogPattern() override = default; 59 IsAtomicNode()60 bool IsAtomicNode() const override 61 { 62 return false; 63 } 64 SetOnWillDismiss(const std::function<void (const int32_t & info,const int32_t & instanceId)> & onWillDismiss)65 void SetOnWillDismiss(const std::function<void(const int32_t& info, const int32_t& instanceId)>& onWillDismiss) 66 { 67 onWillDismiss_ = onWillDismiss; 68 } 69 ShouldDismiss()70 bool ShouldDismiss() const 71 { 72 if (onWillDismiss_) { 73 return true; 74 } 75 return false; 76 } 77 SetOnWillDismissByNDK(const std::function<bool (const int32_t & info)> & onWillDismissByNDK)78 void SetOnWillDismissByNDK(const std::function<bool(const int32_t& info)>& onWillDismissByNDK) 79 { 80 onWillDismissByNDK_ = onWillDismissByNDK; 81 } 82 CallDismissInNDK(int reason)83 bool CallDismissInNDK(int reason) 84 { 85 if (onWillDismissByNDK_ && onWillDismissByNDK_(reason)) { 86 return true; 87 } 88 return false; 89 } 90 CallOnWillDismiss(const int32_t reason,const int32_t instanceId)91 void CallOnWillDismiss(const int32_t reason, const int32_t instanceId) 92 { 93 if (onWillDismiss_) { 94 onWillDismiss_(reason, instanceId); 95 } 96 } 97 CreateLayoutProperty()98 RefPtr<LayoutProperty> CreateLayoutProperty() override 99 { 100 return AceType::MakeRefPtr<DialogLayoutProperty>(); 101 } 102 CreateLayoutAlgorithm()103 RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override 104 { 105 return AceType::MakeRefPtr<DialogLayoutAlgorithm>(); 106 } 107 CreateEventHub()108 RefPtr<EventHub> CreateEventHub() override 109 { 110 return MakeRefPtr<DialogEventHub>(); 111 } 112 CreateAccessibilityProperty()113 RefPtr<AccessibilityProperty> CreateAccessibilityProperty() override 114 { 115 return MakeRefPtr<DialogAccessibilityProperty>(); 116 } 117 GetFocusPattern()118 FocusPattern GetFocusPattern() const override 119 { 120 return { FocusType::SCOPE, true }; 121 } 122 GetRouteOfFirstScope()123 std::list<int32_t> GetRouteOfFirstScope() override 124 { 125 if (dialogProperties_.type == DialogType::ALERT_DIALOG || dialogProperties_.type == DialogType::ACTION_SHEET) { 126 return { 0 }; 127 } 128 return { 0, 0 }; 129 } 130 131 void BuildChild(const DialogProperties& dialogProperties); 132 133 void ToJsonValue(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const override; 134 GetTitle()135 const std::string& GetTitle() 136 { 137 return title_; 138 } 139 GetSubtitle()140 const std::string& GetSubtitle() 141 { 142 return subtitle_; 143 } 144 GetMessage()145 const std::string& GetMessage() 146 { 147 return message_; 148 } 149 GetCustomNode()150 RefPtr<UINode> GetCustomNode() 151 { 152 return customNode_.Upgrade(); 153 } 154 SetOpenAnimation(const std::optional<AnimationOption> & openAnimation)155 void SetOpenAnimation(const std::optional<AnimationOption>& openAnimation) 156 { 157 openAnimation_ = openAnimation; 158 } GetOpenAnimation()159 std::optional<AnimationOption> GetOpenAnimation() const 160 { 161 return openAnimation_; 162 } 163 SetCloseAnimation(const std::optional<AnimationOption> & closeAnimation)164 void SetCloseAnimation(const std::optional<AnimationOption>& closeAnimation) 165 { 166 closeAnimation_ = closeAnimation; 167 } GetCloseAnimation()168 std::optional<AnimationOption> GetCloseAnimation() const 169 { 170 return closeAnimation_; 171 } 172 SetDialogProperties(const DialogProperties & param)173 void SetDialogProperties(const DialogProperties& param) 174 { 175 dialogProperties_ = param; 176 InitHostWindowRect(); 177 } 178 GetDialogProperties()179 const DialogProperties& GetDialogProperties() const 180 { 181 return dialogProperties_; 182 } 183 184 void OnColorConfigurationUpdate() override; 185 186 void OnLanguageConfigurationUpdate() override; 187 188 void DumpInfo() override; 189 void DumpInfo(std::unique_ptr<JsonValue>& json) override; 190 void DumpSimplifyInfo(std::unique_ptr<JsonValue>& json) override; AvoidBottom()191 bool AvoidBottom() const override 192 { 193 return false; 194 } 195 RegisterDialogDidAppearCallback(std::function<void ()> && onDidAppear)196 void RegisterDialogDidAppearCallback(std::function<void()>&& onDidAppear) 197 { 198 onDidAppearCallback_ = std::move(onDidAppear); 199 } 200 RegisterDialogDidDisappearCallback(std::function<void ()> && onDidDisappear)201 void RegisterDialogDidDisappearCallback(std::function<void()>&& onDidDisappear) 202 { 203 onDidDisappearCallback_ = std::move(onDidDisappear); 204 } 205 RegisterDialogWillAppearCallback(std::function<void ()> && onWillAppear)206 void RegisterDialogWillAppearCallback(std::function<void()>&& onWillAppear) 207 { 208 onWillAppearCallback_ = std::move(onWillAppear); 209 } 210 RegisterDialogWillDisappearCallback(std::function<void ()> && onWillDisappear)211 void RegisterDialogWillDisappearCallback(std::function<void()>&& onWillDisappear) 212 { 213 onWillDisappearCallback_ = std::move(onWillDisappear); 214 } 215 CallDialogDidAppearCallback()216 void CallDialogDidAppearCallback() 217 { 218 if (onDidAppearCallback_) { 219 onDidAppearCallback_(); 220 } 221 } 222 CallDialogDidDisappearCallback()223 void CallDialogDidDisappearCallback() 224 { 225 if (onDidDisappearCallback_) { 226 onDidDisappearCallback_(); 227 } 228 } 229 CallDialogWillAppearCallback()230 void CallDialogWillAppearCallback() 231 { 232 if (onWillAppearCallback_) { 233 onWillAppearCallback_(); 234 } 235 } 236 CallDialogWillDisappearCallback()237 void CallDialogWillDisappearCallback() 238 { 239 if (onWillDisappearCallback_) { 240 onWillDisappearCallback_(); 241 } 242 } 243 IsUIExtensionSubWindow()244 bool IsUIExtensionSubWindow() const 245 { 246 return isUIExtensionSubWindow_; 247 } 248 GetHostWindowRect()249 RectF GetHostWindowRect() const 250 { 251 return hostWindowRect_; 252 } 253 UpdateFoldDisplayModeChangedCallbackId(std::optional<int32_t> id)254 void UpdateFoldDisplayModeChangedCallbackId(std::optional<int32_t> id) 255 { 256 foldDisplayModeChangedCallbackId_ = id; 257 } 258 HasFoldDisplayModeChangedCallbackId()259 bool HasFoldDisplayModeChangedCallbackId() 260 { 261 return foldDisplayModeChangedCallbackId_.has_value(); 262 } 263 UpdateHoverModeChangedCallbackId(std::optional<int32_t> id)264 void UpdateHoverModeChangedCallbackId(std::optional<int32_t> id) 265 { 266 hoverModeChangedCallbackId_ = id; 267 } 268 HasHoverModeChangedCallbackId()269 bool HasHoverModeChangedCallbackId() 270 { 271 return hoverModeChangedCallbackId_.has_value(); 272 } 273 GetIsSuitableForAging()274 bool GetIsSuitableForAging() 275 { 276 return isSuitableForElderly_; 277 } 278 GetFontScaleForElderly()279 float GetFontScaleForElderly() 280 { 281 return fontScaleForElderly_; 282 } 283 SetIsPickerDialog(bool value)284 void SetIsPickerDialog(bool value) 285 { 286 isPickerDialog_ = value; 287 } 288 GetIsPickerDialog()289 bool GetIsPickerDialog() 290 { 291 return isPickerDialog_; 292 } 293 294 void UpdateDeviceOrientation(const DeviceOrientation& deviceOrientation); 295 void InitHostWindowRect(); 296 void UpdateHostWindowRect(); 297 void UpdateFontScale(); 298 GetIsSuitOldMeasure()299 bool GetIsSuitOldMeasure() 300 { 301 return isSuitOldMeasure_; 302 } 303 SetIsScrollHeightNegative(bool isScrollHeightNegative)304 void SetIsScrollHeightNegative(bool isScrollHeightNegative) 305 { 306 isScrollHeightNegative_ = isScrollHeightNegative; 307 } 308 TriggerAutoSaveWhenInvisible()309 bool TriggerAutoSaveWhenInvisible() override 310 { 311 return true; 312 } 313 314 bool IsShowInFreeMultiWindow(); 315 bool IsShowInFloatingWindow(); 316 void AddExtraMaskNode(const DialogProperties& props); 317 318 void OverlayDismissDialog(const RefPtr<FrameNode>& dialogNode); 319 RefPtr<OverlayManager> GetEmbeddedOverlay(const RefPtr<OverlayManager>& context); 320 321 private: AvoidKeyboard()322 bool AvoidKeyboard() const override 323 { 324 return false; 325 } 326 void OnModifyDone() override; 327 328 void OnAttachToFrameNode() override; 329 void OnDetachFromFrameNode(FrameNode* frameNode) override; 330 void RegisterHoverModeChangeCallback(); 331 void OnWindowSizeChanged(int32_t width, int32_t height, WindowSizeChangeReason type) override; 332 void InitClickEvent(const RefPtr<GestureEventHub>& gestureHub); 333 RectF GetContentRect(const RefPtr<FrameNode>& contentNode); 334 void HandleClick(const GestureEvent& info); 335 void RegisterOnKeyEvent(const RefPtr<FocusHub>& focusHub); 336 bool OnKeyEvent(const KeyEvent& event); 337 void InitFocusEvent(const RefPtr<FocusHub>& focusHub); 338 void HandleBlurEvent(); 339 void HandleFocusEvent(); 340 341 void PopDialog(int32_t buttonIdx); 342 bool NeedUpdateHostWindowRect(); 343 344 // set render context properties of content frame 345 void UpdateContentRenderContext(const RefPtr<FrameNode>& contentNode, const DialogProperties& props); 346 void BuildCustomChild(const DialogProperties& props, const RefPtr<UINode>& customNode); 347 RefPtr<FrameNode> BuildMainTitle(const DialogProperties& dialogProperties); 348 RefPtr<FrameNode> BuildSubTitle(const DialogProperties& dialogProperties); 349 void ParseButtonFontColorAndBgColor( 350 const ButtonInfo& params, std::string& textColor, std::optional<Color>& bgColor); 351 void SetButtonEnabled(const RefPtr<FrameNode>& buttonNode, bool enabled); 352 // update wrapperNode background style 353 void UpdateWrapperBackgroundStyle(const RefPtr<FrameNode>& host, const RefPtr<DialogTheme>& dialogTheme); 354 RefPtr<FrameNode> BuildTitle(const DialogProperties& dialogProperties); 355 RefPtr<FrameNode> BuildContent(const DialogProperties& dialogProperties); 356 RefPtr<FrameNode> CreateDialogScroll(const DialogProperties& dialogProps); 357 358 void UpdateDialogButtonProperty(RefPtr<FrameNode>& buttonNode, int32_t index, bool isVertical, int32_t length); 359 RefPtr<FrameNode> BuildButtons(const std::vector<ButtonInfo>& buttons, const DialogButtonDirection& direction); 360 void AddButtonAndDivider( 361 const std::vector<ButtonInfo>& buttons, const RefPtr<NG::FrameNode>& container, bool isVertical); 362 RefPtr<FrameNode> CreateDivider( 363 const Dimension& dividerLength, const Dimension& dividerWidth, const Color& color, const Dimension& space); 364 RefPtr<FrameNode> CreateButton( 365 const ButtonInfo& params, int32_t index, bool isCancel = false, bool isVertical = false, int32_t length = 0); 366 RefPtr<FrameNode> CreateButtonText(const std::string& text, const std::string& colorStr); 367 // to close dialog when button is clicked 368 void BindCloseCallBack(const RefPtr<GestureEventHub>& hub, int32_t buttonIdx); 369 // build ActionSheet items 370 RefPtr<FrameNode> BuildSheet(const std::vector<ActionSheetInfo>& sheets); 371 RefPtr<FrameNode> BuildSheetItem(const ActionSheetInfo& item); 372 RefPtr<FrameNode> BuildSheetInfoTitle(const std::string& title); 373 RefPtr<FrameNode> BuildSheetInfoIcon(const std::string& icon); 374 // build actionMenu 375 RefPtr<FrameNode> BuildMenu(const std::vector<ButtonInfo>& buttons, bool hasTitle); 376 void RecordEvent(int32_t btnIndex) const; 377 void ParseBorderRadius(BorderRadiusProperty& raidus); 378 void UpdateSheetIconAndText(); 379 void UpdateButtonsPropertyForEachButton(RefPtr<FrameNode> buttonFrameNode, int32_t btnindex); 380 void UpdateButtonsProperty(); 381 void UpdateNodeContent(const RefPtr<FrameNode>& node, std::string& text); 382 void UpdateTitleAndContentColor(); 383 void UpdateDialogTextColor(const RefPtr<FrameNode>& textNode, const TextStyle& textStyle); 384 void UpdateAlignmentAndOffset(); 385 void DumpBoolProperty(); 386 void DumpBoolProperty(std::unique_ptr<JsonValue>& json); 387 void DumpObjectProperty(); 388 void DumpObjectProperty(std::unique_ptr<JsonValue>& json); 389 void DumpSimplifyBoolProperty(std::unique_ptr<JsonValue>& json); 390 void DumpSimplifyObjectProperty(std::unique_ptr<JsonValue>& json); 391 void DumpSimplifyBorderProperty(std::unique_ptr<JsonValue>& json); 392 void DumpSimplifySizeProperty(std::unique_ptr<JsonValue>& json); 393 void UpdatePropertyForElderly(const std::vector<ButtonInfo>& buttons); 394 bool NeedsButtonDirectionChange(const std::vector<ButtonInfo>& buttons); 395 void OnFontConfigurationUpdate() override; 396 void UpdateTextFontScale(); 397 void UpdateTitleTextFontScale(); 398 void CheckScrollHeightIsNegative(const RefPtr<UINode>& contentColumn, const DialogProperties& props); 399 RefPtr<OverlayManager> GetOverlayManager(const RefPtr<FrameNode>& host); 400 void OnAttachToMainTree() override; 401 RefPtr<DialogTheme> dialogTheme_; 402 WeakPtr<UINode> customNode_; 403 RefPtr<ClickEvent> onClick_; 404 405 std::optional<AnimationOption> openAnimation_; 406 std::optional<AnimationOption> closeAnimation_; 407 std::optional<int32_t> foldDisplayModeChangedCallbackId_; 408 std::optional<int32_t> hoverModeChangedCallbackId_; 409 bool isFoldStatusChanged_ = false; 410 411 // XTS inspector values 412 std::string message_; 413 std::string title_; 414 std::string subtitle_; 415 std::function<void(const int32_t& info, const int32_t& instanceId)> onWillDismiss_; 416 std::function<bool(const int32_t& info)> onWillDismissByNDK_; 417 418 DialogProperties dialogProperties_; 419 WeakPtr<FrameNode> menuNode_; 420 bool isFirstDefaultFocus_ = true; 421 RefPtr<FrameNode> buttonContainer_; 422 RefPtr<FrameNode> contentColumn_; 423 RefPtr<RenderContext> contentRenderContext_; 424 bool isSuitableForElderly_ = false; 425 bool isPickerDialog_ = false; 426 bool notAdapationAging_ = false; 427 bool isSuitOldMeasure_ = false; 428 bool isScrollHeightNegative_ = false; 429 float fontScaleForElderly_ = 1.0f; 430 DeviceOrientation deviceOrientation_ = DeviceOrientation::PORTRAIT; 431 RefPtr<FrameNode> titleContainer_; 432 433 ACE_DISALLOW_COPY_AND_MOVE(DialogPattern); 434 435 std::function<void()> onDidAppearCallback_ = nullptr; 436 std::function<void()> onDidDisappearCallback_ = nullptr; 437 std::function<void()> onWillAppearCallback_ = nullptr; 438 std::function<void()> onWillDisappearCallback_ = nullptr; 439 std::unordered_map<DialogContentNode, RefPtr<FrameNode>> contentNodeMap_; 440 bool isUIExtensionSubWindow_ = false; 441 RectF hostWindowRect_; 442 }; 443 } // namespace OHOS::Ace::NG 444 445 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_DIALOG_DIALOG_PATTERN_H 446