1
This commit is contained in:
1 parent
5efc778165
commit
59125d185c
64 files changed
+7912
-1393
No files matched your search
@@ -201,6 +201,7 @@ class _CodeEditableState extends State<_CodeEditable> with AutomaticKeepAliveCli
|
||||
final Widget child = _CodeScrollable(
|
||||
axisDirection: AxisDirection.down,
|
||||
controller: widget.scrollController.verticalScroller,
|
||||
bidirectionalHorizontalController: widget.wordWrap ? null : widget.scrollController.horizontalScroller,
|
||||
viewportBuilder: (context, ViewportOffset vertical) {
|
||||
final Widget? indicator = widget.indicatorBuilder?.call(
|
||||
context,
|
||||
@@ -244,6 +245,7 @@ class _CodeEditableState extends State<_CodeEditable> with AutomaticKeepAliveCli
|
||||
codeField = _CodeScrollable(
|
||||
axisDirection: AxisDirection.right,
|
||||
controller: widget.scrollController.horizontalScroller,
|
||||
enableDrag: false,
|
||||
viewportBuilder: (context, ViewportOffset horizontal) {
|
||||
final ViewportOffset effectiveHorizontal;
|
||||
if (widget.pinLineNumbers || gutterWidget == null) {
|
||||
@@ -321,11 +323,6 @@ class _CodeEditableState extends State<_CodeEditable> with AutomaticKeepAliveCli
|
||||
onNotification: (notification) {
|
||||
if (notification is ScrollStartNotification) {
|
||||
widget.selectionOverlayController.hideToolbar();
|
||||
if (notification.metrics.axis == Axis.horizontal) {
|
||||
_stopScroller(widget.scrollController.verticalScroller);
|
||||
} else if (notification.metrics.axis == Axis.vertical) {
|
||||
_stopScroller(widget.scrollController.horizontalScroller);
|
||||
}
|
||||
} else if (notification is ScrollEndNotification) {
|
||||
if (!widget.controller.selection.isCollapsed && widget.controller.selection.baseOffset != -1) {
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
@@ -333,6 +330,7 @@ class _CodeEditableState extends State<_CodeEditable> with AutomaticKeepAliveCli
|
||||
!widget.controller.selection.isCollapsed &&
|
||||
widget.controller.selection.baseOffset != -1) {
|
||||
widget.selectionOverlayController.showToolbar(context, Offset.zero);
|
||||
widget.selectionOverlayController.showHandle(context);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -344,17 +342,6 @@ class _CodeEditableState extends State<_CodeEditable> with AutomaticKeepAliveCli
|
||||
);
|
||||
}
|
||||
|
||||
void _stopScroller(ScrollController scroller) {
|
||||
if (!scroller.hasClients) return;
|
||||
for (final position in scroller.positions) {
|
||||
if (position is CodeEditorScrollPosition) {
|
||||
position.stopScrolling();
|
||||
} else if (position.activity?.isScrolling == true) {
|
||||
position.hold(() {});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Widget _buildCodeField(ViewportOffset vertical, ViewportOffset? horizontal) {
|
||||
return _CodeField(
|
||||
key: widget.editorKey,
|
||||
|
||||
@@ -37,8 +37,8 @@ extension _TextSpanExtension on TextSpan {
|
||||
|
||||
extension _OffsetExtension on Offset {
|
||||
|
||||
bool isSamePosition(Offset offset) {
|
||||
return (this - offset).distance < 10;
|
||||
bool isSamePosition(Offset offset, [double slop = 35.0]) {
|
||||
return (this - offset).distance <= slop;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1346,21 +1346,6 @@ class _CodeFieldRender extends RenderBox implements MouseTrackerAnnotation {
|
||||
startIndex = min(((target - paddingTop) / _preferredLineHeight).floor(), max(0, _codes.length - 1));
|
||||
}
|
||||
_displayParagraphs.addAll(_buildDisplayRenderParagraphs(startIndex, effectiveWidth));
|
||||
} else if (_horizontalViewport != null) {
|
||||
// In horizontal scrolling (unwrapped) mode, every line has height == _preferredLineHeight.
|
||||
// There is no line wrapping, so delta is strictly 0 and viewport correction should never occur.
|
||||
final int startIndex;
|
||||
if (target <= paddingTop) {
|
||||
startIndex = 0;
|
||||
} else {
|
||||
startIndex = min(((target - paddingTop) / _preferredLineHeight).floor(), max(0, _codes.length - 1));
|
||||
}
|
||||
if (_displayParagraphs.first.index != startIndex ||
|
||||
target < _displayParagraphs.first.top ||
|
||||
target + size.height > _displayParagraphs.last.bottom) {
|
||||
_displayParagraphs.clear();
|
||||
_displayParagraphs.addAll(_buildDisplayRenderParagraphs(startIndex, effectiveWidth));
|
||||
}
|
||||
} else {
|
||||
if (_codes.length <= _displayParagraphs.first.index) {
|
||||
_displayParagraphs.clear();
|
||||
@@ -1541,13 +1526,21 @@ class _CodeFieldRender extends RenderBox implements MouseTrackerAnnotation {
|
||||
}
|
||||
|
||||
void _calculatePreferredLineHeight() {
|
||||
final StrutStyle strutStyle = StrutStyle(
|
||||
fontSize: _textStyle.fontSize,
|
||||
fontFamily: _textStyle.fontFamily,
|
||||
height: _textStyle.height,
|
||||
forceStrutHeight: true,
|
||||
);
|
||||
final TextPainter painter = TextPainter(
|
||||
textDirection: TextDirection.ltr,
|
||||
strutStyle: strutStyle,
|
||||
);
|
||||
painter.text = TextSpan(
|
||||
text: '0',
|
||||
style: _textStyle,
|
||||
);
|
||||
painter.layout();
|
||||
_preferredLineHeight = painter.preferredLineHeight;
|
||||
_foregroundRender.find<_CodeFieldCursorPainter>().height = painter.preferredLineHeight;
|
||||
_foregroundRender.find<_CodeFieldFloatingCursorPainter>().height = painter.preferredLineHeight;
|
||||
|
||||
@@ -213,24 +213,27 @@ class _CodeParagraphProvider {
|
||||
if (uiStyle == _style) {
|
||||
return;
|
||||
}
|
||||
final StrutStyle strutStyle = StrutStyle(
|
||||
fontSize: style.fontSize,
|
||||
fontFamily: style.fontFamily,
|
||||
height: style.height,
|
||||
forceStrutHeight: true,
|
||||
);
|
||||
_paragraphStyle = style.getParagraphStyle(
|
||||
textAlign: TextAlign.left,
|
||||
textDirection: TextDirection.ltr,
|
||||
strutStyle: StrutStyle(
|
||||
fontSize: style.fontSize,
|
||||
fontFamily: style.fontFamily,
|
||||
height: style.height,
|
||||
forceStrutHeight: true,
|
||||
)
|
||||
strutStyle: strutStyle,
|
||||
);
|
||||
_style = uiStyle;
|
||||
final TextPainter painter = TextPainter(
|
||||
textDirection: TextDirection.ltr,
|
||||
strutStyle: strutStyle,
|
||||
);
|
||||
painter.text = TextSpan(
|
||||
text: '0',
|
||||
style: style
|
||||
style: style,
|
||||
);
|
||||
painter.layout();
|
||||
_preferredLineHeight = painter.preferredLineHeight;
|
||||
clearCache();
|
||||
}
|
||||
|
||||
@@ -6,6 +6,8 @@ class _CodeScrollable extends StatefulWidget {
|
||||
|
||||
final AxisDirection axisDirection;
|
||||
final ScrollController? controller;
|
||||
final ScrollController? bidirectionalHorizontalController;
|
||||
final bool enableDrag;
|
||||
final ViewportBuilder viewportBuilder;
|
||||
final CodeScrollbarBuilder? scrollbarBuilder;
|
||||
|
||||
@@ -13,6 +15,8 @@ class _CodeScrollable extends StatefulWidget {
|
||||
super.key,
|
||||
required this.axisDirection,
|
||||
this.controller,
|
||||
this.bidirectionalHorizontalController,
|
||||
this.enableDrag = true,
|
||||
required this.viewportBuilder,
|
||||
this.scrollbarBuilder,
|
||||
});
|
||||
@@ -26,6 +30,11 @@ class _CodeScrollableState extends State<_CodeScrollable> {
|
||||
|
||||
CodeEditorScrollController? _effectiveController;
|
||||
|
||||
ScrollHoldController? _verticalHold;
|
||||
ScrollHoldController? _horizontalHold;
|
||||
Drag? _verticalDrag;
|
||||
Drag? _horizontalDrag;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
@@ -48,31 +57,191 @@ class _CodeScrollableState extends State<_CodeScrollable> {
|
||||
void didUpdateWidget(_CodeScrollable oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
if (widget.controller != oldWidget.controller) {
|
||||
_handlePanCancel();
|
||||
if (_effectiveController != null && _effectiveController!.delegate != null) {
|
||||
_effectiveController!.dispose();
|
||||
}
|
||||
_initController();
|
||||
}
|
||||
if (widget.bidirectionalHorizontalController != oldWidget.bidirectionalHorizontalController) {
|
||||
_handlePanCancel();
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_handlePanCancel();
|
||||
if (_effectiveController != null && _effectiveController!.delegate != null) {
|
||||
_effectiveController!.dispose();
|
||||
}
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
ScrollPosition? get _verticalPosition {
|
||||
if (_effectiveController != null && _effectiveController!.positions.isNotEmpty) {
|
||||
return _effectiveController!.positions.first;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
ScrollPosition? get _horizontalPosition {
|
||||
final hController = widget.bidirectionalHorizontalController;
|
||||
if (hController != null && hController.positions.isNotEmpty) {
|
||||
return hController.positions.first;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
void _handlePanDown(DragDownDetails details) {
|
||||
if (_effectiveController?.isPinchLocked == true) {
|
||||
return;
|
||||
}
|
||||
final vPos = _verticalPosition;
|
||||
final hPos = _horizontalPosition;
|
||||
_verticalHold = vPos?.hold(_disposeVerticalHold);
|
||||
_horizontalHold = hPos?.hold(_disposeHorizontalHold);
|
||||
}
|
||||
|
||||
void _handlePanStart(DragStartDetails details) {
|
||||
if (_effectiveController?.isPinchLocked == true) {
|
||||
_handlePanCancel();
|
||||
return;
|
||||
}
|
||||
final vPos = _verticalPosition;
|
||||
final hPos = _horizontalPosition;
|
||||
_verticalDrag = vPos?.drag(details, _disposeVerticalDrag);
|
||||
_horizontalDrag = hPos?.drag(details, _disposeHorizontalDrag);
|
||||
_disposeVerticalHold();
|
||||
_disposeHorizontalHold();
|
||||
}
|
||||
|
||||
void _handlePanUpdate(DragUpdateDetails details) {
|
||||
if (_effectiveController?.isPinchLocked == true) {
|
||||
_handlePanCancel();
|
||||
return;
|
||||
}
|
||||
if (_verticalDrag != null) {
|
||||
final verticalDetails = DragUpdateDetails(
|
||||
sourceTimeStamp: details.sourceTimeStamp,
|
||||
delta: Offset(0.0, details.delta.dy),
|
||||
primaryDelta: details.delta.dy,
|
||||
globalPosition: details.globalPosition,
|
||||
localPosition: details.localPosition,
|
||||
);
|
||||
_verticalDrag?.update(verticalDetails);
|
||||
}
|
||||
if (_horizontalDrag != null) {
|
||||
final horizontalDetails = DragUpdateDetails(
|
||||
sourceTimeStamp: details.sourceTimeStamp,
|
||||
delta: Offset(details.delta.dx, 0.0),
|
||||
primaryDelta: details.delta.dx,
|
||||
globalPosition: details.globalPosition,
|
||||
localPosition: details.localPosition,
|
||||
);
|
||||
_horizontalDrag?.update(horizontalDetails);
|
||||
}
|
||||
}
|
||||
|
||||
void _handlePanEnd(DragEndDetails details) {
|
||||
final double dx = details.velocity.pixelsPerSecond.dx;
|
||||
final double dy = details.velocity.pixelsPerSecond.dy;
|
||||
|
||||
final vDrag = _verticalDrag;
|
||||
final hDrag = _horizontalDrag;
|
||||
_verticalDrag = null;
|
||||
_horizontalDrag = null;
|
||||
|
||||
if (vDrag != null) {
|
||||
final verticalDetails = DragEndDetails(
|
||||
velocity: Velocity(pixelsPerSecond: Offset(0.0, dy)),
|
||||
primaryVelocity: dy,
|
||||
);
|
||||
vDrag.end(verticalDetails);
|
||||
}
|
||||
if (hDrag != null) {
|
||||
final horizontalDetails = DragEndDetails(
|
||||
velocity: Velocity(pixelsPerSecond: Offset(dx, 0.0)),
|
||||
primaryVelocity: dx,
|
||||
);
|
||||
hDrag.end(horizontalDetails);
|
||||
}
|
||||
}
|
||||
|
||||
void _handlePanCancel() {
|
||||
final vHold = _verticalHold;
|
||||
final hHold = _horizontalHold;
|
||||
final vDrag = _verticalDrag;
|
||||
final hDrag = _horizontalDrag;
|
||||
_verticalHold = null;
|
||||
_horizontalHold = null;
|
||||
_verticalDrag = null;
|
||||
_horizontalDrag = null;
|
||||
vHold?.cancel();
|
||||
hHold?.cancel();
|
||||
vDrag?.cancel();
|
||||
hDrag?.cancel();
|
||||
}
|
||||
|
||||
void _disposeVerticalHold() {
|
||||
_verticalHold = null;
|
||||
}
|
||||
|
||||
void _disposeHorizontalHold() {
|
||||
_horizontalHold = null;
|
||||
}
|
||||
|
||||
void _disposeVerticalDrag() {
|
||||
_verticalDrag = null;
|
||||
}
|
||||
|
||||
void _disposeHorizontalDrag() {
|
||||
_horizontalDrag = null;
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scrollable(
|
||||
final bool isBidirectional = widget.bidirectionalHorizontalController != null;
|
||||
final Widget scrollable = Scrollable(
|
||||
excludeFromSemantics: true,
|
||||
controller: _effectiveController,
|
||||
scrollBehavior: _ScrollBehavior(widget.scrollbarBuilder),
|
||||
scrollBehavior: _ScrollBehavior(
|
||||
widget.scrollbarBuilder,
|
||||
enableDrag: !isBidirectional && widget.enableDrag,
|
||||
),
|
||||
viewportBuilder: widget.viewportBuilder,
|
||||
axisDirection: widget.axisDirection,
|
||||
physics: const ClampingScrollPhysics(),
|
||||
);
|
||||
|
||||
if (isBidirectional) {
|
||||
return RawGestureDetector(
|
||||
gestures: <Type, GestureRecognizerFactory>{
|
||||
PanGestureRecognizer: GestureRecognizerFactoryWithHandlers<PanGestureRecognizer>(
|
||||
() => PanGestureRecognizer(
|
||||
supportedDevices: const <PointerDeviceKind>{
|
||||
PointerDeviceKind.touch,
|
||||
PointerDeviceKind.stylus,
|
||||
PointerDeviceKind.invertedStylus,
|
||||
PointerDeviceKind.trackpad,
|
||||
},
|
||||
),
|
||||
(PanGestureRecognizer instance) {
|
||||
instance
|
||||
..onDown = _handlePanDown
|
||||
..onStart = _handlePanStart
|
||||
..onUpdate = _handlePanUpdate
|
||||
..onEnd = _handlePanEnd
|
||||
..onCancel = _handlePanCancel
|
||||
..gestureSettings = MediaQuery.maybeGestureSettingsOf(context);
|
||||
},
|
||||
),
|
||||
},
|
||||
behavior: HitTestBehavior.opaque,
|
||||
child: scrollable,
|
||||
);
|
||||
}
|
||||
|
||||
return scrollable;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -81,8 +250,13 @@ class _ScrollBehavior extends MaterialScrollBehavior {
|
||||
|
||||
final _ScrollPhysics physics;
|
||||
final CodeScrollbarBuilder? scrollbarBuilder;
|
||||
final bool enableDrag;
|
||||
|
||||
_ScrollBehavior(this.scrollbarBuilder) : physics = _ScrollPhysics();
|
||||
_ScrollBehavior(this.scrollbarBuilder, {this.enableDrag = true}) : physics = _ScrollPhysics();
|
||||
|
||||
@override
|
||||
Set<PointerDeviceKind> get dragDevices =>
|
||||
enableDrag ? super.dragDevices : const <PointerDeviceKind>{};
|
||||
|
||||
@override
|
||||
Widget buildScrollbar(BuildContext context, Widget child, ScrollableDetails details) {
|
||||
|
||||
@@ -85,7 +85,9 @@ class _CodeSelectionGestureDetectorState extends State<_CodeSelectionGestureDete
|
||||
render?.stopAutoScroll();
|
||||
if (!_isMultiTouch) {
|
||||
widget.selectionOverlayController.hideToolbar();
|
||||
widget.selectionOverlayController.hideHandle();
|
||||
if (widget.controller.selection.isCollapsed) {
|
||||
widget.selectionOverlayController.hideHandle();
|
||||
}
|
||||
}
|
||||
},
|
||||
onLongPressUp: () {
|
||||
|
||||
Reference in new issue
Block a user