-
Notifications
You must be signed in to change notification settings - Fork 9
/
ChordsView.swift
153 lines (147 loc) · 6.13 KB
/
ChordsView.swift
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
143
144
145
146
147
148
149
150
151
152
153
//
// ChordsView.swift
// Chord Provider
//
// © 2024 Nick Berendsen
//
import SwiftUI
import SwiftlyChordUtilities
/// SwiftUI `View` for the chord diagrams
struct ChordsView: View {
/// The ChordPro document
@Binding var document: ChordProDocument
/// The app state
@Environment(AppState.self) private var appState
/// The scene state
@Environment(SceneState.self) private var sceneState
/// Sheet with chords of the selected type
@State var selectedChord: ChordDefinition?
/// A new chord definition in the sheet
@State var defineChord: ChordDefinition?
/// The body of the `View`
var body: some View {
let layout = sceneState.songDisplayOptions.chordsPosition == .bottom ?
AnyLayout(HStackLayout(spacing: 0)) : AnyLayout(VStackLayout(spacing: 0))
ScrollView([sceneState.songDisplayOptions.chordsPosition == .bottom ? .horizontal : .vertical]) {
layout {
ForEach(sceneState.song.chords.sorted(using: KeyPathComparator(\.name))) { chord in
switch chord.status {
case .standardChord, .transposedChord:
Button(
action: {
selectedChord = chord
},
label: {
VStack(spacing: 0) {
ChordDiagramView(chord: chord, width: 120)
}
}
)
.buttonStyle(.plain)
.scrollTransition { effect, phase in
effect
.scaleEffect(phase.isIdentity ? 1 : 0.6)
}
default:
ChordDiagramView(chord: chord, width: 120)
.scrollTransition { effect, phase in
effect
.scaleEffect(phase.isIdentity ? 1 : 0.6)
}
}
}
}
}
.sheet(item: $selectedChord) { _ in
chordsSheet
}
}
}
extension ChordsView {
/// Present a Sheet with chord definitions for the selected chord name
@ViewBuilder var chordsSheet: some View {
if let selectedChord {
VStack {
Text("Chord: \(selectedChord.displayName(options: .init()))")
.font(.title)
HStack {
ForEach(selectedChord.quality.intervals.intervals, id: \.self) { interval in
Text(interval.description)
}
}
ScrollView {
LazyVGrid(
columns: [GridItem(.adaptive(minimum: 140))],
alignment: .center,
spacing: 4,
pinnedViews: [.sectionHeaders, .sectionFooters]
) {
ForEach(getChordDefinitions()) { chord in
Button(action: {
self.defineChord = selected(chord: chord) ? nil : chord
}, label: {
ChordDiagramView(chord: chord, width: 140)
})
.buttonStyle(.plain)
.padding(.horizontal)
.background(
self.defineChord == chord ? .accent.opacity(0.3) : .clear
)
.background(
selected(chord: chord) ? .accent.opacity(0.1) : .clear
)
.cornerRadius(10)
}
}
}
HStack {
Button(
action: {
self.defineChord = nil
self.selectedChord = nil
},
label: {
Text( self.defineChord == nil ? "Close" : "Cancel")
}
)
.keyboardShortcut(.defaultAction)
Button(
action: {
if let chord = self.defineChord {
if let range = document.text.range(of: selectedChord.define) {
document.text = document.text.replacingCharacters(in: range, with: chord.define)
} else {
document.text += "\n\n{define: \(chord.define)}"
}
}
self.defineChord = nil
self.selectedChord = nil
},
label: {
Text("Use Definition")
}
)
.disabled(self.defineChord == nil)
}
}
.padding()
.frame(minWidth: 600, idealWidth: 600, minHeight: 600, idealHeight: 600)
.animation(.default, value: self.defineChord)
}
}
/// Get all chord definitions for the selected chord name
/// - Returns: An array of chord definitions`
private func getChordDefinitions() -> [ChordDefinition] {
guard let selectedChord else {
return []
}
let allChords = Chords.getAllChordsForInstrument(instrument: selectedChord.instrument)
return allChords.matching(root: selectedChord.root).matching(quality: selectedChord.quality).matching(bass: selectedChord.bass)
}
/// Check of a chord in the sheet is the currrent selected chord
/// - Parameter chord: The chord definition
/// - Returns: True when current selection; else false
private func selected(chord: ChordDefinition) -> Bool {
chord.frets == selectedChord?.frets && chord.baseFret == selectedChord?.baseFret
}
}