Skip to content

Commit

Permalink
Improve card size algorithm for grids/stacks
Browse files Browse the repository at this point in the history
  • Loading branch information
karwosts committed Sep 2, 2023
1 parent fe3a63a commit f82d652
Show file tree
Hide file tree
Showing 12 changed files with 31 additions and 26 deletions.
4 changes: 2 additions & 2 deletions src/panels/lovelace/cards/hui-area-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,8 @@ export class HuiAreaCard
];
}

public getCardSize(): number {
return 3;
public getCardSize(hScale?: number): number {
return 3 * (hScale || 1);
}

public setConfig(config: AreaCardConfig): void {
Expand Down
5 changes: 3 additions & 2 deletions src/panels/lovelace/cards/hui-button-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,10 @@ export class HuiButtonCard extends LitElement implements LovelaceCard {
);
}

public getCardSize(): number {
public getCardSize(hScale?: number): number {
return (
(this._config?.show_icon ? 4 : 0) + (this._config?.show_name ? 1 : 0)
(this._config?.show_icon ? 4 * (hScale || 1) : 0) +
(this._config?.show_name ? 1 : 0)
);
}

Expand Down
7 changes: 4 additions & 3 deletions src/panels/lovelace/cards/hui-grid-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ class HuiGridCard extends HuiStackCard<GridCardConfig> {
return document.createElement("hui-grid-card-editor");
}

public async getCardSize(): Promise<number> {
public async getCardSize(hScale?: number): Promise<number> {
if (!this._cards || !this._config) {
return 0;
}

if (this.square) {
const rowHeight = SQUARE_ROW_HEIGHTS_BY_COLUMNS[this.columns] || 1;
const rowHeight =
(SQUARE_ROW_HEIGHTS_BY_COLUMNS[this.columns] || 1) * (hScale || 1);
return (
(this._cards.length < this.columns
? rowHeight
Expand All @@ -35,7 +36,7 @@ class HuiGridCard extends HuiStackCard<GridCardConfig> {
const promises: Array<Promise<number> | number> = [];

for (const element of this._cards) {
promises.push(computeCardSize(element));
promises.push(computeCardSize(element, (hScale || 1) / this.columns));
}

const cardSizes = await Promise.all(promises);
Expand Down
6 changes: 4 additions & 2 deletions src/panels/lovelace/cards/hui-horizontal-stack-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ import { computeCardSize } from "../common/compute-card-size";
import { HuiStackCard } from "./hui-stack-card";

class HuiHorizontalStackCard extends HuiStackCard {
public async getCardSize(): Promise<number> {
public async getCardSize(hScale?: number): Promise<number> {
if (!this._cards) {
return 0;
}

const promises: Array<Promise<number> | number> = [];

for (const element of this._cards) {
promises.push(computeCardSize(element));
promises.push(
computeCardSize(element, (hScale || 1) / this._cards.length)
);
}

const results = await Promise.all(promises);
Expand Down
6 changes: 3 additions & 3 deletions src/panels/lovelace/cards/hui-map-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ class HuiMapCard extends LitElement implements LovelaceCard {
).map((entity) => entity.entity);
}

public getCardSize(): number {
public getCardSize(hScale?: number): number {
if (!this._config?.aspect_ratio) {
return 7;
return 7 * (hScale || 1);
}

const ratio = parseAspectRatio(this._config.aspect_ratio);
Expand All @@ -112,7 +112,7 @@ class HuiMapCard extends LitElement implements LovelaceCard {
? `${((100 * ratio.h) / ratio.w).toFixed(2)}`
: "100";

return 1 + Math.floor(Number(ar) / 25) || 3;
return (1 + Math.floor(Number(ar) / 25) || 3) * (hScale || 1);
}

public static async getConfigElement() {
Expand Down
4 changes: 2 additions & 2 deletions src/panels/lovelace/cards/hui-picture-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ export class HuiPictureCard extends LitElement implements LovelaceCard {

@property() protected _config?: PictureCardConfig;

public getCardSize(): number {
return 5;
public getCardSize(hScale?: number): number {
return 5 * (hScale || 1);
}

public setConfig(config: PictureCardConfig): void {
Expand Down
4 changes: 2 additions & 2 deletions src/panels/lovelace/cards/hui-picture-elements-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ class HuiPictureElementsCard extends LitElement implements LovelaceCard {

@state() private _config?: PictureElementsCardConfig;

public getCardSize(): number {
return 4;
public getCardSize(hScale?: number): number {
return 4 * (hScale || 1);
}

public setConfig(config: PictureElementsCardConfig): void {
Expand Down
4 changes: 2 additions & 2 deletions src/panels/lovelace/cards/hui-picture-glance-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ class HuiPictureGlanceCard extends LitElement implements LovelaceCard {

private _entitiesToggle?: PictureGlanceEntityConfig[];

public getCardSize(): number {
return 3;
public getCardSize(hScale?: number): number {
return 3 * (hScale || 1);
}

public setConfig(config: PictureGlanceCardConfig): void {
Expand Down
4 changes: 2 additions & 2 deletions src/panels/lovelace/cards/hui-statistics-graph-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ export class HuiStatisticsGraphCard extends LitElement implements LovelaceCard {
this._setFetchStatisticsTimer();
}

public getCardSize(): number {
public getCardSize(hScale?: number): number {
return (
5 +
5 * (hScale || 1) +
(this._config?.title ? 2 : 0) +
(!this._config?.hide_legend ? this._entities?.length || 0 : 0)
);
Expand Down
4 changes: 2 additions & 2 deletions src/panels/lovelace/cards/hui-vertical-stack-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import { computeCardSize } from "../common/compute-card-size";
import { HuiStackCard } from "./hui-stack-card";

class HuiVerticalStackCard extends HuiStackCard {
public async getCardSize() {
public async getCardSize(hScale?: number) {
if (!this._cards) {
return 0;
}

const promises: Array<Promise<number> | number> = [];

for (const element of this._cards) {
promises.push(computeCardSize(element));
promises.push(computeCardSize(element, hScale));
}

const results = await Promise.all(promises);
Expand Down
7 changes: 4 additions & 3 deletions src/panels/lovelace/common/compute-card-size.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import { promiseTimeout } from "../../../common/util/promise-timeout";
import { LovelaceCard, LovelaceHeaderFooter } from "../types";

export const computeCardSize = (
card: LovelaceCard | LovelaceHeaderFooter
card: LovelaceCard | LovelaceHeaderFooter,
hScale: number = 1
): number | Promise<number> => {
if (typeof card.getCardSize === "function") {
try {
return promiseTimeout(500, card.getCardSize()).catch(
return promiseTimeout(500, card.getCardSize(hScale)).catch(
() => 1
) as Promise<number>;
} catch (_e: any) {
Expand All @@ -18,5 +19,5 @@ export const computeCardSize = (
}
return customElements
.whenDefined(card.localName)
.then(() => computeCardSize(card));
.then(() => computeCardSize(card, hScale));
};
2 changes: 1 addition & 1 deletion src/panels/lovelace/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export interface LovelaceCard extends HTMLElement {
hass?: HomeAssistant;
isPanel?: boolean;
editMode?: boolean;
getCardSize(): number | Promise<number>;
getCardSize(hScale?: number): number | Promise<number>;
setConfig(config: LovelaceCardConfig): void;
}

Expand Down

0 comments on commit f82d652

Please sign in to comment.