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 #include "core/components/navigation_bar/navigation_bar_element.h"
17
18 #include "core/event/ace_event_helper.h"
19 #include "core/gestures/click_recognizer.h"
20
21 namespace OHOS::Ace {
22 namespace {
23
24 #ifndef WEARABLE_PRODUCT
25 constexpr double MENU_DEFAULT_HEIGHT = 56.0;
26 #endif
27
28 } // namespace
29
PerformBuild()30 void NavigationBarElement::PerformBuild()
31 {
32 RefPtr<NavigationBarComponent> navigationBar = AceType::DynamicCast<NavigationBarComponent>(component_);
33 if (navigationBar) {
34 const auto& child = children_.empty() ? nullptr : children_.front();
35 auto newBar = navigationBar->Build(context_);
36 #ifndef WEARABLE_PRODUCT
37 BindMoreButtonClickEvent(navigationBar);
38 BindClickEventToOptions(navigationBar);
39 #endif
40 UpdateChild(child, newBar);
41 }
42 }
43
44 #ifndef WEARABLE_PRODUCT
BindMoreButtonClickEvent(const RefPtr<NavigationBarComponent> & navigationBar)45 void NavigationBarElement::BindMoreButtonClickEvent(const RefPtr<NavigationBarComponent>& navigationBar)
46 {
47 auto moreButton = navigationBar->GetMoreButton();
48 if (!moreButton) {
49 return;
50 }
51 auto weak = AceType::WeakClaim(this);
52 moreClickMarker_ = BackEndEventManager<void()>::GetInstance().GetAvailableMarker();
53 BackEndEventManager<void()>::GetInstance().BindBackendEvent(
54 moreClickMarker_, [weak = WeakClaim(this), navigationBar]() {
55 LOGD("click collapse icon");
56 auto menu = navigationBar->GetMenu();
57 if (!menu) {
58 LOGW("navigation bar not menu");
59 return;
60 }
61 auto showMenuFunc = menu->GetTargetCallback();
62 if (!showMenuFunc) {
63 return;
64 }
65 auto element = weak.Upgrade();
66 if (!element) {
67 LOGW("weak ptr to ref ptr fail");
68 return;
69 }
70 auto context = element->GetContext().Upgrade();
71 if (!context) {
72 LOGW("context is empty");
73 return;
74 }
75 double morePopupOffsetX = 0.0;
76 if (navigationBar->GetTextDirection() == TextDirection::LTR) {
77 morePopupOffsetX = context->GetStageRect().Width();
78 }
79 showMenuFunc(navigationBar->GetId(),
80 Offset(morePopupOffsetX,
81 context->NormalizeToPx(Dimension(MENU_DEFAULT_HEIGHT, DimensionUnit::VP))));
82 });
83 moreButton->SetClickedEventId(moreClickMarker_);
84 }
85
BindClickEventToOptions(const RefPtr<NavigationBarComponent> & navigationBar)86 void NavigationBarElement::BindClickEventToOptions(const RefPtr<NavigationBarComponent>& navigationBar)
87 {
88 auto menu = navigationBar->GetMenu();
89 if (!menu) {
90 LOGI("navigation bar not menu");
91 return;
92 }
93
94 const auto& menuSuccessEvent = menu->GetOnSuccess();
95 if (menuSuccessEvent.IsEmpty()) {
96 LOGI("navigation bar menu not listen success event");
97 return;
98 }
99
100 menuSelectedEvent_ = AceAsyncEvent<void(const std::string&)>::Create(menuSuccessEvent, context_);
101 for (const auto& menuItem : navigationBar->GetMenuItemsInBar()) {
102 menuItem.second.button->SetClickedEventId(menuItem.second.clickEvent);
103 std::string eventId = menuItem.second.clickEvent.GetData().eventId;
104
105 auto weak = AceType::WeakClaim(this);
106 BackEndEventManager<void()>::GetInstance().BindBackendEvent(
107 menuItem.second.clickEvent, [navigationBar, eventId, weak]() {
108 LOGD("click menuItem %{public}s in bar", eventId.c_str());
109 auto navigationBarElement = weak.Upgrade();
110 if (!navigationBarElement) {
111 LOGE("get navigation bar element failed");
112 return;
113 }
114
115 auto menuItemsInBar = navigationBar->GetMenuItemsInBar();
116 auto menuItemIter = menuItemsInBar.find(eventId);
117 if (menuItemIter == menuItemsInBar.end()) {
118 LOGE("not find which item");
119 return;
120 }
121 std::string selected = menuItemIter->second.value;
122 std::string param = std::string(R"("selected",{"value":")").append(selected.append("\"},null"));
123 navigationBarElement->menuSelectedEvent_(param);
124 });
125 }
126 }
127 #endif
128
129 } // namespace OHOS::Ace
130