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