Skip to content

Commit

Permalink
Add "borders" option
Browse files Browse the repository at this point in the history
The `borders` config option is added which allows setting a rendered
border for bar `span` elements. This makes them more visible when used
without colors on things like e-ink displays.
  • Loading branch information
wrouesnel committed Apr 7, 2024
1 parent 13b4958 commit db28d58
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ decimal by 1). Otherwise, the integration may complain of a duplicate unique ID.
| `offset` | number | **Optional** | Number of forecast segments to offset from start | `0` |
| `label_spacing` | number | **Optional** | Space between time/temperature labels (integer >= 1) | `2` |
| `colors` | [object][color] | **Optional** | Set colors for all or some conditions | |
| `borders` | [color] | **Optional** | Set color for a border around segments. Defaults to null (no border) | |
| `hide_hours` | bool | **Optional** | Whether to hide hour labels under the bar | `false` |
| `hide_temperatures` | bool | **Optional** | Whether to hide temperatures under the bar | `false` |
| `round_temperatures` | bool | **Optional** | Whether to round temperatures to the nearest whole number | `false` |
Expand Down
13 changes: 13 additions & 0 deletions cypress/e2e/weather-bar.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,19 @@ describe('Weather bar', () => {
expect(cs.color).to.eq(expectedCustomObjectColors[i].fg);
});
});

it('draws borders when a border color is specified', () => {
cy.configure({
borders: 'limegreen'
});
cy.get('weather-bar')
.shadow()
.find('div.bar > div.bar-span')
.each((el) => {
cy.wrap(el).should('have.css', 'border-color', 'rgb(50, 205, 50)');
});
});

describe('Labels', () => {
it('shows text descriptions on condition blocks', () => {
cy.get('weather-bar')
Expand Down
1 change: 1 addition & 0 deletions src/hourly-weather.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,7 @@ export class HourlyWeatherCard extends LitElement {
.precipitation=${precipitation}
.icons=${!!config.icons}
.colors=${colorSettings.validColors}
.borders=${config.borders}
.hide_hours=${!!config.hide_hours}
.hide_temperatures=${!!config.hide_temperatures}
.round_temperatures=${!!config.round_temperatures}
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface HourlyWeatherCardConfig extends LovelaceCardConfig {
icons?: boolean;
offset?: string; // number
colors?: ColorConfig;
borders?: ColorDefinition;
hide_bar?: boolean;
hide_hours?: boolean;
hide_temperatures?: boolean;
Expand Down
30 changes: 28 additions & 2 deletions src/weather-bar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { StyleInfo, styleMap } from 'lit/directives/style-map.js';
import tippy, { Instance } from 'tippy.js';
import { LABELS, ICONS } from "./conditions";
import { getWindBarbSVG } from "./lib/svg-wind-barbs";
import type { ColorMap, ConditionSpan, SegmentTemperature, SegmentWind, SegmentPrecipitation, WindType, ShowDateType } from "./types";
import type { ColorMap, ConditionSpan, SegmentTemperature, SegmentWind, SegmentPrecipitation, WindType, ShowDateType, ColorDefinition } from "./types";

const tippyStyles: string = process.env.TIPPY_CSS || '';

Expand All @@ -27,6 +27,9 @@ export class WeatherBar extends LitElement {
@property({ attribute: false })
colors: ColorMap | undefined = void 0;

@property({ attribute: false })
borders: ColorDefinition | undefined = void 0;

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

Expand Down Expand Up @@ -69,8 +72,9 @@ export class WeatherBar extends LitElement {
if (icon === cond[0]) icon = 'mdi:weather-' + icon;
else icon = 'mdi:' + icon;
const barStyles: Readonly<StyleInfo> = { gridColumnStart: String(gridStart), gridColumnEnd: String(gridStart += cond[1] * 2) };
const classname = `${cond[0]} bar-span`;
conditionBars.push(html`
<div class=${cond[0]} style=${styleMap(barStyles)} data-tippy-content=${label}>
<div class=${classname} style=${styleMap(barStyles)} data-tippy-content=${label}>
${this.icons ?
html`<span class="condition-icon"><ha-icon icon=${icon}></ha-icon></span>` :
html`<span class="condition-label">${label}</span>`}
Expand Down Expand Up @@ -150,9 +154,16 @@ export class WeatherBar extends LitElement {
colorStyles = this.getColorStyles(this.colors);
}

let borderStyles: TemplateResult | null = null;
if (this.borders) {
borderStyles = this.getBarSpanStyles(this.borders);
}
console.log(borderStyles);

return html`
<div class="main">
${colorStyles ?? null}
${borderStyles ?? null}
${this.hide_bar ? null : html`<div class="bar">${conditionBars}</div>`}
<div class="axes">${barBlocks}</div>
</div>
Expand Down Expand Up @@ -186,6 +197,15 @@ export class WeatherBar extends LitElement {
</style>`;
}

private getBarSpanStyles(borders: ColorDefinition): TemplateResult | null {
if (!borders) return null;
return html`<style>
.main > div.bar > div.bar-span {
border-color: ${borders};
}
</style>`;
}

private getWindBarb(speed: number, direction: number): TemplateResult {
const svgStyles = {
transform: `rotate(${direction}deg)`
Expand Down Expand Up @@ -308,6 +328,11 @@ export class WeatherBar extends LitElement {
background-color: var(--color-exceptional);
color: var(--color-exceptional-foreground, var(--primary-text-color));
}
.bar-span {
border-style: solid;
border-width: 1px;
border-color: transparent
}
.axes {
display: grid;
grid-auto-flow: column;
Expand All @@ -331,6 +356,7 @@ export class WeatherBar extends LitElement {
grid-area: right;
border: 1px solid var(--divider-color, lightgray);
border-width: 0 0 0 1px;
border-collapse: collapse;
}
.bar-block-bottom {
text-align: center;
Expand Down

0 comments on commit db28d58

Please sign in to comment.