add distance to onClick

Signed-off-by: zhengqiyi <zhengqiyi2@huawei.com>
This commit is contained in:
zhengqiyi 2024-10-16 09:18:59 +00:00
parent b36c6464bf
commit 7c1a05ee2f
26 changed files with 129 additions and 23 deletions

View File

@ -14,6 +14,7 @@
*/
#include "frameworks/bridge/declarative_frontend/jsview/js_button.h"
#include <limits>
#if !defined(PREVIEW) && defined(OHOS_PLATFORM)
#include "interfaces/inner_api/ui_session/ui_session_manager.h"
#endif
@ -551,7 +552,12 @@ void JSButton::JsOnClick(const JSCallbackInfo& info)
#endif
};
ButtonModel::GetInstance()->OnClick(std::move(onTap), std::move(onClick));
double distanceThreshold = std::numeric_limits<double>::infinity();
if (info.Length() > 1 && info[1]->IsNumber()) {
distanceThreshold = info[1]->ToNumber<double>();
distanceThreshold = Dimension(distanceThreshold, DimensionUnit::VP).ConvertToPx();
}
ButtonModel::GetInstance()->OnClick(std::move(onTap), std::move(onClick), distanceThreshold);
}
void JSButton::JsBackgroundColor(const JSCallbackInfo& info)

View File

@ -143,7 +143,12 @@ void JSLocationButton::JsOnClick(const JSCallbackInfo& info)
#endif
};
NG::ViewAbstract::SetOnClick(std::move(onTap));
double distanceThreshold = std::numeric_limits<double>::infinity();
if (info.Length() > 1 && info[1]->IsNumber()) {
distanceThreshold = info[1]->ToNumber<double>();
distanceThreshold = Dimension(distanceThreshold, DimensionUnit::VP).ConvertToPx();
}
NG::ViewAbstract::SetOnClick(std::move(onTap), distanceThreshold);
}
void JSLocationButton::JSBind(BindingTarget globalObj)

View File

@ -142,7 +142,12 @@ void JSPasteButton::JsOnClick(const JSCallbackInfo& info)
#endif
};
NG::ViewAbstract::SetOnClick(std::move(onTap));
double distanceThreshold = std::numeric_limits<double>::infinity();
if (info.Length() > 1 && info[1]->IsNumber()) {
distanceThreshold = info[1]->ToNumber<double>();
distanceThreshold = Dimension(distanceThreshold, DimensionUnit::VP).ConvertToPx();
}
NG::ViewAbstract::SetOnClick(std::move(onTap), distanceThreshold);
}
void JSPasteButton::JSBind(BindingTarget globalObj)

View File

@ -143,7 +143,12 @@ void JSSaveButton::JsOnClick(const JSCallbackInfo& info)
#endif
};
NG::ViewAbstract::SetOnClick(std::move(onTap));
double distanceThreshold = std::numeric_limits<double>::infinity();
if (info.Length() > 1 && info[1]->IsNumber()) {
distanceThreshold = info[1]->ToNumber<double>();
distanceThreshold = Dimension(distanceThreshold, DimensionUnit::VP).ConvertToPx();
}
NG::ViewAbstract::SetOnClick(std::move(onTap), distanceThreshold);
}
void JSSaveButton::JSBind(BindingTarget globalObj)

View File

@ -702,7 +702,12 @@ void JSText::JsOnClick(const JSCallbackInfo& info)
JSInteractableView::ReportClickEvent(node, label);
#endif
};
TextModel::GetInstance()->SetOnClick(std::move(onClick));
double distanceThreshold = std::numeric_limits<double>::infinity();
if (info.Length() > 1 && info[1]->IsNumber()) {
distanceThreshold = info[1]->ToNumber<double>();
distanceThreshold = Dimension(distanceThreshold, DimensionUnit::VP).ConvertToPx();
}
TextModel::GetInstance()->SetOnClick(std::move(onClick), distanceThreshold);
auto focusHub = NG::ViewStackProcessor::GetInstance()->GetOrCreateMainFrameNodeFocusHub();
CHECK_NULL_VOID(focusHub);
@ -733,7 +738,12 @@ void JSText::JsOnClickWithoutNGBUILD(const JSCallbackInfo& info)
PipelineContext::SetCallBackNode(node);
func->Execute(newInfo);
};
TextModel::GetInstance()->SetOnClick(std::move(onClickId));
double distanceThreshold = std::numeric_limits<double>::infinity();
if (info.Length() > 1 && info[1]->IsNumber()) {
distanceThreshold = info[1]->ToNumber<double>();
distanceThreshold = Dimension(distanceThreshold, DimensionUnit::VP).ConvertToPx();
}
TextModel::GetInstance()->SetOnClick(std::move(onClickId), distanceThreshold);
}
#endif
}

