1 /*
2  * Copyright (c) 2021 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_COMMON_IME_TEXT_INPUT_CONNECTION_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_IME_TEXT_INPUT_CONNECTION_H
18 
19 #include <atomic>
20 
21 #include "base/memory/ace_type.h"
22 #include "base/thread/task_executor.h"
23 #include "core/common/ime/constant.h"
24 #include "core/common/ime/text_editing_value.h"
25 #include "core/common/ime/text_input_client.h"
26 
27 namespace OHOS::Ace {
28 
29 class TextInputConnection : public AceType {
30     DECLARE_ACE_TYPE(TextInputConnection, AceType);
31 
32 public:
33     TextInputConnection() = delete;
TextInputConnection(const WeakPtr<TextInputClient> & client,const RefPtr<TaskExecutor> & taskExecutor)34     TextInputConnection(const WeakPtr<TextInputClient>& client, const RefPtr<TaskExecutor>& taskExecutor)
35         : clientId_(id_++), client_(client), taskExecutor_(taskExecutor)
36     {}
37 
38     ~TextInputConnection() override = default;
39 
40     virtual void Show(bool isFocusViewChanged, int32_t instanceId) = 0;
41     virtual void SetEditingState(
42         const TextEditingValue& value, int32_t instanceId, bool needFireChangeEvent = true) = 0;
43     virtual void Close(int32_t instanceId) = 0;
44 
GetClientId()45     int32_t GetClientId() const
46     {
47         return clientId_;
48     }
49 
GetClient()50     RefPtr<TextInputClient> GetClient() const
51     {
52         return client_.Upgrade();
53     }
54 
GetTaskExecutor()55     RefPtr<TaskExecutor> GetTaskExecutor() const
56     {
57         return taskExecutor_;
58     }
59 
60 protected:
61     // May be incremented on multi Ace UI threads.
62     static std::atomic_int32_t id_;
63 
64     int32_t clientId_;
65     WeakPtr<TextInputClient> client_;
66     RefPtr<TaskExecutor> taskExecutor_;
67 };
68 
69 } // namespace OHOS::Ace
70 
71 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_IME_TEXT_INPUT_CONNECTION_H
72