Skip to content

Commit 58225e0

Browse files
committed
Swift formatting with swiftformat
1 parent 997b855 commit 58225e0

13 files changed

+84
-95
lines changed

Demo/Shared/ContentView.swift

+26-24
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import SwiftUI
21
import Keyboard
2+
import SwiftUI
33
import Tonic
44

55
struct ContentView: View {
6-
76
func noteOn(pitch: Pitch) {
87
print("note on \(pitch)")
98
}
@@ -12,44 +11,47 @@ struct ContentView: View {
1211
print("note off \(pitch)")
1312
}
1413

15-
var randomColors: [Color] = {
16-
(0...12).map { _ in Color(red: Double.random(in: 0...1), green: Double.random(in: 0...1), blue: Double.random(in: 0...1), opacity: 1) }
17-
}()
14+
var randomColors: [Color] = (0 ... 12).map { _ in
15+
Color(red: Double.random(in: 0 ... 1),
16+
green: Double.random(in: 0 ... 1),
17+
blue: Double.random(in: 0 ... 1), opacity: 1)
18+
}
1819

1920
@State var lowNote = 24
2021
@State var highNote = 48
2122

2223
var body: some View {
2324
HStack {
24-
Keyboard(layout: .pianoRoll(pitchRange: Pitch(48)...Pitch(77))).frame(width: 200)
25+
Keyboard(layout: .pianoRoll(pitchRange: Pitch(48) ... Pitch(77))).frame(width: 200)
2526
VStack {
2627
HStack {
2728
Stepper("Lowest Note: \(Pitch(intValue: lowNote).note(in: .C).description)",
2829
onIncrement: {
29-
if lowNote < 126 && highNote > lowNote + 12 {
30-
lowNote += 1
31-
}
32-
},
30+
if lowNote < 126, highNote > lowNote + 12 {
31+
lowNote += 1
32+
}
33+
},
3334
onDecrement: {
34-
if lowNote > 0 {
35-
lowNote -= 1
36-
}
37-
})
35+
if lowNote > 0 {
36+
lowNote -= 1
37+
}
38+
})
3839
Stepper("Highest Note: \(Pitch(intValue: highNote).note(in: .C).description)",
3940
onIncrement: {
40-
if highNote < 126 {
41-
highNote += 1
42-
}},
41+
if highNote < 126 {
42+
highNote += 1
43+
}
44+
},
4345
onDecrement: {
44-
if highNote > 1 && highNote > lowNote + 12 {
45-
highNote -= 1
46-
}
46+
if highNote > 1, highNote > lowNote + 12 {
47+
highNote -= 1
48+
}
4749

48-
})
50+
})
4951
}
50-
Keyboard(layout: .piano(pitchRange: Pitch(intValue: lowNote)...Pitch(intValue: highNote)),
52+
Keyboard(layout: .piano(pitchRange: Pitch(intValue: lowNote) ... Pitch(intValue: highNote)),
5153
noteOn: noteOn, noteOff: noteOff)
52-
Keyboard(layout: .isomorphic(pitchRange: Pitch(12)...Pitch(84)),
54+
Keyboard(layout: .isomorphic(pitchRange: Pitch(12) ... Pitch(84)),
5355
noteOn: noteOn, noteOff: noteOff)
5456
Keyboard(layout: .guitar(openPitches: [Pitch(64), Pitch(59), Pitch(55), Pitch(50), Pitch(45), Pitch(40)], fretcount: 22),
5557
noteOn: noteOn, noteOff: noteOff) { pitch, isActivated in
@@ -59,7 +61,7 @@ struct ContentView: View {
5961
pressedColor: Color(PitchColor.newtonian[Int(pitch.pitchClass)]),
6062
alignment: .center)
6163
}
62-
Keyboard(layout: .isomorphic(pitchRange: Pitch(48)...Pitch(65))) { pitch, isActivated in
64+
Keyboard(layout: .isomorphic(pitchRange: Pitch(48) ... Pitch(65))) { pitch, isActivated in
6365
KeyboardKey(pitch: pitch,
6466
isActivated: isActivated,
6567
text: pitch.note(in: .F).description,

Keyboard.playground/Contents.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import Keyboard
2-
import Tonic
3-
import SwiftUI
42
import PlaygroundSupport
3+
import SwiftUI
4+
import Tonic
55

66
struct ContentView: View {
77
var body: some View {
88
Keyboard()
99
Keyboard(layout: .isomorphic)
10-
Keyboard(pitchRange: Pitch(0)...Pitch(60+37))
10+
Keyboard(pitchRange: Pitch(0) ... Pitch(60 + 37))
1111
}
1212
}
1313

Package.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import PackageDescription
44

55
let package = Package(
66
name: "Keyboard",
7-
platforms: [ .macOS(.v12), .iOS(.v15)],
7+
platforms: [.macOS(.v12), .iOS(.v15)],
88
products: [.library(name: "Keyboard", targets: ["Keyboard"])],
99
dependencies: [.package(url: "https://github.com/AudioKit/Tonic.git", from: "1.0.0")],
1010
targets: [

Sources/Keyboard/KeyContainer.swift

+6-5
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import Tonic
44
/// This handles the interaction for key, so the user can provide their own
55
/// visual representation.
66
struct KeyContainer<Content: View>: View {
7-
let content: (Pitch, Bool)->Content
8-
7+
let content: (Pitch, Bool) -> Content
8+
99
var pitch: Pitch
1010
@ObservedObject var model: KeyboardModel
1111

@@ -16,7 +16,8 @@ struct KeyContainer<Content: View>: View {
1616
pitch: Pitch,
1717
zIndex: Int = 0,
1818
latching: Bool,
19-
@ViewBuilder content: @escaping (Pitch, Bool)->Content) {
19+
@ViewBuilder content: @escaping (Pitch, Bool) -> Content)
20+
{
2021
self.model = model
2122
self.pitch = pitch
2223
self.zIndex = zIndex
@@ -36,14 +37,14 @@ struct KeyContainer<Content: View>: View {
3637
}
3738
)
3839
.simultaneousGesture(
39-
TapGesture().onEnded({ _ in
40+
TapGesture().onEnded { _ in
4041
guard latching else { return }
4142
if model.externallyActivatedPitches.contains(pitch) {
4243
model.externallyActivatedPitches.remove(pitch)
4344
} else {
4445
model.externallyActivatedPitches.add(pitch)
4546
}
46-
})
47+
}
4748
)
4849
.preference(key: KeyRectsKey.self,
4950
value: [KeyRectInfo(rect: rect,

Sources/Keyboard/Keyboard.swift

+20-19
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,22 @@ import SwiftUI
22
import Tonic
33

44
public struct Keyboard<Content>: View where Content: View {
5-
let content: (Pitch, Bool)->Content
5+
let content: (Pitch, Bool) -> Content
66

7-
@StateObject var model: KeyboardModel = KeyboardModel()
7+
@StateObject var model: KeyboardModel = .init()
88

99
var latching: Bool
1010
var noteOn: (Pitch) -> Void
1111
var noteOff: (Pitch) -> Void
1212
var layout: KeyboardLayout
1313

14-
public init(layout: KeyboardLayout = .piano(pitchRange: (Pitch(60)...Pitch(72))),
14+
public init(layout: KeyboardLayout = .piano(pitchRange: Pitch(60) ... Pitch(72)),
1515
latching: Bool = false,
1616

1717
noteOn: @escaping (Pitch) -> Void = { _ in },
1818
noteOff: @escaping (Pitch) -> Void = { _ in },
19-
@ViewBuilder content: @escaping (Pitch, Bool)->Content) {
19+
@ViewBuilder content: @escaping (Pitch, Bool) -> Content)
20+
{
2021
self.latching = latching
2122
self.layout = layout
2223
self.noteOn = noteOn
@@ -27,13 +28,13 @@ public struct Keyboard<Content>: View where Content: View {
2728
public var body: some View {
2829
Group {
2930
switch layout {
30-
case .piano(let pitchRange):
31+
case let .piano(pitchRange):
3132
Piano(content: content, model: model, pitchRange: pitchRange, latching: latching)
32-
case .isomorphic(let pitchRange):
33+
case let .isomorphic(pitchRange):
3334
Isomorphic(content: content, model: model, pitchRange: pitchRange, latching: latching)
34-
case .guitar(let openPitches, let fretCount):
35+
case let .guitar(openPitches, fretCount):
3536
Guitar(content: content, model: model, openPitches: openPitches, fretCount: fretCount, latching: latching)
36-
case .pianoRoll(let pitchRange):
37+
case let .pianoRoll(pitchRange):
3738
PianoRoll(content: content, model: model, pitchRange: pitchRange, latching: latching)
3839
}
3940

@@ -48,12 +49,12 @@ public struct Keyboard<Content>: View where Content: View {
4849
}
4950
}
5051

51-
extension Keyboard where Content == KeyboardKey {
52-
53-
public init(layout: KeyboardLayout = .piano(pitchRange: (Pitch(60)...Pitch(72))),
54-
latching: Bool = false,
55-
noteOn: @escaping (Pitch) -> Void = { _ in },
56-
noteOff: @escaping (Pitch) -> Void = { _ in }){
52+
public extension Keyboard where Content == KeyboardKey {
53+
init(layout: KeyboardLayout = .piano(pitchRange: Pitch(60) ... Pitch(72)),
54+
latching: Bool = false,
55+
noteOn: @escaping (Pitch) -> Void = { _ in },
56+
noteOff: @escaping (Pitch) -> Void = { _ in })
57+
{
5758
self.layout = layout
5859
self.latching = latching
5960
self.noteOn = noteOn
@@ -63,15 +64,15 @@ extension Keyboard where Content == KeyboardKey {
6364

6465
var flatTop = false
6566
switch layout {
66-
case .guitar(_, _):
67+
case .guitar:
6768
alignment = .center
68-
case .isomorphic(_):
69+
case .isomorphic:
6970
alignment = .bottom
70-
case .piano(_):
71+
case .piano:
7172
flatTop = true
72-
case .pianoRoll(_):
73+
case .pianoRoll:
7374
alignment = .trailing
7475
}
75-
self.content = { KeyboardKey(pitch: $0, isActivated: $1, flatTop: flatTop, alignment: alignment) }
76+
content = { KeyboardKey(pitch: $0, isActivated: $1, flatTop: flatTop, alignment: alignment) }
7677
}
7778
}

Sources/Keyboard/KeyboardKey.swift

+4-6
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ import Tonic
33

44
/// A default visual representation for a key.
55
public struct KeyboardKey: View {
6-
7-
86
/// Initialize the keyboard key
97
/// - Parameters:
108
/// - pitch: Pitch assigned to the key
@@ -20,7 +18,8 @@ public struct KeyboardKey: View {
2018
pressedColor: Color = .red,
2119
flatTop: Bool = false,
2220
alignment: Alignment = .bottom,
23-
isActivatedExternally: Bool = false) {
21+
isActivatedExternally: Bool = false)
22+
{
2423
self.pitch = pitch
2524
self.isActivated = isActivated
2625
if text == "unset" {
@@ -78,7 +77,7 @@ public struct KeyboardKey: View {
7877

7978
let relativeTextPadding = 0.05
8079

81-
func relativeCornerRadius(in containerSize: CGSize)-> CGFloat {
80+
func relativeCornerRadius(in containerSize: CGSize) -> CGFloat {
8281
minDimension(containerSize) * 0.125
8382
}
8483

@@ -89,14 +88,13 @@ public struct KeyboardKey: View {
8988
.foregroundColor(keyColor)
9089
.padding(.top, flatTop ? relativeCornerRadius(in: proxy.size) : 0)
9190
.cornerRadius(relativeCornerRadius(in: proxy.size))
92-
.padding(.top, flatTop ? -relativeCornerRadius(in: proxy.size): 1)
91+
.padding(.top, flatTop ? -relativeCornerRadius(in: proxy.size) : 1)
9392
.padding(.trailing, 1)
9493
Text(text)
9594
.font(Font(.init(.system, size: relativeFontSize(in: proxy.size))))
9695
.foregroundColor(textColor)
9796
.padding(relativeFontSize(in: proxy.size) / 3.0)
9897
}
99-
10098
}
10199
}
102100
}

Sources/Keyboard/KeyboardModel.swift

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import SwiftUI
22
import Tonic
33

44
class KeyboardModel: ObservableObject {
5-
65
var keyRectInfos: [KeyRectInfo] = []
76
var noteOn: (Pitch) -> Void = { _ in }
87
var noteOff: (Pitch) -> Void = { _ in }
@@ -11,11 +10,11 @@ class KeyboardModel: ObservableObject {
1110
didSet {
1211
var newPitches = PitchSet()
1312
for location in touchLocations {
14-
var pitch: Pitch? = nil
13+
var pitch: Pitch?
1514
var highestZindex = -1
1615
for info in keyRectInfos where info.rect.contains(location) {
1716
if pitch == nil || info.zIndex > highestZindex {
18-
pitch = info.pitch
17+
pitch = info.pitch
1918
highestZindex = info.zIndex
2019
}
2120
}

Sources/Keyboard/Layouts/Guitar.swift

+11-13
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,24 @@ import SwiftUI
22
import Tonic
33

44
struct Guitar<Content>: View where Content: View {
5-
6-
let content: (Pitch, Bool)->Content
5+
let content: (Pitch, Bool) -> Content
76
var model: KeyboardModel
87
var openPitches: [Pitch]
98
var fretCount: Int
109
var latching: Bool
1110

1211
var body: some View {
13-
//Loop through the keys and add rows (strings)
14-
//Each row has a 5 note offset tuning them to 4ths
15-
//The pitchRange is for the lowest row (string)
12+
// Loop through the keys and add rows (strings)
13+
// Each row has a 5 note offset tuning them to 4ths
14+
// The pitchRange is for the lowest row (string)
1615
VStack(spacing: 0) {
17-
ForEach(0..<openPitches.count, id: \.self) { string in
18-
HStack(spacing: 0){
19-
ForEach(0..<fretCount + 1, id: \.self) { fret in
20-
KeyContainer(model: model,
21-
pitch: Pitch(intValue: openPitches[string].intValue + fret),
22-
latching: latching,
23-
content: content)
16+
ForEach(0 ..< openPitches.count, id: \.self) { string in
17+
HStack(spacing: 0) {
18+
ForEach(0 ..< fretCount + 1, id: \.self) { fret in
19+
KeyContainer(model: model,
20+
pitch: Pitch(intValue: openPitches[string].intValue + fret),
21+
latching: latching,
22+
content: content)
2423
}
2524
}
2625
}
@@ -29,4 +28,3 @@ struct Guitar<Content>: View where Content: View {
2928
.clipShape(Rectangle())
3029
}
3130
}
32-

Sources/Keyboard/Layouts/Isomorphic.swift

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ import SwiftUI
22
import Tonic
33

44
struct Isomorphic<Content>: View where Content: View {
5-
6-
let content: (Pitch, Bool)->Content
5+
let content: (Pitch, Bool) -> Content
76
var model: KeyboardModel
87
var pitchRange: ClosedRange<Pitch>
98
var latching: Bool
@@ -21,4 +20,3 @@ struct Isomorphic<Content>: View where Content: View {
2120
.clipShape(Rectangle())
2221
}
2322
}
24-

Sources/Keyboard/Layouts/KeyboardLayout.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import SwiftUI
22
import Tonic
33

44
/// Types of keyboards we can generate
5-
public enum KeyboardLayout: Equatable, Hashable {
5+
public enum KeyboardLayout: Equatable, Hashable {
66
/// Guitar in arbitrary tuning, from first string (highest) to loweset string
77
case guitar(openPitches: [Pitch], fretcount: Int)
88

@@ -11,7 +11,7 @@ public enum KeyboardLayout: Equatable, Hashable {
1111

1212
/// Traditional Piano layout with raised black keys over white keys
1313
case piano(pitchRange: ClosedRange<Pitch>)
14-
14+
1515
/// Vertical isomorphic
1616
case pianoRoll(pitchRange: ClosedRange<Pitch>)
1717
}

0 commit comments

Comments
 (0)