• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_COMPONENTS_V2_EXTENSIONS_EVENTS_ON_AREA_CHANGE_EXTENSION_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_V2_EXTENSIONS_EVENTS_ON_AREA_CHANGE_EXTENSION_H
18 
19 #include <functional>
20 #include <list>
21 
22 #include "base/geometry/offset.h"
23 #include "base/geometry/rect.h"
24 #include "core/components_v2/extensions/extension.h"
25 
26 namespace OHOS::Ace::V2 {
27 
28 using OnAreaChangeEvent = std::function<void(const Rect&, const Offset&, const Rect&, const Offset&)>;
29 
30 class OnAreaChangeExtension : public Extension {
31     DECLARE_ACE_TYPE(OnAreaChangeExtension, Extension);
32 
33 public:
34     OnAreaChangeExtension() = default;
35     ~OnAreaChangeExtension() override = default;
36 
AddOnAreaChangeEvent(const OnAreaChangeEvent & event)37     void AddOnAreaChangeEvent(const OnAreaChangeEvent& event)
38     {
39         if (!event) {
40             return;
41         }
42         callbacks_.emplace_back(event);
43     }
AddOnAreaChangeEvent(OnAreaChangeEvent && event)44     void AddOnAreaChangeEvent(OnAreaChangeEvent&& event)
45     {
46         if (!event) {
47             return;
48         }
49         callbacks_.emplace_back(std::move(event));
50     }
51 
SetBase(const Rect & rect,const Offset & origin)52     void SetBase(const Rect& rect, const Offset& origin)
53     {
54         rect_ = rect;
55         origin_ = origin;
56     }
57 
UpdateArea(const Rect & rect,const Offset & origin)58     void UpdateArea(const Rect& rect, const Offset& origin)
59     {
60         if ((rect_ == rect) && (origin_ == origin)) {
61             return;
62         }
63         for (const auto& callback : callbacks_) {
64             callback(rect_, origin_, rect, origin);
65         }
66         rect_ = rect;
67         origin_ = origin;
68     }
69 
70 private:
71     std::list<OnAreaChangeEvent> callbacks_;
72     Rect rect_;
73     Offset origin_;
74 };
75 
76 } // namespace OHOS::Ace::V2
77 
78 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_V2_EXTENSIONS_EVENTS_ON