Default size of hui-image to 16:9 during loading (#14245)

This commit is contained in:
Bram Kragten 2022-10-31 16:06:33 +01:00 committed by GitHub
parent 3356d559c9
commit 2e988bf5c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 7 deletions

View File

@ -24,7 +24,7 @@ import "./ha-hls-player";
import "./ha-web-rtc-player"; import "./ha-web-rtc-player";
@customElement("ha-camera-stream") @customElement("ha-camera-stream")
class HaCameraStream extends LitElement { export class HaCameraStream extends LitElement {
@property({ attribute: false }) public hass?: HomeAssistant; @property({ attribute: false }) public hass?: HomeAssistant;
@property({ attribute: false }) public stateObj?: CameraEntity; @property({ attribute: false }) public stateObj?: CameraEntity;
@ -81,7 +81,7 @@ class HaCameraStream extends LitElement {
return html``; return html``;
} }
if (__DEMO__ || this._shouldRenderMJPEG) { if (__DEMO__ || this._shouldRenderMJPEG) {
return html` <img return html`<img
.src=${__DEMO__ .src=${__DEMO__
? this.stateObj.attributes.entity_picture! ? this.stateObj.attributes.entity_picture!
: this._connected : this._connected

View File

@ -8,6 +8,7 @@ import {
TemplateResult, TemplateResult,
} from "lit"; } from "lit";
import { customElement, property, query, state } from "lit/decorators"; import { customElement, property, query, state } from "lit/decorators";
import { fireEvent } from "../common/dom/fire_event";
import { nextRender } from "../common/util/render-status"; import { nextRender } from "../common/util/render-status";
import type { HomeAssistant } from "../types"; import type { HomeAssistant } from "../types";
import "./ha-alert"; import "./ha-alert";
@ -85,6 +86,7 @@ class HaHLSPlayer extends LitElement {
.muted=${this.muted} .muted=${this.muted}
?playsinline=${this.playsInline} ?playsinline=${this.playsInline}
?controls=${this.controls} ?controls=${this.controls}
@loadeddata=${this._loadedData}
></video>` ></video>`
: ""} : ""}
`; `;
@ -318,6 +320,11 @@ class HaHLSPlayer extends LitElement {
this._errorIsFatal = false; this._errorIsFatal = false;
} }
private _loadedData() {
// @ts-ignore
fireEvent(this, "load");
}
static get styles(): CSSResultGroup { static get styles(): CSSResultGroup {
return css` return css`
:host, :host,

View File

@ -8,6 +8,7 @@ import {
} from "lit"; } from "lit";
import { customElement, property, state, query } from "lit/decorators"; import { customElement, property, state, query } from "lit/decorators";
import { isComponentLoaded } from "../common/config/is_component_loaded"; import { isComponentLoaded } from "../common/config/is_component_loaded";
import { fireEvent } from "../common/dom/fire_event";
import { handleWebRtcOffer, WebRtcAnswer } from "../data/camera"; import { handleWebRtcOffer, WebRtcAnswer } from "../data/camera";
import { fetchWebRtcSettings } from "../data/rtsp_to_webrtc"; import { fetchWebRtcSettings } from "../data/rtsp_to_webrtc";
import type { HomeAssistant } from "../types"; import type { HomeAssistant } from "../types";
@ -59,6 +60,7 @@ class HaWebRtcPlayer extends LitElement {
?playsinline=${this.playsInline} ?playsinline=${this.playsInline}
?controls=${this.controls} ?controls=${this.controls}
.poster=${this.posterUrl} .poster=${this.posterUrl}
@loadeddata=${this._loadedData}
></video> ></video>
`; `;
} }
@ -188,6 +190,11 @@ class HaWebRtcPlayer extends LitElement {
} }
} }
private _loadedData() {
// @ts-ignore
fireEvent(this, "load");
}
static get styles(): CSSResultGroup { static get styles(): CSSResultGroup {
return css` return css`
:host, :host,

View File

@ -16,6 +16,7 @@ import { CameraEntity, fetchThumbnailUrlWithCache } from "../../../data/camera";
import { UNAVAILABLE } from "../../../data/entity"; import { UNAVAILABLE } from "../../../data/entity";
import { HomeAssistant } from "../../../types"; import { HomeAssistant } from "../../../types";
import "../../../components/ha-circular-progress"; import "../../../components/ha-circular-progress";
import type { HaCameraStream } from "../../../components/ha-camera-stream";
const UPDATE_INTERVAL = 10000; const UPDATE_INTERVAL = 10000;
const DEFAULT_FILTER = "grayscale(100%)"; const DEFAULT_FILTER = "grayscale(100%)";
@ -65,9 +66,9 @@ export class HuiImage extends LitElement {
@state() private _loadedImageSrc?: string; @state() private _loadedImageSrc?: string;
private _intersectionObserver?: IntersectionObserver; @state() private _lastImageHeight?: number;
private _lastImageHeight?: number; private _intersectionObserver?: IntersectionObserver;
private _cameraUpdater?: number; private _cameraUpdater?: number;
@ -192,6 +193,8 @@ export class HuiImage extends LitElement {
style=${styleMap({ style=${styleMap({
paddingBottom: useRatio paddingBottom: useRatio
? `${((100 * this._ratio!.h) / this._ratio!.w).toFixed(2)}%` ? `${((100 * this._ratio!.h) / this._ratio!.w).toFixed(2)}%`
: !this._lastImageHeight
? "56.25%"
: undefined, : undefined,
backgroundImage: backgroundImage:
useRatio && this._loadedImageSrc useRatio && this._loadedImageSrc
@ -203,7 +206,7 @@ export class HuiImage extends LitElement {
: undefined, : undefined,
})} })}
class="container ${classMap({ class="container ${classMap({
ratio: useRatio, ratio: useRatio || !this._lastImageHeight,
})}" })}"
> >
${this.cameraImage && this.cameraView === "live" ${this.cameraImage && this.cameraView === "live"
@ -212,6 +215,7 @@ export class HuiImage extends LitElement {
muted muted
.hass=${this.hass} .hass=${this.hass}
.stateObj=${cameraObj} .stateObj=${cameraObj}
@load=${this._onVideoLoad}
></ha-camera-stream> ></ha-camera-stream>
` `
: imageSrc === undefined : imageSrc === undefined
@ -235,7 +239,7 @@ export class HuiImage extends LitElement {
id="brokenImage" id="brokenImage"
style=${styleMap({ style=${styleMap({
height: !useRatio height: !useRatio
? `${this._lastImageHeight || "100"}px` ? `${this._lastImageHeight}px` || "100%"
: undefined, : undefined,
})} })}
></div>` ></div>`
@ -245,7 +249,7 @@ export class HuiImage extends LitElement {
class="progress-container" class="progress-container"
style=${styleMap({ style=${styleMap({
height: !useRatio height: !useRatio
? `${this._lastImageHeight || "100"}px` ? `${this._lastImageHeight}px` || "100%"
: undefined, : undefined,
})} })}
> >
@ -322,6 +326,13 @@ export class HuiImage extends LitElement {
this._lastImageHeight = imgEl.offsetHeight; this._lastImageHeight = imgEl.offsetHeight;
} }
private async _onVideoLoad(ev: Event): Promise<void> {
this._loadState = LoadState.Loaded;
const videoEl = ev.currentTarget as HaCameraStream;
await this.updateComplete;
this._lastImageHeight = videoEl.offsetHeight;
}
private async _updateCameraImageSrcAtInterval(): Promise<void> { private async _updateCameraImageSrcAtInterval(): Promise<void> {
// If we hit the interval and it was still loading // If we hit the interval and it was still loading
// it means we timed out so we should show the error. // it means we timed out so we should show the error.