Skip to content

Commit

Permalink
fix: Show a background image in case media doesn't load (#1622)
Browse files Browse the repository at this point in the history
  • Loading branch information
dermotduffy authored Oct 6, 2024
1 parent ba90e1d commit d7fa293
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/card-controller/card-element-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export class CardElementManager {
this._api.getMediaLoadedInfoManager().initialize();
this._api.getMicrophoneManager().initialize();
this._api.getKeyboardStateManager().initialize();
this._api.getStyleManager().initialize();

// These initializers are called when the config is updated, but on initial
// creation of the card hass is not yet available when the config is first
Expand Down
12 changes: 12 additions & 0 deletions src/card-controller/style-manager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { StyleInfo } from 'lit/directives/style-map';
import { FrigateCardConfig } from '../config/types';
import irisLogo from '../images/camera-iris.svg';
import { aspectRatioToStyle, setOrRemoveAttribute } from '../utils/basic';
import { View } from '../view/view';
import { CardStyleAPI } from './types';
Expand All @@ -11,6 +12,10 @@ export class StyleManager {
this._api = api;
}

public initialize(): void {
this._setCommonStyleProperties();
}

public setLightOrDarkMode = (): void => {
const config = this._api.getConfigManager().getConfig();
const isDarkMode =
Expand Down Expand Up @@ -133,4 +138,11 @@ export class StyleManager {
}
return aspectRatioToStyle({ defaultStatic: true });
}

protected _setCommonStyleProperties(): void {
this._api
.getCardElementManager()
.getElement()
.style.setProperty('--frigate-card-media-background-image', `url("${irisLogo}")`);
}
}
1 change: 1 addition & 0 deletions src/card-controller/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ export interface CardElementAPI {
getMediaLoadedInfoManager(): MediaLoadedInfoManager;
getMediaPlayerManager(): MediaPlayerManager;
getMicrophoneManager(): MicrophoneManager;
getStyleManager(): StyleManager;
getQueryStringManager(): QueryStringManager;
}

Expand Down
1 change: 1 addition & 0 deletions src/scss/image.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@use 'media-background.scss';
@use 'media-layout.scss';

img {
Expand Down
2 changes: 2 additions & 0 deletions src/scss/live-provider.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@use 'media-background.scss';

:host {
display: block;
height: 100%;
Expand Down
6 changes: 6 additions & 0 deletions src/scss/media-background.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
:host {
background-position: center;
background-repeat: no-repeat;
background-image: var(--frigate-card-media-background-image);
background-size: 25%;
}
1 change: 1 addition & 0 deletions src/scss/viewer-provider.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@use 'media-background.scss';
@use 'media-layout.scss';

:host {
Expand Down
1 change: 1 addition & 0 deletions tests/card-controller/card-element-manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ describe('CardElementManager', () => {
expect(api.getExpandManager().initialize).toBeCalled();
expect(api.getMediaLoadedInfoManager().initialize).toBeCalled();
expect(api.getMicrophoneManager().initialize).toBeCalled();
expect(api.getStyleManager().initialize).toBeCalled();
});

it('should disconnect', () => {
Expand Down
18 changes: 17 additions & 1 deletion tests/card-controller/style-manager.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { FrigateCardView } from '../../src/config/types';
import { StyleManager } from '../../src/card-controller/style-manager';
import { FrigateCardView } from '../../src/config/types';
import irisLogo from '../../src/images/camera-iris.svg';
import { createCardAPI, createConfig, createHASS, createView } from '../test-utils';

// @vitest-environment jsdom
Expand All @@ -9,6 +10,21 @@ describe('StyleManager', () => {
vi.resetAllMocks();
});

describe('initialize should set common properties', () => {
it('should set media background', () => {
const api = createCardAPI();
const element = document.createElement('div');
vi.mocked(api.getCardElementManager().getElement).mockReturnValue(element);
const manager = new StyleManager(api);

manager.initialize();

expect(
element.style.getPropertyValue('--frigate-card-media-background-image'),
).toEqual(`url("${irisLogo}")`);
});
});

describe('setLightOrDarkMode', () => {
it('dark mode unspecified', () => {
const api = createCardAPI();
Expand Down

0 comments on commit d7fa293

Please sign in to comment.