• 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_XCOMPONENT_XCOMPONENT_PATTERN_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_XCOMPONENT_XCOMPONENT_PATTERN_H
18 
19 #include <optional>
20 #include <string>
21 #include <utility>
22 
23 #include "base/geometry/dimension.h"
24 #include "base/geometry/size.h"
25 #include "base/memory/referenced.h"
26 #include "base/utils/utils.h"
27 #include "core/common/thread_checker.h"
28 #include "core/components/common/layout/constants.h"
29 #include "core/components/xcomponent/native_interface_xcomponent_impl.h"
30 #include "core/components/xcomponent/xcomponent_controller.h"
31 #include "core/components_ng/event/focus_hub.h"
32 #include "core/components_ng/event/input_event.h"
33 #include "core/components_ng/pattern/pattern.h"
34 #include "core/components_ng/pattern/xcomponent/xcomponent_event_hub.h"
35 #include "core/components_ng/pattern/xcomponent/xcomponent_layout_algorithm.h"
36 #include "core/components_ng/pattern/xcomponent/xcomponent_layout_property.h"
37 #include "core/components_ng/property/property.h"
38 #include "core/components_ng/render/render_surface.h"
39 #include "core/pipeline_ng/pipeline_context.h"
40 
41 namespace OHOS::Ace::NG {
42 class XComponentExtSurfaceCallbackClient;
43 class XComponentPattern : public Pattern {
44     DECLARE_ACE_TYPE(XComponentPattern, Pattern);
45 
46 public:
47     XComponentPattern() = default;
48     XComponentPattern(const std::string& id, XComponentType type, const std::string& libraryname,
49         const RefPtr<XComponentController>& xcomponentController);
50     ~XComponentPattern() override = default;
51 
IsAtomicNode()52     bool IsAtomicNode() const override
53     {
54         return type_ == XComponentType::SURFACE || type_ == XComponentType::TEXTURE;
55     }
56 
CreateLayoutProperty()57     RefPtr<LayoutProperty> CreateLayoutProperty() override
58     {
59         return MakeRefPtr<XComponentLayoutProperty>();
60     }
61 
CreateEventHub()62     RefPtr<EventHub> CreateEventHub() override
63     {
64         return MakeRefPtr<XComponentEventHub>();
65     }
66 
CreateLayoutAlgorithm()67     RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override
68     {
69         return MakeRefPtr<XComponentLayoutAlgorithm>();
70     }
71 
GetFocusPattern()72     FocusPattern GetFocusPattern() const override
73     {
74         return { FocusType::NODE, false };
75     }
76 
GetNativeXComponent()77     std::pair<RefPtr<OHOS::Ace::NativeXComponentImpl>, std::weak_ptr<OH_NativeXComponent>> GetNativeXComponent()
78     {
79         if (!nativeXComponent_ || !nativeXComponentImpl_) {
80             nativeXComponentImpl_ = AceType::MakeRefPtr<NativeXComponentImpl>();
81             nativeXComponent_ = std::make_shared<OH_NativeXComponent>(AceType::RawPtr(nativeXComponentImpl_));
82         }
83         return std::make_pair(nativeXComponentImpl_, nativeXComponent_);
84     }
85 
NativeXComponentInit()86     void NativeXComponentInit()
87     {
88         CHECK_RUN_ON(UI);
89         CHECK_NULL_VOID(nativeXComponentImpl_);
90         CHECK_NULL_VOID(nativeXComponent_);
91         auto host = GetHost();
92         CHECK_NULL_VOID(host);
93         auto width = initSize_.Width();
94         auto height = initSize_.Height();
95         nativeXComponentImpl_->SetXComponentWidth(static_cast<uint32_t>(width));
96         nativeXComponentImpl_->SetXComponentHeight(static_cast<uint32_t>(height));
97         auto* surface = const_cast<void*>(nativeXComponentImpl_->GetSurface());
98         const auto* callback = nativeXComponentImpl_->GetCallback();
99         if (callback && callback->OnSurfaceCreated != nullptr) {
100             callback->OnSurfaceCreated(nativeXComponent_.get(), surface);
101         }
102     }
103 
104     void OnPaint();
105     void NativeXComponentOffset(double x, double y);
106     void NativeXComponentChange(float width, float height);
107     void NativeXComponentDestroy();
108     void NativeXComponentDispatchTouchEvent(const OH_NativeXComponent_TouchEvent& touchEvent,
109         const std::vector<XComponentTouchPoint>& xComponentTouchPoints);
110     void NativeXComponentDispatchMouseEvent(const OH_NativeXComponent_MouseEvent& mouseEvent);
111 
112     void InitNativeWindow(float textureWidth, float textureHeight);
113     void XComponentSizeInit();
114     void XComponentSizeChange(float textureWidth, float textureHeight);
115 
GetNativeWindow()116     void* GetNativeWindow()
117     {
118         return renderSurface_->GetNativeWindow();
119     }
120 
GetId()121     const std::string& GetId() const
122     {
123         return id_;
124     }
125 
GetLibraryName()126     const std::string& GetLibraryName() const
127     {
128         return libraryname_;
129     }
130 
GetSoPath()131     const std::optional<std::string>& GetSoPath() const
132     {
133         return soPath_;
134     }
135 
SetSoPath(const std::string & soPath)136     void SetSoPath(const std::string& soPath)
137     {
138         soPath_ = soPath;
139     }
140 
141 private:
142     void OnAttachToFrameNode() override;
143     void OnDetachFromFrameNode(FrameNode* frameNode) override;
144     bool OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& dirty, const DirtySwapConfig& config) override;
145     void OnRebuildFrame() override;
146     void OnAreaChangedInner() override;
147 
148     void InitEvent();
149     void InitTouchEvent(const RefPtr<GestureEventHub>& gestureHub);
150     void HandleTouchEvent(const TouchEventInfo& info);
151     void InitMouseEvent(const RefPtr<InputEventHub>& inputHub);
152     void HandleMouseEvent(const MouseInfo& info);
153     void InitMouseHoverEvent(const RefPtr<InputEventHub>& inputHub);
154     void HandleMouseHoverEvent(bool isHover);
155     void InitFocusEvent(const RefPtr<FocusHub>& focusHub);
156     void HandleFocusEvent();
157     bool HandleKeyEvent(const KeyEvent& event);
158     void HandleBlurEvent();
159     ExternalEvent CreateExternalEvent();
160     void CreateSurface();
161     void SetMethodCall();
162     void ConfigSurface(uint32_t surfaceWidth, uint32_t surfaceHeight);
163     void SetTouchPoint(
164         const std::list<TouchLocationInfo>& touchInfoList, int64_t timeStamp, const TouchType& touchType);
165 
166     std::vector<OH_NativeXComponent_HistoricalPoint> SetHistoryPoint(const std::list<TouchLocationInfo>& touchInfoList);
167     std::string id_;
168     XComponentType type_;
169     std::string libraryname_;
170     RefPtr<XComponentController> xcomponentController_;
171     std::optional<std::string> soPath_;
172 
173     RefPtr<RenderSurface> renderSurface_;
174     RefPtr<RenderContext> renderContextForSurface_;
175 
176     std::shared_ptr<OH_NativeXComponent> nativeXComponent_;
177     RefPtr<NativeXComponentImpl> nativeXComponentImpl_;
178 
179     bool hasXComponentInit_ = false;
180 
181     RefPtr<TouchEventImpl> touchEvent_;
182     OH_NativeXComponent_TouchEvent touchEventPoint_;
183     RefPtr<InputEvent> mouseEvent_;
184     RefPtr<InputEvent> mouseHoverEvent_;
185     std::vector<XComponentTouchPoint> nativeXComponentTouchPoints_;
186     RefPtr<XComponentExtSurfaceCallbackClient> extSurfaceClient_;
187     WeakPtr<NG::PipelineContext> context_;
188     int32_t instanceId_;
189     SizeF initSize_;
190 #ifdef OHOS_PLATFORM
191     int64_t startIncreaseTime_ = 0;
192 #endif
193 };
194 } // namespace OHOS::Ace::NG
195 
196 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_XCOMPONENT_XCOMPONENT_PATTERN_H
197