View File

@ -175,7 +175,8 @@ void ButtonModelImpl::Padding(const NG::PaddingProperty& paddingNew, const Edge&
}
}
void ButtonModelImpl::OnClick(GestureEventFunc&& tapEventFunc, ClickEventFunc&& clickEventFunc)
void ButtonModelImpl::OnClick(GestureEventFunc&& tapEventFunc, ClickEventFunc&& clickEventFunc,
double distanceThreshold)
{
auto inspector = ViewStackProcessor::GetInstance()->GetInspectorComposedComponent();
CHECK_NULL_VOID(inspector);

View File

@ -34,7 +34,7 @@ public:
void Create(const CreateWithPara& para, std::list<RefPtr<Component>>& buttonChildren) override;
void CreateWithChild(const CreateWithPara& para) override;
void Padding(const NG::PaddingProperty& paddingNew, const Edge& paddingOld) override;
void OnClick(GestureEventFunc&& tapEventFunc, ClickEventFunc&& clickEventFunc) override;
void OnClick(GestureEventFunc&& tapEventFunc, ClickEventFunc&& clickEventFunc, double distanceThreshold) override;
void BackgroundColor(const Color& color, const bool& colorFlag) override;
void SetWidth(const Dimension& width) override;
void SetHeight(const Dimension& height) override;

View File

@ -275,7 +275,7 @@ void TextModelImpl::OnSetAlign()
}
}
void TextModelImpl::SetOnClick(std::function<void(BaseEventInfo*)>&& click)
void TextModelImpl::SetOnClick(std::function<void(BaseEventInfo*)>&& click, double distanceThreshold)
{
auto clickId = EventMarker(std::move(click));
auto gesture = ViewStackProcessor::GetInstance()->GetClickGestureListenerComponent();

View File

@ -65,7 +65,7 @@ public:
void OnSetHeight() override;
void OnSetWidth() override;
void OnSetAlign() override;
void SetOnClick(std::function<void(BaseEventInfo*)>&& click) override;
void SetOnClick(std::function<void(BaseEventInfo*)>&& click, double distanceThreshold) override;
void ClearOnClick() override {};
void SetRemoteMessage(std::function<void()>&& event) override;
void SetCopyOption(CopyOptions copyOption) override;

View File

@ -1154,6 +1154,14 @@ void ViewAbstract::SetFocusBoxStyle(const NG::FocusBoxStyle& style)
focusHub->GetFocusBox().SetStyle(style);
}
void ViewAbstract::SetClickDistance(FrameNode* frameNode, double clickDistance)
{
CHECK_NULL_VOID(frameNode);
auto gestureHub = frameNode->GetOrCreateGestureEventHub();
CHECK_NULL_VOID(gestureHub);
gestureHub->SetNodeClickDistance(clickDistance);
}
void ViewAbstract::SetDefaultFocus(bool isSet)
{
auto focusHub = ViewStackProcessor::GetInstance()->GetOrCreateMainFrameNodeFocusHub();

View File

@ -642,6 +642,7 @@ public:
static void SetSystemColorModeChangeEvent(FrameNode* frameNode, std::function<void(int32_t)>&& onColorModeChange);
static void SetSystemFontChangeEvent(FrameNode* frameNode, std::function<void(float, float)>&& onFontChange);
static void SetFocusBoxStyle(FrameNode* frameNode, const NG::FocusBoxStyle& style);
static void SetClickDistance(FrameNode* frameNode, double clickDistance);
static bool GetFocusable(FrameNode* frameNode);
static bool GetDefaultFocus(FrameNode* frameNode);

View File

@ -477,11 +477,27 @@ void GestureEventHub::SetUserOnClick(GestureEventFunc&& clickEvent, double dista
SetFocusClickEvent(userParallelClickEventActuator_->GetClickEvent());
auto clickRecognizer = userParallelClickEventActuator_->GetClickRecognizer();
clickRecognizer->SetDistanceThreshold(distanceThreshold);
clickEventActuator_->AddDistanceThreshold(distanceThreshold);
} else {
clickEventActuator_->SetUserCallback(std::move(clickEvent));
SetFocusClickEvent(clickEventActuator_->GetClickEvent());
auto clickRecognizer = clickEventActuator_->GetClickRecognizer();
clickRecognizer->SetDistanceThreshold(distanceThreshold);
clickEventActuator_->AddDistanceThreshold(distanceThreshold);
}
}
void GestureEventHub::SetNodeClickDistance(double distanceThreshold)
{
CheckClickActuator();
if (parallelCombineClick) {
auto clickRecognizer = userParallelClickEventActuator_->GetClickRecognizer();
clickRecognizer->SetDistanceThreshold(distanceThreshold);
clickEventActuator_->AddDistanceThreshold(distanceThreshold);
} else {
auto clickRecognizer = clickEventActuator_->GetClickRecognizer();
clickRecognizer->SetDistanceThreshold(distanceThreshold);
clickEventActuator_->AddDistanceThreshold(distanceThreshold);
}
}

View File

@ -141,6 +141,7 @@ public:
// Set by user define, which will replace old one.
void SetUserOnClick(GestureEventFunc&& clickEvent,
double distanceThreshold = std::numeric_limits<double>::infinity());
void SetNodeClickDistance(double distanceThreshold = std::numeric_limits<double>::infinity());
// Set by JS FrameNode.
void SetJSFrameNodeOnClick(GestureEventFunc&& clickEvent);
void SetOnGestureJudgeBegin(GestureJudgeFunc&& gestureJudgeFunc);

View File

@ -39,7 +39,8 @@ public:
virtual void Create(const CreateWithPara& para, std::list<RefPtr<Component>>& buttonChildren) = 0;
virtual void CreateWithChild(const CreateWithPara& para) = 0;
virtual void Padding(const NG::PaddingProperty& paddingNew, const Edge& paddingOld) = 0;
virtual void OnClick(GestureEventFunc&& tapEventFunc, ClickEventFunc&& clickEventFunc) = 0;
virtual void OnClick(GestureEventFunc&& tapEventFunc, ClickEventFunc&& clickEventFunc,
double distanceThreshold) = 0;
virtual void BackgroundColor(const Color& color, const bool& colorFlag) = 0;
virtual void SetWidth(const Dimension& width) {}
virtual void SetHeight(const Dimension& height) {}

View File

@ -309,9 +309,9 @@ void ButtonModelNG::Padding(const PaddingProperty& paddingNew, const Edge& paddi
pattern->SetHasCustomPadding(true);
}
void ButtonModelNG::OnClick(GestureEventFunc&& tapEventFunc, ClickEventFunc&& clickEventFunc)
void ButtonModelNG::OnClick(GestureEventFunc&& tapEventFunc, ClickEventFunc&& clickEventFunc, double distanceThreshold)
{
ViewAbstract::SetOnClick(std::move(tapEventFunc));
ViewAbstract::SetOnClick(std::move(tapEventFunc), distanceThreshold);
}
void ButtonModelNG::BackgroundColor(const Color& color, const bool& colorFlag)

View File

@ -44,7 +44,7 @@ public:
void Create(const CreateWithPara& para, std::list<RefPtr<Component>>& buttonChildren) override;
void CreateWithChild(const CreateWithPara& para) override;
void Padding(const PaddingProperty& paddingNew, const Edge& paddingOld) override;
void OnClick(GestureEventFunc&& tapEventFunc, ClickEventFunc&& clickEventFunc) override;
void OnClick(GestureEventFunc&& tapEventFunc, ClickEventFunc&& clickEventFunc, double distanceThreshold) override;
void BackgroundColor(const Color& color, const bool& colorFlag) override;
void SetSize(const std::optional<Dimension>& width, const std::optional<Dimension>& height) override;
void SetBorderRadius(const Dimension& radius) override;

View File

@ -122,7 +122,7 @@ public:
virtual void OnSetWidth() {};
virtual void OnSetHeight() {};
virtual void OnSetAlign() {};
virtual void SetOnClick(std::function<void(BaseEventInfo* info)>&& click) = 0;
virtual void SetOnClick(std::function<void(BaseEventInfo* info)>&& click, double distanceThreshold) = 0;
virtual void ClearOnClick() = 0;
virtual void SetRemoteMessage(std::function<void()>&& click) = 0;
virtual void SetCopyOption(CopyOptions copyOption) = 0;

View File

