Skip to content

Commit

Permalink
test autolaunch #6
Browse files Browse the repository at this point in the history
- created the autolaunch method and check if launched automatically
  • Loading branch information
Dadangdut33 committed Apr 20, 2022
1 parent 0aebe14 commit 829dbd4
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 11 deletions.
73 changes: 64 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@
"@mui/x-date-pickers": "^5.0.0-alpha.0",
"adhan": "^4.3.2",
"all-the-cities": "^3.1.0",
"auto-launch": "^5.0.5",
"electron-debug": "^3.2.0",
"electron-fetch": "^1.7.4",
"electron-log": "^4.4.6",
Expand Down
53 changes: 52 additions & 1 deletion src/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { getPrayerTimes } from './handler/praytime';
import { onUnresponsiveWindow, errorBox, NoYesBox } from './handler/messageBox';
import { getLatLong_FromCitiesName, getPosition_absolute, verifyKey } from './handler/getPos';
import Moment from 'moment-timezone';
import os from 'os';

// -------------------------------------------------------------------------------------
// Global vars
Expand All @@ -29,7 +30,14 @@ let mainWindow: BrowserWindow | null = null,
timerTimeout: NodeJS.Timeout,
timerInterval: NodeJS.Timer,
checkTimeChangesInterval: NodeJS.Timer,
startDate = new Date();
startDate = new Date(),
autoLaunch = require('auto-launch'),
launcherOption = {
name: 'Simple PrayerTimes Reminder',
path: process.execPath,
isHidden: true,
},
autoLauncher = new autoLaunch(launcherOption);

let menuBuilder: MenuBuilder, trayManager: TrayManager;

Expand All @@ -39,6 +47,19 @@ const getAssetPath = (...paths: string[]): string => {
return path.join(RESOURCES_PATH, ...paths);
};

const wasOpenedAtLogin = () => {
try {
// mac
if (os.platform() === 'darwin') {
let loginSettings = app.getLoginItemSettings();
return loginSettings.wasOpenedAtLogin;
} // else
return app.commandLine.hasSwitch('hidden');
} catch {
return false;
}
};

// -------------------------------------------------------------------------------------
/**
* Setup
Expand Down Expand Up @@ -134,6 +155,18 @@ const checkConfigOnStart = async () => {
appConfig.locationOption.latitude = latitude;
appConfig.locationOption.longitude = longitude;

// set autolaunch value
autoLauncher
.isEnabled()
.then((isEnabled: boolean) => {
if (isEnabled) return;

autoLauncher.enable();
})
.catch((err: any) => {
console.log(err);
});

// write config file
if (data.toString().includes('ENOENT')) {
// no config file found
Expand Down Expand Up @@ -186,6 +219,15 @@ app.whenReady()
// start detecttimechange interval if enabled
if (appConfig.detectTimeChange) checkIfUserChangesLocalTime(); // check if locale time is changed by user

// auto launch
if (appConfig.runAtStartup) {
const checkLoginOpen = wasOpenedAtLogin();

if (checkLoginOpen) {
mainWindow?.hide();
}
}

app.on('activate', () => {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
Expand Down Expand Up @@ -225,6 +267,15 @@ ipcMain.on('save-config', (event, arg) => {
if (arg.detectTimeChange) checkIfUserChangesLocalTime(); // check enabled or disabled
else clearCheckTimeChangesInterval();

// if runAtStartup is changed
if (appConfig.runAtStartup !== arg.runAtStartup) {
if (arg.runAtStartup) {
autoLauncher.enable();
} else {
autoLauncher.disable();
}
}

appConfig = arg;
updatePt();
trayManager.updatePrayTime(ptGet, appConfig);
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/components/Praytime.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const Praytime = ({ theme }: any) => {
const [clockValue, setClockValue] = useState(new Date());
const [randomColorList, setRandomColorList] = useState<ColorHex[]>([]);
const [colorChangeSecondsList, setColorChangeSecondsList] = useState<number[]>([]);
const forbiddenColor = ['#dfdfdf', '#d9d9d9', '#e9e9e9', '#e2e2e2', '#dadada', '#e1e1d7', '#deb4ac', '#db918a']; // Mostly gray
const forbiddenColor = ['#dfdfdf', '#d9d9d9', '#e9e9e9', '#e2e2e2', '#dadada', '#e1e1d7', '#deb4ac', '#db918a', '#82c0c2']; // Mostly gray
const amountDivider = 75;

// ----------------------------------------------------------------------------------------------------------------------
Expand Down

0 comments on commit 829dbd4

Please sign in to comment.