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

System theme preference #1800

Merged
merged 30 commits into from
May 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
e1a2d43
Add system preference to theme-settings
vallode Oct 7, 2021
2c04d3c
Add watching logic to check for dark theme settings
vallode Oct 7, 2021
aa2686c
Add en system theme translation
vallode Oct 7, 2021
125ab92
Add explicit darkTheme option to browserWindow
vallode Oct 8, 2021
5343f8a
Remove unnecessary themeSource line
vallode Oct 8, 2021
2893fbf
Fix lint errors
vallode Oct 8, 2021
607fc15
Move to using main process messaging for theme change
vallode Oct 9, 2021
f07a3a2
Add system dataset selectors for themes
vallode Oct 9, 2021
f2ef15c
Fix lint errors
vallode Oct 9, 2021
ee93030
Change system theme to system default
vallode Oct 10, 2021
774eb0c
Use system default for deciding background color of newWindow
vallode Oct 10, 2021
025998f
Add baseTheme to state persistance
vallode Oct 10, 2021
1cb02e5
Use baseTheme on browserWindow creation
vallode Oct 10, 2021
24aa328
Fix lint errors
vallode Oct 10, 2021
d65c2c3
Improve window background logic
vallode Oct 14, 2021
b206d68
Catch settingsDb errors
vallode Oct 14, 2021
b2af0f5
Remove dark flash on light themes
vallode Oct 14, 2021
10e719b
Fix lint issues
vallode Oct 14, 2021
2907bfb
Fix system default sync on multiple windows
vallode Oct 15, 2021
1b1e697
Load database on each new window
vallode Oct 15, 2021
642aaf2
Fix lint errors
vallode Oct 15, 2021
08f587d
Update compatibility for shared electron storage
vallode Dec 20, 2021
d87aa54
Remove unused console log
vallode Dec 20, 2021
1b74c3f
Revert unnecessary changes
vallode Dec 21, 2021
6714ff8
Fix window maximize white flash
vallode Jan 12, 2022
b4d4581
Merge remote-tracking branch 'upstream/development' into system-theme…
vallode Mar 5, 2022
1c31a52
Fix handleBaseTheme usage
vallode Mar 5, 2022
5935fb2
Use data-system-theme instead of data-theme
vallode Mar 5, 2022
f3e109c
Revert window maximize changes
vallode Mar 5, 2022
910f60b
Fix theme flash on new window open
vallode Mar 6, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const IpcChannels = {
START_POWER_SAVE_BLOCKER: 'start-power-save-blocker',
CREATE_NEW_WINDOW: 'create-new-window',
OPEN_IN_EXTERNAL_PLAYER: 'open-in-external-player',
NATIVE_THEME_UPDATE: 'native-theme-update',

DB_SETTINGS: 'db-settings',
DB_HISTORY: 'db-history',
Expand Down
4 changes: 4 additions & 0 deletions src/datastores/handlers/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ class Settings {
return db.settings.findOne({ _id: 'bounds' })
}

static _findTheme() {
return db.settings.findOne({ _id: 'baseTheme' })
}

static _updateBounds(value) {
return db.settings.update({ _id: 'bounds' }, { _id: 'bounds', value }, { upsert: true })
}
Expand Down
2 changes: 1 addition & 1 deletion src/index.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<% } %>
</head>

