• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_PATTERN_IMAGE_IMAGE_LAYOUT_PROPERTY_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_IMAGE_IMAGE_LAYOUT_PROPERTY_H
18 
19 #include "core/common/ace_application_info.h"
20 #include "core/components_ng/layout/layout_property.h"
21 #include "core/components_ng/property/property.h"
22 #include "core/image/image_source_info.h"
23 #include "core/pipeline_ng/pipeline_context.h"
24 
25 namespace OHOS::Ace::NG {
26 class ImagePattern;
27 struct ImageSizeStyle {
28     ACE_DEFINE_PROPERTY_GROUP_ITEM(AutoResize, bool);
29     ACE_DEFINE_PROPERTY_GROUP_ITEM(SourceSize, SizeF);
30     ACE_DEFINE_PROPERTY_GROUP_ITEM(FitOriginalSize, bool);
ToJsonValueImageSizeStyle31     void ToJsonValue(std::unique_ptr<JsonValue>& json) const
32     {
33         json->Put("sourceSize", propSourceSize.value_or(SizeF()).ToString().c_str());
34         json->Put("fitOriginalSize", propFitOriginalSize.value_or(false) ? "true" : "false");
35         json->Put("autoResize", propAutoResize.value_or(true) ? "true" : "false");
36     }
37 };
38 
39 class ACE_EXPORT ImageLayoutProperty : public LayoutProperty {
40     DECLARE_ACE_TYPE(ImageLayoutProperty, LayoutProperty);
41 
42 public:
43     ImageLayoutProperty() = default;
44 
45     ~ImageLayoutProperty() override = default;
46 
Clone()47     RefPtr<LayoutProperty> Clone() const override
48     {
49         auto value = MakeRefPtr<ImageLayoutProperty>();
50         value->LayoutProperty::UpdateLayoutProperty(DynamicCast<LayoutProperty>(this));
51         value->propImageSourceInfo_ = CloneImageSourceInfo();
52         value->propAlt_ = CloneAlt();
53         value->propImageFit_ = CloneImageFit();
54         value->propImageSizeStyle_ = CloneImageSizeStyle();
55         value->propVerticalAlign_ = CloneVerticalAlign();
56         return value;
57     }
58 
Reset()59     void Reset() override
60     {
61         LayoutProperty::Reset();
62         ResetImageSourceInfo();
63         ResetAlt();
64         ResetImageFit();
65         ResetImageSizeStyle();
66         ResetVerticalAlign();
67     }
68 
ToJsonValue(std::unique_ptr<JsonValue> & json)69     void ToJsonValue(std::unique_ptr<JsonValue>& json) const override
70     {
71         LayoutProperty::ToJsonValue(json);
72         static const char* OBJECTFITVALUE[] = { "ImageFit.Fill", "ImageFit.Contain", "ImageFit.Cover",
73             "ImageFit.Auto", "ImageFit.FitHeight", "ImageFit.None", "ImageFit.ScaleDown" };
74         static const char* VERTICALALIGNVALUE[] = { "VerticalAlign.NONE", "VerticalAlign.TOP", "VerticalAlign.CENTER",
75             "VerticalAlign.BOTTOM", "CopyOptions.BASELINE", "VerticalAlign.NONE" };
76         json->Put("alt", propAlt_.value_or(ImageSourceInfo("")).GetSrc().c_str());
77         json->Put("objectFit", OBJECTFITVALUE[static_cast<int32_t>(propImageFit_.value_or(ImageFit::COVER))]);
78         json->Put("verticalAlign",
79             VERTICALALIGNVALUE[static_cast<int32_t>(propVerticalAlign_.value_or(VerticalAlign::BOTTOM))]);
80         std::string src;
81         if (propImageSourceInfo_.has_value()) {
82             src = propImageSourceInfo_->GetSrc();
83             if (src.find("resources") != std::string::npos) {
84                 auto num = src.find("resources");
85                 src = src.substr(num);
86             }
87             for (auto& character : src) {
88                 character = tolower(character);
89             }
90         }
91         json->Put("src", src.c_str());
92         json->Put("rawSrc", propImageSourceInfo_->GetSrc().c_str());
93         json->Put("moduleName", propImageSourceInfo_->GetModuleName().c_str());
94         ACE_PROPERTY_TO_JSON_VALUE(propImageSizeStyle_, ImageSizeStyle);
95     }
96 
FromJson(const std::unique_ptr<JsonValue> & json)97     void FromJson(const std::unique_ptr<JsonValue>& json) override
98     {
99         static const std::unordered_map<std::string, ImageFit> uMap {
100             { "ImageFit.Fill", ImageFit::FILL },
101             { "ImageFit.Contain", ImageFit::CONTAIN },
102             { "ImageFit.Cover", ImageFit::COVER },
103             { "ImageFit.FitWidth", ImageFit::FITWIDTH },
104             { "ImageFit.FitHeight", ImageFit::FITHEIGHT },
105             { "ImageFit.None", ImageFit::NONE },
106             { "ImageFit.ScaleDown", ImageFit::SCALE_DOWN },
107         };
108 
109         std::string src = json->GetString("rawSrc");
110         std::string bundleName = AceApplicationInfo::GetInstance().GetPackageName();
111         std::string moduleName = json->GetString("moduleName");
112         UpdateImageSourceInfo(ImageSourceInfo(src, bundleName, moduleName));
113         auto objectFit = json->GetString("objectFit");
114         UpdateImageFit(uMap.count(objectFit) ? uMap.at(objectFit) : ImageFit::COVER);
115         UpdateAutoResize(json->GetString("autoResize") == "true" ? true : false);
116         /* register image frame node to pipeline context to receive memory level notification and window state change
117          * notification */
118         auto pipeline = PipelineContext::GetCurrentContext();
119         CHECK_NULL_VOID(pipeline);
120         auto frameNode = GetHost();
121         CHECK_NULL_VOID(frameNode);
122         pipeline->AddNodesToNotifyMemoryLevel(frameNode->GetId());
123         pipeline->AddWindowStateChangedCallback(frameNode->GetId());
124         LayoutProperty::FromJson(json);
125     }
126 
127     ACE_DEFINE_PROPERTY_ITEM_WITHOUT_GROUP(ImageFit, ImageFit, PROPERTY_UPDATE_LAYOUT);
128     ACE_DEFINE_PROPERTY_ITEM_WITHOUT_GROUP(ImageSourceInfo, ImageSourceInfo, PROPERTY_UPDATE_NORMAL);
129     ACE_DEFINE_PROPERTY_ITEM_WITHOUT_GROUP(Alt, ImageSourceInfo, PROPERTY_UPDATE_NORMAL);
130     ACE_DEFINE_PROPERTY_GROUP(ImageSizeStyle, ImageSizeStyle);
131     ACE_DEFINE_PROPERTY_ITEM_WITH_GROUP(ImageSizeStyle, AutoResize, bool, PROPERTY_UPDATE_LAYOUT);
132     ACE_DEFINE_PROPERTY_ITEM_WITH_GROUP(ImageSizeStyle, SourceSize, SizeF, PROPERTY_UPDATE_LAYOUT);
133     ACE_DEFINE_PROPERTY_ITEM_WITH_GROUP(ImageSizeStyle, FitOriginalSize, bool, PROPERTY_UPDATE_LAYOUT);
134     ACE_DEFINE_PROPERTY_ITEM_WITHOUT_GROUP(VerticalAlign, VerticalAlign, PROPERTY_UPDATE_MEASURE);
135 
136 private:
137     ACE_DISALLOW_COPY_AND_MOVE(ImageLayoutProperty);
138 };
139 } // namespace OHOS::Ace::NG
140 
141 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_IMAGE_IMAGE_LAYOUT_PROPERTY_H
142