How to detect system idle in Electron app and do something

Viewed 503

I developed app and I want to make it if the user do nothing in 30 minutes close the app.

So I search it on google to solve this problem and I found that there is 'powerMonitor' API in electron but the problem is my app is old version (Electron 3.1.14) so it doesn't support various methods.

Electron powerMonitor API Docs

I think I should use powerMonitor.querySystemIdleTime() method and I tried to test 'pwoerMonitor' API so I paste code but

console.log(powerMonitor.querySystemIdleTime()) it returns this error message.
enter image description here

How can I detect system idle and do something in electron? Should I use another package?

import { app, dialog, Menu, Notification, Tray, ipcMain, powerMonitor } from 'electron'
import windowManager from 'electron-window-manager'
import schedule from 'node-schedule'

let mainWindow = null
let tray = null
if (process.env.NODE_ENV !== 'development') {
  global.__static = require('path').join(__dirname, '/static').replace(/\\/g, '\\\\')
}
app.setAppUserModelId('Geomec Cloud Manager')
windowManager.init({
  appBase: process.env.NODE_ENV === 'production'
    ? `file://${__dirname}`
    : 'http://localhost:9080'
})
if (isSecondInstance) {
  app.quit()
} else {
  app.on('second-instance', () => {
    if (mainWindow) {
      mainWindow.object.show()
      mainWindow.object.focus()
    }
  })
  /* Main Window */
  app.on('ready', () => {
    mainWindow = windowManager.createNew('main-window', '', '/index.html', null, {
      title: 'Geomec Cloud Manager',
      height: 500,
      width: 500,
      useContentSize: true,
      frame: false,
      maximizable: false,
      minimizable: false,
      resizable: false
    }).create()
    console.log(powerMonitor.querySystemIdleTime()) // I thought it returns idle time
    if (!process.argv.includes('--systray')) {
      mainWindow.object.once('ready-to-show', () => {
        mainWindow.object.show()
      })
    }
    tray = new Tray(__static + '/tray-icon.ico')
    const trayMenu = Menu.buildFromTemplate([{
      label: '프로그램 정보',
      click () {
        const window = windowManager.createNew('about-window', '', '/index.html#about', null, {
          height: 380,
          width: 550,
          useContentSize: true,
          frame: false,
          maximizable: false,
          minimizable: false,
          resizable: false,
          modal: true,
          parent: windowManager.get('main-window').object
        }).create()
        window.object.once('ready-to-show', () => {
          window.object.show()
        })
      }
    },
    {
      /* Tray 내부 프로그램 종료 함수 */
      label: '프로그램 종료',
      click () {
        const notification = {
          icon: __static + '/' + 'app-icon.png',
          title: '네트워크 드라이브 연결이 해제되었습니다',
          body: '프로그램을 종료합니다'
        }
        new Notification(notification).show()
        mainWindow.object.webContents.send('terminate-child-processes')
        ipcMain.on('child-processes-terminated', () => {
          setTimeout(function () {
            app.quit()
          }, 5000)
        })
      }
    }
    ])
    tray.setToolTip('Geomec Cloud Manager')
    tray.setContextMenu(trayMenu)
    tray.on('click', () => {
      mainWindow.object.show()
    })
  })
}


1 Answers

According to the docs the call is: powerMonitor.querySystemIdleTime(callback) and as the error message says, you are not giving the required argument.

Try changing:

console.log(powerMonitor.querySystemIdleTime())

to:

powerMonitor.querySystemIdleTime((idleSecs)=>{
  console.log(`The system has been idle for ${idleSecs} seconds.`)
  })

Based on your use case, it is actually powerMonitor.querySystemIdleState(idleThreshold, callback) you will want to be using, with idleThreshold set to 30 * 60.

Related