From 4fc00d4735d43ef0ab9504111541928a3c2167ea Mon Sep 17 00:00:00 2001 From: Michael Goderbauer Date: Tue, 12 Dec 2017 10:06:04 -0800 Subject: [PATCH] Add parameters to SemanticActions; implement extend selection for a11y (flutter/engine#4444) --- engine/src/flutter/lib/ui/hooks.dart | 5 +++-- engine/src/flutter/lib/ui/window.dart | 4 ++-- engine/src/flutter/lib/ui/window/window.cc | 14 +++++++++++--- engine/src/flutter/lib/ui/window/window.h | 4 +++- .../src/flutter/runtime/runtime_controller.cc | 5 +++-- .../src/flutter/runtime/runtime_controller.h | 4 +++- engine/src/flutter/shell/common/engine.cc | 6 ++++-- engine/src/flutter/shell/common/engine.h | 4 +++- .../src/flutter/shell/common/platform_view.cc | 7 ++++--- .../src/flutter/shell/common/platform_view.h | 4 +++- .../io/flutter/view/AccessibilityBridge.java | 9 ++++----- .../android/io/flutter/view/FlutterView.java | 14 ++++++++++++-- .../platform/android/platform_view_android.cc | 19 +++++++++++++++++-- .../platform/android/platform_view_android.h | 6 +++++- .../android/platform_view_android_jni.cc | 9 ++++++--- 15 files changed, 83 insertions(+), 31 deletions(-) diff --git a/engine/src/flutter/lib/ui/hooks.dart b/engine/src/flutter/lib/ui/hooks.dart index 8f0f6ae9c4..ed201c8c50 100644 --- a/engine/src/flutter/lib/ui/hooks.dart +++ b/engine/src/flutter/lib/ui/hooks.dart @@ -85,12 +85,13 @@ void _dispatchPointerDataPacket(ByteData packet) { _invoke1(window.onPointerDataPacket, window._onPointerDataPacketZone, _unpackPointerDataPacket(packet)); } -void _dispatchSemanticsAction(int id, int action) { - _invoke2( +void _dispatchSemanticsAction(int id, int action, ByteData args) { + _invoke3( window.onSemanticsAction, window._onSemanticsActionZone, id, SemanticsAction.values[action], + args, ); } diff --git a/engine/src/flutter/lib/ui/window.dart b/engine/src/flutter/lib/ui/window.dart index f6a9d6e01d..2e8ea7a1b4 100644 --- a/engine/src/flutter/lib/ui/window.dart +++ b/engine/src/flutter/lib/ui/window.dart @@ -14,7 +14,7 @@ typedef void FrameCallback(Duration duration); typedef void PointerDataPacketCallback(PointerDataPacket packet); /// Signature for [Window.onSemanticsAction]. -typedef void SemanticsActionCallback(int id, SemanticsAction action); +typedef void SemanticsActionCallback(int id, SemanticsAction action, ByteData args); /// Signature for responses to platform messages. /// @@ -439,7 +439,7 @@ class Window { /// The setting indicating whether time should always be shown in the 24-hour /// format. - /// + /// /// This option is used by [showTimePicker]. bool get alwaysUse24HourFormat => _alwaysUse24HourFormat; bool _alwaysUse24HourFormat = false; diff --git a/engine/src/flutter/lib/ui/window/window.cc b/engine/src/flutter/lib/ui/window/window.cc index beb655df77..7b4cf2cde1 100644 --- a/engine/src/flutter/lib/ui/window/window.cc +++ b/engine/src/flutter/lib/ui/window/window.cc @@ -223,14 +223,22 @@ void Window::DispatchPointerDataPacket(const PointerDataPacket& packet) { {data_handle}); } -void Window::DispatchSemanticsAction(int32_t id, SemanticsAction action) { +void Window::DispatchSemanticsAction(int32_t id, + SemanticsAction action, + std::vector args) { tonic::DartState* dart_state = library_.dart_state().get(); if (!dart_state) return; tonic::DartState::Scope scope(dart_state); - DartInvokeField(library_.value(), "_dispatchSemanticsAction", - {ToDart(id), ToDart(static_cast(action))}); + Dart_Handle args_handle = (args.empty()) ? Dart_Null() : ToByteData(args); + + if (Dart_IsError(args_handle)) + return; + + DartInvokeField( + library_.value(), "_dispatchSemanticsAction", + {ToDart(id), ToDart(static_cast(action)), args_handle}); } void Window::BeginFrame(fxl::TimePoint frameTime) { diff --git a/engine/src/flutter/lib/ui/window/window.h b/engine/src/flutter/lib/ui/window/window.h index ac65feaf26..fa8ce41835 100644 --- a/engine/src/flutter/lib/ui/window/window.h +++ b/engine/src/flutter/lib/ui/window/window.h @@ -49,7 +49,9 @@ class Window { void UpdateSemanticsEnabled(bool enabled); void DispatchPlatformMessage(fxl::RefPtr message); void DispatchPointerDataPacket(const PointerDataPacket& packet); - void DispatchSemanticsAction(int32_t id, SemanticsAction action); + void DispatchSemanticsAction(int32_t id, + SemanticsAction action, + std::vector args); void BeginFrame(fxl::TimePoint frameTime); void CompletePlatformMessageResponse(int response_id, diff --git a/engine/src/flutter/runtime/runtime_controller.cc b/engine/src/flutter/runtime/runtime_controller.cc index d3348ad7c5..025e4917ac 100644 --- a/engine/src/flutter/runtime/runtime_controller.cc +++ b/engine/src/flutter/runtime/runtime_controller.cc @@ -106,10 +106,11 @@ void RuntimeController::DispatchPointerDataPacket( } void RuntimeController::DispatchSemanticsAction(int32_t id, - SemanticsAction action) { + SemanticsAction action, + std::vector args) { TRACE_EVENT1("flutter", "RuntimeController::DispatchSemanticsAction", "mode", "basic"); - GetWindow()->DispatchSemanticsAction(id, action); + GetWindow()->DispatchSemanticsAction(id, action, std::move(args)); } Window* RuntimeController::GetWindow() { diff --git a/engine/src/flutter/runtime/runtime_controller.h b/engine/src/flutter/runtime/runtime_controller.h index d774c5de87..d70bb177c8 100644 --- a/engine/src/flutter/runtime/runtime_controller.h +++ b/engine/src/flutter/runtime/runtime_controller.h @@ -42,7 +42,9 @@ class RuntimeController : public WindowClient, public IsolateClient { void DispatchPlatformMessage(fxl::RefPtr message); void DispatchPointerDataPacket(const PointerDataPacket& packet); - void DispatchSemanticsAction(int32_t id, SemanticsAction action); + void DispatchSemanticsAction(int32_t id, + SemanticsAction action, + std::vector args); Dart_Port GetMainPort(); std::string GetIsolateName(); diff --git a/engine/src/flutter/shell/common/engine.cc b/engine/src/flutter/shell/common/engine.cc index e550e0a393..04de8a2b41 100644 --- a/engine/src/flutter/shell/common/engine.cc +++ b/engine/src/flutter/shell/common/engine.cc @@ -554,9 +554,11 @@ void Engine::DispatchPointerDataPacket(const PointerDataPacket& packet) { runtime_->DispatchPointerDataPacket(packet); } -void Engine::DispatchSemanticsAction(int id, blink::SemanticsAction action) { +void Engine::DispatchSemanticsAction(int id, + blink::SemanticsAction action, + std::vector args) { if (runtime_) - runtime_->DispatchSemanticsAction(id, action); + runtime_->DispatchSemanticsAction(id, action, std::move(args)); } void Engine::SetSemanticsEnabled(bool enabled) { diff --git a/engine/src/flutter/shell/common/engine.h b/engine/src/flutter/shell/common/engine.h index 6f7eb7c087..7706b6da50 100644 --- a/engine/src/flutter/shell/common/engine.h +++ b/engine/src/flutter/shell/common/engine.h @@ -72,7 +72,9 @@ class Engine : public blink::RuntimeDelegate { void SetViewportMetrics(const blink::ViewportMetrics& metrics); void DispatchPlatformMessage(fxl::RefPtr message); void DispatchPointerDataPacket(const PointerDataPacket& packet); - void DispatchSemanticsAction(int id, blink::SemanticsAction action); + void DispatchSemanticsAction(int id, + blink::SemanticsAction action, + std::vector args); void SetSemanticsEnabled(bool enabled); void ScheduleFrame(bool regenerate_layer_tree = true) override; diff --git a/engine/src/flutter/shell/common/platform_view.cc b/engine/src/flutter/shell/common/platform_view.cc index eccd26916d..fc331c962e 100644 --- a/engine/src/flutter/shell/common/platform_view.cc +++ b/engine/src/flutter/shell/common/platform_view.cc @@ -55,12 +55,13 @@ void PlatformView::DispatchPlatformMessage( } void PlatformView::DispatchSemanticsAction(int32_t id, - blink::SemanticsAction action) { + blink::SemanticsAction action, + std::vector args) { blink::Threads::UI()->PostTask( - [ engine = engine_->GetWeakPtr(), id, action ] { + [ engine = engine_->GetWeakPtr(), id, action, args = std::move(args) ] { if (engine) { engine->DispatchSemanticsAction( - id, static_cast(action)); + id, static_cast(action), std::move(args)); } }); } diff --git a/engine/src/flutter/shell/common/platform_view.h b/engine/src/flutter/shell/common/platform_view.h index a377f721ef..f572cb2bdf 100644 --- a/engine/src/flutter/shell/common/platform_view.h +++ b/engine/src/flutter/shell/common/platform_view.h @@ -41,7 +41,9 @@ class PlatformView : public std::enable_shared_from_this { virtual void Attach() = 0; void DispatchPlatformMessage(fxl::RefPtr message); - void DispatchSemanticsAction(int32_t id, blink::SemanticsAction action); + void DispatchSemanticsAction(int32_t id, + blink::SemanticsAction action, + std::vector args); void SetSemanticsEnabled(bool enabled); void NotifyCreated(std::unique_ptr surface); diff --git a/engine/src/flutter/shell/platform/android/io/flutter/view/AccessibilityBridge.java b/engine/src/flutter/shell/platform/android/io/flutter/view/AccessibilityBridge.java index 81b6e99e02..e0bbec66b0 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/view/AccessibilityBridge.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/view/AccessibilityBridge.java @@ -312,17 +312,16 @@ class AccessibilityBridge extends AccessibilityNodeProvider implements BasicMess boolean performCursorMoveAction(SemanticsObject object, int virtualViewId, Bundle arguments, boolean forward) { final int granularity = arguments.getInt( AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT); - // TODO(goderbauer): support extending selections. - // final boolean extendSelection = arguments.getBoolean( - // AccessibilityNodeInfo.ACTION_ARGUMENT_EXTEND_SELECTION_BOOLEAN); + final boolean extendSelection = arguments.getBoolean( + AccessibilityNodeInfo.ACTION_ARGUMENT_EXTEND_SELECTION_BOOLEAN); switch (granularity) { case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER: { if (forward && object.hasAction(Action.MOVE_CURSOR_FORWARD_BY_CHARACTER)) { - mOwner.dispatchSemanticsAction(virtualViewId, Action.MOVE_CURSOR_FORWARD_BY_CHARACTER); + mOwner.dispatchSemanticsAction(virtualViewId, Action.MOVE_CURSOR_FORWARD_BY_CHARACTER, extendSelection); return true; } if (!forward && object.hasAction(Action.MOVE_CURSOR_BACKWARD_BY_CHARACTER)) { - mOwner.dispatchSemanticsAction(virtualViewId, Action.MOVE_CURSOR_BACKWARD_BY_CHARACTER); + mOwner.dispatchSemanticsAction(virtualViewId, Action.MOVE_CURSOR_BACKWARD_BY_CHARACTER, extendSelection); return true; } } diff --git a/engine/src/flutter/shell/platform/android/io/flutter/view/FlutterView.java b/engine/src/flutter/shell/platform/android/io/flutter/view/FlutterView.java index 3c287ce84e..4d29787d5a 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/view/FlutterView.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/view/FlutterView.java @@ -682,7 +682,7 @@ public class FlutterView extends SurfaceView ByteBuffer buffer, int position); private static native void nativeDispatchSemanticsAction(long nativePlatformViewAndroid, int id, - int action); + int action, ByteBuffer args, int argsPosition); private static native void nativeSetSemanticsEnabled(long nativePlatformViewAndroid, boolean enabled); @@ -743,9 +743,19 @@ public class FlutterView extends SurfaceView private TouchExplorationListener mTouchExplorationListener; protected void dispatchSemanticsAction(int id, AccessibilityBridge.Action action) { + dispatchSemanticsAction(id, action, null); + } + + protected void dispatchSemanticsAction(int id, AccessibilityBridge.Action action, Object args) { if (!isAttached()) return; - nativeDispatchSemanticsAction(mNativeView.get(), id, action.value); + ByteBuffer encodedArgs = null; + int position = 0; + if (args != null) { + encodedArgs = StandardMessageCodec.INSTANCE.encodeMessage(args); + position = encodedArgs.position(); + } + nativeDispatchSemanticsAction(mNativeView.get(), id, action.value, encodedArgs, position); } @Override diff --git a/engine/src/flutter/shell/platform/android/platform_view_android.cc b/engine/src/flutter/shell/platform/android/platform_view_android.cc index 61893a58f1..8e628c7397 100644 --- a/engine/src/flutter/shell/platform/android/platform_view_android.cc +++ b/engine/src/flutter/shell/platform/android/platform_view_android.cc @@ -410,9 +410,24 @@ void PlatformViewAndroid::HandlePlatformMessageEmptyResponse(int response_id) { nullptr); } -void PlatformViewAndroid::DispatchSemanticsAction(jint id, jint action) { +void PlatformViewAndroid::DispatchSemanticsAction(JNIEnv* env, + jint id, + jint action, + jobject args, + jint args_position) { + if (env->IsSameObject(args, NULL)) { + std::vector args_vector; + PlatformView::DispatchSemanticsAction( + id, static_cast(action), args_vector); + return; + } + + uint8_t* args_data = static_cast(env->GetDirectBufferAddress(args)); + std::vector args_vector = + std::vector(args_data, args_data + args_position); + PlatformView::DispatchSemanticsAction( - id, static_cast(action)); + id, static_cast(action), std::move(args_vector)); } void PlatformViewAndroid::SetSemanticsEnabled(jboolean enabled) { diff --git a/engine/src/flutter/shell/platform/android/platform_view_android.h b/engine/src/flutter/shell/platform/android/platform_view_android.h index 6308d4c744..48b2604001 100644 --- a/engine/src/flutter/shell/platform/android/platform_view_android.h +++ b/engine/src/flutter/shell/platform/android/platform_view_android.h @@ -79,7 +79,11 @@ class PlatformViewAndroid : public PlatformView { void InvokePlatformMessageEmptyResponseCallback(JNIEnv* env, jint response_id); - void DispatchSemanticsAction(jint id, jint action); + void DispatchSemanticsAction(JNIEnv* env, + jint id, + jint action, + jobject args, + jint args_position); void SetSemanticsEnabled(jboolean enabled); diff --git a/engine/src/flutter/shell/platform/android/platform_view_android_jni.cc b/engine/src/flutter/shell/platform/android/platform_view_android_jni.cc index 42673a32b3..448e809f6c 100644 --- a/engine/src/flutter/shell/platform/android/platform_view_android_jni.cc +++ b/engine/src/flutter/shell/platform/android/platform_view_android_jni.cc @@ -236,8 +236,11 @@ static void DispatchSemanticsAction(JNIEnv* env, jobject jcaller, jlong platform_view, jint id, - jint action) { - return PLATFORM_VIEW->DispatchSemanticsAction(id, action); + jint action, + jobject args, + jint args_position) { + return PLATFORM_VIEW->DispatchSemanticsAction(env, id, action, args, + args_position); } static void SetSemanticsEnabled(JNIEnv* env, @@ -413,7 +416,7 @@ bool PlatformViewAndroid::Register(JNIEnv* env) { }, { .name = "nativeDispatchSemanticsAction", - .signature = "(JII)V", + .signature = "(JIILjava/nio/ByteBuffer;I)V", .fnPtr = reinterpret_cast(&shell::DispatchSemanticsAction), }, {