1 /*
2  * Copyright (c) 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/window_scene/scene/window_node.h"
17 
18 #include "core/components_ng/pattern/window_scene/scene/window_pattern.h"
19 #include "core/components_ng/pattern/window_scene/screen/screen_pattern.h"
20 #include "core/pipeline_ng/pipeline_context.h"
21 #include "session_manager/include/scene_session_manager.h"
22 
23 namespace OHOS::Ace::NG {
24 namespace {
25 constexpr float MOUSE_RECT_HOT_VP = 4.0f;
26 constexpr float TOUCH_RECT_HOT_VP = 20.0f;
27 constexpr double DEFAULT_HOT_DENSITY = 1.5f;
28 std::map<int32_t, std::map<int32_t, WeakPtr<WindowNode>>> g_windowNodeMap;
29 }
30 
WindowNode(const std::string & tag,int32_t nodeId,const RefPtr<Pattern> & pattern,bool isRoot)31 WindowNode::WindowNode(const std::string& tag,
32     int32_t nodeId, const RefPtr<Pattern>& pattern, bool isRoot)
33     : FrameNode(tag, nodeId, pattern, isRoot) {}
34 
WindowNode(const std::string & tag,int32_t nodeId,int32_t sessionId,const RefPtr<Pattern> & pattern,bool isRoot,int32_t screenId)35 WindowNode::WindowNode(const std::string& tag,
36     int32_t nodeId, int32_t sessionId, const RefPtr<Pattern>& pattern, bool isRoot, int32_t screenId)
37     : FrameNode(tag, nodeId, pattern, isRoot)
38 {
39     screenId_ = screenId;
40     sessionId_ = sessionId;
41 }
42 
~WindowNode()43 WindowNode::~WindowNode()
44 {
45     g_windowNodeMap[screenId_].erase(sessionId_);
46 }
47 
GetOrCreateWindowNode(const std::string & tag,int32_t nodeId,int32_t sessionId,const std::function<RefPtr<Pattern> (void)> & patternCreator)48 RefPtr<WindowNode> WindowNode::GetOrCreateWindowNode(const std::string& tag,
49     int32_t nodeId, int32_t sessionId, const std::function<RefPtr<Pattern>(void)>& patternCreator)
50 {
51     auto windowNode = ElementRegister::GetInstance()->GetSpecificItemById<WindowNode>(nodeId);
52     if (windowNode) {
53         if (windowNode->GetTag() == tag) {
54             return windowNode;
55         }
56         ElementRegister::GetInstance()->RemoveItemSilently(nodeId);
57         auto parent = windowNode->GetParent();
58         if (parent) {
59             parent->RemoveChild(windowNode);
60         }
61     }
62 
63     auto sceneSession = Rosen::SceneSessionManager::GetInstance().GetSceneSession(sessionId);
64     if (sceneSession == nullptr) {
65         TAG_LOGE(AceLogTag::ACE_WINDOW_SCENE, "sessionId: %{public}d", sessionId);
66         return nullptr;
67     }
68     auto screenId = static_cast<int>(sceneSession->GetScreenId());
69     auto screenIter = g_windowNodeMap.find(screenId);
70     if (screenIter != g_windowNodeMap.end()) {
71         auto sessionMap = screenIter->second;
72         auto iter = sessionMap.find(sessionId);
73         if (iter != sessionMap.end()) {
74             auto node = iter->second.Upgrade();
75             if (node) {
76                 TAG_LOGI(AceLogTag::ACE_WINDOW_SCENE,
77                     "screenId: %{public}d, node id: %{public}d, sessionId: %{public}d",
78                     screenId, node->GetId(), sessionId);
79                 return node;
80             }
81         }
82     }
83 
84     auto pattern = patternCreator ? patternCreator() : AceType::MakeRefPtr<Pattern>();
85     windowNode = AceType::MakeRefPtr<WindowNode>(tag, nodeId, sessionId, pattern, false, screenId);
86     windowNode->InitializePatternAndContext();
87     bool added = ElementRegister::GetInstance()->AddUINode(windowNode);
88     if (!added) {
89         TAG_LOGW(AceLogTag::ACE_WINDOW_SCENE, "Add UINode failed, node id: %{public}d",
90             nodeId);
91     }
92     g_windowNodeMap[screenId][sessionId] = WeakPtr<WindowNode>(windowNode);
93     return windowNode;
94 }
95 
SetParent(const WeakPtr<UINode> & parent)96 void WindowNode::SetParent(const WeakPtr<UINode>& parent)
97 {
98     auto prevParent = GetParent();
99     if (prevParent && prevParent != parent.Upgrade()) {
100         RemoveFromParentCleanly(Claim(this), prevParent);
101     }
102     UINode::SetParent(parent);
103 }
104 
IsOutOfTouchTestRegion(const PointF & parentLocalPoint,const TouchEvent & touchEvent)105 bool WindowNode::IsOutOfTouchTestRegion(const PointF& parentLocalPoint, const TouchEvent& touchEvent)
106 {
107     auto pattern = GetPattern<WindowPattern>();
108     if (pattern != nullptr) {
109         auto hotAreas = pattern->GetHotAreas();
110         if (!hotAreas.empty()) {
111             auto hotRects = ConvertHotRects(hotAreas);
112             for (auto& hotRect : hotRects) {
113                 if (hotRect.IsInRegion(parentLocalPoint)) {
114                     return false;
115                 }
116             }
117             return true;
118         }
119     }
120     const auto& rect = GetPaintRectWithTransform();
121     const auto& hotRect = ConvertHotRect(rect, static_cast<int32_t>(touchEvent.sourceType));
122     if (!hotRect.IsInRegion(parentLocalPoint)) {
123         return true;
124     }
125     return false;
126 }
127 
GetResponseRegionList(const RectF & rect,int32_t sourceType)128 std::vector<RectF> WindowNode::GetResponseRegionList(const RectF& rect, int32_t sourceType)
129 {
130     auto pattern = GetPattern<WindowPattern>();
131     if (pattern != nullptr) {
132         auto hotAreas = pattern->GetHotAreas();
133         if (!hotAreas.empty()) {
134             return ConvertHotRects(hotAreas);
135         }
136     }
137     std::vector<RectF> responseRegionList;
138     responseRegionList.emplace_back(ConvertHotRect(rect, sourceType));
139     return responseRegionList;
140 }
141 
ConvertHotRects(const std::vector<Rosen::Rect> & hotAreas)142 std::vector<RectF> WindowNode::ConvertHotRects(const std::vector<Rosen::Rect>& hotAreas)
143 {
144     std::vector<RectF> responseRegionList;
145     for (size_t i = 0; i < hotAreas.size(); i++) {
146         float hotX = static_cast<float>(hotAreas[i].posX_);
147         float hotY = static_cast<float>(hotAreas[i].posY_);
148         float hotWidth = static_cast<float>(hotAreas[i].width_);
149         float hotHeight = static_cast<float>(hotAreas[i].height_);
150         RectF rectHot(hotX, hotY, hotWidth, hotHeight);
151         responseRegionList.emplace_back(rectHot);
152     }
153     return responseRegionList;
154 }
155 
ConvertHotRect(const RectF & rect,int32_t sourceType)156 RectF WindowNode::ConvertHotRect(const RectF& rect, int32_t sourceType)
157 {
158     auto pattern = GetPattern<WindowPattern>();
159     if (pattern != nullptr && pattern->GetSession() != nullptr &&
160         (pattern->GetSession()->GetWindowType() != Rosen::WindowType::WINDOW_TYPE_APP_MAIN_WINDOW &&
161         pattern->GetSession()->GetWindowType() != Rosen::WindowType::WINDOW_TYPE_APP_SUB_WINDOW)) {
162         return rect;
163     }
164     float hotOffsetVp = (sourceType == static_cast<int32_t>(Ace::SourceType::MOUSE)) ?
165         MOUSE_RECT_HOT_VP : TOUCH_RECT_HOT_VP;
166     auto context = PipelineContext::GetCurrentContext();
167     double density = (context != nullptr) ? context->GetDensity() : DEFAULT_HOT_DENSITY;
168     float hotOffset = static_cast<float>(hotOffsetVp * density);
169     float hotX = rect.GetX() - hotOffset;
170     float hotY = rect.GetY() - hotOffset;
171     float hotWidth = rect.Width() + hotOffset * 2;
172     float hotHeight = rect.Height() + hotOffset * 2;
173     RectF rectHot(hotX, hotY, hotWidth, hotHeight);
174     return rectHot;
175 }
176 } // namespace OHOS::Ace::NG
177