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 #include "core/components_ng/pattern/ability_component/ability_component_pattern.h"
17
18 #include "session/host/include/extension_session.h"
19 #include "session_manager/include/extension_session_manager.h"
20
21 #include "adapter/ohos/entrance/ace_container.h"
22 #include "adapter/ohos/entrance/mmi_event_convertor.h"
23 #include "adapter/ohos/osal/want_wrap_ohos.h"
24 #include "base/utils/utils.h"
25 #include "core/common/container.h"
26 #include "core/pipeline_ng/pipeline_context.h"
27
28 namespace OHOS::Ace::NG {
AbilityComponentPattern(const std::string & bundleName,const std::string & abilityName)29 AbilityComponentPattern::AbilityComponentPattern(const std::string& bundleName, const std::string& abilityName)
30 {
31 auto container = AceType::DynamicCast<Platform::AceContainer>(Container::Current());
32 if (container && container->IsSceneBoardEnabled()) {
33 auto wantWrap = Ace::WantWrap::CreateWantWrap(bundleName, abilityName);
34 auto want = AceType::DynamicCast<WantWrapOhos>(wantWrap)->GetWant();
35 Rosen::SessionInfo extensionSessionInfo = {
36 .bundleName_ = bundleName,
37 .abilityName_ = abilityName,
38 .callerToken_ = container->GetToken(),
39 .want = std::make_shared<Want>(want),
40 };
41 session_ = Rosen::ExtensionSessionManager::GetInstance().RequestExtensionSession(extensionSessionInfo);
42 }
43 }
44
OnModifyDone()45 void AbilityComponentPattern::OnModifyDone()
46 {
47 auto container = Container::Current();
48 if (container && container->IsSceneBoardEnabled()) {
49 Pattern::OnModifyDone();
50 auto host = GetHost();
51 CHECK_NULL_VOID(host);
52 auto hub = host->GetEventHub<EventHub>();
53 CHECK_NULL_VOID(hub);
54 auto gestureHub = hub->GetOrCreateGestureEventHub();
55 CHECK_NULL_VOID(gestureHub);
56 InitTouchEvent(gestureHub);
57 auto inputHub = hub->GetOrCreateInputEventHub();
58 CHECK_NULL_VOID(inputHub);
59 InitMouseEvent(inputHub);
60 auto focusHub = host->GetFocusHub();
61 CHECK_NULL_VOID(focusHub);
62 InitOnKeyEvent(focusHub);
63 }
64 if (adapter_) {
65 UpdateWindowRect();
66 } else {
67 auto host = GetHost();
68 CHECK_NULL_VOID(host);
69 auto pipelineContext = host->GetContext();
70 CHECK_NULL_VOID(pipelineContext);
71 auto windowId = pipelineContext->GetWindowId();
72 adapter_ = WindowExtensionConnectionProxyNG::CreateAdapter();
73 CHECK_NULL_VOID(adapter_);
74 if (container && container->IsSceneBoardEnabled()) {
75 CHECK_NULL_VOID(session_);
76 sptr<Rosen::ExtensionSession> extensionSession(static_cast<Rosen::ExtensionSession*>(session_.GetRefPtr()));
77 CHECK_NULL_VOID(extensionSession);
78 adapter_->SetExtensionSession(extensionSession);
79 }
80 adapter_->ConnectExtension(GetHost(), windowId);
81 pipelineContext->AddOnAreaChangeNode(host->GetId());
82 pipelineContext->AddWindowStateChangedCallback(host->GetId());
83 LOGI("connect to windows extension begin %{public}s", GetHost()->GetTag().c_str());
84 }
85 }
86
FireConnect()87 void AbilityComponentPattern::FireConnect()
88 {
89 hasConnectionToAbility_ = true;
90 UpdateWindowRect();
91 auto pipeline = PipelineBase::GetCurrentContext();
92 TransferFocusState(IsCurrentFocus());
93
94 auto abilityComponentEventHub = GetEventHub<AbilityComponentEventHub>();
95 CHECK_NULL_VOID(abilityComponentEventHub);
96 abilityComponentEventHub->FireOnConnect();
97 }
98
FireDisConnect()99 void AbilityComponentPattern::FireDisConnect()
100 {
101 hasConnectionToAbility_ = false;
102 auto abilityComponentEventHub = GetEventHub<AbilityComponentEventHub>();
103 CHECK_NULL_VOID(abilityComponentEventHub);
104 abilityComponentEventHub->FireOnDisConnect();
105 }
106
OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper> &,const DirtySwapConfig & config)107 bool AbilityComponentPattern::OnDirtyLayoutWrapperSwap(
108 const RefPtr<LayoutWrapper>& /*dirty*/, const DirtySwapConfig& config)
109 {
110 if (config.frameSizeChange || config.frameOffsetChange) {
111 UpdateWindowRect();
112 }
113 return false;
114 }
115
GetFocusPattern() const116 FocusPattern AbilityComponentPattern::GetFocusPattern() const
117 {
118 return { FocusType::NODE, true, FocusStyleType::NONE };
119 }
120
UpdateWindowRect()121 void AbilityComponentPattern::UpdateWindowRect()
122 {
123 if (!hasConnectionToAbility_) {
124 return;
125 }
126 auto host = GetHost();
127 CHECK_NULL_VOID(host);
128 auto size = host->GetGeometryNode()->GetFrameSize();
129 auto offset = host->GetTransformRelativeOffset();
130 auto pipeline = host->GetContext();
131 CHECK_NULL_VOID(pipeline);
132 Rect rect = pipeline->GetDisplayWindowRectInfo();
133 rect = Rect(offset.GetX() + rect.Left(), offset.GetY() + rect.Top(), size.Width(), size.Height());
134 if (adapter_ && rect != lastRect_) {
135 LOGI("ConnectExtension: %{public}f %{public}f %{public}f %{public}f", offset.GetX(), offset.GetY(),
136 size.Width(), size.Height());
137 adapter_->UpdateRect(rect);
138 lastRect_ = rect;
139 }
140 }
141
OnAreaChangedInner()142 void AbilityComponentPattern::OnAreaChangedInner()
143 {
144 UpdateWindowRect();
145 }
146
InitTouchEvent(const RefPtr<GestureEventHub> & gestureHub)147 void AbilityComponentPattern::InitTouchEvent(const RefPtr<GestureEventHub>& gestureHub)
148 {
149 if (touchEvent_) {
150 return;
151 }
152 auto callback = [weak = WeakClaim(this)](const TouchEventInfo& info) {
153 auto pattern = weak.Upgrade();
154 if (pattern) {
155 pattern->HandleTouchEvent(info);
156 }
157 };
158 if (touchEvent_) {
159 gestureHub->RemoveTouchEvent(touchEvent_);
160 }
161 touchEvent_ = MakeRefPtr<TouchEventImpl>(std::move(callback));
162 gestureHub->AddTouchEvent(touchEvent_);
163 }
164
InitMouseEvent(const RefPtr<InputEventHub> & inputHub)165 void AbilityComponentPattern::InitMouseEvent(const RefPtr<InputEventHub>& inputHub)
166 {
167 if (mouseEvent_) {
168 return;
169 }
170 auto callback = [weak = WeakClaim(this)](MouseInfo& info) {
171 auto pattern = weak.Upgrade();
172 if (pattern) {
173 pattern->HandleMouseEvent(info);
174 }
175 };
176 if (mouseEvent_) {
177 inputHub->RemoveOnMouseEvent(mouseEvent_);
178 }
179 mouseEvent_ = MakeRefPtr<InputEvent>(std::move(callback));
180 inputHub->AddOnMouseEvent(mouseEvent_);
181 }
182
HandleTouchEvent(const TouchEventInfo & info)183 void AbilityComponentPattern::HandleTouchEvent(const TouchEventInfo& info)
184 {
185 if (info.GetSourceDevice() != SourceType::TOUCH) {
186 return;
187 }
188 const auto pointerEvent = info.GetPointerEvent();
189 CHECK_NULL_VOID(pointerEvent);
190 auto host = GetHost();
191 CHECK_NULL_VOID(host);
192 Platform::CalculatePointerEvent(pointerEvent, host);
193 WindowPattern::DispatchPointerEvent(pointerEvent);
194 auto hub = host->GetFocusHub();
195 CHECK_NULL_VOID(hub);
196 hub->RequestFocusImmediately();
197 }
198
HandleMouseEvent(const MouseInfo & info)199 void AbilityComponentPattern::HandleMouseEvent(const MouseInfo& info)
200 {
201 if (info.GetSourceDevice() != SourceType::MOUSE) {
202 return;
203 }
204 const auto pointerEvent = info.GetPointerEvent();
205 CHECK_NULL_VOID(pointerEvent);
206 auto host = GetHost();
207 CHECK_NULL_VOID(host);
208 auto selfGlobalOffset = host->GetTransformRelativeOffset();
209 auto scale = host->GetTransformScale();
210 Platform::CalculatePointerEvent(selfGlobalOffset, pointerEvent, scale);
211 if (info.GetAction() == MouseAction::PRESS) {
212 auto hub = host->GetFocusHub();
213 CHECK_NULL_VOID(hub);
214 hub->RequestFocusImmediately();
215 }
216 WindowPattern::DispatchPointerEvent(pointerEvent);
217 }
218
InitOnKeyEvent(const RefPtr<FocusHub> & focusHub)219 void AbilityComponentPattern::InitOnKeyEvent(const RefPtr<FocusHub>& focusHub)
220 {
221 focusHub->SetOnFocusInternal([weak = WeakClaim(this)]() {
222 auto pattern = weak.Upgrade();
223 if (pattern) {
224 pattern->HandleFocusEvent();
225 }
226 });
227
228 focusHub->SetOnBlurInternal([weak = WeakClaim(this)]() {
229 auto pattern = weak.Upgrade();
230 if (pattern) {
231 pattern->HandleBlurEvent();
232 }
233 });
234
235 focusHub->SetOnClearFocusStateInternal([weak = WeakClaim(this)]() {
236 auto pattern = weak.Upgrade();
237 if (pattern) {
238 pattern->DisPatchFocusActiveEvent(false);
239 }
240 });
241 focusHub->SetOnPaintFocusStateInternal([weak = WeakClaim(this)]() -> bool {
242 auto pattern = weak.Upgrade();
243 if (pattern) {
244 pattern->DisPatchFocusActiveEvent(true);
245 return true;
246 }
247 return false;
248 });
249
250 focusHub->SetOnKeyEventInternal([wp = WeakClaim(this)](const KeyEvent& event) -> bool {
251 auto pattern = wp.Upgrade();
252 if (pattern) {
253 return pattern->OnKeyEvent(event);
254 }
255 return false;
256 });
257 }
258
HandleFocusEvent()259 void AbilityComponentPattern::HandleFocusEvent()
260 {
261 auto host = GetHost();
262 CHECK_NULL_VOID(host);
263 auto pipeline = host->GetContext();
264 CHECK_NULL_VOID(pipeline);
265 if (pipeline->GetIsFocusActive()) {
266 WindowPattern::DisPatchFocusActiveEvent(true);
267 }
268 TransferFocusState(true);
269 }
270
HandleBlurEvent()271 void AbilityComponentPattern::HandleBlurEvent()
272 {
273 WindowPattern::DisPatchFocusActiveEvent(false);
274 TransferFocusState(false);
275 }
276
KeyEventConsumed(const KeyEvent & event)277 bool AbilityComponentPattern::KeyEventConsumed(const KeyEvent& event)
278 {
279 bool isConsumed = false;
280 WindowPattern::DispatchKeyEventForConsumed(event.rawKeyEvent, isConsumed);
281 return isConsumed;
282 }
283
OnKeyEvent(const KeyEvent & event)284 bool AbilityComponentPattern::OnKeyEvent(const KeyEvent& event)
285 {
286 if (event.code == KeyCode::KEY_TAB && event.action == KeyAction::DOWN) {
287 auto host = GetHost();
288 CHECK_NULL_RETURN(host, false);
289 auto pipeline = host->GetContext();
290 CHECK_NULL_RETURN(pipeline, false);
291 // tab trigger consume the key event
292 return pipeline->IsTabJustTriggerOnKeyEvent();
293 } else {
294 return KeyEventConsumed(event);
295 }
296 }
297
IsCurrentFocus() const298 bool AbilityComponentPattern::IsCurrentFocus() const
299 {
300 auto host = GetHost();
301 CHECK_NULL_RETURN(host, false);
302 auto focusHub = host->GetFocusHub();
303 CHECK_NULL_RETURN(focusHub, false);
304 return focusHub->IsCurrentFocus();
305 }
306 } // namespace OHOS::Ace::NG
307