Skip to content

Commit

Permalink
Added autoswitching checkbox for apply on startup & resume + redux + …
Browse files Browse the repository at this point in the history
…types, improved notifications design.
  • Loading branch information
aredden committed Jan 11, 2021
1 parent 57b8628 commit be5792b
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 54 deletions.
8 changes: 7 additions & 1 deletion electron/src/AppUpdater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export default class AutoUpdater<Updater> {
checkForUpdate = async () => {
LOGGER.info('Checking for update.');
let { updater } = this.state;
if (!is_dev)
if (!is_dev) {
updater
.checkForUpdatesAndNotify({
title: 'G14ControlV2',
Expand All @@ -62,6 +62,7 @@ export default class AutoUpdater<Updater> {
LOGGER.info('Current update info: ' + JSON.stringify(r, null, 2));
}
});
}
};

getAllInfo = () => {
Expand All @@ -79,6 +80,11 @@ export default class AutoUpdater<Updater> {
return this.state.version;
};

downloadUpdate = () => {
let { updater } = this.state;
updater.downloadUpdate();
};

quitAndUpdate = () => {
LOGGER.info('Initializing "quit and update"');
let { updater } = this.state;
Expand Down
29 changes: 3 additions & 26 deletions electron/src/AutoPowerSwitching.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
/** @format */

import { getConfig } from './electron';
import { getConfig, showNotification } from './electron';
import getLogger from './Logger';
import { setG14ControlPlan } from './IPCEvents/G14ControlPlans';
import { Notification } from 'electron';
const LOGGER = getLogger('AutoPowerSwitching');

export type PowerSwitchArgs = {
Expand Down Expand Up @@ -34,10 +33,7 @@ export const initSwitch = async (state: 'battery' | 'ac') => {
if (!result) {
LOGGER.error(`Could not switch plan from AC to battery plan.`);
} else {
new Notification({
title: 'Power Switching',
body: 'Switched to plan:' + dcPlan.name,
}).show();
showNotification('Power Switching', 'Switched to plan:' + dcPlan.name);
}
} else if (acPlan) {
let ryz = acPlan.ryzenadj
Expand All @@ -54,26 +50,7 @@ export const initSwitch = async (state: 'battery' | 'ac') => {
if (!result) {
LOGGER.error(`Could not switch plan from battery to AC plan.`);
} else {
new Notification({
title: 'Power Switching',
body: 'Switched to plan:\n' + acPlan.name,
}).show();
showNotification('Power Switching', 'Switched to plan:' + acPlan.name);
}
}
};

// export const checkForAutoSwitching = async (discharge: number) => {
// if (discharge > 0 && powerDelivery === 'ac') {
// let pow = await whichCharger();
// if (pow && pow.dc) {
// powerDelivery = 'battery';
// initSwitch('battery');
// }
// } else if (discharge <= 1 && powerDelivery === 'battery') {
// let pow = await whichCharger();
// if (pow && (pow.ac || pow.usb)) {
// powerDelivery = 'ac';
// initSwitch('ac');
// }
// }
// };
23 changes: 13 additions & 10 deletions electron/src/StartupPlan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import { setG14ControlPlan } from './IPCEvents/G14ControlPlans';
import { whichCharger } from './IPCEvents/WMI/HardwareControl';
import { Notification } from 'electron';
import getLogger from './Logger';
import { showNotification } from './electron';

const LOGGER = getLogger('StartupPlan');

Expand Down Expand Up @@ -43,7 +43,7 @@ const applyPlan = async (plano: G14ControlPlan, config: G14Config) => {
if (result) {
return true;
} else {
LOGGER.error('Failed to set G14Control plan.');
LOGGER.error('Failed to set G14Control plan via startup autoswitch.');
return false;
}
} else {
Expand All @@ -57,26 +57,29 @@ const applyPlan = async (plano: G14ControlPlan, config: G14Config) => {
export const initStartupPlan = async (config: G14Config) => {
let { autoSwitch } = config;
if (autoSwitch && autoSwitch.enabled) {
LOGGER.info('Autoswitch is enabled. Will apply plan.');
let result = await whichCharger();
if (result) {
let { ac, dc, usb } = result;
if ((ac || usb) && autoSwitch.acPlan) {
let result = await applyPlan(autoSwitch.acPlan, config);
if (result) {
new Notification({
title: 'Auto Switching',
body: 'Switched to: ' + autoSwitch.acPlan.name,
}).show();
showNotification(
'Auto Switching',
'Switched to: ' + autoSwitch.acPlan.name
);
}
} else if (dc && autoSwitch.dcPlan) {
let result = await applyPlan(autoSwitch.dcPlan, config);
if (result) {
new Notification({
title: 'Auto Switching',
body: 'Switched to: ' + autoSwitch.dcPlan.name,
}).show();
showNotification(
'Auto Switching',
'Switched to: ' + autoSwitch.dcPlan.name
);
}
}
} else {
LOGGER.error('Current charger state could not be identified.');
}
}
};
25 changes: 17 additions & 8 deletions electron/src/electron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,7 @@ export const updateMenuVisible = (minimized?: boolean) => {

const args = process.argv;

// eslint-disable-next-line
const showNotification = (title: string, body: string) => {
export const showNotification = (title: string, body: string) => {
const notification: NotificationConstructorOptions = {
icon: ICONPATH,
title: title,
Expand Down Expand Up @@ -178,6 +177,10 @@ export async function createWindow(
} catch (err) {
LOGGER.error('Error loading config at startup');
}

// Set appid so notifications don't show entire app id.
app.setAppUserModelId('G14ControlV2');

// logic for start on boot minimized
let startWithFocus = true;
if (args.length > 1 && args[1] === 'hide') {
Expand Down Expand Up @@ -291,7 +294,12 @@ export async function createWindow(
}
}
browserWindow.setMenu(null);
initStartupPlan(g14Config);
if (g14Config.autoSwitch && g14Config.autoSwitch.applyOnBoot) {
setTimeout(() => {
initStartupPlan(getConfig());
}, 3000);
}

return { tray, browserWindow, g14Config, trayContext };
}

Expand Down Expand Up @@ -344,12 +352,13 @@ powerMonitor.on('shutdown', () => {
}
});

powerMonitor.on('unlock-screen', () => {
initStartupPlan(getConfig());
});

powerMonitor.on('resume', () => {
initStartupPlan(getConfig());
LOGGER.info('Resume: initializing startup plan.');
if (g14Config.autoSwitch && g14Config.autoSwitch.applyOnBoot) {
setTimeout(() => {
initStartupPlan(getConfig());
}, 3000);
}
});

powerMonitor.on('suspend', () => {
Expand Down
1 change: 1 addition & 0 deletions electron/src/g14controlv2-main.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ declare type G14Config = {
special?: boolean;
autoSwitch?: {
enabled: boolean;
applyOnBoot?: boolean;
acPlan: G14ControlPlan;
dcPlan: G14ControlPlan;
};
Expand Down
48 changes: 39 additions & 9 deletions src/Components/Content/G14Plans/AutoPowerSwitch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import {
setACAutoPowerSwitching,
setAutoSwitchingEnabled,
setDCAutoPowerSwitching,
setAutoSwitchOnBootResume,
store,
} from '../../../Store/ReduxStore';
import './AutoPowerSwitch.scss';
interface Props {}

interface State {
startBootResume: boolean;
allPlans: G14ControlPlan[];
acPlan: G14ControlPlan | undefined;
dcPlan: G14ControlPlan | undefined;
Expand All @@ -27,12 +29,14 @@ export default class AutoPowerSwitch extends Component<Props, State> {
let { autoSwitch } = curState;
let acPlan = undefined,
dcPlan = undefined,
enabled = false;
enabled = false,
startBootResume = false;

if (autoSwitch) {
acPlan = Object.assign({}, autoSwitch.acPlan);
dcPlan = Object.assign({}, autoSwitch.dcPlan);
enabled = autoSwitch.enabled ? autoSwitch.enabled : false;
startBootResume = !!autoSwitch.applyOnBoot;
}
let unsub = store.subscribe(this.onAddNewPlan);

Expand All @@ -42,6 +46,7 @@ export default class AutoPowerSwitch extends Component<Props, State> {
dcPlan,
enabled,
unsub,
startBootResume,
};
}

Expand Down Expand Up @@ -88,6 +93,13 @@ export default class AutoPowerSwitch extends Component<Props, State> {
});
};

chooseOnBoot = () => {
let { startBootResume } = this.state;
this.setState({ startBootResume: !startBootResume }, () => {
store.dispatch(setAutoSwitchOnBootResume(!startBootResume));
});
};

componentWillUnmount() {
this.state.unsub();
}
Expand All @@ -98,20 +110,38 @@ export default class AutoPowerSwitch extends Component<Props, State> {
return { value: val.name };
});

let { enabled, acPlan, dcPlan } = this.state;
let { enabled, acPlan, dcPlan, startBootResume } = this.state;

return (
<>
<Card title={'Auto Power Switching'}>
<Space direction="horizontal" className="powersw-container">
<div>
<label style={{ marginRight: '.5rem' }} htmlFor="checkAutoSwitch">
Enable Auto Power Switching
</label>
<Checkbox
onClick={this.chooseEnabled}
checked={enabled}
id="checkAutoSwitch"></Checkbox>
<Space direction="vertical">
<div>
<label
style={{ marginRight: '.5rem' }}
htmlFor="checkAutoSwitch">
Enable Auto Power Switching
</label>
<Checkbox
onClick={this.chooseEnabled}
checked={enabled}
id="checkAutoSwitch"></Checkbox>
</div>

<div>
<label
style={{ marginRight: '.5rem' }}
htmlFor="checkStartBootResume">
Apply on app startup & resume
</label>
<Checkbox
onClick={this.chooseOnBoot}
checked={startBootResume}
id="checkStartBootResume"></Checkbox>
</div>
</Space>
</div>

<Space direction="vertical" className="powersw-select-container">
Expand Down
16 changes: 16 additions & 0 deletions src/Store/ReduxStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,12 @@ export const removeRyzenadjPlan = createAction(
}
);

export const setAutoSwitchOnBootResume = createAction(
'SET_AUTOSWITCH_BOOT_RESUME',
(value: boolean) => {
return { payload: value };
}
);
const createRootReducer = (initialState: G14Config) => {
let reducer = createReducer(initialState, (reducer) => {
/**
Expand Down Expand Up @@ -372,6 +378,16 @@ const createRootReducer = (initialState: G14Config) => {
state.current.shortcuts = payload;
return state;
});

reducer.addCase(setAutoSwitchOnBootResume, (state, action) => {
let { autoSwitch } = state;
state.autoSwitch = autoSwitch
? Object.assign(state.autoSwitch, {
applyOnBoot: action.payload,
})
: { applyOnBoot: action.payload };
return state;
});
});
return reducer;
};
1 change: 1 addition & 0 deletions src/react-app-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ declare type G14Config = {
};
autoSwitch?: {
enabled?: boolean;
applyOnBoot?: boolean;
acPlan?: G14ControlPlan;
dcPlan?: G14ControlPlan;
};
Expand Down

0 comments on commit be5792b

Please sign in to comment.