1 /*
2  * Copyright (c) 2022 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_PROPERTIES_MAGIC_LAYOUT_PROPERTIES_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PROPERTIES_MAGIC_LAYOUT_PROPERTIES_H
18 
19 #include <memory>
20 
21 #include "core/common/ace_application_info.h"
22 #include "core/components/common/layout/constants.h"
23 #include "core/components_ng/base/inspector_filter.h"
24 #include "core/components_ng/property/measure_property.h"
25 #include "core/components_ng/property/property.h"
26 #include "core/pipeline/pipeline_base.h"
27 
28 namespace OHOS::Ace::NG {
29 struct MagicItemProperty {
30     ACE_DEFINE_PROPERTY_GROUP_ITEM(LayoutWeight, float);
31     ACE_DEFINE_PROPERTY_GROUP_ITEM(AspectRatio, float);
32 
ToJsonValueMagicItemProperty33     void ToJsonValue(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const
34     {
35         /* no fixed attr below, just return */
36         if (filter.IsFastFilter()) {
37             return;
38         }
39         json->PutExtAttr("layoutWeight", propLayoutWeight.value_or(0), filter);
40         auto context = PipelineBase::GetCurrentContext();
41         // add version protection, null as default start from API 10 or higher
42         if (context && context->GetMinPlatformVersion() > static_cast<int32_t>(PlatformVersion::VERSION_NINE)) {
43             if (propAspectRatio.has_value()) {
44                 json->PutExtAttr("aspectRatio",
45                     round(static_cast<double>(propAspectRatio.value()) * 100) / 100, filter);
46             } else {
47                 json->PutExtAttr("aspectRatio", "", filter);
48             }
49         } else {
50             json->PutExtAttr("aspectRatio",
51                 round(static_cast<double>(propAspectRatio.value_or(0.0)) * 100) / 100, filter);
52         }
53     }
54 
ResetMagicItemProperty55     inline void Reset()
56     {
57         propLayoutWeight.reset();
58         propAspectRatio.reset();
59     }
60 };
61 } // namespace OHOS::Ace::NG
62 
63 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PROPERTIES_MAGIC_LAYOUT_PROPERTIES_H
64