From 6d7f9463ce13e021165e242fb244be5b6b8bdb6d Mon Sep 17 00:00:00 2001 From: singerdmx Date: Fri, 18 Dec 2020 17:50:41 -0800 Subject: [PATCH] Add initState and dispose in RawEditorState --- lib/widgets/editor.dart | 118 ++++++++++++++++++++++++++++---- lib/widgets/text_selection.dart | 19 ++++- 2 files changed, 122 insertions(+), 15 deletions(-) diff --git a/lib/widgets/editor.dart b/lib/widgets/editor.dart index c4b60aa6..54b72c16 100644 --- a/lib/widgets/editor.dart +++ b/lib/widgets/editor.dart @@ -10,6 +10,7 @@ import 'box.dart'; import 'controller.dart'; import 'cursor.dart'; import 'delegate.dart'; +import 'keyborad_listener.dart'; const Set WHITE_SPACE = { 0x9, @@ -326,6 +327,14 @@ class RawEditorState extends EditorState TextEditingValue _lastKnownRemoteTextEditingValue; int _cursorResetLocation = -1; bool _wasSelectingVerticallyWithKeyboard = false; + EditorTextSelectionOverlay _selectionOverlay; + FocusAttachment _focusAttachment; + CursorCont _cursorCont; + ScrollController _scrollController; + KeyboardListener _keyboardListener; + final ClipboardStatusNotifier _clipboardStatus = ClipboardStatusNotifier(); + + bool get _hasFocus => widget.focusNode.hasFocus; handleCursorMovement( LogicalKeyboardKey key, @@ -697,22 +706,95 @@ class RawEditorState extends EditorState throw UnimplementedError(); } + @override + void initState() { + super.initState(); + + _clipboardStatus?.addListener(_onChangedClipboardStatus); + + widget.controller.addListener(_didChangeTextEditingValue); + + _scrollController = widget.scrollController ?? ScrollController(); + _scrollController.addListener(_updateSelectionOverlayForScroll); + + _cursorCont = CursorCont( + show: ValueNotifier(widget.showCursor ?? false), + style: widget.cursorStyle ?? + CursorStyle( + color: Colors.blueAccent, + backgroundColor: Colors.grey, + width: 2.0, + ), + tickerProvider: this, + ); + + _keyboardListener = KeyboardListener( + handleCursorMovement, + handleShortcut, + handleDelete, + ); + + _focusAttachment = widget.focusNode.attach(context, + onKey: (node, event) => _keyboardListener.handleRawKeyEvent(event)); + widget.focusNode.addListener(_handleFocusChanged); + } + + handleDelete(bool forward) { + // TODO + } + + Future handleShortcut(InputShortcut shortcut) async { + // TODO + } + + @override + void dispose() { + closeConnectionIfNeeded(); + assert(!hasConnection); + _selectionOverlay?.dispose(); + _selectionOverlay = null; + widget.controller.removeListener(_didChangeTextEditingValue); + widget.focusNode.removeListener(_handleFocusChanged); + _focusAttachment.detach(); + _cursorCont.dispose(); + _clipboardStatus?.removeListener(_onChangedClipboardStatus); + _clipboardStatus?.dispose(); + super.dispose(); + } + + _updateSelectionOverlayForScroll() { + _selectionOverlay?.markNeedsBuild(); + } + + _didChangeTextEditingValue() { + // TODO + } + + _handleFocusChanged() { + // TODO + } + + _onChangedClipboardStatus() { + // TODO + } + + _showCaretOnScreen() { + + } + @override RenderEditor getRenderEditor() { - // TODO: implement getRenderEditor - throw UnimplementedError(); + return _editorKey.currentContext.findRenderObject(); } @override EditorTextSelectionOverlay getSelectionOverlay() { - // TODO: implement getSelectionOverlay - throw UnimplementedError(); + return _selectionOverlay; } @override TextEditingValue getTextEditingValue() { - // TODO: implement getTextEditingValue - throw UnimplementedError(); + return widget.controller.plainTextEditingValue; } @override @@ -735,24 +817,32 @@ class RawEditorState extends EditorState bool get selectAllEnabled => widget.toolbarOptions.selectAll; @override - void requestKeyboard() { - // TODO: implement requestKeyboard + requestKeyboard() { + if (_hasFocus) { + openConnectionIfNeeded(); + } else { + widget.focusNode.requestFocus(); + } } @override - void setTextEditingValue(TextEditingValue value) { - // TODO: implement setTextEditingValue + setTextEditingValue(TextEditingValue value) { + widget.controller + .updateSelection(value.selection, ChangeSource.LOCAL); } @override bool showToolbar() { - // TODO: implement showToolbar - throw UnimplementedError(); + if (_selectionOverlay == null || _selectionOverlay.toolbar != null) { + return false; + } + + _selectionOverlay.showToolbar(); + return true; } @override - // TODO: implement wantKeepAlive - bool get wantKeepAlive => throw UnimplementedError(); + bool get wantKeepAlive => widget.focusNode.hasFocus; } class RenderEditor extends RenderEditableContainerBox diff --git a/lib/widgets/text_selection.dart b/lib/widgets/text_selection.dart index 9fb92357..4bb6cc96 100644 --- a/lib/widgets/text_selection.dart +++ b/lib/widgets/text_selection.dart @@ -80,11 +80,28 @@ class EditorTextSelectionOverlay { } } - _markNeedsBuild() { + markNeedsBuild() { if (_handles != null) { _handles[0].markNeedsBuild(); _handles[1].markNeedsBuild(); } toolbar?.markNeedsBuild(); } + + hide() { + if (_handles != null) { + _handles[0].remove(); + _handles[1].remove(); + _handles = null; + } + if (toolbar != null) { + hideToolbar(); + } + } + + dispose() { + hide(); + _toolbarController.dispose(); + } + }