@ -378,14 +378,14 @@ void TextModelNG::SetTextDetectConfig(const TextDetectConfig& textDetectConfig)
textPattern->SetTextDetectConfig(textDetectConfig);
}
void TextModelNG::SetOnClick(std::function<void(BaseEventInfo* info)>&& click)
void TextModelNG::SetOnClick(std::function<void(BaseEventInfo* info)>&& click, double distanceThreshold)
{
auto clickFunc = [func = std::move(click)](GestureEvent& info) { func(&info); };
auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
CHECK_NULL_VOID(frameNode);
auto textPattern = frameNode->GetPattern<TextPattern>();
CHECK_NULL_VOID(textPattern);
textPattern->SetOnClickEvent(std::move(clickFunc));
textPattern->SetOnClickEvent(std::move(clickFunc), distanceThreshold);
}
void TextModelNG::ClearOnClick()

View File

@ -65,7 +65,7 @@ public:
void SetHeightAdaptivePolicy(TextHeightAdaptivePolicy value) override;
void SetTextDetectEnable(bool value) override;
void SetTextDetectConfig(const TextDetectConfig& textDetectConfig) override;
void SetOnClick(std::function<void(BaseEventInfo* info)>&& click) override;
void SetOnClick(std::function<void(BaseEventInfo* info)>&& click, double distanceThreshold) override;
void ClearOnClick() override;
void SetRemoteMessage(std::function<void()>&& event) override;
void SetCopyOption(CopyOptions copyOption) override;

View File

@ -1253,7 +1253,7 @@ void TextPattern::InitClickEvent(const RefPtr<GestureEventHub>& gestureHub)
}
return GestureJudgeResult::CONTINUE;
});
gestureHub->AddClickEvent(clickListener);
gestureHub->AddClickEvent(clickListener, distanceThreshold_);
clickEventInitialized_ = true;
}

View File

@ -16,6 +16,7 @@
#ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_TEXT_TEXT_PATTERN_H
#define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_TEXT_TEXT_PATTERN_H
#include <limits>
#include <optional>
#include <string>
#include <unordered_map>
@ -344,9 +345,10 @@ public:
surfacePositionChangedCallbackId_ = id;
}
void SetOnClickEvent(GestureEventFunc&& onClick)
void SetOnClickEvent(GestureEventFunc&& onClick, double distanceThreshold = std::numeric_limits<double>::infinity())
{
onClick_ = std::move(onClick);
distanceThreshold_ = distanceThreshold;
}
virtual void OnColorConfigurationUpdate() override;
@ -975,6 +977,7 @@ private:
OffsetF contentOffset_;
GestureEventFunc onClick_;
double distanceThreshold_ = std::numeric_limits<double>::infinity();
RefPtr<DragWindow> dragWindow_;
RefPtr<DragDropProxy> dragDropProxy_;
std::optional<int32_t> surfaceChangedCallbackId_;

View File

@ -1970,6 +1970,8 @@ struct ArkUICommonModifier {
void (*setFocusBoxStyle)(ArkUINodeHandle node, ArkUI_Float32 valueMargin, ArkUI_Int32 marginUnit,
ArkUI_Float32 valueStrokeWidth, ArkUI_Int32 widthUnit, ArkUI_Uint32 valueColor, ArkUI_Uint32 hasValue);
void (*resetFocusBoxStyle)(ArkUINodeHandle node);
void (*setClickDistance)(ArkUINodeHandle node, ArkUI_Float32 valueMargin);
void (*resetClickDistance)(ArkUINodeHandle node);
void (*setDisAllowDrop)(ArkUINodeHandle node);
void (*setBlendModeByBlender)(ArkUINodeHandle node, ArkUINodeHandle blender, ArkUI_Int32 blendApplyTypeValue);
void (*resetEnableAnalyzer)(ArkUINodeHandle node);

View File

@ -6155,6 +6155,21 @@ void ResetFocusBoxStyle(ArkUINodeHandle node)
ViewAbstract::SetFocusBoxStyle(frameNode, style);
}
void SetClickDistance(ArkUINodeHandle node, ArkUI_Float32 valueMargin)
{
auto* frameNode = reinterpret_cast<FrameNode*>(node);
CHECK_NULL_VOID(frameNode);
double clickDistance = static_cast<double>(valueMargin);
ViewAbstract::SetClickDistance(frameNode, clickDistance);
}
void ResetClickDistance(ArkUINodeHandle node)
{
auto* frameNode = reinterpret_cast<FrameNode*>(node);
CHECK_NULL_VOID(frameNode);
ViewAbstract::SetClickDistance(frameNode, std::numeric_limits<double>::infinity());
}
void SetBlendModeByBlender(ArkUINodeHandle node, ArkUINodeHandle blender, ArkUI_Int32 blendApplyTypeValue)
{
auto* frameNode = reinterpret_cast<FrameNode*>(node);
@ -6238,8 +6253,8 @@ const ArkUICommonModifier* GetCommonModifier()
ResetAccessibilityActions, GetAccessibilityActions, SetAccessibilityRole, ResetAccessibilityRole,
GetAccessibilityRole, SetFocusScopeId, ResetFocusScopeId, SetFocusScopePriority, ResetFocusScopePriority,
SetPixelRound, ResetPixelRound, SetBorderDashParams, GetExpandSafeArea, SetTransition, SetDragPreview,
ResetDragPreview, GetNodeUniqueId, SetFocusBoxStyle, ResetFocusBoxStyle, SetDisAllowDrop,
SetBlendModeByBlender };
ResetDragPreview, GetNodeUniqueId, SetFocusBoxStyle, ResetFocusBoxStyle, SetClickDistance, ResetClickDistance,
SetDisAllowDrop, SetBlendModeByBlender };
return &modifier;
}

