Remove dynamic height/width calcs on graph nodes (#8832)

This commit is contained in:
Bram Kragten 2021-04-06 22:31:22 +02:00 committed by GitHub
parent 109910d18f
commit 3a4fffdb0b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 77 additions and 54 deletions

View File

@ -10,8 +10,6 @@ export class HatGraphNode extends LitElement {
@property({ reflect: true, type: Boolean }) graphstart?: boolean; @property({ reflect: true, type: Boolean }) graphstart?: boolean;
@property({ reflect: true, type: Boolean }) spacer?: boolean;
@property({ reflect: true, type: Boolean }) nofocus?: boolean; @property({ reflect: true, type: Boolean }) nofocus?: boolean;
@property({ reflect: true, type: Number }) badge?: number; @property({ reflect: true, type: Number }) badge?: number;
@ -22,34 +20,18 @@ export class HatGraphNode extends LitElement {
this.setAttribute("tabindex", "0"); this.setAttribute("tabindex", "0");
} }
updated() {
const svgEl = this.shadowRoot?.querySelector("svg");
if (!svgEl) {
return;
}
if (this.spacer) {
svgEl.setAttribute("width", "10px");
svgEl.setAttribute("height", "41px");
svgEl.setAttribute("viewBox", "-5 -40 10 26");
return;
}
const bbox = svgEl.getBBox();
const extra_height = this.graphstart ? 2 : 1;
const extra_width = SPACING;
svgEl.setAttribute("width", `${bbox.width + extra_width}px`);
svgEl.setAttribute("height", `${bbox.height + extra_height}px`);
svgEl.setAttribute(
"viewBox",
`${Math.ceil(bbox.x - extra_width / 2)}
${Math.ceil(bbox.y - extra_height / 2)}
${bbox.width + extra_width}
${bbox.height + extra_height}`
);
}
render() { render() {
const height = NODE_SIZE + (this.graphstart ? 2 : SPACING + 1);
const width = SPACING + NODE_SIZE;
return svg` return svg`
<svg <svg
width="${width}px"
height="${height}px"
viewBox="-${Math.ceil(width / 2)} -${
this.graphstart
? Math.ceil(height / 2)
: Math.ceil((NODE_SIZE + SPACING * 2) / 2)
} ${width} ${height}"
> >
${ ${
this.graphstart this.graphstart
@ -58,11 +40,7 @@ export class HatGraphNode extends LitElement {
<path <path
class="connector" class="connector"
d=" d="
M 0 ${ M 0 ${-SPACING - NODE_SIZE / 2}
this.spacer
? -SPACING * 2 - NODE_SIZE
: -SPACING - NODE_SIZE / 2
}
L 0 0 L 0 0
" "
line-caps="round" line-caps="round"
@ -70,14 +48,11 @@ export class HatGraphNode extends LitElement {
` `
} }
<g class="node"> <g class="node">
${ <circle
!this.spacer
? svg`<circle
cx="0" cx="0"
cy="0" cy="0"
r="${NODE_SIZE / 2}" r="${NODE_SIZE / 2}"
/>` />
: ""
} }
${ ${
this.badge this.badge
@ -114,16 +89,6 @@ export class HatGraphNode extends LitElement {
:host { :host {
display: flex; display: flex;
flex-direction: column; flex-direction: column;
--stroke-clr: var(--stroke-color, var(--secondary-text-color));
--active-clr: var(--active-color, var(--primary-color));
--track-clr: var(--track-color, var(--accent-color));
--hover-clr: var(--hover-color, var(--primary-color));
--disabled-clr: var(--disabled-color, var(--disabled-text-color));
--default-trigger-color: 3, 169, 244;
--rgb-trigger-color: var(--trigger-color, var(--default-trigger-color));
--background-clr: var(--background-color, white);
--default-icon-clr: var(--icon-color, black);
--icon-clr: var(--stroke-clr);
} }
:host(.track) { :host(.track) {
--stroke-clr: var(--track-clr); --stroke-clr: var(--track-clr);
@ -146,13 +111,11 @@ export class HatGraphNode extends LitElement {
:host-context([disabled]) { :host-context([disabled]) {
--stroke-clr: var(--disabled-clr); --stroke-clr: var(--disabled-clr);
} }
:host([nofocus]):host-context(.active), :host([nofocus]):host-context(.active),
:host([nofocus]):host-context(:focus) { :host([nofocus]):host-context(:focus) {
--circle-clr: var(--active-clr); --circle-clr: var(--active-clr);
--icon-clr: var(--default-icon-clr); --icon-clr: var(--default-icon-clr);
} }
circle, circle,
path.connector { path.connector {
stroke: var(--stroke-clr); stroke: var(--stroke-clr);

View File

@ -0,0 +1,55 @@
import { css, customElement, LitElement, property, svg } from "lit-element";
import { NODE_SIZE, SPACING } from "./hat-graph";
@customElement("hat-graph-spacer")
export class HatGraphSpacer extends LitElement {
@property({ reflect: true, type: Boolean }) disabled?: boolean;
render() {
return svg`
<svg
width="${SPACING}px"
height="${SPACING + NODE_SIZE + 1}px"
viewBox="-${SPACING / 2} 0 10 ${SPACING + NODE_SIZE + 1}"
>
<path
class="connector"
d="
M 0 ${SPACING + NODE_SIZE + 1}
L 0 0
"
line-caps="round"
/>
}
</svg>
`;
}
static get styles() {
return css`
:host {
display: flex;
flex-direction: column;
}
:host(.track) {
--stroke-clr: var(--track-clr);
--icon-clr: var(--default-icon-clr);
}
:host-context([disabled]) {
--stroke-clr: var(--disabled-clr);
}
path.connector {
stroke: var(--stroke-clr);
stroke-width: 2;
fill: none;
}
`;
}
}
declare global {
interface HTMLElementTagNameMap {
"hat-graph-spacer": HatGraphSpacer;
}
}

View File

@ -171,7 +171,13 @@ export class HatGraph extends LitElement {
--stroke-clr: var(--stroke-color, var(--secondary-text-color)); --stroke-clr: var(--stroke-color, var(--secondary-text-color));
--active-clr: var(--active-color, var(--primary-color)); --active-clr: var(--active-color, var(--primary-color));
--track-clr: var(--track-color, var(--accent-color)); --track-clr: var(--track-color, var(--accent-color));
--disabled-clr: var(--disabled-color, gray); --hover-clr: var(--hover-color, var(--primary-color));
--disabled-clr: var(--disabled-color, var(--disabled-text-color));
--default-trigger-color: 3, 169, 244;
--rgb-trigger-color: var(--trigger-color, var(--default-trigger-color));
--background-clr: var(--background-color, white);
--default-icon-clr: var(--icon-color, black);
--icon-clr: var(--stroke-clr);
} }
:host(:focus) { :host(:focus) {
outline: none; outline: none;

View File

@ -49,6 +49,7 @@ import {
WaitForTriggerAction, WaitForTriggerAction,
} from "../../data/script"; } from "../../data/script";
import { ensureArray } from "../../common/ensure-array"; import { ensureArray } from "../../common/ensure-array";
import "./hat-graph-spacer";
declare global { declare global {
interface HASSDomEvents { interface HASSDomEvents {
@ -192,14 +193,12 @@ class HatScriptGraph extends LitElement {
`; `;
})} })}
<hat-graph> <hat-graph>
<hat-graph-node <hat-graph-spacer
nofocus
spacer
class=${classMap({ class=${classMap({
track: track:
trace !== undefined && trace[0].result?.choice === "default", trace !== undefined && trace[0].result?.choice === "default",
})} })}
></hat-graph-node> ></hat-graph-spacer>
${ensureArray(config.default)?.map((action, i) => ${ensureArray(config.default)?.map((action, i) =>
this.render_node(action, `${path}/default/${i}`) this.render_node(action, `${path}/default/${i}`)
)} )}