Skip to content

Commit

Permalink
feat(main-preferences): 增加偏好设置框架
Browse files Browse the repository at this point in the history
  • Loading branch information
Blore-lzn authored and Brucezhuu committed May 1, 2023
1 parent 75a78a4 commit d1d6157
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import EAU from './main/update'
import path from 'path'
import * as url from 'url'
import { isOsx, isWindows } from './main/config'
import Preference from './main/preferences'

const isDevelopment = process.env.NODE_ENV !== 'production'
let ficusPath = ''
Expand Down
19 changes: 19 additions & 0 deletions src/main/environment/appPaths.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { app } from 'electron'

class AppPaths {
constructor (userDataPath = '') {
if (!userDataPath) {
// Use default user data path.
userDataPath = app.getPath('userData')
}

this._userDataPath = userDataPath
app.setPath('userData', this._userDataPath)
}

get userDataPath () {
return this._userDataPath
}
}

export default AppPaths
3 changes: 2 additions & 1 deletion src/main/filesystem/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ exports.initFromFolder = async () => {
return { relation: {}, error: -2 }
}
console.log(result.filePaths[0])
accessor.menu.addRecentlyUsedDocument(result.filePaths[0])

const folderName = path.basename(result.filePaths[0])
accessor.menu.addRecentlyUsedDocument(folderName)
const tree = await getProject(result.filePaths[0])
const projectStat = {
version: 1,
Expand Down
9 changes: 9 additions & 0 deletions src/main/helper/container.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function hasSameKey (a, b) {
const aKeys = Object.keys(a).sort()
const bKeys = Object.keys(b).sort()
return JSON.stringify(aKeys) === JSON.stringify(bKeys)
}

export {
hasSameKey
}
76 changes: 76 additions & 0 deletions src/main/preferences/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import fs from 'fs-extra'
import path from 'path'
import Store from 'electron-store'
import schema from './schema.json'
import { hasSameKey } from '../helper/container'

/**
* 用户偏好设置
* 实现参考:https://github.com/marktext/marktext/blob/develop/src/main/preferences
*/
class Preference {
constructor (preferencePath) {
this._preferencePath = path.resolve(preferencePath, 'preferences.json')
this._defaultPreferencePath = path.resolve(__dirname, '..', 'static', 'preferences.json')
// 注:electron-store有性能问题(IO),但支持JSON scheme验证
this._store = new Store({
schema,
name: 'preferences'
})
}

init () {
let defaultSettings = null
try {
defaultSettings = JSON.parse(fs.readFileSync(this.staticPath, { encoding: 'utf8' }) || '{}')
} catch (err) {
console.log(err)
}

if (!defaultSettings) {
throw new Error('Can not load static preference.json file')
}

if (!fs.existsSync(this._preferencePath)) {
this.store.set(defaultSettings)
} else {
const userSetting = this.getAll()
const requiresUpdate = !hasSameKey(defaultSettings, userSetting)
if (requiresUpdate) {
// Remove outdated settings
for (const key of userSetting.keys()) {
if (!defaultSettings.keys().includes(key)) {
delete userSetting[key]
this.store.delete(key)
}
}

// Add new setting options
let addedNewEntries = false
for (const key in defaultSettings) {
if (!userSetting.keys().includes(key)) {
addedNewEntries = true
userSetting[key] = defaultSettings[key]
}
}
if (addedNewEntries) {
this.store.set(userSetting)
}
}
}
}

setItem (key, value) {
return this._store.set(key, value)
}

getItem (key) {
return this._store.get(key)
}

getAll () {
return this._store.store
}
}

export default Preference
13 changes: 13 additions & 0 deletions src/main/preferences/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"autoMaximize": {
"description": "General--Auto maxmize window when start",
"type": "boolean",
"default": false
},
"showTrafficLights": {
"description": "General--Show traffic lights on MacOS",
"type": "boolean",
"default": false
}
}

4 changes: 4 additions & 0 deletions static/preference.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"autoMaximize": false,
"showTrafficLights": false
}

0 comments on commit d1d6157

Please sign in to comment.