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