Skip to content

Commit 773cd14

Browse files
authored
Customize cursor opacity animates (#1165)
2 parents b33b510 + 13a8cb5 commit 773cd14

File tree

4 files changed

+37
-3
lines changed

4 files changed

+37
-3
lines changed

.vscode/settings.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
{
22
"dart.analysisExcludedFolders": [
33
"lib/l10n"
4-
]
4+
],
5+
"dart.lineLength": 80,
6+
"[dart]": {
7+
"editor.rulers": [
8+
80
9+
],
10+
}
511
}

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
## [next]
22

33
- fix: hide Tab's close button when `onClosed` is null
4+
- feat: Add `TextBox.cursorOpacityAnimates` (defaults to `FluentThemeData.cursorOpacityAnimates`, which defaults to `false`); default setting improves CPU/GPU efficiency while TextBox has focus ([#1164](https://github.com/bdlukaa/fluent_ui/issues/1164))
45

56
## 4.10.0
67

lib/src/controls/form/text_box.dart

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ class TextBox extends StatefulWidget {
176176
this.cursorHeight,
177177
this.cursorRadius = const Radius.circular(2.0),
178178
this.cursorColor,
179+
this.cursorOpacityAnimates,
179180
this.selectionHeightStyle = ui.BoxHeightStyle.tight,
180181
this.selectionWidthStyle = ui.BoxWidthStyle.tight,
181182
this.keyboardAppearance,
@@ -447,6 +448,12 @@ class TextBox extends StatefulWidget {
447448
/// null, it uses the [FluentThemeData.inactiveColor] of the ambient theme.
448449
final Color? cursorColor;
449450

451+
/// When the widget has focus and the cursor should be blinking, indicates
452+
/// whether or not to use high fidelity animation for the cursor's opacity,
453+
/// or to just use simple blinking when the widget has focus.
454+
/// Defaults to [FluentThemeData.cursorOpacityAnimates].
455+
final bool? cursorOpacityAnimates;
456+
450457
/// Controls how tall the selection highlight boxes are computed to be.
451458
///
452459
/// See [ui.BoxHeightStyle] for details on available styles.
@@ -615,6 +622,8 @@ class TextBox extends StatefulWidget {
615622
..add(DiagnosticsProperty<Radius>('cursorRadius', cursorRadius,
616623
defaultValue: null))
617624
..add(ColorProperty('cursorColor', cursorColor, defaultValue: null))
625+
..add(DiagnosticsProperty<bool>(
626+
'cursorOpacityAnimates', cursorOpacityAnimates))
618627
..add(FlagProperty('selectionEnabled',
619628
value: selectionEnabled,
620629
defaultValue: true,
@@ -1008,6 +1017,8 @@ class _TextBoxState extends State<TextBox>
10081017
final cursorColor = widget.cursorColor ??
10091018
DefaultSelectionStyle.of(context).cursorColor ??
10101019
themeData.inactiveColor;
1020+
final cursorOpacityAnimates =
1021+
widget.cursorOpacityAnimates ?? themeData.cursorOpacityAnimates;
10111022

10121023
final selectionColor = DefaultSelectionStyle.of(context).selectionColor ??
10131024
themeData.accentColor.normal;
@@ -1071,7 +1082,7 @@ class _TextBoxState extends State<TextBox>
10711082
cursorHeight: widget.cursorHeight,
10721083
cursorRadius: widget.cursorRadius,
10731084
cursorColor: cursorColor,
1074-
cursorOpacityAnimates: true,
1085+
cursorOpacityAnimates: cursorOpacityAnimates,
10751086
cursorOffset: cursorOffset,
10761087
paintCursorAboveText: true,
10771088
autocorrectionTextRectColor: selectionColor,

lib/src/styles/theme.dart

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,11 @@ class FluentThemeData with Diagnosticable {
199199
final Duration slowAnimationDuration;
200200
final Curve animationCurve;
201201

202+
/// See [EditableText.cursorOpacityAnimates]. Whether or not to have high
203+
/// fidelity animation for the opacity of the blinking text cursor, at the
204+
/// expense of higher CPU/GPU usage. Defaults to false (recommended).
205+
final bool cursorOpacityAnimates;
206+
202207
final Brightness brightness;
203208
final VisualDensity visualDensity;
204209

@@ -243,6 +248,7 @@ class FluentThemeData with Diagnosticable {
243248
Duration? mediumAnimationDuration,
244249
Duration? slowAnimationDuration,
245250
Curve? animationCurve,
251+
bool? cursorOpacityAnimates,
246252
BottomNavigationThemeData? bottomNavigationTheme,
247253
ButtonThemeData? buttonTheme,
248254
CheckboxThemeData? checkboxTheme,
@@ -274,6 +280,7 @@ class FluentThemeData with Diagnosticable {
274280
? const ResourceDictionary.light()
275281
: const ResourceDictionary.dark();
276282
animationCurve ??= standardCurve;
283+
cursorOpacityAnimates ??= false;
277284
accentColor ??= Colors.blue;
278285
activeColor ??= Colors.white;
279286
inactiveColor ??= isLight ? Colors.black : Colors.white;
@@ -325,6 +332,7 @@ class FluentThemeData with Diagnosticable {
325332
mediumAnimationDuration: mediumAnimationDuration,
326333
slowAnimationDuration: slowAnimationDuration,
327334
animationCurve: animationCurve,
335+
cursorOpacityAnimates: cursorOpacityAnimates,
328336
accentColor: accentColor,
329337
activeColor: activeColor,
330338
inactiveColor: inactiveColor,
@@ -369,6 +377,7 @@ class FluentThemeData with Diagnosticable {
369377
required this.mediumAnimationDuration,
370378
required this.slowAnimationDuration,
371379
required this.animationCurve,
380+
required this.cursorOpacityAnimates,
372381
required this.brightness,
373382
required this.visualDensity,
374383
required this.scaffoldBackgroundColor,
@@ -432,6 +441,8 @@ class FluentThemeData with Diagnosticable {
432441
slowAnimationDuration:
433442
lerpDuration(a.slowAnimationDuration, b.slowAnimationDuration, t),
434443
animationCurve: t < 0.5 ? a.animationCurve : b.animationCurve,
444+
cursorOpacityAnimates:
445+
t < 0.5 ? a.cursorOpacityAnimates : b.cursorOpacityAnimates,
435446
buttonTheme: ButtonThemeData.lerp(a.buttonTheme, b.buttonTheme, t),
436447
checkboxTheme:
437448
CheckboxThemeData.lerp(a.checkboxTheme, b.checkboxTheme, t),
@@ -499,6 +510,7 @@ class FluentThemeData with Diagnosticable {
499510
Duration? mediumAnimationDuration,
500511
Duration? slowAnimationDuration,
501512
Curve? animationCurve,
513+
bool? cursorOpacityAnimates,
502514
ButtonThemeData? buttonTheme,
503515
BottomNavigationThemeData? bottomNavigationTheme,
504516
CheckboxThemeData? checkboxTheme,
@@ -546,6 +558,8 @@ class FluentThemeData with Diagnosticable {
546558
slowAnimationDuration:
547559
slowAnimationDuration ?? this.slowAnimationDuration,
548560
animationCurve: animationCurve ?? this.animationCurve,
561+
cursorOpacityAnimates:
562+
cursorOpacityAnimates ?? this.cursorOpacityAnimates,
549563
buttonTheme: this.buttonTheme.merge(buttonTheme),
550564
bottomNavigationTheme:
551565
this.bottomNavigationTheme.merge(bottomNavigationTheme),
@@ -590,6 +604,8 @@ class FluentThemeData with Diagnosticable {
590604
'fastAnimationDuration', fastAnimationDuration))
591605
..add(DiagnosticsProperty<Duration>(
592606
'fasterAnimationDuration', fasterAnimationDuration))
593-
..add(DiagnosticsProperty<Curve>('animationCurve', animationCurve));
607+
..add(DiagnosticsProperty<Curve>('animationCurve', animationCurve))
608+
..add(DiagnosticsProperty<bool>(
609+
'cursorOpacityAnimates', cursorOpacityAnimates));
594610
}
595611
}

0 commit comments

Comments
 (0)