Skip to content

Commit

Permalink
Various fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Cédric committed Feb 5, 2019
1 parent a65001d commit 803560e
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 86 deletions.
61 changes: 20 additions & 41 deletions src/CCAcontroller.js
Expand Up @@ -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
Expand All @@ -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") {
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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)
}
});
}
Expand Down Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion src/controllers/picker.js
Expand Up @@ -23,7 +23,6 @@ module.exports = (browsers, mainController) => {

ipcMain.on('showPicker', (event, section) => {
global['currentPicker'] = section
mainController.sendEventToAll('pickerToggelled', global['currentPicker'], true)
picker.init()
})

Expand Down
24 changes: 6 additions & 18 deletions src/views/js/deficiency.js
Expand Up @@ -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)
}
Expand Down
40 changes: 15 additions & 25 deletions src/views/js/main.js
Expand Up @@ -25,20 +25,15 @@ 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 => {
applyContrastRatio()
})

ipcRenderer.on('pickerToggelled', (event, section, state) => {
console.log(section, state)
document.querySelector('#' + section + '-color .picker').setAttribute('aria-pressed', state)
})

Expand Down Expand Up @@ -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) {
Expand All @@ -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
}

Expand Down Expand Up @@ -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"
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/views/js/preferences.js
Expand Up @@ -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()
})

Expand Down

0 comments on commit 803560e

Please sign in to comment.