Skip to content

Commit f1f5144

Browse files
add lineCap param to circular progress
1 parent 5ca7b7d commit f1f5144

File tree

6 files changed

+59
-5
lines changed

6 files changed

+59
-5
lines changed

Examples/DemosApp/DemosApp/ComponentsPreview/PreviewPages/CircularProgressPreview.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,22 @@ struct CircularProgressPreview: View {
3737
Form {
3838
ComponentColorPicker(selection: self.$model.color)
3939
CaptionFontPicker(selection: self.$model.font)
40+
Picker("Line Cap", selection: self.$model.lineCap) {
41+
Text("Butt").tag(CircularProgressVM.LineCap.butt)
42+
Text("Rounded").tag(CircularProgressVM.LineCap.rounded)
43+
Text("Square").tag(CircularProgressVM.LineCap.square)
44+
}
4045
Picker("Line Width", selection: self.$model.lineWidth) {
4146
Text("Default").tag(Optional<CGFloat>.none)
4247
Text("2").tag(Optional<CGFloat>.some(2))
4348
Text("4").tag(Optional<CGFloat>.some(4))
4449
Text("8").tag(Optional<CGFloat>.some(8))
4550
}
46-
SizePicker(selection: self.$model.size)
4751
Picker("Shape", selection: self.$model.shape) {
4852
Text("Circle").tag(CircularProgressVM.Shape.circle)
4953
Text("Arc").tag(CircularProgressVM.Shape.arc)
5054
}
55+
SizePicker(selection: self.$model.size)
5156
}
5257
.onReceive(self.timer) { _ in
5358
if self.currentValue < self.model.maxValue {
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import SwiftUI
2+
import UIKit
3+
4+
extension CircularProgressVM {
5+
/// Defines the style of line endings.
6+
public enum LineCap {
7+
/// The line ends exactly at the endpoint with a flat edge.
8+
case butt
9+
/// The line ends with a semicircular arc that extends beyond the endpoint, creating a rounded appearance.
10+
case rounded
11+
/// The line ends with a square cap that extends beyond the endpoint by half the line width, similar to `butt` but with a protruding end.
12+
case square
13+
}
14+
}
15+
16+
// MARK: - UIKit Helpers
17+
18+
extension CircularProgressVM.LineCap {
19+
var shapeLayerLineCap: CAShapeLayerLineCap {
20+
switch self {
21+
case .butt:
22+
return .butt
23+
case .rounded:
24+
return .round
25+
case .square:
26+
return .square
27+
}
28+
}
29+
}
30+
31+
// MARK: - SwiftUI Helpers
32+
33+
extension CircularProgressVM.LineCap {
34+
var cgLineCap: CGLineCap {
35+
switch self {
36+
case .butt:
37+
return .butt
38+
case .rounded:
39+
return .round
40+
case .square:
41+
return .square
42+
}
43+
}
44+
}

Sources/ComponentsKit/Components/CircularProgress/Models/CircularProgressShape.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import Foundation
33
extension CircularProgressVM {
44
/// Defines the shapes for the circular progress component.
55
public enum Shape {
6+
/// Renders a complete circle to represent the progress.
67
case circle
8+
/// Renders only a portion of the circle (an arc) to represent progress.
79
case arc
810
}
911
}

Sources/ComponentsKit/Components/CircularProgress/Models/CircularProgressVM.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ public struct CircularProgressVM: ComponentVM {
1313
/// An optional label to display inside the circular progress.
1414
public var label: String?
1515

16+
/// The style of line endings.
17+
public var lineCap: LineCap = .rounded
18+
1619
/// The width of the circular progress stroke.
1720
public var lineWidth: CGFloat?
1821

Sources/ComponentsKit/Components/CircularProgress/SUCircularProgress.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public struct SUCircularProgress: View {
4646
self.model.color.background.color,
4747
style: StrokeStyle(
4848
lineWidth: self.model.circularLineWidth,
49-
lineCap: .round
49+
lineCap: self.model.lineCap.cgLineCap
5050
)
5151
)
5252
.frame(
@@ -69,7 +69,7 @@ public struct SUCircularProgress: View {
6969
self.model.color.main.color,
7070
style: StrokeStyle(
7171
lineWidth: self.model.circularLineWidth,
72-
lineCap: .round
72+
lineCap: self.model.lineCap.cgLineCap
7373
)
7474
)
7575
.frame(

Sources/ComponentsKit/Components/CircularProgress/UKCircularProgress.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ extension UKCircularProgress {
182182
) {
183183
layer.fillColor = UIColor.clear.cgColor
184184
layer.strokeColor = model.color.background.cgColor
185-
layer.lineCap = .round
185+
layer.lineCap = model.lineCap.shapeLayerLineCap
186186
layer.lineWidth = model.circularLineWidth
187187
}
188188

@@ -192,7 +192,7 @@ extension UKCircularProgress {
192192
) {
193193
layer.fillColor = UIColor.clear.cgColor
194194
layer.strokeColor = model.color.main.uiColor.cgColor
195-
layer.lineCap = .round
195+
layer.lineCap = model.lineCap.shapeLayerLineCap
196196
layer.lineWidth = model.circularLineWidth
197197
}
198198

0 commit comments

Comments
 (0)