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 #50898
Closed
Closed
Changes from all commits
Commits
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,93 @@ | ||
// 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/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) { | ||
return; | ||
} | ||
const auto& type_itr = map->find(EncodableValue{kTypeKey}); | ||
const auto& data_itr = map->find(EncodableValue{kDataKey}); | ||
if (type_itr == map->end() || data_itr == map->end()) { | ||
return; | ||
} | ||
const auto* type = std::get_if<std::string>(&type_itr->second); | ||
const auto* data = std::get_if<EncodableMap>(&data_itr->second); | ||
if (!type || !data) { | ||
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); | ||
} | ||
} | ||
|
||
} // 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.
Should this produce an error if type or data is missing?
Uh oh!
There was an error while loading. Please reload this page.
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.
I'm not sure why, but the
flutter/accessibility
channel does not follow the normal method call protocol. AFAICT it does not support error handling. Ignoring malformed messages appears to be the "correct" implementation:null
.true
.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.
Would we want to log an error message? Or just ignore it if the other platforms do so?
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.
Excellent idea, added!
Uh oh!
There was an error while loading. Please reload this page.
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.
Hmm, GitHub seems to be broken... it isn't showing the latest commit... I tried force pushing my changes too 🙃
You can find the latest implementation here: https://github.com/loic-sharma/flutter-engine/blob/c5be9d79e3c55835ece522bd8e3b8261ee19f529/shell/platform/windows/accessibility_plugin.cc#L27-L69