arena.dart 6.47 KB
Newer Older
Adam Barth's avatar
Adam Barth 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';

7
/// Whether the gesture was accepted or rejected.
Adam Barth's avatar
Adam Barth committed
8
enum GestureDisposition {
9
  /// This gesture was accepted as the interpretation of the user's input.
Adam Barth's avatar
Adam Barth committed
10
  accepted,
11 12 13

  /// This gesture was rejected as the interpretation  of the user's input.
  rejected,
Adam Barth's avatar
Adam Barth committed
14 15
}

16 17 18 19
/// Represents an object participating in an arena.
///
/// Receives callbacks from the GestureArena to notify the object when it wins
/// or loses a gesture negotiation. Exactly one of [acceptGesture] or
20
/// [rejectGesture] will be called for each arena this member was added to,
21 22 23
/// regardless of what caused the arena to be resolved. For example, if a
/// member resolves the arena itself, that member still receives an
/// [acceptGesture] callback.
Adam Barth's avatar
Adam Barth committed
24
abstract class GestureArenaMember {
25 26
  /// Called when this member wins the arena for the given pointer id.
  void acceptGesture(int pointer);
Adam Barth's avatar
Adam Barth committed
27

28 29
  /// Called when this member loses the arena for the given pointer id.
  void rejectGesture(int pointer);
Adam Barth's avatar
Adam Barth committed
30 31
}

Florian Loitsch's avatar
Florian Loitsch committed
32
/// An interface to information to an arena.
33 34
///
/// A given [GestureArenaMember] can have multiple entries in multiple arenas
35
/// with different pointer ids.
Adam Barth's avatar
Adam Barth committed
36
class GestureArenaEntry {
37
  GestureArenaEntry._(this._arena, this._pointer, this._member);
Adam Barth's avatar
Adam Barth committed
38

39 40
  final GestureArenaManager _arena;
  final int _pointer;
Adam Barth's avatar
Adam Barth committed
41 42 43
  final GestureArenaMember _member;

  /// Call this member to claim victory (with accepted) or admit defeat (with rejected).
44 45
  ///
  /// It's fine to attempt to resolve an arena that is already resolved.
Adam Barth's avatar
Adam Barth committed
46
  void resolve(GestureDisposition disposition) {
47
    _arena._resolve(_pointer, _member, disposition);
Adam Barth's avatar
Adam Barth committed
48 49 50
  }
}

51
class _GestureArena {
52 53
  final List<GestureArenaMember> members = new List<GestureArenaMember>();
  bool isOpen = true;
54
  bool isHeld = false;
55
  bool hasPendingSweep = false;
56

Hixie's avatar
Hixie committed
57 58
  /// If a gesture attempts to win while the arena is still open, it becomes the
  /// "eager winnner". We look for an eager winner when closing the arena to new
59
  /// participants, and if there is one, we resolve the arena in its favor at
Hixie's avatar
Hixie committed
60 61 62
  /// that time.
  GestureArenaMember eagerWinner;

63 64 65 66 67 68
  void add(GestureArenaMember member) {
    assert(isOpen);
    members.add(member);
  }
}

