window.dart 10.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

I
Ian Hickson 已提交
7
/// Signature of callbacks that have no arguments and return no data.
A
Adam Barth 已提交
8
typedef void VoidCallback();
I
Ian Hickson 已提交
9 10 11 12

/// Signature for [Window.onBeginFrame].
typedef void FrameCallback(Duration duration);

13 14 15
/// Signature for [Window.onPointerDataPacket].
typedef void PointerDataPacketCallback(PointerDataPacket packet);

16 17 18
/// Signature for [Window.onSemanticsAction].
typedef void SemanticsActionCallback(int id, SemanticsAction action);

A
Adam Barth 已提交
19 20 21 22
/// Signature for responses to platform messages.
///
/// Used as a parameter to [Window.sendPlatformMessage] and
/// [Window.onPlatformMessage].
23 24
typedef void PlatformMessageResponseCallback(ByteData data);

A
Adam Barth 已提交
25 26 27
/// Signature for [Window.onPlatformMessage].
typedef void PlatformMessageCallback(String name, ByteData data, PlatformMessageResponseCallback callback);

28 29
/// States that an application can be in.
enum AppLifecycleState {
30 31
  /// The application is not currently visible to the user. When the
  /// application is in this state, the engine will not call the
I
Ian Hickson 已提交
32
  /// [Window.onBeginFrame] and [Window.onDrawFrame] callbacks.
33
  paused,
34 35

  /// The application is visible to the user.
36 37
  resumed,
}
A
Adam Barth 已提交
38

39 40 41 42 43
/// A representation of distances for each of the four edges of a rectangle,
/// used to encode the padding that applications should place around their user
/// interface, as exposed by [Window.padding].
///
/// For a generic class that represents distances around a rectangle, see the
I
Ian Hickson 已提交
44
/// [EdgeInsets] class.
45
class WindowPadding {
46
  const WindowPadding._({ this.left, this.top, this.right, this.bottom });
A
Adam Barth 已提交
47

A
Adam Barth 已提交
48
  /// The distance from the left edge to the first unobscured pixel, in physical pixels.
49
  final double left;
A
Adam Barth 已提交
50 51

  /// The distance from the top edge to the first unobscured pixel, in physical pixels.
A
Adam Barth 已提交
52
  final double top;
A
Adam Barth 已提交
53 54

  /// The distance from the right edge to the first unobscured pixel, in physical pixels.
A
Adam Barth 已提交
55
  final double right;
A
Adam Barth 已提交
56 57

  /// The distance from the bottom edge to the first unobscured pixel, in physical pixels.
A
Adam Barth 已提交
58
  final double bottom;
59

A
Adam Barth 已提交
60
  /// A window padding that has zeros for each edge.
61
  static const WindowPadding zero = const WindowPadding._(left: 0.0, top: 0.0, right: 0.0, bottom: 0.0);
A
Adam Barth 已提交
62 63
}

64 65 66
/// An identifier used to select a user's language and formatting preferences,
/// consisting of a language and a country. This is a subset of locale
/// identifiers as defined by BCP 47.
67
class Locale {
H
Hixie 已提交
68 69 70 71 72 73 74
  /// Creates a new Locale object. The first argument is the
  /// primary language subtag, the second is the region subtag.
  ///
  /// For example:
  ///
  ///     const Locale swissFrench = const Locale('fr', 'CH');
  ///     const Locale canadianFrench = const Locale('fr', 'CA');
75
  const Locale(this.languageCode, this.countryCode);
76

77
  /// The primary language subtag for the locale.
78
  final String languageCode;
79 80

  /// The region subtag for the locale.
81 82
  final String countryCode;

83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
  bool operator ==(dynamic other) {
    if (identical(this, other))
      return true;
    if (other is! Locale)
      return false;
    final Locale typedOther = other;
    return languageCode == typedOther.languageCode
        && countryCode == typedOther.countryCode;
  }

  int get hashCode {
    int result = 373;
    result = 37 * result + languageCode.hashCode;
    result = 37 * result + countryCode.hashCode;
    return result;
  }

100 101 102
  String toString() => '${languageCode}_$countryCode';
}

103
/// The most basic interface to the host operating system's user interface.
H
Hixie 已提交
104 105 106
///
/// There is a single Window instance in the system, which you can
/// obtain from the [window] property.
A
Adam Barth 已提交
107 108 109
class Window {
  Window._();

110 111 112
  /// The number of device pixels for each logical pixel. This number might not
  /// be a power of two. Indeed, it might not even be an integer. For example,
  /// the Nexus 6 has a device pixel ratio of 3.5.
113 114 115 116 117 118 119 120 121 122
  ///
  /// Device pixels are also referred to as physical pixels. Logical pixels are
  /// also referred to as device-independent or resolution-independent pixels.
  ///
  /// By definition, there are roughly 38 logical pixels per centimeter, or
  /// about 96 logical pixels per inch, of the physical display. The value
  /// returned by [devicePixelRatio] is ultimately obtained either from the
  /// hardware itself, the device drivers, or a hard-coded value stored in the
  /// operating system or firmware, and may be inaccurate, sometimes by a
  /// significant margin.
A
Adam Barth 已提交
123
  double get devicePixelRatio => _devicePixelRatio;
124
  double _devicePixelRatio = 1.0;
A
Adam Barth 已提交
125

126
  /// The dimensions of the rectangle into which the application will be drawn,
A
Adam Barth 已提交
127 128 129 130 131 132 133 134 135
  /// in physical pixels.
  Size get physicalSize => _physicalSize;
  Size _physicalSize = Size.zero;

  /// The number of physical pixels on each side of the display rectangle into
  /// which the application can render, but over which the operating system will
  /// likely place system UI (such as the Android system notification area), or
  /// which might be rendered outside of the physical display (e.g. overscan
  /// regions on television screens).
136
  WindowPadding get padding => _padding;
137
  WindowPadding _padding = WindowPadding.zero;
A
Adam Barth 已提交
138

A
Adam Barth 已提交
139
  /// A callback that is invoked whenever the [devicePixelRatio],
140 141 142
  /// [physicalSize], or [padding] values change, for example when the device is
  /// rotated or when the application is resized (e.g. when showing applications
  /// side-by-side on Android).
143 144
  VoidCallback onMetricsChanged;

145 146 147 148 149 150
  /// The system-reported locale.
  ///
  /// This establishes the language and formatting conventions that application
  /// should, if possible, use to render their user interface.
  ///
  /// The [onLocaleChanged] callback is called whenever this value changes.
151 152 153
  Locale get locale => _locale;
  Locale _locale;

154 155 156 157 158
  /// A callback that is invoked whenever [locale] changes value.
  VoidCallback onLocaleChanged;

  /// A callback that is invoked to notify the application that it is an
  /// appropriate time to provide a scene using the [SceneBuilder] API and the
A
Adam Barth 已提交
159 160
  /// [render] method. When possible, this is driven by the hardware VSync
  /// signal. This is only called if [scheduleFrame] has been called since the
161
  /// last time this callback was invoked.
I
Ian Hickson 已提交
162 163 164 165
  ///
  /// The [onDrawFrame] callback is invoked immediately after [onBeginFrame],
  /// after draining any microtasks (e.g. completions of any [Future]s) queued
  /// by the [onBeginFrame] handler.
I
Ian Hickson 已提交
166
  FrameCallback onBeginFrame;
167

I
Ian Hickson 已提交
168 169
  /// A callback that is invoked for each frame after [onBeginFrame] has
  /// completed and after the microtask queue has been drained. This can be
170
  /// used to implement a second phase of frame rendering that happens
I
Ian Hickson 已提交
171
  /// after any deferred work queued by the [onBeginFrame] phase.
172 173
  VoidCallback onDrawFrame;

174 175 176
  /// A callback that is invoked when pointer data is available.
  PointerDataPacketCallback onPointerDataPacket;

177 178
  /// The route or path that the operating system requested when the application
  /// was launched.
H
Hixie 已提交
179 180
  String get defaultRouteName => _defaultRouteName;
  String _defaultRouteName;
181 182

  /// Requests that, at the next appropriate opportunity, the [onBeginFrame]
I
Ian Hickson 已提交
183
  /// and [onDrawFrame] callbacks be invoked.
A
Adam Barth 已提交
184
  void scheduleFrame() native "Window_scheduleFrame";
185 186

