mirror of
https://github.com/cupcakearmy/unpixel.git
synced 2025-09-02 08:50:40 +00:00
pause only for limited time
This commit is contained in:
@@ -3,6 +3,7 @@ import os from 'os'
|
||||
import { BrowserWindow, BrowserWindowConstructorOptions, ipcMain } from 'electron'
|
||||
import dayjs from 'dayjs'
|
||||
import logger from 'electron-log'
|
||||
import ms from 'ms'
|
||||
|
||||
import { DEV } from '.'
|
||||
import Settings from './settings'
|
||||
@@ -15,24 +16,16 @@ export default class Banner {
|
||||
static init() {
|
||||
if (this.interval) return
|
||||
this.interval = setInterval(this.check, 1000)
|
||||
this.check()
|
||||
ipcMain.on('close', () => {
|
||||
this.close()
|
||||
})
|
||||
}
|
||||
|
||||
static check() {
|
||||
const paused: boolean = Settings.load('paused')
|
||||
if (paused) {
|
||||
TrayUtility.setStatus('Paused')
|
||||
return
|
||||
}
|
||||
|
||||
const every = Settings.load('every')
|
||||
const now = dayjs()
|
||||
const lastRun = Settings.load('lastRun')
|
||||
const diff = every - now.diff(dayjs(lastRun), 'minutes')
|
||||
TrayUtility.setStatus(`Next break: ${diff}m`)
|
||||
if (diff < 1) {
|
||||
TrayUtility.build()
|
||||
const [paused, interval] = Settings.getStatus()
|
||||
if (!paused && interval < 1000) {
|
||||
Banner.open()
|
||||
}
|
||||
}
|
||||
|
@@ -8,13 +8,15 @@ import { productName } from '../../package.json'
|
||||
const autoLaunch = new AutoLaunch({ name: productName, mac: { useLaunchAgent: true } })
|
||||
|
||||
import { DEV } from '.'
|
||||
import ms from 'ms'
|
||||
import dayjs from 'dayjs'
|
||||
|
||||
const store = new Store()
|
||||
const defaults = {
|
||||
every: 20,
|
||||
duration: 20,
|
||||
boot: true,
|
||||
paused: false,
|
||||
paused: 0,
|
||||
lastRun: 0,
|
||||
autoClose: false,
|
||||
}
|
||||
@@ -26,7 +28,7 @@ const normalizers: Record<SettingKeys, (x: any) => any> = {
|
||||
duration: IntNormalizer,
|
||||
boot: BoolNormalizer,
|
||||
autoClose: BoolNormalizer,
|
||||
paused: BoolNormalizer,
|
||||
paused: IntNormalizer,
|
||||
lastRun: IntNormalizer,
|
||||
}
|
||||
|
||||
@@ -82,4 +84,17 @@ export default class Settings {
|
||||
Settings.win.webContents.openDevTools()
|
||||
}
|
||||
}
|
||||
|
||||
static getStatus(): [boolean, number] {
|
||||
const paused: number = Settings.load('paused')
|
||||
const now = Date.now()
|
||||
if (paused > now) {
|
||||
return [true, paused - now]
|
||||
}
|
||||
|
||||
const every = Settings.load('every')
|
||||
const lastRun = Settings.load('lastRun')
|
||||
const diff = every * 60 * 1000 - dayjs(now).diff(dayjs(lastRun), 'ms')
|
||||
return [false, diff]
|
||||
}
|
||||
}
|
||||
|
@@ -1,53 +1,61 @@
|
||||
import { Tray, Menu, nativeImage } from 'electron'
|
||||
import path from 'path'
|
||||
import ms from 'ms'
|
||||
|
||||
import Banner from './banner'
|
||||
import Settings from './settings'
|
||||
|
||||
enum Items {
|
||||
Status = 'status',
|
||||
Pause = 'pause',
|
||||
Run = 'run',
|
||||
}
|
||||
|
||||
export default class TrayUtility {
|
||||
static menu: Parameters<typeof Menu['buildFromTemplate']>[0] = [
|
||||
{ label: 'Status', type: 'normal', enabled: false, id: Items.Status },
|
||||
{ type: 'separator' },
|
||||
{
|
||||
label: 'Take a break now',
|
||||
type: 'normal',
|
||||
id: Items.Run,
|
||||
click: () => Banner.open(),
|
||||
},
|
||||
{ label: 'Pause', type: 'checkbox', id: Items.Pause },
|
||||
{ label: 'Settings', type: 'normal', click: () => Settings.open() },
|
||||
{ type: 'separator' },
|
||||
{ label: 'Quit', type: 'normal', role: 'quit' },
|
||||
]
|
||||
|
||||
static tray: Tray | null = null
|
||||
|
||||
static setStatus(status: string) {
|
||||
this.menu[0].label = status
|
||||
this.tray?.setContextMenu(this.build())
|
||||
}
|
||||
static build() {
|
||||
const [paused, interval] = Settings.getStatus()
|
||||
const status = paused ? `Paused for: ${ms(interval)}` : `Next break: ${ms(interval)}`
|
||||
|
||||
private static build() {
|
||||
const menu = Menu.buildFromTemplate(this.menu)
|
||||
for (const item of menu.items) {
|
||||
if (item.id === Items.Pause) {
|
||||
let initial = Settings.load('paused')
|
||||
item.checked = initial
|
||||
item.click = () => {
|
||||
initial = !initial
|
||||
item.checked = initial
|
||||
Settings.save('paused', initial)
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
return menu
|
||||
const template: Parameters<typeof Menu['buildFromTemplate']>[0] = [
|
||||
{ label: status, type: 'normal', enabled: false },
|
||||
{ type: 'separator' },
|
||||
{
|
||||
label: 'Take a break now',
|
||||
type: 'normal',
|
||||
click: () => Banner.open(),
|
||||
},
|
||||
]
|
||||
|
||||
template.push(
|
||||
paused
|
||||
? {
|
||||
label: 'Break pause',
|
||||
click: () => {
|
||||
Settings.save('paused', 0)
|
||||
this.build()
|
||||
},
|
||||
}
|
||||
: {
|
||||
label: 'Pause for...',
|
||||
submenu: Menu.buildFromTemplate(
|
||||
// Minutes to pause
|
||||
[10, 30, 60, 120, 360]
|
||||
.map((minutes) => minutes * 60 * 1000)
|
||||
.map((time) => ({
|
||||
label: ms(time),
|
||||
click: () => {
|
||||
Settings.save('paused', Date.now() + time)
|
||||
this.build()
|
||||
},
|
||||
}))
|
||||
),
|
||||
}
|
||||
)
|
||||
|
||||
template.push(
|
||||
{ label: 'Settings', click: () => Settings.open() },
|
||||
{ type: 'separator' },
|
||||
{ label: 'Quit', role: 'quit' }
|
||||
)
|
||||
|
||||
const menu = Menu.buildFromTemplate(template)
|
||||
this.tray?.setContextMenu(menu)
|
||||
}
|
||||
|
||||
static init() {
|
||||
@@ -58,7 +66,7 @@ export default class TrayUtility {
|
||||
height: 24,
|
||||
})
|
||||
this.tray = new Tray(icon)
|
||||
this.tray.setContextMenu(this.build())
|
||||
this.build()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user