Skip to content

Commit db217b3

Browse files
authored
Merge pull request #759 from victrme/pomodoro-alarms
Add pomodoro alarm choices and volume slider
2 parents 4968f38 + 39f0cdd commit db217b3

File tree

12 files changed

+180
-65
lines changed

12 files changed

+180
-65
lines changed

src/assets/sounds/marimba.mp3

242 KB
Binary file not shown.

src/scripts/compatibility/filters.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,17 @@ export function newLinkIcons(data: Import): Import {
5050
return data
5151
}
5252

53+
export function addAlarmsToPomodoro(data: Import): Import {
54+
if (!data.pomodoro) {
55+
data.pomodoro = SYNC_DEFAULT.pomodoro
56+
}
57+
58+
data.pomodoro.alarm = data.pomodoro.alarm ?? SYNC_DEFAULT.pomodoro.alarm
59+
data.pomodoro.volume = data.pomodoro.volume ?? SYNC_DEFAULT.pomodoro.alarm
60+
61+
return data
62+
}
63+
5364
export function fixNullBrightness(data: Import): Import {
5465
if (data.backgrounds?.bright === null) {
5566
data.backgrounds.bright = SYNC_DEFAULT.backgrounds.bright

src/scripts/compatibility/versions.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ export function filterByVersion(data: Partial<Sync>, version: SemVer): Partial<S
77

88
if (major < 22) {
99
data = filter.newLinkIcons(data)
10+
11+
if (minor < 1) {
12+
data = filter.addAlarmsToPomodoro(data)
13+
}
1014
}
1115

1216
if (major <= 21) {

src/scripts/defaults.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,8 @@ export const SYNC_DEFAULT: Sync = {
203203
},
204204
focus: false,
205205
sound: true,
206+
volume: 0.7,
207+
alarm: 'marimba',
206208
history: [],
207209
},
208210
font: {

src/scripts/features/fonts.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -246,9 +246,6 @@ function displayFont({ family, size, weight, system }: Font) {
246246

247247
function setFontSize(size: string) {
248248
document.documentElement.style.setProperty('--font-size', `${Number.parseInt(size) / 16}em`)
249-
250-
// Pomodoro container's font needs to be in pixels for focus animation to work properly
251-
document.documentElement.style.setProperty('--pomodoro-font-size', `${Number.parseInt(size)}px`)
252249
}
253250

254251
//

src/scripts/features/pomodoro.ts

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@ type PomodoroUpdate = {
1515
mode?: PomodoroMode
1616
pause?: number
1717
focus?: boolean
18-
timeFor?: Partial<Record<PomodoroMode, number>>
1918
sound?: boolean
19+
volume?: number
20+
alarm?: string
21+
listen?: true
22+
timeFor?: Partial<Record<PomodoroMode, number>>
2023
history?: { endedAt: string; duration?: number }
2124
}
2225

@@ -27,8 +30,6 @@ type PomodoroHistoryEntry = {
2730

2831
let currentPomodoroData: Pomodoro
2932

30-
const alarmSound = new Audio('src/assets/sounds/clock-alarm-classic.mp3')
31-
3233
const pomodoroContainer = document.getElementById('pomodoro_container') as HTMLDivElement
3334
const pomodoroStart = document.getElementById('pmdr_start') as HTMLButtonElement
3435
const pomodoroPause = document.getElementById('pmdr_pause') as HTMLButtonElement
@@ -44,6 +45,7 @@ let countdown: number
4445
let timeModeTimeout: number
4546
let tabTitleTimeout: number
4647
const timeBeforeReset = 10000 // time before the timer resets after the end
48+
const alarmAudio: HTMLAudioElement = new Audio()
4749

4850
const setModeButton = (value = '') => {
4951
return (document.getElementById(`pmdr-${value}`) as HTMLInputElement).checked = true
@@ -422,6 +424,7 @@ export function togglePomodoroFocus(focus: boolean) {
422424
clone.style.position = 'absolute'
423425
clone.style.top = originalRect.top + 'px'
424426
clone.style.left = originalRect.left + 'px'
427+
clone.style.fontSize = document.documentElement.style.getPropertyValue('--font-size')
425428
clone.style.fontFamily = document.documentElement.style.getPropertyValue('--font-family')
426429
clone.classList.add('clone')
427430

@@ -472,7 +475,7 @@ function ringTheAlarm() {
472475

473476
if (willRingAndSave) {
474477
if (currentPomodoroData.sound) {
475-
alarmSound.play()
478+
playSound()
476479
}
477480

478481
// if pomodoro ends, registers new session
@@ -491,6 +494,15 @@ function ringTheAlarm() {
491494
}
492495
}
493496

497+
function playSound() {
498+
const filename = currentPomodoroData.alarm || 'marimba'
499+
const volume = currentPomodoroData.volume ?? .7
500+
501+
alarmAudio.src = `src/assets/sounds/${filename}.mp3`
502+
alarmAudio.volume = volume
503+
alarmAudio.play()
504+
}
505+
494506
function setPomodoroInfo(history: PomodoroHistoryEntry[]) {
495507
const now = new Date()
496508

@@ -522,42 +534,57 @@ function setPomodoroInfo(history: PomodoroHistoryEntry[]) {
522534
;(document.getElementById('poms-month') as HTMLSpanElement).textContent = pomsMonth.toString()
523535
}
524536

525-
async function updatePomodoro({ on, sound, end, mode, pause, focus, timeFor, history }: PomodoroUpdate) {
537+
async function updatePomodoro(update: PomodoroUpdate) {
526538
const data = await storage.sync.get(['pomodoro'])
527539

528-
if (on !== undefined) {
529-
data.pomodoro.on = on
540+
if (update.listen) {
541+
playSound()
542+
return
543+
}
544+
545+
if (update.on !== undefined) {
546+
data.pomodoro.on = update.on
547+
}
548+
549+
if (update.sound !== undefined) {
550+
data.pomodoro.sound = update.sound
530551
}
531552

532-
if (sound !== undefined) {
533-
data.pomodoro.sound = sound
553+
if (update.alarm) {
554+
data.pomodoro.alarm = update.alarm
534555
}
535556

536-
if (end !== undefined) {
537-
data.pomodoro.end = end
557+
if (update.volume) {
558+
data.pomodoro.volume = update.volume
538559
}
539560

540-
if (mode) {
541-
data.pomodoro.mode = mode
561+
if (update.end !== undefined) {
562+
data.pomodoro.end = update.end
542563
}
543564

544-
if (pause !== undefined) {
545-
data.pomodoro.pause = pause
565+
if (update.mode) {
566+
data.pomodoro.mode = update.mode
546567
}
547568

548-
if (focus !== undefined) {
549-
data.pomodoro.focus = focus
569+
if (update.pause !== undefined) {
570+
data.pomodoro.pause = update.pause
550571
}
551572

552-
if (history !== undefined) {
573+
if (update.focus !== undefined) {
574+
data.pomodoro.focus = update.focus
575+
}
576+
577+
if (update.history !== undefined) {
553578
data.pomodoro.history.push({
554-
endedAt: history.endedAt,
579+
endedAt: update.history.endedAt,
555580
duration: data.pomodoro.timeFor['pomodoro'],
556581
})
557582
}
558583

559584
// the time defined by the user for each mode (pomodoro, break...)
560-
if (timeFor) {
585+
if (update.timeFor) {
586+
const { timeFor } = update
587+
561588
for (const mode of Object.keys(timeFor) as PomodoroMode[]) {
562589
const value = timeFor[mode]
563590

@@ -574,7 +601,7 @@ async function updatePomodoro({ on, sound, end, mode, pause, focus, timeFor, his
574601
// known flaw: sessions are only up to date on the ringing tab
575602
setPomodoroInfo(data.pomodoro.history)
576603

577-
if (timeFor) {
604+
if (update.timeFor) {
578605
resetTimer()
579606
}
580607
}

src/scripts/settings.ts

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,12 @@ function initOptionsValues(data: Sync, local: Local) {
228228
setInput('i_size', data.font?.size || (IS_MOBILE ? '11' : '14'))
229229
setInput('i_announce', data.announcements ?? 'major')
230230
setInput('i_synctype', local.syncType ?? (PLATFORM === 'online' ? 'off' : 'browser'))
231-
setInput('i_pmdr_break', data.pomodoro.timeFor.break / 60)
232-
setInput('i_pmdr_pomodoro', data.pomodoro.timeFor.pomodoro / 60)
233-
setInput('i_pmdr_longbreak', data.pomodoro.timeFor.longbreak / 60)
231+
setInput('i_pmdr_volume', data.pomodoro?.volume ?? 0.7)
232+
setInput('i_pmdr_alarms', data.pomodoro?.alarm ?? 'marimba')
233+
setInput('i_pmdr_break', data.pomodoro?.timeFor.break / 60)
234+
setInput('i_pmdr_break', data.pomodoro?.timeFor.break / 60)
235+
setInput('i_pmdr_pomodoro', data.pomodoro?.timeFor.pomodoro / 60)
236+
setInput('i_pmdr_longbreak', data.pomodoro?.timeFor.longbreak / 60)
234237

235238
setFormInput('i_city', local.lastWeather?.approximation?.city ?? 'Paris', data.weather.city)
236239
setFormInput('i_customfont', systemfont.placeholder, data.font?.family)
@@ -886,6 +889,18 @@ function initOptionsEvents() {
886889
pomodoro(undefined, { sound: target.checked })
887890
})
888891

892+
onclickdown(paramId('i_pmdr_listen'), () => {
893+
pomodoro(undefined, { listen: true })
894+
})
895+
896+
paramId('i_pmdr_alarms').addEventListener('change', function () {
897+
pomodoro(undefined, { alarm: this.value })
898+
})
899+
900+
paramId('i_pmdr_volume').addEventListener('input', function () {
901+
pomodoro(undefined, { volume: Number(this.value) })
902+
})
903+
889904
paramId('i_pmdr_pomodoro').addEventListener('input', function () {
890905
pomodoro(undefined, { timeFor: { pomodoro: Number(this.value) } })
891906
})
@@ -898,16 +913,6 @@ function initOptionsEvents() {
898913
pomodoro(undefined, { timeFor: { longbreak: Number(this.value) } })
899914
})
900915

901-
// paramId('i_pmdr_pomodoro').addEventListener('change', () => {
902-
// paramId('i_pmdr_pomodoro').blur()
903-
// })
904-
// paramId('i_pmdr_break').addEventListener('change', () => {
905-
// paramId('i_pmdr_break').blur()
906-
// })
907-
// paramId('i_pmdr_longbreak').addEventListener('change', () => {
908-
// paramId('i_pmdr_longbreak').blur()
909-
// })
910-
911916
// Custom fonts
912917

913918
paramId('i_customfont').addEventListener('pointerenter', () => {

src/settings.html

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1681,6 +1681,67 @@ <h2 id="lbl-quotes" class="trn">Pomodoro timer</h2>
16811681
<div class="as as_pomodoro dropdown">
16821682
<hr />
16831683

1684+
<div class="wrapper">
1685+
<label for="i_pmdr_alarms" class="trn">Alarm tune</label>
1686+
1687+
<div class="split-inputs">
1688+
<select id="i_pmdr_alarms">
1689+
<option value="marimba">Marimba</option>
1690+
<option value="clock-alarm">Clock Alarm</option>
1691+
</select>
1692+
1693+
<button
1694+
id="i_pmdr_listen"
1695+
class="input-btn icon-only"
1696+
aria-label="Listen to pomodoro alarm"
1697+
title="Listen to pomodoro alarm"
1698+
>
1699+
<span>
1700+
<svg
1701+
width="24"
1702+
height="24"
1703+
viewBox="0 0 24 24"
1704+
fill="none"
1705+
xmlns="http://www.w3.org/2000/svg"
1706+
>
1707+
<path
1708+
d="M24 12C24 16.4183 20.4183 20 16 20V18C19.3137 18 22 15.3137 22 12C22 8.68629 19.3137 6 16 6V4C20.4183 4 24 7.58172 24 12Z"
1709+
fill="currentColor"
1710+
/>
1711+
<path
1712+
d="M20 12C20 14.2091 18.2091 16 16 16V14C17.1046 14 18 13.1046 18 12C18 10.8954 17.1046 10 16 10V8C18.2091 8 20 9.79086 20 12Z"
1713+
fill="currentColor"
1714+
/>
1715+
<path
1716+
fill-rule="evenodd"
1717+
clip-rule="evenodd"
1718+
d="M9 16L15 20V4L9 8H5C2.79086 8 1 9.79086 1 12C1 14.2091 2.79086 16 5 16H9ZM5 10H9L13 7.5V16.5L9 14H5C3.89543 14 3 13.1046 3 12C3 10.8954 3.89543 10 5 10Z"
1719+
fill="currentColor"
1720+
/>
1721+
</svg>
1722+
</span>
1723+
</button>
1724+
</div>
1725+
</div>
1726+
1727+
<hr />
1728+
1729+
<div class="wrapper">
1730+
<label for="i_pmdr_volume" class="trn">Alarm volume</label>
1731+
<input
1732+
id="i_pmdr_volume"
1733+
name="i_pmdr_volume"
1734+
type="range"
1735+
class="range"
1736+
min=".1"
1737+
max="1"
1738+
step="0.1"
1739+
value=".6"
1740+
/>
1741+
</div>
1742+
1743+
<hr />
1744+
16841745
<div class="wrapper">
16851746
<label for="i_pmdr_pomodoro_time" class="trn">Default time</label>
16861747
<div class="inputWithSuffix">

0 commit comments

Comments
 (0)