1 /* 2 * Copyright (c) 2024 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_EVENT_FOCUS_HANDLER_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_EVENT_FOCUS_HANDLER_H 18 19 #include "focus_state.h" 20 #include "core/event/focus_axis_event.h" 21 #include "core/event/crown_event.h" 22 #include "core/event/key_event.h" 23 #include "core/gestures/gesture_event.h" 24 namespace OHOS::Ace::NG { 25 #define ACE_DEFINE_FOCUS_EVENT(func, type, name) \ 26 public: \ 27 void Set##func(type&& (name)) \ 28 { \ 29 if (!focusCallbackEvents_) { \ 30 focusCallbackEvents_ = MakeRefPtr<FocusCallbackEvents>(); \ 31 } \ 32 focusCallbackEvents_->name##_ = std::move(name); \ 33 } \ 34 void Clear##func() \ 35 { \ 36 if (focusCallbackEvents_ && focusCallbackEvents_->name##_) { \ 37 focusCallbackEvents_->name##_ = nullptr; \ 38 } \ 39 } \ 40 type Get##func() \ 41 { \ 42 return focusCallbackEvents_ ? focusCallbackEvents_->name##_ : nullptr; \ 43 } 44 45 enum class FocusIntension : int32_t { 46 NONE = 0, 47 LEFT, 48 UP, 49 RIGHT, 50 DOWN, 51 TAB, 52 SHIFT_TAB, 53 HOME, 54 END, 55 SELECT, 56 SPACE, 57 ESC, 58 }; 59 60 struct FocusEvent { FocusEventFocusEvent61 explicit FocusEvent(const NonPointerEvent& nonPointerEvent) 62 : intension(GetFocusIntension(nonPointerEvent)), event(nonPointerEvent) 63 {} 64 65 FocusIntension intension; 66 const NonPointerEvent& event; 67 static FocusIntension GetFocusIntension(const NonPointerEvent& event); 68 static FocusIntension GetFocusIntensionFromKey(KeyIntention keyIntention); 69 }; 70 71 class ACE_EXPORT FocusCallbackEvents : public virtual AceType { 72 DECLARE_ACE_TYPE(FocusCallbackEvents, AceType) 73 public: 74 FocusCallbackEvents() = default; 75 ~FocusCallbackEvents() override = default; 76 77 OnFocusFunc onFocusCallback_; 78 OnFocusFunc onJSFrameNodeFocusCallback_; 79 OnBlurFunc onBlurCallback_; 80 OnBlurFunc onJSFrameNodeBlurCallback_; 81 OnKeyConsumeFunc onKeyEventCallback_; 82 OnKeyCallbackFunc onJSFrameNodeKeyEventCallback_; 83 OnKeyConsumeFunc onKeyPreImeCallback_; 84 GestureEventFunc onClickEventCallback_; 85 OnFocusAxisEventFunc onFocusAxisEventCallback_; 86 OnKeyEventDispatchFunc onKeyEventDispatchCallback_; 87 OnCrownCallbackFunc onCrownEventCallback_; 88 OnCrownEventFunc onCrownEventsInternal_; 89 90 WeakPtr<FocusHub> defaultFocusNode_; 91 bool isDefaultFocus_ = { false }; 92 bool isDefaultHasFocused_ = { false }; 93 bool isDefaultGroupFocus_ = { false }; 94 bool isDefaultGroupHasFocused_ { false }; 95 std::optional<bool> isFocusOnTouch_; 96 97 int32_t tabIndex_ = 0; 98 }; 99 100 enum class OnKeyEventType : int32_t { 101 DEFAULT = 0, 102 CONTEXT_MENU = 1, 103 }; 104 105 class FocusEventHandler : public virtual FocusState { 106 DECLARE_ACE_TYPE(FocusEventHandler, FocusState) 107 public: 108 FocusEventHandler() = default; 109 ~FocusEventHandler() override = default; 110 111 void SetOnKeyEventInternal(OnKeyEventFunc&& onKeyEvent, OnKeyEventType type = OnKeyEventType::DEFAULT) 112 { 113 onKeyEventsInternal_[type] = std::move(onKeyEvent); 114 } FindContextMenuOnKeyEvent(OnKeyEventType type)115 bool FindContextMenuOnKeyEvent(OnKeyEventType type) 116 { 117 return (onKeyEventsInternal_.find(type) != onKeyEventsInternal_.end()); 118 } 119 bool OnClick(const KeyEvent& event); 120 121 bool ProcessOnCrownEventInternal(const CrownEvent& event); 122 123 protected: 124 bool OnCrownEvent(const CrownEvent& CrownEvent); 125 bool OnFocusEvent(const FocusEvent& event); 126 virtual bool HandleFocusTravel(const FocusEvent& event) = 0; // bad design which need to be deleted 127 int32_t GetKeyProcessingMode(); 128 129 ACE_DEFINE_FOCUS_EVENT(OnFocusCallback, OnFocusFunc, onFocusCallback) 130 ACE_DEFINE_FOCUS_EVENT(OnBlurCallback, OnBlurFunc, onBlurCallback) 131 ACE_DEFINE_FOCUS_EVENT(JSFrameNodeOnFocusCallback, OnFocusFunc, onJSFrameNodeFocusCallback) 132 ACE_DEFINE_FOCUS_EVENT(JSFrameNodeOnBlurCallback, OnBlurFunc, onJSFrameNodeBlurCallback) 133 ACE_DEFINE_FOCUS_EVENT(JSFrameNodeOnKeyCallback, OnKeyCallbackFunc, onJSFrameNodeKeyEventCallback) 134 ACE_DEFINE_FOCUS_EVENT(OnKeyCallback, OnKeyConsumeFunc, onKeyEventCallback) 135 ACE_DEFINE_FOCUS_EVENT(OnKeyPreIme, OnKeyConsumeFunc, onKeyPreImeCallback) 136 ACE_DEFINE_FOCUS_EVENT(OnClickCallback, GestureEventFunc, onClickEventCallback) 137 ACE_DEFINE_FOCUS_EVENT(OnFocusAxisCallback, OnFocusAxisEventFunc, onFocusAxisEventCallback) 138 ACE_DEFINE_FOCUS_EVENT(OnKeyEventDispatchCallback, OnKeyEventDispatchFunc, onKeyEventDispatchCallback) 139 ACE_DEFINE_FOCUS_EVENT(OnCrownCallback, OnCrownCallbackFunc, onCrownEventCallback) 140 ACE_DEFINE_FOCUS_EVENT(OnCrownEventInternal, OnCrownEventFunc, onCrownEventsInternal) 141 std::unordered_map<OnKeyEventType, OnKeyEventFunc> onKeyEventsInternal_; 142 bool isNodeNeedKey_ { false }; // extension use only 143 RefPtr<FocusCallbackEvents> focusCallbackEvents_; 144 145 private: 146 bool OnFocusEventScope(const FocusEvent& event); 147 bool OnFocusEventNode(const FocusEvent& focusEvent); 148 149 bool HandleKeyEvent(const KeyEvent& event, FocusIntension intension); 150 bool OnKeyPreIme(KeyEventInfo& info, const KeyEvent& keyEvent); 151 bool OnKeyEventNodeInternal(const KeyEvent& keyEvent); 152 bool OnKeyEventNodeUser(KeyEventInfo& info, const KeyEvent& keyEvent); 153 bool ProcessOnKeyEventInternal(const KeyEvent& event); 154 bool HandleFocusAxisEvent(const FocusAxisEvent& event); 155 bool HasCustomKeyEventDispatch(const FocusEvent& event); 156 bool HandleCustomEventDispatch(const FocusEvent& event); 157 bool HandleCrownEvent(const CrownEvent& CrownEvent); 158 159 void PrintOnKeyEventUserInfo(const KeyEvent& keyEvent, bool retCallback); 160 }; 161 } // namespace OHOS::Ace::NG 162 #endif