diff --git a/app/modules/battery.js b/app/modules/battery.js index a1cb91e..dee476d 100644 --- a/app/modules/battery.js +++ b/app/modules/battery.js @@ -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 } \ No newline at end of file diff --git a/app/modules/helpers.js b/app/modules/helpers.js index 1f86593..9da04c8 100644 --- a/app/modules/helpers.js +++ b/app/modules/helpers.js @@ -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 } \ No newline at end of file diff --git a/app/modules/interface.js b/app/modules/interface.js index 8ab6f83..6ec6600 100644 --- a/app/modules/interface.js +++ b/app/modules/interface.js @@ -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` ) diff --git a/app/package.json b/app/package.json index 5c8fcd1..46ac0ce 100644 --- a/app/package.json +++ b/app/package.json @@ -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": {