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_PATTERN_SLIDER_SLIDER_LAYOUT_PROPERTY_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_SLIDER_SLIDER_LAYOUT_PROPERTY_H
18 
19 #include "core/components_ng/base/inspector_filter.h"
20 #include "core/components_ng/layout/layout_property.h"
21 #include "core/components_ng/pattern/slider/slider_style.h"
22 #include "core/pipeline/pipeline_base.h"
23 
24 namespace OHOS::Ace::NG {
25 
26 class ACE_EXPORT SliderLayoutProperty : public LayoutProperty {
27     DECLARE_ACE_TYPE(SliderLayoutProperty, LayoutProperty);
28 
29 public:
30     SliderLayoutProperty() = default;
31     ~SliderLayoutProperty() override = default;
Clone()32     RefPtr<LayoutProperty> Clone() const override
33     {
34         auto value = MakeRefPtr<SliderLayoutProperty>();
35         value->LayoutProperty::UpdateLayoutProperty(DynamicCast<LayoutProperty>(this));
36         value->propSliderLayoutStyle_ = CloneSliderLayoutStyle();
37         return value;
38     }
39 
Reset()40     void Reset() override
41     {
42         LayoutProperty::Reset();
43         ResetSliderLayoutStyle();
44     }
45 
ToJsonValue(std::unique_ptr<JsonValue> & json,const InspectorFilter & filter)46     void ToJsonValue(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const override
47     {
48         LayoutProperty::ToJsonValue(json, filter);
49         /* no fixed attr below, just return */
50         if (filter.IsFastFilter()) {
51             return;
52         }
53         auto pipeline = PipelineBase::GetCurrentContext();
54         CHECK_NULL_VOID(pipeline);
55         auto theme = pipeline->GetTheme<SliderTheme>();
56         CHECK_NULL_VOID(theme);
57         json->PutExtAttr("trackThickness",
58             GetThickness()
59                 .value_or(GetSliderModeValue(SliderModel::SliderMode::OUTSET) == SliderModel::SliderMode::OUTSET
60                               ? theme->GetOutsetTrackThickness()
61                               : theme->GetInsetTrackThickness())
62                 .ToString().c_str(), filter);
63         static const std::array<std::string, 4> SLIDER_MODE_TO_STRING = {
64             "SliderStyle.OutSet",
65             "SliderStyle.InSet",
66             "SliderStyle.NONE",
67             "SliderStyle.Capsule",
68         };
69         // should be in constructor
70         json->PutExtAttr("style",
71             SLIDER_MODE_TO_STRING.at(static_cast<int32_t>(GetSliderMode().value_or(SliderModel::SliderMode::OUTSET)))
72                 .c_str(), filter);
73         auto jsonValue = JsonUtil::Create(true);
74         if (jsonValue) {
75             if (GetBlockSize().has_value()) {
76                 jsonValue->Put("width", GetBlockSize().value().Width().ToString().c_str());
77                 jsonValue->Put("height", GetBlockSize().value().Height().ToString().c_str());
78             } else {
79                 auto sliderMode = GetSliderMode().value_or(SliderModel::SliderMode::OUTSET);
80                 auto themeBlockSize = sliderMode == SliderModelNG::SliderMode::OUTSET ? theme->GetOutsetBlockSize()
81                                                                                       : theme->GetInsetBlockSize();
82                 jsonValue->Put("width", themeBlockSize.ToString().c_str());
83                 jsonValue->Put("height", themeBlockSize.ToString().c_str());
84             }
85             json->PutExtAttr("blockSize", jsonValue, filter);
86         }
87     }
88 
GetBlockSizeValue(const SizeF & defaultValue)89     SizeF GetBlockSizeValue(const SizeF& defaultValue)
90     {
91         auto& groupProperty = GetSliderLayoutStyle();
92         if (groupProperty) {
93             if (groupProperty->HasBlockSize()) {
94                 return SizeF(groupProperty->GetBlockSizeValue().Width().ConvertToPx(),
95                     groupProperty->GetBlockSizeValue().Height().ConvertToPx());
96             }
97         }
98         return defaultValue;
99     }
100 
101     ACE_DEFINE_PROPERTY_GROUP(SliderLayoutStyle, SliderLayoutStyle)
102     ACE_DEFINE_PROPERTY_ITEM_WITH_GROUP(SliderLayoutStyle, Direction, Axis, PROPERTY_UPDATE_MEASURE)
103     ACE_DEFINE_PROPERTY_ITEM_WITH_GROUP(SliderLayoutStyle, Thickness, Dimension, PROPERTY_UPDATE_MEASURE)
104     ACE_DEFINE_PROPERTY_ITEM_WITH_GROUP(SliderLayoutStyle, SliderMode, SliderModel::SliderMode, PROPERTY_UPDATE_MEASURE)
105     ACE_DEFINE_PROPERTY_ITEM_WITH_GROUP(SliderLayoutStyle, Reverse, bool, PROPERTY_UPDATE_MEASURE)
106     ACE_DEFINE_PROPERTY_ITEM_WITH_GROUP(SliderLayoutStyle, BlockSize, SizeT<Dimension>, PROPERTY_UPDATE_MEASURE)
107 private:
108     ACE_DISALLOW_COPY_AND_MOVE(SliderLayoutProperty);
109 };
110 
111 } // namespace OHOS::Ace::NG
112 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_SLIDER_SLIDER_LAYOUT_PROPERTY_H
113