-
Notifications
You must be signed in to change notification settings - Fork 1.2k
[Fabric] Implement onFocus and onBlur #11276
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
change/react-native-windows-0d249a84-b206-4efd-bdcb-1888c83a3b3d.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "type": "prerelease", | ||
| "comment": "[Fabric] Implement onFocus and onBlur", | ||
| "packageName": "react-native-windows", | ||
| "email": "30809111+acoates-ms@users.noreply.github.com", | ||
| "dependentChangeType": "patch" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
...actCommon/TEMP_UntilReactCommonUpdate/react/renderer/components/view/ViewEventEmitter.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| /* | ||
| * Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| * | ||
| * This source code is licensed under the MIT license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| */ | ||
|
|
||
| #include "ViewEventEmitter.h" | ||
|
|
||
| namespace facebook::react { | ||
|
|
||
| #pragma mark - Accessibility | ||
|
|
||
| void ViewEventEmitter::onAccessibilityAction(std::string const &name) const { | ||
| dispatchEvent("accessibilityAction", [name](jsi::Runtime &runtime) { | ||
| auto payload = jsi::Object(runtime); | ||
| payload.setProperty(runtime, "actionName", name); | ||
| return payload; | ||
| }); | ||
| } | ||
|
|
||
| void ViewEventEmitter::onAccessibilityTap() const { | ||
| dispatchEvent("accessibilityTap"); | ||
| } | ||
|
|
||
| void ViewEventEmitter::onAccessibilityMagicTap() const { | ||
| dispatchEvent("magicTap"); | ||
| } | ||
|
|
||
| void ViewEventEmitter::onAccessibilityEscape() const { | ||
| dispatchEvent("accessibilityEscape"); | ||
| } | ||
|
|
||
| #pragma mark - Layout | ||
|
|
||
| void ViewEventEmitter::onLayout(const LayoutMetrics &layoutMetrics) const { | ||
| // A copy of a shared pointer (`layoutEventState_`) establishes shared | ||
| // ownership that will be captured by lambda. | ||
| auto layoutEventState = layoutEventState_; | ||
|
|
||
| // Dispatched `frame` values to JavaScript thread are throttled here. | ||
| // Basic ideas: | ||
| // - Scheduling a lambda with some value that already was dispatched, does | ||
| // nothing. | ||
| // - If some lambda is already in flight, we don't schedule another; | ||
| // - When a lambda is being executed on the JavaScript thread, the *most | ||
| // recent* `frame` value is used (not the value that was current at the | ||
| // moment of scheduling the lambda). | ||
| // | ||
| // This implies the following caveats: | ||
| // - Some events can be skipped; | ||
| // - When values change rapidly, even events with different values | ||
| // can be skipped (only the very last will be delivered). | ||
| // - Ordering is preserved. | ||
|
|
||
| { | ||
| std::lock_guard<std::mutex> guard(layoutEventState->mutex); | ||
|
|
||
| // If a *particular* `frame` was already dispatched to the JavaScript side, | ||
| // no other work is required. | ||
| if (layoutEventState->frame == layoutMetrics.frame && | ||
| layoutEventState->wasDispatched) { | ||
| return; | ||
| } | ||
|
|
||
| // If the *particular* `frame` was not already dispatched *or* | ||
| // some *other* `frame` was dispatched before, | ||
| // we need to schedule the dispatching. | ||
| layoutEventState->wasDispatched = false; | ||
| layoutEventState->frame = layoutMetrics.frame; | ||
|
|
||
| // Something is already in flight, dispatching another event is not | ||
| // required. | ||
| if (layoutEventState->isDispatching) { | ||
| return; | ||
| } | ||
|
|
||
| layoutEventState->isDispatching = true; | ||
| } | ||
|
|
||
| dispatchEvent( | ||
| "layout", | ||
| [layoutEventState](jsi::Runtime &runtime) { | ||
| auto frame = Rect{}; | ||
|
|
||
| { | ||
| std::lock_guard<std::mutex> guard(layoutEventState->mutex); | ||
|
|
||
| layoutEventState->isDispatching = false; | ||
|
|
||
| // If some *particular* `frame` was already dispatched before, | ||
| // and since then there were no other new values of the `frame` | ||
| // observed, do nothing. | ||
| if (layoutEventState->wasDispatched) { | ||
| return jsi::Value::null(); | ||
| } | ||
|
|
||
| frame = layoutEventState->frame; | ||
|
|
||
| // If some *particular* `frame` was *not* already dispatched before, | ||
| // it's time to dispatch it and mark as dispatched. | ||
| layoutEventState->wasDispatched = true; | ||
| } | ||
|
|
||
| auto layout = jsi::Object(runtime); | ||
| layout.setProperty(runtime, "x", frame.origin.x); | ||
| layout.setProperty(runtime, "y", frame.origin.y); | ||
| layout.setProperty(runtime, "width", frame.size.width); | ||
| layout.setProperty(runtime, "height", frame.size.height); | ||
| auto payload = jsi::Object(runtime); | ||
| payload.setProperty(runtime, "layout", std::move(layout)); | ||
| return jsi::Value(std::move(payload)); | ||
| }, | ||
| EventPriority::AsynchronousUnbatched); | ||
| } | ||
|
|
||
| // [Windows] | ||
| void ViewEventEmitter::onFocus() const { | ||
| dispatchEvent("focus"); | ||
| } | ||
|
|
||
| // [Windows] | ||
| void ViewEventEmitter::onBlur() const { | ||
| dispatchEvent("blur"); | ||
| } | ||
|
|
||
| } // namespace facebook::react |
82 changes: 82 additions & 0 deletions
82
...ReactCommon/TEMP_UntilReactCommonUpdate/react/renderer/components/view/ViewEventEmitter.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| /* | ||
| * Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| * | ||
| * 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 <memory> | ||
| #include <mutex> | ||
|
|
||
| #include <react/renderer/core/LayoutMetrics.h> | ||
| #include <react/renderer/core/ReactPrimitives.h> | ||
|
|
||
| #include "TouchEventEmitter.h" | ||
|
|
||
| namespace facebook { | ||
| namespace react { | ||
|
|
||
| struct FocusEvent { | ||
| int target; | ||
| }; | ||
|
|
||
| class ViewEventEmitter; | ||
|
|
||
| using SharedViewEventEmitter = std::shared_ptr<const ViewEventEmitter>; | ||
|
|
||
| class ViewEventEmitter : public TouchEventEmitter { | ||
| public: | ||
| using TouchEventEmitter::TouchEventEmitter; | ||
|
|
||
| #pragma mark - Accessibility | ||
|
|
||
| void onAccessibilityAction(std::string const &name) const; | ||
| void onAccessibilityTap() const; | ||
| void onAccessibilityMagicTap() const; | ||
| void onAccessibilityEscape() const; | ||
|
|
||
| #pragma mark - Layout | ||
|
|
||
| void onLayout(const LayoutMetrics &layoutMetrics) const; | ||
|
|
||
| #pragma mark - NativeHostPlatform | ||
|
|
||
| void onFocus() const; // [Windows] | ||
| void onBlur() const; // [Windows] | ||
|
|
||
| private: | ||
| /* | ||
| * Contains the most recent `frame` and a `mutex` protecting access to it. | ||
| */ | ||
| struct LayoutEventState { | ||
| /* | ||
| * Protects an access to other fields of the struct. | ||
| */ | ||
| std::mutex mutex; | ||
|
|
||
| /* | ||
| * Last dispatched `frame` value or value that's being dispatched right now. | ||
| */ | ||
| Rect frame{}; | ||
|
|
||
| /* | ||
| * Indicates that the `frame` value was already dispatched (and dispatching | ||
| * of the *same* value is not needed). | ||
| */ | ||
| bool wasDispatched{false}; | ||
|
|
||
| /* | ||
| * Indicates that some lambda is already being dispatching (and dispatching | ||
| * another one is not needed). | ||
| */ | ||
| bool isDispatching{false}; | ||
| }; | ||
|
|
||
| mutable std::shared_ptr<LayoutEventState> layoutEventState_{ | ||
| std::make_shared<LayoutEventState>()}; | ||
| }; | ||
|
|
||
| } // namespace react | ||
| } // namespace facebook |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.