Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial improve thumbnail performance #22238

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions src/components/ha-selector/ha-selector-media.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,19 +124,23 @@ export class HaMediaSelector extends LitElement {
>
${this.value?.metadata?.thumbnail
? html`
<div
<img
loading="lazy"
alt=${!this.value?.media_content_id
? this.hass.localize(
"ui.components.selectors.media.pick_media"
)
: this.value.metadata?.title ||
this.value.media_content_id}
class="${classMap({
"centered-image":
!!this.value.metadata.media_class &&
["app", "directory"].includes(
this.value.metadata.media_class
),
})}
image"
style=${this._thumbnailUrl
? `background-image: url(${this._thumbnailUrl});`
: ""}
></div>
})} image"
src=${this._thumbnailUrl ?? ""}
/>
`
: html`
<div class="icon-holder image">
Expand Down
195 changes: 122 additions & 73 deletions src/components/media-player/ha-media-player-browse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ import {
MediaPlayerBrowseAction,
MediaPlayerLayoutType,
} from "../../data/media-player";
import { browseLocalMediaPlayer } from "../../data/media_source";
import {
browseLocalMediaPlayer,
resolveMediaSource,
} from "../../data/media_source";
import { isTTSMediaSource } from "../../data/tts";
import { showAlertDialog } from "../../dialogs/generic/show-dialog-box";
import { haStyle } from "../../resources/styles";
Expand Down Expand Up @@ -86,7 +89,8 @@ export class HaMediaPlayerBrowse extends LitElement {

@property() public action: MediaPlayerBrowseAction = "play";

@property() public preferredLayout: MediaPlayerLayoutType = "auto";
@property({ reflect: true, attribute: "preferred-layout" })
public preferredLayout: MediaPlayerLayoutType = "auto";

@property({ type: Boolean }) public dialog = false;

Expand All @@ -98,6 +102,8 @@ export class HaMediaPlayerBrowse extends LitElement {
// @todo Consider reworking to eliminate need for attribute since it is manipulated internally
@property({ type: Boolean, reflect: true }) public scrolled = false;

@property({ attribute: false }) public backgroundImage: string | undefined;

@state() private _error?: { message: string; code: string };

@state() private _parentItem?: MediaPlayerItem;
Expand Down Expand Up @@ -344,10 +350,8 @@ export class HaMediaPlayerBrowse extends LitElement {
: MediaClassBrowserSettings.directory;

const backgroundImage = currentItem.thumbnail
? this._getThumbnailURLorBase64(currentItem.thumbnail).then(
(value) => `url(${value})`
)
: "none";
? this._getThumbnailURLorBase64(currentItem.thumbnail)
: undefined;

return html`
${
Expand All @@ -363,13 +367,14 @@ export class HaMediaPlayerBrowse extends LitElement {
<div class="header-content">
${currentItem.thumbnail
? html`
<div
class="img"
style="background-image: ${until(
backgroundImage,
""
)}"
>
<div class="img">
<img
alt=${currentItem.title ??
currentItem.media_content_id}
src=${until(backgroundImage, "")}
loading="lazy"
/>

${this.narrow && currentItem?.can_play
? html`
<ha-fab
Expand Down Expand Up @@ -482,7 +487,6 @@ export class HaMediaPlayerBrowse extends LitElement {
childrenMediaClass.layout === "grid")
? html`
<lit-virtualizer
scroller
.layout=${grid({
itemSize: {
width: "175px",
Expand Down Expand Up @@ -522,7 +526,6 @@ export class HaMediaPlayerBrowse extends LitElement {
: html`
<mwc-list>
<lit-virtualizer
scroller
.items=${children}
style=${styleMap({
height: `${children.length * 72 + 26}px`,
Expand Down Expand Up @@ -558,26 +561,32 @@ export class HaMediaPlayerBrowse extends LitElement {

private _renderGridItem = (child: MediaPlayerItem): TemplateResult => {
const backgroundImage = child.thumbnail
? this._getThumbnailURLorBase64(child.thumbnail).then(
(value) => `url(${value})`
)
: "none";
? this._getThumbnailURLorBase64(child.thumbnail)
: child.media_class === "image"
? resolveMediaSource(this.hass, child.media_content_id).then(
({ url }) => url
)
: undefined;

return html`
<div class="child" .item=${child} @click=${this._childClicked}>
<ha-card outlined>
<div class="thumbnail">
${child.thumbnail
${child.thumbnail || child.media_class === "image"
? html`
<div
<img
alt=${child.title}
class="${classMap({
"centered-image": ["app", "directory"].includes(
child.media_class
),
"brand-image": isBrandUrl(child.thumbnail),
"brand-image": child.thumbnail
? isBrandUrl(child.thumbnail)
: false,
})} image"
style="background-image: ${until(backgroundImage, "")}"
></div>
src=${until(backgroundImage, "")}
loading="lazy"
/>
`
: html`
<div class="icon-holder image">
Expand Down Expand Up @@ -622,20 +631,24 @@ export class HaMediaPlayerBrowse extends LitElement {
const currentItem = this._currentItem;
const mediaClass = MediaClassBrowserSettings[currentItem!.media_class];

const backgroundImage =
mediaClass.show_list_images && child.thumbnail
? this._getThumbnailURLorBase64(child.thumbnail).then(
(value) => `url(${value})`
)
: "none";
let backgroundImage;
if (mediaClass.show_list_images) {
backgroundImage = child.thumbnail
? this._getThumbnailURLorBase64(child.thumbnail)
: child.media_class === "image"
? resolveMediaSource(this.hass, child.media_content_id).then(
({ url }) => url
)
: undefined;
}

return html`
<mwc-list-item
@click=${this._childClicked}
.item=${child}
.graphic=${mediaClass.show_list_images ? "medium" : "avatar"}
>
${backgroundImage === "none" && !child.can_play
${backgroundImage === undefined && !child.can_play
? html`<ha-svg-icon
.path=${MediaClassBrowserSettings[
child.media_class === "directory"
Expand All @@ -644,28 +657,54 @@ export class HaMediaPlayerBrowse extends LitElement {
].icon}
slot="graphic"
></ha-svg-icon>`
: html`<div
class=${classMap({
graphic: true,
thumbnail: mediaClass.show_list_images === true,
})}
style="background-image: ${until(backgroundImage, "")}"
slot="graphic"
>
${child.can_play
? html`<ha-icon-button
class="play ${classMap({
show: !mediaClass.show_list_images || !child.thumbnail,
})}"
.item=${child}
.label=${this.hass.localize(
`ui.components.media-browser.${this.action}-media`
)}
.path=${this.action === "play" ? mdiPlay : mdiPlus}
@click=${this._actionClicked}
></ha-icon-button>`
: nothing}
</div>`}
: backgroundImage === undefined
? html`<ha-svg-icon
.path=${MediaClassBrowserSettings[
child.media_class === "directory"
? child.children_media_class || child.media_class
: child.media_class
].icon}
slot="graphic"
></ha-svg-icon>
${child.can_play
? html`<ha-icon-button
class="play ${classMap({
show: !mediaClass.show_list_images || !child.thumbnail,
})}"
.item=${child}
.label=${this.hass.localize(
`ui.components.media-browser.${this.action}-media`
)}
.path=${this.action === "play" ? mdiPlay : mdiPlus}
@click=${this._actionClicked}
></ha-icon-button>`
: nothing}`
: html`<div
class=${classMap({
graphic: true,
thumbnail: mediaClass.show_list_images === true,
})}
slot="graphic"
>
<img
alt=${child.title}
src=${until(backgroundImage, "")}
loading="lazy"
/>
${child.can_play
? html`<ha-icon-button
class="play ${classMap({
show: !mediaClass.show_list_images || !child.thumbnail,
})}"
.item=${child}
.label=${this.hass.localize(
`ui.components.media-browser.${this.action}-media`
)}
.path=${this.action === "play" ? mdiPlay : mdiPlus}
@click=${this._actionClicked}
></ha-icon-button>`
: nothing}
</div>`}
<span class="title">${child.title}</span>
</mwc-list-item>
`;
Expand Down Expand Up @@ -909,10 +948,17 @@ export class HaMediaPlayerBrowse extends LitElement {

.content {
overflow-y: auto;
box-sizing: border-box;
height: 100%;
}

:host([preferred-layout="list"]) .content {
align-items: center;
display: flex;
flex-direction: column;
box-sizing: border-box;
padding: 4px;
}

/* HEADER */

.header {
Expand Down Expand Up @@ -1016,6 +1062,8 @@ export class HaMediaPlayerBrowse extends LitElement {
--mdc-list-item-graphic-margin: 0;
--mdc-theme-text-icon-on-background: var(--secondary-text-color);
margin-top: 10px;
width: 100%;
max-width: 1120px;
}

mwc-list li:last-child {
Expand Down Expand Up @@ -1072,27 +1120,27 @@ export class HaMediaPlayerBrowse extends LitElement {
}

ha-card .image {
border-radius: 3px 3px 0 0;
border-radius: var(--ha-card-border-radius, 12px);
padding: 4px;
box-sizing: border-box;
}

.image {
position: absolute;
top: 0;
right: 0;
left: 0;
bottom: 0;
background-size: cover;
background-repeat: no-repeat;
background-position: center;
:host([preferred-layout="list"]) img {
height: 56px;
width: 56px;
}

.centered-image {
margin: 0 8px;
background-size: contain;
.image {
max-height: 173px;
width: 100%;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}

.brand-image {
background-size: 40%;
width: 40%;
}

.children ha-card .icon-holder {
Expand Down Expand Up @@ -1163,17 +1211,19 @@ export class HaMediaPlayerBrowse extends LitElement {
padding-left: 16px;
}

mwc-list-item .graphic {
background-size: contain;
background-repeat: no-repeat;
background-position: center;
mwc-list-item .graphic,
mwc-list-item .graphic img {
border-radius: 2px;
display: flex;
align-content: center;
align-items: center;
line-height: initial;
}

mwc-list-item .graphic img {
position: absolute;
}

mwc-list-item .graphic .play {
opacity: 0;
transition: all 0.5s;
Expand Down Expand Up @@ -1319,7 +1369,6 @@ export class HaMediaPlayerBrowse extends LitElement {

lit-virtualizer {
height: 100%;
overflow: overlay !important;
contain: size layout !important;
}

Expand Down
Loading