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

content: Put code-block styles in ContentTheme; use weightVariableTextStyle for bold spans #727

Merged
merged 6 commits into from
Jun 17, 2024

Conversation

chrisbobbe
Copy link
Collaborator

Before After
34AB2CF1-E279-4702-A855-80722BE9056A 17039B63-B706-4F85-9253-CC5DF863DFB3

Related: #95

@chrisbobbe chrisbobbe added a-content Parsing and rendering Zulip HTML content, notably message contents integration review Added by maintainers when PR may be ready for integration labels Jun 6, 2024
@chrisbobbe chrisbobbe requested a review from gnprice June 6, 2024 20:55
Copy link
Member

@gnprice gnprice left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! This looks good — various small comments below.

Comment on lines 573 to 581
InlineSpan _buildNodes(List<CodeBlockSpanNode> nodes) {
InlineSpan _buildNodes(List<CodeBlockSpanNode> nodes, {required CodeBlockTextStyles styles}) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, this helper and the _buildNode below it are a bit silly — they each have just one call site, so they're not doing much. That's largely harmless as is, but makes it a pain to pass down additional data through them, as here.

So a good prep commit would be to inline both these helpers into build. Then this can just be a local styles.

Comment on lines +275 to +279
required TextStyle vi,
required TextStyle il,
}) :
_hll = hll,
_c = c,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oof, lotta boilerplate.

But I think this is the structure we need in order to support dark mode (and particularly to support nicely lerping in and out of it), so that's just how it'll be. Definitely glad we have this off in its own file.

Comment on lines -51 to +62
final _kCodeBlockStyleGh = TextStyle(color: const HSLColor.fromAHSL(1, 240, 1, 0.25).toColor(), fontWeight: FontWeight.bold);
// .gh { color: hsl(240deg 100% 25%); font-weight: bold; }
gh: TextStyle(color: const HSLColor.fromAHSL(1, 240, 1, 0.25).toColor()).merge(bold),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm I see. The FontWeight.bold was getting ignored because of the wght variation, which leaked through from the ambient TextStyle (which in turn came from the ContentTheme). Is that right?

It'd be good to add a few words to the commit message about the visible effect of this. I wasn't sure what to expect before I opened up the screenshots at full size and compared them.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In particular:

And while we're add it, add some tests that spot-check the weight of
a non-bold span and a bold span.

Those tests aren't just a by-the-way — they're regression tests for the bug this fixes 🙂

Copy link
Collaborator Author

@chrisbobbe chrisbobbe Jun 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm I see. The FontWeight.bold was getting ignored because of the wght variation, which leaked through from the ambient TextStyle (which in turn came from the ContentTheme). Is that right?

No, I don't think that's quite right. The "Before" screenshot already shows the text being bolder than surrounding text, just not in the way we want it to be. I think this was caused by the FontWeight.bold here and:

For glyphs that render in a variable-weight font, we want to specify a wght value, so that they appear as the font designer intended. Just setting TextStyle.fontWeight by itself doesn't do that—not until flutter/flutter#148026 anyway—and our way of setting wght is to use weightVariableTextStyle. These glyphs did have a wght value before this change, but it was wrong. It should be 700, but it was 400, from the ambient TextStyle from ContentTheme.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I guess "ignored" isn't right. But as you say, it (the FontWeight.bold) wasn't having the desired effect.

/// Used to render syntax highlighting.
// Span styles adapted from:
// https://github.com/zulip/zulip/blob/213387249e7ba7772084411b22d8cef64b135dd0/web/styles/pygments.css
TextStyle? span(CodeBlockSpanType type) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about:

Suggested change
TextStyle? span(CodeBlockSpanType type) {
TextStyle? forSpan(CodeBlockSpanType type) {

The name just "span" reads to me like it's meant to return a span.

Comment on lines 340 to 344

final TextStyle plain;

final TextStyle _hll;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

plain here has a different role from all the other styles, in that it's meant to be a baseline that each of them gets applied on top of.

So that asymmetry would be good to make explicit, perhaps in dartdoc on plain. In particular that helps explain why in the last commit, an explicit weight gets added to plain but there's no need for it on the rest.

@chrisbobbe
Copy link
Collaborator Author

Thanks for the review! Revision pushed.

@gnprice
Copy link
Member

gnprice commented Jun 15, 2024

Thanks for the revision! This all looks good, but it seems to have a conflict with #728, which I just merged. So please go ahead and rebase past that, and then merge.

With the code-block span styles all bundled together in a new
CodeBlockTextStyles class, it'll be convenient to make separate
`light` and `dark` constructors toward zulip#95 dark theme.

Next, we'll pass a BuildContext to the CodeBlockTextStyles
constructor, in order to use weightVariableTextStyle for the spans
that are supposed to be bold. (The code-block font, Source Code Pro,
is variable-weight.) This would have been possible before the
current refactor, of course, but this way the spans don't have to
repeat the weightVariableTextStyle computations on-demand. They can
instead just look up the result from this central source.

Related: zulip#95
Bold spans still don't set `wght` to 700; we'll fix that and test
for it next.
Now, as the added test confirms, bold spans are getting a `wght`
value of 700, instead of 400 from the ambient style.

The FontWeight.bold seems to have already been causing the spans to
render bolder than surrounding text, but in a way that doesn't match
what we get for Source Code Pro with `wght` 700. I think this is
because of
  flutter/flutter#136779
.
I *think* it's not possible for a code block to appear in a bold
context. In particular I haven't found a way to put one inside a
spoiler header.

Anyway, just in case it does end up being possible somehow, might as
well clobber the bold with normal weight, to make sure the code
block's bold spans remain distinct from non-bold spans. It's
convenient (and cheap) to do that here in the CodeBlockTextStyles
constructor.
@chrisbobbe chrisbobbe merged commit 3196263 into zulip:main Jun 17, 2024
1 check passed
@chrisbobbe
Copy link
Collaborator Author

Done; thanks for the review!

@chrisbobbe chrisbobbe deleted the pr-code-block-styles branch June 17, 2024 17:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a-content Parsing and rendering Zulip HTML content, notably message contents integration review Added by maintainers when PR may be ready for integration
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants