• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 #include "core/components_ng/pattern/menu/preview/menu_preview_layout_algorithm.h"
17 
18 #include "base/utils/utils.h"
19 #include "core/components/common/properties/shadow_config.h"
20 #include "core/components/declaration/common/declaration_constants.h"
21 #include "core/components_ng/pattern/menu/menu_layout_algorithm.h"
22 #include "core/components_ng/pattern/menu/menu_paint_property.h"
23 #include "core/components_ng/pattern/menu/menu_theme.h"
24 #include "core/components_ng/pattern/menu/menu_pattern.h"
25 #include "core/components_ng/pattern/menu/preview/menu_preview_pattern.h"
26 #include "core/components_ng/pattern/menu/wrapper/menu_wrapper_pattern.h"
27 #include "core/components_ng/property/measure_property.h"
28 #include "core/components_ng/property/measure_utils.h"
29 
30 namespace OHOS::Ace::NG {
Measure(LayoutWrapper * layoutWrapper)31 void MenuPreviewLayoutAlgorithm::Measure(LayoutWrapper* layoutWrapper)
32 {
33     UpdateLayoutConstraintForPreview(layoutWrapper);
34     auto layoutConstraint = layoutWrapper->GetLayoutProperty()->CreateChildConstraint();
35     for (const auto& child : layoutWrapper->GetAllChildrenWithBuild()) {
36         child->Measure(layoutConstraint);
37     }
38 
39     LinearLayoutAlgorithm::Measure(layoutWrapper);
40 }
41 
Layout(LayoutWrapper * layoutWrapper)42 void MenuPreviewLayoutAlgorithm::Layout(LayoutWrapper* layoutWrapper)
43 {
44     auto preview = layoutWrapper->GetHostNode();
45     CHECK_NULL_VOID(preview);
46     auto previewPattern = preview->GetPattern<MenuPreviewPattern>();
47     CHECK_NULL_VOID(previewPattern);
48     auto menuWrapper = previewPattern->GetMenuWrapper();
49     CHECK_NULL_VOID(menuWrapper);
50     auto menuWrapperPattern = menuWrapper->GetPattern<MenuWrapperPattern>();
51     CHECK_NULL_VOID(menuWrapperPattern);
52     auto menuNode = menuWrapperPattern->GetMenu();
53     CHECK_NULL_VOID(menuNode);
54     auto menuLayoutAlgorithmWrapper = menuNode->GetLayoutAlgorithm();
55     CHECK_NULL_VOID(menuLayoutAlgorithmWrapper);
56     auto menuLayoutAlgorithm = DynamicCast<MenuLayoutAlgorithm>(menuLayoutAlgorithmWrapper->GetLayoutAlgorithm());
57     CHECK_NULL_VOID(menuLayoutAlgorithm);
58     auto menuPattern = menuNode->GetPattern<MenuPattern>();
59     CHECK_NULL_VOID(menuPattern);
60     if (!menuPattern->HasLaid()) {
61         menuLayoutAlgorithm->Measure(AceType::RawPtr(menuNode));
62         menuLayoutAlgorithm->Layout(AceType::RawPtr(menuNode));
63     }
64     menuPattern->SetHasLaid(false);
65     for (const auto& child : layoutWrapper->GetAllChildrenWithBuild()) {
66         child->Layout();
67     }
68     LinearLayoutAlgorithm::Layout(layoutWrapper);
69 }
70 
UpdateLayoutConstraintForPreview(LayoutWrapper * layoutWrapper)71 void MenuPreviewLayoutAlgorithm::UpdateLayoutConstraintForPreview(LayoutWrapper* layoutWrapper)
72 {
73     auto preview = layoutWrapper->GetHostNode();
74     CHECK_NULL_VOID(preview);
75     auto previewPattern = preview->GetPattern<MenuPreviewPattern>();
76     CHECK_NULL_VOID(previewPattern);
77     auto menuWrapper = previewPattern->GetMenuWrapper();
78     CHECK_NULL_VOID(menuWrapper);
79     auto menuWrapperPattern = menuWrapper->GetPattern<MenuWrapperPattern>();
80     CHECK_NULL_VOID(menuWrapperPattern);
81     auto menuParam = menuWrapperPattern->GetMenuParam();
82     CHECK_NULL_VOID(menuParam.isPreviewContainScale);
83     auto menuNode = menuWrapperPattern->GetMenu();
84     CHECK_NULL_VOID(menuNode);
85     auto menuPattern = menuNode->GetPattern<MenuPattern>();
86     CHECK_NULL_VOID(menuPattern);
87     auto menuWindowRect = menuPattern->GetMenuWindowRect();
88     auto maxWidth = menuWindowRect.Width();
89     auto maxHeight = menuWindowRect.Height();
90     auto targetSize = menuPattern->GetTargetSize();
91     auto isOversize = GreatNotEqual(targetSize.Width(), maxWidth) || GreatNotEqual(targetSize.Height(), maxHeight);
92     if (isOversize) {
93         auto widthDelta = targetSize.Width() - maxWidth;
94         auto heightDelta = targetSize.Height() - maxHeight;
95         if (GreatOrEqual(widthDelta, heightDelta)) {
96             maxHeight = targetSize.Height() * (maxWidth / targetSize.Width());
97         } else {
98             maxWidth = targetSize.Width() * (maxHeight / targetSize.Height());
99         }
100         auto layoutConstraint = layoutWrapper->GetLayoutProperty()->CreateChildConstraint();
101         layoutConstraint.maxSize.SetWidth(maxWidth);
102         layoutConstraint.maxSize.SetHeight(maxHeight);
103         layoutConstraint.selfIdealSize.SetWidth(maxWidth);
104         layoutConstraint.selfIdealSize.SetHeight(maxHeight);
105         layoutWrapper->GetLayoutProperty()->UpdateLayoutConstraint(layoutConstraint);
106     }
107 }
108 } // namespace OHOS::Ace::NG
109