Skip to content

Commit b7422e2

Browse files
committed
content: Show code, mentions, and times properly even in headings etc.
Fixes: zulip#538
1 parent a9090bc commit b7422e2

File tree

1 file changed

+42
-12
lines changed

1 file changed

+42
-12
lines changed

lib/widgets/content.dart

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -628,18 +628,22 @@ class _InlineContentBuilder {
628628
return _buildInlineCode(node);
629629
} else if (node is UserMentionNode) {
630630
return WidgetSpan(alignment: PlaceholderAlignment.middle,
631-
child: UserMention(node: node));
631+
child: UserMention(node: node, surroundingTextStyle: widget.style));
632632
} else if (node is UnicodeEmojiNode) {
633633
return TextSpan(text: node.emojiUnicode, recognizer: _recognizer);
634634
} else if (node is ImageEmojiNode) {
635635
return WidgetSpan(alignment: PlaceholderAlignment.middle,
636636
child: MessageImageEmoji(node: node));
637637
} else if (node is MathInlineNode) {
638-
return TextSpan(style: _kInlineMathStyle,
638+
assert(widget.style.fontSize != null);
639+
return TextSpan(
640+
style: widget.style
641+
.merge(_kInlineMathStyle)
642+
.apply(fontSizeFactor: _kInlineCodeFontSizeFactor),
639643
children: [TextSpan(text: node.texSource)]);
640644
} else if (node is GlobalTimeNode) {
641645
return WidgetSpan(alignment: PlaceholderAlignment.middle,
642-
child: GlobalTime(node: node));
646+
child: GlobalTime(node: node, surroundingTextStyle: widget.style));
643647
} else if (node is UnimplementedInlineContentNode) {
644648
return _errorUnimplemented(node);
645649
} else {
@@ -688,8 +692,14 @@ class _InlineContentBuilder {
688692

689693
// TODO `code`: find equivalent of web's `unicode-bidi: embed; direction: ltr`
690694

691-
// Use a light gray background, instead of a border.
692-
return _buildNodes(style: _kInlineCodeStyle, node.nodes);
695+
assert(widget.style.fontSize != null);
696+
return _buildNodes(
697+
// Use a light gray background, instead of a border.
698+
style: widget.style
699+
.merge(_kInlineCodeStyle)
700+
.apply(fontSizeFactor: _kInlineCodeFontSizeFactor),
701+
node.nodes,
702+
);
693703

694704
// Another fun solution -- we can in fact have a border! Like so:
695705
// TextStyle(
@@ -709,17 +719,27 @@ class _InlineContentBuilder {
709719
}
710720
}
711721

722+
/// The [TextStyle] for inline math, excluding font-size adjustment.
723+
///
724+
/// Inline math should use this and also apply [_kInlineCodeFontSizeFactor]
725+
/// to the font size of the surrounding text
726+
/// (which might be a Paragraph, a Heading, etc.).
712727
final _kInlineMathStyle = _kInlineCodeStyle.merge(TextStyle(
713728
backgroundColor: const HSLColor.fromAHSL(1, 240, 0.4, 0.93).toColor()));
714729

730+
const _kInlineCodeFontSizeFactor = 0.825;
731+
732+
/// The [TextStyle] for inline code, excluding font-size adjustment.
733+
///
734+
/// Inline code should use this and also apply [_kInlineCodeFontSizeFactor]
735+
/// to the font size of the surrounding text
736+
/// (which might be a Paragraph, a Heading, etc.).
715737
// Even though [kMonospaceTextStyle] is a variable-weight font,
716738
// it's acceptable to skip [weightVariableTextStyle] here,
717739
// assuming the text gets the effect of [weightVariableTextStyle]
718740
// through inheritance, e.g., from a [DefaultTextStyle].
719741
final _kInlineCodeStyle = kMonospaceTextStyle
720-
.merge(const TextStyle(
721-
backgroundColor: Color(0xffeeeeee),
722-
fontSize: 0.825 * kBaseFontSize));
742+
.merge(const TextStyle(backgroundColor: Color(0xffeeeeee)));
723743

724744
final _kCodeBlockStyle = kMonospaceTextStyle
725745
.merge(const TextStyle(
@@ -750,9 +770,14 @@ final _kCodeBlockStyle = kMonospaceTextStyle
750770
// const _kInlineCodeRightBracket = '⟩';
751771

752772
class UserMention extends StatelessWidget {
753-
const UserMention({super.key, required this.node});
773+
const UserMention({
774+
super.key,
775+
required this.node,
776+
required this.surroundingTextStyle,
777+
});
754778

755779
final UserMentionNode node;
780+
final TextStyle surroundingTextStyle;
756781

757782
@override
758783
Widget build(BuildContext context) {
@@ -765,7 +790,7 @@ class UserMention extends StatelessWidget {
765790
// One hopes an @-mention can't contain an embedded link.
766791
// (The parser on creating a UserMentionNode has a TODO to check that.)
767792
linkRecognizers: null,
768-
style: Paragraph.textStyle,
793+
style: surroundingTextStyle,
769794
nodes: node.nodes));
770795
}
771796

@@ -832,9 +857,14 @@ class MessageImageEmoji extends StatelessWidget {
832857
}
833858

834859
class GlobalTime extends StatelessWidget {
835-
const GlobalTime({super.key, required this.node});
860+
const GlobalTime({
861+
super.key,
862+
required this.node,
863+
required this.surroundingTextStyle,
864+
});
836865

837866
final GlobalTimeNode node;
867+
final TextStyle surroundingTextStyle;
838868

839869
static final _backgroundColor = const HSLColor.fromAHSL(1, 0, 0, 0.93).toColor();
840870
static final _borderColor = const HSLColor.fromAHSL(1, 0, 0, 0.8).toColor();
@@ -861,7 +891,7 @@ class GlobalTime extends StatelessWidget {
861891
// Ad-hoc spacing adjustment per feedback:
862892
// https://chat.zulip.org/#narrow/stream/101-design/topic/clock.20icons/near/1729345
863893
const SizedBox(width: 1),
864-
Text(text, style: Paragraph.textStyle),
894+
Text(text, style: surroundingTextStyle),
865895
]))));
866896
}
867897
}

0 commit comments

Comments
 (0)