hit_test.dart 1.82 KB
Newer Older
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.

Ian Hickson's avatar
Ian Hickson committed
5 6 7
import 'events.dart';

/// An object that can hit-test pointers.
Ian Hickson's avatar
Ian Hickson committed
8
abstract class HitTestable { // ignore: one_member_abstracts
Ian Hickson's avatar
Ian Hickson committed
9 10
  void hitTest(HitTestResult result, Point position);
}
11

Adam Barth's avatar
Adam Barth committed
12
/// An object that can handle events.
Ian Hickson's avatar
Ian Hickson committed
13
abstract class HitTestTarget { // ignore: one_member_abstracts
Adam Barth's avatar
Adam Barth committed
14
  /// Override this function to receive events.
Ian Hickson's avatar
Ian Hickson committed
15
  void handleEvent(PointerEvent event, HitTestEntry entry);
16 17
}

Adam Barth's avatar
Adam Barth committed
18 19 20 21
/// Data collected during a hit test about a specific [HitTestTarget].
///
/// Subclass this object to pass additional information from the hit test phase
/// to the event propagation phase.
22 23
class HitTestEntry {
  const HitTestEntry(this.target);
Adam Barth's avatar
Adam Barth committed
24 25

  /// The [HitTestTarget] encountered during the hit test.
26
  final HitTestTarget target;
Ian Hickson's avatar
Ian Hickson committed
27

28
  @override
Ian Hickson's avatar
Ian Hickson committed
29
  String toString() => '$target';
30 31
}

Adam Barth's avatar
Adam Barth committed
32
/// The result of performing a hit test.
33
class HitTestResult {
34
  HitTestResult({ List<HitTestEntry> path })
Ian Hickson's avatar
Ian Hickson committed
35
    : path = path ?? <HitTestEntry>[];
36

Adam Barth's avatar
Adam Barth committed
37 38
  /// The list of [HitTestEntry] objects recorded during the hit test.
  ///
39 40 41
  /// The first entry in the path is the most specific, typically the one at
  /// the leaf of tree being hit tested. Event propagation starts with the most
  /// specific (i.e., first) entry and proceeds in order through the path.
42 43
  final List<HitTestEntry> path;

Adam Barth's avatar
Adam Barth committed
44 45 46
  /// Add a [HitTestEntry] to the path.
  ///
  /// The new entry is added at the end of the path, which means entries should
Ian Hickson's avatar
Ian Hickson committed
47 48
  /// be added in order from most specific to least specific, typically during an
  /// upward walk of the tree being hit tested.
Adam Barth's avatar
Adam Barth committed
49 50
  void add(HitTestEntry entry) {
    path.add(entry);
51
  }
Ian Hickson's avatar
Ian Hickson committed
52

53
  @override
Ian Hickson's avatar
Ian Hickson committed
54
  String toString() => 'HitTestResult(${path.isEmpty ? "<empty path>" : path.join(", ")})';
55
}