hooks.dart 5.9 KB
Newer Older
A
Adam Barth 已提交
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.

A
Adam Barth 已提交
5
part of dart.ui;
A
Adam Barth 已提交
6

7 8 9 10 11 12 13 14
String _decodeUTF8(ByteData message) {
  return message != null ? UTF8.decoder.convert(message.buffer.asUint8List()) : null;
}

dynamic _decodeJSON(String message) {
  return message != null ? JSON.decode(message) : null;
}

A
Adam Barth 已提交
15 16 17 18 19 20 21 22 23
void _updateWindowMetrics(double devicePixelRatio,
                          double width,
                          double height,
                          double top,
                          double right,
                          double bottom,
                          double left) {
  window
    .._devicePixelRatio = devicePixelRatio
A
Adam Barth 已提交
24
    .._physicalSize = new Size(width, height)
25
    .._padding = new WindowPadding._(
A
Adam Barth 已提交
26
      top: top, right: right, bottom: bottom, left: left);
Y
Yegor 已提交
27
  _invoke(window.onMetricsChanged, window._onMetricsChangedZone);
A
Adam Barth 已提交
28 29
}

30 31
void _updateLocale(String languageCode, String countryCode) {
  window._locale = new Locale(languageCode, countryCode);
Y
Yegor 已提交
32
  _invoke(window.onLocaleChanged, window._onLocaleChangedZone);
33 34
}

35 36
void _updateSemanticsEnabled(bool enabled) {
  window._semanticsEnabled = enabled;
Y
Yegor 已提交
37
  _invoke(window.onSemanticsEnabledChanged, window._onSemanticsEnabledChangedZone);
38 39
}

A
Adam Barth 已提交
40 41
void _dispatchPlatformMessage(String name, ByteData data, int responseId) {
  if (window.onPlatformMessage != null) {
Y
Yegor 已提交
42 43 44 45 46 47 48 49 50
    _invoke3<String, ByteData, PlatformMessageResponseCallback>(
      window.onPlatformMessage,
      window._onPlatformMessageZone,
      name,
      data,
      (ByteData responseData) {
        window._respondToPlatformMessage(responseId, responseData);
      },
    );
A
Adam Barth 已提交
51 52 53 54 55
  } else {
    window._respondToPlatformMessage(responseId, null);
  }
}

56 57
void _dispatchPointerDataPacket(ByteData packet) {
  if (window.onPointerDataPacket != null)
Y
Yegor 已提交
58
    _invoke1<PointerDataPacket>(window.onPointerDataPacket, window._onPointerDataPacketZone, _unpackPointerDataPacket(packet));
59 60
}

61
void _dispatchSemanticsAction(int id, int action) {
Y
Yegor 已提交
62 63 64 65 66 67
  _invoke2<int, SemanticsAction>(
    window.onSemanticsAction,
    window._onSemanticsActionZone,
    id,
    SemanticsAction.values[action],
  );
68 69
}

A
Adam Barth 已提交
70
void _beginFrame(int microseconds) {
Y
Yegor 已提交
71
  _invoke1<Duration>(window.onBeginFrame, window._onBeginFrameZone, new Duration(microseconds: microseconds));
A
Adam Barth 已提交
72
}
73

74
void _drawFrame() {
Y
Yegor 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
  _invoke(window.onDrawFrame, window._onDrawFrameZone);
}

/// Invokes [callback] inside the given [zone].
void _invoke(void callback(), Zone zone) {
  if (callback == null)
    return;

  assert(zone != null);

  if (identical(zone, Zone.current)) {
    callback();
  } else {
    zone.runGuarded(callback);
  }
}

/// Invokes [callback] inside the given [zone] passing it [arg].
void _invoke1<A>(void callback(A a), Zone zone, A arg) {
  if (callback == null)
    return;

  assert(zone != null);

  if (identical(zone, Zone.current)) {
    callback(arg);
  } else {
    zone.runUnaryGuarded<Null, A>(callback, arg);
  }
}

