From 051dcb0c2689a4d2273fcf66fdfc5c09abe67c4a Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Sun, 11 Sep 2016 15:53:48 -0700 Subject: [PATCH] Add hello_flutter and make it work on Fuchsia (flutter/engine#3016) --- engine/src/flutter/examples/BUILD.gn | 3 +- .../flutter/examples/hello_flutter/.packages | 1 + .../flutter/examples/hello_flutter/BUILD.gn | 9 ++++ .../examples/hello_flutter/lib/main.dart | 50 +++++++++++++++++++ 4 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 engine/src/flutter/examples/hello_flutter/.packages create mode 100644 engine/src/flutter/examples/hello_flutter/BUILD.gn create mode 100644 engine/src/flutter/examples/hello_flutter/lib/main.dart diff --git a/engine/src/flutter/examples/BUILD.gn b/engine/src/flutter/examples/BUILD.gn index 81b122feef..d243070350 100644 --- a/engine/src/flutter/examples/BUILD.gn +++ b/engine/src/flutter/examples/BUILD.gn @@ -4,6 +4,7 @@ group("examples") { deps = [ - "spinning_square" + "hello_flutter", + "spinning_square", ] } diff --git a/engine/src/flutter/examples/hello_flutter/.packages b/engine/src/flutter/examples/hello_flutter/.packages new file mode 100644 index 0000000000..ea5386b459 --- /dev/null +++ b/engine/src/flutter/examples/hello_flutter/.packages @@ -0,0 +1 @@ +# hello_flutter doesn't depend on any packages. diff --git a/engine/src/flutter/examples/hello_flutter/BUILD.gn b/engine/src/flutter/examples/hello_flutter/BUILD.gn new file mode 100644 index 0000000000..8f5027cd22 --- /dev/null +++ b/engine/src/flutter/examples/hello_flutter/BUILD.gn @@ -0,0 +1,9 @@ +# Copyright 2016 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. + +import("//flutter/build/flutter_app.gni") + +flutter_app("hello_flutter") { + main_dart = "lib/main.dart" +} diff --git a/engine/src/flutter/examples/hello_flutter/lib/main.dart b/engine/src/flutter/examples/hello_flutter/lib/main.dart new file mode 100644 index 0000000000..74ed55c2f0 --- /dev/null +++ b/engine/src/flutter/examples/hello_flutter/lib/main.dart @@ -0,0 +1,50 @@ +// Copyright 2016 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. + +// This example shows how to show the text 'Hello, world.' using using the raw +// interface to the engine. + +import 'dart:ui' as ui; + +void beginFrame(Duration timeStamp) { + final double devicePixelRatio = ui.window.devicePixelRatio; + // TODO(abarth): ui.window.size should be in physical units. + final ui.Size logicalSize = ui.window.size; + + final ui.ParagraphBuilder paragraphBuilder = new ui.ParagraphBuilder() + ..addText('Hello, world.'); + final ui.Paragraph paragraph = paragraphBuilder.build(new ui.ParagraphStyle()) + ..layout(new ui.ParagraphConstraints(width: logicalSize.width)); + + final ui.Rect physicalBounds = ui.Point.origin & (logicalSize * devicePixelRatio); + final ui.PictureRecorder recorder = new ui.PictureRecorder(); + final ui.Canvas canvas = new ui.Canvas(recorder, physicalBounds); + canvas.scale(devicePixelRatio, devicePixelRatio); + canvas.drawRect(ui.Point.origin & logicalSize, new ui.Paint()..color = const ui.Color(0xFF0000FF)); + canvas.drawParagraph(paragraph, new ui.Offset( + (logicalSize.width - paragraph.maxIntrinsicWidth) / 2.0, + (logicalSize.height - paragraph.height) / 2.0 + )); + final ui.Picture picture = recorder.endRecording(); + + final ui.SceneBuilder sceneBuilder = new ui.SceneBuilder() + // TODO(abarth): We should be able to add a picture without pushing a + // container layer first. + ..pushClipRect(physicalBounds) + ..addPicture(ui.Offset.zero, picture) + ..pop(); + + ui.window.render(sceneBuilder.build()); +} + +// This function is the primary entry point to your application. The engine +// calls main() as soon as it has loaded your code. +void main() { + // The engine calls onBeginFrame whenever it wants us to produce a frame. + ui.window.onBeginFrame = beginFrame; + // Here we kick off the whole process by asking the engine to schedule a new + // frame. The engine will eventually call onBeginFrame when it is time for us + // to actually produce the frame. + ui.window.scheduleFrame(); +}