• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2016 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// This example shows how to show the text 'Hello, world.' using using the raw
6// interface to the engine.
7
8import 'dart:ui' as ui;
9
10void beginFrame(Duration timeStamp) {
11  final double devicePixelRatio = ui.window.devicePixelRatio;
12  final ui.Size logicalSize = ui.window.physicalSize / devicePixelRatio;
13
14  final ui.ParagraphBuilder paragraphBuilder = ui.ParagraphBuilder(
15    ui.ParagraphStyle(textDirection: ui.TextDirection.ltr),
16  )
17    ..addText('Hello, world.');
18  final ui.Paragraph paragraph = paragraphBuilder.build()
19    ..layout(ui.ParagraphConstraints(width: logicalSize.width));
20
21  final ui.Rect physicalBounds = ui.Offset.zero & (logicalSize * devicePixelRatio);
22  final ui.PictureRecorder recorder = ui.PictureRecorder();
23  final ui.Canvas canvas = ui.Canvas(recorder, physicalBounds);
24  canvas.scale(devicePixelRatio, devicePixelRatio);
25  canvas.drawParagraph(paragraph, ui.Offset(
26    (logicalSize.width - paragraph.maxIntrinsicWidth) / 2.0,
27    (logicalSize.height - paragraph.height) / 2.0,
28  ));
29  final ui.Picture picture = recorder.endRecording();
30
31  final ui.SceneBuilder sceneBuilder = ui.SceneBuilder()
32    // TODO(abarth): We should be able to add a picture without pushing a
33    // container layer first.
34    ..pushClipRect(physicalBounds)
35    ..addPicture(ui.Offset.zero, picture)
36    ..pop();
37
38  ui.window.render(sceneBuilder.build());
39}
40
41// This function is the primary entry point to your application. The engine
42// calls main() as soon as it has loaded your code.
43void main() {
44  // The engine calls onBeginFrame whenever it wants us to produce a frame.
45  ui.window.onBeginFrame = beginFrame;
46  // Here we kick off the whole process by asking the engine to schedule a new
47  // frame. The engine will eventually call onBeginFrame when it is time for us
48  // to actually produce the frame.
49  ui.window.scheduleFrame();
50}
51