From dd3fba20527df758cb071d31150c1cf2b3c77eef Mon Sep 17 00:00:00 2001 From: "3m5.Danny Spina" Date: Thu, 5 Mar 2020 16:46:07 +0100 Subject: [PATCH 1/2] Fix precision of battery level --- package.json | 2 +- src/index.html | 5 ++--- src/index.js | 2 +- src/main.js | 27 +++++++++++---------------- 4 files changed, 15 insertions(+), 21 deletions(-) diff --git a/package.json b/package.json index a6be641..f0d5134 100755 --- a/package.json +++ b/package.json @@ -16,7 +16,7 @@ }, "dependencies": { "auto-launch": "^5", - "battery-level": "^3", + "child_process": "^1.0.2", "dark-mode": "^3.0.0", "electron-settings": "^3" } diff --git a/src/index.html b/src/index.html index 760ecd8..825cab1 100755 --- a/src/index.html +++ b/src/index.html @@ -3,7 +3,7 @@ - Weather + Volta - \ No newline at end of file + diff --git a/src/index.js b/src/index.js index f6199fc..4d34498 100755 --- a/src/index.js +++ b/src/index.js @@ -66,5 +66,5 @@ ipcRenderer.on('launch', (event, checked) => { }) ipcRenderer.on('battery', (event, value) => { - document.getElementById('currentBattery').innerText = `${value}%` + document.getElementById('currentBattery').innerText = `${value}` }) diff --git a/src/main.js b/src/main.js index 0dcfabb..b4c6a47 100755 --- a/src/main.js +++ b/src/main.js @@ -12,6 +12,7 @@ const settings = require('electron-settings') const path = require('path') const AutoLaunch = require('auto-launch') const batteryLevel = require('battery-level') +const exec = require('child_process').exec const al = new AutoLaunch({ name: 'Volta' @@ -148,21 +149,15 @@ function sendMax() { }).show() } +let level = 'checking...'; setInterval(() => { - batteryLevel().then(level => { - level = parseInt(level * 100) - - if (level === lastBattery) return - if (level > lastBattery) charging = true - else charging = false - lastBattery = level - - const limits = settings.get('values', defaultValues) - - if (level <= limits.min) sendMin() - else if (level >= limits.max) sendMax() - else numMax = numMin = 0 - - window.webContents.send('battery', level) - }).catch(() => console.log('Could not get Battery level')) + exec('pmset -g batt | egrep "([0-9]+\%)" -o', function(err, stdout, stderr) { + if (err) { + console.log(error.stack) + console.log('Error code: ' + error.code) + console.log('Signal received: ' + error.signal) + } + level = String(stdout) + }) + window.webContents.send('battery', level) }, 3000) From 24ec6dfd1a6a4fb0ce6394b3ad0ad41be1593400 Mon Sep 17 00:00:00 2001 From: "3m5.Danny Spina" Date: Fri, 6 Mar 2020 23:01:39 +0100 Subject: [PATCH 2/2] - Fix accuracy battery level (right way now) - Fix bug multiple desktops --- src/index.css | 4 +--- src/index.js | 2 +- src/main.js | 16 +++++++++++++--- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/index.css b/src/index.css index 2861382..01495cf 100755 --- a/src/index.css +++ b/src/index.css @@ -23,10 +23,10 @@ body, #app { color: #3B252C; font-family: 'Source Sans Pro', sans-serif; - height: 100%; width: 100%; padding: 0; margin: 0; + border-radius: 8px; } #app.dark { @@ -54,9 +54,7 @@ hr { #app .link { color: #3F8EFC; - /* color: inherit; */ font-size: 1em; - /* font-weight: normal; */ cursor: pointer; } diff --git a/src/index.js b/src/index.js index 4d34498..f6199fc 100755 --- a/src/index.js +++ b/src/index.js @@ -66,5 +66,5 @@ ipcRenderer.on('launch', (event, checked) => { }) ipcRenderer.on('battery', (event, value) => { - document.getElementById('currentBattery').innerText = `${value}` + document.getElementById('currentBattery').innerText = `${value}%` }) diff --git a/src/main.js b/src/main.js index b4c6a47..b5ccb04 100755 --- a/src/main.js +++ b/src/main.js @@ -11,7 +11,6 @@ const { const settings = require('electron-settings') const path = require('path') const AutoLaunch = require('auto-launch') -const batteryLevel = require('battery-level') const exec = require('child_process').exec const al = new AutoLaunch({ @@ -95,6 +94,7 @@ const createWindow = () => { } }) window.loadURL(`file://${path.join(__dirname, 'index.html')}`) + window.setVisibleOnAllWorkspaces(true) window.on('blur', () => { if (!window.webContents.isDevToolsOpened()) window.hide() @@ -149,7 +149,7 @@ function sendMax() { }).show() } -let level = 'checking...'; +let level = '--'; setInterval(() => { exec('pmset -g batt | egrep "([0-9]+\%)" -o', function(err, stdout, stderr) { if (err) { @@ -157,7 +157,17 @@ setInterval(() => { console.log('Error code: ' + error.code) console.log('Signal received: ' + error.signal) } - level = String(stdout) + level = parseInt(stdout) }) + if (level > lastBattery) charging = true + else charging = false + lastBattery = level + + const limits = settings.get('values', defaultValues) + + if (level <= limits.min) sendMin() + else if (level >= limits.max) sendMax() + else numMax = numMin = 0 + window.webContents.send('battery', level) }, 3000)