Skip to content

Commit

Permalink
Add MouseTrackerAnnotationTextSpan for hovering support
Browse files Browse the repository at this point in the history
  • Loading branch information
MegatronKing committed Mar 19, 2024
1 parent dad40ef commit d915467
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 2 deletions.
13 changes: 13 additions & 0 deletions lib/src/_code_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,19 @@ class _CodeFieldRender extends RenderBox implements MouseTrackerAnnotation {
bool hitTarget = false;
if (size.contains(position)) {
result.add(BoxHitTestEntry(this, position));
final CodeLineRenderParagraph? paragraph = _findDisplayRenderParagraph(position + paintOffset);
final InlineSpan? span = paragraph?.getSpanForPosition(position - paragraph.offset + paintOffset);
if (span is MouseTrackerAnnotationTextSpan) {
result.add(HitTestEntry(_MouseTrackerAnnotationTextSpan(
id: paragraph!.index,
rects: paragraph.getRangeRects(paragraph.getRangeForSpan(span)).map((rect) {
return Rect.fromPoints(localToGlobal(rect.topLeft + paragraph.offset - paintOffset), localToGlobal(rect.bottomRight + paragraph.offset - paintOffset));
}).toList(),
span: span,
)));
} else if (span is HitTestTarget) {
result.add(HitTestEntry(span as HitTestTarget));
}
if (_chunkIndicators.where((element) => element.region.contains(position)).isNotEmpty) {
_cursor = SystemMouseCursors.click;
} else {
Expand Down
2 changes: 1 addition & 1 deletion lib/src/_code_highlight.dart
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class _CodeHighlighter extends ValueNotifier<List<_HighlightResult>> {
], style);
}

TextSpan _buildSpanFromNodes(List<_HighlightNode> nodes, TextStyle? baseStyle) {
TextSpan _buildSpanFromNodes(List<_HighlightNode> nodes, TextStyle baseStyle) {
return TextSpan(
children: nodes.map((e) => TextSpan(
text: e.value,
Expand Down
30 changes: 29 additions & 1 deletion lib/src/_code_paragraph.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class _ParagraphImpl extends IParagraph {
static const int _zwjUtf16 = 0x200d;

final String text;
final TextSpan span;
final ui.Paragraph paragraph;
final double _preferredLineHeight;
final int _lineCount;
Expand All @@ -15,6 +16,7 @@ class _ParagraphImpl extends IParagraph {

_ParagraphImpl({
required this.text,
required this.span,
required this.paragraph,
required double preferredLineHeight,
}) : _preferredLineHeight = preferredLineHeight,
Expand Down Expand Up @@ -51,7 +53,32 @@ class _ParagraphImpl extends IParagraph {

@override
TextPosition getPosition(Offset offset) {
return paragraph.getPositionForOffset(offset);
final TextPosition position = paragraph.getPositionForOffset(offset);
return position;
}

@override
InlineSpan? getSpanForPosition(TextPosition position) {
if (position.offset >= length - 1) {
return null;
}
return span.getSpanForPosition(position);
}

@override
TextRange getRangeForSpan(InlineSpan span) {
int offset = 0;
this.span.visitChildren((child) {
if (identical(child, span)) {
return false;
}
offset += child.length;
return true;
});
return TextRange(
start: offset,
end: offset + span.length
);
}

@override
Expand Down Expand Up @@ -225,6 +252,7 @@ class _CodeParagraphProvider {
paragraph.layout(_constraints!);
return _ParagraphImpl(
text: span.toPlainText(),
span: span,
paragraph: paragraph,
preferredLineHeight: _preferredLineHeight!
);
Expand Down
38 changes: 38 additions & 0 deletions lib/src/_code_span.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
part of re_editor;

@immutable
class _MouseTrackerAnnotationTextSpan extends TextSpan {

final int id;
final MouseTrackerAnnotationTextSpan span;
final List<Rect> rects;

const _MouseTrackerAnnotationTextSpan({
required this.id,
required this.rects,
required this.span,
});

@override
PointerEnterEventListener? get onEnter => (event) {
span.onEnterWithRect(event, id, rects);
};

@override
PointerExitEventListener? get onExit => (event) {
span.onExitWithRect(event, id, rects);
};

@override
int get hashCode => span.hashCode;

@override
bool operator ==(Object other) {
if (identical(this, other)) {
return true;
}
return other is _MouseTrackerAnnotationTextSpan && span == other.span &&
id == other.id;
}

}
4 changes: 4 additions & 0 deletions lib/src/code_line.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1076,6 +1076,10 @@ class CodeLineRenderParagraph {
range: paragraph.getWord(offset)
);

InlineSpan? getSpanForPosition(Offset offset) => paragraph.getSpanForPosition(getPosition(offset));

TextRange getRangeForSpan(InlineSpan span) => paragraph.getRangeForSpan(span);

Offset? getOffset(TextPosition position) => paragraph.getOffset(position);

List<Rect> getRangeRects(TextRange range) => paragraph.getRangeRects(range);
Expand Down
4 changes: 4 additions & 0 deletions lib/src/code_paragraph.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ abstract class IParagraph {
TextPosition getPosition(Offset offset);

TextRange getWord(Offset offset);

InlineSpan? getSpanForPosition(TextPosition position);

TextRange getRangeForSpan(InlineSpan span);

TextRange getLineBoundary(TextPosition position);

Expand Down
26 changes: 26 additions & 0 deletions lib/src/code_span.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
part of re_editor;

typedef PointerEnterEventWithRectListener = void Function(PointerEnterEvent event, int id, List<Rect> rects);

typedef PointerExitEventWithRectListener = void Function(PointerExitEvent event, int id, List<Rect> rects);

@immutable
class MouseTrackerAnnotationTextSpan extends TextSpan {

final PointerEnterEventWithRectListener onEnterWithRect;
final PointerExitEventWithRectListener onExitWithRect;

const MouseTrackerAnnotationTextSpan({
super.text,
super.children,
super.style,
super.recognizer,
super.mouseCursor,
super.semanticsLabel,
super.locale,
super.spellOut,
required this.onEnterWithRect,
required this.onExitWithRect,
});

}
2 changes: 2 additions & 0 deletions lib/src/re_editor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ part '_code_paragraph.dart';
part '_code_scroll.dart';
part '_code_selection.dart';
part '_code_shortcuts.dart';
part '_code_span.dart';
part '_isolate.dart';
part 'code_autocomplete.dart';
part 'code_chunk.dart';
Expand All @@ -43,6 +44,7 @@ part 'code_lines.dart';
part 'code_paragraph.dart';
part 'code_shortcuts.dart';
part 'code_scroll.dart';
part 'code_span.dart';
part 'code_theme.dart';
part 'code_toolbar.dart';
part 'debug/_trace.dart';

0 comments on commit d915467

Please sign in to comment.