Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scroll to selection after keyboard opened #285

Merged
merged 2 commits into from
Mar 27, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
if (_hasFocus) {
// Listen for changing viewInsets, which indicates keyboard showing up.
WidgetsBinding.instance.addObserver(this);
_lastBottomViewInset = View.of(context).viewInsets.bottom;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this redundant with didChangeMetrics?

Copy link
Member Author

@Amir-P Amir-P Mar 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are not receiving view inset changes while editor is unfocused. In order to have the latest bottom view inset we are fetching it when editor gained focus.

_showCaretOnScreen();
// _lastBottomViewInset = WidgetsBinding.instance.window.viewInsets.bottom;
// if (!_value.selection.isValid) {
Expand Down Expand Up @@ -1514,7 +1515,7 @@

bool _showCaretOnScreenScheduled = false;

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

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 @@
}
}

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;
Amir-P marked this conversation as resolved.
Show resolved Hide resolved
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
Loading