Skip to content

Commit

Permalink
Scroll to selection after keyboard opened
Browse files Browse the repository at this point in the history
Fixes #280.
  • Loading branch information
Amir-P committed Feb 23, 2024
1 parent be24068 commit b0b2277
Showing 1 changed file with 34 additions and 6 deletions.
40 changes: 34 additions & 6 deletions packages/fleather/lib/src/widgets/editor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1474,6 +1474,7 @@ class RawEditorState extends EditorState
if (_hasFocus) {
// Listen for changing viewInsets, which indicates keyboard showing up.
WidgetsBinding.instance.addObserver(this);
_lastBottomViewInset = View.of(context).viewInsets.bottom;
_showCaretOnScreen();
// _lastBottomViewInset = WidgetsBinding.instance.window.viewInsets.bottom;
// if (!_value.selection.isValid) {
Expand Down Expand Up @@ -1514,7 +1515,7 @@ class RawEditorState extends EditorState

bool _showCaretOnScreenScheduled = false;

void _showCaretOnScreen() {
void _showCaretOnScreen([bool withAnimation = true]) {
if (!widget.showCursor || _showCaretOnScreenScheduled) {
return;
}
Expand All @@ -1539,11 +1540,16 @@ class RawEditorState extends EditorState
);

if (offset != null) {
_scrollController.animateTo(
math.min(offset, _scrollController.position.maxScrollExtent),
duration: _caretAnimationDuration,
curve: _caretAnimationCurve,
);
if (withAnimation) {
_scrollController.animateTo(
math.min(offset, _scrollController.position.maxScrollExtent),
duration: _caretAnimationDuration,
curve: _caretAnimationCurve,
);
} else {
_scrollController.jumpTo(
math.min(offset, _scrollController.position.maxScrollExtent));

Check warning on line 1551 in packages/fleather/lib/src/widgets/editor.dart

View check run for this annotation

Codecov / codecov/patch

packages/fleather/lib/src/widgets/editor.dart#L1550-L1551

Added lines #L1550 - L1551 were not covered by tests
}
}
});
}
Expand All @@ -1569,6 +1575,28 @@ class RawEditorState extends EditorState
}
}

late double _lastBottomViewInset;

@override

Check warning on line 1580 in packages/fleather/lib/src/widgets/editor.dart

View check run for this annotation

Codecov / codecov/patch

packages/fleather/lib/src/widgets/editor.dart#L1580

Added line #L1580 was not covered by tests
void didChangeMetrics() {
super.didChangeMetrics();
if (!mounted) {

Check warning on line 1583 in packages/fleather/lib/src/widgets/editor.dart

View check run for this annotation

Codecov / codecov/patch

packages/fleather/lib/src/widgets/editor.dart#L1582-L1583

Added lines #L1582 - L1583 were not covered by tests
return;
}
final bottomBiewInset = View.of(context).viewInsets.bottom;
if (_lastBottomViewInset != bottomBiewInset) {
SchedulerBinding.instance.addPostFrameCallback((_) {
_selectionOverlay?.updateForScroll();

Check warning on line 1589 in packages/fleather/lib/src/widgets/editor.dart

View check run for this annotation

Codecov / codecov/patch

packages/fleather/lib/src/widgets/editor.dart#L1586-L1589

Added lines #L1586 - L1589 were not covered by tests
});
if (_lastBottomViewInset < bottomBiewInset) {

Check warning on line 1591 in packages/fleather/lib/src/widgets/editor.dart

View check run for this annotation

Codecov / codecov/patch

packages/fleather/lib/src/widgets/editor.dart#L1591

Added line #L1591 was not covered by tests
// Because the metrics change signal from engine will come here every frame
// (on both iOS and Android). So we don't need to show caret with animation.
_showCaretOnScreen(false);

Check warning on line 1594 in packages/fleather/lib/src/widgets/editor.dart

View check run for this annotation

Codecov / codecov/patch

packages/fleather/lib/src/widgets/editor.dart#L1594

Added line #L1594 was not covered by tests
}
}
_lastBottomViewInset = bottomBiewInset;

Check warning on line 1597 in packages/fleather/lib/src/widgets/editor.dart

View check run for this annotation

Codecov / codecov/patch

packages/fleather/lib/src/widgets/editor.dart#L1597

Added line #L1597 was not covered by tests
}

// On MacOS some actions are sent as selectors. We need to manually find the right Action and invoke it.
// Ref: https://github.com/flutter/flutter/blob/3.7.0/packages/flutter/lib/src/widgets/editable_text.dart#L3731
@override
Expand Down

0 comments on commit b0b2277

Please sign in to comment.