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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_WINDOW_PATTERN_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_WINDOW_PATTERN_H
18 
19 #include "common/rs_vector4.h"
20 #include "key_event.h"
21 #include "pointer_event.h"
22 #include "session/host/include/session.h"
23 
24 #include "core/common/container.h"
25 #include "core/components_ng/pattern/stack/stack_pattern.h"
26 #include "core/image/image_source_info.h"
27 
28 namespace OHOS::Ace::NG {
29 class WindowPattern : public StackPattern {
30     DECLARE_ACE_TYPE(WindowPattern, StackPattern);
31 
32 public:
33     WindowPattern() = default;
34     ~WindowPattern() override = default;
35 
36     std::vector<Rosen::Rect> GetHotAreas();
37     sptr<Rosen::Session> GetSession();
38 
39 protected:
40     void OnAttachToFrameNode() override;
41 
42     void DispatchPointerEvent(const std::shared_ptr<MMI::PointerEvent>& pointerEvent);
43     void DispatchKeyEvent(const std::shared_ptr<MMI::KeyEvent>& keyEvent);
44     void DispatchKeyEventForConsumed(const std::shared_ptr<MMI::KeyEvent>& keyEvent, bool& isConsumed);
45     void DisPatchFocusActiveEvent(bool isFocusActive);
46     void TransferFocusState(bool focusState);
47 
48     virtual bool HasStartingPage() = 0;
49     bool IsMainWindow() const;
50 
51     void RegisterLifecycleListener();
52     void UnregisterLifecycleListener();
53 
54 #ifdef ATOMIC_SERVICE_ATTRIBUTION_ENABLE
55     RefPtr<FrameNode> BuildTextNode(const std::string& appNameInfo);
56     RefPtr<FrameNode> BuildAnimateNode(const std::string& base64Resource);
57     RefPtr<FrameNode> BuildStaticImageNode(const std::string& base64Resource);
58     void CreateASStartingWindow();
59 #endif
60 
61     void CreateAppWindow();
62     void CreateBlankWindow();
63     void CreateStartingWindow();
64     void CreateSnapshotWindow(std::optional<std::shared_ptr<Media::PixelMap>> snapshot = std::nullopt);
65     void ClearImageCache(const ImageSourceInfo& sourceInfo);
66 
67     void AddChild(const RefPtr<FrameNode>& host, const RefPtr<FrameNode>& child,
68         const std::string& nodeType, int32_t index = DEFAULT_NODE_SLOT);
69     void RemoveChild(const RefPtr<FrameNode>& host, const RefPtr<FrameNode>& child,
70         const std::string& nodeType, bool allowTransition = false);
71 
OnActivation()72     virtual void OnActivation() {}
OnConnect()73     virtual void OnConnect() {}
OnForeground()74     virtual void OnForeground() {}
OnBackground()75     virtual void OnBackground() {}
OnDisconnect()76     virtual void OnDisconnect() {}
OnLayoutFinished()77     virtual void OnLayoutFinished() {}
OnDrawingCompleted()78     virtual void OnDrawingCompleted() {}
OnRemoveBlank()79     virtual void OnRemoveBlank() {}
OnAppRemoveStartingWindow()80     virtual void OnAppRemoveStartingWindow() {}
81 
82     RefPtr<FrameNode> startingWindow_;
83     RefPtr<FrameNode> appWindow_;
84     RefPtr<FrameNode> snapshotWindow_;
85     RefPtr<FrameNode> blankWindow_;
86     std::string startingWindowName_ = "StartingWindow";
87     std::string appWindowName_ = "AppWindow";
88     std::string snapshotWindowName_ = "SnapshotWindow";
89     std::string blankWindowName_ = "BlankWindow";
90     bool attachToFrameNodeFlag_ = false;
91 
92     sptr<Rosen::Session> session_;
93     int32_t instanceId_ = Container::CurrentId();
94     std::function<void()> callback_;
95     std::function<void(const Rosen::Vector4f&)> boundsChangedCallback_;
96 
97 private:
98     void UpdateSnapshotWindowProperty();
99     bool CheckAndAddStartingWindowAboveLocked();
100     void UpdateStartingWindowProperty(const Rosen::SessionInfo& sessionInfo,
101         Color &color, ImageSourceInfo &sourceInfo);
102 
103     std::shared_ptr<Rosen::ILifecycleListener> lifecycleListener_;
104 
105     friend class LifecycleListener;
106     friend class WindowEventProcess;
107 
108     ACE_DISALLOW_COPY_AND_MOVE(WindowPattern);
109 };
110 } // namespace OHOS::Ace::NG
111 
112 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_WINDOW_PATTERN_H
113