Skip to content

Commit

Permalink
fix: software does not automatically sleep when the computer sleeps
Browse files Browse the repository at this point in the history
  • Loading branch information
badspider7 committed Aug 29, 2024
1 parent 272a8e0 commit 3758874
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,3 @@ jobs:

- name: Lint
run: pnpm lint

- name: Release window
run: pnpm release:win
3 changes: 2 additions & 1 deletion electron/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import path from 'node:path'
import os from 'node:os'
import { BrowserWindow, app, ipcMain, shell } from 'electron'
import { setupHandle } from '../handle'
import { clearAllTimer, startRecord } from '../utils'
import { clearAllTimer, isComputerSleep, startRecord } from '../utils'
import logger from '../logger'
import { initDb } from '../db'

Expand Down Expand Up @@ -95,6 +95,7 @@ export async function createWindow() {
})
// 开始统计应用时间
startRecord()
isComputerSleep(win)
})

// Make all links open with the browser, not with the application
Expand Down
14 changes: 14 additions & 0 deletions electron/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { BrowserWindow } from 'electron'
import { powerMonitor } from 'electron'
import platform from '../const/getPlatform'
import TimerListener from './timeTracker'
import { getWinIco } from './appIcon/win'
Expand All @@ -19,3 +21,15 @@ export function clearAllTimer() {
TimerListener.clearAll()
cache.stopSaveInterval()
}

export function isComputerSleep(win: BrowserWindow) {
powerMonitor.on('suspend', () => {
TimerListener.sleep()
console.log('系统即将进入休眠状态')
})

powerMonitor.on('resume', () => {
TimerListener.wake()
console.log('系统已从休眠状态恢复')
})
}
1 change: 0 additions & 1 deletion electron/utils/timeTracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ class TimeTracker {
}

// 超过5 分钟没有操作就进入休眠
// TODO: 优化:要考虑电脑休眠的情况
if (!this.isSleeping && idleTime > SLEEP_TIME) {
this.sleep()
}
Expand Down

0 comments on commit 3758874

Please sign in to comment.