Repository navigation
gui, lib/api: Adds support for prefers-color-scheme (fixes #6115) - #6124
Conversation
…ixes syncthing#6115) - Renames current default theme into a new "light" theme - Modifies assets serving to allow getting assets from different themes
|
The path for getting an asset for a different theme is now |
|
This needs to fall back correctly if the media query is not supported. As per https://caniuse.com/#feat=prefers-color-scheme, it's currently not supported by:
Unless I'm mistaken, all the queries this is making right now will evaluate to false on such browsers. I'm pretty sure that handling this is going to require dropping to JS, though at that point it makes more sense to just use JS to set a cookie (or header, or whatever else) to pass the info back to the server component. |
|
I haven't tested it - does it work? On both browsers with and without (I don't see any acknowledgement of @Ferroin s concerns above) support for this magic? |
|
@Ferroin is right, it does not fall back correctly in a browser without support. The A possible solution is to import the light theme unconditionally first and rely on CSS precedence rules, as in: @import "/theme-assets/light/assets/css/theme.css";
@import "/theme-assets/dark/assets/css/theme.css" (prefers-color-scheme: dark);This works, but care should be taken in the future to avoid any rules on the light theme having more specificity than the ones in the dark theme. If that is not a feasible option, I see no other way but to use JS (for example as web.dev suggests here). I am not familiar with Angular so I would probably need some help to implement that. |
|
Can't we do this in js? In that case we would be able to check the absence of value. |
|
I have used the article's code to include support for older browsers and tested it with an older Firefox version. As I said, I am not familiar with Angular so please check that I haven't messed up anything. I have also done some testing with regards to printing and it looks like in recent versions of Firefox and Chromium it works just fine (maybe they ignore the color scheme when printing?). |
|
I am not a Syncthing developer, so you can ignore my advice, but honestly this looks very hacky. The whole
The popular Web browsers only started to support it a few months ago, so everything released prior to that has no support for the feature at all. If you want to implement support for such an experimental feature, I believe that it would be a good idea to do it in the most unobtrusive way possible, with no risk of affecting the older browsers. I personally think that the method of just overriding the light theme values mentioned above is much safer, as the unsupported media query will simply be ignored by the old browsers, with no need for a JavaScript bandaid. |
|
Even if the official document is still technically a draft, I think the feature is mature enough for Syncthing to support it. Roughly three quarters of users could benefit from it right now and the feature seems stable: the issues at WebKit, Chromium and Firefox have been closed for months and the existing CSSWG Drafts issues do not discuss any changes to the syntax or the expected behavior of the feature. That being said, I also favor the method that does not use Javascript as @tomasz1986 suggests, since I find it simpler to maintain and update, but of course I am not a Syncthing developer either and based on previous comments I understood the Javascript solution was the one that @AudriusButkevicius preferred. |
|
Actually thinking about this further, I think we might be able to do this using css hackery. Is there a way to do something like "if you support this property include this file otherwise don't"? Or browsers that do not support it just defaults to matching the selector? |
I personally would not believe any browser usage statistics, as they are heavily skewed by adblockers, which usually also block analytics, and adblocker usage in some countries is as high as 40-50%. Also, Android users are often stuck with a very old version of WebView, which Syncthing uses to display the GUI, regardless of the actual web browsers installed. In addition to that, IE11 support is a must if you care about any enterprise or government systems. Nevertheless, my point was just to be very careful when implementing such a feature, not to not implement it all 😉.
The above-mentioned CSS should do it. The feature shall be ignored in the browsers which have no support for it. If you want to exclude printing too, then the following code will probably suffice. This should limit the automatic dark theme application only to screens (i.e. exclude printers and screen readers), and only when the |
|
I have actually just tested the changes both in Chromium, Firefox, and IE11.
When the option is enabled, using just |
| document.head.insertAdjacentHTML( | ||
| 'beforeend', | ||
| '<link rel="stylesheet" href="/theme-assets/light/assets/css/theme.css" onload="document.documentElement.style.display = \'\'">' | ||
| ); |
There was a problem hiding this comment.
Why are we setting something to display none? Also, on a browser that does not support this, we'd be repeatedly injecting the same element on every config update (even if nothing has changed).
There was a problem hiding this comment.
Also, I guess this ends up being a third css file we load if there is no support?
Or does it not load the other two?
If it does not, cannot we just match on 'not all' in css?
There was a problem hiding this comment.
Or use a not(selector) ?
There was a problem hiding this comment.
Why are we setting something to display none?
This is suggested by web.dev: "to avoid a flash of unstyled content, I hide the content of the page until light.css has loaded."
we'd be repeatedly injecting the same element on every config update
my bad, I only tested loading a new theme and in that case the page is reloaded.
There was a problem hiding this comment.
If it does not, cannot we just match on 'not all' in css? Or use a not(selector) ?
I tried not using JS and changing the light theme condition to not all and (prefers-color-scheme: dark). It works correctly on Firefox 70 but it doesn't on Firefox 52 (a browser without support for prefers-color-scheme).
There was a problem hiding this comment.
But is this not the case right now (without this commit, that is)?
The original default theme was light, and the other themes have been built on top of it. Is this wrong? Do you want to completely separate the light theme from the rest? The problem is that by doing so, there will be no fallback, which means good bye to progressive enhancement.
There was a problem hiding this comment.
That is not the case now, as all the default stuff comes from bootstrap itself.
The dark theme just overrides what bootstrap does, which we can assume is stable and we can cover the ground.
Our light theme on the other hand, is customisation to bootstrap, which for now does not have a dodgy td: white, but it might, at which point that will override the dark td inherited from body when setting body colour in bootstrap.
There was a problem hiding this comment.
Oh, you are right. I wrongly assumed that the theme.css from the default theme was applied to all the other themes, while this does not seem to be the case. I am sorry for the confusion.
I still do not believe that using JavaScript to detect browser features in order to provide a "fallback" for CSS is the right way. If something goes wrong with the JavaScript hackery here (which is very likely e.g. in a quirky older browser like the Android's WebView), then neither the light nor the dark theme will be applied at all. This may not be a big deal at the moment, as the two themes have a very limited set of styles right now, but may become a problem if they are expanded in the future.
There was a problem hiding this comment.
not allmeansnot screen,not print, andnot speech, so it is basically the same as not having the media query at all. It has nothing to do with support for the specific feature, and of course will be completely ignored by any browser.
That media query can be confusing but it doesn't work that way. not all and (prefers-color-scheme: dark) is parsed as not (all and (prefers-color-scheme: dark)), that is, the negation of prefers-color-scheme: dark.
all is included because one can't negate prefers-color-scheme directly; as MDN says : "The not keyword can't be used to negate an individual feature query, only an entire media query". See also this SO answer.
This query works on recent browsers (and it is the one used in the last revision of Thunderbird's CSS to set a light theme, see line 23), but it didn't work on Firefox 52 when I tried. You are welcome to suggest other possible media queries to see if we can come up with something compatible with older browsers, but sadly I don't think there is a pure CSS solution.
There was a problem hiding this comment.
I have updated myself on the media queries specification (https://www.w3.org/TR/css3-mediaqueries and https://www.w3.org/TR/mediaqueries-4), and yes, you are right about the not keyword, which is applied to the whole declaration following it. The whole all is not required though. By the official specification, you could even do a simple negation like this:
@import "/theme-assets/dark/assets/css/theme.css" screen and (prefers-color-scheme: dark);
@import "/theme-assets/light/assets/css/theme.css" not screen and (prefers-color-scheme: dark);
The problem is that this does not work in real life. While the syntax may work correctly in newer browsers, IE11 completely ignores it, despite the fact that theoretically the browser should support CSS3 media queries.
With this in mind, in my opinion, the only way to do the whole thing with CSS only is to apply one of the themes by default, and then override the values with the other one (i.e. use the CSS cascade). This would indeed require to maintain the two themes simultaneously. They could even be merged into a single file. If the Syncthing developers do not like this, then indeed a JavaScript band-aid may be the only possible method.
All in all, if the JavaScript hackery is inevitable, how about using the above-mentioned negation instead of the current rule?
@import "/theme-assets/dark/assets/css/theme.css" screen and (prefers-color-scheme: dark);
@import "/theme-assets/light/assets/css/theme.css" not screen and (prefers-color-scheme: dark);
This should be safer and hopefully compatible with more browsers (i.e. those with proper CSS3 media query implementation yet no prefers-color-scheme support). If not, then I would suggest to at least add print to the light theme's rule.
@import "/theme-assets/dark/assets/css/theme.css" screen and (prefers-color-scheme: dark);
@import "/theme-assets/light/assets/css/theme.css" print, (prefers-color-scheme: light), (prefers-color-scheme: no-preference);
|
Ignore the above. I was mistaken. The If the modification is inevitable, would it not be a good idea to update the documentation accordingly and explain the new behaviour there? |
The CSS is always loaded if there is no support for `matchMedia`.
AudriusButkevicius
left a comment
There was a problem hiding this comment.
Looks good, but I need to try it out
This comment was marked as spam.
This comment was marked as spam.
|
Looks good. I have a follow up PR fixing cache invalidation sucking badly. |
|
Thanks for the PR |
|
Great, thank you for your assistance and your patience! |
|
I wrote about it above in a comment, but it was apparently ignored. When using dark theme and printing the page, no theme is used for the printed page. Is this the desirable behaviour? You need to have |
|
I think so. |
|
Why not use the light theme? Now, when This means that the printed page will look different depending on the OS/browser color scheme, even though the theme selected in Syncthing is just "default" in both cases. |
|
Sure, file a PR fixing it, but I think this is so moot that nobody actually cares. The page is not made for printing, it doesn't make sense to do that. |
|
Yes, that is true, and I am personally not interested in printing the page myself too, but you were the one to mention print in the first place, hence my question. |
|
This doesn't work with reverse proxy, as the URL @import "../../theme-assets/dark/assets/css/theme.css" screen and (prefers-color-scheme: dark);The link in the JS fallback will probably work, if you just remove the leading |

Purpose
Fixes #6115. In particular:
theme-assetsandprefers-color-scheme.Testing
Instructions on how to activate dark mode on Firefox for testing are available here.
prefers-color-schemeset tolightorno-preference→ it has the light themeprefers-color-schemeset todark→ it has the dark themeprefers-color-scheme→ it has the light themeprefers-color-scheme)prefers-color-schemeor whether it is supported)