-
Notifications
You must be signed in to change notification settings - Fork 5
fix: improved wind indicator font and tip visibility #31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
17a64e1
fix: improved wind indicator arrow visibility
troinine d2b27e7
test: add tests for undefined wind_bearing and wind_speed
troinine a4b5261
fix: use correct bearing in aria-label
troinine 4327720
fix: improve wind indicator speed and tip visibility
troinine b39db93
test: fix review comments
troinine c877830
fix: scale tip based on the indicator size
troinine 330b62a
fix: scle dy based on font
troinine File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,112 +1,108 @@ | ||
| import { LitElement, html, css } from "lit"; | ||
| import { LitElement, html, css, TemplateResult, nothing } from "lit"; | ||
| import { customElement, property } from "lit/decorators.js"; | ||
| import { ExtendedHomeAssistant } from "../types"; | ||
| import { | ||
| ForecastAttribute, | ||
| getNormalizedWindSpeed, | ||
| WeatherEntity, | ||
| } from "../data/weather"; | ||
|
|
||
| @customElement("wfc-wind-indicator") | ||
| export class WfcWindIndicator extends LitElement { | ||
| @property({ attribute: false }) windBearing = 0; | ||
| @property({ attribute: false }) windSpeed = 0; | ||
| @property({ attribute: false }) windUnit = "m/s"; | ||
| @property({ attribute: false }) hass!: ExtendedHomeAssistant; | ||
| @property({ attribute: false }) weatherEntity!: WeatherEntity; | ||
| @property({ attribute: false }) forecast!: ForecastAttribute; | ||
| @property({ attribute: false }) size = 35; | ||
| @property({ attribute: false }) radius = 15; | ||
| @property({ attribute: false }) type: "bearing" | "direction" = "bearing"; | ||
|
|
||
| static styles = css` | ||
| :host { | ||
| display: inline-block; | ||
| line-height: 0; | ||
| } | ||
| text { | ||
| font-size: calc(var(--ha-font-size-s, 12px) * 1.2); | ||
| font-size: calc(var(--ha-font-size-s, 12px) * 1.1); | ||
| fill: var(--primary-text-color); | ||
| font-weight: 500; | ||
| dominant-baseline: central; | ||
| } | ||
| `; | ||
|
|
||
| render() { | ||
| protected render(): TemplateResult | typeof nothing { | ||
| if (!this.hass || !this.forecast || !this.weatherEntity) { | ||
| return nothing; | ||
| } | ||
|
|
||
| const windSpeed = this.forecast.wind_speed || 0; | ||
| const windBearing = this.forecast.wind_bearing || 0; | ||
|
|
||
| const R = this.radius; | ||
| const padding = 8; | ||
| const tipOffset = 7; | ||
| const strokeWidth = 3; | ||
| const padding = 10; | ||
troinine marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const cx = R + padding; | ||
| const cy = R + padding; | ||
| const boxSize = 2 * (R + padding); | ||
|
|
||
| const speed = Math.round(this.windSpeed || 0); | ||
| const lineColor = computeLineColor(speed, this.windUnit); | ||
| let bearing = this.windBearing || 0; | ||
| const speed = Math.round(windSpeed); | ||
| const lineColor = this.computeLineColor(); | ||
|
|
||
| let bearing = windBearing; | ||
| if (this.type === "direction") { | ||
| bearing = (bearing + 180) % 360; | ||
| } | ||
|
|
||
| const polar = (deg: number, r: number) => { | ||
| const a = (deg * Math.PI) / 180; | ||
| return { | ||
| x: cx + r * Math.cos(a), | ||
| y: cy + r * Math.sin(a), | ||
| }; | ||
| }; | ||
|
|
||
| const baseAngle = -90; | ||
| const spread = 12; | ||
| const p1 = polar(baseAngle - spread, R); | ||
| const p2 = polar(baseAngle + spread, R); | ||
| const tip = polar(baseAngle, R + tipOffset); | ||
| const baseY = cy - R; | ||
| const tipY = baseY - 8; | ||
| const spreadX = 7; | ||
|
|
||
| return html` | ||
| <svg | ||
| width="${this.size}" | ||
| height="${this.size}" | ||
| viewBox="0 0 ${boxSize} ${boxSize}" | ||
| role="img" | ||
| aria-label="Wind speed: ${speed}, bearing: ${windBearing} degrees" | ||
troinine marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| > | ||
| <circle | ||
| cx="${cx}" | ||
| cy="${cy}" | ||
| r="${R}" | ||
| stroke="${lineColor}" | ||
| stroke-width="3" | ||
| stroke-width="${strokeWidth}" | ||
| fill="none" | ||
| /> | ||
|
|
||
| <g transform="rotate(${bearing} ${cx} ${cy})"> | ||
| <polygon | ||
| points=" | ||
| ${p1.x},${p1.y} | ||
| ${p2.x},${p2.y} | ||
| ${tip.x},${tip.y} | ||
| ${cx - spreadX},${baseY} | ||
| ${cx + spreadX},${baseY} | ||
| ${cx},${tipY} | ||
| " | ||
| fill="${lineColor}" | ||
| /> | ||
| </g> | ||
|
|
||
| <text x="${cx}" y="${cy + 5}" text-anchor="middle">${speed}</text> | ||
| <text x="${cx}" y="${cy}" text-anchor="middle">${speed}</text> | ||
| </svg> | ||
| `; | ||
| } | ||
| } | ||
|
|
||
| const computeLineColor = (windSpeed: number, windUnit: string): string => { | ||
| const unit = windUnit.toLowerCase(); | ||
|
|
||
| const multipliers: Record<string, number> = { | ||
| "km/h": 1 / 3.6, | ||
| kmh: 1 / 3.6, | ||
| mph: 0.44704, | ||
| kn: 0.514444, | ||
| kt: 0.514444, | ||
| knot: 0.514444, | ||
| knots: 0.514444, | ||
| }; | ||
| private computeLineColor(): string { | ||
| const speedMS = getNormalizedWindSpeed( | ||
| this.hass, | ||
| this.weatherEntity, | ||
| this.forecast | ||
| ); | ||
|
|
||
| const multiplier = multipliers[unit] ?? 1; | ||
| const speedMS = windSpeed * multiplier; | ||
|
|
||
| if (speedMS <= 3) { | ||
| return "var(--wfc-wind-low)"; | ||
| } | ||
|
|
||
| if (speedMS <= 8) { | ||
| return "var(--wfc-wind-medium)"; | ||
| } | ||
|
|
||
| return "var(--wfc-wind-high)"; | ||
| }; | ||
| if (!speedMS || speedMS <= 3) { | ||
| return "var(--wfc-wind-low)"; | ||
| } | ||
| if (speedMS <= 8) { | ||
| return "var(--wfc-wind-medium)"; | ||
| } | ||
|
|
||
| declare global { | ||
| interface HTMLElementTagNameMap { | ||
| "wfc-wind-indicator": WfcWindIndicator; | ||
| return "var(--wfc-wind-high)"; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.