Adam Barth's avatar
Adam Barth committed
69
/// The first member to accept or the last member to not to reject wins.
70 71 72
///
/// See [https://flutter.io/gestures/#gesture-disambiguation] for more
/// information about the role this class plays in the gesture system.
73 74
class GestureArenaManager {
  final Map<int, _GestureArena> _arenas = new Map<int, _GestureArena>();
Adam Barth's avatar
Adam Barth committed
75

76
  /// Adds a new member (e.g., gesture recognizer) to the arena.
77 78
  GestureArenaEntry add(int pointer, GestureArenaMember member) {
    _GestureArena state = _arenas.putIfAbsent(pointer, () => new _GestureArena());
79
    state.add(member);
80
    return new GestureArenaEntry._(this, pointer, member);
Adam Barth's avatar
Adam Barth committed
81 82
  }

83 84 85
  /// Prevents new members from entering the arena.
  ///
  /// Called after the framework has finished dispatching the pointer down event.
86 87
  void close(int pointer) {
    _GestureArena state = _arenas[pointer];
88 89 90
    if (state == null)
      return;  // This arena either never existed or has been resolved.
    state.isOpen = false;
91
    _tryToResolveArena(pointer, state);
92 93
  }

94
  /// Forces resolution of the arena, giving the win to the first member.
95 96
  void sweep(int pointer) {
    _GestureArena state = _arenas[pointer];
97 98 99
    if (state == null)
      return;  // This arena either never existed or has been resolved.
    assert(!state.isOpen);
100 101
    if (state.isHeld) {
      state.hasPendingSweep = true;
102
      return;  // This arena is being held for a long-lived member
103
    }
104
    _arenas.remove(pointer);
Ian Hickson's avatar
Ian Hickson committed
105
    if (state.members.isNotEmpty) {
106
      // First member wins
107
      state.members.first.acceptGesture(pointer);
108 109
      // Give all the other members the bad news
      for (int i = 1; i < state.members.length; i++)
110
        state.members[i].rejectGesture(pointer);
111 112 113
    }
  }

Florian Loitsch's avatar
Florian Loitsch committed
114
  /// Prevents the arena from being swept.
115 116
  void hold(int pointer) {
    _GestureArena state = _arenas[pointer];
117 118 119 120 121
    if (state == null)
      return;  // This arena either never existed or has been resolved.
    state.isHeld = true;
  }

Florian Loitsch's avatar
Florian Loitsch committed
122 123
  /// Releases a hold, allowing the arena to be swept.
  ///
124
  /// If a sweep was attempted on a held arena, the sweep will be done
Florian Loitsch's avatar
Florian Loitsch committed
125
  /// on release.
126 127
  void release(int pointer) {
    _GestureArena state = _arenas[pointer];
128 129 130 131
    if (state == null)
      return;  // This arena either never existed or has been resolved.
    state.isHeld = false;
    if (state.hasPendingSweep)
132
      sweep(pointer);
133 134
  }

135 136 137 138 139 140 141 142 143 144 145
  void _resolveByDefault(int pointer, _GestureArena state) {
    if (!_arenas.containsKey(pointer))
      return;  // Already resolved earlier.
    assert(_arenas[pointer] == state);
    assert(!state.isOpen);
    final List<GestureArenaMember> members = state.members;
    assert(members.length == 1);
    _arenas.remove(pointer);
    state.members.first.acceptGesture(pointer);
  }

146 147
  void _tryToResolveArena(int pointer, _GestureArena state) {
    assert(_arenas[pointer] == state);
148 149
    assert(!state.isOpen);
    if (state.members.length == 1) {
150
      scheduleMicrotask(() => _resolveByDefault(pointer, state));
151
    } else if (state.members.isEmpty) {
152
      _arenas.remove(pointer);
Hixie's avatar
Hixie committed
153
    } else if (state.eagerWinner != null) {
154
      _resolveInFavorOf(pointer, state, state.eagerWinner);
155 156 157
    }
  }

158 159
  void _resolve(int pointer, GestureArenaMember member, GestureDisposition disposition) {
    _GestureArena state = _arenas[pointer];
160
    if (state == null)
161
      return;  // This arena has already resolved.
162
    assert(state.members.contains(member));
Adam Barth's avatar
Adam Barth committed
163
    if (disposition == GestureDisposition.rejected) {
164
      state.members.remove(member);
165
      member.rejectGesture(pointer);
Hixie's avatar
Hixie committed
166
      if (!state.isOpen)
167
        _tryToResolveArena(pointer, state);
Adam Barth's avatar
Adam Barth committed
168 169
    } else {
      assert(disposition == GestureDisposition.accepted);
Hixie's avatar
Hixie committed
170
      if (state.isOpen) {
Ian Hickson's avatar
Ian Hickson committed
171
        state.eagerWinner ??= member;
Hixie's avatar
Hixie committed
172
      } else {
173
        _resolveInFavorOf(pointer, state, member);
Adam Barth's avatar
Adam Barth committed
174 175 176
      }
    }
  }
Hixie's avatar
Hixie committed
177

178 179
  void _resolveInFavorOf(int pointer, _GestureArena state, GestureArenaMember member) {
    assert(state == _arenas[pointer]);
Hixie's avatar
Hixie committed
180 181 182
    assert(state != null);
    assert(state.eagerWinner == null || state.eagerWinner == member);
    assert(!state.isOpen);
183
    _arenas.remove(pointer);
Hixie's avatar
Hixie committed
184 185
    for (GestureArenaMember rejectedMember in state.members) {
      if (rejectedMember != member)
186
        rejectedMember.rejectGesture(pointer);
Hixie's avatar
Hixie committed
187
    }
188
    member.acceptGesture(pointer);
Hixie's avatar
Hixie committed
189
  }
Ian Hickson's avatar
Ian Hickson committed
190
}