DFSegmentBar.swift
1.51 KB
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
//
// DFSegmentBar.swift
// hippo-watch-extension
//
import SwiftUI
struct DFSegmentBar: View {
let selected: DFStressStatus?
private let kinds: [DFStressStatus] = [.overloaded, .little, .normal, .excellent]
var body: some View {
if selected == nil {
Capsule()
.fill(DFWatchFaceColor.grayBar)
} else {
GeometryReader { proxy in
HStack(spacing: 2) {
ForEach(kinds, id: \.rawValue) { kind in
Capsule()
.fill(kind.color)
.frame(width: segmentWidth(kind: kind, totalWidth: proxy.size.width))
.overlay {
if selected == kind {
Capsule()
.stroke(kind.color, lineWidth: 3)
.background(Capsule().fill(Color.black))
.frame(width: 10, height: 10)
}
}
}
}
}
}
}
private func segmentWidth(kind: DFStressStatus, totalWidth: CGFloat) -> CGFloat {
guard let selected else {
return totalWidth
}
let spacing: CGFloat = 6
let extra: CGFloat = totalWidth * 0.18
let base = (totalWidth - spacing - extra) / CGFloat(kinds.count)
return selected == kind ? base + extra : base
}
}