/// Invokes [callback] inside the given [zone] passing it [arg1] and [arg2].
void _invoke2<A1, A2>(void callback(A1 a1, A2 a2), Zone zone, A1 arg1, A2 arg2) {
  if (callback == null)
    return;

  assert(zone != null);

  if (identical(zone, Zone.current)) {
    callback(arg1, arg2);
  } else {
    zone.runBinaryGuarded<Null, A1, A2>(callback, arg1, arg2);
  }
}

/// Invokes [callback] inside the given [zone] passing it [arg1], [arg2] and [arg3].
void _invoke3<A1, A2, A3>(void callback(A1 a1, A2 a2, A3 a3), Zone zone, A1 arg1, A2 arg2, A3 arg3) {
  if (callback == null)
    return;

  assert(zone != null);

  if (identical(zone, Zone.current)) {
    callback(arg1, arg2, arg3);
  } else {
    zone.runGuarded(() {
      callback(arg1, arg2, arg3);
    });
  }
134 135
}

136 137 138 139
// If this value changes, update the encoding code in the following files:
//
//  * pointer_data.cc
//  * FlutterView.java
140 141 142 143 144 145 146
const int _kPointerDataFieldCount = 19;

PointerDataPacket _unpackPointerDataPacket(ByteData packet) {
  const int kStride = Int64List.BYTES_PER_ELEMENT;
  const int kBytesPerPointerData = _kPointerDataFieldCount * kStride;
  final int length = packet.lengthInBytes ~/ kBytesPerPointerData;
  assert(length * kBytesPerPointerData == packet.lengthInBytes);
147
  List<PointerData> data = new List<PointerData>(length);
148 149
  for (int i = 0; i < length; ++i) {
    int offset = i * _kPointerDataFieldCount;
150
    data[i] = new PointerData(
151 152 153
      timeStamp: new Duration(microseconds: packet.getInt64(kStride * offset++, _kFakeHostEndian)),
      change: PointerChange.values[packet.getInt64(kStride * offset++, _kFakeHostEndian)],
      kind: PointerDeviceKind.values[packet.getInt64(kStride * offset++, _kFakeHostEndian)],
154
      device: packet.getInt64(kStride * offset++, _kFakeHostEndian),
155 156 157 158 159 160 161 162 163 164 165 166 167 168
      physicalX: packet.getFloat64(kStride * offset++, _kFakeHostEndian),
      physicalY: packet.getFloat64(kStride * offset++, _kFakeHostEndian),
      buttons: packet.getInt64(kStride * offset++, _kFakeHostEndian),
      obscured: packet.getInt64(kStride * offset++, _kFakeHostEndian) != 0,
      pressure: packet.getFloat64(kStride * offset++, _kFakeHostEndian),
      pressureMin: packet.getFloat64(kStride * offset++, _kFakeHostEndian),
      pressureMax: packet.getFloat64(kStride * offset++, _kFakeHostEndian),
      distance: packet.getFloat64(kStride * offset++, _kFakeHostEndian),
      distanceMax: packet.getFloat64(kStride * offset++, _kFakeHostEndian),
      radiusMajor: packet.getFloat64(kStride * offset++, _kFakeHostEndian),
      radiusMinor: packet.getFloat64(kStride * offset++, _kFakeHostEndian),
      radiusMin: packet.getFloat64(kStride * offset++, _kFakeHostEndian),
      radiusMax: packet.getFloat64(kStride * offset++, _kFakeHostEndian),
      orientation: packet.getFloat64(kStride * offset++, _kFakeHostEndian),
A
Adam Barth 已提交
169
      tilt: packet.getFloat64(kStride * offset++, _kFakeHostEndian)
170 171 172
    );
    assert(offset == (i + 1) * _kPointerDataFieldCount);
  }
173
  return new PointerDataPacket(data: data);
174
}