<body class="dark mainRed secBlue">
<body>
<div id="app"></div>
<!-- Set `__static` path to static files in production -->
<script>
Expand Down
37 changes: 35 additions & 2 deletions src/main/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
app, BrowserWindow, dialog, Menu, ipcMain,
powerSaveBlocker, screen, session, shell
powerSaveBlocker, screen, session, shell, nativeTheme
} from 'electron'
import path from 'path'
import cp from 'child_process'
Expand Down Expand Up @@ -35,6 +35,7 @@ function runApp() {
process.env.ELECTRON_DISABLE_SECURITY_WARNINGS = 'true'
const isDev = process.env.NODE_ENV === 'development'
const isDebug = process.argv.includes('--debug')

let mainWindow
let startupUrl

Expand Down Expand Up @@ -172,11 +173,33 @@ function runApp() {
}

async function createWindow({ replaceMainWindow = true, windowStartupUrl = null, showWindowNow = false } = { }) {
// Syncing new window background to theme choice.
const windowBackground = await baseHandlers.settings._findTheme().then(({ value }) => {
switch (value) {
case 'dark':
return '#212121'
case 'light':
return '#f1f1f1'
case 'black':
return '#000000'
case 'dracula':
return '#282a36'
case 'system':
default:
return nativeTheme.shouldUseDarkColors ? '#212121' : '#f1f1f1'
}
}).catch((error) => {
console.log(error)
// Default to nativeTheme settings if nothing is found.
return nativeTheme.shouldUseDarkColors ? '#212121' : '#f1f1f1'
})

/**
* Initial window options
*/
const commonBrowserWindowOptions = {
backgroundColor: '#212121',
backgroundColor: windowBackground,
darkTheme: nativeTheme.shouldUseDarkColors,
icon: isDev
? path.join(__dirname, '../../_icons/iconColor.png')
/* eslint-disable-next-line */
Expand All @@ -191,6 +214,7 @@ function runApp() {
contextIsolation: false
}
}

const newWindow = new BrowserWindow(
Object.assign(
{
Expand Down Expand Up @@ -241,6 +265,7 @@ function runApp() {
height: bounds.height
})
}

if (maximized) {
newWindow.maximize()
}
Expand Down Expand Up @@ -345,6 +370,14 @@ function runApp() {
app.quit()
})

nativeTheme.on('updated', () => {
const allWindows = BrowserWindow.getAllWindows()

allWindows.forEach((window) => {
window.webContents.send(IpcChannels.NATIVE_THEME_UPDATE, nativeTheme.shouldUseDarkColors)
})
})

ipcMain.on(IpcChannels.ENABLE_PROXY, (_, url) => {
console.log(url)
session.defaultSession.setProxy({
Expand Down
25 changes: 21 additions & 4 deletions src/renderer/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import FtProgressBar from './components/ft-progress-bar/ft-progress-bar.vue'
import $ from 'jquery'
import { marked } from 'marked'
import Parser from 'rss-parser'
import { IpcChannels } from '../constants'

let ipcRenderer = null

Expand Down Expand Up @@ -113,6 +114,10 @@ export default Vue.extend({
return this.$store.getters.getSecColor
},

systemTheme: function () {
return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'
},

externalLinkOpeningPromptNames: function () {
return [
this.$t('Yes'),
Expand Down Expand Up @@ -140,11 +145,13 @@ export default Vue.extend({
}
},
created () {
this.checkThemeSettings()
this.setBodyTheme()
this.setWindowTitle()
},
mounted: function () {
this.grabUserSettings().then(async () => {
this.checkThemeSettings()

await this.fetchInvidiousInstances({ isDev: this.isDev })
if (this.defaultInvidiousInstance === '') {
await this.setRandomCurrentInvidiousInstance()
Expand All @@ -161,6 +168,7 @@ export default Vue.extend({
this.activateKeyboardShortcuts()
this.openAllLinksExternally()
this.enableOpenUrl()
this.watchSystemTheme()
await this.checkExternalPlayer()
}

Expand Down Expand Up @@ -191,9 +199,8 @@ export default Vue.extend({
updateTheme: function (theme) {
console.group('updateTheme')
console.log('Theme: ', theme)
const className = `${theme.baseTheme} main${theme.mainColor} sec${theme.secColor}`
const body = document.getElementsByTagName('body')[0]
body.className = className
document.body.className = `${theme.baseTheme} main${theme.mainColor} sec${theme.secColor}`
document.body.dataset.systemTheme = this.systemTheme
console.groupEnd()
},

Expand Down Expand Up @@ -432,6 +439,16 @@ export default Vue.extend({
})
},

/**
* Linux fix for dynamically updating theme preference, this works on
* all systems running the electron app.
*/
watchSystemTheme: function () {
ipcRenderer.on(IpcChannels.NATIVE_THEME_UPDATE, (event, shouldUseDarkColors) => {
document.body.dataset.systemTheme = shouldUseDarkColors ? 'dark' : 'light'
})
},

enableOpenUrl: function () {
ipcRenderer.on('openUrl', (event, url) => {
if (url) {
Expand Down
1 change: 1 addition & 0 deletions src/renderer/App.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<template>
<div
v-if="dataReady"
id="app"
:class="{
hideOutlines: hideOutlines,
Expand Down
2 changes: 2 additions & 0 deletions src/renderer/components/theme-settings/theme-settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export default Vue.extend({
'no'
],
baseThemeValues: [
'system',
'light',
'dark',
'black',
Expand Down Expand Up @@ -111,6 +112,7 @@ export default Vue.extend({

baseThemeNames: function () {
return [
this.$t('Settings.Theme Settings.Base Theme.System Default'),
this.$t('Settings.Theme Settings.Base Theme.Light'),
this.$t('Settings.Theme Settings.Base Theme.Dark'),
this.$t('Settings.Theme Settings.Base Theme.Black'),
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/store/modules/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ const state = {
barColor: false,
checkForBlogPosts: true,
checkForUpdates: true,
baseTheme: 'dark',
baseTheme: 'system',
vallode marked this conversation as resolved.
Show resolved Hide resolved
mainColor: 'Red',
secColor: 'Blue',
defaultCaptionSettings: '{}',
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/themes.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.light {
.system[data-system-theme*='light'], .light {
--primary-text-color: #212121;
--secondary-text-color: #424242;
--tertiary-text-color: #757575;
Expand All @@ -22,7 +22,7 @@
--logo-text: url("~../../_icons/textColorSmall.png");
}

.dark {
.system[data-system-theme*='dark'], .dark {
--primary-text-color: #EEEEEE;
--secondary-text-color: #ddd;
--tertiary-text-color: #999;
Expand Down
1 change: 1 addition & 0 deletions static/locales/en-US.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ Settings:
Base Theme: Base Theme
Black: Black
Dark: Dark
System Default: System Default
Light: Light
Dracula: Dracula
Main Color Theme:
Expand Down
1 change: 1 addition & 0 deletions static/locales/en_GB.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ Settings:
Base Theme: 'Base theme'
Black: 'Black'
Dark: 'Dark'
System Default: 'System Default'
Light: 'Light'
Dracula: 'Dracula'
Main Color Theme:
Expand Down