From 20645681d126e4d36c22a1782849f745e915f43e Mon Sep 17 00:00:00 2001 From: Valentin Shergin Date: Tue, 22 May 2018 15:48:24 -0700 Subject: [PATCH] Fabric: Support of accessibility events for component Summary: This also illustrates how we should use EventHandlers objects. Reviewed By: fkgozali Differential Revision: D8053357 fbshipit-source-id: cba084c8a871e40c7536720fce290c3467d33061 --- .../View/RCTViewComponentView.h | 3 ++ .../View/RCTViewComponentView.mm | 32 ++++++++++++++++-- .../paragraph/ParagraphComponentDescriptor.h | 8 +++-- .../fabric/view/ConcreteViewShadowNode.h | 17 ++++++---- ReactCommon/fabric/view/ViewEventHandlers.cpp | 28 ++++++++++++++++ ReactCommon/fabric/view/ViewEventHandlers.h | 33 +++++++++++++++++++ ReactCommon/fabric/view/ViewShadowNode.h | 2 +- .../view/accessibility/AccessibilityProps.h | 3 -- 8 files changed, 111 insertions(+), 15 deletions(-) create mode 100644 ReactCommon/fabric/view/ViewEventHandlers.cpp create mode 100644 ReactCommon/fabric/view/ViewEventHandlers.h diff --git a/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.h b/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.h index 8ae84d138..f0d482347 100644 --- a/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.h +++ b/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.h @@ -9,8 +9,10 @@ #import #import +#import #import #import +#import NS_ASSUME_NONNULL_BEGIN @@ -21,6 +23,7 @@ NS_ASSUME_NONNULL_BEGIN @protected facebook::react::LayoutMetrics _layoutMetrics; facebook::react::SharedProps _props; + facebook::react::SharedViewEventHandlers _eventHandlers; } @end diff --git a/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.mm b/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.mm index 9183968b5..189850df9 100644 --- a/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.mm +++ b/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.mm @@ -8,13 +8,15 @@ #import "RCTViewComponentView.h" #import +#import + using namespace facebook::react; @implementation RCTViewComponentView -- (void)updateProps:(facebook::react::SharedProps)props - oldProps:(facebook::react::SharedProps)oldProps +- (void)updateProps:(SharedProps)props + oldProps:(SharedProps)oldProps { if (!oldProps) { oldProps = _props ?: std::make_shared(); @@ -31,6 +33,12 @@ using namespace facebook::react; // TODO: Implement all sutable non-layout props. } +- (void)updateEventHandlers:(SharedEventHandlers)eventHandlers +{ + assert(std::dynamic_pointer_cast(eventHandlers)); + _eventHandlers = std::static_pointer_cast(eventHandlers); +} + - (void)updateLayoutMetrics:(LayoutMetrics)layoutMetrics oldLayoutMetrics:(LayoutMetrics)oldLayoutMetrics { @@ -39,4 +47,24 @@ using namespace facebook::react; _layoutMetrics = layoutMetrics; } +#pragma mark - Accessibility Events + +- (BOOL)accessibilityActivate +{ + _eventHandlers->onAccessibilityTap(); + return YES; +} + +- (BOOL)accessibilityPerformMagicTap +{ + _eventHandlers->onAccessibilityMagicTap(); + return YES; +} + +- (BOOL)didActivateAccessibilityCustomAction:(UIAccessibilityCustomAction *)action +{ + _eventHandlers->onAccessibilityAction([action.name cStringUsingEncoding:NSASCIIStringEncoding]); + return YES; +} + @end diff --git a/ReactCommon/fabric/text/paragraph/ParagraphComponentDescriptor.h b/ReactCommon/fabric/text/paragraph/ParagraphComponentDescriptor.h index 6648ddb5e..71e6b8ada 100644 --- a/ReactCommon/fabric/text/paragraph/ParagraphComponentDescriptor.h +++ b/ReactCommon/fabric/text/paragraph/ParagraphComponentDescriptor.h @@ -17,11 +17,13 @@ namespace react { /* * Descriptor for component. */ -class ParagraphComponentDescriptor: public ConcreteComponentDescriptor { +class ParagraphComponentDescriptor final: + public ConcreteComponentDescriptor { + public: - ParagraphComponentDescriptor(): - ConcreteComponentDescriptor() { + ParagraphComponentDescriptor(SharedEventDispatcher eventDispatcher): + ConcreteComponentDescriptor(eventDispatcher) { // Every single `ParagraphShadowNode` will have a reference to // a shared `TextLayoutManager`. textLayoutManager_ = std::make_shared(); diff --git a/ReactCommon/fabric/view/ConcreteViewShadowNode.h b/ReactCommon/fabric/view/ConcreteViewShadowNode.h index 83149be33..df1707ea5 100644 --- a/ReactCommon/fabric/view/ConcreteViewShadowNode.h +++ b/ReactCommon/fabric/view/ConcreteViewShadowNode.h @@ -12,6 +12,7 @@ #include #include #include +#include #include #include @@ -23,9 +24,9 @@ namespace react { * as and similar basic behaviour). * For example: , , but not , . */ -template +template class ConcreteViewShadowNode: - public ConcreteShadowNode, + public ConcreteShadowNode, public AccessibleShadowNode, public YogaLayoutableShadowNode { @@ -37,21 +38,23 @@ public: using ConcreteViewProps = ViewPropsT; using SharedConcreteViewProps = std::shared_ptr; + using ConcreteViewEventHandlers = ViewEventHandlersT; + using SharedConcreteViewEventHandlers = std::shared_ptr; using SharedConcreteViewShadowNode = std::shared_ptr; ConcreteViewShadowNode( const Tag &tag, const Tag &rootTag, - const InstanceHandle &instanceHandle, const SharedConcreteViewProps &props, + const SharedConcreteViewEventHandlers &eventHandlers, const SharedShadowNodeSharedList &children, const ShadowNodeCloneFunction &cloneFunction ): - ConcreteShadowNode( + ConcreteShadowNode( tag, rootTag, - instanceHandle, props, + eventHandlers, children, cloneFunction ), @@ -66,11 +69,13 @@ public: ConcreteViewShadowNode( const SharedConcreteViewShadowNode &shadowNode, const SharedConcreteViewProps &props, + const SharedConcreteViewEventHandlers &eventHandlers, const SharedShadowNodeSharedList &children ): - ConcreteShadowNode( + ConcreteShadowNode( shadowNode, props, + eventHandlers, children ), AccessibleShadowNode( diff --git a/ReactCommon/fabric/view/ViewEventHandlers.cpp b/ReactCommon/fabric/view/ViewEventHandlers.cpp new file mode 100644 index 000000000..261f38c3a --- /dev/null +++ b/ReactCommon/fabric/view/ViewEventHandlers.cpp @@ -0,0 +1,28 @@ +/** + * Copyright (c) 2015-present, Facebook, Inc. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#include "ViewEventHandlers.h" + +namespace facebook { +namespace react { + +#pragma mark - Accessibility + +void ViewEventHandlers::onAccessibilityAction(const std::string &name) const { + dispatchEvent("accessibilityAction", folly::dynamic::object("action", name)); +} + +void ViewEventHandlers::onAccessibilityTap() const { + dispatchEvent("accessibilityTap"); +} + +void ViewEventHandlers::onAccessibilityMagicTap() const { + dispatchEvent("magicTap"); +} + +} // namespace react +} // namespace facebook diff --git a/ReactCommon/fabric/view/ViewEventHandlers.h b/ReactCommon/fabric/view/ViewEventHandlers.h new file mode 100644 index 000000000..3e8236b4b --- /dev/null +++ b/ReactCommon/fabric/view/ViewEventHandlers.h @@ -0,0 +1,33 @@ +/** + * Copyright (c) 2015-present, Facebook, Inc. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ +#pragma once + +#include + +#include + +namespace facebook { +namespace react { + +class ViewEventHandlers; + +using SharedViewEventHandlers = std::shared_ptr; + +class ViewEventHandlers: + public EventHandlers { + +public: + + using EventHandlers::EventHandlers; + + void onAccessibilityAction(const std::string &name) const; + void onAccessibilityTap() const; + void onAccessibilityMagicTap() const; +}; + +} // namespace react +} // namespace facebook diff --git a/ReactCommon/fabric/view/ViewShadowNode.h b/ReactCommon/fabric/view/ViewShadowNode.h index d063b0196..4ad05c83f 100644 --- a/ReactCommon/fabric/view/ViewShadowNode.h +++ b/ReactCommon/fabric/view/ViewShadowNode.h @@ -20,7 +20,7 @@ class ViewShadowNode; using SharedViewShadowNode = std::shared_ptr; class ViewShadowNode final: - public ConcreteViewShadowNode { + public ConcreteViewShadowNode { public: diff --git a/ReactCommon/fabric/view/accessibility/AccessibilityProps.h b/ReactCommon/fabric/view/accessibility/AccessibilityProps.h index f807c5b55..c19881dfb 100644 --- a/ReactCommon/fabric/view/accessibility/AccessibilityProps.h +++ b/ReactCommon/fabric/view/accessibility/AccessibilityProps.h @@ -35,9 +35,6 @@ public: const AccessibilityTraits accessibilityTraits {AccessibilityTraits::None}; const bool accessibilityViewIsModal {false}; const bool accessibilityElementsHidden {false}; - const SharedDirectEventHandler onAccessibilityAction {nullptr}; - const SharedDirectEventHandler onAccessibilityTap {nullptr}; - const SharedDirectEventHandler onMagicTap {nullptr}; }; } // namespace react