diff --git a/engine/src/flutter/lib/ui/semantics.dart b/engine/src/flutter/lib/ui/semantics.dart index e7df3aba65..c619ab4e59 100644 --- a/engine/src/flutter/lib/ui/semantics.dart +++ b/engine/src/flutter/lib/ui/semantics.dart @@ -283,6 +283,8 @@ class SemanticsFlag { static const int _kIsSliderIndex = 1 << 23; static const int _kIsKeyboardKeyIndex = 1 << 24; static const int _kIsCheckStateMixedIndex = 1 << 25; + static const int _kHasExpandedStateIndex = 1 << 26; + static const int _kIsExpandedIndex = 1 << 27; // READ THIS: if you add a flag here, you MUST update the numSemanticsFlags // value in testing/dart/semantics_test.dart, or tests will fail. Also, // please update the Flag enum in @@ -527,6 +529,27 @@ class SemanticsFlag { /// navigate to the next page when reaching the end of the current one. static const SemanticsFlag hasImplicitScrolling = SemanticsFlag._(_kHasImplicitScrollingIndex, 'hasImplicitScrolling'); + /// The semantics node has the quality of either being "expanded" or "collapsed". + /// + /// For example, a [SubmenuButton] widget has expanded state. + /// + /// See also: + /// + /// * [SemanticsFlag.isExpanded], which controls whether the node is "expanded" or "collapsed". + static const SemanticsFlag hasExpandedState = SemanticsFlag._(_kHasExpandedStateIndex, 'hasExpandedState'); + + /// Whether a semantics node is expanded. + /// + /// If true, the semantics node is "expanded". If false, the semantics node is + /// "collapsed". + /// + /// For example, if a [SubmenuButton] shows its children, [isExpanded] is true. + /// + /// See also: + /// + /// * [SemanticsFlag.hasExpandedState], which enables an expanded/collapsed state. + static const SemanticsFlag isExpanded = SemanticsFlag._(_kIsExpandedIndex, 'isExpanded'); + /// The possible semantics flags. /// /// The map's key is the [index] of the flag and the value is the flag itself. @@ -557,6 +580,8 @@ class SemanticsFlag { _kIsSliderIndex: isSlider, _kIsKeyboardKeyIndex: isKeyboardKey, _kIsCheckStateMixedIndex: isCheckStateMixed, + _kHasExpandedStateIndex: hasExpandedState, + _kIsExpandedIndex: isExpanded, }; static List get values => _kFlagById.values.toList(growable: false); diff --git a/engine/src/flutter/lib/ui/semantics/semantics_node.h b/engine/src/flutter/lib/ui/semantics/semantics_node.h index 3fad29b844..b3789c078c 100644 --- a/engine/src/flutter/lib/ui/semantics/semantics_node.h +++ b/engine/src/flutter/lib/ui/semantics/semantics_node.h @@ -88,6 +88,8 @@ enum class SemanticsFlags : int32_t { kIsSlider = 1 << 23, kIsKeyboardKey = 1 << 24, kIsCheckStateMixed = 1 << 25, + kHasExpandedState = 1 << 26, + kIsExpanded = 1 << 27, }; const int kScrollableSemanticsFlags = diff --git a/engine/src/flutter/lib/web_ui/lib/semantics.dart b/engine/src/flutter/lib/web_ui/lib/semantics.dart index 01a46bfad2..d1b1904dda 100644 --- a/engine/src/flutter/lib/web_ui/lib/semantics.dart +++ b/engine/src/flutter/lib/web_ui/lib/semantics.dart @@ -121,6 +121,8 @@ class SemanticsFlag { static const int _kIsSliderIndex = 1 << 23; static const int _kIsKeyboardKeyIndex = 1 << 24; static const int _kIsCheckStateMixedIndex = 1 << 25; + static const int _kHasExpandedStateIndex = 1 << 26; + static const int _kIsExpandedIndex = 1 << 27; static const SemanticsFlag hasCheckedState = SemanticsFlag._(_kHasCheckedStateIndex, 'hasCheckedState'); static const SemanticsFlag isChecked = SemanticsFlag._(_kIsCheckedIndex, 'isChecked'); @@ -148,6 +150,8 @@ class SemanticsFlag { static const SemanticsFlag isToggled = SemanticsFlag._(_kIsToggledIndex, 'isToggled'); static const SemanticsFlag hasImplicitScrolling = SemanticsFlag._(_kHasImplicitScrollingIndex, 'hasImplicitScrolling'); static const SemanticsFlag isCheckStateMixed = SemanticsFlag._(_kIsCheckStateMixedIndex, 'isCheckStateMixed'); + static const SemanticsFlag hasExpandedState = SemanticsFlag._(_kHasExpandedStateIndex, 'hasExpandedState'); + static const SemanticsFlag isExpanded = SemanticsFlag._(_kIsExpandedIndex, 'isExpanded'); static const Map _kFlagById = { _kHasCheckedStateIndex: hasCheckedState, @@ -176,6 +180,8 @@ class SemanticsFlag { _kIsSliderIndex: isSlider, _kIsKeyboardKeyIndex: isKeyboardKey, _kIsCheckStateMixedIndex: isCheckStateMixed, + _kHasExpandedStateIndex: hasExpandedState, + _kIsExpandedIndex: isExpanded, }; static List get values => _kFlagById.values.toList(growable: false); diff --git a/engine/src/flutter/lib/web_ui/test/engine/semantics/semantics_api_test.dart b/engine/src/flutter/lib/web_ui/test/engine/semantics/semantics_api_test.dart index 7e79f3e87b..db1cda282e 100644 --- a/engine/src/flutter/lib/web_ui/test/engine/semantics/semantics_api_test.dart +++ b/engine/src/flutter/lib/web_ui/test/engine/semantics/semantics_api_test.dart @@ -18,7 +18,7 @@ void main() { void testMain() { // This must match the number of flags in lib/ui/semantics.dart - const int numSemanticsFlags = 26; + const int numSemanticsFlags = 28; test('SemanticsFlag.values refers to all flags.', () async { expect(SemanticsFlag.values.length, equals(numSemanticsFlags)); for (int index = 0; index < numSemanticsFlags; ++index) { 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 3ade64dff2..134c9fbcff 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 @@ -2169,7 +2169,9 @@ public class AccessibilityBridge extends AccessibilityNodeProvider { IS_LINK(1 << 22), IS_SLIDER(1 << 23), IS_KEYBOARD_KEY(1 << 24), - IS_CHECK_STATE_MIXED(1 << 25); + IS_CHECK_STATE_MIXED(1 << 25), + HAS_EXPANDED_STATE(1 << 26), + IS_EXPANDED(1 << 27); final int value; diff --git a/engine/src/flutter/shell/platform/common/accessibility_bridge.cc b/engine/src/flutter/shell/platform/common/accessibility_bridge.cc index ccabc7b293..d42febd485 100644 --- a/engine/src/flutter/shell/platform/common/accessibility_bridge.cc +++ b/engine/src/flutter/shell/platform/common/accessibility_bridge.cc @@ -353,6 +353,13 @@ void AccessibilityBridge::SetStateFromFlutterUpdate(ui::AXNodeData& node_data, const SemanticsNode& node) { FlutterSemanticsFlag flags = node.flags; FlutterSemanticsAction actions = node.actions; + if (flags & FlutterSemanticsFlag::kFlutterSemanticsFlagHasExpandedState && + flags & FlutterSemanticsFlag::kFlutterSemanticsFlagIsExpanded) { + node_data.AddState(ax::mojom::State::kExpanded); + } else if (flags & + FlutterSemanticsFlag::kFlutterSemanticsFlagHasExpandedState) { + node_data.AddState(ax::mojom::State::kCollapsed); + } if (flags & FlutterSemanticsFlag::kFlutterSemanticsFlagIsTextField && (flags & FlutterSemanticsFlag::kFlutterSemanticsFlagIsReadOnly) == 0) { node_data.AddState(ax::mojom::State::kEditable); diff --git a/engine/src/flutter/shell/platform/embedder/embedder.h b/engine/src/flutter/shell/platform/embedder/embedder.h index f1197403b7..a1e9036b68 100644 --- a/engine/src/flutter/shell/platform/embedder/embedder.h +++ b/engine/src/flutter/shell/platform/embedder/embedder.h @@ -236,6 +236,11 @@ typedef enum { kFlutterSemanticsFlagIsKeyboardKey = 1 << 24, /// Whether the semantics node represents a tristate checkbox in mixed state. kFlutterSemanticsFlagIsCheckStateMixed = 1 << 25, + /// The semantics node has the quality of either being "expanded" or + /// "collapsed". + kFlutterSemanticsFlagHasExpandedState = 1 << 26, + /// Whether a semantic node that hasExpandedState is currently expanded. + kFlutterSemanticsFlagIsExpanded = 1 << 27, } FlutterSemanticsFlag; typedef enum { diff --git a/engine/src/flutter/shell/platform/windows/flutter_windows_view_unittests.cc b/engine/src/flutter/shell/platform/windows/flutter_windows_view_unittests.cc index 099ad22a67..850f2708d8 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_windows_view_unittests.cc +++ b/engine/src/flutter/shell/platform/windows/flutter_windows_view_unittests.cc @@ -132,6 +132,110 @@ class MockAngleSurfaceManager : public AngleSurfaceManager { } // namespace +// Ensure that submenu buttons have their expanded/collapsed status set +// apropriately. +TEST(FlutterWindowsViewTest, SubMenuExpandedState) { + std::unique_ptr engine = GetTestEngine(); + EngineModifier modifier(engine.get()); + modifier.embedder_api().UpdateSemanticsEnabled = + [](FLUTTER_API_SYMBOL(FlutterEngine) engine, bool enabled) { + return kSuccess; + }; + + auto window_binding_handler = + std::make_unique>(); + FlutterWindowsView view(std::move(window_binding_handler)); + view.SetEngine(std::move(engine)); + + // Enable semantics to instantiate accessibility bridge. + view.OnUpdateSemanticsEnabled(true); + + auto bridge = view.accessibility_bridge().lock(); + ASSERT_TRUE(bridge); + + FlutterSemanticsNode2 root{sizeof(FlutterSemanticsNode2), 0}; + root.id = 0; + root.label = "root"; + root.hint = ""; + root.value = ""; + root.increased_value = ""; + root.decreased_value = ""; + root.child_count = 0; + root.custom_accessibility_actions_count = 0; + root.flags = static_cast( + FlutterSemanticsFlag::kFlutterSemanticsFlagHasExpandedState | + FlutterSemanticsFlag::kFlutterSemanticsFlagIsExpanded); + bridge->AddFlutterSemanticsNodeUpdate(root); + + bridge->CommitUpdates(); + + { + auto root_node = bridge->GetFlutterPlatformNodeDelegateFromID(0).lock(); + EXPECT_TRUE(root_node->GetData().HasState(ax::mojom::State::kExpanded)); + + // Get the IAccessible for the root node. + IAccessible* native_view = root_node->GetNativeViewAccessible(); + ASSERT_TRUE(native_view != nullptr); + + // Look up against the node itself (not one of its children). + VARIANT varchild = {}; + varchild.vt = VT_I4; + + // Verify the submenu is expanded. + varchild.lVal = CHILDID_SELF; + VARIANT native_state = {}; + ASSERT_TRUE(SUCCEEDED(native_view->get_accState(varchild, &native_state))); + EXPECT_TRUE(native_state.lVal & STATE_SYSTEM_EXPANDED); + + // Perform similar tests for UIA value; + IRawElementProviderSimple* uia_node; + native_view->QueryInterface(IID_PPV_ARGS(&uia_node)); + ASSERT_TRUE(SUCCEEDED(uia_node->GetPropertyValue( + UIA_ExpandCollapseExpandCollapseStatePropertyId, &native_state))); + EXPECT_EQ(native_state.lVal, ExpandCollapseState_Expanded); + + ASSERT_TRUE(SUCCEEDED(uia_node->GetPropertyValue( + UIA_AriaPropertiesPropertyId, &native_state))); + EXPECT_NE(std::wcsstr(native_state.bstrVal, L"expanded=true"), nullptr); + } + + // Test collapsed too. + root.flags = static_cast( + FlutterSemanticsFlag::kFlutterSemanticsFlagHasExpandedState); + bridge->AddFlutterSemanticsNodeUpdate(root); + bridge->CommitUpdates(); + + { + auto root_node = bridge->GetFlutterPlatformNodeDelegateFromID(0).lock(); + EXPECT_TRUE(root_node->GetData().HasState(ax::mojom::State::kCollapsed)); + + // Get the IAccessible for the root node. + IAccessible* native_view = root_node->GetNativeViewAccessible(); + ASSERT_TRUE(native_view != nullptr); + + // Look up against the node itself (not one of its children). + VARIANT varchild = {}; + varchild.vt = VT_I4; + + // Verify the submenu is collapsed. + varchild.lVal = CHILDID_SELF; + VARIANT native_state = {}; + ASSERT_TRUE(SUCCEEDED(native_view->get_accState(varchild, &native_state))); + EXPECT_TRUE(native_state.lVal & STATE_SYSTEM_COLLAPSED); + + // Perform similar tests for UIA value; + IRawElementProviderSimple* uia_node; + native_view->QueryInterface(IID_PPV_ARGS(&uia_node)); + ASSERT_TRUE(SUCCEEDED(uia_node->GetPropertyValue( + UIA_ExpandCollapseExpandCollapseStatePropertyId, &native_state))); + EXPECT_EQ(native_state.lVal, ExpandCollapseState_Collapsed); + + ASSERT_TRUE(SUCCEEDED(uia_node->GetPropertyValue( + UIA_AriaPropertiesPropertyId, &native_state))); + EXPECT_NE(std::wcsstr(native_state.bstrVal, L"expanded=false"), nullptr); + } +} + // The view's surface must be destroyed after the engine is shutdown. // See: https://github.com/flutter/flutter/issues/124463 TEST(FlutterWindowsViewTest, Shutdown) { diff --git a/engine/src/flutter/testing/dart/semantics_test.dart b/engine/src/flutter/testing/dart/semantics_test.dart index 791a9753a6..a995b4037f 100644 --- a/engine/src/flutter/testing/dart/semantics_test.dart +++ b/engine/src/flutter/testing/dart/semantics_test.dart @@ -11,7 +11,7 @@ import 'package:litetest/litetest.dart'; void main() { // This must match the number of flags in lib/ui/semantics.dart - const int numSemanticsFlags = 26; + const int numSemanticsFlags = 28; test('SemanticsFlag.values refers to all flags.', () async { expect(SemanticsFlag.values.length, equals(numSemanticsFlags)); for (int index = 0; index < numSemanticsFlags; ++index) {