Skip to content

Load theme for raw code - #1675

Merged
laurmaedje merged 7 commits into
typst:mainfrom
Myriad-Dreamin:code-theme
Jul 17, 2023
Merged

laurmaedje merged 7 commits into
typst:mainfrom
Myriad-Dreamin:code-theme

Conversation

@Myriad-Dreamin

@Myriad-Dreamin Myriad-Dreamin commented Jul 7, 2023 •

Copy link
Copy Markdown
Contributor

This PR adds an option theme to the raw element.

It introduces several changes which can be argued:

Set default value txt to lang option

Set default value txt to lang option, otherwise Syntect does not fill any color for text in raw code, which is not good to dark themes.

image

A txt highlighter will simply attach a text.plain scope to the whole raw text:

https://github.com/trishume/syntect/blob/f0ece85c5a2dc4436442558babd9fc159dca372b/src/parsing/syntax_set.rs#L457-L467

Always use foreground color from the theme setting

Typst should not override foreground color (to black color) for dark themes. It is also meaningless to detect whether a theme is in dark style. So I removed the raw element inheriting foreground color from ancestors.

Under draft: It is under drafting since it introduced another non-trivial option, which needs to discuss. It adds a styling option which is of type...

@Myriad-Dreamin Myriad-Dreamin changed the title Load raw syntax theme Load theme for raw code Jul 7, 2023
Comment thread NOTICE Resolved
Comment thread crates/typst-library/src/text/raw.rs Resolved
Comment thread crates/typst-library/src/text/raw.rs Resolved
Comment thread tests/typ/text/raw-theme.typ Resolved
@laurmaedje

Copy link
Copy Markdown
Member

About not inheriting the foreground color: I see how it makes sense, but is there any way to configure it now for block-level raw text?

Regarding the styling function: It does not feel really Typst-y to me. This seems more like the thing a show rule should do. Which would in theory be possible with a synthesized field, but that would need careful design. I would propose leaving it out of this PR to get this merged quickly and then revisiting it.

@Myriad-Dreamin

Copy link
Copy Markdown
Contributor Author

@laurmaedje

About not inheriting the foreground color: I see how it makes sense, but is there any way to configure it now for block-level raw text?

A simple override rule could be implemented as follows (just what it was):

fn styled(piece: &str, foreground: Paint, style: synt::Style) -> Content {
let mut body = TextElem::packed(piece);
let paint = to_typst(style.foreground).into();
if paint != foreground {
body = body.styled(TextElem::set_fill(paint));
}

The problem is that we cannot enable it by default, since it corrupts many themes. We could consider configure it in the styling. However, I'm unsure about the best design for modifying theme settings at the moment.


Which would in theory be possible with a synthesized field, but that would need careful design.

If we don't have a styling option, we cannot customize a box with the same background color as that in the theme setting. But as you said, we can leave it out of this PR for future works. Users can copy the background color from tmTheme files manually.

I wasn't previously familiar with the concept of a "synthesized field". If you could provide guidance on which files to refer to, I can check it and rework on styling.

@Myriad-Dreamin
Myriad-Dreamin force-pushed the code-theme branch 2 times, most recently from 4027724 to 50a42f6 Compare July 7, 2023 09:43
@laurmaedje

Copy link
Copy Markdown
Member

Users can copy the background color from tmTheme files manually.

I think that's good enough for now. They'll have to write a show rule for the rectangle anyway, so copying the color won't be that big of a deal. Of course, reading it is cleaner and more composable, but it's a minor issue for now.

Synthesized fields are basically field that are constructed from other fields in impl Synthesize for .... You can see how it works for FigureElem. That way, we could have a dictionary with relevant theme properties synthesized. But I'm not sure how we would name the synthesized field. I'm also not a big fan of the synthesized fields anyway, so I'm not sure whether I want to add even more of them (which would make it more difficult to get rid of them in the future).

@laurmaedje

Copy link
Copy Markdown
Member

So, in principle, this looks good to merge now. The only thing I'm still unsure about is the foreground thing. Even if we use no theme at all, we couldn't affect the text color anymore because it defaults to the one from THEME, if I understand correctly.

@Myriad-Dreamin

Myriad-Dreamin commented Jul 7, 2023 •

Copy link
Copy Markdown
Contributor Author

we couldn't affect the text color

I understand it now. This could be weird if we cannot affect text in raw code at all, such as #text(fill: red, [`red code`]). Both of scenarios should work: we should not simply view text in raw blocks as TextElem since #1675; At the same time, we must provide a more proper way to affect them.

So we should find a way to make theme settings available in context of show rules, so that users can write #show raw(it) => show_red_code(it). In other word, it might be a better decision to postpone merge of this PR at least before v0.7.0.


The efforts related to show raw rules would come in a new PR and block this PR. For synthesized fields, we may start from supporting it.foreground and it.background for minimum viable production.

@laurmaedje

laurmaedje commented Jul 7, 2023 •

Copy link
Copy Markdown
Member

I don't understand how having access to theme properties would help if the default is to respect the theme's foreground color. The other way around it would make sense: If we ignore the foreground color from the theme and let the user set it manually, then they could write a show rule that sets the text color to the one from the theme. But if do it like that, this PR isn't blocked, we would just need to revert the foreground change.

@Myriad-Dreamin

Copy link
Copy Markdown
Contributor Author

@laurmaedje I have rolled back the changes about foreground color in the commit 42d7b97

The rest commits are kept.

@reknih

reknih commented Jul 12, 2023

Copy link
Copy Markdown
Member

Paint bucket icon

I have prepared an icon for tmTheme files in the web app

@Myriad-Dreamin

Copy link
Copy Markdown
Contributor Author

Paint bucket icon

I have prepared an icon for tmTheme files in the web app

lovely icon~

@laurmaedje
laurmaedje merged commit 9b72ee4 into typst:main Jul 17, 2023
@laurmaedje

Copy link
Copy Markdown
Member

Thank you! Also for your patience with the foreground/background discussion. I think of this as the minimal, initial approach and am optimistic that we'll find a good solution for foreground/background handling down the road.

@Myriad-Dreamin
Myriad-Dreamin deleted the code-theme branch July 20, 2023 11:16
Dikluwe pushed a commit to Dikluwe/typst-crystalline that referenced this pull request Sep 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants