1 /*
2  * Copyright (c) 2023 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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_TEXT_TEXT_EVENT_HUB_H
16 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_TEXT_TEXT_EVENT_HUB_H
17 
18 #include <cstdint>
19 #include <utility>
20 
21 #include "base/memory/ace_type.h"
22 #include "base/utils/noncopyable.h"
23 #include "core/components_ng/event/event_hub.h"
24 
25 namespace OHOS::Ace::NG {
26 class TextEventHub : public EventHub {
27     DECLARE_ACE_TYPE(TextEventHub, EventHub)
28 
29 public:
30     TextEventHub() = default;
31     ~TextEventHub() override = default;
32 
SetOnCopy(std::function<void (const std::string &)> && func)33     void SetOnCopy(std::function<void(const std::string&)>&& func)
34     {
35         onCopy_ = std::move(func);
36     }
37 
FireOnCopy(const std::string & value)38     void FireOnCopy(const std::string& value)
39     {
40         if (onCopy_) {
41             onCopy_(value);
42         }
43     }
44 
SetOnSelectionChange(std::function<void (int32_t,int32_t)> && func)45     void SetOnSelectionChange(std::function<void(int32_t, int32_t)>&& func)
46     {
47         onSelectionChange_ = std::move(func);
48     }
49 
FireOnSelectionChange(int32_t selectionStart,int32_t selectionEnd)50     void FireOnSelectionChange(int32_t selectionStart, int32_t selectionEnd)
51     {
52         if (onSelectionChange_) {
53             onSelectionChange_(selectionStart, selectionEnd);
54         }
55     }
56 
57 private:
58     std::function<void(const std::string&)> onCopy_;
59     std::function<void(int32_t, int32_t)> onSelectionChange_;
60     ACE_DISALLOW_COPY_AND_MOVE(TextEventHub);
61 };
62 } // namespace OHOS::Ace::NG
63 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_TEXT_TEXT_EVENT_HUB_H