-
Notifications
You must be signed in to change notification settings - Fork 6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Windows] Introduce an accessibility plugin
- Loading branch information
1 parent
3c036c0
commit afbf2e7
Showing
8 changed files
with
257 additions
and
61 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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.