From 931285dc9207fa6da9fa3b11a1831f4cd615404b Mon Sep 17 00:00:00 2001 From: Daniel Hobi Date: Tue, 27 Apr 2021 16:29:55 +0200 Subject: [PATCH] - Battery as a feature (enabled by default for backwards compatibility) - Refresh tray before opening it (updated battery state #347) - Get rid of custom DPI settings Actually, we could move the most code / stuff from device into the feature classes which would allow to get rid of quite some if's in the device code. It would just be (again) quite some refactoring and testing (statemanager) needed. --- src/devices/abyssus_v2.json | 2 +- src/devices/mamba_elite.json | 2 +- src/main/application.js | 15 +++++-- src/main/device/razerdevice.js | 3 ++ src/main/device/razerdevicemouse.js | 41 +++++++++++++------ src/main/feature/featurebattery.js | 8 ++++ src/main/feature/featurehelper.js | 4 ++ src/main/feature/featureidentifier.js | 3 +- src/main/menu/menubuilderdevice.js | 4 +- src/main/razerdevicemanager.js | 12 +++--- .../sections/sectionsettingsensitivity.jsx | 4 +- 11 files changed, 72 insertions(+), 26 deletions(-) create mode 100644 src/main/feature/featurebattery.js diff --git a/src/devices/abyssus_v2.json b/src/devices/abyssus_v2.json index e4f45fd..014b8f0 100644 --- a/src/devices/abyssus_v2.json +++ b/src/devices/abyssus_v2.json @@ -3,7 +3,7 @@ "productId": "0x005B", "mainType": "mouse", "image": "https://assets.razerzone.com/eeimages/support/products/721/721_abyssusv2.png", - "featuresMissing": ["none", "waveSimple", "spectrum", "reactive", "breathe"], + "featuresMissing": ["none", "waveSimple", "spectrum", "reactive", "breathe", "battery"], "featuresConfig": [ { "static": { diff --git a/src/devices/mamba_elite.json b/src/devices/mamba_elite.json index f5cf5cf..c774f52 100644 --- a/src/devices/mamba_elite.json +++ b/src/devices/mamba_elite.json @@ -4,7 +4,7 @@ "mainType": "mouse", "image": "https://assets.razerzone.com/eeimages/support/products/1390/1390_mamba_elite.png", "features": null, - "featuresMissing": ["oldMouseEffects"], + "featuresMissing": ["oldMouseEffects", "battery"], "featuresConfig": [ { "dpi": { diff --git a/src/main/application.js b/src/main/application.js index afc245f..cab3e2a 100644 --- a/src/main/application.js +++ b/src/main/application.js @@ -116,10 +116,9 @@ export class Application { // mouse dpi rpc listener ipcMain.on('request-set-dpi', (_, arg) => { - const { device } = arg; + const { device, dpi } = arg; const currentDevice = this.razerApplication.deviceManager.getByInternalId(device.internalId); - currentDevice.setSettings(device.settings); - currentDevice.setDPI(currentDevice.settings.customSensitivity); + currentDevice.setDPI(dpi); this.refreshTray(); }); @@ -292,6 +291,16 @@ export class Application { // Template.png will be automatically inverted by electron: https://www.electronjs.org/docs/api/native-image#template-image this.tray = new Tray(path.join(__static, '/assets/iconTemplate.png')); this.tray.setToolTip('Razer macOS menu'); + this.tray.on('click', () => { + if(this.razerApplication.deviceManager.activeRazerDevices != null) { + this.razerApplication.deviceManager.activeRazerDevices.forEach(device => { + if (device !== null) { + device.refresh(); + } + }); + } + this.refreshTray(); + }); this.refreshTray(true); } diff --git a/src/main/device/razerdevice.js b/src/main/device/razerdevice.js index f9803cd..0adf2b3 100644 --- a/src/main/device/razerdevice.js +++ b/src/main/device/razerdevice.js @@ -38,6 +38,9 @@ export class RazerDevice { }; } + refresh() { + } + destroy() { this.addon = null; } diff --git a/src/main/device/razerdevicemouse.js b/src/main/device/razerdevicemouse.js index 0f88e50..45be8f5 100644 --- a/src/main/device/razerdevicemouse.js +++ b/src/main/device/razerdevicemouse.js @@ -7,11 +7,18 @@ export class RazerDeviceMouse extends RazerDevice { } async init() { - this.batteryLevel = this.addon.getBatteryLevel(this.internalId); - this.chargingStatus = this.addon.getChargingStatus(this.internalId); + if(this.hasFeature(FeatureIdentifier.BATTERY)) { + this.batteryLevel = this.addon.getBatteryLevel(this.internalId); + this.chargingStatus = this.addon.getChargingStatus(this.internalId); + } - this.dpi = this.addon.mouseGetDpi(this.internalId); - this.pollRate = this.addon.mouseGetPollRate(this.internalId); + if(this.hasFeature(FeatureIdentifier.MOUSE_DPI)) { + this.dpi = this.addon.mouseGetDpi(this.internalId); + } + + if(this.hasFeature(FeatureIdentifier.POLL_RATE)) { + this.pollRate = this.addon.mouseGetPollRate(this.internalId); + } const featureMouseBrightness = this.getFeature(FeatureIdentifier.MOUSE_BRIGHTNESS); @@ -36,17 +43,23 @@ export class RazerDeviceMouse extends RazerDevice { return super.init(); } - getDefaultSettings() { - return { - customSensitivity: this.getDPI(), - customColor1: this.defaultColorSettings, + refresh() { + super.refresh(); + if(this.hasFeature(FeatureIdentifier.BATTERY)) { + this.batteryLevel = this.addon.getBatteryLevel(this.internalId); + this.chargingStatus = this.addon.getChargingStatus(this.internalId); } } getState() { const deviceState = super.getState(); - deviceState['dpi'] = this.dpi; - deviceState['pollRate'] = this.pollRate; + if(this.hasFeature(FeatureIdentifier.MOUSE_DPI)) { + deviceState['dpi'] = this.dpi; + } + + if(this.hasFeature(FeatureIdentifier.POLL_RATE)) { + deviceState['pollRate'] = this.pollRate; + } const featureMouseBrightness = this.getFeature(FeatureIdentifier.MOUSE_BRIGHTNESS); if(typeof featureMouseBrightness !== 'undefined') { @@ -71,8 +84,12 @@ export class RazerDeviceMouse extends RazerDevice { resetToState(state) { super.resetToState(state); - this.setDPI(state.dpi); - this.setPollRate(state.pollRate); + if(this.hasFeature(FeatureIdentifier.MOUSE_DPI)) { + this.setDPI(state.dpi); + } + if(this.hasFeature(FeatureIdentifier.POLL_RATE)) { + this.setPollRate(state.pollRate); + } const featureMouseBrightness = this.getFeature(FeatureIdentifier.MOUSE_BRIGHTNESS); if(typeof featureMouseBrightness !== 'undefined') { diff --git a/src/main/feature/featurebattery.js b/src/main/feature/featurebattery.js new file mode 100644 index 0000000..5b7a22f --- /dev/null +++ b/src/main/feature/featurebattery.js @@ -0,0 +1,8 @@ +import { Feature } from './feature'; +import { FeatureIdentifier } from './featureidentifier'; + +export class FeatureBattery extends Feature { + constructor(config) { + super(FeatureIdentifier.BATTERY, config); + } +} \ No newline at end of file diff --git a/src/main/feature/featurehelper.js b/src/main/feature/featurehelper.js index 522cb83..05cb71d 100644 --- a/src/main/feature/featurehelper.js +++ b/src/main/feature/featurehelper.js @@ -15,6 +15,7 @@ import { FeatureMousePollRate } from './featuremousepollrate'; import { FeatureMouseDPI } from './featuremousedpi'; import { RazerDeviceType } from '../device/razerdevicetype'; import { FeatureIdentifier } from './featureidentifier'; +import { FeatureBattery } from './featurebattery'; export class FeatureHelper { @@ -37,6 +38,7 @@ export class FeatureHelper { case FeatureIdentifier.MOUSE_BRIGHTNESS: return new FeatureMouseBrightness(configuration); case FeatureIdentifier.POLL_RATE: return new FeatureMousePollRate(configuration); case FeatureIdentifier.MOUSE_DPI: return new FeatureMouseDPI(configuration); + case FeatureIdentifier.BATTERY: return new FeatureBattery(configuration); default: throw featureIdentifier+' is not a valid feature identifier!' } @@ -69,6 +71,7 @@ export class FeatureHelper { new FeatureMouseBrightness(), new FeatureMousePollRate(), new FeatureMouseDPI(), + new FeatureBattery(), ]; case RazerDeviceType.MOUSEDOCK: return [ @@ -76,6 +79,7 @@ export class FeatureHelper { new FeatureStatic(), new FeatureSpectrum(), new FeatureBreathe(), + new FeatureBattery(), ]; case RazerDeviceType.MOUSEMAT: return [ diff --git a/src/main/feature/featureidentifier.js b/src/main/feature/featureidentifier.js index 20eb91b..ba8c930 100644 --- a/src/main/feature/featureidentifier.js +++ b/src/main/feature/featureidentifier.js @@ -14,4 +14,5 @@ FeatureIdentifier.WHEEL = 'wheel'; FeatureIdentifier.OLD_MOUSE_EFFECTS = 'oldMouseEffects'; FeatureIdentifier.MOUSE_BRIGHTNESS = 'mouseBrightness'; FeatureIdentifier.POLL_RATE = 'pollRate'; -FeatureIdentifier.MOUSE_DPI = 'dpi'; \ No newline at end of file +FeatureIdentifier.MOUSE_DPI = 'dpi'; +FeatureIdentifier.BATTERY = 'battery'; \ No newline at end of file diff --git a/src/main/menu/menubuilderdevice.js b/src/main/menu/menubuilderdevice.js index dce1241..a5af038 100644 --- a/src/main/menu/menubuilderdevice.js +++ b/src/main/menu/menubuilderdevice.js @@ -21,7 +21,7 @@ function getHeaderFor(application, razerDevice) { case RazerDeviceType.KEYBOARD: break; case RazerDeviceType.MOUSE: - if (razerDevice.batteryLevel !== -1) { + if (razerDevice.hasFeature(FeatureIdentifier.BATTERY) && razerDevice.batteryLevel !== -1) { if (razerDevice.chargingStatus) { label = label + ' - ⚡' + razerDevice.batteryLevel.toString() + '%'; } else { @@ -85,6 +85,8 @@ function getFeatureMenuFor(application, device, feature) { return null; case FeatureIdentifier.MOUSE_DPI: return null; + case FeatureIdentifier.BATTERY: + return null; default: throw 'Unmapped feature for identifier ' + feature.featureIdentifier + ' detected.'; } diff --git a/src/main/razerdevicemanager.js b/src/main/razerdevicemanager.js index 2f491a0..361bfb1 100644 --- a/src/main/razerdevicemanager.js +++ b/src/main/razerdevicemanager.js @@ -178,11 +178,13 @@ export class RazerDeviceManager { } destroy() { - this.activeRazerDevices.forEach(device => { - if (device !== null) { - device.destroy(); - } - }); + if(this.activeRazerDevices != null) { + this.activeRazerDevices.forEach(device => { + if (device !== null) { + device.destroy(); + } + }); + } this.closeDevices(); this.addon = null; } diff --git a/src/renderer/sections/sectionsettingsensitivity.jsx b/src/renderer/sections/sectionsettingsensitivity.jsx index b7e6156..ed86dc1 100644 --- a/src/renderer/sections/sectionsettingsensitivity.jsx +++ b/src/renderer/sections/sectionsettingsensitivity.jsx @@ -16,9 +16,9 @@ export class SectionSettingSensitivity extends SectionSettingBlock { } handleClick(currentDpi) { - this.deviceSelected.settings.customSensitivity = currentDpi; let payload = { - device: this.deviceSelected + device: this.deviceSelected, + dpi: currentDpi }; ipcRenderer.send('request-set-dpi', payload); };