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

take into account the system theme #61236

Merged
merged 2 commits into from Aug 21, 2019
Merged

Conversation

GuillaumeGomez
Copy link
Member

Fixes #61079.

The CSS can now take into account the system theme. I used it to generate some content on the document and from there, if no theme has already been selected, it'll look at the system level theme.

r? @QuietMisdreavus
cc @fenhl

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label May 27, 2019
@fenhl
Copy link
Contributor

fenhl commented May 27, 2019

How does this interact with having set the theme manually? I switch my system theme between light and dark mode based on time of day and would like rustdoc to adhere to that.

@fenhl
Copy link
Contributor

fenhl commented May 27, 2019

Looking at the changes, it seems that I'd just have to clear local storage. I'm okay with that.

@GuillaumeGomez
Copy link
Member Author

You'd still have to reload the page, but that's a browser limitation (and I don't want to check multiple times after the page has been loaded if the theme hasn't change...).

@@ -54,6 +54,21 @@
box-sizing: border-box;
}

/* This part handles the "default" theme being used depending on the system one. */
html {
content: "";
Copy link
Member

Choose a reason for hiding this comment

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

Maybe use a custom css property instead: https://developer.mozilla.org/en-US/docs/Web/CSS/--*

Copy link
Member Author

Choose a reason for hiding this comment

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

You mean CSS variable? Not supported on IE so we can't use it.

Copy link
Member

Choose a reason for hiding this comment

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

IE doesn't support prefers-color-scheme anyway. IE would not set the custom property to any value, but the javascript has a fallback for that already.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'd prefer avoid potential failures if possible.

Copy link
Member

Choose a reason for hiding this comment

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

Just to confirm, since string values for content is only ever allowed for ::before and ::after, this won't cause a situation where the entire page is replaced with whatever is set here? MDN seems to imply this, but i want to double-check that that's what's actually going on.

Copy link
Member Author

Choose a reason for hiding this comment

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

The content is a hack that isn't displayed on this tag. You can only "see" it with js (I think it's because it's not a ::before or ::after element).

Copy link
Member

Choose a reason for hiding this comment

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

What are the chances of this behavior changing in the future? I don't want to be trapped in a situation where the CSS spec starts allowing element replacement with strings, and a whole bunch of docs are rendered unusable because we started depending on this workaround.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not sure to follow you. There is nothing in the specs or anywhere changing this behavior for the moment and if there is any change, we'll have quite a lot of time before needing to update it. Also, what are you referring to when talking about "and a whole bunch of docs are rendered unusable because we started depending on this workaround."?

Copy link
Member

Choose a reason for hiding this comment

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

If this behavior changes, i.e. some browser decides that setting content to a string on an element will cause the element to be replaced by that string, then any documentation already built by a rustdoc using this CSS will only display the string set here.

As long as the specs are on our side, and setting content to a string on an element does nothing, then i think i can be okay with this.

Copy link
Member

@QuietMisdreavus QuietMisdreavus left a comment

Choose a reason for hiding this comment

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

This PR seems fine, just one naming nit.

Would it be worth checking for whether it's set and adding a "system" entry to the drop-down that clears the local storage and loads the prefers-color-scheme value? I'm not sure how many people would be willing to dive into browser dev tools to clear the theme value.

@@ -88,7 +88,7 @@ function getCurrentValue(name) {
return null;
}

function switchTheme(styleElem, mainStyleElem, newTheme) {
function switchTheme(styleElem, mainStyleElem, newTheme, dontSaveNewValue) {
Copy link
Member

Choose a reason for hiding this comment

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

I'm a bit wary of naming the parameter "don't save new value", just because it feels weird to name a boolean parameter after the opposite of what it does. Maybe something like initialLoad or skipStorage? Or even inverting the value that is sent on the initial call and calling it persist?

Copy link
Member Author

Choose a reason for hiding this comment

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

I like skipStorage! Let's go for it! :)

@@ -54,6 +54,21 @@
box-sizing: border-box;
}

/* This part handles the "default" theme being used depending on the system one. */
html {
content: "";
Copy link
Member

Choose a reason for hiding this comment

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

Just to confirm, since string values for content is only ever allowed for ::before and ::after, this won't cause a situation where the entire page is replaced with whatever is set here? MDN seems to imply this, but i want to double-check that that's what's actually going on.

@GuillaumeGomez
Copy link
Member Author

@QuietMisdreavus: I'm not sure it's worth it to add a system theme entry. It's just in case you didn't set your theme after all, and since it's locally stored, if you change of computer it'll just pick a theme depending on your system theme.

@GuillaumeGomez
Copy link
Member Author

Updated!

@QuietMisdreavus
Copy link
Member

@QuietMisdreavus: I'm not sure it's worth it to add a system theme entry. It's just in case you didn't set your theme after all, and since it's locally stored, if you change of computer it'll just pick a theme depending on your system theme.

I'm not sure what you mean by "change of computer". My concern is that people who have already set a theme on doc.rust-lang.org won't have the option of using the system theme as a default without clearing their browser history or manually digging into developer tools to fiddle with specific Local Storage values. Since we've had themes available on that domain for a long time now, it's very likely that users will already have a theme setting saved. The same goes for docs.rs.

@GuillaumeGomez
Copy link
Member Author

Why would you want to use it system theme if you already set it yourself? I'm not sure to follow on this one...

@QuietMisdreavus
Copy link
Member

Why would you want to use it system theme if you already set it yourself? I'm not sure to follow on this one...

What if you decide you wanted to change your system theme? What if you have your system theme change on different times of day (e.g. to track sunset and sunrise, or to differentiate between "work mode" and "personal mode", etc)? All of our current users have already set a theme choice, and may want to start taking advantage of this feature once it lands.

@GuillaumeGomez
Copy link
Member Author

Sounds weird to change the website design when switching pages though. Hummm... I don't want to add the "system theme" entry but I don't really know what to do for this one...

@Dylan-DPC-zz
Copy link

ping from triage @QuietMisdreavus any updates on this?

@totsteps
Copy link
Member

ping from triage, @QuietMisdreavus any updates on this?

@fmckeogh
Copy link
Member

Second ping from wg-triage, pinging random member of Rustdoc.

@ollie27

@GuillaumeGomez
Copy link
Member Author

It's been around for a while and I resolved all issues (as long as the users don't change the theme by themselves, it won't be set in the local storage so changing page will still rely on the system theme).

I'll wait to see if @ollie27 has anything to say.

@fmckeogh
Copy link
Member

fmckeogh commented Jul 31, 2019

Ah sorry, my bad, I wasn't sure if the final concern was blocking or not.

@GuillaumeGomez
Copy link
Member Author

If you're talking about window.matchMedia, it can't be added because not supported in old browsers (but very interesting more new projects of mine!).

@dima74
Copy link
Contributor

dima74 commented Aug 3, 2019

@GuillaumeGomez may I ask, which old browsers did you mean? I don't know exactly policy about browsers (best what I found is this rfc), but looks like window.matchMedia has strong browser support:

image

@GuillaumeGomez
Copy link
Member Author

We unofficially support rustdoc starting IE9. I think I'll drop it not too far in the future though.

@JohnTitor
Copy link
Member

Ping from triage: @QuietMisdreavus @ollie27 waiting on your review

@bjorn3
Copy link
Member

bjorn3 commented Aug 20, 2019

@Chocol4te I am not a rust-lang member.

@fmckeogh
Copy link
Member

@bjorn3 I'm very sorry, I saw the Reviewers tab and mistakenly assumed you were

@GuillaumeGomez
Copy link
Member Author

r? @Mark-Simulacrum

updateLocalStorage("rustdoc-theme", newTheme);
// If this new value comes from a system setting or from the previously saved theme, no
// need to save it.
if (skipStorage !== true) {
Copy link
Member

Choose a reason for hiding this comment

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

Could we make this a positive variable instead of skip? So that the if could become if (saveTheme) { ... }

src/librustdoc/html/static/storage.js Show resolved Hide resolved
@Mark-Simulacrum
Copy link
Member

As to allowing users to say "go with whatever the system does" after having configured something, it does seem like we could always add that functionality at a later point -- there's no need to block this PR on that then. I agree with @GuillaumeGomez that it seems like a fairly unusual desire (but I don't know anyone who changes system themes often...)

@GuillaumeGomez
Copy link
Member Author

@Mark-Simulacrum Applied the change you suggested.

@Mark-Simulacrum
Copy link
Member

@bors r+

Looks good to me!

@bors
Copy link
Contributor

bors commented Aug 21, 2019

📌 Commit 1bd9424 has been approved by Mark-Simulacrum

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 21, 2019
Centril added a commit to Centril/rust that referenced this pull request Aug 21, 2019
…-Simulacrum

take into account the system theme

Fixes rust-lang#61079.

The CSS can now take into account the system theme. I used it to generate some content on the document and from there, if no theme has already been selected, it'll look at the system level theme.

r? @QuietMisdreavus
cc @fenhl
bors added a commit that referenced this pull request Aug 21, 2019
Rollup of 6 pull requests

Successful merges:

 - #61236 (take into account the system theme)
 - #63717 (Fix nested eager expansions in arguments of `format_args`)
 - #63747 (update Miri)
 - #63772 (ci: move libc mirrors to the rust-lang-ci-mirrors bucket)
 - #63780 (Improve diagnostics: break/continue in wrong context)
 - #63781 (Run Clippy without json-rendered flag)

Failed merges:

r? @ghost
@bors bors merged commit 1bd9424 into rust-lang:master Aug 21, 2019
@fenhl
Copy link
Contributor

fenhl commented Aug 22, 2019

Unfortunately, it doesn't seem to be working. Inspector is showing content: "dark"; in the styles for the <html> tag, and local storage is empty, but I'm still seeing the light theme. (This is on a file: URL, haven't tested https: since docs.rs isn't updated yet.)

@GuillaumeGomez GuillaumeGomez deleted the system-theme branch August 22, 2019 11:30
@GuillaumeGomez
Copy link
Member Author

GuillaumeGomez commented Aug 22, 2019

Give it a try on https://doc.rust-lang.org/nightly/std

@fenhl
Copy link
Contributor

fenhl commented Aug 23, 2019

Same there.

@GuillaumeGomez
Copy link
Member Author

Can you open an issue so I can add it to my todo list please?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

rustdoc: Automatic light/dark theme