window.dart 7.2 KB
Newer Older
A
Adam Barth 已提交
1 2 3 4 5 6
// 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.

part of dart_ui;

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 13 14 15 16 17

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

/// Signature for [Window.onPointerPacket].
typedef void PointerPacketCallback(ByteData serializedPacket);

/// Signature for [Window.onAppLifecycleStateChanged].
typedef void AppLifecycleStateCallback(AppLifecycleState state);
18 19 20

/// States that an application can be in.
enum AppLifecycleState {
21 22 23 24 25 26
  // These values must match the order of the values of
  // AppLifecycleState in sky_engine.mojom

  /// The application is not currently visible to the user. When the
  /// application is in this state, the engine will not call the
  /// [onBeginFrame] callback.
27
  paused,
28 29

  /// The application is visible to the user.
30 31
  resumed,
}
A
Adam Barth 已提交
32

33 34 35 36 37 38
/// 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
/// [EdgeDims] class.
39
class WindowPadding {
40
  const WindowPadding._({ this.left, this.top, this.right, this.bottom });
A
Adam Barth 已提交
41

42
  final double left;
A
Adam Barth 已提交
43 44 45
  final double top;
  final double right;
  final double bottom;
46 47

  static const WindowPadding zero = const WindowPadding._(left: 0.0, top: 0.0, right: 0.0, bottom: 0.0);
A
Adam Barth 已提交
48 49
}

50 51 52
/// 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.
53
class Locale {
H
Hixie 已提交
54 55 56 57 58 59 60
  /// 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');
61
  const Locale(this.languageCode, this.countryCode);
62

63
  /// The primary language subtag for the locale.
64
  final String languageCode;
65 66

  /// The region subtag for the locale.
67 68
  final String countryCode;

69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
  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;
  }

86 87 88
  String toString() => '${languageCode}_$countryCode';
}

89
/// The most basic interface to the host operating system's user interface.
H
Hixie 已提交
90 91 92
///
/// There is a single Window instance in the system, which you can
/// obtain from the [window] property.
A
Adam Barth 已提交
93 94 95
class Window {
  Window._();

96 97 98
  /// 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.
A
Adam Barth 已提交
99
  double get devicePixelRatio => _devicePixelRatio;
100
  double _devicePixelRatio = 1.0;
A
Adam Barth 已提交
101

102 103
  /// The dimensions of the rectangle into which the application will be drawn,
  /// in logical pixels.
104 105 106 107 108
  ///
  /// Logical pixels are roughly the same visual size across devices. Physical
  /// pixels are the size of the actual hardware pixels on the device. The
  /// number of physical pixels per logical pixel is described by the
  /// [devicePixelRatio].
A
Adam Barth 已提交
109
  Size get size => _size;
110
  Size _size = Size.zero;
A
Adam Barth 已提交
111

112 113
  /// The number of pixels on each side of the display rectangle into which the
  /// application can render, but over which the operating system will likely
H
Hixie 已提交
114 115 116
  /// 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).
117
  WindowPadding get padding => _padding;
118
  WindowPadding _padding = WindowPadding.zero;
A
Adam Barth 已提交
119

120 121 122 123 124 125 126
  /// A callback that is invoked whenever the [devicePixelRatio], [size], or
  /// [padding] values change.
  VoidCallback onMetricsChanged;

  /// The system-reported locale. This establishes the language and formatting
  /// conventions that application should, if possible, use to render their user
  /// interface.
127 128 129
  Locale get locale => _locale;
  Locale _locale;

130 131 132
  /// A callback that is invoked whenever [locale] changes value.
  VoidCallback onLocaleChanged;

133 134
  /// A callback that is invoked when there is a transition in the application's
  /// lifecycle (such as pausing or resuming).
I
Ian Hickson 已提交
135
  AppLifecycleStateCallback onAppLifecycleStateChanged;
136

137 138 139 140 141
  /// 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
  /// [render()] method. When possible, this is driven by the hardware VSync
  /// signal. This is only called if [scheduleFrame()] has been called since the
  /// last time this callback was invoked.
I
Ian Hickson 已提交
142
  FrameCallback onBeginFrame;
143 144 145 146

  /// A callback that is invoked when pointer data is available. The data is
  /// provided in the form of a raw byte stream containing an encoded mojo
  /// PointerPacket.
I
Ian Hickson 已提交
147
  PointerPacketCallback onPointerPacket;
A
Adam Barth 已提交
148

149 150
  /// The route or path that the operating system requested when the application
  /// was launched.
H
Hixie 已提交
151 152
  String get defaultRouteName => _defaultRouteName;
  String _defaultRouteName;
153 154 155 156

  /// A callback that is invoked when the operating system requests that the
  /// application goes "back" one step in its history. For example, on Android
  /// this is invoked in response to the "back" button.
A
Adam Barth 已提交
157 158
  VoidCallback onPopRoute;

159 160
  /// Requests that, at the next appropriate opportunity, the [onBeginFrame]
  /// callback be invoked.
A
Adam Barth 已提交
161
  void scheduleFrame() native "Window_scheduleFrame";
162 163

  /// Updates the application's rendering on the GPU with the newly provided
164 165 166 167
  /// [Scene]. This function must be called within the scope of the
  /// [onBeginFrame] callback being invoked. If this function is called multiple
  /// times during a single [onBeginFrame] callback or called outside the scope
  /// of an [onBeginFrame], the call will be ignored.
H
Hixie 已提交
168 169 170 171 172 173 174 175 176 177 178 179
  ///
  /// 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 [endRecording] function on the
  /// [PictureRecorder] to obtain the final [Picture] that represents
  /// the issued graphical operations.
  /// 
  /// Next, create a [SceneBuilder], and add the [Picture] to it using
  /// [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.
180
  void render(Scene scene) native "Window_render";
181 182 183

  /// Flushes pending real-time events, executing their callbacks.
  void flushRealTimeEvents() native "Scheduler_FlushRealTimeEvents";
A
Adam Barth 已提交
184 185
}

I
Ian Hickson 已提交
186 187 188
/// 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 已提交
189
final Window window = new Window._();