|
| 1 | +// Copyright 2013 The Flutter 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 | +import 'dart:convert'; |
| 6 | +import 'dart:typed_data'; |
| 7 | +import 'dart:io'; |
| 8 | +import 'dart:ui'; |
| 9 | + |
| 10 | +import 'package:fidl_fuchsia_ui_app/fidl_async.dart'; |
| 11 | +import 'package:fidl_fuchsia_ui_views/fidl_async.dart'; |
| 12 | +import 'package:fidl_fuchsia_ui_test_input/fidl_async.dart' as test_touch; |
| 13 | +import 'package:fuchsia_services/services.dart'; |
| 14 | +import 'package:zircon/zircon.dart'; |
| 15 | + |
| 16 | +void main(List<String> args) { |
| 17 | + print('Launching embedding-flutter-view'); |
| 18 | + TestApp app = TestApp(ChildView.gfx(_launchGfxChildView())); |
| 19 | + app.run(); |
| 20 | +} |
| 21 | + |
| 22 | +class TestApp { |
| 23 | + static const _black = Color.fromARGB(255, 0, 0, 0); |
| 24 | + static const _blue = Color.fromARGB(255, 0, 0, 255); |
| 25 | + |
| 26 | + final ChildView childView; |
| 27 | + final _responseListener = test_touch.TouchInputListenerProxy(); |
| 28 | + |
| 29 | + Color _backgroundColor = _blue; |
| 30 | + |
| 31 | + TestApp(this.childView) {} |
| 32 | + |
| 33 | + void run() { |
| 34 | + childView.create((ByteData reply) { |
| 35 | + // Set up window callbacks. |
| 36 | + window.onPointerDataPacket = (PointerDataPacket packet) { |
| 37 | + this.pointerDataPacket(packet); |
| 38 | + }; |
| 39 | + window.onMetricsChanged = () { |
| 40 | + window.scheduleFrame(); |
| 41 | + }; |
| 42 | + window.onBeginFrame = (Duration duration) { |
| 43 | + this.beginFrame(duration); |
| 44 | + }; |
| 45 | + |
| 46 | + // The child view should be attached to Scenic now. |
| 47 | + // Ready to build the scene. |
| 48 | + window.scheduleFrame(); |
| 49 | + }); |
| 50 | + } |
| 51 | + |
| 52 | + void beginFrame(Duration duration) { |
| 53 | + // Convert physical screen size of device to values |
| 54 | + final pixelRatio = window.devicePixelRatio; |
| 55 | + final size = window.physicalSize / pixelRatio; |
| 56 | + final physicalBounds = Offset.zero & window.physicalSize; |
| 57 | + final windowBounds = Offset.zero & size; |
| 58 | + // Set up a Canvas that uses the screen size |
| 59 | + final recorder = PictureRecorder(); |
| 60 | + final canvas = Canvas(recorder, physicalBounds); |
| 61 | + canvas.scale(pixelRatio); |
| 62 | + // Draw something |
| 63 | + final paint = Paint()..color = this._backgroundColor; |
| 64 | + canvas.drawRect(windowBounds, paint); |
| 65 | + final picture = recorder.endRecording(); |
| 66 | + // Build the scene |
| 67 | + final sceneBuilder = SceneBuilder() |
| 68 | + ..pushClipRect(physicalBounds) |
| 69 | + ..addPicture(Offset.zero, picture); |
| 70 | + // Child view should take up half the screen |
| 71 | + final childPhysicalSize = window.physicalSize * 0.5; |
| 72 | + sceneBuilder |
| 73 | + ..addPlatformView(childView.viewId, |
| 74 | + width: childPhysicalSize.width, |
| 75 | + height: size.height) |
| 76 | + ..pop(); |
| 77 | + sceneBuilder.pop(); |
| 78 | + window.render(sceneBuilder.build()); |
| 79 | + } |
| 80 | + |
| 81 | + void pointerDataPacket(PointerDataPacket packet) async { |
| 82 | + int nowNanos = System.clockGetMonotonic(); |
| 83 | + |
| 84 | + for (PointerData data in packet.data) { |
| 85 | + print('embedding-flutter-view received tap: ${data.toStringFull()}'); |
| 86 | + |
| 87 | + if (data.change == PointerChange.down) { |
| 88 | + this._backgroundColor = _black; |
| 89 | + } |
| 90 | + |
| 91 | + if (data.change == PointerChange.down || data.change == PointerChange.move) { |
| 92 | + Incoming.fromSvcPath() |
| 93 | + ..connectToService(_responseListener) |
| 94 | + ..close(); |
| 95 | + |
| 96 | + _respond(test_touch.TouchInputListenerReportTouchInputRequest( |
| 97 | + localX: data.physicalX, |
| 98 | + localY: data.physicalY, |
| 99 | + timeReceived: nowNanos, |
| 100 | + componentName: 'embedding-flutter-view', |
| 101 | + )); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + window.scheduleFrame(); |
| 106 | + } |
| 107 | + |
| 108 | + void _respond(test_touch.TouchInputListenerReportTouchInputRequest request) async { |
| 109 | + print('embedding-flutter-view reporting touch input to TouchInputListener'); |
| 110 | + await _responseListener.reportTouchInput(request); |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +class ChildView { |
| 115 | + final ViewHolderToken viewHolderToken; |
| 116 | + final ViewportCreationToken viewportCreationToken; |
| 117 | + final int viewId; |
| 118 | + |
| 119 | + ChildView(this.viewportCreationToken) : viewHolderToken = null, viewId = viewportCreationToken.value.handle.handle { |
| 120 | + assert(viewId != null); |
| 121 | + } |
| 122 | + |
| 123 | + ChildView.gfx(this.viewHolderToken) : viewportCreationToken = null, viewId = viewHolderToken.value.handle.handle { |
| 124 | + assert(viewId != null); |
| 125 | + } |
| 126 | + |
| 127 | + void create(PlatformMessageResponseCallback callback) { |
| 128 | + // Construct the dart:ui platform message to create the view, and when the |
| 129 | + // return callback is invoked, build the scene. At that point, it is safe |
| 130 | + // to embed the child view in the scene. |
| 131 | + final viewOcclusionHint = Rect.zero; |
| 132 | + final Map<String, dynamic> args = <String, dynamic>{ |
| 133 | + 'viewId': viewId, |
| 134 | + 'hitTestable': true, |
| 135 | + 'focusable': true, |
| 136 | + 'viewOcclusionHintLTRB': <double>[ |
| 137 | + viewOcclusionHint.left, |
| 138 | + viewOcclusionHint.top, |
| 139 | + viewOcclusionHint.right, |
| 140 | + viewOcclusionHint.bottom |
| 141 | + ], |
| 142 | + }; |
| 143 | + |
| 144 | + final ByteData createViewMessage = utf8.encoder.convert( |
| 145 | + json.encode(<String, Object>{ |
| 146 | + 'method': 'View.create', |
| 147 | + 'args': args, |
| 148 | + }) |
| 149 | + ).buffer.asByteData(); |
| 150 | + |
| 151 | + final platformViewsChannel = 'flutter/platform_views'; |
| 152 | + |
| 153 | + PlatformDispatcher.instance.sendPlatformMessage( |
| 154 | + platformViewsChannel, |
| 155 | + createViewMessage, |
| 156 | + callback); |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +ViewHolderToken _launchGfxChildView() { |
| 161 | + ViewProviderProxy viewProvider = ViewProviderProxy(); |
| 162 | + Incoming.fromSvcPath() |
| 163 | + ..connectToService(viewProvider) |
| 164 | + ..close(); |
| 165 | + |
| 166 | + final viewTokens = EventPairPair(); |
| 167 | + assert(viewTokens.status == ZX.OK); |
| 168 | + final viewHolderToken = ViewHolderToken(value: viewTokens.first); |
| 169 | + final viewToken = ViewToken(value: viewTokens.second); |
| 170 | + |
| 171 | + viewProvider.createView(viewToken.value, null, null); |
| 172 | + viewProvider.ctrl.close(); |
| 173 | + |
| 174 | + return viewHolderToken; |
| 175 | +} |
0 commit comments