Skip to content

Commit 497f706

Browse files
committed
Handle alignment
1 parent 7ddf090 commit 497f706

File tree

2 files changed

+10
-12
lines changed

2 files changed

+10
-12
lines changed

Sources/Keyboard/Keyboard.swift

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,20 +63,17 @@ extension Keyboard where Content == KeyboardKey {
6363
self.layout = layout
6464
self.noteOn = noteOn
6565
self.noteOff = noteOff
66+
var alignment: Alignment = .bottom
6667
switch layout {
6768
case .guitar(let row):
6869
self.rowCount = row
70+
alignment = .center
71+
case .pianoRoll:
72+
alignment = .trailing
6973
default:
7074
self.rowCount = 1
71-
}
72-
self.content = { KeyboardKey(pitch: $0, isActivated: $1, flatTop: layout == .piano) }
73-
}
74-
}
7575

76-
struct Keyboard_Previews: PreviewProvider {
77-
static var previews: some View {
78-
Keyboard() { pitch, isActivated in
79-
KeyboardKey(pitch: pitch, isActivated: isActivated)
8076
}
77+
self.content = { KeyboardKey(pitch: $0, isActivated: $1, flatTop: layout == .piano, alignment: alignment) }
8178
}
8279
}

Sources/Keyboard/KeyboardKey.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public struct KeyboardKey: View {
1919
blackKeyColor: Color = .black,
2020
pressedColor: Color = .red,
2121
flatTop: Bool = false,
22+
alignment: Alignment = .bottom,
2223
isActivatedExternally: Bool = false) {
2324
self.pitch = pitch
2425
self.isActivated = isActivated
@@ -37,6 +38,7 @@ public struct KeyboardKey: View {
3738
self.blackKeyColor = blackKeyColor
3839
self.pressedColor = pressedColor
3940
self.flatTop = flatTop
41+
self.alignment = alignment
4042
self.isActivatedExternally = isActivatedExternally
4143
}
4244

@@ -46,6 +48,7 @@ public struct KeyboardKey: View {
4648
var blackKeyColor: Color
4749
var pressedColor: Color
4850
var flatTop: Bool
51+
var alignment: Alignment
4952
var text: String
5053
var isActivatedExternally: Bool
5154

@@ -81,7 +84,7 @@ public struct KeyboardKey: View {
8184

8285
public var body: some View {
8386
GeometryReader { proxy in
84-
ZStack(alignment: proxy.size.height > proxy.size.width ? .bottom : .trailing) {
87+
ZStack(alignment: alignment) {
8588
Rectangle()
8689
.foregroundColor(keyColor)
8790
.padding(.top, flatTop ? relativeCornerRadius(in: proxy.size) : 0)
@@ -91,9 +94,7 @@ public struct KeyboardKey: View {
9194
Text(text)
9295
.font(Font(.init(.system, size: relativeFontSize(in: proxy.size))))
9396
.foregroundColor(textColor)
94-
.padding(EdgeInsets(top: 0, leading: 0,
95-
bottom: isTall(size: proxy.size) ? proxy.size.height * relativeTextPadding : 0,
96-
trailing: isTall(size: proxy.size) ? 0 : proxy.size.width * relativeTextPadding))
97+
.padding(relativeFontSize(in: proxy.size) / 3.0)
9798
}
9899

99100
}

0 commit comments

Comments
 (0)