Skip to content

Commit 8eedca5

Browse files
committed
added drawing rectangles and min bar heights
1 parent a873966 commit 8eedca5

4 files changed

Lines changed: 24 additions & 8 deletions

File tree

Example/WaveformScrubberExample/ContentView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ struct ContentView: View {
2727
Section("Audio") {
2828
WaveformScrubber(
2929
config: scrubberConfig,
30-
drawer: BarDrawer(config: .init(barWidth: 2, spacing: 2), upsampleStrategy: .smooth),
30+
drawer: BarDrawer(config: .init(barWidth: 2, spacing: 2, minBarHeight: 4, cornerRadius: 2), upsampleStrategy: .smooth),
3131
url: audioURL,
3232
progress: $progress
3333
) { info in

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ A "Drawer" defines the geometric shape of the waveform. You can choose from seve
8484
The classic bar graph style.
8585
```swift
8686
WaveformScrubber(
87-
drawer: BarDrawer(config: .init(barWidth: 3, spacing: 5)),
87+
drawer: BarDrawer(config: .init(barWidth: 4, spacing: 5, minBarHeight: 4, cornerRadius: 2)),
8888
url: audioURL,
8989
progress: $progress
9090
)

Sources/WaveformScrubber/Domain/Shapes/BarDrawer/BarDrawer+Config.swift

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,31 @@ import SwiftUI
99

1010
extension BarDrawer {
1111
public struct Config: Sendable {
12-
public let barWidth: CGFloat
13-
public let spacing: CGFloat
12+
/// Width of each bar.
13+
public var barWidth: CGFloat
14+
/// Spacing between bars.
15+
public var spacing: CGFloat
16+
/// Minimum height for each bar (to ensure visibility).
17+
public var minBarHeight: CGFloat
18+
/// Corner radius for rounding bar edges.
19+
public var cornerRadius: CGFloat
1420

1521
/// Creates a new configuration for the `BarDrawer`'s appearance.
1622
/// - Parameters:
1723
/// - spacing: The distance between each waveform bar. Defaults to `2`.
1824
/// - barWidth: The width of each waveform bar. Defaults to `2`.
25+
/// - minBarHeight: Minimum height for each bar (to ensure visibility). Defaults to `1`.
26+
/// - cornerRadius: Corner radius for rounding bar edges. Defaults to `0`.
1927
public init(
2028
barWidth: CGFloat = 2,
21-
spacing: CGFloat = 2
29+
spacing: CGFloat = 2,
30+
minBarHeight: CGFloat = 1,
31+
cornerRadius: CGFloat = 0
2232
) {
2333
self.barWidth = barWidth
2434
self.spacing = spacing
35+
self.minBarHeight = minBarHeight
36+
self.cornerRadius = cornerRadius
2537
}
2638
}
2739
}

Sources/WaveformScrubber/Domain/Shapes/BarDrawer/BarDrawer.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,20 @@ public struct BarDrawer: WaveformDrawing {
2929

3030
for (index, sample) in samples.enumerated() {
3131
let x = CGFloat(index) * totalBarWidth
32-
// Ensure height is at least 1 to be visible
33-
let height = max(1, CGFloat(sample) * rect.height)
32+
let rawHeight = CGFloat(sample) * rect.height
33+
// Ensure height is at least minBarHeight to be visible
34+
let height = max(config.minBarHeight, rawHeight)
3435

3536
let barRect = CGRect(
3637
x: x,
3738
y: midY - height / 2,
3839
width: config.barWidth,
3940
height: height
4041
)
41-
path.addRect(barRect)
42+
path.addRoundedRect(
43+
in: barRect,
44+
cornerSize: CGSize(width: config.cornerRadius, height: config.cornerRadius)
45+
)
4246
}
4347
}
4448
}

0 commit comments

Comments
 (0)