diff --git a/src/panels/profile/ha-enable-shortcuts-row.ts b/src/panels/profile/ha-enable-shortcuts-row.ts new file mode 100644 index 0000000000..8fb362c76b --- /dev/null +++ b/src/panels/profile/ha-enable-shortcuts-row.ts @@ -0,0 +1,51 @@ +import { + customElement, + html, + LitElement, + property, + TemplateResult, +} from "lit-element"; +import { fireEvent } from "../../common/dom/fire_event"; +import "../../components/ha-switch"; +import type { HaSwitch } from "../../components/ha-switch"; +import type { HomeAssistant } from "../../types"; +import "../../components/ha-settings-row"; + +@customElement("ha-enable-shortcuts-row") +class HaEnableShortcutsRow extends LitElement { + @property({ attribute: false }) public hass!: HomeAssistant; + + @property() public narrow!: boolean; + + protected render(): TemplateResult { + return html` + + + ${this.hass.localize("ui.panel.profile.enable_shortcuts.header")} + + + ${this.hass.localize("ui.panel.profile.enable_shortcuts.description")} + + + + `; + } + + private async _checkedChanged(ev: Event) { + const enabled = (ev.target as HaSwitch).checked; + if (enabled === this.hass.enableShortcuts) { + return; + } + + fireEvent(this, "hass-enable-shortcuts", enabled); + } +} + +declare global { + interface HTMLElementTagNameMap { + "ha-enable-shortcuts-row": HaEnableShortcutsRow; + } +} diff --git a/src/panels/profile/ha-panel-profile.ts b/src/panels/profile/ha-panel-profile.ts index 3c8e6ab6a4..9ce8a0bb6e 100644 --- a/src/panels/profile/ha-panel-profile.ts +++ b/src/panels/profile/ha-panel-profile.ts @@ -38,6 +38,7 @@ import "./ha-push-notifications-row"; import "./ha-refresh-tokens-card"; import "./ha-set-suspend-row"; import "./ha-set-vibrate-row"; +import "./ha-enable-shortcuts-row"; class HaPanelProfile extends LitElement { @property({ attribute: false }) public hass!: HomeAssistant; @@ -161,7 +162,10 @@ class HaPanelProfile extends LitElement { .narrow=${this.narrow} .hass=${this.hass} > - +
${this.hass.localize("ui.panel.profile.logout")} diff --git a/src/state/connection-mixin.ts b/src/state/connection-mixin.ts index 7af421029d..9862ed3bb3 100644 --- a/src/state/connection-mixin.ts +++ b/src/state/connection-mixin.ts @@ -48,6 +48,7 @@ export const connectionMixin = >( dockedSidebar: "docked", vibrate: true, suspendWhenHidden: true, + enableShortcuts: true, moreInfoEntityId: null, hassUrl: (path = "") => new URL(path, auth.data.hassUrl).toString(), callService: async (domain, service, serviceData = {}) => { diff --git a/src/state/quick-bar-mixin.ts b/src/state/quick-bar-mixin.ts index cfefcb388c..c7a8844285 100644 --- a/src/state/quick-bar-mixin.ts +++ b/src/state/quick-bar-mixin.ts @@ -4,10 +4,13 @@ import { QuickBarParams, showQuickBar, } from "../dialogs/quick-bar/show-dialog-quick-bar"; +import { HomeAssistant } from "../types"; +import { storeState } from "../util/ha-pref-storage"; declare global { interface HASSDomEvents { "hass-quick-bar": QuickBarParams; + "hass-enable-shortcuts": HomeAssistant["enableShortcuts"]; } } @@ -18,12 +21,17 @@ export default >(superClass: T) => protected firstUpdated(changedProps: PropertyValues) { super.firstUpdated(changedProps); + this.addEventListener("hass-enable-shortcuts", (ev) => { + this._updateHass({ enableShortcuts: ev.detail }); + storeState(this.hass!); + }); + this._registerShortcut(); } private _registerShortcut() { document.addEventListener("keydown", (e: KeyboardEvent) => { - if (!this.hass?.user?.is_admin) { + if (!this.hass?.user?.is_admin || !this.hass.enableShortcuts) { return; } if (this.isOSCtrlKey(e) && e.code === "KeyP") { diff --git a/src/translations/en.json b/src/translations/en.json index 4136d5b2ea..760327e3be 100755 --- a/src/translations/en.json +++ b/src/translations/en.json @@ -2666,6 +2666,10 @@ "header": "Vibrate", "description": "Enable or disable vibration on this device when controlling devices." }, + "enable_shortcuts": { + "header": "Keyboard Shortcuts", + "description": "Enable or disable keyboard shortcuts for performing various actions in the UI." + }, "suspend": { "header": "Automatically close connection", "description": "Should we close the connection to the server after being hidden for 5 minutes?" diff --git a/src/types.ts b/src/types.ts index 4092ac6c6a..5f302d4895 100644 --- a/src/types.ts +++ b/src/types.ts @@ -237,6 +237,7 @@ export interface HomeAssistant { localize: LocalizeFunc; translationMetadata: TranslationMetadata; suspendWhenHidden: boolean; + enableShortcuts: boolean; vibrate: boolean; dockedSidebar: "docked" | "always_hidden" | "auto"; defaultPanel: string; diff --git a/src/util/ha-pref-storage.ts b/src/util/ha-pref-storage.ts index bb06674f35..8c21de00b7 100644 --- a/src/util/ha-pref-storage.ts +++ b/src/util/ha-pref-storage.ts @@ -6,6 +6,7 @@ const STORED_STATE = [ "selectedLanguage", "vibrate", "suspendWhenHidden", + "enableShortcuts", "defaultPanel", ]; const STORAGE = window.localStorage || {};