• 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_PATTERNS_IMAGE_IMAGE_PATTERN_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_IMAGE_IMAGE_PATTERN_H
18 
19 #include <memory>
20 
21 #include "base/geometry/offset.h"
22 #include "base/memory/referenced.h"
23 #include "core/components/common/layout/constants.h"
24 #include "core/components_ng/event/click_event.h"
25 #include "core/components_ng/manager/select_overlay/selection_host.h"
26 #include "core/components_ng/pattern/image/image_event_hub.h"
27 #include "core/components_ng/pattern/image/image_layout_algorithm.h"
28 #include "core/components_ng/pattern/image/image_layout_property.h"
29 #include "core/components_ng/pattern/image/image_render_property.h"
30 #include "core/components_ng/pattern/pattern.h"
31 #include "core/components_ng/render/canvas_image.h"
32 #include "core/image/image_source_info.h"
33 
34 namespace OHOS::Ace::NG {
35 
36 class ACE_EXPORT ImagePattern : public Pattern, public SelectionHost {
37     DECLARE_ACE_TYPE(ImagePattern, Pattern, SelectionHost);
38 
39 public:
40     ImagePattern() = default;
41     ~ImagePattern() override = default;
42 
GetContextParam()43     std::optional<RenderContext::ContextParam> GetContextParam() const override
44     {
45         return RenderContext::ContextParam { RenderContext::ContextType::CANVAS };
46     }
47 
48     RefPtr<NodePaintMethod> CreateNodePaintMethod() override;
49 
CreateLayoutProperty()50     RefPtr<LayoutProperty> CreateLayoutProperty() override
51     {
52         return MakeRefPtr<ImageLayoutProperty>();
53     }
54 
CreatePaintProperty()55     RefPtr<PaintProperty> CreatePaintProperty() override
56     {
57         return MakeRefPtr<ImageRenderProperty>();
58     }
59 
CreateLayoutAlgorithm()60     RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override
61     {
62         return MakeRefPtr<ImageLayoutAlgorithm>(loadingCtx_, altLoadingCtx_);
63     }
64 
CreateEventHub()65     RefPtr<EventHub> CreateEventHub() override
66     {
67         return MakeRefPtr<ImageEventHub>();
68     }
69 
70     // Called on main thread to check if need rerender of the content.
71     bool OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& dirty, const DirtySwapConfig& config) override;
72 
GetFocusPattern()73     FocusPattern GetFocusPattern() const override
74     {
75         return { FocusType::NODE, false };
76     }
77 
GetCanvasImage()78     const RefPtr<CanvasImage>& GetCanvasImage()
79     {
80         return image_;
81     }
82 
83     void CreateObscuredImage();
84     void LoadImageDataIfNeed();
85     void OnNotifyMemoryLevel(int32_t level) override;
86     void OnWindowHide() override;
87     void OnWindowShow() override;
88     void OnVisibleChange(bool isVisible) override;
89 
90     void EnableDrag();
91     bool BetweenSelectedPosition(const Offset& globalOffset) override;
92 
DefaultSupportDrag()93     bool DefaultSupportDrag() override
94     {
95         return true;
96     }
97 
SetCopyOption(CopyOptions value)98     void SetCopyOption(CopyOptions value)
99     {
100         copyOption_ = value;
101     }
102 
SetSyncLoad(bool value)103     void SetSyncLoad(bool value)
104     {
105         syncLoad_ = value;
106     }
107 
108     void BeforeCreatePaintWrapper() override;
109     void DumpInfo() override;
110 
111 private:
112     class ObscuredImage : public CanvasImage {
DrawToRSCanvas(RSCanvas & canvas,const RSRect & srcRect,const RSRect & dstRect,const BorderRadiusArray & radiusXY)113         void DrawToRSCanvas(
114             RSCanvas& canvas, const RSRect& srcRect, const RSRect& dstRect, const BorderRadiusArray& radiusXY) override
115         {}
GetWidth()116         int32_t GetWidth() const override
117         {
118             return 0;
119         }
GetHeight()120         int32_t GetHeight() const override
121         {
122             return 0;
123         }
124     };
125 
126     void OnAttachToFrameNode() override;
127     void OnDetachFromFrameNode(FrameNode* frameNode) override;
128 
129     void OnModifyDone() override;
130 
131     void OnLanguageConfigurationUpdate() override;
132 
133     void OnImageDataReady();
134     void OnImageLoadFail();
135     void OnImageLoadSuccess();
136     void SetImagePaintConfig(
137         const RefPtr<CanvasImage>& canvasImage, const RectF& srcRect, const RectF& dstRect, bool isSvg);
138     void UpdateInternalResource(ImageSourceInfo& sourceInfo);
139 
140     void PrepareAnimation(const RefPtr<CanvasImage>& image);
141     void SetRedrawCallback(const RefPtr<CanvasImage>& image);
142     void RegisterVisibleAreaChange();
143 
144     void InitCopy();
145     void HandleCopy();
146     void OpenSelectOverlay();
147     void CloseSelectOverlay();
148 
149     void UpdateFillColorIfForegroundColor();
150     void UpdateDragEvent(const RefPtr<OHOS::Ace::DragEvent>& event);
151 
152     void ToJsonValue(std::unique_ptr<JsonValue>& json) const override;
153 
154     RectF CalcImageContentPaintSize(const RefPtr<GeometryNode>& geometryNode);
155 
156     DataReadyNotifyTask CreateDataReadyCallback();
157     LoadSuccessNotifyTask CreateLoadSuccessCallback();
158     LoadFailNotifyTask CreateLoadFailCallback();
159 
160     DataReadyNotifyTask CreateDataReadyCallbackForAlt();
161     LoadSuccessNotifyTask CreateLoadSuccessCallbackForAlt();
162     LoadFailNotifyTask CreateLoadFailCallbackForAlt();
163 
164     CopyOptions copyOption_ = CopyOptions::None;
165 
166     RefPtr<ImageLoadingContext> loadingCtx_;
167     RefPtr<CanvasImage> image_;
168     RectF dstRect_;
169     RectF srcRect_;
170 
171     RefPtr<CanvasImage> obscuredImage_;
172 
173     // clear alt data after [OnImageLoadSuccess] being called
174     RefPtr<ImageLoadingContext> altLoadingCtx_;
175     RefPtr<CanvasImage> altImage_;
176     std::unique_ptr<RectF> altDstRect_;
177     std::unique_ptr<RectF> altSrcRect_;
178 
179     RefPtr<LongPressEvent> longPressEvent_;
180     RefPtr<ClickEvent> clickEvent_;
181     RefPtr<InputEvent> mouseEvent_;
182     RefPtr<Clipboard> clipboard_;
183     RefPtr<SelectOverlayProxy> selectOverlay_;
184 
185     bool syncLoad_ = false;
186     bool isShow_ = true;
187 
188     ACE_DISALLOW_COPY_AND_MOVE(ImagePattern);
189 };
190 
191 } // namespace OHOS::Ace::NG
192 
193 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_IMAGE_IMAGE_PATTERN_H
194