• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 PC_FOLD_SCREEN_MANAGER_H
17 #define PC_FOLD_SCREEN_MANAGER_H
18 
19 #include <mutex>
20 #include <shared_mutex>
21 #include <tuple>
22 #include <unordered_map>
23 
24 #include <animation/rs_animation_timing_curve.h>
25 #include <animation/rs_animation_timing_protocol.h>
26 #include "dm_common.h"
27 #include "interfaces/include/ws_common.h"
28 #include "wm_single_instance.h"
29 
30 namespace OHOS::Rosen {
31 using FoldScreenStatusChangeCallback = std::function<void(DisplayId displayId,
32     SuperFoldStatus status, SuperFoldStatus prevStatus)>;
33 using SystemKeyboardStatusChangeCallback = std::function<void(DisplayId displayId, bool hasSystemKeyboard)>;
34 
35 enum class ScreenSide : uint8_t {
36     EXPAND = 0,
37     FOLD_B = 1,
38     FOLD_C = 2,
39 };
40 
41 enum class ThrowSlipWindowMode : int32_t {
42     FLOAT = 0,
43     FULLSCREEN = 1,
44     FULLSCREEN_WATERFALLMODE = 2,
45 };
46 
47 enum class ThrowSlipMode : int32_t {
48     INVALID = -1,
49     MOVE = 0,
50     BUTTON = 2,
51     THREE_FINGERS_SWIPE = 3,
52     FOUR_FINGERS_SWIPE = 4,
53     FIVE_FINGERS_SWIPE = 5,
54 };
55 
56 class PcFoldScreenManager {
57 WM_DECLARE_SINGLE_INSTANCE(PcFoldScreenManager);
58 public:
59     void UpdateFoldScreenStatus(DisplayId displayId, SuperFoldStatus status,
60         const WSRect& defaultDisplayRect, const WSRect& virtualDisplayRect, const WSRect& foldCreaseRect);
61     SuperFoldStatus GetScreenFoldStatus() const;
62     SuperFoldStatus GetScreenFoldStatus(DisplayId displayId) const;
63     bool IsHalfFolded(DisplayId displayId) const;
64     bool IsHalfFoldedOnMainDisplay(DisplayId displayId) const;
65     bool IsPcFoldScreen(DisplayId displayId) const;
66 
67     void UpdateSystemKeyboardStatus(bool hasSystemKeyboard);
68     bool HasSystemKeyboard() const;
69 
70     int32_t GetVirtualDisplayPosY() const;
71     std::tuple<WSRect, WSRect, WSRect> GetDisplayRects() const;
72 
73     // animation parameters
74     RSAnimationTimingProtocol GetMovingTimingProtocol();
75     RSAnimationTimingCurve GetMovingTimingCurve();
76     RSAnimationTimingProtocol GetThrowSlipTimingProtocol();
77     RSAnimationTimingCurve GetThrowSlipTimingCurve();
78 
79     ScreenSide CalculateScreenSide(const WSRect& rect);
80     ScreenSide CalculateScreenSide(int32_t posY);
81     bool IsCrossFoldCrease(const WSRect& rect);
82 
83     void ResetArrangeRule();
84     void ResetArrangeRule(ScreenSide side);
85     void ResetArrangeRule(const WSRect& rect);
86 
87     void ResizeToFullScreen(WSRect& rect, int32_t topAvoidHeight, int32_t botAvoidHeight);
88 
89     bool NeedDoThrowSlip(const WSRect& rect, const WSRectF& velocity, ScreenSide& throwSide);
90     bool NeedDoEasyThrowSlip(const WSRect& rect, const WSRect& startRect,
91         const WSRectF& velocity, ScreenSide& throwSide);
92     bool CheckVelocityOrientation(const WSRect& rect, const WSRectF& velocity);
93     WSRect CalculateThrowBacktracingRect(const WSRect& rect, const WSRectF& velocity);
94     WSRect CalculateThrowEnd(const WSRect& rect, const WSRectF& velocity);
95     bool ThrowSlipToOppositeSide(ScreenSide startSide, WSRect& rect,
96         int32_t topAvoidHeight, int32_t botAvoidHeight, int32_t titleHeight);
97     void MappingRectInScreenSide(ScreenSide side, WSRect& rect,
98         int32_t topAvoidHeight, int32_t botAvoidHeight);
99     void MappingRectInScreenSideWithArrangeRule(ScreenSide side, WSRect& rect,
100         int32_t topAvoidHeight, int32_t botAvoidHeight, int32_t titleHeight);
101 
102     void ApplyInitArrangeRule(WSRect& rect, WSRect& lastArrangedRect,
103         const WSRect& limitRect, int32_t titleHeight);
104     void ApplyArrangeRule(WSRect& rect, WSRect& lastArrangedRect,
105         const WSRect& limitRect, int32_t titleHeight);
106 
107     void RegisterFoldScreenStatusChangeCallback(int32_t persistentId,
108         const std::weak_ptr<FoldScreenStatusChangeCallback>& func);
109     void UnregisterFoldScreenStatusChangeCallback(int32_t persistentId);
110     void RegisterSystemKeyboardStatusChangeCallback(int32_t persistentId,
111         const std::weak_ptr<SystemKeyboardStatusChangeCallback>& func);
112     void UnregisterSystemKeyboardStatusChangeCallback(int32_t persistentId);
GetDisplayId()113     DisplayId GetDisplayId() const { return displayId_; }
114 
115 private:
116     void SetDisplayInfo(DisplayId displayId, SuperFoldStatus status);
117     void SetDisplayRects(
118         const WSRect& defaultDisplayRect, const WSRect& virtualDisplayRect, const WSRect& foldCreaseRect);
119     float GetVpr() const;
120 
121     /*
122      * fold screen property
123      * if need, use map for multi fold screen
124      */
125     mutable std::shared_mutex displayInfoMutex_; // protect display infos
126     DisplayId displayId_ { SCREEN_ID_INVALID };
127     float vpr_ { 1.5f }; // display vp ratio
128     SuperFoldStatus prevScreenFoldStatus_ { SuperFoldStatus::UNKNOWN };
129     SuperFoldStatus screenFoldStatus_ { SuperFoldStatus::UNKNOWN };
130     bool hasSystemKeyboard_ { false }; // bottom system keyboard
131     // Above guarded by displayInfoMutex_
132 
133     mutable std::shared_mutex rectsMutex_; // protect rects
134     WSRect defaultDisplayRect_;
135     WSRect virtualDisplayRect_;
136     WSRect foldCreaseRect_;
137     // Above guarded by rectsMutex_
138 
139     /*
140      * arranged rect
141      * x,y: last arranged position
142      * w,h: offset for next arranged position
143      */
144     std::mutex arrangedRectsMutex_;
145     WSRect defaultArrangedRect_;
146     WSRect virtualArrangedRect_;
147     // Above guarded by arrangedRectsMutex_
148 
149     void ExecuteFoldScreenStatusChangeCallbacks(DisplayId displayId,
150         SuperFoldStatus status, SuperFoldStatus prevStatus);
151     void ExecuteSystemKeyboardStatusChangeCallbacks(DisplayId displayId, bool hasSystemKeyboard);
152     std::mutex callbackMutex_;
153     std::unordered_map<int32_t, std::weak_ptr<FoldScreenStatusChangeCallback>> foldScreenStatusChangeCallbacks_;
154     std::unordered_map<int32_t, std::weak_ptr<SystemKeyboardStatusChangeCallback>>
155         systemKeyboardStatusChangeCallbacks_;
156 };
157 } // namespace OHOS::Rosen
158 
159 #endif // PC_FOLD_SCREEN_MANAGER_H
160