hit_test.dart 1.73 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 8 9 10
import 'events.dart';

/// An object that can hit-test pointers.
abstract class HitTestable {
  void hitTest(HitTestResult result, Point position);
}
11

Adam Barth's avatar
Adam Barth committed
12
/// An object that can handle events.
13
abstract class HitTestTarget {
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

  String toString() => '$target';
29 30
}

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

Adam Barth's avatar
Adam Barth committed
36 37
  /// The list of [HitTestEntry] objects recorded during the hit test.
  ///
38 39 40
  /// 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.
41 42
  final List<HitTestEntry> path;

Adam Barth's avatar
Adam Barth committed
43 44 45
  /// 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
46 47
  /// 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
48 49
  void add(HitTestEntry entry) {
    path.add(entry);
50
  }
Ian Hickson's avatar
Ian Hickson committed
51 52

  String toString() => 'HitTestResult(${path.isEmpty ? "<empty path>" : path.join(", ")})';
53
}