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 #include "core/components_v2/ability_component/render_ability_component.h"
17
18 #include "base/memory/ace_type.h"
19 #include "core/common/container.h"
20 #include "core/components/container_modal/container_modal_constants.h"
21 #include "core/components_v2/ability_component/ability_component.h"
22
23 namespace OHOS::Ace::V2 {
24
~RenderAbilityComponent()25 RenderAbilityComponent::~RenderAbilityComponent()
26 {
27 if (adapter_) {
28 adapter_->RemoveExtension();
29 }
30
31 auto context = context_.Upgrade();
32 if (!context || callbackId_ <= 0) {
33 return;
34 }
35 context->UnregisterSurfacePositionChangedCallback(callbackId_);
36 }
37
Update(const RefPtr<Component> & component)38 void RenderAbilityComponent::Update(const RefPtr<Component>& component)
39 {
40 RefPtr<V2::AbilityComponent> abilityComponent = AceType::DynamicCast<V2::AbilityComponent>(component);
41 if (!abilityComponent) {
42 LOGE("[abilityComponent] Update Get component failed");
43 return;
44 }
45
46 auto context = context_.Upgrade();
47 if (context && callbackId_ <= 0) {
48 callbackId_ =
49 context->RegisterSurfacePositionChangedCallback([weak = WeakClaim(this)](int32_t posX, int32_t posY) {
50 auto client = weak.Upgrade();
51 if (client) {
52 client->ConnectOrUpdateExtension();
53 }
54 });
55 }
56
57 component_ = abilityComponent;
58 auto width = abilityComponent->GetWidth();
59 auto height = abilityComponent->GetHeight();
60 if (width == width_ && height == height_) {
61 LOGI("size not change.");
62 return;
63 }
64 width_ = width;
65 height_ = height;
66 needLayout_ = true;
67 }
68
PerformLayout()69 void RenderAbilityComponent::PerformLayout()
70 {
71 Size size = Size(NormalizePercentToPx(width_, false), NormalizePercentToPx(height_, true));
72 currentRect_.SetSize(size);
73 if (currentRect_.GetSize().IsEmpty()) {
74 currentRect_.SetSize(GetLayoutParam().GetMaxSize());
75 }
76
77 SetLayoutSize(currentRect_.GetSize());
78 }
79
Paint(RenderContext & context,const Offset & offset)80 void RenderAbilityComponent::Paint(RenderContext& context, const Offset& offset)
81 {
82 ConnectOrUpdateExtension();
83 }
84
ConnectOrUpdateExtension()85 void RenderAbilityComponent::ConnectOrUpdateExtension()
86 {
87 Offset globalOffset = GetGlobalOffsetExternal();
88 if (currentRect_.GetOffset() == globalOffset && !needLayout_ && hasConnectionToAbility_) {
89 return;
90 }
91
92 auto pipelineContext = context_.Upgrade();
93 if (!pipelineContext) {
94 return;
95 }
96
97 auto parentWindowOffset = pipelineContext->GetCurrentWindowRect().GetOffset();
98 Offset containerModalOffset;
99 auto isContainerModal = pipelineContext->GetWindowModal() == WindowModal::CONTAINER_MODAL &&
100 pipelineContext->GetWindowManager()->GetWindowMode() == WindowMode::WINDOW_MODE_FLOATING;
101 if (isContainerModal) {
102 containerModalOffset = Offset((NormalizeToPx(CONTAINER_BORDER_WIDTH) + NormalizeToPx(CONTENT_PADDING)),
103 NormalizeToPx(CONTAINER_TITLE_HEIGHT));
104 }
105 currentRect_.SetOffset(globalOffset + parentWindowOffset + containerModalOffset);
106
107 if (!adapter_) {
108 adapter_ = WindowExtensionConnectionProxy::CreateAdapter();
109 auto windowId = pipelineContext->GetWindowId();
110 adapter_->ConnectExtension(component_->GetWant(), currentRect_, AceType::Claim(this), windowId);
111 return;
112 }
113
114 if (hasConnectionToAbility_) {
115 adapter_->UpdateRect(currentRect_);
116 }
117 }
118
OnPaintFinish()119 void RenderAbilityComponent::OnPaintFinish()
120 {
121 if (globalOffsetExternal_ != GetGlobalOffsetExternal()) {
122 globalOffsetExternal_ = GetGlobalOffsetExternal();
123 ConnectOrUpdateExtension();
124 }
125 }
126
127 } // namespace OHOS::Ace::V2
128