Update quick bar (#8823)

This commit is contained in:
Bram Kragten 2021-04-06 18:05:29 +02:00 committed by GitHub
parent 4843ee80a7
commit cafbea9c42
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -3,7 +3,14 @@ import type { List } from "@material/mwc-list/mwc-list";
import { SingleSelectedEvent } from "@material/mwc-list/mwc-list-foundation"; import { SingleSelectedEvent } from "@material/mwc-list/mwc-list-foundation";
import "@material/mwc-list/mwc-list-item"; import "@material/mwc-list/mwc-list-item";
import type { ListItem } from "@material/mwc-list/mwc-list-item"; import type { ListItem } from "@material/mwc-list/mwc-list-item";
import { mdiConsoleLine, mdiEarth, mdiReload, mdiServerNetwork } from "@mdi/js"; import {
mdiClose,
mdiConsoleLine,
mdiEarth,
mdiMagnify,
mdiReload,
mdiServerNetwork,
} from "@mdi/js";
import { import {
css, css,
customElement, customElement,
@ -11,7 +18,6 @@ import {
internalProperty, internalProperty,
LitElement, LitElement,
property, property,
PropertyValues,
query, query,
} from "lit-element"; } from "lit-element";
import { ifDefined } from "lit-html/directives/if-defined"; import { ifDefined } from "lit-html/directives/if-defined";
@ -85,8 +91,6 @@ export class QuickBar extends LitElement {
@internalProperty() private _entityItems?: EntityItem[]; @internalProperty() private _entityItems?: EntityItem[];
@internalProperty() private _items?: QuickBarItem[] = [];
@internalProperty() private _filter = ""; @internalProperty() private _filter = "";
@internalProperty() private _search = ""; @internalProperty() private _search = "";
@ -97,7 +101,7 @@ export class QuickBar extends LitElement {
@internalProperty() private _done = false; @internalProperty() private _done = false;
@query("search-input", false) private _filterInputField?: HTMLElement; @query("paper-input", false) private _filterInputField?: HTMLElement;
private _focusSet = false; private _focusSet = false;
@ -113,25 +117,22 @@ export class QuickBar extends LitElement {
this._focusSet = false; this._focusSet = false;
this._filter = ""; this._filter = "";
this._search = ""; this._search = "";
this._items = [];
fireEvent(this, "dialog-closed", { dialog: this.localName }); fireEvent(this, "dialog-closed", { dialog: this.localName });
} }
protected updated(changedProperties: PropertyValues) {
if (
this._opened &&
(changedProperties.has("_filter") ||
changedProperties.has("_commandMode"))
) {
this._setFilteredItems();
}
}
protected render() { protected render() {
if (!this._opened) { if (!this._opened) {
return html``; return html``;
} }
let items: QuickBarItem[] | undefined = this._commandMode
? this._commandItems
: this._entityItems;
if (items && this._filter && this._filter !== " ") {
items = this._filterItems(items || [], this._filter);
}
return html` return html`
<ha-dialog <ha-dialog
.heading=${true} .heading=${true}
@ -140,7 +141,7 @@ export class QuickBar extends LitElement {
@closed=${this.closeDialog} @closed=${this.closeDialog}
hideActions hideActions
> >
<search-input <paper-input
dialogInitialFocus dialogInitialFocus
no-label-float no-label-float
slot="heading" slot="heading"
@ -149,7 +150,7 @@ export class QuickBar extends LitElement {
.label=${this.hass.localize( .label=${this.hass.localize(
"ui.dialogs.quick-bar.filter_placeholder" "ui.dialogs.quick-bar.filter_placeholder"
)} )}
.filter=${this._commandMode ? `>${this._search}` : this._search} .value=${this._commandMode ? `>${this._search}` : this._search}
@keydown=${this._handleInputKeyDown} @keydown=${this._handleInputKeyDown}
@focus=${this._setFocusFirstListItem} @focus=${this._setFocusFirstListItem}
> >
@ -159,9 +160,23 @@ export class QuickBar extends LitElement {
class="prefix" class="prefix"
.path=${mdiConsoleLine} .path=${mdiConsoleLine}
></ha-svg-icon>` ></ha-svg-icon>`
: ""} : html`<ha-svg-icon
</search-input> slot="prefix"
${!this._items class="prefix"
.path=${mdiMagnify}
></ha-svg-icon>`}
${this._search &&
html`
<mwc-icon-button
slot="suffix"
@click=${this._clearSearch}
title="Clear"
>
<ha-svg-icon .path=${mdiClose}></ha-svg-icon>
</mwc-icon-button>
`}
</paper-input>
${!items
? html`<ha-circular-progress ? html`<ha-circular-progress
size="small" size="small"
active active
@ -172,13 +187,13 @@ export class QuickBar extends LitElement {
@selected=${this._handleSelected} @selected=${this._handleSelected}
style=${styleMap({ style=${styleMap({
height: `${Math.min( height: `${Math.min(
this._items.length * (this._commandMode ? 56 : 72) + 26, items.length * (this._commandMode ? 56 : 72) + 26,
this._done ? 500 : 0 this._done ? 500 : 0
)}px`, )}px`,
})} })}
> >
${scroll({ ${scroll({
items: this._items, items,
renderItem: (item: QuickBarItem, index?: number) => renderItem: (item: QuickBarItem, index?: number) =>
this._renderItem(item, index), this._renderItem(item, index),
})} })}
@ -196,7 +211,6 @@ export class QuickBar extends LitElement {
} }
private _handleOpened() { private _handleOpened() {
this._setFilteredItems();
this.updateComplete.then(() => { this.updateComplete.then(() => {
this._done = true; this._done = true;
}); });
@ -302,11 +316,11 @@ export class QuickBar extends LitElement {
private _handleInputKeyDown(ev: KeyboardEvent) { private _handleInputKeyDown(ev: KeyboardEvent) {
if (ev.code === "Enter") { if (ev.code === "Enter") {
if (!this._items?.length) { const firstItem = this._getItemAtIndex(0);
if (!firstItem || firstItem.style.display === "none") {
return; return;
} }
this.processItemAndCloseDialog((firstItem as any).item, 0);
this.processItemAndCloseDialog(this._items[0], 0);
} else if (ev.code === "ArrowDown") { } else if (ev.code === "ArrowDown") {
ev.preventDefault(); ev.preventDefault();
this._getItemAtIndex(0)?.focus(); this._getItemAtIndex(0)?.focus();
@ -338,16 +352,20 @@ export class QuickBar extends LitElement {
this._search = newFilter; this._search = newFilter;
} }
this._debouncedSetFilter(this._search);
if (oldCommandMode !== this._commandMode) { if (oldCommandMode !== this._commandMode) {
this._items = undefined;
this._focusSet = false; this._focusSet = false;
this._initializeItemsIfNeeded(); this._initializeItemsIfNeeded();
this._filter = this._search;
} else {
this._debouncedSetFilter(this._search);
} }
} }
private _clearSearch() {
this._search = "";
this._filter = "";
}
private _debouncedSetFilter = debounce((filter: string) => { private _debouncedSetFilter = debounce((filter: string) => {
this._filter = filter; this._filter = filter;
}, 100); }, 100);
@ -553,16 +571,10 @@ export class QuickBar extends LitElement {
return this._opened ? !this._commandMode : false; return this._opened ? !this._commandMode : false;
} }
private _setFilteredItems() {
const items = this._commandMode ? this._commandItems : this._entityItems;
this._items = this._filter
? this._filterItems(items || [], this._filter)
: items;
}
private _filterItems = memoizeOne( private _filterItems = memoizeOne(
(items: QuickBarItem[], filter: string): QuickBarItem[] => (items: QuickBarItem[], filter: string): QuickBarItem[] => {
fuzzyFilterSort<QuickBarItem>(filter.trimLeft(), items) return fuzzyFilterSort<QuickBarItem>(filter.trimLeft(), items);
}
); );
static get styles() { static get styles() {
@ -598,27 +610,26 @@ export class QuickBar extends LitElement {
color: var(--primary-text-color); color: var(--primary-text-color);
} }
span.command-category { paper-input mwc-icon-button {
font-weight: bold; --mdc-icon-button-size: 24px;
padding: 3px; color: var(--primary-text-color);
display: inline-flex; }
border-radius: 6px;
color: black; .command-category {
--ha-chip-icon-color: #585858;
--ha-chip-text-color: #212121;
} }
.command-category.reload { .command-category.reload {
--ha-chip-background-color: #cddc39; --ha-chip-background-color: #cddc39;
--ha-chip-text-color: black;
} }
.command-category.navigation { .command-category.navigation {
--ha-chip-background-color: var(--light-primary-color); --ha-chip-background-color: var(--light-primary-color);
--ha-chip-text-color: black;
} }
.command-category.server_control { .command-category.server_control {
--ha-chip-background-color: var(--warning-color); --ha-chip-background-color: var(--warning-color);
--ha-chip-text-color: black;
} }
span.command-text { span.command-text {