Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into m1-fix2
Browse files Browse the repository at this point in the history
  • Loading branch information
synle committed Jul 3, 2022
2 parents ac487ec + 4644cdf commit 3c33c31
Show file tree
Hide file tree
Showing 25 changed files with 277 additions and 204 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ The challenge of work from home in the last 2 years with 2 young toddlers is tha

![image](https://user-images.githubusercontent.com/3792401/168950471-15589781-ee7f-442f-afa4-58c53e136abb.png)


### Mac OSX Monterey

![demo-mac](https://user-images.githubusercontent.com/3792401/159141171-c6c8a6a5-4b7b-4fc6-af28-c082fc1bd723.gif)
Expand All @@ -61,7 +60,6 @@ By default, we will give each display a name. You can rename the display to some

![image](https://user-images.githubusercontent.com/3792401/168950610-576cc751-14fa-4a6d-82bf-57874cafec04.png)


### Toggling Dark Mode and Light Mode

The toggle for dark and light mode is located at the bottom of the control, you can choose either dark mode or light mode. This change will update the system dark mode accordingly. So it's best to keep all your apps aware of the dark mode. So this way it will change the dark mode according to the app.
Expand Down Expand Up @@ -235,6 +233,7 @@ This app has limited support for M1 Mac. Volume settings and individual display
![image](https://user-images.githubusercontent.com/3792401/177044893-8d3fd19e-4fbf-4557-9048-c4a0d6fd8b2f.png)

This requires preferences JSON to be updated (`~/Library/Application Support/display-dj/preferences.json`)

```js
{
// ...
Expand Down
5 changes: 5 additions & 0 deletions dist.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ async function doDistWork() {
path.join(__dirname, `dist/display-dj-darwin-x64/display-dj.app/Contents/Resources`, `ddcctl`)
);

fs.copyFileSync(
path.join(__dirname, `src/binaries/darwin_m1ddc`),
path.join(__dirname, `dist/display-dj-darwin-x64/display-dj.app/Contents/Resources`, `m1ddc`)
);

fs.copyFileSync(
path.join(__dirname, `src/binaries/darwin_brightness`),
path.join(__dirname, `dist/display-dj-darwin-x64/display-dj.app/Contents/Resources`, `brightness`)
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "display-dj",
"private": true,
"version": "1.11.4",
"version": "1.11.6",
"description": "A cross platform desktop application that supports brightness adjustment for integrated laptop monitor as well as external monitors and dark mode toggle supporting Windows and MacOSX at the moment.",
"scripts": {
"clean-dist": "rimraf dist",
Expand All @@ -20,7 +20,7 @@
"fix-import": "npx import-fixer --groupImport --aggressive --transformRelativeImport --importQuote=single",
"preformat": "npm run fix-import",
"format": "concurrently \"npm run format-js\" \"npm run format-html\"",
"format-js": "npx prettier --config ./.prettierrc --no-error-on-unmatched-pattern --write **/**/**/**/**/*.{ts,tsx,js,jsx,scss,yml,html,json,md}",
"format-js": "npx prettier --config ./.prettierrc --no-error-on-unmatched-pattern --write src/**/**/**/**/**/*.{ts,tsx,js,jsx,scss,yml,html,json,md} **/*.{ts,tsx,js,jsx,scss,yml,html,json,md}",
"format-html": "npx prettier --config ./.prettierrc --parser html --no-error-on-unmatched-pattern --write **/**/**/**/**/*.svg",
"test-ci": "cross-env CI=true npm test",
"test": "jest src",
Expand Down
42 changes: 20 additions & 22 deletions src/binaries/win32_ddcci.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,25 @@ let ddcci;
const path = require('path');
const { spawn, exec } = require('child_process');

process.on('message', async function(msg) {
process.on('message', async function (msg) {
try {
// first attempt to find ddcci as part of the dev mode (pure third party require)
ddcci = require("@hensm/ddcci");
if(!ddcci){
throw 'Cannot find local node module for ddcci'
ddcci = require('@hensm/ddcci');
if (!ddcci) {
throw 'Cannot find local node module for ddcci';
}
}
catch(err){
} catch (err) {
// otherwise, will look into it from the msg[0] = process['resourcesPath']
ddcci = require(path.join(msg[0], "node_modules/@hensm/ddcci"));
ddcci = require(path.join(msg[0], 'node_modules/@hensm/ddcci'));
}

const command = msg[1];
const targetMonitorId = msg[2];
const newBrightness = parseInt(msg[3]);

try{
try {
let res;
switch(command){
switch (command) {
case 'setBrightness':
res = await _setBrightness(targetMonitorId, newBrightness);
break;
Expand Down Expand Up @@ -63,39 +62,38 @@ process.on('message', async function(msg) {
break;

default:
throw `Not supported command - ${command}`
throw `Not supported command - ${command}`;
break;
}
process.send({success: true, command, data: res});
} catch(error){
process.send({success: false, command, error: error.toString()});
process.send({ success: true, command, data: res });
} catch (error) {
process.send({ success: false, command, error: error.toString() });
}
process.exit();
});

function _setBrightness(targetMonitorId, newBrightness){
if(isNaN(newBrightness) || newBrightness < 0 || newBrightness > 100){
throw 'newBrightness needs to be a number between 0 and 100'
function _setBrightness(targetMonitorId, newBrightness) {
if (isNaN(newBrightness) || newBrightness < 0 || newBrightness > 100) {
throw 'newBrightness needs to be a number between 0 and 100';
}

for (const monitorId of ddcci.getMonitorList()) {
if(monitorId === targetMonitorId){
try{
if (monitorId === targetMonitorId) {
try {
return ddcci.setBrightness(monitorId, newBrightness);
} catch(err){
} catch (err) {
throw err;
}

}
}

throw `targetMonitorId (${targetMonitorId}) not found`;
}

function _getBrightness(targetMonitorId){
function _getBrightness(targetMonitorId) {
return ddcci.getBrightness(targetMonitorId);
}

function _getMonitorList(){
function _getMonitorList() {
return ddcci.getMonitorList();
}
83 changes: 51 additions & 32 deletions src/main/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
// @ts-nocheck
import AutoLaunch from 'auto-launch';
import { app, BrowserWindow, dialog, globalShortcut, ipcMain, Menu, nativeTheme, shell, Tray } from 'electron';
import {
app,
BrowserWindow,
dialog,
globalShortcut,
ipcMain,
Menu,
nativeTheme,
shell,
Tray,
} from 'electron';
import { EventEmitter } from 'events';
import path from 'path';
import { matchPath } from 'react-router-dom';
Expand All @@ -11,7 +21,11 @@ import PositionUtils from 'src/main/utils/PositionUtils';
import PreferenceUtils from 'src/main/utils/PreferenceUtils';
import { getJSON } from 'src/main/utils/RestUtils';
import SoundUtils from 'src/main/utils/SoundUtils';
import { LOG_FILE_PATH, MONITOR_CONFIG_FILE_PATH, PREFERENCE_FILE_PATH } from 'src/main/utils/StorageUtils';
import {
LOG_FILE_PATH,
MONITOR_CONFIG_FILE_PATH,
PREFERENCE_FILE_PATH,
} from 'src/main/utils/StorageUtils';
import 'src/main/utils/SetupShortcutForWin32'; // this is to set up the icon shortcut for win32
import 'src/main/utils/LogUtils';

Expand Down Expand Up @@ -113,7 +127,7 @@ async function setUpShortcuts() {
global.emitAppEvent({ command });
}

if(keyBinding.notification){
if (keyBinding.notification) {
showNotification(keyBinding.notification);
}
});
Expand Down Expand Up @@ -163,7 +177,7 @@ async function setupCommandChannel() {
global.subscribeAppEvent(async (data) => {
const { command } = data;

if(command.includes(`command/refetch`)){
if (command.includes(`command/refetch`)) {
// global.emitAppEvent({ command: 'command/refetch' });

switch (command) {
Expand Down Expand Up @@ -197,7 +211,11 @@ async function setupCommandChannel() {
break;
}

if(!isNaN(allMonitorBrightness) && allMonitorBrightness >= 0 && allMonitorBrightness <= 100){
if (
!isNaN(allMonitorBrightness) &&
allMonitorBrightness >= 0 &&
allMonitorBrightness <= 100
) {
await DisplayUtils.batchUpdateBrightness(allMonitorBrightness, delta);
} else {
console.trace(`changeVolume failed due to invalid volume`, allMonitorBrightness, delta);
Expand Down Expand Up @@ -229,11 +247,11 @@ async function setupCommandChannel() {
if (command.includes(`command/changeVolume`)) {
const volume = parseInt(command.replace('command/changeVolume/', ''));

if(!isNaN(volume) && volume >= 0 && volume <= 100){
if (!isNaN(volume) && volume >= 0 && volume <= 100) {
const promises = [];
promises.push(SoundUtils.setMuted(volume === 0));
promises.push(SoundUtils.setVolume(volume));
await Promise.all(promises)
await Promise.all(promises);
_sendRefetchEventToFrontEnd('configs');
} else {
console.trace(`changeVolume failed due to invalid volume`, volume);
Expand Down Expand Up @@ -308,16 +326,16 @@ function _getTrayIcon() {
return nativeTheme.shouldUseDarkColors ? DARK_ICON : LIGHT_ICON;
}

async function _getLatestAppVersion(){
try{
const {version} = await getJSON(`https://synle.github.io/display-dj/release.json`);
async function _getLatestAppVersion() {
try {
const { version } = await getJSON(`https://synle.github.io/display-dj/release.json`);
return version;
} catch(err){
} catch (err) {
return '';
}
}

async function _getContextMenu(){
async function _getContextMenu() {
const brightnessPresets = await PreferenceUtils.getBrightnessPresets();
const volumePresets = await PreferenceUtils.getVolumePresets();
const latestAppVersion = await _getLatestAppVersion();
Expand All @@ -326,34 +344,34 @@ async function _getContextMenu(){
{
label: `Use Light Mode`,
click: async () => {
global.emitAppEvent({ command: 'command/changeDarkMode/light' })
global.emitAppEvent({ command: 'command/changeDarkMode/light' });
showNotification(`Turn on Light Mode`);
},
},
{
label: `Use Dark Mode`,
click: async () => {
global.emitAppEvent({ command: 'command/changeDarkMode/dark' })
global.emitAppEvent({ command: 'command/changeDarkMode/dark' });
showNotification(`Turn on Dark Mode`);
},
},
{
type: 'separator',
},
...brightnessPresets.map(brightnessPreset => ({
...brightnessPresets.map((brightnessPreset) => ({
label: `Change brightness to ${brightnessPreset.level}%`,
click: async () => {
global.emitAppEvent({ command: `command/changeBrightness/${brightnessPreset.level}` })
global.emitAppEvent({ command: `command/changeBrightness/${brightnessPreset.level}` });
showNotification(`Brightness of all monitors changed to ${brightnessPreset.level}%`);
},
})),
{
type: 'separator',
},
...volumePresets.map(volumePreset => ({
...volumePresets.map((volumePreset) => ({
label: `Change volume to ${volumePreset.level}%`,
click: async () => {
global.emitAppEvent({ command: `command/changeVolume/${volumePreset.level}` })
global.emitAppEvent({ command: `command/changeVolume/${volumePreset.level}` });
showNotification(`Volume changed to ${volumePreset.level}%`);
},
})),
Expand Down Expand Up @@ -386,8 +404,9 @@ async function _getContextMenu(){
},
{
label: `New Version Available (${latestAppVersion})`,
click: async () => global.emitAppEvent({ command: 'command/openExternal/link/downloadNewVersion' }),
visible: await latestAppVersion !== process.env.APP_VERSION
click: async () =>
global.emitAppEvent({ command: 'command/openExternal/link/downloadNewVersion' }),
visible: (await latestAppVersion) !== process.env.APP_VERSION,
},
{
type: 'separator',
Expand Down Expand Up @@ -429,29 +448,29 @@ async function _getContextMenu(){
/**
* @return None - Hide the dock icon for Mac...
*/
async function setupDockIcon(){
switch(process.platform){
async function setupDockIcon() {
switch (process.platform) {
case 'darwin':
try{
try {
app.dock.hide();
console.debug('Hide dock icon success');
} catch(err){
} catch (err) {
console.error('Hide dock icon failed with error', err);
}
break;
}
}

function _sendRefetchEventToFrontEnd(type: 'all' | 'preferences' | 'configs' = 'all'){
function _sendRefetchEventToFrontEnd(type: 'all' | 'preferences' | 'configs' = 'all') {
const eventName = 'mainAppEvent/refetch';
if(mainWindow){
mainWindow.webContents.send(eventName, {type});
if (mainWindow) {
mainWindow.webContents.send(eventName, { type });
}
}

function _shouldTraceCall( method: string, url: string){
if(method.toLowerCase() === 'get'){
if(url.includes('/api/configs') || url.includes('/api/preferences')){
function _shouldTraceCall(method: string, url: string) {
if (method.toLowerCase() === 'get') {
if (url.includes('/api/configs') || url.includes('/api/preferences')) {
return false;
}
}
Expand Down Expand Up @@ -517,7 +536,7 @@ ipcMain.on('mainAppEvent/fetch', async (event, data) => {
ok = false;
}

if(_shouldTraceCall(method, url)){
if (_shouldTraceCall(method, url)) {
console.trace(
'Response',
status,
Expand Down Expand Up @@ -598,4 +617,4 @@ ipcMain.on('mainAppEvent/fetch', async (event, data) => {
} catch (err) {
console.info('error', err);
}
});
});
Loading

0 comments on commit 3c33c31

Please sign in to comment.