hooks.dart 7.3 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
String _decodeUTF8(ByteData message) {
8
  return message != null ? utf8.decoder.convert(message.buffer.asUint8List()) : null;
9 10 11
}

dynamic _decodeJSON(String message) {
12
  return message != null ? json.decode(message) : null;
13 14
}

A
Adam Barth 已提交
15 16 17
void _updateWindowMetrics(double devicePixelRatio,
                          double width,
                          double height,
18 19 20 21 22 23 24 25
                          double paddingTop,
                          double paddingRight,
                          double paddingBottom,
                          double paddingLeft,
                          double viewInsetTop,
                          double viewInsetRight,
                          double viewInsetBottom,
                          double viewInsetLeft) {
A
Adam Barth 已提交
26 27
  window
    .._devicePixelRatio = devicePixelRatio
A
Adam Barth 已提交
28
    .._physicalSize = new Size(width, height)
29
    .._padding = new WindowPadding._(
30 31 32 33
        top: paddingTop,
        right: paddingRight,
        bottom: paddingBottom,
        left: paddingLeft)
34
    .._viewInsets = new WindowPadding._(
35 36 37 38
        top: viewInsetTop,
        right: viewInsetRight,
        bottom: viewInsetBottom,
        left: viewInsetLeft);
Y
Yegor 已提交
39
  _invoke(window.onMetricsChanged, window._onMetricsChangedZone);
A
Adam Barth 已提交
40 41
}

42
typedef _LocaleClosure = String Function();
43 44 45

String _localeClosure() => window._locale.toString();

46
_LocaleClosure _getLocaleClosure() => _localeClosure;
47

48 49
void _updateLocale(String languageCode, String countryCode) {
  window._locale = new Locale(languageCode, countryCode);
Y
Yegor 已提交
50
  _invoke(window.onLocaleChanged, window._onLocaleChangedZone);
51 52
}

53 54
void _updateUserSettingsData(String jsonData) {
  final Map<String, dynamic> data = json.decode(jsonData);
55 56 57 58
  _updateTextScaleFactor(data['textScaleFactor'].toDouble());
  _updateAlwaysUse24HourFormat(data['alwaysUse24HourFormat']);
}

59 60 61 62 63
void _updateTextScaleFactor(double textScaleFactor) {
  window._textScaleFactor = textScaleFactor;
  _invoke(window.onTextScaleFactorChanged, window._onTextScaleFactorChangedZone);
}

64 65 66 67
void _updateAlwaysUse24HourFormat(bool alwaysUse24HourFormat) {
  window._alwaysUse24HourFormat = alwaysUse24HourFormat;
}

68 69
void _updateSemanticsEnabled(bool enabled) {
  window._semanticsEnabled = enabled;
Y
Yegor 已提交
70
  _invoke(window.onSemanticsEnabledChanged, window._onSemanticsEnabledChangedZone);
71 72
}

73 74 75 76 77 78
void _updateAccessibilityFeatures(int values) {
  final AccessibilityFeatures newFeatures = new AccessibilityFeatures._(values);
  if (newFeatures == window._accessibilityFeatures)
    return;
  window._accessibilityFeatures = newFeatures;
  _invoke(window.onAccessibilityFeaturesChanged, window._onAccessibilityFlagsChangedZone);
79 80
}

A
Adam Barth 已提交
81 82
void _dispatchPlatformMessage(String name, ByteData data, int responseId) {
  if (window.onPlatformMessage != null) {
Y
Yegor 已提交
83 84 85 86 87 88 89 90 91
    _invoke3<String, ByteData, PlatformMessageResponseCallback>(
      window.onPlatformMessage,
      window._onPlatformMessageZone,
      name,
      data,
      (ByteData responseData) {
        window._respondToPlatformMessage(responseId, responseData);
      },
    );
A
Adam Barth 已提交
92 93 94 95 96
  } else {
    window._respondToPlatformMessage(responseId, null);
  }
}

97 98
void _dispatchPointerDataPacket(ByteData packet) {
  if (window.onPointerDataPacket != null)
Y
Yegor 已提交
99
    _invoke1<PointerDataPacket>(window.onPointerDataPacket, window._onPointerDataPacketZone, _unpackPointerDataPacket(packet));
100 101
}

102 103
void _dispatchSemanticsAction(int id, int action, ByteData args) {
  _invoke3<int, SemanticsAction, ByteData>(
Y
Yegor 已提交
104 105 106 107
    window.onSemanticsAction,
    window._onSemanticsActionZone,
    id,
    SemanticsAction.values[action],
108
    args,
Y
Yegor 已提交
109
  );
110 111
}

A
Adam Barth 已提交
112
void _beginFrame(int microseconds) {
Y
Yegor 已提交
113
  _invoke1<Duration>(window.onBeginFrame, window._onBeginFrameZone, new Duration(microseconds: microseconds));
A
Adam Barth 已提交
114
}
115

116
void _drawFrame() {
Y
Yegor 已提交
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
  _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 {
144
    zone.runUnaryGuarded<A>(callback, arg);
Y
Yegor 已提交
145 146 147 148 149 150 151 152 153 154 155 156 157
  }
}

/// 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 {
158
    zone.runBinaryGuarded<A1, A2>(callback, arg1, arg2);
Y
Yegor 已提交
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
  }
}

/// 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);
    });
  }
176 177
}

178 179 180 181
// If this value changes, update the encoding code in the following files:
//
//  * pointer_data.cc
//  * FlutterView.java
182 183 184
const int _kPointerDataFieldCount = 19;

PointerDataPacket _unpackPointerDataPacket(ByteData packet) {
185
  const int kStride = Int64List.bytesPerElement;
186 187 188
  const int kBytesPerPointerData = _kPointerDataFieldCount * kStride;
  final int length = packet.lengthInBytes ~/ kBytesPerPointerData;
  assert(length * kBytesPerPointerData == packet.lengthInBytes);
189
  final List<PointerData> data = new List<PointerData>(length);
190 191
  for (int i = 0; i < length; ++i) {
    int offset = i * _kPointerDataFieldCount;
192
    data[i] = new PointerData(
193 194 195
      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)],
196
      device: packet.getInt64(kStride * offset++, _kFakeHostEndian),
197 198 199 200 201 202 203 204 205 206 207 208 209 210
      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 已提交
211
      tilt: packet.getFloat64(kStride * offset++, _kFakeHostEndian)
212 213 214
    );
    assert(offset == (i + 1) * _kPointerDataFieldCount);
  }
215
  return new PointerDataPacket(data: data);
216
}