geometry.dart 2.49 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 'enum_util.dart';
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
import 'find.dart';
import 'message.dart';

/// Offset types that can be requested by [GetOffset].
enum OffsetType {
  /// The top left point.
  topLeft,

  /// The top right point.
  topRight,

  /// The bottom left point.
  bottomLeft,

  /// The bottom right point.
  bottomRight,

  /// The center point.
  center,
}

EnumIndex<OffsetType> _offsetTypeIndex = EnumIndex<OffsetType>(OffsetType.values);

29
/// A Flutter Driver command that returns the [offsetType] from the RenderObject
30
/// identified by [finder].
31 32 33
///
/// The requested offset is returned in logical pixels, which can be translated
/// to device pixels via [Window.devicePixelRatio].
34 35 36 37 38
class GetOffset extends CommandWithTarget {
  /// The `finder` looks for an element to get its rect.
  GetOffset(SerializableFinder finder,  this.offsetType, { Duration timeout }) : super(finder, timeout: timeout);

  /// Deserializes this command from the value generated by [serialize].
39
  GetOffset.deserialize(Map<String, String> json)
40 41 42 43 44 45 46 47 48 49 50 51
      : offsetType = _offsetTypeIndex.lookupBySimpleName(json['offsetType']),
        super.deserialize(json);

  @override
  Map<String, String> serialize() => super.serialize()..addAll(<String, String>{
    'offsetType': _offsetTypeIndex.toSimpleName(offsetType),
  });

  /// The type of the requested offset.
  final OffsetType offsetType;

  @override
52
  String get kind => 'get_offset';
53 54 55
}

/// The result of the [GetRect] command.
56 57 58
///
/// The offset is provided in logical pixels, which can be translated
/// to device pixels via [Window.devicePixelRatio].
59 60
class GetOffsetResult extends Result {
  /// Creates a result with the offset defined by [dx] and [dy].
61
  const GetOffsetResult({ this.dx = 0.0, this.dy = 0.0});
62

63 64 65 66
  /// The x component of the offset in logical pixels.
  ///
  /// The value can be translated to device pixels via
  /// [Window.devicePixelRatio].
67 68
  final double dx;

69 70 71 72
  /// The y component of the offset in logical pixels.
  ///
  /// The value can be translated to device pixels via
  /// [Window.devicePixelRatio].
73 74 75 76 77
  final double dy;

  /// Deserializes the result from JSON.
  static GetOffsetResult fromJson(Map<String, dynamic> json) {
    return GetOffsetResult(
78 79
      dx: json['dx'] as double,
      dy: json['dy'] as double,
80 81 82 83 84 85 86 87 88
    );
  }

  @override
  Map<String, dynamic> toJson() => <String, double>{
    'dx': dx,
    'dy': dy,
  };
}