Skip to content

Commit 7274ca4

Browse files
committed
revert last change
1 parent 226afa0 commit 7274ca4

File tree

4 files changed

+28
-100
lines changed

4 files changed

+28
-100
lines changed
Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,13 @@
11
// Use different implementations for web vs non-web platforms
2-
import 'package:flutter/rendering.dart';
32
import 'package:flutter_code_editor/src/code_field/browser_detection_web.dart'
43
if (dart.library.io) 'browser_detection_io.dart' as detection;
54

6-
// Function to determine if we need to use specific Chrome text selection fixes
7-
Map<String, dynamic> getChromeTextSelectionFixes() {
8-
return detection.getChromeTextSelectionFixes();
9-
}
10-
11-
// Legacy function kept for backward compatibility
12-
// Now returns null to use the default line height
13-
@deprecated
5+
// Function to determine the line height based on platform
146
double? getChromeLineHeight() {
15-
return null;
7+
return detection.isChromeBrowser() ? 1.18 : null;
168
}
179

18-
// Function to get appropriate top padding based on browser
10+
// Function to get appropriate top padding based on line height
1911
double getGutterTopPadding() {
20-
return 16.0;
12+
return getChromeLineHeight() == 1.18 ? 13.0 : 16.0;
2113
}
Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
import 'package:flutter/rendering.dart';
2-
31
bool isChromeBrowser() {
42
return false;
53
}
6-
7-
Map<String, dynamic> getChromeTextSelectionFixes() {
8-
return {
9-
'useCustomSelectionStyle': false,
10-
};
11-
}
Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
// ignore: deprecated_member_use
22
import 'dart:js' as js;
3-
import 'dart:ui';
43

54
import 'package:flutter/foundation.dart';
6-
import 'package:flutter/rendering.dart';
75

86
// Returns true if the browser is Chrome
97
bool isChromeBrowser() {
@@ -21,20 +19,3 @@ bool isChromeBrowser() {
2119
}
2220
return false;
2321
}
24-
25-
// Optimizes text selection in Chrome by adjusting selection behavior
26-
// instead of modifying line height
27-
Map<String, dynamic> getChromeTextSelectionFixes() {
28-
if (!isChromeBrowser()) {
29-
return {
30-
'useCustomSelectionStyle': false,
31-
};
32-
}
33-
34-
return {
35-
'useCustomSelectionStyle': true,
36-
'selectionHeightFix': true,
37-
'leadingDistribution': TextLeadingDistribution.even,
38-
'selectionAlignmentFix': true,
39-
};
40-
}

lib/src/code_field/code_field.dart

