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
16 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_BASE_CUSTOM_TITLE_NODE_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_BASE_CUSTOM_TITLE_NODE_H
18
19 #include "core/components_ng/pattern/custom/custom_node.h"
20
21 namespace OHOS::Ace::NG {
22
23 const std::string EVENT_NAME_COLOR_CONFIGURATION = "arkui_color_configuration";
24 const std::string EVENT_NAME_HIDE_SPLIT = "arkui_hide_split";
25 const std::string EVENT_NAME_MAXIMIZE_VISIBILITY = "arkui_maximize_visibility";
26 const std::string EVENT_NAME_MINIMIZE_VISIBILITY = "arkui_minimize_visibility";
27 const std::string EVENT_NAME_CLOSE_VISIBILITY = "arkui_close_visibility";
28 const std::string EVENT_NAME_CLOSE_STATUS = "arkui_close_status";
29 const std::string EVENT_NAME_MAXIMIZE_IS_RECOVER = "arkui_maximize_is_recover";
30 const std::string EVENT_NAME_MENU_WIDTH_CHANGE = "arkui_menu_width_change";
31 const std::string EVENT_NAME_BUTTON_SPACING_CHANGE = "arkui_button_spacing_change";
32 const std::string EVENT_NAME_COLOR_CONFIGURATION_LOCKED = "arkui_color_mode_locked";
33 const std::string EVENT_NAME_BUTTON_SIZE_CHANGE = "arkui_button_size_change";
34 const std::string EVENT_NAME_BUTTON_RIGHT_OFFSET_CHANGE = "arkui_button_right_offset_change";
35
36 namespace {
BoolToString(bool value)37 inline std::string BoolToString(bool value)
38 {
39 return value ? "true" : "false";
40 }
41 } // namespace
42
43 class ACE_EXPORT CustomTitleNode : public CustomNode {
44 DECLARE_ACE_TYPE(CustomTitleNode, CustomNode);
45
46 public:
47 static RefPtr<CustomTitleNode> CreateCustomTitleNode(int32_t nodeId, const std::string& viewKey);
48
49 CustomTitleNode(int32_t nodeId, const std::string& viewKey);
50 ~CustomTitleNode() override = default;
51
SetAppTitleCallback(const std::function<void (const std::string &)> & callback)52 void SetAppTitleCallback(const std::function<void(const std::string&)>& callback)
53 {
54 appTitleCallback_ = callback;
55 }
56
FireAppTitleCallback(const std::string & title)57 void FireAppTitleCallback(const std::string& title)
58 {
59 if (appTitleCallback_) {
60 appTitleCallback_(title);
61 }
62 }
63
SetAppIconCallback(const std::function<void (const RefPtr<PixelMap> &)> & callback)64 void SetAppIconCallback(const std::function<void(const RefPtr<PixelMap>&)>& callback)
65 {
66 appIconCallback_ = callback;
67 }
68
FireAppIconCallback(const RefPtr<PixelMap> & icon)69 void FireAppIconCallback(const RefPtr<PixelMap>& icon)
70 {
71 if (appIconCallback_) {
72 appIconCallback_(icon);
73 }
74 }
75
SetOnWindowFocusedCallback(const std::function<void ()> & callback)76 void SetOnWindowFocusedCallback(const std::function<void()>& callback)
77 {
78 onWindowFocusedCallback_ = callback;
79 }
80
FireOnWindowFocusedCallback()81 void FireOnWindowFocusedCallback()
82 {
83 if (onWindowFocusedCallback_) {
84 onWindowFocusedCallback_();
85 }
86 }
87
SetOnWindowUnfocusedCallback(const std::function<void ()> & callback)88 void SetOnWindowUnfocusedCallback(const std::function<void()>& callback)
89 {
90 onWindowUnfocusedCallback_ = callback;
91 }
92
FireOnWindowUnfocusedCallback()93 void FireOnWindowUnfocusedCallback()
94 {
95 if (onWindowUnfocusedCallback_) {
96 onWindowUnfocusedCallback_();
97 }
98 }
99
SetCustomCallback(const std::function<void (const std::string & eventName,const std::string & param)> & callback)100 void SetCustomCallback(const std::function<void(const std::string& eventName, const std::string& param)>& callback)
101 {
102 customCallback_ = callback;
103 }
104
FireCustomCallback(const std::string & eventName,const std::string & param)105 void FireCustomCallback(const std::string& eventName, const std::string& param)
106 {
107 if (customCallback_) {
108 customCallback_(eventName, param);
109 }
110 }
111
FireCustomCallback(const std::string & eventName,bool value)112 void FireCustomCallback(const std::string& eventName, bool value)
113 {
114 if (customCallback_) {
115 customCallback_(eventName, BoolToString(value));
116 }
117 }
118
119 private:
120 std::function<void(const std::string&)> appTitleCallback_ = nullptr;
121 std::function<void(const RefPtr<PixelMap>&)> appIconCallback_ = nullptr;
122 std::function<void()> onWindowFocusedCallback_ = nullptr;
123 std::function<void()> onWindowUnfocusedCallback_ = nullptr;
124
125 std::function<void(const std::string&, const std::string&)> customCallback_ = nullptr;
126 };
127 } // namespace OHOS::Ace::NG
128 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_BASE_CUSTOM_NODE_H