-
Notifications
You must be signed in to change notification settings - Fork 40
/
renderer.js
154 lines (123 loc) Β· 3.87 KB
/
renderer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
'use strict'
// DOM elements & variables
// =============================================================================
// Get menubar instance from main.js
const {mb} = require('electron').remote.getGlobal('sharedObject')
const {ipcRenderer} = require('electron')
const path = require('path')
const Timer = require('tiny-timer')
const appContainer = document.querySelector('.js-app')
const startButton = document.querySelector('.js-start-btn')
const stopButton = document.querySelector('.js-stop-btn')
const slider = document.querySelector('.js-slider')
// Sounds
let soundEnabled = true
const soundWindup = new Audio(path.join(__dirname, '/wav/windup.wav'))
const soundClick = new Audio(path.join(__dirname, '/wav/click.wav'))
const soundDing = new Audio(path.join(__dirname, '/wav/ding.wav'))
let state = ''
let currentMinute = 0
const workMinutes = 25
const breakMinutes = 5
// Timer stuff
const timer = new Timer()
// Utilities
// =============================================================================
const minToMs = min => min * 60 * 1000
const msToMin = ms => ms / 60 / 1000
const getCurrentMinutes = () => state === 'breaking' ? breakMinutes : workMinutes
const getCurrentSliderWidth = () => state === 'breaking' ? 100 : 500
const playSound = sound => {
sound.currentTime = 0
if (soundEnabled) {
sound.play()
}
}
// State handling
// =============================================================================
const setState = newState => {
appContainer.classList.remove('is-stopped', 'is-working', 'is-breaking')
appContainer.classList.add(`is-${newState}`)
state = newState
}
setState('stopped')
const setIcon = (currentMinute, currentState) => {
const process = require('process')
let file = ''
const breakSuffix = currentState === 'breaking' ? '-break' : ''
switch (process.platform) {
case 'darwin': {
file = path.join(__dirname, `img/template/icon-${currentMinute}${breakSuffix}-Template.png`)
break
}
case 'win32': {
file = path.join(__dirname, `img/ico/icon-${currentMinute}${breakSuffix}.ico`)
break
}
default: {
file = path.join(__dirname, `img/png/icon-${currentMinute}${breakSuffix}.png`)
}
}
mb.tray.setImage(file)
}
const setCurrentMinute = ms => {
currentMinute = Math.ceil(msToMin(ms))
setIcon(currentMinute, state)
}
setCurrentMinute(0)
// Event handlers
// =============================================================================
document.addEventListener('keydown', event => {
switch (event.key) {
case 'Escape': {
mb.hideWindow()
break
}
default: {
break
}
}
})
startButton.addEventListener('click', () => {
playSound(soundWindup)
timer.start(minToMs(workMinutes))
setState('working')
slider.classList.add('is-resetting-work')
setTimeout(() => slider.classList.remove('is-resetting-work'), 1000)
})
stopButton.addEventListener('click', () => {
playSound(soundClick)
timer.stop()
setState('stopped')
})
mb.on('after-hide', () => {
mb.app.hide()
})
timer.on('tick', ms => {
const minutes = getCurrentMinutes()
const sliderWidth = getCurrentSliderWidth()
slider.style.transform = 'translateX(-' + Math.ceil((sliderWidth * ms) / (minToMs(minutes))) + 'px)'
setCurrentMinute(ms)
})
timer.on('done', () => {
playSound(soundDing)
setCurrentMinute(0)
mb.showWindow()
setTimeout(() => {
playSound(soundWindup)
if (state === 'working') {
setState('breaking')
timer.start(minToMs(breakMinutes))
slider.classList.add('is-resetting-break')
setTimeout(() => slider.classList.remove('is-resetting-break'), 1000)
} else {
setState('working')
timer.start(minToMs(workMinutes))
slider.classList.add('is-resetting-work')
setTimeout(() => slider.classList.remove('is-resetting-work'), 1000)
}
}, 2000)
})
ipcRenderer.on('TOGGLE_SOUND', (event, data) => {
soundEnabled = data
})