From 318b69be1000d69a699945238935051746415684 Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Sun, 6 Sep 2015 12:45:48 -0700 Subject: [PATCH] Fold package:sky/editing/* into package:sky/widgets.dart The editing directory just defined two widgets. We might as well fold them into the main widgets library. --- examples/address_book/lib/main.dart | 1 - examples/fitness/lib/main.dart | 1 - examples/stocks/lib/main.dart | 1 - packages/flutter/lib/README.md | 7 +- .../flutter/lib/editing/editable_text.dart | 111 ------------------ .../widgets/editable_text.dart} | 105 +++++++++++++++++ .../lib/{editing => src/widgets}/input.dart | 3 +- packages/flutter/lib/widgets.dart | 2 + 8 files changed, 109 insertions(+), 122 deletions(-) delete mode 100644 packages/flutter/lib/editing/editable_text.dart rename packages/flutter/lib/{editing/editable_string.dart => src/widgets/editable_text.dart} (57%) rename packages/flutter/lib/{editing => src/widgets}/input.dart (97%) diff --git a/examples/address_book/lib/main.dart b/examples/address_book/lib/main.dart index 80cdf496f0..540ba13d3c 100644 --- a/examples/address_book/lib/main.dart +++ b/examples/address_book/lib/main.dart @@ -2,7 +2,6 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -import 'package:sky/editing/input.dart'; import 'package:sky/theme/colors.dart' as colors; import 'package:sky/theme/typography.dart' as typography; import 'package:sky/widgets.dart'; diff --git a/examples/fitness/lib/main.dart b/examples/fitness/lib/main.dart index ac6a98d8b4..a4d40d1b2a 100644 --- a/examples/fitness/lib/main.dart +++ b/examples/fitness/lib/main.dart @@ -5,7 +5,6 @@ library fitness; import 'package:playfair/playfair.dart' as playfair; -import 'package:sky/editing/input.dart'; import 'package:sky/painting/text_style.dart'; import 'package:sky/theme/colors.dart' as colors; import 'package:sky/widgets.dart'; diff --git a/examples/stocks/lib/main.dart b/examples/stocks/lib/main.dart index 4f271c2e76..ba81c856ac 100644 --- a/examples/stocks/lib/main.dart +++ b/examples/stocks/lib/main.dart @@ -7,7 +7,6 @@ library stocks; import 'dart:math' as math; import 'dart:sky' as sky; -import 'package:sky/editing/input.dart'; import 'package:sky/theme/colors.dart' as colors; import 'package:sky/theme/typography.dart' as typography; import 'package:sky/widgets.dart'; diff --git a/packages/flutter/lib/README.md b/packages/flutter/lib/README.md index 6f4e716242..c52ef87186 100644 --- a/packages/flutter/lib/README.md +++ b/packages/flutter/lib/README.md @@ -33,9 +33,6 @@ directory, using a reactive framework. They use data given in the [theme/](theme/) directory to select styles consistent with Material Design. -Text input widgets are layered on this mechanism and can be found in -the [editing/](editing/) directory. - Alongside the above is the [mojo/](mojo/) directory, which contains anything that uses the Mojo IPC mechanism, typically as part of wrapping host operating system features. Some of those Host APIs are @@ -45,9 +42,7 @@ Here is a diagram summarising all this: +-----------------------------+ ------ | YOUR APP | - | +--------------------+--+ - | | editing/ | | - | +--+-------------------++ | + | +----------------------+---+ | | widgets/ (theme/) | | | ++---------------------++ | | | rendering/ | | Dart diff --git a/packages/flutter/lib/editing/editable_text.dart b/packages/flutter/lib/editing/editable_text.dart deleted file mode 100644 index 3a2da193bb..0000000000 --- a/packages/flutter/lib/editing/editable_text.dart +++ /dev/null @@ -1,111 +0,0 @@ -// 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. - -import 'dart:async'; -import 'dart:sky' as sky; - -import 'package:sky/editing/editable_string.dart'; -import 'package:sky/painting/text_style.dart'; -import 'package:sky/src/rendering/object.dart'; -import 'package:sky/src/widgets/basic.dart'; -import 'package:sky/src/widgets/framework.dart'; - -const _kCursorBlinkPeriod = 500; // milliseconds -const _kCursorGap = 1.0; -const _kCursorHeightOffset = 2.0; -const _kCursorWidth = 1.0; - -class EditableText extends StatefulComponent { - - EditableText({ - Key key, - this.value, - this.focused: false, - this.style, - this.cursorColor}) : super(key: key); - - EditableString value; - bool focused; - TextStyle style; - Color cursorColor; - - void syncConstructorArguments(EditableText source) { - value = source.value; - focused = source.focused; - style = source.style; - cursorColor = source.cursorColor; - } - - Timer _cursorTimer; - bool _showCursor = false; - - void _cursorTick(Timer timer) { - setState(() { - _showCursor = !_showCursor; - }); - } - - void _startCursorTimer() { - _showCursor = true; - _cursorTimer = new Timer.periodic( - new Duration(milliseconds: _kCursorBlinkPeriod), _cursorTick); - } - - void didUnmount() { - if (_cursorTimer != null) - _stopCursorTimer(); - super.didUnmount(); - } - - void _stopCursorTimer() { - _cursorTimer.cancel(); - _cursorTimer = null; - _showCursor = false; - } - - void _paintCursor(sky.Canvas canvas, Size size) { - if (!_showCursor) - return; - - Rect cursorRect = new Rect.fromLTWH( - _kCursorGap, - -_kCursorHeightOffset, - _kCursorWidth, - style.fontSize + 2 * _kCursorHeightOffset - ); - canvas.drawRect(cursorRect, new Paint()..color = cursorColor); - } - - Widget build() { - assert(style != null); - assert(focused != null); - assert(cursorColor != null); - - if (focused && _cursorTimer == null) - _startCursorTimer(); - else if (!focused && _cursorTimer != null) - _stopCursorTimer(); - - if (!value.composing.isValid) { - // TODO(eseidel): This is the wrong height if empty! - return new Text(value.text, style: style); - } - - TextStyle composingStyle = style.merge(const TextStyle(decoration: underline)); - StyledText text = new StyledText(elements: [ - style, - value.textBefore(value.composing), - [composingStyle, value.textInside(value.composing)], - value.textAfter(value.composing) - ]); - - Widget cursor = new Container( - height: style.fontSize, - width: _kCursorGap + _kCursorWidth, - child: new CustomPaint(callback: _paintCursor, token: _showCursor) - ); - - return new Row([text, cursor]); - } -} diff --git a/packages/flutter/lib/editing/editable_string.dart b/packages/flutter/lib/src/widgets/editable_text.dart similarity index 57% rename from packages/flutter/lib/editing/editable_string.dart rename to packages/flutter/lib/src/widgets/editable_text.dart index e9331b4cce..59aaea3eb5 100644 --- a/packages/flutter/lib/editing/editable_string.dart +++ b/packages/flutter/lib/src/widgets/editable_text.dart @@ -2,7 +2,18 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +import 'dart:async'; +import 'dart:sky' as sky; + import 'package:mojo_services/keyboard/keyboard.mojom.dart'; +import 'package:sky/painting/text_style.dart'; +import 'package:sky/src/widgets/basic.dart'; +import 'package:sky/src/widgets/framework.dart'; + +const _kCursorBlinkPeriod = 500; // milliseconds +const _kCursorGap = 1.0; +const _kCursorHeightOffset = 2.0; +const _kCursorWidth = 1.0; typedef void StringUpdated(); @@ -120,3 +131,97 @@ class EditableString implements KeyboardClient { onUpdated(); } } + +class EditableText extends StatefulComponent { + + EditableText({ + Key key, + this.value, + this.focused: false, + this.style, + this.cursorColor}) : super(key: key); + + EditableString value; + bool focused; + TextStyle style; + Color cursorColor; + + void syncConstructorArguments(EditableText source) { + value = source.value; + focused = source.focused; + style = source.style; + cursorColor = source.cursorColor; + } + + Timer _cursorTimer; + bool _showCursor = false; + + void _cursorTick(Timer timer) { + setState(() { + _showCursor = !_showCursor; + }); + } + + void _startCursorTimer() { + _showCursor = true; + _cursorTimer = new Timer.periodic( + new Duration(milliseconds: _kCursorBlinkPeriod), _cursorTick); + } + + void didUnmount() { + if (_cursorTimer != null) + _stopCursorTimer(); + super.didUnmount(); + } + + void _stopCursorTimer() { + _cursorTimer.cancel(); + _cursorTimer = null; + _showCursor = false; + } + + void _paintCursor(sky.Canvas canvas, Size size) { + if (!_showCursor) + return; + + Rect cursorRect = new Rect.fromLTWH( + _kCursorGap, + -_kCursorHeightOffset, + _kCursorWidth, + style.fontSize + 2 * _kCursorHeightOffset + ); + canvas.drawRect(cursorRect, new Paint()..color = cursorColor); + } + + Widget build() { + assert(style != null); + assert(focused != null); + assert(cursorColor != null); + + if (focused && _cursorTimer == null) + _startCursorTimer(); + else if (!focused && _cursorTimer != null) + _stopCursorTimer(); + + if (!value.composing.isValid) { + // TODO(eseidel): This is the wrong height if empty! + return new Text(value.text, style: style); + } + + TextStyle composingStyle = style.merge(const TextStyle(decoration: underline)); + StyledText text = new StyledText(elements: [ + style, + value.textBefore(value.composing), + [composingStyle, value.textInside(value.composing)], + value.textAfter(value.composing) + ]); + + Widget cursor = new Container( + height: style.fontSize, + width: _kCursorGap + _kCursorWidth, + child: new CustomPaint(callback: _paintCursor, token: _showCursor) + ); + + return new Row([text, cursor]); + } +} diff --git a/packages/flutter/lib/editing/input.dart b/packages/flutter/lib/src/widgets/input.dart similarity index 97% rename from packages/flutter/lib/editing/input.dart rename to packages/flutter/lib/src/widgets/input.dart index a253c1a4ce..0d46254b78 100644 --- a/packages/flutter/lib/editing/input.dart +++ b/packages/flutter/lib/src/widgets/input.dart @@ -2,11 +2,10 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -import 'package:sky/editing/editable_string.dart'; -import 'package:sky/editing/editable_text.dart'; import 'package:sky/mojo/keyboard.dart'; import 'package:sky/painting/text_style.dart'; import 'package:sky/src/widgets/basic.dart'; +import 'package:sky/src/widgets/editable_text.dart'; import 'package:sky/src/widgets/focus.dart'; import 'package:sky/src/widgets/framework.dart'; import 'package:sky/src/widgets/theme.dart'; diff --git a/packages/flutter/lib/widgets.dart b/packages/flutter/lib/widgets.dart index a424786999..70c9123ff4 100644 --- a/packages/flutter/lib/widgets.dart +++ b/packages/flutter/lib/widgets.dart @@ -22,6 +22,7 @@ export 'package:sky/src/widgets/drawer.dart'; export 'package:sky/src/widgets/drawer_divider.dart'; export 'package:sky/src/widgets/drawer_header.dart'; export 'package:sky/src/widgets/drawer_item.dart'; +export 'package:sky/src/widgets/editable_text.dart'; export 'package:sky/src/widgets/flat_button.dart'; export 'package:sky/src/widgets/floating_action_button.dart'; export 'package:sky/src/widgets/focus.dart'; @@ -31,6 +32,7 @@ export 'package:sky/src/widgets/homogeneous_viewport.dart'; export 'package:sky/src/widgets/icon.dart'; export 'package:sky/src/widgets/icon_button.dart'; export 'package:sky/src/widgets/ink_well.dart'; +export 'package:sky/src/widgets/input.dart'; export 'package:sky/src/widgets/material.dart'; export 'package:sky/src/widgets/material_button.dart'; export 'package:sky/src/widgets/mimic.dart';