Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Feature/issue#41 auto launch #42

Merged
merged 2 commits into from
Jun 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const path = require('path')
const { Monitor, CHANGED_TYPES } = require('./monitor')
const { app, Menu, Notification, Tray, nativeImage, BrowserWindow } = require('electron')
const { isDev } = require('./tool')
const { app, Menu, Notification, Tray, nativeImage, BrowserWindow, ipcMain } = require('electron')
const { isDev, getIpcStoreKey } = require('./tool')
const { store, STORE_KEY } = require('./store')
const logger = require('electron-log')

Expand Down Expand Up @@ -166,12 +166,21 @@ function initMenuBar () {
menuTray.setContextMenu(Menu.buildFromTemplate(cachedMenus))
}

function initListeners () {
ipcMain.on(getIpcStoreKey(STORE_KEY.autoLaunch), (event, value) => {
app.setLoginItemSettings({
openAtLogin: value
})
})
}

// This method will be called when Electron has done everything
// initialization and ready for creating menu bar.
app.whenReady()
.then(() => {
logger.info('It\'s time to initialize')
})
.then(initListeners)
.then(initMenuBar)
.then(() => {
logger.info('initialized')
Expand Down
4 changes: 4 additions & 0 deletions preference.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
<input type="range" id="silent-time" name="silent-time" value="1" min="1" max="6" step="1" oninput="this.nextElementSibling.value = `${this.value}h`">
<output>1h</output>
</div>
<div>
<label for="auto-launch">Run on system startup</label>
<input type="checkbox" id="auto-launch" name="auto-launch">
</div>

<script type="text/javascript" src="renderer.js"></script>

Expand Down
8 changes: 6 additions & 2 deletions preload.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
const { contextBridge } = require('electron')
const { contextBridge, ipcRenderer } = require('electron')
const { store, STORE_KEY } = require('./store')
const { getIpcStoreKey } = require('./tool')

// Expose protected methods off of window (ie.
// window.api.sendToA) in order to use ipcRenderer
// without exposing the entire object
contextBridge.exposeInMainWorld('api', {
STORE_KEY,
getStore: (key, defaultValue) => store.get(key, defaultValue),
setStore: (key, value) => store.set(key, value)
setStore: (key, value) => {
store.set(key, value)
ipcRenderer.send(getIpcStoreKey(key), value)
}
})
8 changes: 8 additions & 0 deletions renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,11 @@ silentTimeRange.addEventListener('change', (event) => {

silentTimeRange.value = window.api.getStore(window.api.STORE_KEY.silentTimeHr)
silentTimeRange.nextElementSibling.value = `${silentTimeRange.value}h`

const autoLaunch = document.querySelector('#auto-launch')

autoLaunch.addEventListener('change', (event) => {
window.api.setStore(window.api.STORE_KEY.autoLaunch, event.target.checked)
})

autoLaunch.checked = window.api.getStore(window.api.STORE_KEY.autoLaunch, false)
3 changes: 2 additions & 1 deletion store.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ const Store = require('electron-store')
const store = new Store()

const STORE_KEY = {
silentTimeHr: 'silentTimeHr'
silentTimeHr: 'silentTimeHr',
autoLaunch: 'autoLaunch'
}

module.exports = { store, STORE_KEY }
7 changes: 6 additions & 1 deletion tool.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ function isDev () {
return process.env.NODE_ENV === 'development'
}

function getIpcStoreKey (key) {
return `store:${key}`
}

module.exports = {
isDev
isDev,
getIpcStoreKey
}