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 MOVE = 0, 49 GESTURE = 1, 50 BUTTON = 2, 51 }; 52 53 class PcFoldScreenManager { 54 WM_DECLARE_SINGLE_INSTANCE(PcFoldScreenManager); 55 public: 56 void UpdateFoldScreenStatus(DisplayId displayId, SuperFoldStatus status, 57 const WSRect& defaultDisplayRect, const WSRect& virtualDisplayRect, const WSRect& foldCreaseRect); 58 SuperFoldStatus GetScreenFoldStatus() const; 59 SuperFoldStatus GetScreenFoldStatus(DisplayId displayId) const; 60 bool IsHalfFolded(DisplayId displayId) const; 61 bool IsHalfFoldedOnMainDisplay(DisplayId displayId) const; 62 bool IsPcFoldScreen(DisplayId displayId) const; 63 64 void UpdateSystemKeyboardStatus(bool hasSystemKeyboard); 65 bool HasSystemKeyboard() const; 66 67 std::tuple<WSRect, WSRect, WSRect> GetDisplayRects() const; 68 69 // animation parameters 70 RSAnimationTimingProtocol GetMovingTimingProtocol(); 71 RSAnimationTimingCurve GetMovingTimingCurve(); 72 RSAnimationTimingProtocol GetThrowSlipTimingProtocol(); 73 RSAnimationTimingCurve GetThrowSlipTimingCurve(); 74 75 ScreenSide CalculateScreenSide(const WSRect& rect); 76 bool IsCrossFoldCrease(const WSRect& rect); 77 78 void ResetArrangeRule(); 79 void ResetArrangeRule(ScreenSide side); 80 void ResetArrangeRule(const WSRect& rect); 81 82 void ResizeToFullScreen(WSRect& rect, int32_t topAvoidHeight, int32_t botAvoidHeight); 83 84 bool NeedDoThrowSlip(const WSRect& rect, const WSRectF& velocity, ScreenSide& throwSide); 85 bool NeedDoEasyThrowSlip(const WSRect& rect, const WSRect& startRect, 86 const WSRectF& velocity, ScreenSide& throwSide); 87 bool CheckVelocityOrientation(const WSRect& rect, const WSRectF& velocity); 88 WSRect CalculateThrowBacktracingRect(const WSRect& rect, const WSRectF& velocity); 89 WSRect CalculateThrowEnd(const WSRect& rect, const WSRectF& velocity); 90 bool ThrowSlipToOppositeSide(ScreenSide startSide, WSRect& rect, 91 int32_t topAvoidHeight, int32_t botAvoidHeight, int32_t titleHeight); 92 void MappingRectInScreenSide(ScreenSide side, WSRect& rect, 93 int32_t topAvoidHeight, int32_t botAvoidHeight); 94 void MappingRectInScreenSideWithArrangeRule(ScreenSide side, WSRect& rect, 95 int32_t topAvoidHeight, int32_t botAvoidHeight, int32_t titleHeight); 96 97 void ApplyInitArrangeRule(WSRect& rect, WSRect& lastArrangedRect, 98 const WSRect& limitRect, int32_t titleHeight); 99 void ApplyArrangeRule(WSRect& rect, WSRect& lastArrangedRect, 100 const WSRect& limitRect, int32_t titleHeight); 101 102 void RegisterFoldScreenStatusChangeCallback(int32_t persistentId, 103 const std::weak_ptr<FoldScreenStatusChangeCallback>& func); 104 void UnregisterFoldScreenStatusChangeCallback(int32_t persistentId); 105 void RegisterSystemKeyboardStatusChangeCallback(int32_t persistentId, 106 const std::weak_ptr<SystemKeyboardStatusChangeCallback>& func); 107 void UnregisterSystemKeyboardStatusChangeCallback(int32_t persistentId); GetDisplayId()108 DisplayId GetDisplayId() const { return displayId_; } 109 110 private: 111 void SetDisplayInfo(DisplayId displayId, SuperFoldStatus status); 112 void SetDisplayRects( 113 const WSRect& defaultDisplayRect, const WSRect& virtualDisplayRect, const WSRect& foldCreaseRect); 114 float GetVpr() const; 115 116 /* 117 * fold screen property 118 * if need, use map for multi fold screen 119 */ 120 mutable std::shared_mutex displayInfoMutex_; // protect display infos 121 DisplayId displayId_ { SCREEN_ID_INVALID }; 122 float vpr_ { 1.5f }; // display vp ratio 123 SuperFoldStatus prevScreenFoldStatus_ { SuperFoldStatus::UNKNOWN }; 124 SuperFoldStatus screenFoldStatus_ { SuperFoldStatus::UNKNOWN }; 125 bool hasSystemKeyboard_ { false }; // bottom system keyboard 126 // Above guarded by displayInfoMutex_ 127 128 mutable std::shared_mutex rectsMutex_; // protect rects 129 WSRect defaultDisplayRect_; 130 WSRect virtualDisplayRect_; 131 WSRect foldCreaseRect_; 132 // Above guarded by rectsMutex_ 133 134 /* 135 * arranged rect 136 * x,y: last arranged position 137 * w,h: offset for next arranged position 138 */ 139 std::mutex arrangedRectsMutex_; 140 WSRect defaultArrangedRect_; 141 WSRect virtualArrangedRect_; 142 // Above guarded by arrangedRectsMutex_ 143 144 void ExecuteFoldScreenStatusChangeCallbacks(DisplayId displayId, 145 SuperFoldStatus status, SuperFoldStatus prevStatus); 146 void ExecuteSystemKeyboardStatusChangeCallbacks(DisplayId displayId, bool hasSystemKeyboard); 147 std::mutex callbackMutex_; 148 std::unordered_map<int32_t, std::weak_ptr<FoldScreenStatusChangeCallback>> foldScreenStatusChangeCallbacks_; 149 std::unordered_map<int32_t, std::weak_ptr<SystemKeyboardStatusChangeCallback>> 150 systemKeyboardStatusChangeCallbacks_; 151 }; 152 } // namespace OHOS::Rosen 153 154 #endif // PC_FOLD_SCREEN_MANAGER_H 155