Skip to content

Commit e4f7b22

Browse files
committed
Listen to alarm with volume button
1 parent 5701245 commit e4f7b22

File tree

4 files changed

+67
-21
lines changed

4 files changed

+67
-21
lines changed

src/scripts/features/pomodoro.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ type PomodoroUpdate = {
1818
sound?: boolean
1919
volume?: number
2020
alarm?: string
21+
listen?: true
2122
timeFor?: Partial<Record<PomodoroMode, number>>
2223
history?: { endedAt: string; duration?: number }
2324
}
@@ -40,6 +41,7 @@ const focusButton = document.getElementById('pmdr-focus') as HTMLInputElement
4041
// to communicate with other tabs
4142
const broadcast = new BroadcastChannel('bonjourr_pomodoro') as BroadcastChannel
4243

44+
let alarmAudio: HTMLAudioElement = new Audio()
4345
let countdown: number
4446
let timeModeTimeout: number
4547
let tabTitleTimeout: number
@@ -467,16 +469,13 @@ export function togglePomodoroFocus(focus: boolean) {
467469
}
468470

469471
function ringTheAlarm() {
470-
const alarmSound = new Audio('src/assets/sounds/clock-alarm-classic.mp3')
471-
472472
// only triggers on the last active tab
473473
const lastTab = localStorage.getItem('lastActiveTab')
474474
const willRingAndSave = lastTab === TAB_ID
475475

476476
if (willRingAndSave) {
477477
if (currentPomodoroData.sound) {
478-
alarmSound.volume = 0.6
479-
alarmSound.play()
478+
playSound()
480479
}
481480

482481
// if pomodoro ends, registers new session
@@ -495,6 +494,12 @@ function ringTheAlarm() {
495494
}
496495
}
497496

497+
function playSound() {
498+
alarmAudio.src = ('src/assets/sounds/clock-alarm-classic.mp3')
499+
alarmAudio.volume = currentPomodoroData.volume ?? .7
500+
alarmAudio.play()
501+
}
502+
498503
function setPomodoroInfo(history: PomodoroHistoryEntry[]) {
499504
const now = new Date()
500505

@@ -529,6 +534,11 @@ function setPomodoroInfo(history: PomodoroHistoryEntry[]) {
529534
async function updatePomodoro(update: PomodoroUpdate) {
530535
const data = await storage.sync.get(['pomodoro'])
531536

537+
if (update.listen) {
538+
playSound()
539+
return
540+
}
541+
532542
if (update.on !== undefined) {
533543
data.pomodoro.on = update.on
534544
}

src/scripts/settings.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,10 @@ function initOptionsEvents() {
886886
pomodoro(undefined, { sound: target.checked })
887887
})
888888

889+
onclickdown(paramId('i_pmdr_listen'), () => {
890+
pomodoro(undefined, { listen: true })
891+
})
892+
889893
paramId('i_pmdr_alarms').addEventListener('change', function () {
890894
pomodoro(undefined, { alarm: this.value })
891895
})
@@ -906,16 +910,6 @@ function initOptionsEvents() {
906910
pomodoro(undefined, { timeFor: { longbreak: Number(this.value) } })
907911
})
908912

909-
// paramId('i_pmdr_pomodoro').addEventListener('change', () => {
910-
// paramId('i_pmdr_pomodoro').blur()
911-
// })
912-
// paramId('i_pmdr_break').addEventListener('change', () => {
913-
// paramId('i_pmdr_break').blur()
914-
// })
915-
// paramId('i_pmdr_longbreak').addEventListener('change', () => {
916-
// paramId('i_pmdr_longbreak').blur()
917-
// })
918-
919913
// Custom fonts
920914

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

src/settings.html

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1683,13 +1683,47 @@ <h2 id="lbl-quotes" class="trn">Pomodoro timer</h2>
16831683

16841684
<div class="wrapper">
16851685
<label for="i_pmdr_alarms" class="trn">Alarm tune</label>
1686-
<select id="i_pmdr_alarms">
1687-
<option value="default" class="trn">Default</option>
1688-
<option value="marimbas">Marimbas</option>
1689-
<option value="thunderstorm">Thunderstorm</option>
1690-
<option value="birds">Birds</option>
1691-
<option value="clock">Clock</option>
1692-
</select>
1686+
1687+
<div class="split-inputs">
1688+
<select id="i_pmdr_alarms">
1689+
<option value="marimbas">Marimbas</option>
1690+
<option value="thunderstorm">Thunderstorm</option>
1691+
<option value="birds">Birds</option>
1692+
<option value="clock">Clock</option>
1693+
</select>
1694+
1695+
<button
1696+
id="i_pmdr_listen"
1697+
class="input-btn icon-only"
1698+
aria-label="Listen to pomodoro alarm"
1699+
title="Listen to pomodoro alarm"
1700+
>
1701+
<span>
1702+
<svg
1703+
width="24"
1704+
height="24"
1705+
viewBox="0 0 24 24"
1706+
fill="none"
1707+
xmlns="http://www.w3.org/2000/svg"
1708+
>
1709+
<path
1710+
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"
1711+
fill="currentColor"
1712+
/>
1713+
<path
1714+
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"
1715+
fill="currentColor"
1716+
/>
1717+
<path
1718+
fill-rule="evenodd"
1719+
clip-rule="evenodd"
1720+
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"
1721+
fill="currentColor"
1722+
/>
1723+
</svg>
1724+
</span>
1725+
</button>
1726+
</div>
16931727
</div>
16941728

16951729
<hr />

src/styles/settings/inputs.css

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,14 @@ aside .input-btn {
7070
background-color: var(--color-input);
7171
}
7272

73+
aside .input-btn:hover {
74+
filter: brightness(.9);
75+
}
76+
77+
aside .input-btn:active {
78+
filter: brightness(.8);
79+
}
80+
7381
aside .input-btn.icon-only span {
7482
display: flex;
7583
justify-content: center;

0 commit comments

Comments
 (0)