View File

@ -1664,6 +1664,16 @@ typedef enum {
*/
NODE_FOCUS_BOX = 96,
/**
* @brief Defines the moving distance limit for the component-bound tap gesture.
* This attribute can be set as required through APIs.
*
* Format of the {@link ArkUI_AttributeItem} parameter for setting the attribute:\n
* .value[0].f32: allowed moving distance of a finger, in vp. \n
*
*/
NODE_CLICK_DISTANCE = 97,
/**
* @brief Defines the text content attribute, which can be set, reset, and obtained as required through APIs.
*

View File

@ -3726,6 +3726,20 @@ void ResetFocusBox(ArkUI_NodeHandle node)
fullImpl->getNodeModifiers()->getCommonModifier()->resetFocusBoxStyle(node->uiNodeHandle);
}
int32_t SetClickDistance(ArkUI_NodeHandle node, const ArkUI_AttributeItem* item)
{
auto* fullImpl = GetFullImpl();
fullImpl->getNodeModifiers()->getCommonModifier()->setClickDistance(
node->uiNodeHandle, item->value[0].f32);
return ERROR_CODE_NO_ERROR;
}
void ResetClickDistance(ArkUI_NodeHandle node)
{
auto* fullImpl = GetFullImpl();
fullImpl->getNodeModifiers()->getCommonModifier()->resetClickDistance(node->uiNodeHandle);
}
const ArkUI_AttributeItem* GetTransition(ArkUI_NodeHandle node)
{
g_attributeItem.object = node->transitionOption;
@ -12706,6 +12720,7 @@ int32_t SetCommonAttribute(ArkUI_NodeHandle node, int32_t subTypeId, const ArkUI
SetTransition,
nullptr,
SetFocusBox,
SetClickDistance,
};
if (static_cast<uint32_t>(subTypeId) >= sizeof(setters) / sizeof(Setter*)) {
TAG_LOGE(AceLogTag::ACE_NATIVE_NODE, "common node attribute: %{public}d NOT IMPLEMENT", subTypeId);
@ -12926,6 +12941,7 @@ void ResetCommonAttribute(ArkUI_NodeHandle node, int32_t subTypeId)
nullptr,
nullptr,
ResetFocusBox,
ResetClickDistance,
};
if (static_cast<uint32_t>(subTypeId) >= sizeof(resetters) / sizeof(Resetter*)) {
TAG_LOGE(AceLogTag::ACE_NATIVE_NODE, "common node attribute: %{public}d NOT IMPLEMENT", subTypeId);

View File

@ -13,6 +13,7 @@
* limitations under the License.
*/
#include <limits>
#include "gtest/gtest.h"
#include "text_base.h"
#include "core/components/text_overlay/text_overlay_theme.h"
@ -189,7 +190,7 @@ HWTEST_F(TextTestNg, TextFrameNodeCreator003, TestSize.Level1)
textStyle.SetFontWeight(FontWeight::W900);
EXPECT_EQ(textStyle.GetFontWeight(), FontWeight::W900);
textModelNG.SetOnClick(onClickFunc);
textModelNG.SetOnClick(onClickFunc, std::numeric_limits<double>::infinity());
textModelNG.SetRemoteMessage(onRemoteMessage);
textModelNG.SetCopyOption(copyOption);
textModelNG.SetOnDragStart(OnDragStartFunction);