From 9e78279226f894193910a726ce10de4c8d20e3a2 Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Sun, 4 Mar 2018 10:47:14 +0100 Subject: [PATCH] main: handle theme from a dedicated module --- app/main/callbacks.js | 2 + app/main/electron-window.js | 10 ++--- app/main/server.js | 3 +- app/main/settings.js | 52 +------------------------ app/main/theme.js | 76 +++++++++++++++++++++++++++++++++++++ 5 files changed, 85 insertions(+), 58 deletions(-) create mode 100644 app/main/theme.js diff --git a/app/main/callbacks.js b/app/main/callbacks.js index 19895148..be4b0303 100644 --- a/app/main/callbacks.js +++ b/app/main/callbacks.js @@ -4,6 +4,7 @@ var vm = require('vm'), settings = require('./settings'), osc = require('./osc'), {ipc, clients} = require('./server'), + theme = require('./theme'), chokidar = require('chokidar') var openedSessions = {}, @@ -268,6 +269,7 @@ module.exports = { }, reloadCss:function(){ + theme.load() ipc.send('reloadCss') }, diff --git a/app/main/electron-window.js b/app/main/electron-window.js index 7ddf565d..d61af59d 100644 --- a/app/main/electron-window.js +++ b/app/main/electron-window.js @@ -1,24 +1,20 @@ var path = require('path'), {BrowserWindow, Menu, dialog} = require('electron'), shortcut = require('electron-localshortcut'), - settings = require('./settings') + settings = require('./settings'), + theme = require('./theme') module.exports = function(options={}) { var window - var theme = settings.read('theme'), - bgColor = theme && theme.join('\n').indexOf('--color-bg:') != -1 ? - theme.join('\n').match(/--color-bg:([^;]*);/)[1].trim() - : '#191f2a' - window = new BrowserWindow({ width: options.width || 800, height: options.height || 600, title: options.title || settings.read('appName'), icon: path.resolve(__dirname + '/../browser/logo.png'), - backgroundColor: options.color || bgColor, + backgroundColor: options.color || theme.backgroundColor, type: options.type, fullscreen: options.fullscreen, useContentSize: true, diff --git a/app/main/server.js b/app/main/server.js index 999238ff..08677c97 100644 --- a/app/main/server.js +++ b/app/main/server.js @@ -5,6 +5,7 @@ var express = require('express')(), Ipc = require('./ipc/server') ipc = new Ipc(server), settings = require('./settings'), + theme = require('./theme').init(), zeroconf = require('./zeroconf'), appAddresses = settings.read('appAddresses'), clients = {} @@ -17,7 +18,7 @@ express.get('*', function(req, res){ if (req.path.indexOf('theme.css') != -1) { res.set('Content-Type', 'text/css'); if (settings.read('theme')) { - res.send(new Buffer(settings.read('theme').join('\n'))) + res.send(new Buffer(theme.get())) } else { res.send('') } diff --git a/app/main/settings.js b/app/main/settings.js index dc1d18b4..8c82c59a 100644 --- a/app/main/settings.js +++ b/app/main/settings.js @@ -4,9 +4,7 @@ if (process.argv[1]&&process.argv[1].indexOf('-')==0) process.argv.unshift('') var baseDir = process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'], configPath = require('path').join(baseDir, '.open-stage-control'), fs = require('fs'), - ifaces = require('os').networkInterfaces(), - chokidar = require('chokidar'), - path = require('path') + ifaces = require('os').networkInterfaces() var options = { 's':{alias:'send',type:'array',describe:'default targets for all widgets (ip:port pairs)', @@ -107,58 +105,12 @@ var configFile = function(){try {return JSON.parse(fs.readFileSync(configPath,'u defaultConfig -var loadTheme = function(themes){ - if (!themes) return - var style = [] - - for (let i in themes) { - let file = themes[i] - if (!fs.existsSync(file)) { - file = path.resolve(__dirname + '/../browser/themes/' + themes[i] + '.css') - if (!fs.existsSync(file)) { - file = false - } - } - - if (file) { - - let updateStyle = (i)=>{ - try { - style[i] = fs.readFileSync(file,'utf-8') - } catch(err) { - throw err - } - } - let watchFile = ()=>{ - var watcher = chokidar.watch(file) - watcher.on('change',()=>{ - updateStyle(i) - require('./callbacks').reloadCss() - watcher.close() - watchFile() - }) - } - - watchFile() - updateStyle(i) - - - } else { - console.error('Theme "' + themes[i] + '" not found.') - } - - } - - return style -} - var makeDefaultConfig = function(argv){ defaultConfig = { argv:argv, presetPath : process.cwd(), sessionPath: process.cwd(), recentSessions: [], - appName: 'Open Stage Control', instanceName: argv['instance-name'] || false, targets: argv.s || argv.sync || false, @@ -190,7 +142,7 @@ var makeDefaultConfig = function(argv){ return appAddresses }(), examples: argv.e, - theme: loadTheme(argv.t) + theme: argv.t || [] } } diff --git a/app/main/theme.js b/app/main/theme.js new file mode 100644 index 00000000..33ce97e1 --- /dev/null +++ b/app/main/theme.js @@ -0,0 +1,76 @@ +var fs = require('fs'), + path = require('path'), + settings = require('./settings') + +class Theme { + + constructor() { + + this.themes = [] + this.files = [] + this.css = [] + + this.defaultColor = '#191f2a' + this.backgroundColor = this.defaultColor + + } + + init() { + + this.themes = settings.read('theme') + this.files = [] + + for (let theme of this.themes) { + + if (theme.includes('.css') && fs.existsSync(theme)) { + this.files.push(theme) + } else if (!theme.includes('.css') && fs.existsSync(path.resolve(__dirname + '/../browser/themes/' + theme + '.css'))) { + this.files.push(path.resolve(__dirname + '/../browser/themes/' + theme + '.css')) + } else { + console.error('Theme error: "' + themes + '" not found.') + } + } + + this.load() + + return this + + } + + load() { + + this.css = [] + + for (let i in this.files) { + + try { + this.css.push(fs.readFileSync(this.files[i],'utf-8')) + } catch(err) { + console.error('Theme error: could not load "' + this.files[i] + '".') + } + + } + + var css = this.get() + + if (css.includes('--color-bg:')) { + this.backgroundColor = css.match(/--color-bg:([^;]*);/)[1].trim() + } else { + this.backgroundColor = this.defaultColor + } + + return this + + } + + get() { + + return this.css.join('\n') + + } + +} + +var theme = new Theme() + +module.exports = theme