1kc / razer-macos

Color effects manager for Razer devices for macOS. Supports High Sierra (10.13) to Monterey (12.0). Made by the community, based on openrazer.
GNU General Public License v2.0
2.34k stars 254 forks source link

Battery level on tray and charging dock #179

Open JCKodel opened 3 years ago

JCKodel commented 3 years ago

I did this:

image

(charging icon and battery % refreshed each 4 seconds in the tray).

I also did this:

image

(added an "static" option to show battery level on the dock (from red to green)).

I will not make a pull request because I'm not sure if the code is right and I also only did that for the dock. I don't like the battery indicator in the mouse itself and I don't have Razer keyboards.

I also don't know what would happen if there is no battery available (for wired devices), so, I can't make a decent PR.

Nevertheless, those are the changes:

I've created a new menu entry above the dock Static -> White entry here: https://github.com/1kc/razer-macos/blob/801745a893a97c7e2a641224381f903107fb5ddf/src/main/index.js#L1235

{
    label: 'Battery Level',
    click() {
      clearInterval(cycleColorsInterval);

      cycleColorsInterval = setInterval(
        () => {
          refreshDevices();
          refreshTray();

          var r, g, b = 0;
          var batteryLevel = addon.getBatteryLevel();

          if (batteryLevel < 50) {
            r = 255;
            g = Math.round(5.1 * batteryLevel);
          }
          else {
            g = 255;
            r = Math.round(510 - 5.10 * batteryLevel);
          }

          addon.mouseDockSetModeStatic(
            new Uint8Array([r, g, b])
          );
        },
        4000,
      )
    },
  },

Each 4 seconds (just copied the interval from elsewhere, don't know if means something), I refresh the devices and rebuild the tray, then I get a green to red scale depending on the battery level (I guess this is something like what Razer Windows do) and I set the color to that RGB, as a static color.

The tray itself is quite simple (but bugged, I guess bacause of Electron, more on that in a minute).

On https://github.com/1kc/razer-macos/blob/801745a893a97c7e2a641224381f903107fb5ddf/src/main/index.js#L1830:

if (mouseDeviceName) {
  if (mouseBatteryLevel == -1) {
    mouseMenu[1].label = mouseDeviceName;
  } else if (mouseCharging) {
    mouseMenu[1].label = mouseDeviceName.concat(
      ' - ⚡'.concat(mouseBatteryLevel.toString().concat('%'))
    );

    tray.setTitle('⚡'.concat(mouseBatteryLevel.toString().concat(' %')));
  } else {
    mouseMenu[1].label = mouseDeviceName.concat(
      ' - 🔋'.concat(mouseBatteryLevel.toString().concat('%'))
    );

    tray.setTitle('🔋'.concat(mouseBatteryLevel.toString().concat(' %')));
  }
  menu = menu.concat(mouseMenu);
}

What I did was to use the tray.setTitle() to write the battery level and charging indicator on the tray. The only issue is when you have two monitors, MacCrappyOS dimms the inactive monitor menu bar and the text is rendered grey (notice that my temperature indicator doesn't have this issue):

image

When the monitor is active, it shows as white (or black, because Big Sur app bar doesn't get dark theme properly):

Active monitor with dark menu bar: image

Active monitor with dark menu bar that MacCrappyOS think is light because of an not visible wallpaper: image

dhobi commented 3 years ago

Cool stuff! I like both, the option to see the battery level in the menu as well as setting a device to the battery level color (green to red).

jchobantonov commented 3 years ago

@JCKodel could you share .dmg installation file?

JCKodel commented 3 years ago

@JCKodel could you share .dmg installation file?

Sorry, I don't work with Macs anymore and apparently I didn't backup the whole thing (I searched here in my filed and couldn't find it) =(

JCKodel commented 1 year ago

@JCKodel could you share .dmg installation file?

@jchobantonov Available here: https://github.com/JCKodel/razer-macos/releases/tag/0.5.0