Add uninstall button to GUI

This commit is contained in:
Mentor
2023-03-10 16:58:35 +01:00
parent 5f00cb5811
commit b3e268e984
4 changed files with 41 additions and 6 deletions

View File

@@ -1,9 +1,9 @@
// Command line interactors
const { app } = require( 'electron' )
const { exec } = require( 'node:child_process' )
const { log, alert, wait } = require( './helpers' )
const { log, alert, wait, confirm } = require( './helpers' )
const { USER } = process.env
const path_fix = 'PATH=$PATH:/bin:/usr/bin:/usr/local/bin:/usr/sbin:/opt/homebrew:/usr/bin/'
const path_fix = 'PATH=/bin:/usr/bin:/usr/local/bin:/usr/sbin:/opt/homebrew'
const battery = `${ path_fix } battery`
const shell_options = {
shell: '/bin/bash',
@@ -177,6 +177,22 @@ const initialize_battery = async () => {
}
const uninstall_battery = async () => {
try {
const confirmed = await confirm( `Are you sure you want to uninstall Battery?` )
if( !confirmed ) return false
await exec_sudo_async( `${ path_fix } sudo battery uninstall silent` )
await alert( `Battery is now uninstalled!` )
return true
} catch ( e ) {
log( 'Error uninstalling battery: ', e )
alert( `Error uninstalling battery: ${ e.message }` )
return false
}
}
const is_limiter_enabled = async () => {
@@ -197,5 +213,6 @@ module.exports = {
disable_battery_limiter,
initialize_battery,
is_limiter_enabled,
get_battery_status
get_battery_status,
uninstall_battery
}

View File

@@ -4,6 +4,10 @@ let has_alerted_user_no_home = false
const { dialog } = require( 'electron' )
const alert = ( message ) => dialog.showMessageBox( { message } )
const confirm = ( message ) => dialog.showMessageBox( { message, buttons: [ "Confirm", "Cancel" ] } ).then( ( { response } ) => {
if( response == 0 ) return true
return false
} )
const wait = time_in_ms => new Promise( resolve => {
setTimeout( resolve, time_in_ms )
} )
@@ -30,5 +34,6 @@ const log = async ( ...messages ) => {
module.exports = {
log,
alert,
wait
wait,
confirm
}

View File

@@ -1,5 +1,5 @@
const { shell, app, Tray, Menu, powerMonitor, nativeTheme } = require( 'electron' )
const { enable_battery_limiter, disable_battery_limiter, initialize_battery, is_limiter_enabled, get_battery_status } = require( './battery' )
const { enable_battery_limiter, disable_battery_limiter, initialize_battery, is_limiter_enabled, get_battery_status, uninstall_battery } = require( './battery' )
const { log } = require( "./helpers" )
const { get_logo_template } = require( './theme' )
@@ -58,6 +58,19 @@ const generate_app_menu = async () => {
label: `Check for updates`,
click: () => shell.openExternal( `https://github.com/actuallymentor/battery/releases` )
},
{
type: 'normal',
label: `Uninstall Battery ${ app.getVersion() }`,
click: async () => {
const uninstalled = await uninstall_battery()
if( !uninstalled ) return
tray.destroy()
app.quit()
}
},
{
type: 'separator'
},
{
label: `User manual`,
click: () => shell.openExternal( `https://github.com/actuallymentor/battery#readme` )

View File

@@ -1,6 +1,6 @@
{
"name": "battery",
"version": "1.1.2",
"version": "1.1.3",
"description": "A battery charge limiter for Apple silicon Mac devices",
"main": "main.js",
"build": {