-
Notifications
You must be signed in to change notification settings - Fork 6.6k
/
xf_pie_progress.ts
142 lines (120 loc) · 3.29 KB
/
xf_pie_progress.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import {addCSSPrefixSelector} from '../common/js/dom_utils.js';
import {css, customElement, html, property, svg, XfBase} from './xf_base.js';
/**
* Displays a pie shaped progress indicator.
* Accepts a `progress` property ranging from 0 to 1.
*/
@customElement('xf-pie-progress')
export class XfPieProgress extends XfBase {
// This should be a number between 0 and 1.
@property({type: Number, reflect: true}) progress = 0;
private size = 16; // Size of the SVG square measured by its side length.
private center = this.size / 2.0; // Center of the pie circle (both X and Y).
private radius = 5.4; // Radius of the pie circle.
private outlineShape = svg`
<circle
class="outline"
cx="${this.center}"
cy="${this.center}"
r="${this.center}"
/>`;
private queuedShape = svg`
<circle
class="queued"
stroke-width="1.6"
cx="${this.center}"
cy="${this.center}"
r="5.6"
/>`;
private edgeShape = svg`
<circle
class="edge"
stroke-width="2"
cx="${this.center}"
cy="${this.center}"
r="${this.radius}"
/>`;
static override get styles() {
return getCSS();
}
override render() {
const {progress, size, center, radius} = this;
const isQueued = progress === 0;
// The progress pie is drawn as an arc with a thick stroke width (as thick
// as the radius of the pie).
const arcRadius = radius * 0.5;
const maxArcLength = 2.0 * Math.PI * arcRadius;
const pie = svg`
<circle class="pie"
stroke-width="${radius}"
stroke-dasharray="${maxArcLength}"
stroke-dashoffset="${maxArcLength * (1 - progress)}"
cx="${center}"
cy="${center}"
r="${arcRadius}"
transform="rotate(-90, ${center}, ${center})"
visibility="${isQueued ? 'hidden' : 'visible'}"
/>`;
const edge = isQueued ? this.queuedShape : this.edgeShape;
return html`
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${size} ${size}">
${this.outlineShape} ${pie} ${edge}
</svg>
`;
}
}
function getCSS() {
const legacyStyle = css`
svg {
height: 100%;
width: 100%;
}
.queued {
fill: none;
stroke: var(--cros-icon-color-secondary);
}
.edge {
fill: none;
stroke: var(--cros-icon-color-prominent);
}
.pie {
fill: none;
stroke: var(--cros-icon-color-prominent);
transition: stroke-dashoffset 1s ease-out;
}
.outline {
fill: var(--xf-icon-color-outline, transparent);
stroke: none;
}
`;
const refresh23Style = css`
svg {
height: 100%;
width: 100%;
}
.queued {
fill: none;
stroke: currentColor
}
.edge {
fill: none;
stroke: var(--cros-sys-progress);
}
.pie {
fill: none;
stroke: var(--cros-sys-progress);
transition: stroke-dashoffset 1s ease-out;
}
.outline {
fill: var(--xf-icon-color-outline, transparent);
stroke: none;
}
`;
return [
addCSSPrefixSelector(legacyStyle, '[theme="legacy"]'),
addCSSPrefixSelector(refresh23Style, '[theme="refresh23"]'),
];
}