Set dark theme when first saving (#901)

This commit is contained in:
Felix Häusler 2019-04-10 16:42:15 +02:00 committed by Ran Luo
parent 960b065a88
commit 2e50b62d99
5 changed files with 70 additions and 6 deletions

View File

@ -27,7 +27,7 @@
"postinstall": "npm run rebuild && npm run lint:fix",
"build:muya": "cd src/muya && webpack --progress --colors --config webpack.config.js",
"release:muya": "npm run build:muya && cd src/muya && npm publish",
"rebuild": "electron-rebuild -f -w keyboard-layout -o keyboard-layout",
"rebuild": "electron-rebuild -f -o keyboard-layout,vscode-windows-registry",
"gen-third-party": "node tools/generateThirdPartyLicense.js",
"validate-licenses": "node tools/validateLicenses.js"
},
@ -262,6 +262,9 @@
"webpack-hot-middleware": "^2.24.3",
"webpack-merge": "^4.2.1"
},
"optionalDependencies": {
"vscode-windows-registry": "1.0.1"
},
"repository": {
"type": "git",
"url": "git@github.com:marktext/marktext.git"

View File

@ -82,9 +82,9 @@ class App {
if (process.platform === 'darwin') {
app.dock.setMenu(dockMenu)
// listen for system theme change and change Mark Text own `dark` and `light`,
// In macOS 10.14 Mojave,
// Apple introduced a new system-wide dark mode for all macOS computers.
// Listen for system theme change and change Mark Text own `dark` and `light`.
// In macOS 10.14 Mojave, Apple introduced a new system-wide dark mode for
// all macOS computers.
systemPreferences.subscribeNotification(
'AppleInterfaceThemeChangedNotification',
() => {

View File

@ -0,0 +1,34 @@
import { isWindows } from '../../config'
let GetStringRegKey = null
if (isWindows) {
try {
GetStringRegKey = require('vscode-windows-registry').GetStringRegKey
} catch (e) {
// Ignore webpack build error on macOS and Linux.
}
}
export const winHKEY = {
HKCU: 'HKEY_CURRENT_USER',
HKLM: 'HKEY_LOCAL_MACHINE',
HKCR: 'HKEY_CLASSES_ROOT',
HKU: 'HKEY_USERS',
HKCC: 'HKEY_CURRENT_CONFIG'
}
/**
* Returns the registry key value.
*
* @param {winHKEY} hive The registry key
* @param {string} path The registry subkey
* @param {string} name The registry name
* @returns {string|null|undefined} The registry key value or null/undefined.
*/
export const getStringRegKey = (hive, path, name) => {
try {
return GetStringRegKey(hive, path, name)
} catch (e) {
return null
}
}

View File

@ -1,9 +1,21 @@
import fs from 'fs'
import path from 'path'
import { ipcMain, BrowserWindow } from 'electron'
import { isOsx } from './config'
import { ipcMain, BrowserWindow, systemPreferences } from 'electron'
import { isOsx, isWindows } from './config'
import appWindow from './window'
import { getPath, hasSameKeys, log, ensureDir } from './utils'
import { getStringRegKey, winHKEY } from './platform/win32/registry.js'
const isDarkSystemMode = () => {
if (isOsx) {
return systemPreferences.isDarkMode()
} else if (isWindows) {
// NOTE: This key is a 32-Bit DWORD but converted to JS string!
const buf = getStringRegKey(winHKEY.HKCU, 'Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize', 'AppsUseLightTheme')
return buf === '' // zero (0)
}
return false
}
class Preference {
constructor () {
@ -30,6 +42,9 @@ class Preference {
fs.writeFileSync(userDataPath, content, 'utf-8')
userSetting = this.loadJson(userDataPath)
if (isDarkSystemMode()) {
userSetting.theme = 'dark'
}
this.validateSettings(userSetting)
} else {
userSetting = this.loadJson(userDataPath)

View File

@ -7202,6 +7202,11 @@ nan@^2.10.0, nan@^2.9.2:
resolved "https://registry.yarnpkg.com/nan/-/nan-2.12.1.tgz#7b1aa193e9aa86057e3c7bbd0ac448e770925552"
integrity sha512-JY7V6lRkStKcKTvHO5NVSQRv+RV+FIL5pvDoLiAtSL9pKlC5x9PKQcZDsq7m4FO4d57mkhC6Z+QhAh3Jdk5JFw==
nan@^2.12.1:
version "2.13.2"
resolved "https://registry.yarnpkg.com/nan/-/nan-2.13.2.tgz#f51dc7ae66ba7d5d55e1e6d4d8092e802c9aefe7"
integrity sha512-TghvYc72wlMGMVMluVo9WRJc0mB8KxxF/gZ4YYFy7V2ZQX9l7rgbPg7vjS9mt6U5HXODVFVI2bOduCzwOMv/lw==
nanomatch@^1.2.1, nanomatch@^1.2.9:
version "1.2.13"
resolved "https://registry.yarnpkg.com/nanomatch/-/nanomatch-1.2.13.tgz#b87a8aa4fc0de8fe6be88895b38983ff265bd119"
@ -11050,6 +11055,13 @@ void-elements@^2.0.0:
resolved "https://registry.yarnpkg.com/void-elements/-/void-elements-2.0.1.tgz#c066afb582bb1cb4128d60ea92392e94d5e9dbec"
integrity sha1-wGavtYK7HLQSjWDqkjkulNXp2+w=
vscode-windows-registry@1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/vscode-windows-registry/-/vscode-windows-registry-1.0.1.tgz#bc9f765563eb6dc1c9ad9a41f9eaacc84dfadc7c"
integrity sha512-q0aKXi9Py1OBdmXIJJFeJBzpPJMMUxMJNBU9FysWIXEwJyMQGEVevKzM2J3Qz/cHSc5LVqibmoUWzZ7g+97qRg==
dependencies:
nan "^2.12.1"
vue-electron@^1.0.6:
version "1.0.6"
resolved "https://registry.yarnpkg.com/vue-electron/-/vue-electron-1.0.6.tgz#e798e03180b8933539defe31f92e53b9242b9406"