Lines changed: 24 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -366,16 +366,11 @@ class _CodeFieldState extends State<CodeField> {
366366

367367
textStyle = defaultTextStyle.merge(widget.textStyle);
368368

369-
// Get Chrome-specific text selection fixes
370-
final chromeTextSelectionFixes = getChromeTextSelectionFixes();
371-
final useCustomSelectionStyle = chromeTextSelectionFixes['useCustomSelectionStyle'] as bool;
372-
373-
// Adjust textStyle to have consistent line height and Chrome-specific fixes
369+
// Adjust textStyle to have consistent line height
370+
// This is a key fix for Chrome selection issues
374371
final adjustedTextStyle = textStyle.copyWith(
375-
height: getLineHeight(),
376-
leadingDistribution: useCustomSelectionStyle
377-
? chromeTextSelectionFixes['leadingDistribution'] as TextLeadingDistribution
378-
: TextLeadingDistribution.proportional,
372+
height: getLineHeight(), // Use the function instead of constant
373+
leadingDistribution: TextLeadingDistribution.even, // Added for consistent text baselines
379374
);
380375

381376
final codeField = TextField(
@@ -389,17 +384,15 @@ class _CodeFieldState extends State<CodeField> {
389384
scrollController: _codeScroll,
390385
keyboardType: widget.keyboardType,
391386
selectionControls: kIsWeb ? DesktopTextSelectionControls() : MaterialTextSelectionControls(),
392-
decoration: InputDecoration(
387+
decoration: const InputDecoration(
393388
isCollapsed: true,
394-
// Add vertical padding - adjust based on selection fixes
395-
contentPadding: EdgeInsets.symmetric(
396-
vertical: useCustomSelectionStyle ? 14.0 : 16.0,
397-
),
389+
// Add more vertical padding to help with line selection
390+
contentPadding: EdgeInsets.symmetric(vertical: 16),
398391
disabledBorder: InputBorder.none,
399392
border: InputBorder.none,
400393
focusedBorder: InputBorder.none,
401394
),
402-
textAlignVertical: TextAlignVertical.top,
395+
textAlignVertical: TextAlignVertical.top, // Align text at the top for better matching
403396
cursorColor: widget.cursorColor ?? defaultTextStyle.color,
404397
cursorHeight: widget.cursorHeight,
405398
autocorrect: false,
@@ -421,7 +414,14 @@ class _CodeFieldState extends State<CodeField> {
421414

422415
final editingField = Theme(
423416
data: Theme.of(context).copyWith(
424-
textSelectionTheme: _getTextSelectionTheme(context),
417+
textSelectionTheme: widget.textSelectionTheme ??
418+
TextSelectionThemeData(
419+
// Use a more pronounced and distinguishable selection color
420+
// This helps with Chrome's selection rendering
421+
selectionColor: Theme.of(context).colorScheme.primary.withOpacity(0.5),
422+
cursorColor: widget.cursorColor ?? defaultTextStyle.color,
423+
selectionHandleColor: Theme.of(context).colorScheme.primary,
424+
),
425425
),
426426
child: LayoutBuilder(
427427
builder: (BuildContext context, BoxConstraints constraints) {
@@ -458,20 +458,14 @@ class _CodeFieldState extends State<CodeField> {
458458
final lineNumberSize = textStyle.fontSize;
459459
final lineNumberColor = widget.gutterStyle.textStyle?.color ?? textStyle.color?.withOpacity(.5);
460460

461-
// Get Chrome-specific text selection fixes
462-
final chromeTextSelectionFixes = getChromeTextSelectionFixes();
463-
final useCustomSelectionStyle = chromeTextSelectionFixes['useCustomSelectionStyle'] as bool;
464-
465461
// Ensure same text style properties for consistent line height
466462
final lineNumberTextStyle = (widget.gutterStyle.textStyle ?? textStyle).copyWith(
467463
color: lineNumberColor,
468464
fontFamily: textStyle.fontFamily,
469465
fontSize: lineNumberSize,
470-
height: getLineHeight(),
466+
height: getLineHeight(), // Use the function instead of constant
471467
// Add additional properties to ensure metrics consistency
472-
leadingDistribution: useCustomSelectionStyle
473-
? chromeTextSelectionFixes['leadingDistribution'] as TextLeadingDistribution
474-
: TextLeadingDistribution.proportional,
468+
leadingDistribution: TextLeadingDistribution.even,
475469
);
476470

477471
final gutterStyle = widget.gutterStyle.copyWith(
@@ -555,11 +549,9 @@ class _CodeFieldState extends State<CodeField> {
555549
return renderBox.localToGlobal(Offset.zero);
556550
}
557551

558-
// Get Chrome-specific text selection fixes
559-
final chromeTextSelectionFixes = getChromeTextSelectionFixes();
560-
final useCustomSelectionStyle = chromeTextSelectionFixes['useCustomSelectionStyle'] as bool;
561-
final selectionHeightFix = useCustomSelectionStyle ?
562-
(chromeTextSelectionFixes['selectionHeightFix'] as bool? ?? false) : false;
552+
// Instead of calculating line position based on newlines,
553+
// we'll use the TextPainter's getOffsetForCaret method which
554+
// properly accounts for text wrapping
563555

564556
// Create a text painter for the entire text
565557
final fullTextPainter = TextPainter(
@@ -585,21 +577,15 @@ class _CodeFieldState extends State<CodeField> {
585577

586578
// Apply scroll offsets
587579
final double adjustedX = rawOffset.dx - horizontalScrollOffset;
588-
double adjustedY = rawOffset.dy - scrollY;
589-
590-
// Apply Chrome-specific vertical adjustment if needed
591-
if (selectionHeightFix) {
592-
// This small offset helps Chrome render selections correctly without changing line height
593-
adjustedY += 2.0;
594-
}
580+
final double adjustedY = rawOffset.dy - scrollY;
595581

596582
// Convert to global coordinates
597583
return renderBox.localToGlobal(Offset(adjustedX, adjustedY));
598584
}
599585

600586
double getLineHeight() {
601-
// Use a consistent line height that looks good (not the special Chrome value)
602-
return textStyle.height ?? 1.2;
587+
// Default line height multiple if not specified in the style
588+
return getChromeLineHeight() ?? (textStyle.height ?? 1.2);
603589
}
604590

605591
void _onPopupStateChanged() {
@@ -654,27 +640,4 @@ class _CodeFieldState extends State<CodeField> {
654640
}
655641
}
656642
}
657-
658-
TextSelectionThemeData _getTextSelectionTheme(BuildContext context) {
659-
final chromeTextSelectionFixes = getChromeTextSelectionFixes();
660-
final useCustomSelectionStyle = chromeTextSelectionFixes['useCustomSelectionStyle'] as bool;
661-
662-
// Create a more optimized selection style for Chrome
663-
if (useCustomSelectionStyle) {
664-
return widget.textSelectionTheme ??
665-
TextSelectionThemeData(
666-
selectionColor: Theme.of(context).colorScheme.primary.withOpacity(0.3),
667-
cursorColor: widget.cursorColor ?? textStyle.color,
668-
selectionHandleColor: Theme.of(context).colorScheme.primary,
669-
);
670-
}
671-
672-
// Default selection style for other browsers
673-
return widget.textSelectionTheme ??
674-
TextSelectionThemeData(
675-
selectionColor: Theme.of(context).colorScheme.primary.withOpacity(0.5),
676-
cursorColor: widget.cursorColor ?? textStyle.color,
677-
selectionHandleColor: Theme.of(context).colorScheme.primary,
678-
);
679-
}
680643
}

0 commit comments

Comments
 (0)