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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_MENU_MENU_PAINT_PROPERTY_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_MENU_MENU_PAINT_PROPERTY_H
18 
19 #include "base/geometry/dimension.h"
20 #include "base/utils/utils.h"
21 #include "core/components/common/properties/placement.h"
22 #include "core/components_ng/base/inspector_filter.h"
23 #include "core/components_ng/render/paint_property.h"
24 #include "core/components_v2/inspector/utils.h"
25 
26 namespace OHOS::Ace::NG {
27 namespace {
28 constexpr Dimension ARROW_WIDTH = 32.0_vp;
29 // arrow actual display height against shadow
30 constexpr Dimension ARROW_HIGHT = 7.4_vp;
31 constexpr Dimension TARGET_SECURITY = 8.0_vp;
32 // space from menu to target for arrow display
33 constexpr Dimension TARGET_SPACE = 8.0_vp;
34 constexpr Dimension ARROW_ZERO_PERCENT_VALUE = Dimension(0.0, DimensionUnit::PERCENT);
35 constexpr Dimension ARROW_HALF_PERCENT_VALUE = Dimension(0.5, DimensionUnit::PERCENT);
36 constexpr Dimension ARROW_ONE_HUNDRED_PERCENT_VALUE = Dimension(1.0, DimensionUnit::PERCENT);
37 // preview menu
38 constexpr Dimension PORTRAIT_TOP_SECURITY = 48.0_vp;
39 constexpr Dimension PORTRAIT_BOTTOM_SECURITY = 48.0_vp;
40 constexpr Dimension PORTRAIT_TOP_SECURITY_API12 = 16.0_vp;
41 constexpr Dimension PORTRAIT_BOTTOM_SECURITY_API12 = 4.0_vp;
42 constexpr Dimension LANDSCAPE_TOP_SECURITY = 24.0_vp;
43 constexpr Dimension LANDSCAPE_BOTTOM_SECURITY = 24.0_vp;
44 constexpr Dimension PREVIEW_INNER_SECURITY = 24.0_vp;
45 } // namespace
46 class ACE_EXPORT MenuPaintProperty : public PaintProperty {
47     DECLARE_ACE_TYPE(MenuPaintProperty, PaintProperty)
48 
49 public:
50     MenuPaintProperty() = default;
51     ~MenuPaintProperty() override = default;
52 
Clone()53     RefPtr<PaintProperty> Clone() const override
54     {
55         auto paintProperty = MakeRefPtr<MenuPaintProperty>();
56         paintProperty->propEnableArrow_ = CloneEnableArrow();
57         paintProperty->propArrowOffset_ = CloneArrowOffset();
58         paintProperty->propArrowPosition_ = CloneArrowPosition();
59         paintProperty->propArrowPlacement_ = CloneArrowPlacement();
60         return paintProperty;
61     }
62 
Reset()63     void Reset() override
64     {
65         PaintProperty::Reset();
66         ResetEnableArrow();
67         ResetArrowOffset();
68         ResetArrowPosition();
69         ResetArrowPlacement();
70     }
71 
ConvertPlacementToString(const Placement & place)72     std::string ConvertPlacementToString(const Placement& place) const
73     {
74         static const LinearEnumMapNode<Placement, std::string> placementTable[] = {
75             { Placement::LEFT, "Placement.Left" },
76             { Placement::RIGHT, "Placement.Right" },
77             { Placement::TOP, "Placement.Top" },
78             { Placement::BOTTOM, "Placement.Bottom" },
79             { Placement::TOP_LEFT, "Placement.TopLeft" },
80             { Placement::TOP_RIGHT, "Placement.TopRight" },
81             { Placement::BOTTOM_LEFT, "Placement.BottomLeft" },
82             { Placement::BOTTOM_RIGHT, "Placement.BottomRight" },
83             { Placement::LEFT_TOP, "Placement.LeftTop" },
84             { Placement::LEFT_BOTTOM, "Placement.LeftBottom" },
85             { Placement::RIGHT_TOP, "Placement.RightTop" },
86             { Placement::RIGHT_BOTTOM, "Placement.RightBottom" },
87             { Placement::NONE, "Placement.None" },
88         };
89 
90         auto index = BinarySearchFindIndex(placementTable, ArraySize(placementTable), place);
91         return index < 0 ? "Placement.None" : placementTable[index].value;
92     }
93 
ToJsonValue(std::unique_ptr<JsonValue> & json,const InspectorFilter & filter)94     void ToJsonValue(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const override
95     {
96         PaintProperty::ToJsonValue(json, filter);
97         /* no fixed attr below, just return */
98         if (filter.IsFastFilter()) {
99             return;
100         }
101         json->PutExtAttr("enableArrow", V2::ConvertBoolToString(GetEnableArrow().value_or(false)).c_str(), filter);
102         json->PutExtAttr("arrowOffset",
103             GetArrowOffset().value_or(Dimension(0.0, DimensionUnit::VP)).ToString().c_str(), filter);
104         json->PutExtAttr("arrowPosition",
105             GetArrowPosition().value_or(OffsetF(0.0f, 0.0f)).ToString().c_str(), filter);
106         json->PutExtAttr("arrowPlacement",
107             ConvertPlacementToString(GetArrowPlacement().value_or(Placement::NONE)).c_str(), filter);
108     }
109 
110     ACE_DEFINE_PROPERTY_ITEM_WITHOUT_GROUP(EnableArrow, bool, PROPERTY_UPDATE_RENDER);
111     ACE_DEFINE_PROPERTY_ITEM_WITHOUT_GROUP(ArrowOffset, Dimension, PROPERTY_UPDATE_RENDER);
112     ACE_DEFINE_PROPERTY_ITEM_WITHOUT_GROUP(ArrowPosition, OffsetF, PROPERTY_UPDATE_RENDER);
113     ACE_DEFINE_PROPERTY_ITEM_WITHOUT_GROUP(ArrowPlacement, Placement, PROPERTY_UPDATE_RENDER);
114     ACE_DISALLOW_COPY_AND_MOVE(MenuPaintProperty);
115 };
116 } // namespace OHOS::Ace::NG
117 
118 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_MENU_MENU_PAINT_PROPERTY_H
119