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