@@ -9,8 +9,9 @@ import SwiftUI
99import PianoKeyboard
1010
1111struct ContentView : View {
12- @ObservedObject private var viewModel : PianoKeyboardViewModel
13- @State var styleIndex : Int
12+
13+ @Bindable private var viewModel : PianoKeyboardViewModel
14+ @State private var styleIndex : Int
1415
1516 private let audioEngine : AudioEngine
1617
@@ -25,91 +26,73 @@ struct ContentView: View {
2526 }
2627
2728 var body : some View {
28- VStack ( spacing: 0 ) {
29- ZStack ( alignment: . bottom) {
30- Rectangle ( )
31- . fill (
32- LinearGradient ( gradient: Gradient ( stops: [
33- Gradient . Stop ( color: Color ( white: 0.2 ) , location: 0 ) ,
34- Gradient . Stop ( color: Color ( white: 0.3 ) , location: 0.96 ) ,
35- Gradient . Stop ( color: . black, location: 1 ) ,
36- ] ) , startPoint: . top, endPoint: . bottom)
37- )
38- . shadow ( radius: 8 )
29+ GeometryReader { geometry in
30+ VStack ( spacing: 0 ) {
31+ ZStack ( alignment: . bottom) {
32+ Rectangle ( )
33+ . fill (
34+ LinearGradient ( gradient: Gradient ( stops: [
35+ Gradient . Stop ( color: Color ( white: 0.2 ) , location: 0 ) ,
36+ Gradient . Stop ( color: Color ( white: 0.3 ) , location: 0.96 ) ,
37+ Gradient . Stop ( color: . black, location: 1 ) ,
38+ ] ) , startPoint: . top, endPoint: . bottom)
39+ )
40+ . shadow ( radius: 8 )
3941
40- HStack ( alignment: . top) {
41- VStack ( alignment: . leading, spacing: 10 ) {
42- Stepper ( " Keys: \( viewModel. numberOfKeys) " ) {
43- viewModel. numberOfKeys += 1
44- } onDecrement: {
45- viewModel. numberOfKeys -= 1
46- }
47- . font ( . subheadline. bold ( ) )
48- . foregroundColor ( . white)
42+ VStack ( alignment: . trailing) {
43+ HStack ( spacing: 30 ) {
44+ Stepper ( " Keys " ) {
45+ viewModel. numberOfKeys += 1
46+ } onDecrement: {
47+ viewModel. numberOfKeys -= 1
48+ }
4949
50- Stepper ( " Style: " , value: $styleIndex, in: 0 ... 2 )
51- . font ( . subheadline. bold ( ) )
52- . foregroundColor ( . white)
53- . tint ( . blue)
50+ Stepper ( " Style " , value: $styleIndex, in: 0 ... 2 )
5451
55- }
56- . padding ( EdgeInsets ( top : 0 , leading : 20 , bottom : 30 , trailing : 0 ) )
57- . frame ( width : 200 )
52+ Toggle ( " Latch " , isOn : $viewModel . latch )
53+ . toggleStyle ( . switch )
54+ . tint ( . blue )
5855
56+ HStack {
57+ Text ( " Notes: " )
58+ Text ( " \( viewModel. keysPressed. joined ( separator: " , " ) ) " )
59+ }
5960
60- VStack {
61- Toggle ( " Latch: " , isOn: $viewModel. latch)
62- . font ( . subheadline. bold ( ) )
63- . foregroundColor ( . white)
64-
65- HStack {
66- Text ( " Notes: " )
67- . font ( . subheadline. bold ( ) )
68- . foregroundStyle ( . white)
6961 Spacer ( )
70- Text ( " \( viewModel. keysPressed. joined ( separator: " , " ) ) " )
71- . font ( . subheadline. bold ( ) )
72- . foregroundStyle ( . white)
7362 }
74- }
75- . padding ( EdgeInsets ( top: 0 , leading: 20 , bottom: 30 , trailing: 0 ) )
76- . frame ( width: 200 )
63+ . foregroundStyle ( . white)
64+ . padding ( 20 )
7765
78- Spacer ( )
66+ Spacer ( )
7967
80- Text ( " PianoKeyboard " )
81- . font ( . title2. bold ( ) )
82- . foregroundColor ( . white)
83- . padding ( 30 )
68+ Text ( " PianoKeyboard " )
69+ . font ( . title. bold ( ) )
70+ . foregroundColor ( . white)
71+ . padding ( 20 )
72+ }
8473 }
85- }
86- . frame ( height: UIScreen . main. bounds. height * 0.45 )
74+ . frame ( height: geometry. size. height * 0.45 )
8775
88- if styleIndex == 0 {
89- PianoKeyboardView ( viewModel: viewModel, style: ClassicStyle ( sfKeyWidthMultiplier: 0.55 ) )
90- . frame ( height: UIScreen . main. bounds. height * 0.55 )
91- } else if styleIndex == 1 {
92- PianoKeyboardView ( viewModel: viewModel, style: ModernStyle ( ) )
93- . frame ( height: UIScreen . main. bounds. height * 0.55 )
94- } else if styleIndex == 2 {
95- PianoKeyboardView ( viewModel: viewModel, style: CustomStyle ( showLabels: true ) )
96- . frame ( height: UIScreen . main. bounds. height * 0.55 )
76+ if styleIndex == 0 {
77+ PianoKeyboardView ( viewModel: viewModel, style: ClassicStyle ( sfKeyWidthMultiplier: 0.55 ) )
78+ . frame ( height: geometry. size. height * 0.55 )
79+ } else if styleIndex == 1 {
80+ PianoKeyboardView ( viewModel: viewModel, style: ModernStyle ( ) )
81+ . frame ( height: geometry. size. height * 0.55 )
82+ } else if styleIndex == 2 {
83+ PianoKeyboardView ( viewModel: viewModel, style: CustomStyle ( showLabels: true ) )
84+ . frame ( height: geometry. size. height * 0.55 )
85+ }
9786 }
87+ . background ( . black)
9888 }
99- . background ( . black)
10089 . onAppear ( ) {
10190 viewModel. delegate = audioEngine
10291 audioEngine. start ( )
10392 }
10493 }
10594}
10695
107- struct ContentView_Previews : PreviewProvider {
108- static var previews : some View {
109- NavigationView {
110- ContentView ( )
111- }
112- . navigationViewStyle ( . stack)
113- . previewInterfaceOrientation ( . landscapeLeft)
114- }
96+ #Preview( traits: . landscapeRight) {
97+ ContentView ( )
11598}
0 commit comments