• 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 
41 class KeyboardSession : public SystemSession {
42 public:
43     // callback for notify SceneSessionManager
44     struct KeyboardSessionCallback : public RefBase {
45         OnGetSceneSessionCallback onGetSceneSession;
46         OnGetFocusedSessionIdCallback onGetFocusedSessionId;
47         OnCallingSessionIdChangeCallback onCallingSessionIdChange;
48         OnSystemKeyboardAvoidChangeCallback onSystemKeyboardAvoidChange;
49         NotifyOccupiedAreaChangeCallback onNotifyOccupiedAreaChange;
50     };
51     KeyboardSession(const SessionInfo& info, const sptr<SpecificSessionCallback>& specificCallback,
52         const sptr<KeyboardSessionCallback>& keyboardCallback);
53     ~KeyboardSession();
54 
55     WSError Show(sptr<WindowSessionProperty> property) override;
56     WSError Hide() override;
57     WSError Disconnect(bool isFromClient = false, const std::string& identityToken = "") override;
58     WSError NotifyClientToUpdateRect(const std::string& updateReason,
59         std::shared_ptr<RSTransaction> rsTransaction) override;
60 
61     void BindKeyboardPanelSession(sptr<SceneSession> panelSession) override;
62     sptr<SceneSession> GetKeyboardPanelSession() const override;
63     SessionGravity GetKeyboardGravity() const override;
64     void OnKeyboardPanelUpdated() override;
65     void OnCallingSessionUpdated() override;
66     void OpenKeyboardSyncTransaction() override;
67     void CloseKeyboardSyncTransaction(const WSRect& keyboardPanelRect, bool isKeyboardShow, bool isRotating) override;
68     bool IsVisibleForeground() const override;
69     uint32_t GetCallingSessionId() override;
70     void RecalculatePanelRectForAvoidArea(WSRect& panelRect) override;
71     WSError ChangeKeyboardViewMode(KeyboardViewMode mode) override;
72     void SetKeyboardViewModeChangeListener(const NotifyKeyboarViewModeChangeFunc& func) override;
73 
74 protected:
75     void EnableCallingSessionAvoidArea() override;
76     void RestoreCallingSession(const std::shared_ptr<RSTransaction>& rsTransaction = nullptr) override;
77 
78 private:
79     sptr<SceneSession> GetSceneSession(uint32_t persistentId);
80     int32_t GetFocusedSessionId();
81     WSRect GetPanelRect() const;
82 
83     void SetCallingSessionId(uint32_t callingSessionId) override;
84 
85     void NotifyOccupiedAreaChangeInfo(const sptr<SceneSession>& callingSession, const WSRect& rect,
86         const WSRect& occupiedArea, const std::shared_ptr<RSTransaction>& rsTransaction = nullptr);
87     void RaiseCallingSession(const WSRect& keyboardPanelRect, bool needCheckVisible,
88         const std::shared_ptr<RSTransaction>& rsTransaction = nullptr);
89     void UpdateKeyboardAvoidArea();
90     void UseFocusIdIfCallingSessionIdInvalid();
91     void UpdateCallingSessionIdAndPosition(uint32_t newCallingSessionId);
92     void NotifyKeyboardPanelInfoChange(WSRect rect, bool isKeyboardPanelShow);
93     bool CheckIfNeedRaiseCallingSession(sptr<SceneSession> callingSession, bool isCallingSessionFloating);
94     WSError AdjustKeyboardLayout(const KeyboardLayoutParams& params) override;
95     std::shared_ptr<RSTransaction> GetRSTransaction();
96     std::string GetSessionScreenName();
97     void MoveAndResizeKeyboard(const KeyboardLayoutParams& params, const sptr<WindowSessionProperty>& sessionProperty,
98         bool isShow);
99     void NotifySystemKeyboardAvoidChange(SystemKeyboardAvoidChangeReason reason);
100     void NotifyRootSceneOccupiedAreaChange(const sptr<OccupiedAreaChangeInfo>& info);
101 
102     sptr<KeyboardSessionCallback> keyboardCallback_ = nullptr;
103     bool isKeyboardSyncTransactionOpen_ = false;
104     NotifyKeyboarViewModeChangeFunc changeKeyboardViewModeFunc_;
105 };
106 } // namespace OHOS::Rosen
107 #endif // OHOS_ROSEN_WINDOW_SCENE_KEYBOARD_SESSION_H