Skip to content

Commit fc71a43

Browse files
committed
[#80] Show a notification when caps lock is on
1 parent cf53ca1 commit fc71a43

4 files changed

Lines changed: 28 additions & 4 deletions

File tree

src/_locales/de/messages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,10 @@
359359
"message": "Frame zurück",
360360
"description": "Notification text für 'frame back' Aktion"
361361
},
362+
"playerCaps": {
363+
"message": "Achtung: Caps-Lock ist aktiv, manche Tastenkombinationen werden nicht funktionieren",
364+
"description": "Notification text für wenn CapsLock aktiv"
365+
},
362366
"playerFrameForward": {
363367
"message": "Frame nach vorne",
364368
"description": "Notification text für 'frame back' Aktion"

src/_locales/en/messages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,10 @@
363363
"message": "Jump frame forward",
364364
"description": "Notification text for 'frame back' action"
365365
},
366+
"playerCaps": {
367+
"message": "Warning: Caps-Lock is enabled, some keybinds may not work properly",
368+
"description": "Notification text for when CapsLock is on"
369+
},
366370
"pageAddToQueue": {
367371
"message": "Add to queue",
368372
"description": "Hidden hint for queue button."
Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,31 @@
1-
21
let timeout = 0;
32
const stop = () => {
43
window.clearTimeout(timeout);
54
timeout = 0;
65
};
76

8-
export const notification = (text: string | HTMLElement, duration = 1600, source: HTMLElement = document.body) => {
7+
export const notification = (text: string | HTMLElement, duration: number | null = 1600, source: HTMLElement = document.body) => {
98
if (timeout) {
109
stop();
1110
}
1211
const existing = source.querySelector<HTMLDivElement>('.notification');
1312
const n = existing || source.appendChild(document.createElement('div'));
1413
n.classList.add('notification');
1514
const content = typeof text === 'string' ? document.createTextNode(text) : text;
16-
n.replaceChildren(n.appendChild(content));
15+
n.replaceChildren(content);
16+
17+
const hide = () => {
18+
n.classList.remove('show');
19+
stop();
20+
};
1721

1822
const show = () => {
1923
n.classList.add('show');
20-
timeout = window.setTimeout(() => (n.classList.remove('show'), stop()), duration);
24+
if (duration !== null)
25+
timeout = window.setTimeout(hide, duration);
2126
};
2227
// when creating a new element, make sure it first has its position outside the viewport, to play animation
2328
if (existing === null) window.setTimeout(show, 1);
2429
else show();
30+
return hide;
2531
};

src/scripts/page/player.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ export const init = async () => {
6464
msgs['playerFrameBack'] = await sendMessage(Message.GET_MESSAGE, { message: 'playerFrameBack' });
6565
msgs['playerFrameForward'] = await sendMessage(Message.GET_MESSAGE, { message: 'playerFrameForward' });
6666
msgs['playerSpeed'] = await sendMessage(Message.GET_MESSAGE, { message: 'playerSpeed' });
67+
msgs['playerCaps'] = await sendMessage(Message.GET_MESSAGE, { message: 'playerCaps' });
6768

6869
onStorageChange(changed => {
6970
console.dev.log('Options changed');
@@ -235,7 +236,16 @@ export const updatePlayerControls = (player: Player, canNext: boolean, canPrev:
235236
toggleQueueButton(player, false, canPrev);
236237
};
237238

239+
let capsHide: () => void | undefined = undefined;
238240
const keydownHandler = (e: KeyboardEvent) => {
241+
let isCaps = e.getModifierState('CapsLock');
242+
if (e.key === 'CapsLock') isCaps = !isCaps; // modifier state is updated after the event
243+
if (isCaps) capsHide = notification(msgs['playerCaps'], null);
244+
else {
245+
capsHide?.();
246+
capsHide = undefined;
247+
}
248+
239249
if (e.altKey || e.ctrlKey || e.metaKey)
240250
return;
241251
if (document.activeElement.tagName === 'INPUT')

0 commit comments

Comments
 (0)