From 803560ee2cf5d25fbbf354ec2ff0e14fdf327857 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ce=CC=81dric?= Date: Tue, 5 Feb 2019 10:04:26 +0100 Subject: [PATCH] Various fixes --- src/CCAcontroller.js | 61 ++++++++++++------------------------- src/controllers/picker.js | 1 - src/views/js/deficiency.js | 24 ++++----------- src/views/js/main.js | 40 +++++++++--------------- src/views/js/preferences.js | 2 +- 5 files changed, 42 insertions(+), 86 deletions(-) diff --git a/src/CCAcontroller.js b/src/CCAcontroller.js index b1643f3..765033c 100644 --- a/src/CCAcontroller.js +++ b/src/CCAcontroller.js @@ -15,18 +15,17 @@ class CCAController { this.updateDeficiency('foreground') this.updateDeficiency('background') this.updateContrastRatio() - this.sendEventToAll('foregroundColorChanged') - this.sendEventToAll('backgroundColorChanged') + this.sendEventToAll('colorChanged', 'foreground') + this.sendEventToAll('colorChanged', 'background') }) - ipcMain.on('changeRGBComponent', this.updateRGBComponent.bind(this)) - ipcMain.on('changeHSLComponent', this.updateHSLComponent.bind(this)) - ipcMain.on('changeForeground', this.updateFromString.bind(this)) - ipcMain.on('changeBackground', this.updateFromString.bind(this)) + ipcMain.on('changeFromRGBComponent', this.updateRGBComponent.bind(this)) + ipcMain.on('changeFromHSLComponent', this.updateHSLComponent.bind(this)) + ipcMain.on('changeFromString', this.updateFromString.bind(this)) ipcMain.on('switchColors', this.switchColors.bind(this)) ipcMain.on('setPreference', this.setPreference.bind(this)) } - updateRGBComponent(event, group, component, value, synced = false) { + updateRGBComponent(event, section, component, value, synced = false) { if (component === 'alpha') { value = parseFloat(value) if (value > 1) value = 1 @@ -37,12 +36,7 @@ class CCAController { if (value < 0) value = 0 } - let color - if (group === "foreground") { - color = this.sharedObject.deficiencies.normal.foregroundColor - } else if (group === "background") { - color = this.sharedObject.deficiencies.normal.backgroundColor - } + let color = this.sharedObject.deficiencies.normal[section + 'Color'] let dist if (synced && component !== "alpha") { @@ -87,16 +81,11 @@ class CCAController { } } - if (group === "foreground") { - this.sharedObject.deficiencies.normal.foregroundColor = color - this.updateGlobalF() - } else if (group === "background") { - this.sharedObject.deficiencies.normal.backgroundColor = color - this.updateGlobalB() - } + this.sharedObject.deficiencies.normal[section + 'Color'] = color + this.updateGlobal(section) } - updateHSLComponent(event, group, component, value) { + updateHSLComponent(event, section, component, value) { if (component === 'alpha') { value = parseFloat(value) if (value > 1) value = 1 @@ -111,12 +100,7 @@ class CCAController { if (value < 0) value = 0 } - let color - if (group === "foreground") { - color = this.sharedObject.deficiencies.normal.foregroundColor - } else if (group === "background") { - color = this.sharedObject.deficiencies.normal.backgroundColor - } + let color = this.sharedObject.deficiencies.normal[section + 'Color'] if (component === "hue") { color = color.hue(value) @@ -128,13 +112,8 @@ class CCAController { color = color.alpha(value) } - if (group === "foreground") { - this.sharedObject.deficiencies.normal.foregroundColor = color - this.updateGlobalF() - } else if (group === "background") { - this.sharedObject.deficiencies.normal.backgroundColor = color - this.updateGlobalB() - } + this.sharedObject.deficiencies.normal[section + 'Color'] = color + this.updateGlobal(section) } updateFromString(event, section, stringColor) { @@ -155,9 +134,9 @@ class CCAController { } this.updateContrastRatio() if (section == 'background' && this.sharedObject.deficiencies.normal.foregroundColor.alpha() !== 1) { // Then mixed has changed - this.sendEventToAll('foregroundColorChanged') + this.sendEventToAll('colorChanged', 'foreground') } - this.sendEventToAll(section + 'ColorChanged') + this.sendEventToAll('colorChanged', section) } switchColors(event) { @@ -168,8 +147,8 @@ class CCAController { this.updateDeficiencyForeground() this.updateDeficiencyBackground() this.updateContrastRatio() - this.sendEventToAll('foregroundColorChanged') - this.sendEventToAll('backgroundColorChanged') + this.sendEventToAll('colorChanged', 'foreground') + this.sendEventToAll('colorChanged', 'background') } updateDeficiency(section) { @@ -218,12 +197,12 @@ class CCAController { this.sendEventToAll('contrastRatioChanged') } - sendEventToAll(event, params) { + sendEventToAll(event, ...params) { const browsers = this.browsers Object.keys(browsers).map(function(key, index) { const browser = browsers[key] if (browser.getWindow()) { - browser.getWindow().webContents.send(event, params) + browser.getWindow().webContents.send(event, ...params) } }); } @@ -263,7 +242,7 @@ The contrast ratio is: ${normal.contrastRatioString} clipboard.writeText(text) } - setPreference(event, section, level, sublevel, value) { + setPreference(event, value, section, level, sublevel) { var option if (sublevel) { this.sharedObject.preferences[section][level][sublevel] = value diff --git a/src/controllers/picker.js b/src/controllers/picker.js index e04a990..6342322 100644 --- a/src/controllers/picker.js +++ b/src/controllers/picker.js @@ -23,7 +23,6 @@ module.exports = (browsers, mainController) => { ipcMain.on('showPicker', (event, section) => { global['currentPicker'] = section - mainController.sendEventToAll('pickerToggelled', global['currentPicker'], true) picker.init() }) diff --git a/src/views/js/deficiency.js b/src/views/js/deficiency.js index 1c97b0d..93f55af 100644 --- a/src/views/js/deficiency.js +++ b/src/views/js/deficiency.js @@ -4,35 +4,23 @@ const sharedObject = require('electron').remote.getGlobal('sharedObject') document.addEventListener('DOMContentLoaded', () => ipcRenderer.send('init-deficiency'), false) ipcRenderer.on('init', event => { - applyForegroundColor() - applyBackgroundColor() + applyColor('foreground') + applyColor('background') applyContrastRatio() }) -ipcRenderer.on('foregroundColorChanged', event => { - applyForegroundColor() -}) - -ipcRenderer.on('backgroundColorChanged', event => { - applyBackgroundColor() +ipcRenderer.on('colorChanged', (event, section) => { + applyColor(section) }) ipcRenderer.on('contrastRatioChanged', event => { applyContrastRatio() }) -function applyForegroundColor () { - Object.keys(sharedObject.deficiencies).forEach(function(key, index) { - if (key !== 'normal') { - document.getElementById('deficiency-' + key + '-preview').style.color = this[key].foregroundColor.rgb().string() - } - }, sharedObject.deficiencies) -} - -function applyBackgroundColor () { +function applyColor (section) { Object.keys(sharedObject.deficiencies).forEach(function(key, index) { if (key !== 'normal') { - document.getElementById('deficiency-' + key + '-preview').style.background = this[key].backgroundColor.rgb().string() + document.getElementById('deficiency-' + key + '-preview').style.color = this[key][section + 'Color'].rgb().string() } }, sharedObject.deficiencies) } diff --git a/src/views/js/main.js b/src/views/js/main.js index fa1d43a..4ffe8db 100644 --- a/src/views/js/main.js +++ b/src/views/js/main.js @@ -25,12 +25,8 @@ ipcRenderer.on('init', event => { initEvents() }) -ipcRenderer.on('foregroundColorChanged', event => { - applyColor('foreground') -}) - -ipcRenderer.on('backgroundColorChanged', event => { - applyColor('background') +ipcRenderer.on('colorChanged', (event, section) => { + applyColor(section) }) ipcRenderer.on('contrastRatioChanged', event => { @@ -38,7 +34,6 @@ ipcRenderer.on('contrastRatioChanged', event => { }) ipcRenderer.on('pickerToggelled', (event, section, state) => { - console.log(section, state) document.querySelector('#' + section + '-color .picker').setAttribute('aria-pressed', state) }) @@ -97,21 +92,21 @@ function initEvents () { }); } -function sliderRGBOnInput(group, component, value) { - let sync = document.querySelector('#' + group + '-rgb .sync input[type=checkbox]').checked - ipcRenderer.send('changeRGBComponent', group, component, value, sync) +function sliderRGBOnInput(section, component, value) { + let sync = document.querySelector('#' + section + '-rgb .sync input[type=checkbox]').checked + ipcRenderer.send('changeFromRGBComponent', section, component, value, sync) } -function numberRGBOnInput(group, component, value) { - ipcRenderer.send('changeRGBComponent', group, component, value) +function numberRGBOnInput(section, component, value) { + ipcRenderer.send('changeFromRGBComponent', section, component, value) } -function sliderHSLOnInput(group, component, value) { - ipcRenderer.send('changeHSLComponent', group, component, value) +function sliderHSLOnInput(section, component, value) { + ipcRenderer.send('changeFromHSLComponent', section, component, value) } -function numberHSLOnInput(group, component, value) { - ipcRenderer.send('changeHSLComponent', group, component, value) +function numberHSLOnInput(section, component, value) { + ipcRenderer.send('changeFromHSLComponent', section, component, value) } function showHide(el) { @@ -128,12 +123,11 @@ function showHide(el) { } function applyColor(section) { - let color, colorReal + let colorReal + let color = sharedObject.deficiencies.normal[section + "Color"] if (section == 'foreground') { - color = sharedObject.deficiencies.normal.foregroundColor colorReal = sharedObject.deficiencies.normal.foregroundColorMixed } else { - color = sharedObject.deficiencies.normal.backgroundColor colorReal = color } @@ -306,11 +300,7 @@ function displayValidate(section, format, string) { freeFormat.setAttribute('aria-live', 'polite') } if (format) { - if (section == 'foreground') { - ipcRenderer.send('changeForeground', string, format) - } else { - ipcRenderer.send('changeBackground', string, format) - } + ipcRenderer.send('changeFromString', section, string, format) input.setAttribute('aria-invalid', false) formatSelector.value = format.toLowerCase() formatSelector.style.display = "block" @@ -334,7 +324,7 @@ function leaveText(section, el) { function changeFormat(section, el) { let colorReal // We send the selected format to the controller for saving and sharedObject update - ipcRenderer.send('setPreference', section, 'format', el.value) + ipcRenderer.send('setPreference', el.value, section, 'format') // Then we update the current text field if (section == 'foreground') { colorReal = sharedObject.deficiencies.normal.foregroundColorMixed diff --git a/src/views/js/preferences.js b/src/views/js/preferences.js index 2da364b..43f98a5 100644 --- a/src/views/js/preferences.js +++ b/src/views/js/preferences.js @@ -11,7 +11,7 @@ ipcRenderer.on('init', event => { /* Tab Options */ document.getElementById('save').addEventListener('click', function () { var rounding = document.getElementById('option-rounding').value - ipcRenderer.send('setPreference', 'main', 'rounding', null, rounding) + ipcRenderer.send('setPreference', rounding, 'main', 'rounding') close() })