From 39a4d85de34f5b33a63ccbe546ecdb5497000666 Mon Sep 17 00:00:00 2001 From: tsm-odoo Date: Mon, 11 Sep 2023 10:53:18 +0200 Subject: [PATCH] [FIX] mail: fix push to talk key detection Before this commit, the push to talk key combination was wrongly detected. Indeed, it was dependent on which key was pressed first. Steps to reproduce: - Register "ctrl + alt" as the push to talk key - Press control then alt, push to talk does not work - Press alt then control, push to talk works - If you unpress the alt key, the push to talk will stop as expected - If you unpress the control key, the push to talk will keep going This commit fixes the issue. closes odoo/odoo#134981 X-original-commit: 3e4ed2a43ad23116c095af67cb7e5736a9854820 Signed-off-by: Thanh Son Dodeur (tso) Signed-off-by: Matthieu Stockbauer (tsm) --- .../src/core/common/user_settings_service.js | 48 ++++++++++++++----- .../src/discuss/call/common/call_settings.js | 5 +- .../src/discuss/call/common/rtc_service.js | 2 +- 3 files changed, 40 insertions(+), 15 deletions(-) diff --git a/addons/mail/static/src/core/common/user_settings_service.js b/addons/mail/static/src/core/common/user_settings_service.js index eb32b744220..84e898584af 100644 --- a/addons/mail/static/src/core/common/user_settings_service.js +++ b/addons/mail/static/src/core/common/user_settings_service.js @@ -108,9 +108,13 @@ export class UserSettings { * @param {event} ev */ async setPushToTalkKey(ev) { - const pushToTalkKey = `${ev.shiftKey || ""}.${ev.ctrlKey || ev.metaKey || ""}.${ + const nonElligibleKeys = new Set(["Shift", "Control", "Alt", "Meta"]); + let pushToTalkKey = `${ev.shiftKey || ""}.${ev.ctrlKey || ev.metaKey || ""}.${ ev.altKey || "" - }.${ev.key === " " ? "Space" : ev.key}`; + }`; + if (!nonElligibleKeys.has(ev.key)) { + pushToTalkKey += `.${ev.key === " " ? "Space" : ev.key}`; + } this.pushToTalkKey = pushToTalkKey; this._saveSettings(); } @@ -149,25 +153,43 @@ export class UserSettings { // methods + buildKeySet({ shiftKey, ctrlKey, altKey, key }) { + const keys = new Set(); + if (key) { + keys.add(key === "Meta" ? "Alt" : key); + } + if (shiftKey) { + keys.add("Shift"); + } + if (ctrlKey) { + keys.add("Control"); + } + if (altKey) { + keys.add("Alt"); + } + return keys; + } + /** * @param {event} ev * @param {Object} param1 - * @param {boolean} param1.ignoreModifiers */ - isPushToTalkKey(ev, { ignoreModifiers = false } = {}) { + isPushToTalkKey(ev) { if (!this.usePushToTalk || !this.pushToTalkKey) { return false; } - const { key, shiftKey, ctrlKey, altKey } = this.pushToTalkKeyFormat(); - if (ignoreModifiers) { - return ev.key === key; + const [shiftKey, ctrlKey, altKey, key] = this.pushToTalkKey.split("."); + const settingsKeySet = this.buildKeySet({ shiftKey, ctrlKey, altKey, key }); + const eventKeySet = this.buildKeySet({ + shiftKey: ev.shiftKey, + ctrlKey: ev.ctrlKey, + altKey: ev.altKey, + key: ev.key, + }); + if (ev.type === "keydown") { + return [...settingsKeySet].every((key) => eventKeySet.has(key)); } - return ( - ev.key === key && - ev.shiftKey === shiftKey && - ev.ctrlKey === ctrlKey && - ev.altKey === altKey - ); + return settingsKeySet.has(ev.key === "Meta" ? "Alt" : ev.key); } pushToTalkKeyFormat() { if (!this.pushToTalkKey) { diff --git a/addons/mail/static/src/discuss/call/common/call_settings.js b/addons/mail/static/src/discuss/call/common/call_settings.js index a771daaf14a..42e2e98830f 100644 --- a/addons/mail/static/src/discuss/call/common/call_settings.js +++ b/addons/mail/static/src/discuss/call/common/call_settings.js @@ -39,7 +39,10 @@ export class CallSettings extends Component { get pushToTalkKeyText() { const { shiftKey, ctrlKey, altKey, key } = this.userSettings.pushToTalkKeyFormat(); const f = (k, name) => (k ? name : ""); - return `${f(ctrlKey, "Ctrl + ")}${f(altKey, "Alt + ")}${f(shiftKey, "Shift + ")}${key}`; + const keys = [f(ctrlKey, "Ctrl"), f(altKey, "Alt"), f(shiftKey, "Shift"), key].filter( + Boolean + ); + return keys.join(" + "); } _onKeyDown(ev) { diff --git a/addons/mail/static/src/discuss/call/common/rtc_service.js b/addons/mail/static/src/discuss/call/common/rtc_service.js index 7671220dfb0..ed403f4e799 100644 --- a/addons/mail/static/src/discuss/call/common/rtc_service.js +++ b/addons/mail/static/src/discuss/call/common/rtc_service.js @@ -214,7 +214,7 @@ export class Rtc { if ( !this.state.channel || !this.userSettingsService.usePushToTalk || - !this.userSettingsService.isPushToTalkKey(ev, { ignoreModifiers: true }) || + !this.userSettingsService.isPushToTalkKey(ev) || !this.state.selfSession.isTalking ) { return;