diff --git a/index.html b/index.html index a98b8b2..cc6a913 100644 --- a/index.html +++ b/index.html @@ -52,6 +52,7 @@ let root = document.querySelector(':root'); let isClassCountdown = true let isClassHidden = true + let isUseLocalConfig = false let lastScheduleData = { currentHighlight: { index: null, @@ -168,6 +169,21 @@ leftSidebar.style.display = scheduleConfig.week_display ? 'block' : 'none' } + function setStyle(){ + for(key in scheduleConfig.css_style){ + root.style.setProperty(key, scheduleConfig.css_style[key]) + } + } + + function setAll(){ + setCountdownerContent() + setScheduleClass() + setCountdownerPosition() + setSidebar() + setBackgroundDisplay() + setStyle() + } + function tick(reset = false) { scheduleData = getScheduleData(); setCountdownerContent() @@ -192,9 +208,7 @@ } }, 20); - for (key in scheduleConfig.css_style) { - root.style.setProperty(key, scheduleConfig.css_style[key]) - } + setStyle(); let timer = undefined window.addEventListener("mousemove", event => { @@ -230,6 +244,34 @@ }) } + ipcRenderer.on('openLoadConfig', (e, arg) => { + ipcRenderer.send('selectConfig', { + options: { + title: '选取本地配置文件(config.json)', + filters: [ + { + name: 'config', + extensions: ['json'] + } + ] + } + }) + }) + + ipcRenderer.on('setScheduleConfig', (e, arg) => { + console.log(arg.data) + isUseLocalConfig = true + scheduleConfig = JSON.parse(arg.data) + }) + + ipcRenderer.on('modifyScheduleConfig', (e, arg) => { + let filePath = arg.path + let fileContent = arg.data + scheduleConfig = JSON.parse(fileContent) + scheduleData = getScheduleData() + setAll() + }) + ipcRenderer.on('getSelectedClassIndex', (e, arg) => { if (arg.index == -1) return let text = arg.arg.options.buttons[arg.index] @@ -267,7 +309,9 @@ }); ipcRenderer.on('setWeekIndex', (e, arg) => { - scheduleConfig = JSON.parse(JSON.stringify(_scheduleConfig)) + if(!isUseLocalConfig){ + scheduleConfig = JSON.parse(JSON.stringify(_scheduleConfig)) + } weekIndex = arg localStorage.setItem('weekIndex', weekIndex.toString()) }) @@ -298,4 +342,4 @@ }) - \ No newline at end of file + diff --git a/main.js b/main.js index cfdb591..f7b81c4 100644 --- a/main.js +++ b/main.js @@ -62,6 +62,23 @@ app.whenReady().then(() => { createWindow() Menu.setApplicationMenu(null) win.webContents.on('did-finish-load', () => { + if(store.get('isUseLocalConfig',false)){ + dialog.showMessageBox(win,{ + message: store.get('configPath') + }) + fs.readFileSync(store.get('configPath'), { encoding: "utf8" } ,(err, data) => { + if(err) { + store.set('isUseLocalConfig',false) + throw err + } + if(data){ + dialog.showMessageBox(win,{ + message: data + }) + win.webContents.send('setScheduleConfig',data); + } + }) + } win.webContents.send('getWeekIndex'); }) const handle = win.getNativeWindowHandle(); @@ -167,6 +184,17 @@ ipcMain.on('getWeekIndex', (e, arg) => { setAutoLaunch() } }, + { + label: '使用本地配置文件', + type: 'checkbox', + checked: store.get('isUseLocalConfig', false), + click: (e) => { + store.set('isUseLocalConfig', e.checked) + if(e.checked){ + win.webContents.send('openLoadConfig') + } + } + }, { type: 'separator' }, @@ -214,6 +242,30 @@ ipcMain.on('dialog', (e, arg) => { }) }) +ipcMain.on('selectConfig', (e, arg) => { + dialog.showOpenDialog(win, arg.options).then((res) => { + if(!res.canceled){ + fs.readFile(res.filePaths[0], { encoding: 'utf8' }, (err, data) => { + if(err) { + console.log('[selectFile] Error while reading selected File.Please check if you have access to the file.') + store.set('isUseLocalConfig', false) + store.set('configPath', undefined) + return; + } + if(data) { + store.set('configPath', res.filePaths[0]) + e.sender.send('modifyScheduleConfig', { 'path': res.filePaths[0], 'data': data}) + return; + } + }) + } + else { + console.log("[selectFile] user cancelled") + store.set('isUseLocalConfig',false) + } + }) +}) + ipcMain.on('pop', (e, arg) => { tray.popUpContextMenu(form) }) @@ -237,4 +289,4 @@ ipcMain.on('getTimeOffset', (e, arg) => { win.webContents.send('setTimeOffset', Number(r) % 10000000000000) } }) -}) \ No newline at end of file +})