  /// Updates the application's rendering on the GPU with the newly provided
187
  /// [Scene]. This function must be called within the scope of the
I
Ian Hickson 已提交
188 189 190 191
  /// [onBeginFrame] or [onDrawFrame] callbacks being invoked. If this function
  /// is called a second time during a single [onBeginFrame]/[onDrawFrame]
  /// callback sequence or called outside the scope of those callbacks, the call
  /// will be ignored.
H
Hixie 已提交
192
  ///
I
Ian Hickson 已提交
193 194 195 196 197
  /// To record graphical operations, first create a [PictureRecorder], then
  /// construct a [Canvas], passing that [PictureRecorder] to its constructor.
  /// After issuing all the graphical operations, call the
  /// [PictureRecorder.endRecording] function on the [PictureRecorder] to obtain
  /// the final [Picture] that represents the issued graphical operations.
198
  ///
H
Hixie 已提交
199
  /// Next, create a [SceneBuilder], and add the [Picture] to it using
I
Ian Hickson 已提交
200 201 202
  /// [SceneBuilder.addPicture]. With the [SceneBuilder.build] method you can
  /// then obtain a [Scene] object, which you can display to the user via this
  /// [render] function.
203
  void render(Scene scene) native "Window_render";
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230

  /// Whether the user has requested that [updateSemantics] be called when
  /// the semantic contents of window changes.
  ///
  /// The [onSemanticsEnabledChanged] callback is called whenever this value
  /// changes.
  bool get semanticsEnabled => _semanticsEnabled;
  bool _semanticsEnabled = false;

  /// A callback that is invoked when the value of [semanticsEnabled] changes.
  VoidCallback onSemanticsEnabledChanged;

  /// A callback that is invoked whenever the user requests an action to be
  /// performed.
  ///
  /// This callback is used when the user expresses the action they wish to
  /// perform based on the semantics supplied by [updateSemantics].
  SemanticsActionCallback onSemanticsAction;

  /// Change the retained semantics data about this window.
  ///
  /// If [semanticsEnabled] is true, the user has requested that this funciton
  /// be called whenever the semantic content of this window changes.
  ///
  /// In either case, this function disposes the given update, which means the
  /// semantics update cannot be used further.
  void updateSemantics(SemanticsUpdate update) native "Window_updateSemantics";
231

A
Adam Barth 已提交
232 233 234 235 236 237
  /// Sends a message to a platform-specific plugin.
  ///
  /// The `name` parameter determines which plugin receives the message. The
  /// `data` parameter contains the message payload and is typically UTF-8
  /// encoded JSON but can be arbitrary data. If the plugin replies to the
  /// message, `callback` will be called with the response.
A
Adam Barth 已提交
238 239 240 241
  void sendPlatformMessage(String name,
                           ByteData data,
                           PlatformMessageResponseCallback callback) {
    _sendPlatformMessage(name, callback, data);
242
  }
A
Adam Barth 已提交
243 244 245
  void _sendPlatformMessage(String name,
                            PlatformMessageResponseCallback callback,
                            ByteData data) native "Window_sendPlatformMessage";
246

A
Adam Barth 已提交
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
  /// Called whenever this window receives a message from a platform-specific
  /// plugin.
  ///
  /// The `name` parameter determines which plugin sent the message. The `data`
  /// parameter is the payload and is typically UTF-8 encoded JSON but can be
  /// arbitrary data.
  ///
  /// Message handlers must call the function given in the `callback` parameter.
  /// If the handler does not need to respond, the handler should pass `null` to
  /// the callback.
  PlatformMessageCallback onPlatformMessage;

  /// Called by [_dispatchPlatformMessage].
  void _respondToPlatformMessage(int responseId, ByteData data)
      native "Window_respondToPlatformMessage";
A
Adam Barth 已提交
262 263
}

I
Ian Hickson 已提交
264 265 266
/// The [Window] singleton. This object exposes the size of the display, the
/// core scheduler API, the input event callback, the graphics drawing API, and
/// other such core services.
A
Adam Barth 已提交
267
final Window window = new Window._();