Commit 481b764f authored by Adam Barth's avatar Adam Barth

Port Input and EditableText to fn3

parent fe77808d
...@@ -18,6 +18,7 @@ export 'fn3/drawer.dart'; ...@@ -18,6 +18,7 @@ export 'fn3/drawer.dart';
export 'fn3/drawer_divider.dart'; export 'fn3/drawer_divider.dart';
export 'fn3/drawer_header.dart'; export 'fn3/drawer_header.dart';
export 'fn3/drawer_item.dart'; export 'fn3/drawer_item.dart';
export 'fn3/editable_text.dart';
export 'fn3/flat_button.dart'; export 'fn3/flat_button.dart';
export 'fn3/floating_action_button.dart'; export 'fn3/floating_action_button.dart';
export 'fn3/focus.dart'; export 'fn3/focus.dart';
...@@ -27,6 +28,7 @@ export 'fn3/homogeneous_viewport.dart'; ...@@ -27,6 +28,7 @@ export 'fn3/homogeneous_viewport.dart';
export 'fn3/icon.dart'; export 'fn3/icon.dart';
export 'fn3/icon_button.dart'; export 'fn3/icon_button.dart';
export 'fn3/ink_well.dart'; export 'fn3/ink_well.dart';
export 'fn3/input.dart';
export 'fn3/material.dart'; export 'fn3/material.dart';
export 'fn3/material_button.dart'; export 'fn3/material_button.dart';
export 'fn3/mixed_viewport.dart'; export 'fn3/mixed_viewport.dart';
......
...@@ -7,8 +7,8 @@ import 'dart:sky' as sky; ...@@ -7,8 +7,8 @@ import 'dart:sky' as sky;
import 'package:mojo_services/keyboard/keyboard.mojom.dart'; import 'package:mojo_services/keyboard/keyboard.mojom.dart';
import 'package:sky/painting.dart'; import 'package:sky/painting.dart';
import 'package:sky/src/widgets/basic.dart'; import 'package:sky/src/fn3/basic.dart';
import 'package:sky/src/widgets/framework.dart'; import 'package:sky/src/fn3/framework.dart';
const _kCursorBlinkPeriod = 500; // milliseconds const _kCursorBlinkPeriod = 500; // milliseconds
const _kCursorGap = 1.0; const _kCursorGap = 1.0;
...@@ -133,7 +133,6 @@ class EditableString implements KeyboardClient { ...@@ -133,7 +133,6 @@ class EditableString implements KeyboardClient {
} }
class EditableText extends StatefulComponent { class EditableText extends StatefulComponent {
EditableText({ EditableText({
Key key, Key key,
this.value, this.value,
...@@ -141,18 +140,15 @@ class EditableText extends StatefulComponent { ...@@ -141,18 +140,15 @@ class EditableText extends StatefulComponent {
this.style, this.style,
this.cursorColor}) : super(key: key); this.cursorColor}) : super(key: key);
EditableString value; final EditableString value;
bool focused; final bool focused;
TextStyle style; final TextStyle style;
Color cursorColor; final Color cursorColor;
void syncConstructorArguments(EditableText source) { EditableTextState createState() => new EditableTextState();
value = source.value; }
focused = source.focused;
style = source.style;
cursorColor = source.cursorColor;
}
class EditableTextState extends State<EditableText> {
Timer _cursorTimer; Timer _cursorTimer;
bool _showCursor = false; bool _showCursor = false;
...@@ -175,10 +171,10 @@ class EditableText extends StatefulComponent { ...@@ -175,10 +171,10 @@ class EditableText extends StatefulComponent {
new Duration(milliseconds: _kCursorBlinkPeriod), _cursorTick); new Duration(milliseconds: _kCursorBlinkPeriod), _cursorTick);
} }
void didUnmount() { void dispose() {
if (_cursorTimer != null) if (_cursorTimer != null)
_stopCursorTimer(); _stopCursorTimer();
super.didUnmount(); super.dispose();
} }
void _stopCursorTimer() { void _stopCursorTimer() {
...@@ -191,26 +187,29 @@ class EditableText extends StatefulComponent { ...@@ -191,26 +187,29 @@ class EditableText extends StatefulComponent {
if (!_showCursor) if (!_showCursor)
return; return;
double cursorHeight = style.fontSize + 2.0 * _kCursorHeightOffset; double cursorHeight = config.style.fontSize + 2.0 * _kCursorHeightOffset;
Rect cursorRect = new Rect.fromLTWH( Rect cursorRect = new Rect.fromLTWH(
_kCursorGap, _kCursorGap,
(size.height - cursorHeight) / 2.0, (size.height - cursorHeight) / 2.0,
_kCursorWidth, _kCursorWidth,
cursorHeight cursorHeight
); );
canvas.drawRect(cursorRect, new Paint()..color = cursorColor); canvas.drawRect(cursorRect, new Paint()..color = config.cursorColor);
} }
Widget build() { Widget build(BuildContext context) {
assert(style != null); assert(config.style != null);
assert(focused != null); assert(config.focused != null);
assert(cursorColor != null); assert(config.cursorColor != null);
if (focused && _cursorTimer == null) if (config.focused && _cursorTimer == null)
_startCursorTimer(); _startCursorTimer();
else if (!focused && _cursorTimer != null) else if (!config.focused && _cursorTimer != null)
_stopCursorTimer(); _stopCursorTimer();
final EditableString value = config.value;
final TextStyle style = config.style;
Widget text; Widget text;
if (value.composing.isValid) { if (value.composing.isValid) {
TextStyle composingStyle = style.merge(const TextStyle(decoration: underline)); TextStyle composingStyle = style.merge(const TextStyle(decoration: underline));
......
...@@ -4,11 +4,11 @@ ...@@ -4,11 +4,11 @@
import 'package:sky/services.dart'; import 'package:sky/services.dart';
import 'package:sky/painting.dart'; import 'package:sky/painting.dart';
import 'package:sky/src/widgets/basic.dart'; import 'package:sky/src/fn3/basic.dart';
import 'package:sky/src/widgets/editable_text.dart'; import 'package:sky/src/fn3/editable_text.dart';
import 'package:sky/src/widgets/focus.dart'; import 'package:sky/src/fn3/focus.dart';
import 'package:sky/src/widgets/framework.dart'; import 'package:sky/src/fn3/framework.dart';
import 'package:sky/src/widgets/theme.dart'; import 'package:sky/src/fn3/theme.dart';
export 'package:sky/services.dart' show KeyboardType; export 'package:sky/services.dart' show KeyboardType;
...@@ -19,35 +19,34 @@ typedef void StringValueChanged(String value); ...@@ -19,35 +19,34 @@ typedef void StringValueChanged(String value);
const EdgeDims _kTextfieldPadding = const EdgeDims.symmetric(vertical: 8.0); const EdgeDims _kTextfieldPadding = const EdgeDims.symmetric(vertical: 8.0);
class Input extends StatefulComponent { class Input extends StatefulComponent {
Input({ Input({
GlobalKey key, GlobalKey key,
String initialValue: '', this.initialValue: '',
this.placeholder, this.placeholder,
this.onChanged, this.onChanged,
this.keyboardType : KeyboardType.TEXT this.keyboardType: KeyboardType.TEXT
}): _value = initialValue, super(key: key); }): super(key: key);
final String initialValue;
final KeyboardType keyboardType;
final String placeholder;
final StringValueChanged onChanged;
KeyboardType keyboardType; InputState createState() => new InputState();
String placeholder; }
StringValueChanged onChanged;
class InputState extends State<Input> {
String _value; String _value;
EditableString _editableValue; EditableString _editableValue;
KeyboardHandle _keyboardHandle = KeyboardHandle.unattached; KeyboardHandle _keyboardHandle = KeyboardHandle.unattached;
void initState() { void initState(BuildContext context) {
super.initState(context);
_value = config.initialValue;
_editableValue = new EditableString( _editableValue = new EditableString(
text: _value, text: _value,
onUpdated: _handleTextUpdated onUpdated: _handleTextUpdated
); );
super.initState();
}
void syncConstructorArguments(Input source) {
placeholder = source.placeholder;
onChanged = source.onChanged;
keyboardType = source.keyboardType;
} }
void _handleTextUpdated() { void _handleTextUpdated() {
...@@ -55,17 +54,17 @@ class Input extends StatefulComponent { ...@@ -55,17 +54,17 @@ class Input extends StatefulComponent {
setState(() { setState(() {
_value = _editableValue.text; _value = _editableValue.text;
}); });
if (onChanged != null) if (config.onChanged != null)
onChanged(_value); config.onChanged(_value);
} }
} }
Widget build() { Widget build(BuildContext context) {
ThemeData themeData = Theme.of(this); ThemeData themeData = Theme.of(context);
bool focused = Focus.at(this); bool focused = FocusState.at(context, config);
if (focused && !_keyboardHandle.attached) { if (focused && !_keyboardHandle.attached) {
_keyboardHandle = keyboard.show(_editableValue.stub, keyboardType); _keyboardHandle = keyboard.show(_editableValue.stub, config.keyboardType);
} else if (!focused && _keyboardHandle.attached) { } else if (!focused && _keyboardHandle.attached) {
_keyboardHandle.release(); _keyboardHandle.release();
} }
...@@ -73,10 +72,10 @@ class Input extends StatefulComponent { ...@@ -73,10 +72,10 @@ class Input extends StatefulComponent {
TextStyle textStyle = themeData.text.subhead; TextStyle textStyle = themeData.text.subhead;
List<Widget> textChildren = <Widget>[]; List<Widget> textChildren = <Widget>[];
if (placeholder != null && _value.isEmpty) { if (config.placeholder != null && _value.isEmpty) {
Widget child = new Opacity( Widget child = new Opacity(
key: const ValueKey<String>('placeholder'), key: const ValueKey<String>('placeholder'),
child: new Text(placeholder, style: textStyle), child: new Text(config.placeholder, style: textStyle),
opacity: themeData.hintOpacity opacity: themeData.hintOpacity
); );
textChildren.add(child); textChildren.add(child);
...@@ -109,23 +108,21 @@ class Input extends StatefulComponent { ...@@ -109,23 +108,21 @@ class Input extends StatefulComponent {
return new Listener( return new Listener(
child: input, child: input,
onPointerDown: focus onPointerDown: (_) {
if (FocusState.at(context, config)) {
assert(_keyboardHandle.attached);
_keyboardHandle.showByRequest();
} else {
FocusState.moveTo(context, config);
// we'll get told to rebuild and we'll take care of the keyboard then
}
}
); );
} }
void focus(_) { void dispose() {
if (Focus.at(this)) {
assert(_keyboardHandle.attached);
_keyboardHandle.showByRequest();
} else {
Focus.moveTo(this);
// we'll get told to rebuild and we'll take care of the keyboard then
}
}
void didUnmount() {
if (_keyboardHandle.attached) if (_keyboardHandle.attached)
_keyboardHandle.release(); _keyboardHandle.release();
super.didUnmount(); super.dispose();
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment