-
Notifications
You must be signed in to change notification settings - Fork 266
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
Add background color to mainWindow to prevent white flashes while launching #274
Conversation
@@ -925,7 +925,7 @@ app.on('ready', async () => { | |||
const preloadScript = path.normalize(`${__dirname}/preload.js`); | |||
mainWindow = global.mainWindow = new BrowserWindow({ | |||
// https://www.electronjs.org/docs/faq#the-font-looks-blurry-what-is-this-and-what-can-i-do | |||
backgroundColor: '#fff', | |||
backgroundColor: nativeTheme.shouldUseDarkColors ? '#16191e' : '#fff', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it a good idea to hardcode the colors in the first place? Is there any way of importing them dynamically while still fixing the white flash?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried to not hardcode the colors, but i could't found the file with them. I've got these colors from the element-web repo theming.md file.
@@ -1015,7 +1015,9 @@ app.on('window-all-closed', () => { | |||
}); | |||
|
|||
app.on('activate', () => { | |||
mainWindow.show(); | |||
mainWindow.once('ready-to-show', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean, the background color might also be a moot point if you’re using the ready-to-show
event…
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand, but this line seems to do nothing.
I would suggest making an IPC call from the react-sdk with the background colour every time the user changes their theme, this way we're not hardcoding the colour. Then I would listen for this on the Electron side and write the colour to electron store so that we can get it at load time. We do something similar for spell checking, see #179 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed with everything that was mentioned here,
We can not afford to hardcode the colours in element-web
as different users might use different themes
It would also be very useful to have a before/after screen recording for reference in the PR description
I posted a short unlisted video of the “before” behavior on YouTube and linked it in the initial Issue. I don’t have a build setup for Element, though, so I can’t compare any “after” at the moment. |
The colors for iOS are set in these three files: The colors for Android are set in these two files: On the web, colors are set by these files:
However, neither of these files appears anywhere in the Element GitHub organization . Only similar file I could find was src/skins/vector/css/common.css, but that file (along with the entire src/skins directory) abruptly disappears in April 2016. It turns out that all of the theming code got moved to Best practices discourage referencing the CSS directly, though const cssThemes = {
// CSS themes
"theme-legacy-light": "./node_modules/matrix-react-sdk/res/themes/legacy-light/css/legacy-light.scss",
"theme-legacy-dark": "./node_modules/matrix-react-sdk/res/themes/legacy-dark/css/legacy-dark.scss",
"theme-light": "./node_modules/matrix-react-sdk/res/themes/light/css/light.scss",
"theme-light-high-contrast":
"./node_modules/matrix-react-sdk/res/themes/light-high-contrast/css/light-high-contrast.scss",
"theme-dark": "./node_modules/matrix-react-sdk/res/themes/dark/css/dark.scss",
"theme-light-custom": "./node_modules/matrix-react-sdk/res/themes/light-custom/css/light-custom.scss",
"theme-dark-custom": "./node_modules/matrix-react-sdk/res/themes/dark-custom/css/dark-custom.scss",
}; If you want to import the color value "correctly", you can probably do so with the function import bodyToHtml from '@matrix-react-sdk/src/HtmlUtils';
...
const html = bodyToHtml(content, null);
backgroundColor = html.attribs['background-color']; The main issue here is that I don't know what you would use for the parameter If you can't use |
Another approach you could take if The main complication is that you would need to make this change over at |
Actually, I just realized that despite this pull request being on |
I'm facing some problems when calling the matrix-react-sdk from element-desktop. Do I need to install the package with yarn? Because I did that and there's a lot of typing errors. |
I haven’t attempted anything myself, but I did a quick cross-repository search for Basically it seems like I’ve been digging into this on my own, and I will hopefully be able to get back to you with something soon. |
I just linked back from what I’ve been working on, but it’s kind of only tangentially related at this point. Two potential routes for making the background color available to Electron could be |
If you wanted to copy the basic functionality of First, create the functions for saving and loading the window background color to and from a JSON file on disk: const path = require('path');
const jsonfile = require('jsonfile');
const mkdirp = require('mkdirp');
const backgroundColorStoreFilename = path.join(app.getPath('userData'), 'background-color.json');
...
function saveBackgroundColor(win) {
state = {'backgroundColor':win.getBackgroundColor()}
try {
mkdirp.sync(path.dirname(backgroundColorStoreFilename));
jsonfile.writeFileSync(backgroundColorStoreFilename, state);
} catch (err) {
// Do nothing
}
}
function loadBackgroundColor() {}
try {
return jsonfile.readFileSync(backgroundColorStoreFilename)['backgroundColor']
} catch (err) {
return backgroundColorPreload = nativeTheme.shouldUseDarkColors ? '#15191e' : '#fff'
}
} Next, add a call to mainWindow.on('close', async (e) => {
saveBackgroundColor(mainWindow)
...
}); (We use the window-close event rather than the app-quit event in case the user quits the application while no windows are open.) Then, finally, add a call to mainWindow = global.mainWindow = new BrowserWindow({
backgroundColor: loadBackgroundColor(),
...
}) I haven't tested this, but barring any typos it should probably work? The main deficiency with this approach is that it doesn’t account for the possibility of the system switching between light and dark while the application is closed (assuming the user has the appearance set to match the system theme). Accounting for this scenario would require |
Could you please elaborate more on this solution? |
I'm not sure, but I think at the time I was misunderstanding how Electron interacts with the React client. If I remember correctly |
Basically if I remember correctly I couldn't find a way for React to cache the background color in such a way that Electron would have access to it, which is why I suggested piggy-backing on |
|
To revisit this: the code now only shows the window after the ready-to-show event, so I think this should be unnecessary. I'll close this unless anyone knows otherwise? |
Type: defect
Fix #817
notes: Add background color to mainWindow to prevent white flashes while launching. Got solution from official docs.
Signed-off-by: Pedro Costa [email protected]
Here's what your changelog entry will look like:
🐛 Bug Fixes