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

Refactor suggestions #225

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
22 changes: 12 additions & 10 deletions example/lib/03.change_language_theme/home_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -104,17 +104,19 @@ class _HomeScreenState extends State<HomeScreen> {
children: [
CodeTheme(
data: CodeThemeData(styles: themes[_theme]),
child: CodeField(
focusNode: _codeFieldFocusNode,
controller: _codeController,
textStyle: const TextStyle(fontFamily: 'SourceCode'),
gutterStyle: GutterStyle(
textStyle: const TextStyle(
color: Colors.purple,
child: CodeFieldOverlay(
child: CodeField(
focusNode: _codeFieldFocusNode,
controller: _codeController,
textStyle: const TextStyle(fontFamily: 'SourceCode'),
gutterStyle: GutterStyle(
textStyle: const TextStyle(
color: Colors.purple,
),
showLineNumbers: _showNumbers,
showErrors: _showErrors,
showFoldingHandles: _showFoldingHandles,
),
showLineNumbers: _showNumbers,
showErrors: _showErrors,
showFoldingHandles: _showFoldingHandles,
),
),
),
Expand Down
1 change: 1 addition & 0 deletions lib/flutter_code_editor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export 'src/code/tokens.dart';

export 'src/code_field/code_controller.dart';
export 'src/code_field/code_field.dart';
export 'src/code_field/code_field_overlay.dart';
export 'src/code_field/editor_params.dart';
export 'src/code_field/text_editing_value.dart';

Expand Down
28 changes: 14 additions & 14 deletions lib/src/code_field/code_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -493,20 +493,20 @@ class _CodeFieldState extends State<CodeField> {
}

void _onPopupStateChanged() {
final shouldShow =
widget.controller.popupController.shouldShow && windowSize != null;
if (!shouldShow) {
_suggestionsPopup?.remove();
_suggestionsPopup = null;
return;
}

if (_suggestionsPopup == null) {
_suggestionsPopup = _buildSuggestionOverlay();
Overlay.of(context).insert(_suggestionsPopup!);
}

_suggestionsPopup!.markNeedsBuild();
// final shouldShow =
// widget.controller.popupController.shouldShow && windowSize != null;
// if (!shouldShow) {
// _suggestionsPopup?.remove();
// _suggestionsPopup = null;
// return;
// }

// if (_suggestionsPopup == null) {
// _suggestionsPopup = _buildSuggestionOverlay();
// Overlay.of(context).insert(_suggestionsPopup!);
// }

// _suggestionsPopup!.markNeedsBuild();
}

OverlayEntry _buildSuggestionOverlay() {
Expand Down
174 changes: 174 additions & 0 deletions lib/src/code_field/code_field_overlay.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
import 'dart:math';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:flutter/src/widgets/placeholder.dart';

import '../../flutter_code_editor.dart';
import '../sizes.dart';
import '../wip/autocomplete/popup.dart';
import '../wip/autocomplete/popup_controller.dart';
import 'default_styles.dart';

class CodeFieldOverlay extends StatefulWidget {
final CodeField child;
late final CodeController controller;

CodeFieldOverlay({
super.key,
required this.child,
}) {
controller = child.controller;
}

@override
State<CodeFieldOverlay> createState() => _CodeFieldOverlayState();
}

class _CodeFieldOverlayState extends State<CodeFieldOverlay> {
late TextStyle textStyle;
OverlayEntry? _suggestionsPopup;
Offset _normalPopupOffset = Offset.zero;
Offset _flippedPopupOffset = Offset.zero;
Offset? _editorOffset;
Size? windowSize;
Color _backgroundCol = Colors.black;

@override
void initState() {
widget.controller.addListener(_onTextChanged);
widget.controller.addListener(_updatePopupOffset);
widget.controller.popupController.addListener(_onPopupStateChanged);

super.initState();
}

@override
Widget build(BuildContext context) {
const rootKey = 'root';

final themeData = Theme.of(context);
final styles = CodeTheme.of(context)?.styles;
_backgroundCol =
styles?[rootKey]?.backgroundColor ?? DefaultStyles.backgroundColor;

final defaultTextStyle = TextStyle(
color: styles?[rootKey]?.color ?? DefaultStyles.textColor,
fontSize: themeData.textTheme.titleMedium?.fontSize,
);

textStyle = defaultTextStyle.merge(widget.child.textStyle);

return widget.child;
}

void _updatePopupOffset() {
final textPainter = _getTextPainter(widget.controller.text);
final caretHeight = _getCaretHeight(textPainter);

final leftOffset = _getPopupLeftOffset(textPainter);
final normalTopOffset = _getPopupTopOffset(textPainter, caretHeight);
final flippedTopOffset = normalTopOffset -
(Sizes.autocompletePopupMaxHeight + caretHeight + Sizes.caretPadding);

setState(() {
_normalPopupOffset = Offset(leftOffset, normalTopOffset);
_flippedPopupOffset = Offset(leftOffset, flippedTopOffset);
});
}

TextPainter _getTextPainter(String text) {
return TextPainter(
textDirection: TextDirection.ltr,
text: TextSpan(text: text, style: textStyle),
)..layout();
}

Offset _getCaretOffset(TextPainter textPainter) {
return textPainter.getOffsetForCaret(
widget.controller.selection.base,
Rect.zero,
);
}

double _getCaretHeight(TextPainter textPainter) {
final double? caretFullHeight = textPainter.getFullHeightForCaret(
widget.controller.selection.base,
Rect.zero,
);
return (widget.controller.selection.base.offset > 0) ? caretFullHeight! : 0;
}

double _getPopupLeftOffset(TextPainter textPainter) {
return max(
_getCaretOffset(textPainter).dx +
widget.child.padding.left +
(widget.child.focusNode?.offset.dx ?? 0) +
(_editorOffset?.dx ?? 0),
0,
);
}

double _getPopupTopOffset(TextPainter textPainter, double caretHeight) {
return max(
_getCaretOffset(textPainter).dy +
caretHeight +
16 +
widget.child.padding.top +
(_editorOffset?.dy ?? 0),
0,
);
}

void _onPopupStateChanged() {
final shouldShow =
widget.controller.popupController.shouldShow && windowSize != null;
if (!shouldShow) {
_suggestionsPopup?.remove();
_suggestionsPopup = null;
return;
}

if (_suggestionsPopup == null) {
_suggestionsPopup = _buildSuggestionOverlay();
Overlay.of(context).insert(_suggestionsPopup!);
}

_suggestionsPopup!.markNeedsBuild();
}

OverlayEntry _buildSuggestionOverlay() {
return OverlayEntry(
builder: (context) {
return Popup(
normalOffset: _normalPopupOffset,
flippedOffset: _flippedPopupOffset,
controller: widget.controller.popupController,
editingWindowSize: windowSize!,
style: textStyle,
backgroundColor: Colors.black,
parentFocusNode: widget.child.focusNode!,
editorOffset: _editorOffset,
);
},
);
}

void _onTextChanged() {
final box = context.findRenderObject() as RenderBox?;
_editorOffset = box?.localToGlobal(Offset.zero);

rebuild();
}

void rebuild() {
setState(() {
WidgetsBinding.instance.addPostFrameCallback((_) {
final double width = context.size!.width;
final double height = context.size!.height;
windowSize = Size(width, height);
});
});
}
}