This repository was archived by the owner on Feb 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6k
[Windows] Introduce an accessibility plugin #50975
Merged
auto-submit
merged 2 commits into
flutter:main
from
loic-sharma:windows_accessibility_plugin
Feb 27, 2024
Merged
Changes from all commits
Commits
Show all changes
2 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| // Copyright 2013 The Flutter Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| #include "flutter/shell/platform/windows/accessibility_plugin.h" | ||
|
|
||
| #include <variant> | ||
|
|
||
| #include "flutter/fml/logging.h" | ||
| #include "flutter/fml/platform/win/wstring_conversion.h" | ||
| #include "flutter/shell/platform/common/client_wrapper/include/flutter/standard_message_codec.h" | ||
| #include "flutter/shell/platform/windows/flutter_windows_engine.h" | ||
| #include "flutter/shell/platform/windows/flutter_windows_view.h" | ||
|
|
||
| namespace flutter { | ||
|
|
||
| namespace { | ||
|
|
||
| static constexpr char kAccessibilityChannelName[] = "flutter/accessibility"; | ||
| static constexpr char kTypeKey[] = "type"; | ||
| static constexpr char kDataKey[] = "data"; | ||
| static constexpr char kMessageKey[] = "message"; | ||
| static constexpr char kAnnounceValue[] = "announce"; | ||
|
|
||
| // Handles messages like: | ||
| // {"type": "announce", "data": {"message": "Hello"}} | ||
| void HandleMessage(AccessibilityPlugin* plugin, const EncodableValue& message) { | ||
| const auto* map = std::get_if<EncodableMap>(&message); | ||
| if (!map) { | ||
| FML_LOG(ERROR) << "Accessibility message must be a map."; | ||
| return; | ||
| } | ||
| const auto& type_itr = map->find(EncodableValue{kTypeKey}); | ||
| const auto& data_itr = map->find(EncodableValue{kDataKey}); | ||
| if (type_itr == map->end()) { | ||
| FML_LOG(ERROR) << "Accessibility message must have a 'type' property."; | ||
| return; | ||
| } | ||
| if (data_itr == map->end()) { | ||
| FML_LOG(ERROR) << "Accessibility message must have a 'data' property."; | ||
| return; | ||
| } | ||
| const auto* type = std::get_if<std::string>(&type_itr->second); | ||
| const auto* data = std::get_if<EncodableMap>(&data_itr->second); | ||
| if (!type) { | ||
| FML_LOG(ERROR) << "Accessibility message 'type' property must be a string."; | ||
| return; | ||
| } | ||
| if (!data) { | ||
| FML_LOG(ERROR) << "Accessibility message 'data' property must be a map."; | ||
| return; | ||
| } | ||
|
|
||
| if (type->compare(kAnnounceValue) == 0) { | ||
| const auto& message_itr = data->find(EncodableValue{kMessageKey}); | ||
| if (message_itr == data->end()) { | ||
| return; | ||
| } | ||
| const auto* message = std::get_if<std::string>(&message_itr->second); | ||
| if (!message) { | ||
| return; | ||
| } | ||
|
|
||
| plugin->Announce(*message); | ||
| } else { | ||
| FML_LOG(ERROR) << "Accessibility message type '" << *type | ||
| << "' is not supported."; | ||
| } | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| AccessibilityPlugin::AccessibilityPlugin(FlutterWindowsEngine* engine) | ||
| : engine_(engine) {} | ||
|
|
||
| void AccessibilityPlugin::SetUp(BinaryMessenger* binary_messenger, | ||
| AccessibilityPlugin* plugin) { | ||
| BasicMessageChannel<> channel{binary_messenger, kAccessibilityChannelName, | ||
| &StandardMessageCodec::GetInstance()}; | ||
|
|
||
| channel.SetMessageHandler( | ||
| [plugin](const EncodableValue& message, | ||
| const MessageReply<EncodableValue>& reply) { | ||
| HandleMessage(plugin, message); | ||
|
|
||
| // The accessibility channel does not support error handling. | ||
| // Always return an empty response even on failure. | ||
| reply(EncodableValue{std::monostate{}}); | ||
| }); | ||
| } | ||
|
|
||
| void AccessibilityPlugin::Announce(const std::string_view message) { | ||
| if (!engine_->semantics_enabled()) { | ||
| return; | ||
| } | ||
|
|
||
| // TODO(loicsharma): Remove implicit view assumption. | ||
| // https://github.com/flutter/flutter/issues/142845 | ||
| auto view = engine_->view(kImplicitViewId); | ||
| if (!view) { | ||
| return; | ||
| } | ||
|
|
||
| std::wstring wide_text = fml::Utf8ToWideString(message); | ||
| view->AnnounceAlert(wide_text); | ||
| } | ||
|
|
||
| } // namespace flutter | ||
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,41 @@ | ||
| // Copyright 2013 The Flutter Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| #ifndef FLUTTER_SHELL_PLATFORM_WINDOWS_ACCESSIBILITY_PLUGIN_H_ | ||
| #define FLUTTER_SHELL_PLATFORM_WINDOWS_ACCESSIBILITY_PLUGIN_H_ | ||
|
|
||
| #include <string_view> | ||
|
|
||
| #include "flutter/fml/macros.h" | ||
| #include "flutter/shell/platform/common/client_wrapper/include/flutter/binary_messenger.h" | ||
|
|
||
| namespace flutter { | ||
|
|
||
| class FlutterWindowsEngine; | ||
|
|
||
| // Handles messages on the flutter/accessibility channel. | ||
| // | ||
| // See: | ||
| // https://api.flutter.dev/flutter/semantics/SemanticsService-class.html | ||
| class AccessibilityPlugin { | ||
| public: | ||
| explicit AccessibilityPlugin(FlutterWindowsEngine* engine); | ||
|
|
||
| // Begin handling accessibility messages on the `binary_messenger`. | ||
| static void SetUp(BinaryMessenger* binary_messenger, | ||
| AccessibilityPlugin* plugin); | ||
|
|
||
| // Announce a message through the assistive technology. | ||
| virtual void Announce(const std::string_view message); | ||
|
|
||
| private: | ||
| // The engine that owns this plugin. | ||
| FlutterWindowsEngine* engine_ = nullptr; | ||
|
|
||
| FML_DISALLOW_COPY_AND_ASSIGN(AccessibilityPlugin); | ||
| }; | ||
|
|
||
| } // namespace flutter | ||
|
|
||
| #endif // FLUTTER_SHELL_PLATFORM_WINDOWS_ACCESSIBILITY_PLUGIN_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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@yaakovschectman following up on this conversation: #50898 (comment)
I added error logs for malformed top-level
typeanddataproperties. I kept the plugin "flexible" on the announcement message's innermessagedata property, similar to other embedders' logic.