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

Add maxWidth to tooltip theme for easy text-wrapping #1095

Merged
merged 1 commit into from
Jul 30, 2024
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* fix: Resolved issue where `PaneItem` within `PaneItemExpander` remained accessible in `NavigationPane` compact mode ([#1081](https://github.com/bdlukaa/fluent_ui/issues/1081))
* fix: Correct number of days on `DatePicker` popup ([#1049](https://github.com/bdlukaa/fluent_ui/issues/1049))
* feat: Create `PaneItemWidgetAdapter` ([#1087](https://github.com/bdlukaa/fluent_ui/issues/1087))
* feat: Add `maxWidth` to `TooltipThemeData` for optional wrapping of long tooltips ([#1094](https://github.com/bdlukaa/fluent_ui/issues/1094))

## 4.9.0

Expand Down
40 changes: 40 additions & 0 deletions example/lib/screens/popups/tooltip.dart
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,46 @@ class TooltipPage extends StatelessWidget with PageMixin {
),
),
),
subtitle(
content: const Text(
'Extra long tooltip content that wraps',
),
),
CardHighlight(
codeSnippet: '''Tooltip(
message:
List.generate(25, (_) => 'This is a really long tooltip! ')
.join(" "),
useMousePosition: false,
style: const TooltipThemeData(
maxWidth: 500,
preferBelow: true,
waitDuration: Duration(),
),
child: IconButton(
icon: const Icon(FluentIcons.text_overflow, size: 24.0),
onPressed: () {},
),
),''',
child: Align(
alignment: AlignmentDirectional.centerStart,
child: Tooltip(
message:
List.generate(25, (_) => 'This is a really long tooltip! ')
.join(" "),
useMousePosition: false,
style: const TooltipThemeData(
maxWidth: 500,
preferBelow: true,
waitDuration: Duration(),
),
child: IconButton(
icon: const Icon(FluentIcons.text_overflow, size: 24.0),
onPressed: () {},
),
),
),
),
],
);
}
Expand Down
15 changes: 14 additions & 1 deletion lib/src/controls/surfaces/tooltip.dart
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ class _TooltipState extends State<Tooltip> with SingleTickerProviderStateMixin {
Offset? mousePosition;
late TooltipTriggerMode triggerMode;
late bool enableFeedback;
late double? maxWidth;
late bool _isConcealed;
late bool _forceRemoval;
late bool _visible;
Expand Down Expand Up @@ -406,6 +407,7 @@ class _TooltipState extends State<Tooltip> with SingleTickerProviderStateMixin {
verticalOffset: verticalOffset,
preferBelow: preferBelow,
displayHorizontally: widget.displayHorizontally,
maxWidth: maxWidth,
),
);
_entry = OverlayEntry(builder: (_) => overlay);
Expand Down Expand Up @@ -518,6 +520,7 @@ class _TooltipState extends State<Tooltip> with SingleTickerProviderStateMixin {
hoverShowDuration = _defaultHoverShowDuration;
triggerMode = widget.triggerMode ?? _defaultTriggerMode;
enableFeedback = widget.enableFeedback ?? _defaultEnableFeedback;
maxWidth = tooltipTheme.maxWidth;

Widget result = Semantics(
label: excludeFromSemantics ? null : _tooltipMessage,
Expand Down Expand Up @@ -675,6 +678,9 @@ class TooltipThemeData with Diagnosticable {
/// If null, [Typography.caption] is used
final TextStyle? textStyle;

/// If non-null, the maximum width of the tooltip text before it wraps.
final double? maxWidth;

const TooltipThemeData({
this.height,
this.verticalOffset,
Expand All @@ -685,6 +691,7 @@ class TooltipThemeData with Diagnosticable {
this.showDuration,
this.waitDuration,
this.textStyle,
this.maxWidth,
});

factory TooltipThemeData.standard(FluentThemeData theme) {
Expand Down Expand Up @@ -749,6 +756,7 @@ class TooltipThemeData with Diagnosticable {
verticalOffset: lerpDouble(a?.verticalOffset, b?.verticalOffset, t),
waitDuration: lerpDuration(a?.waitDuration ?? Duration.zero,
b?.waitDuration ?? Duration.zero, t),
maxWidth: lerpDouble(a?.maxWidth, b?.maxWidth, t),
);
}

Expand All @@ -764,6 +772,7 @@ class TooltipThemeData with Diagnosticable {
textStyle: style.textStyle ?? textStyle,
verticalOffset: style.verticalOffset ?? verticalOffset,
waitDuration: style.waitDuration ?? waitDuration,
maxWidth: style.maxWidth ?? maxWidth,
);
}

Expand All @@ -787,7 +796,8 @@ class TooltipThemeData with Diagnosticable {
..add(DiagnosticsProperty<Decoration>('decoration', decoration))
..add(DiagnosticsProperty<Duration>('waitDuration', waitDuration))
..add(DiagnosticsProperty<Duration>('showDuration', showDuration))
..add(DiagnosticsProperty<TextStyle>('textStyle', textStyle));
..add(DiagnosticsProperty<TextStyle>('textStyle', textStyle))
..add(DoubleProperty('maxWidth', maxWidth));
}
}

Expand Down Expand Up @@ -866,6 +876,7 @@ class _TooltipOverlay extends StatelessWidget {
required this.verticalOffset,
required this.preferBelow,
this.displayHorizontally = false,
this.maxWidth,
});

final InlineSpan richMessage;
Expand All @@ -878,6 +889,7 @@ class _TooltipOverlay extends StatelessWidget {
final double verticalOffset;
final bool preferBelow;
final bool displayHorizontally;
final double? maxWidth;

@override
Widget build(BuildContext context) {
Expand All @@ -893,6 +905,7 @@ class _TooltipOverlay extends StatelessWidget {
decoration: decoration,
padding: padding,
margin: margin,
constraints: BoxConstraints(maxWidth: maxWidth ?? double.infinity),
child: Center(
widthFactor: 1.0,
heightFactor: 1.0,
Expand Down
Loading