diff --git a/examples/widgets/raw_keyboard.dart b/examples/widgets/raw_keyboard.dart index 15402cb8e94..77e94d84aab 100644 --- a/examples/widgets/raw_keyboard.dart +++ b/examples/widgets/raw_keyboard.dart @@ -1,81 +1,79 @@ -// Copyright (c) 2015, the Flutter project authors. Please see the AUTHORS file -// for details. All rights reserved. Use of this source code is governed by a -// BSD-style license that can be found in the LICENSE file. +// 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 'package:flutter/material.dart'; -import 'package:flutter/services.dart'; -import 'package:sky_services/raw_keyboard/raw_keyboard.mojom.dart' as mojo; -import 'package:sky_services/sky/input_event.mojom.dart' as mojo; +import 'package:sky_services/sky/input_event.mojom.dart' as mojom; + +GlobalKey _key = new GlobalKey(); void main() { runApp( new MaterialApp( title: "Hardware Key Demo", - routes: { - '/': (RouteArguments args) => const HardwareKeyDemo() + routes: { + '/': (RouteArguments args) { + return new Scaffold( + toolBar: new ToolBar( + center: new Text("Hardware Key Demo") + ), + body: new Material( + child: new RawKeyboardDemo( + key: _key + ) + ) + ); + } } ) ); } -class HardwareKeyDemo extends StatefulComponent { - const HardwareKeyDemo(); - HardwareKeyDemoState createState() => new HardwareKeyDemoState(); +class RawKeyboardDemo extends StatefulComponent { + RawKeyboardDemo({ GlobalKey key }) : super(key: key); + + _HardwareKeyDemoState createState() => new _HardwareKeyDemoState(); } -class HardwareKeyDemoState extends State implements mojo.RawKeyboardListener { +class _HardwareKeyDemoState extends State { + mojom.InputEvent _event = null; - mojo.InputEvent _event = null; - - void initState() { - mojo.RawKeyboardServiceProxy rawKeyboardService = new mojo.RawKeyboardServiceProxy.unbound(); - try { - shell.connectToService(null, rawKeyboardService); - mojo.RawKeyboardListenerStub listener = new mojo.RawKeyboardListenerStub.unbound() - ..impl = this; - rawKeyboardService.ptr.addListener(listener); - } finally { - rawKeyboardService.close(); - } - super.initState(); - } - - void onKey(mojo.InputEvent event) { + void _handleKey(mojom.InputEvent event) { setState(() { _event = event; }); } - Widget _buildBody() { - if (_event == null) { - return new Center( - child: new Text("Press a key", style: Typography.black.display1) + Widget build(BuildContext context) { + bool focused = Focus.at(context); + Widget child; + if (!focused) { + child = new GestureDetector( + onTap: () { + Focus.moveTo(config.key); + }, + child: new Center( + child: new Text('Tap to focus', style: Typography.black.display1) + ) + ); + } else if (_event == null) { + child = new Center( + child: new Text('Press a key', style: Typography.black.display1) + ); + } else { + child = new Column( + children: [ + new Text('${_event.type}', style: Typography.black.body2), + new Text('${_event.keyData.keyCode}', style: Typography.black.display4) + ], + justifyContent: FlexJustifyContent.center ); } - return new Column( - children: [ - new Text( - '${_event.type}', - style: Typography.black.body2 - ), - new Text( - '${_event.keyData.keyCode}', - style: Typography.black.display4 - ) - ], - justifyContent: FlexJustifyContent.center - ); - } - - Widget build(BuildContext context) { - return new Scaffold( - toolBar: new ToolBar( - center: new Text("Hardware Key Demo") - ), - body: new Material( - child: _buildBody() - ) + return new RawKeyboardListener( + focused: focused, + onKey: _handleKey, + child: child ); } } diff --git a/packages/flutter/lib/src/widgets/raw_keyboard_listener.dart b/packages/flutter/lib/src/widgets/raw_keyboard_listener.dart new file mode 100644 index 00000000000..9048c751ee7 --- /dev/null +++ b/packages/flutter/lib/src/widgets/raw_keyboard_listener.dart @@ -0,0 +1,76 @@ +// 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 'package:flutter/services.dart'; +import 'package:sky_services/raw_keyboard/raw_keyboard.mojom.dart' as mojom; +import 'package:sky_services/sky/input_event.mojom.dart' as mojom; + +import 'basic.dart'; +import 'framework.dart'; + +class RawKeyboardListener extends StatefulComponent { + RawKeyboardListener({ + Key key, + this.focused: false, + this.onKey, + this.child + }) : super(key: key) { + assert(child != null); + } + + final bool focused; + final ValueChanged onKey; + final Widget child; + + _RawKeyboardListenerState createState() => new _RawKeyboardListenerState(); +} + +class _RawKeyboardListenerState extends State implements mojom.RawKeyboardListener { + void initState() { + super.initState(); + _attachOrDetachKeyboard(); + } + + mojom.RawKeyboardListenerStub _stub; + + void didUpdateConfig(RawKeyboardListener oldConfig) { + _attachOrDetachKeyboard(); + } + + void dispose() { + _detachKeyboard(); + super.dispose(); + } + + void _attachOrDetachKeyboard() { + if (config.focused && _stub == null) + _attachKeyboard(); + else if (!config.focused && _stub != null) + _detachKeyboard(); + } + + void _attachKeyboard() { + assert(_stub == null); + _stub = new mojom.RawKeyboardListenerStub.unbound()..impl = this; + mojom.RawKeyboardServiceProxy keyboard = new mojom.RawKeyboardServiceProxy.unbound(); + shell.connectToService(null, keyboard); + keyboard.ptr.addListener(_stub); + keyboard.close(); + } + + void _detachKeyboard() { + assert(_stub != null); + _stub.close(); + _stub = null; + } + + void onKey(mojom.InputEvent event) { + if (config.onKey != null) + config.onKey(event); + } + + Widget build(BuildContext context) { + return config.child; + } +} diff --git a/packages/flutter/lib/widgets.dart b/packages/flutter/lib/widgets.dart index ea1d9b4c4b5..cb4e8f62e17 100644 --- a/packages/flutter/lib/widgets.dart +++ b/packages/flutter/lib/widgets.dart @@ -31,6 +31,7 @@ export 'src/widgets/pageable_list.dart'; export 'src/widgets/pages.dart'; export 'src/widgets/performance_overlay.dart'; export 'src/widgets/placeholder.dart'; +export 'src/widgets/raw_keyboard_listener.dart'; export 'src/widgets/routes.dart'; export 'src/widgets/scrollable.dart'; export 'src/widgets/scrollable_grid.dart';