Skip to content

content: Handle bold-text setting in code blocks and error-unimplementeds #691

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

Merged
merged 4 commits into from
May 20, 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
18 changes: 3 additions & 15 deletions lib/widgets/content.dart
Original file line number Diff line number Diff line change
Expand Up @@ -765,7 +765,7 @@ class _InlineContentBuilder {
}

InlineSpan _buildStrong(StrongNode node) => _buildNodes(node.nodes,
style: weightVariableTextStyle(_context, wght: 600, wghtIfPlatformRequestsBold: 900));
style: weightVariableTextStyle(_context!, wght: 600));

InlineSpan _buildDeleted(DeletedNode node) => _buildNodes(node.nodes,
style: const TextStyle(decoration: TextDecoration.lineThrough));
Expand Down Expand Up @@ -847,23 +847,12 @@ const _kInlineCodeFontSizeFactor = 0.825;
/// Inline code should use this and also apply [_kInlineCodeFontSizeFactor]
/// to the font size of the surrounding text
/// (which might be a Paragraph, a Heading, etc.).
// Even though [kMonospaceTextStyle] is a variable-weight font,
// it's acceptable to skip [weightVariableTextStyle] here,
// assuming the text gets the effect of [weightVariableTextStyle]
// through inheritance, e.g., from a [DefaultTextStyle].
final _kInlineCodeStyle = kMonospaceTextStyle
.merge(TextStyle(
backgroundColor: const HSLColor.fromAHSL(0.04, 0, 0, 0).toColor()));

final _kCodeBlockStyle = kMonospaceTextStyle
.merge(const TextStyle(fontSize: 0.825 * kBaseFontSize))
.merge(
// TODO(a11y) pass a BuildContext, to handle platform request for bold text.
// To get one, the result of this whole computation (to the TextStyle
// we get at the end) could live on one [InheritedWidget], at the
// MessageList or higher, so the computation doesn't get repeated
// frequently. Then consumers can just look it up on the InheritedWidget.
weightVariableTextStyle(null));
.merge(const TextStyle(fontSize: 0.825 * kBaseFontSize));

// const _kInlineCodeLeftBracket = '⸤';
// const _kInlineCodeRightBracket = '⸣';
Expand Down Expand Up @@ -1286,5 +1275,4 @@ const errorStyle = TextStyle(
fontSize: kBaseFontSize, fontWeight: FontWeight.bold, color: Colors.red);

final errorCodeStyle = kMonospaceTextStyle
.merge(const TextStyle(fontSize: kBaseFontSize, color: Colors.red))
.merge(weightVariableTextStyle(null)); // TODO(a11y) pass a BuildContext
.merge(const TextStyle(fontSize: kBaseFontSize, color: Colors.red));
3 changes: 1 addition & 2 deletions lib/widgets/message_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -926,8 +926,7 @@ class MessageWithPossibleSender extends StatelessWidget {
fontFamily: 'Source Sans 3',
fontSize: 18,
height: (22 / 18),
).merge(weightVariableTextStyle(context, wght: 600,
wghtIfPlatformRequestsBold: 900)),
).merge(weightVariableTextStyle(context, wght: 600)),
overflow: TextOverflow.ellipsis)),
if (sender?.isBot ?? false) ...[
const SizedBox(width: 5),
Expand Down
8 changes: 5 additions & 3 deletions lib/widgets/text.dart
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ List<String> get defaultFontFamilyFallback => [
///
/// Callers should also call [weightVariableTextStyle] and merge that in too,
/// because for this font, we use "variable font" assets with a "wght" axis.
/// That is, unless it would be redundant with another step that applies
/// [weightVariableTextStyle]; for example, a [DefaultTextStyle] that specifies
/// a different variable-weight font that we're overriding with this one.
///
/// Example:
///
Expand Down Expand Up @@ -200,13 +203,12 @@ const kButtonTextLetterSpacingProportion = 0.01;
/// ```
///
/// See also [FontVariation] for more background on variable fonts.
// TODO(a11y) make `context` required when callers can adapt?
TextStyle weightVariableTextStyle(BuildContext? context, {
TextStyle weightVariableTextStyle(BuildContext context, {
double? wght,
double? wghtIfPlatformRequestsBold,
}) {
double value = wght ?? FontWeight.normal.value.toDouble();
if (context != null && MediaQuery.boldTextOf(context)) {
if (MediaQuery.boldTextOf(context)) {
// The framework has a condition on [MediaQueryData.boldText]
// in the [Text] widget, but that only affects `fontWeight`.
// [Text] doesn't know where to land on the chosen font's "wght" axis if any,
Expand Down
9 changes: 0 additions & 9 deletions test/widgets/text_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,6 @@ void main() {
});
}

testWeights('no context passed; default wght values',
styleBuilder: (context) => weightVariableTextStyle(null),
expectedFontVariations: const [FontVariation('wght', 400)],
expectedFontWeight: FontWeight.normal);
testWeights('no context passed; specific wght',
styleBuilder: (context) => weightVariableTextStyle(null, wght: 225, wghtIfPlatformRequestsBold: 425),
expectedFontVariations: const [FontVariation('wght', 225)],
expectedFontWeight: FontWeight.w200);

testWeights('default values; platform does not request bold',
styleBuilder: (context) => weightVariableTextStyle(context),
platformRequestsBold: false,
Expand Down
Loading