binding.dart 2.61 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'dart:ui' as ui show AccessibilityFeatures, SemanticsUpdateBuilder;
6 7 8

import 'package:flutter/foundation.dart';

9 10
import 'debug.dart';

11
export 'dart:ui' show AccessibilityFeatures;
12 13

/// The glue between the semantics layer and the Flutter engine.
14
// TODO(zanderso): move the remaining semantic related bindings here.
15
mixin SemanticsBinding on BindingBase {
16 17 18 19
  @override
  void initInstances() {
    super.initInstances();
    _instance = this;
20
    _accessibilityFeatures = platformDispatcher.accessibilityFeatures;
21 22
  }

23 24 25 26 27 28 29 30
  /// The current [SemanticsBinding], if one has been created.
  ///
  /// Provides access to the features exposed by this mixin. The binding must
  /// be initialized before using this getter; this is typically done by calling
  /// [runApp] or [WidgetsFlutterBinding.ensureInitialized].
  static SemanticsBinding get instance => BindingBase.checkInstance(_instance);
  static SemanticsBinding? _instance;

31 32
  /// Called when the platform accessibility features change.
  ///
33
  /// See [dart:ui.PlatformDispatcher.onAccessibilityFeaturesChanged].
34 35
  @protected
  void handleAccessibilityFeaturesChanged() {
36
    _accessibilityFeatures = platformDispatcher.accessibilityFeatures;
37 38
  }

39 40 41 42 43 44 45 46 47 48
  /// Creates an empty semantics update builder.
  ///
  /// The caller is responsible for filling out the semantics node updates.
  ///
  /// This method is used by the [SemanticsOwner] to create builder for all its
  /// semantics updates.
  ui.SemanticsUpdateBuilder createSemanticsUpdateBuilder() {
    return ui.SemanticsUpdateBuilder();
  }

49 50 51 52 53 54
  /// The currently active set of [AccessibilityFeatures].
  ///
  /// This is initialized the first time [runApp] is called and updated whenever
  /// a flag is changed.
  ///
  /// To listen to changes to accessibility features, create a
55 56
  /// [WidgetsBindingObserver] and listen to
  /// [WidgetsBindingObserver.didChangeAccessibilityFeatures].
57
  ui.AccessibilityFeatures get accessibilityFeatures => _accessibilityFeatures;
58
  late ui.AccessibilityFeatures _accessibilityFeatures;
59 60 61

  /// The platform is requesting that animations be disabled or simplified.
  ///
62
  /// This setting can be overridden for testing or debugging by setting
63 64 65 66
  /// [debugSemanticsDisableAnimations].
  bool get disableAnimations {
    bool value = _accessibilityFeatures.disableAnimations;
    assert(() {
67
      if (debugSemanticsDisableAnimations != null) {
68
        value = debugSemanticsDisableAnimations!;
69
      }
70 71 72 73 74
      return true;
    }());
    return value;
  }
}