• 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 OHOS_ROSEN_WINDOW_SCENE_KEYBOARD_SESSION_H
17 #define OHOS_ROSEN_WINDOW_SCENE_KEYBOARD_SESSION_H
18 
19 #include "session/host/include/system_session.h"
20 #include "transaction/rs_sync_transaction_controller.h"
21 
22 namespace OHOS::Rosen {
23 enum class SystemKeyboardAvoidChangeReason : uint32_t {
24     KEYBOARD_BEGIN,
25     KEYBOARD_CREATED,
26     KEYBOARD_SHOW,
27     KEYBOARD_HIDE,
28     KEYBOARD_DISCONNECT,
29     KEYBOARD_GRAVITY_BOTTOM,
30     KEYBOARD_GRAVITY_FLOAT,
31     KEYBOARD_END,
32 };
33 
34 using OnGetSceneSessionCallback = std::function<sptr<SceneSession>(uint32_t callingSessionId)>;
35 using OnGetFocusedSessionIdCallback = std::function<int32_t()>;
36 using OnCallingSessionIdChangeCallback = std::function<void(uint32_t callingSessionId)>;
37 using OnSystemKeyboardAvoidChangeCallback = std::function<void(DisplayId displayId,
38     SystemKeyboardAvoidChangeReason reason)>;
39 using NotifyOccupiedAreaChangeCallback = std::function<void(const sptr<OccupiedAreaChangeInfo>& info)>;
40 using IsLastFrameLayoutFinishedCallback = std::function<bool()>;
41 
42 const std::string KEYBOARD_ANIM_SYNC_EVENT_NAME { "KeyboardAnimationSyncException" };
43 
44 class KeyboardSession : public SystemSession {
45 public:
46     // callback for notify SceneSessionManager
47     struct KeyboardSessionCallback : public RefBase {
48         OnGetSceneSessionCallback onGetSceneSession;
49         OnGetFocusedSessionIdCallback onGetFocusedSessionId;
50         OnCallingSessionIdChangeCallback onCallingSessionIdChange;
51         OnSystemKeyboardAvoidChangeCallback onSystemKeyboardAvoidChange;
52         NotifyOccupiedAreaChangeCallback onNotifyOccupiedAreaChange;
53         IsLastFrameLayoutFinishedCallback isLastFrameLayoutFinished;
54     };
55     KeyboardSession(const SessionInfo& info, const sptr<SpecificSessionCallback>& specificCallback,
56         const sptr<KeyboardSessionCallback>& keyboardCallback);
57     ~KeyboardSession();
58 
59     WSError Show(sptr<WindowSessionProperty> property) override;
60     WSError Hide() override;
61     WSError Disconnect(bool isFromClient = false, const std::string& identityToken = "") override;
62     WSError NotifyClientToUpdateRect(const std::string& updateReason,
63         std::shared_ptr<RSTransaction> rsTransaction) override;
64 
65     void BindKeyboardPanelSession(sptr<SceneSession> panelSession) override;
66     sptr<SceneSession> GetKeyboardPanelSession() const override;
67     SessionGravity GetKeyboardGravity() const override;
68     void OpenKeyboardSyncTransaction() override;
69     void CloseKeyboardSyncTransaction(const WSRect& keyboardPanelRect, bool isKeyboardShow,
70         const WindowAnimationInfo& animationInfo) override;
71     bool IsVisibleForeground() const override;
72     bool IsVisibleNotBackground() const override;
73     uint32_t GetCallingSessionId() override;
74     void RecalculatePanelRectForAvoidArea(WSRect& panelRect) override;
75     WSError ChangeKeyboardEffectOption(const KeyboardEffectOption& effectOption) override;
76     void SetKeyboardEffectOptionChangeListener(const NotifyKeyboarEffectOptionChangeFunc& func) override;
77     void SetSkipSelfWhenShowOnVirtualScreen(bool isSkip) override;
78     WSError UpdateSizeChangeReason(SizeChangeReason reason) override;
GetIsKeyboardSyncTransactionOpen()79     bool GetIsKeyboardSyncTransactionOpen() const { return isKeyboardSyncTransactionOpen_; }
80     void SetSkipEventOnCastPlus(bool isSkip) override;
81 
82 protected:
83     void EnableCallingSessionAvoidArea() override;
84     void NotifySessionRectChange(const WSRect& rect,
85         SizeChangeReason reason = SizeChangeReason::UNDEFINED,
86         DisplayId displayId = DISPLAY_ID_INVALID,
87         const RectAnimationConfig& rectAnimationConfig = {}) override;
88     void RestoreCallingSession(uint32_t callingId, const std::shared_ptr<RSTransaction>& rsTransaction) override;
89 
90 private:
91     sptr<SceneSession> GetSceneSession(uint32_t persistentId);
92     int32_t GetFocusedSessionId();
93     WSRect GetPanelRect() const;
94 
95     void SetCallingSessionId(uint32_t callingSessionId) override;
96     void UseFocusIdIfCallingSessionIdInvalid();
97     void NotifyKeyboardPanelInfoChange(WSRect rect, bool isKeyboardPanelShow);
98     bool CheckIfNeedRaiseCallingSession(sptr<SceneSession> callingSession, bool isCallingSessionFloating);
99     WSError AdjustKeyboardLayout(const KeyboardLayoutParams& params) override;
100     std::shared_ptr<RSTransaction> GetRSTransaction();
101     std::string GetSessionScreenName();
102     void NotifySystemKeyboardAvoidChange(SystemKeyboardAvoidChangeReason reason);
103     void NotifyRootSceneOccupiedAreaChange(const sptr<OccupiedAreaChangeInfo>& info);
104     void HandleCrossScreenChild(bool isMoveOrDrag);
105     void HandleMoveDragSurfaceNode(SizeChangeReason reason) override;
106     void SetSurfaceBounds(const WSRect& rect, bool isGlobal, bool needFlush = true) override;
107     bool IsNeedRaiseSubWindow(const sptr<SceneSession>& callingSession, const WSRect& callingSessionRect);
108     void PostKeyboardAnimationSyncTimeoutTask();
109     void ProcessKeyboardOccupiedAreaInfo(uint32_t callingId, bool needRecalculateAvoidAreas,
110         bool needCheckRSTransaction);
111     void NotifyOccupiedAreaChanged(const sptr<SceneSession>& callingSession,
112         sptr<OccupiedAreaChangeInfo>& occupiedAreaInfo, bool needRecalculateAvoidAreas,
113         std::shared_ptr<RSTransaction> rsTransaction);
114     bool RaiseCallingSession(const sptr<SceneSession>& callingSession, sptr<OccupiedAreaChangeInfo>& occupiedAreaInfo);
115     bool CalculateOccupiedArea(const sptr<SceneSession>& callingSession, const WSRect& callingSessionRect,
116         const WSRect& panelRect, sptr<OccupiedAreaChangeInfo>& occupiedAreaInfo);
117     void CloseRSTransaction();
118     bool stateChanged_ = false;
119     void CalculateOccupiedAreaAfterUIRefresh() override;
120     WSRect CalculateScaledRect(WSRect sessionRect, float scaleX, float scaleY);
121 
122     sptr<KeyboardSessionCallback> keyboardCallback_ = nullptr;
123     bool isKeyboardSyncTransactionOpen_ = false;
124     NotifyKeyboarEffectOptionChangeFunc changeKeyboardEffectOptionFunc_;
125 };
126 } // namespace OHOS::Rosen
127 #endif // OHOS_ROSEN_WINDOW_SCENE_KEYBOARD_SESSION_H