1
This commit is contained in:
1 parent
ce598b2d86
commit
b5328277ab
17 files changed
+1656
-242
No files matched your search
@@ -28,6 +28,8 @@ class _CodeEditable extends StatefulWidget {
|
||||
final EdgeInsetsGeometry margin;
|
||||
final Widget? leadingDivider;
|
||||
final bool pinLineNumbers;
|
||||
final double extraHorizontalScroll;
|
||||
final double? extraVerticalScroll;
|
||||
final Border? border;
|
||||
final BorderRadius? borderRadius;
|
||||
final Clip clipBehavior;
|
||||
@@ -69,7 +71,9 @@ class _CodeEditable extends StatefulWidget {
|
||||
required this.padding,
|
||||
required this.margin,
|
||||
required this.leadingDivider,
|
||||
this.pinLineNumbers = true,
|
||||
this.pinLineNumbers = false,
|
||||
this.extraHorizontalScroll = 160.0,
|
||||
this.extraVerticalScroll,
|
||||
this.border,
|
||||
this.borderRadius,
|
||||
this.clipBehavior = Clip.none,
|
||||
@@ -313,10 +317,15 @@ class _CodeEditableState extends State<_CodeEditable> with AutomaticKeepAliveCli
|
||||
onTapOutside: (_) {
|
||||
widget.focusNode.unfocus();
|
||||
},
|
||||
child: NotificationListener(
|
||||
child: NotificationListener<ScrollNotification>(
|
||||
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((_) {
|
||||
@@ -335,6 +344,17 @@ 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,
|
||||
@@ -364,6 +384,8 @@ class _CodeEditableState extends State<_CodeEditable> with AutomaticKeepAliveCli
|
||||
cursorWidth: widget.cursorWidth,
|
||||
padding: widget.padding,
|
||||
readOnly: widget.readOnly,
|
||||
extraHorizontalScroll: widget.extraHorizontalScroll,
|
||||
extraVerticalScroll: widget.extraVerticalScroll,
|
||||
// Enable long text rendering when the find is on.
|
||||
maxLengthSingleLineRendering: widget.findController.value != null ? null : widget.maxLengthSingleLineRendering,
|
||||
startHandleLayerLink: widget.startHandleLayerLink,
|
||||
|
||||
@@ -25,6 +25,8 @@ class _CodeField extends SingleChildRenderObjectWidget {
|
||||
final double floatingCursorWidth;
|
||||
final EdgeInsetsGeometry padding;
|
||||
final bool readOnly;
|
||||
final double extraHorizontalScroll;
|
||||
final double? extraVerticalScroll;
|
||||
final int? maxLengthSingleLineRendering;
|
||||
final LayerLink startHandleLayerLink;
|
||||
final LayerLink endHandleLayerLink;
|
||||
@@ -54,6 +56,8 @@ class _CodeField extends SingleChildRenderObjectWidget {
|
||||
floatingCursorWidth,
|
||||
required this.padding,
|
||||
required this.readOnly,
|
||||
this.extraHorizontalScroll = 160.0,
|
||||
this.extraVerticalScroll,
|
||||
this.maxLengthSingleLineRendering,
|
||||
required this.startHandleLayerLink,
|
||||
required this.endHandleLayerLink,
|
||||
@@ -87,6 +91,8 @@ class _CodeField extends SingleChildRenderObjectWidget {
|
||||
floatingCursorWidth: floatingCursorWidth,
|
||||
padding: padding,
|
||||
readOnly: readOnly,
|
||||
extraHorizontalScroll: extraHorizontalScroll,
|
||||
extraVerticalScroll: extraVerticalScroll,
|
||||
maxLengthSingleLineRendering: maxLengthSingleLineRendering,
|
||||
startHandleLayerLink: startHandleLayerLink,
|
||||
endHandleLayerLink: endHandleLayerLink,
|
||||
@@ -118,6 +124,8 @@ class _CodeField extends SingleChildRenderObjectWidget {
|
||||
..floatingCursorWidth = floatingCursorWidth
|
||||
..padding = padding
|
||||
..readOnly = readOnly
|
||||
..extraHorizontalScroll = extraHorizontalScroll
|
||||
..extraVerticalScroll = extraVerticalScroll
|
||||
..maxLengthSingleLineRendering = maxLengthSingleLineRendering
|
||||
..startHandleLayerLink = startHandleLayerLink
|
||||
..endHandleLayerLink = endHandleLayerLink;
|
||||
@@ -143,6 +151,8 @@ class _CodeFieldRender extends RenderBox implements MouseTrackerAnnotation {
|
||||
ValueChanged<List<CodeLineRenderParagraph>> _onRenderParagraphsChanged;
|
||||
EdgeInsetsGeometry _padding;
|
||||
bool _readOnly;
|
||||
double _extraHorizontalScroll;
|
||||
double? _extraVerticalScroll;
|
||||
int? _maxLengthSingleLineRendering;
|
||||
Color? _chunkIndicatorColor;
|
||||
|
||||
@@ -184,6 +194,8 @@ class _CodeFieldRender extends RenderBox implements MouseTrackerAnnotation {
|
||||
required double floatingCursorWidth,
|
||||
required EdgeInsetsGeometry padding,
|
||||
required bool readOnly,
|
||||
double extraHorizontalScroll = 160.0,
|
||||
double? extraVerticalScroll,
|
||||
int? maxLengthSingleLineRendering,
|
||||
required LayerLink startHandleLayerLink,
|
||||
required LayerLink endHandleLayerLink,
|
||||
@@ -201,6 +213,8 @@ class _CodeFieldRender extends RenderBox implements MouseTrackerAnnotation {
|
||||
_onRenderParagraphsChanged = onRenderParagraphsChanged,
|
||||
_padding = padding,
|
||||
_readOnly = readOnly,
|
||||
_extraHorizontalScroll = extraHorizontalScroll,
|
||||
_extraVerticalScroll = extraVerticalScroll,
|
||||
_maxLengthSingleLineRendering = maxLengthSingleLineRendering,
|
||||
_chunkIndicatorColor = chunkIndicatorColor,
|
||||
_paint = Paint(),
|
||||
@@ -531,6 +545,22 @@ class _CodeFieldRender extends RenderBox implements MouseTrackerAnnotation {
|
||||
markNeedsLayout();
|
||||
}
|
||||
|
||||
double get extraHorizontalScroll => _extraHorizontalScroll;
|
||||
|
||||
set extraHorizontalScroll(double value) {
|
||||
if (_extraHorizontalScroll == value) return;
|
||||
_extraHorizontalScroll = value;
|
||||
markNeedsLayout();
|
||||
}
|
||||
|
||||
double? get extraVerticalScroll => _extraVerticalScroll;
|
||||
|
||||
set extraVerticalScroll(double? value) {
|
||||
if (_extraVerticalScroll == value) return;
|
||||
_extraVerticalScroll = value;
|
||||
markNeedsLayout();
|
||||
}
|
||||
|
||||
List<CodeLineRenderParagraph> get displayParagraphs => _displayParagraphs;
|
||||
|
||||
Offset get paintOffset => Offset(_horizontalViewport?.pixels ?? 0, _verticalViewport.pixels);
|
||||
@@ -1312,7 +1342,7 @@ class _CodeFieldRender extends RenderBox implements MouseTrackerAnnotation {
|
||||
if (target <= paddingTop) {
|
||||
startIndex = 0;
|
||||
} else {
|
||||
startIndex = min(((target - paddingTop) / _preferredLineHeight).ceil(), _codes.length - 1);
|
||||
startIndex = min(((target - paddingTop) / _preferredLineHeight).ceil(), max(0, _codes.length - 1));
|
||||
}
|
||||
_displayParagraphs.addAll(_buildDisplayRenderParagraphs(startIndex, effectiveWidth));
|
||||
} else {
|
||||
@@ -1342,7 +1372,7 @@ class _CodeFieldRender extends RenderBox implements MouseTrackerAnnotation {
|
||||
if (target <= paddingTop) {
|
||||
startIndex = 0;
|
||||
} else {
|
||||
startIndex = (target / _preferredLineHeight).floor();
|
||||
startIndex = min((target / _preferredLineHeight).floor(), max(0, _codes.length - 1));
|
||||
}
|
||||
_displayParagraphs.clear();
|
||||
_displayParagraphs.addAll(_buildDisplayRenderParagraphs(startIndex, effectiveWidth));
|
||||
@@ -1364,18 +1394,27 @@ class _CodeFieldRender extends RenderBox implements MouseTrackerAnnotation {
|
||||
}
|
||||
// The codes length maybe changed, this will make the displayParagraphs empty.
|
||||
if (_displayParagraphs.isEmpty) {
|
||||
_updateDisplayRenderParagraphs();
|
||||
return;
|
||||
if (_codes.isNotEmpty) {
|
||||
_displayParagraphs.addAll(_buildDisplayRenderParagraphs(max(0, _codes.length - 1), effectiveWidth));
|
||||
}
|
||||
if (_displayParagraphs.isEmpty) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
final double totalHeight = _displayParagraphs.last.bottom + (_codes.length - (_displayParagraphs.last.index + 1)) * _preferredLineHeight + paddingBottom;
|
||||
_verticalViewportSize = max(0, totalHeight - size.height);
|
||||
final double extraBottom = _extraVerticalScroll ?? max(0.0, size.height * 0.5);
|
||||
_verticalViewportSize = max(0, totalHeight - size.height + extraBottom);
|
||||
if (_verticalViewport.pixels > _verticalViewportSize!) {
|
||||
_verticalViewport.correctBy(_verticalViewportSize! - _verticalViewport.pixels);
|
||||
}
|
||||
_verticalViewport.applyContentDimensions(0, _verticalViewportSize!);
|
||||
if (_horizontalViewport != null) {
|
||||
final double maxWidth = _displayParagraphs.map((e) => e.width).reduce(max);
|
||||
_horizontalViewportSize = max(0, maxWidth + _padding.horizontal - size.width);
|
||||
if (maxWidth + _padding.horizontal > size.width) {
|
||||
_horizontalViewportSize = max(0, maxWidth + _padding.horizontal - size.width + _extraHorizontalScroll);
|
||||
} else {
|
||||
_horizontalViewportSize = 0;
|
||||
}
|
||||
_horizontalViewport!.applyContentDimensions(0, _horizontalViewportSize!);
|
||||
}
|
||||
// applyContentDimensions will change the _verticalViewport.pixels, we should rebuild.
|
||||
@@ -1578,6 +1617,7 @@ class _CodeFieldRender extends RenderBox implements MouseTrackerAnnotation {
|
||||
}
|
||||
|
||||
List<CodeLineRenderParagraph> _buildDisplayRenderParagraphs(int startIndex, double maxWidth) {
|
||||
startIndex = min(max(0, startIndex), max(0, _codes.length - 1));
|
||||
double offset = startIndex * _preferredLineHeight;
|
||||
final List<CodeLineRenderParagraph> paragraphs = [];
|
||||
for (int i = startIndex; i < _codes.length; i++) {
|
||||
|
||||
@@ -2,7 +2,7 @@ part of re_editor;
|
||||
|
||||
const double _kScrollbarThickness = 8.0;
|
||||
|
||||
class _CodeScrollable extends StatelessWidget {
|
||||
class _CodeScrollable extends StatefulWidget {
|
||||
|
||||
final AxisDirection axisDirection;
|
||||
final ScrollController? controller;
|
||||
@@ -10,20 +10,67 @@ class _CodeScrollable extends StatelessWidget {
|
||||
final CodeScrollbarBuilder? scrollbarBuilder;
|
||||
|
||||
const _CodeScrollable({
|
||||
super.key,
|
||||
required this.axisDirection,
|
||||
this.controller,
|
||||
required this.viewportBuilder,
|
||||
this.scrollbarBuilder
|
||||
this.scrollbarBuilder,
|
||||
});
|
||||
|
||||
@override
|
||||
State<_CodeScrollable> createState() => _CodeScrollableState();
|
||||
|
||||
}
|
||||
|
||||
class _CodeScrollableState extends State<_CodeScrollable> {
|
||||
|
||||
CodeEditorScrollController? _effectiveController;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_initController();
|
||||
}
|
||||
|
||||
void _initController() {
|
||||
if (widget.controller is CodeEditorScrollController) {
|
||||
_effectiveController = widget.controller as CodeEditorScrollController;
|
||||
} else {
|
||||
_effectiveController = CodeEditorScrollController(
|
||||
delegate: widget.controller,
|
||||
initialScrollOffset: widget.controller?.initialScrollOffset ?? 0.0,
|
||||
keepScrollOffset: widget.controller?.keepScrollOffset ?? true,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void didUpdateWidget(_CodeScrollable oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
if (widget.controller != oldWidget.controller) {
|
||||
if (_effectiveController != null && _effectiveController!.delegate != null) {
|
||||
_effectiveController!.dispose();
|
||||
}
|
||||
_initController();
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
if (_effectiveController != null && _effectiveController!.delegate != null) {
|
||||
_effectiveController!.dispose();
|
||||
}
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scrollable(
|
||||
excludeFromSemantics: true,
|
||||
controller: controller,
|
||||
scrollBehavior: _ScrollBehavior(scrollbarBuilder),
|
||||
viewportBuilder: viewportBuilder,
|
||||
axisDirection: axisDirection,
|
||||
controller: _effectiveController,
|
||||
scrollBehavior: _ScrollBehavior(widget.scrollbarBuilder),
|
||||
viewportBuilder: widget.viewportBuilder,
|
||||
axisDirection: widget.axisDirection,
|
||||
physics: const ClampingScrollPhysics(),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -37,12 +37,15 @@ class _CodeSelectionGestureDetectorState extends State<_CodeSelectionGestureDete
|
||||
|
||||
_CodeFieldRender? get render => widget.editorKey.currentContext?.findRenderObject() as _CodeFieldRender?;
|
||||
|
||||
int _activePointers = 0;
|
||||
bool _isMultiTouch = false;
|
||||
bool _tapping = false;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final Widget detector;
|
||||
if (_isMobile) {
|
||||
return GestureDetector(
|
||||
detector = GestureDetector(
|
||||
onLongPressMoveUpdate: (details) {
|
||||
if (_longPressOnSelection == true) {
|
||||
return;
|
||||
@@ -80,8 +83,10 @@ class _CodeSelectionGestureDetectorState extends State<_CodeSelectionGestureDete
|
||||
_longPressOnSelection = false;
|
||||
_dragging = false;
|
||||
render?.stopAutoScroll();
|
||||
widget.selectionOverlayController.hideToolbar();
|
||||
widget.selectionOverlayController.hideHandle();
|
||||
if (!_isMultiTouch) {
|
||||
widget.selectionOverlayController.hideToolbar();
|
||||
widget.selectionOverlayController.hideHandle();
|
||||
}
|
||||
},
|
||||
onLongPressUp: () {
|
||||
_dragPosition = null;
|
||||
@@ -167,6 +172,37 @@ class _CodeSelectionGestureDetectorState extends State<_CodeSelectionGestureDete
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
return Listener(
|
||||
behavior: HitTestBehavior.translucent,
|
||||
onPointerDown: (event) {
|
||||
_activePointers++;
|
||||
if (_activePointers >= 2) {
|
||||
_isMultiTouch = true;
|
||||
}
|
||||
},
|
||||
onPointerUp: (event) {
|
||||
_activePointers = max(0, _activePointers - 1);
|
||||
if (_activePointers == 0) {
|
||||
SchedulerBinding.instance.addPostFrameCallback((_) {
|
||||
if (mounted && _activePointers == 0) {
|
||||
_isMultiTouch = false;
|
||||
}
|
||||
});
|
||||
}
|
||||
},
|
||||
onPointerCancel: (event) {
|
||||
_activePointers = max(0, _activePointers - 1);
|
||||
if (_activePointers == 0) {
|
||||
SchedulerBinding.instance.addPostFrameCallback((_) {
|
||||
if (mounted && _activePointers == 0) {
|
||||
_isMultiTouch = false;
|
||||
}
|
||||
});
|
||||
}
|
||||
},
|
||||
child: detector,
|
||||
);
|
||||
}
|
||||
|
||||
bool get _isMobile => kIsAndroid || kIsIOS;
|
||||
@@ -178,6 +214,9 @@ class _CodeSelectionGestureDetectorState extends State<_CodeSelectionGestureDete
|
||||
}.contains);
|
||||
|
||||
void _onMobileTapDown(Offset position) {
|
||||
if (_isMultiTouch) {
|
||||
return;
|
||||
}
|
||||
if (!widget.controller.selection.isCollapsed && _isPositionOnSelection(position)) {
|
||||
return;
|
||||
}
|
||||
@@ -187,6 +226,12 @@ class _CodeSelectionGestureDetectorState extends State<_CodeSelectionGestureDete
|
||||
}
|
||||
|
||||
void _onMobileTapUp(Offset position) {
|
||||
if (_isMultiTouch) {
|
||||
if (!widget.controller.selection.isCollapsed) {
|
||||
widget.selectionOverlayController.showHandle(context);
|
||||
}
|
||||
return;
|
||||
}
|
||||
final DateTime now = DateTime.now();
|
||||
if (_pointerTapTimestamp != null && (now.millisecondsSinceEpoch - _pointerTapTimestamp!.millisecondsSinceEpoch) <
|
||||
kDoubleTapTimeout.inMilliseconds && _pointerTapPosition != null && _pointerTapPosition!.isSamePosition(position)) {
|
||||
@@ -662,9 +707,9 @@ class _MobileSelectionOverlayController implements _SelectionOverlayController {
|
||||
: 0;
|
||||
|
||||
if (selection.startIndex <= firstVisibleLine && selection.endIndex >= lastVisibleLine) {
|
||||
// 框选文本完全占据当前屏幕:在中间显示
|
||||
placement = EditorToolbarPlacement.center;
|
||||
targetOffset = editingRegion.center;
|
||||
// 框选文本完全占据当前屏幕:显示在上方(避免在中间遮挡文本)
|
||||
placement = EditorToolbarPlacement.top;
|
||||
targetOffset = Offset(editingRegion.center.dx, editingRegion.top);
|
||||
} else if (selection.endIndex < firstVisibleLine) {
|
||||
// 选区整体在屏幕上方:在上面显示
|
||||
placement = EditorToolbarPlacement.top;
|
||||
@@ -674,9 +719,9 @@ class _MobileSelectionOverlayController implements _SelectionOverlayController {
|
||||
placement = EditorToolbarPlacement.bottom;
|
||||
targetOffset = Offset(editingRegion.center.dx, editingRegion.bottom);
|
||||
} else {
|
||||
// 兜底(水平滚动偏离等异常情况):在中间显示
|
||||
placement = EditorToolbarPlacement.center;
|
||||
targetOffset = editingRegion.center;
|
||||
// 兜底(水平滚动偏离等异常情况):显示在上方(避免在中间遮挡文本)
|
||||
placement = EditorToolbarPlacement.top;
|
||||
targetOffset = Offset(editingRegion.center.dx, editingRegion.top);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -757,6 +802,10 @@ class _MobileSelectionOverlayController implements _SelectionOverlayController {
|
||||
void _buildHandles(BuildContext context) {
|
||||
final bool isCollapsed = controller.selection.isCollapsed;
|
||||
if (_handleCollapsed == isCollapsed) {
|
||||
if (_handles != null) {
|
||||
_handles![0].markNeedsBuild();
|
||||
_handles![1].markNeedsBuild();
|
||||
}
|
||||
return;
|
||||
}
|
||||
_handleCollapsed = isCollapsed;
|
||||
|
||||
@@ -199,7 +199,7 @@ class CodeEditor extends StatefulWidget {
|
||||
this.shortcutsActivatorsBuilder,
|
||||
this.shortcutOverrideActions,
|
||||
this.leadingDivider,
|
||||
this.pinLineNumbers = true,
|
||||
this.pinLineNumbers = false,
|
||||
this.border,
|
||||
this.borderRadius,
|
||||
this.clipBehavior = Clip.none,
|
||||
@@ -212,6 +212,8 @@ class CodeEditor extends StatefulWidget {
|
||||
this.maxLengthSingleLineRendering,
|
||||
this.chunkAnalyzer,
|
||||
this.commentFormatter,
|
||||
this.extraHorizontalScroll = 160.0,
|
||||
this.extraVerticalScroll,
|
||||
}) : assert(indicatorBuilder != null || (indicatorBuilder == null && leadingDivider == null));
|
||||
|
||||
/// Similar to [TextField], editor uses [CodeLineEditingController] as the content controller.
|
||||
@@ -273,6 +275,14 @@ class CodeEditor extends StatefulWidget {
|
||||
/// Whether line numbers and indicator are pinned to the left during horizontal scroll.
|
||||
final bool pinLineNumbers;
|
||||
|
||||
/// Extra horizontal scrollable distance (overscroll beyond the longest line).
|
||||
final double extraHorizontalScroll;
|
||||
|
||||
/// Extra vertical scrollable distance (overscroll beyond the last line).
|
||||
/// If null, defaults to half of the viewport height (size.height * 0.5), allowing
|
||||
/// the last line to scroll up to the center of the viewport.
|
||||
final double? extraVerticalScroll;
|
||||
|
||||
/// The border of the editor.
|
||||
final Border? border;
|
||||
|
||||
@@ -528,6 +538,8 @@ class _CodeEditorState extends State<CodeEditor> {
|
||||
showCursorWhenReadOnly: widget.showCursorWhenReadOnly ?? true,
|
||||
leadingDivider: widget.leadingDivider,
|
||||
pinLineNumbers: widget.pinLineNumbers,
|
||||
extraHorizontalScroll: widget.extraHorizontalScroll,
|
||||
extraVerticalScroll: widget.extraVerticalScroll,
|
||||
border: widget.border,
|
||||
borderRadius: widget.borderRadius,
|
||||
clipBehavior: widget.clipBehavior,
|
||||
|
||||
@@ -2,6 +2,97 @@ part of re_editor;
|
||||
|
||||
typedef CodeScrollbarBuilder = Widget Function(BuildContext context, Widget child, ScrollableDetails details);
|
||||
|
||||
/// Custom ScrollPosition for code editor that prevents blocking child pointers
|
||||
/// during ballistic scrolling (inertia).
|
||||
///
|
||||
/// In standard Flutter, [ScrollPosition.shouldIgnorePointer] returns true during
|
||||
/// ballistic activity, which causes [Scrollable] to wrap its viewport in an
|
||||
/// [IgnorePointer(ignoring: true)]. In nested bidirectional scrolling (outer vertical,
|
||||
/// inner horizontal), this completely blocked the inner horizontal scrollable from
|
||||
/// receiving the initial touch-down when vertical was scrolling, forcing the user
|
||||
/// to swipe twice horizontally to start horizontal scrolling. Overriding
|
||||
/// [shouldIgnorePointer] to return false allows bidirectional scroll gestures to
|
||||
/// seamlessly take over on the very first swipe.
|
||||
class CodeEditorScrollPosition extends ScrollPositionWithSingleContext {
|
||||
CodeEditorScrollPosition({
|
||||
required super.physics,
|
||||
required super.context,
|
||||
super.oldPosition,
|
||||
super.initialPixels,
|
||||
super.keepScrollOffset,
|
||||
super.debugLabel,
|
||||
});
|
||||
|
||||
@override
|
||||
bool get shouldIgnorePointer => false;
|
||||
|
||||
/// Immediately stop any ongoing scrolling animation or ballistic activity.
|
||||
void stopScrolling() {
|
||||
if (activity?.isScrolling == true) {
|
||||
goIdle();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A [ScrollController] that creates [CodeEditorScrollPosition] for smooth,
|
||||
/// non-blocking bidirectional scrolling. Supports delegating to an external
|
||||
/// [ScrollController] if provided.
|
||||
class CodeEditorScrollController extends ScrollController {
|
||||
final ScrollController? delegate;
|
||||
|
||||
CodeEditorScrollController({
|
||||
this.delegate,
|
||||
super.initialScrollOffset,
|
||||
super.keepScrollOffset,
|
||||
super.debugLabel,
|
||||
super.onAttach,
|
||||
super.onDetach,
|
||||
});
|
||||
|
||||
@override
|
||||
ScrollPosition createScrollPosition(
|
||||
ScrollPhysics physics,
|
||||
ScrollContext context,
|
||||
ScrollPosition? oldPosition,
|
||||
) {
|
||||
return CodeEditorScrollPosition(
|
||||
physics: physics,
|
||||
context: context,
|
||||
initialPixels: delegate?.initialScrollOffset ?? initialScrollOffset,
|
||||
keepScrollOffset: delegate?.keepScrollOffset ?? keepScrollOffset,
|
||||
oldPosition: oldPosition,
|
||||
debugLabel: delegate?.debugLabel ?? debugLabel,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void attach(ScrollPosition position) {
|
||||
super.attach(position);
|
||||
if (delegate != null && !delegate!.positions.contains(position)) {
|
||||
delegate!.attach(position);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void detach(ScrollPosition position) {
|
||||
if (delegate != null && delegate!.positions.contains(position)) {
|
||||
delegate!.detach(position);
|
||||
}
|
||||
super.detach(position);
|
||||
}
|
||||
|
||||
/// Immediately stop any ongoing scrolling animation or ballistic activity on all attached positions.
|
||||
void stopScrolling() {
|
||||
for (final position in positions) {
|
||||
if (position is CodeEditorScrollPosition) {
|
||||
position.stopScrolling();
|
||||
} else if (position is ScrollPositionWithSingleContext && position.activity?.isScrolling == true) {
|
||||
position.goIdle();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class CodeScrollController {
|
||||
|
||||
final ScrollController verticalScroller;
|
||||
@@ -12,8 +103,8 @@ class CodeScrollController {
|
||||
CodeScrollController({
|
||||
ScrollController? verticalScroller,
|
||||
ScrollController? horizontalScroller,
|
||||
}) : verticalScroller = verticalScroller ?? ScrollController(),
|
||||
horizontalScroller = horizontalScroller ?? ScrollController();
|
||||
}) : verticalScroller = verticalScroller ?? CodeEditorScrollController(),
|
||||
horizontalScroller = horizontalScroller ?? CodeEditorScrollController();
|
||||
|
||||
void makeCenterIfInvisible(CodeLinePosition position) {
|
||||
_render?.makePositionCenterIfInvisible(position);
|
||||
|
||||
@@ -6,9 +6,9 @@ enum EditorToolbarPlacement {
|
||||
aboveStart,
|
||||
/// 在结束端点(右端点)下方弹出,若下方空间不足则翻转至上方
|
||||
belowEnd,
|
||||
/// 选区文本完全贯穿/占据视口时,在可视屏幕正中间弹出
|
||||
/// 选区文本完全贯穿/占据视口时,在屏幕最上方贴边弹出(避免在屏幕中间遮挡文本)
|
||||
center,
|
||||
/// 选区整体在视口上方时,在视口顶部贴边弹出
|
||||
/// 选区整体在视口上方或跨屏长选区时,在屏幕最上方贴边弹出(允许覆盖 Tab/AppBar,避免遮挡文本)
|
||||
top,
|
||||
/// 选区整体在视口下方时,在视口底部贴边弹出
|
||||
bottom,
|
||||
|
||||
Reference in new issue
Block a user