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

5 6
import 'dart:async';
import 'dart:collection';
Ian Hickson's avatar
Ian Hickson committed
7
import 'dart:typed_data';
8
import 'dart:ui' as ui show window;
Ian Hickson's avatar
Ian Hickson committed
9

10
import 'package:flutter/foundation.dart';
Ian Hickson's avatar
Ian Hickson committed
11 12 13 14 15 16 17 18 19 20
import 'package:mojo/bindings.dart' as mojo_bindings;
import 'package:mojo/core.dart' as mojo_core;
import 'package:sky_services/pointer/pointer.mojom.dart';

import 'arena.dart';
import 'converter.dart';
import 'events.dart';
import 'hit_test.dart';
import 'pointer_router.dart';

21
/// A binding for the gesture subsystem.
22
abstract class GestureBinding extends BindingBase implements HitTestable, HitTestDispatcher, HitTestTarget {
Ian Hickson's avatar
Ian Hickson committed
23

24
  @override
Ian Hickson's avatar
Ian Hickson committed
25 26 27 28 29 30
  void initInstances() {
    super.initInstances();
    _instance = this;
    ui.window.onPointerPacket = _handlePointerPacket;
  }

31
  /// The singleton instance of this object.
32 33
  static GestureBinding get instance => _instance;
  static GestureBinding _instance;
Ian Hickson's avatar
Ian Hickson committed
34 35 36 37 38 39 40 41 42

  void _handlePointerPacket(ByteData serializedPacket) {
    final mojo_bindings.Message message = new mojo_bindings.Message(
      serializedPacket,
      <mojo_core.MojoHandle>[],
      serializedPacket.lengthInBytes,
      0
    );
    final PointerPacket packet = PointerPacket.deserialize(message);
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
    _pendingPointerEvents.addAll(PointerEventConverter.expand(packet.pointers));
    _flushPointerEventQueue();
  }

  final Queue<PointerEvent> _pendingPointerEvents = new Queue<PointerEvent>();

  void _flushPointerEventQueue() {
    while (_pendingPointerEvents.isNotEmpty)
      _handlePointerEvent(_pendingPointerEvents.removeFirst());
  }

  /// Dispatch a [PointerCancelEvent] for the given pointer soon.
  ///
  /// The pointer event will be dispatch before the next pointer event and
  /// before the end of the microtask but not within this function call.
  void cancelPointer(int pointer) {
    if (_pendingPointerEvents.isEmpty)
      scheduleMicrotask(_flushPointerEventQueue);
    _pendingPointerEvents.addFirst(new PointerCancelEvent(pointer: pointer));
Ian Hickson's avatar
Ian Hickson committed
62 63 64 65 66 67 68
  }

  /// A router that routes all pointer events received from the engine.
  final PointerRouter pointerRouter = new PointerRouter();

  /// The gesture arenas used for disambiguating the meaning of sequences of
  /// pointer events.
69
  final GestureArenaManager gestureArena = new GestureArenaManager();
Ian Hickson's avatar
Ian Hickson committed
70 71 72 73 74 75 76 77

  /// State for all pointers which are currently down.
  ///
  /// The state of hovering pointers is not tracked because that would require
  /// hit-testing on every frame.
  Map<int, HitTestResult> _hitTests = <int, HitTestResult>{};

  void _handlePointerEvent(PointerEvent event) {
78
    HitTestResult result;
Ian Hickson's avatar
Ian Hickson committed
79 80
    if (event is PointerDownEvent) {
      assert(!_hitTests.containsKey(event.pointer));
81
      result = new HitTestResult();
Ian Hickson's avatar
Ian Hickson committed
82 83
      hitTest(result, event.position);
      _hitTests[event.pointer] = result;
84 85 86 87 88 89
    } else if (event is PointerUpEvent || event is PointerCancelEvent) {
      result = _hitTests.remove(event.pointer);
    } else if (event.down) {
      result = _hitTests[event.pointer];
    } else {
      return;  // We currently ignore add, remove, and hover move events.
Ian Hickson's avatar
Ian Hickson committed
90
    }
91 92
    if (result != null)
      dispatchEvent(event, result);
Ian Hickson's avatar
Ian Hickson committed
93 94 95
  }

  /// Determine which [HitTestTarget] objects are located at a given position.
96
  @override // from HitTestable
Ian Hickson's avatar
Ian Hickson committed
97 98 99 100
  void hitTest(HitTestResult result, Point position) {
    result.add(new HitTestEntry(this));
  }

101 102 103 104 105 106
  /// Dispatch an event to a hit test result's path.
  ///
  /// This sends the given event to every [HitTestTarget] in the entries
  /// of the given [HitTestResult], and catches exceptions that any of
  /// the handlers might throw. The `result` argument must not be null.
  @override // from HitTestDispatcher
Ian Hickson's avatar
Ian Hickson committed
107 108
  void dispatchEvent(PointerEvent event, HitTestResult result) {
    assert(result != null);
109 110 111 112
    for (HitTestEntry entry in result.path) {
      try {
        entry.target.handleEvent(event, entry);
      } catch (exception, stack) {
113 114 115 116 117 118 119 120 121 122 123 124 125 126
        FlutterError.reportError(new FlutterErrorDetailsForPointerEventDispatcher(
          exception: exception,
          stack: stack,
          library: 'gesture library',
          context: 'while dispatching a pointer event',
          event: event,
          hitTestEntry: entry,
          informationCollector: (StringBuffer information) {
            information.writeln('Event:');
            information.writeln('  $event');
            information.writeln('Target:');
            information.write('  ${entry.target}');
          }
        ));
127 128
      }
    }
Ian Hickson's avatar
Ian Hickson committed
129 130
  }

131
  @override // from HitTestTarget
Ian Hickson's avatar
Ian Hickson committed
132 133 134 135 136 137 138 139 140
  void handleEvent(PointerEvent event, HitTestEntry entry) {
    pointerRouter.route(event);
    if (event is PointerDownEvent) {
      gestureArena.close(event.pointer);
    } else if (event is PointerUpEvent) {
      gestureArena.sweep(event.pointer);
    }
  }
}
141 142

/// Variant of [FlutterErrorDetails] with extra fields for the gesture
143
/// library's binding's pointer event dispatcher ([GestureBinding.dispatchEvent]).
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
///
/// See also [FlutterErrorDetailsForPointerRouter], which is also used by the
/// gesture library.
class FlutterErrorDetailsForPointerEventDispatcher extends FlutterErrorDetails {
  /// Creates a [FlutterErrorDetailsForPointerEventDispatcher] object with the given
  /// arguments setting the object's properties.
  ///
  /// The gesture library calls this constructor when catching an exception
  /// that will subsequently be reported using [FlutterError.onError].
  const FlutterErrorDetailsForPointerEventDispatcher({
    dynamic exception,
    StackTrace stack,
    String library,
    String context,
    this.event,
    this.hitTestEntry,
160
    InformationCollector informationCollector,
161
    bool silent: false
162 163 164 165 166 167 168 169 170 171 172 173 174 175
  }) : super(
    exception: exception,
    stack: stack,
    library: library,
    context: context,
    informationCollector: informationCollector,
    silent: silent
  );

  /// The pointer event that was being routed when the exception was raised.
  final PointerEvent event;

  /// The hit test result entry for the object whose handleEvent method threw
  /// the exception.
176
  ///
177 178 179 180
  /// The target object itself is given by the [HitTestEntry.target] property of
  /// the hitTestEntry object.
  final HitTestEntry hitTestEntry;
}