Style custom buttons based on actions.

This commit is contained in:
Dermot Duffy
2022-04-29 22:12:36 -07:00
parent 22dc35e024
commit 2f74b8d37c
2 changed files with 52 additions and 3 deletions
+51 -2
View File
@@ -21,10 +21,11 @@ import {
ActionType, ActionType,
CameraConfig, CameraConfig,
FrigateCardView, FrigateCardView,
FrigateCardCustomAction,
FRIGATE_CARD_VIEWS_USER_SPECIFIED,
RawFrigateCardConfig, RawFrigateCardConfig,
entitySchema, entitySchema,
frigateCardConfigSchema, frigateCardConfigSchema,
FrigateCardCustomAction,
} from './types.js'; } from './types.js';
import type { import type {
Entity, Entity,
@@ -271,6 +272,48 @@ export class FrigateCard extends LitElement {
}; };
} }
/**
* Given a button determine if the style should be emphasized by examining all
* of the actions sequentially.
* @param button The button to examine.
* @returns A StyleInfo object.
*/
protected _getStyleFromActions(button: MenuButton): StyleInfo {
for (const actionSet of [
button.tap_action,
button.double_tap_action,
button.hold_action,
button.start_tap_action,
button.end_tap_action,
]) {
const actions = Array.isArray(actionSet) ? actionSet : [actionSet];
for (const action of actions) {
// All frigate card actions will have action of 'fire-dom-event' and
// styling only applies to those.
if (!action || action.action !== 'fire-dom-event') {
continue;
}
if (
FRIGATE_CARD_VIEWS_USER_SPECIFIED.some(
(view) =>
view === action?.frigate_card_action &&
this._view?.is(action.frigate_card_action),
) ||
(action?.frigate_card_action === 'default' &&
this._view?.is(this._getConfig().view.default)) ||
(action?.frigate_card_action === 'fullscreen' &&
screenfull.isEnabled &&
screenfull.isFullscreen) ||
(action?.frigate_card_action === 'camera_select' &&
this._view?.camera === action.camera)
) {
return this._getEmphasizedStyle();
}
}
}
return {};
}
/** /**
* Get the menu buttons to display. * Get the menu buttons to display.
* @returns An array of menu buttons. * @returns An array of menu buttons.
@@ -417,7 +460,13 @@ export class FrigateCard extends LitElement {
style: screenfull.isFullscreen ? this._getEmphasizedStyle() : {}, style: screenfull.isFullscreen ? this._getEmphasizedStyle() : {},
}); });
} }
return buttons.concat(this._dynamicMenuButtons);
const styledDynamicButtons = this._dynamicMenuButtons.map((button) => ({
style: this._getStyleFromActions(button),
...button,
}));
return buttons.concat(styledDynamicButtons);
} }
/** /**
+1 -1
View File
@@ -34,7 +34,7 @@ declare global {
* Internal types. * Internal types.
*/ */
const FRIGATE_CARD_VIEWS_USER_SPECIFIED = [ export const FRIGATE_CARD_VIEWS_USER_SPECIFIED = [
'live', 'live',
'clip', 'clip',
'clips', 'clips',