main: handle theme from a dedicated module

This commit is contained in:
jean-emmanuel 2018-03-04 10:47:14 +01:00
parent ac5a90b4e1
commit 9e78279226
5 changed files with 85 additions and 58 deletions

View file

@ -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')
},

View file

@ -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,

View file

@ -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('')
}

View file

@ -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 || []
}
}

76
app/main/theme.js Normal file
View file

@ -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