Skip to content

Commit cc2b563

Browse files
committed
unify edit button impl with key; reuse backspace
1 parent 1409c98 commit cc2b563

File tree

3 files changed

+69
-80
lines changed

3 files changed

+69
-80
lines changed

uipanel/Edit.swift

Lines changed: 51 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import SwiftUI
22

3+
let editKeyCornerRadius: CGFloat = 8
4+
let editKeyGap: CGFloat = 8
5+
36
struct EditView: View {
47
@Environment(\.colorScheme) var colorScheme
58
let totalWidth: CGFloat
@@ -12,136 +15,111 @@ struct EditView: View {
1215

1316
ZStack {
1417
button(
15-
image: "arrowtriangle.left.fill", shrink: 15,
16-
background: getNormalBackground(colorScheme),
17-
foreground: getNormalForeground(colorScheme),
18-
shadow: getShadow(colorScheme),
18+
image: "arrowtriangle.left.fill", shrink: 15, normal: true,
1919
index: 0, width: width, height: height * 3
2020
) {
2121
client.keyPressed("", "ArrowLeft")
2222
}
2323
button(
24-
image: "arrowtriangle.up.fill", shrink: 15,
25-
background: getNormalBackground(colorScheme),
26-
foreground: getNormalForeground(colorScheme),
27-
shadow: getShadow(colorScheme),
24+
image: "arrowtriangle.up.fill", shrink: 15, normal: true,
2825
index: 1, width: width, height: height * 1.5
2926
) {
3027
client.keyPressed("", "ArrowUp")
3128
}
3229
button(
33-
image: "arrowtriangle.right.fill", shrink: 15,
34-
background: getNormalBackground(colorScheme),
35-
foreground: getNormalForeground(colorScheme),
36-
shadow: getShadow(colorScheme),
30+
image: "arrowtriangle.right.fill", shrink: 15, normal: true,
3731
index: 2, width: width, height: height * 3
3832
) {
3933
client.keyPressed("", "ArrowRight")
4034
}
4135
button(
42-
image: "scissors", shrink: 10,
43-
background: getFunctionBackground(colorScheme),
44-
foreground: getNormalForeground(colorScheme),
45-
shadow: getShadow(colorScheme),
36+
image: "scissors", shrink: 10, normal: false,
4637
index: 3, width: width, height: height
4738
) {
4839
client.cut()
4940
}
5041
button(
51-
image: "doc.on.doc", shrink: 10,
52-
background: getFunctionBackground(colorScheme),
53-
foreground: getNormalForeground(colorScheme),
54-
shadow: getShadow(colorScheme),
42+
image: "doc.on.doc", shrink: 10, normal: false,
5543
index: 7, width: width, height: height
5644
) {
5745
client.copy()
5846
}
5947
button(
60-
image: "arrowtriangle.down.fill", shrink: 15,
61-
background: getNormalBackground(colorScheme),
62-
foreground: getNormalForeground(colorScheme),
63-
shadow: getShadow(colorScheme),
48+
image: "arrowtriangle.down.fill", shrink: 15, normal: true,
6449
index: 5, width: width, height: height * 1.5
6550
) {
6651
client.keyPressed("", "ArrowDown")
6752
}
6853
button(
69-
image: "doc.on.clipboard", shrink: 10,
70-
background: getFunctionBackground(colorScheme),
71-
foreground: getNormalForeground(colorScheme),
72-
shadow: getShadow(colorScheme),
54+
image: "doc.on.clipboard", shrink: 10, normal: false,
7355
index: 11, width: width, height: height
7456
) {
7557
client.paste()
7658
}
7759
button(
78-
image: "arrow.left.to.line", shrink: 15,
79-
background: getNormalBackground(colorScheme),
80-
foreground: getNormalForeground(colorScheme),
81-
shadow: getShadow(colorScheme),
60+
image: "arrow.left.to.line", shrink: 15, normal: true,
8261
index: 12, width: width * 1.5, height: height
8362
) {
8463
client.keyPressed("", "Home")
8564
}
8665
button(
87-
image: "arrow.right.to.line", shrink: 15,
88-
background: getNormalBackground(colorScheme),
89-
foreground: getNormalForeground(colorScheme),
90-
shadow: getShadow(colorScheme),
66+
image: "arrow.right.to.line", shrink: 15, normal: true,
9167
index: 13, width: width * 1.5, height: height
9268
) {
9369
client.keyPressed("", "End")
9470
}
9571
button(
96-
image: "delete.left", shrink: 10,
97-
background: getFunctionBackground(colorScheme),
98-
foreground: getNormalForeground(colorScheme),
99-
shadow: getShadow(colorScheme),
72+
image: "delete.left", shrink: 10, normal: false,
10073
index: 15, width: width, height: height
101-
) {
102-
client.keyPressed("", "Backspace")
103-
}
74+
) {}
10475
}
10576
}
10677
}
10778

10879
private func position(_ index: Int, _ width: CGFloat, _ height: CGFloat) -> CGPoint {
10980
let row = index / 4
11081
let col = index % 4
111-
return CGPoint(x: (CGFloat(col) + 0.5) * width, y: (CGFloat(row) + 0.5) * height)
82+
return CGPoint(x: CGFloat(col) * width, y: CGFloat(row) * height)
11283
}
11384

11485
private func button(
115-
image: String, shrink: CGFloat, background: Color, foreground: Color, shadow: Color,
86+
image: String, shrink: CGFloat, normal: Bool,
11687
index: Int, width: CGFloat, height: CGFloat, action: @escaping () -> Void
11788
) -> some View {
118-
let w = width - 8
119-
let h = height - 8
120-
let r: CGFloat = 8
121-
return Button {
122-
action()
123-
} label: {
124-
VStack {
125-
let symbol = Image(systemName: image)
126-
.resizable()
127-
.aspectRatio(contentMode: .fit)
128-
.foregroundColor(foreground)
129-
let size = keyboardHeight / shrink
130-
if image == "arrowtriangle.up.fill" || image == "arrowtriangle.down.fill" {
131-
symbol.frame(width: size)
132-
} else {
133-
symbol.frame(height: size)
134-
}
135-
}.frame(width: w, height: h)
136-
.background(background)
137-
.foregroundColor(.black)
138-
.cornerRadius(r)
139-
}.buttonStyle(PlainButtonStyle())
140-
.shadow(color: shadow, radius: 0, x: 0, y: 1)
141-
.frame(width: w, height: h)
142-
.overlay(
143-
RoundedRectangle(cornerRadius: r)
144-
.stroke(Color.clear, lineWidth: 1)
145-
).position(position(index, width, height))
89+
let background = normal ? getNormalBackground(colorScheme) : getFunctionBackground(colorScheme)
90+
let pressedBackground =
91+
normal ? getFunctionBackground(colorScheme) : getNormalBackground(colorScheme)
92+
let foreground = getNormalForeground(colorScheme)
93+
let p = position(index, width, height)
94+
let size = keyboardHeight / shrink
95+
let useWidth = image == "arrowtriangle.up.fill" || image == "arrowtriangle.down.fill"
96+
let symbol = Image(systemName: image)
97+
.resizable()
98+
.aspectRatio(contentMode: .fit)
99+
.condition(useWidth) {
100+
$0.frame(width: size)
101+
}
102+
.condition(!useWidth) {
103+
$0.frame(height: size)
104+
}
105+
if image == "delete.left" {
106+
return AnyView(
107+
BackspaceView(
108+
x: p.x, y: p.y, width: width, height: height, hMargin: editKeyGap, vMargin: editKeyGap,
109+
radius: editKeyCornerRadius))
110+
}
111+
return AnyView(
112+
symbol.keyProperties(
113+
x: p.x, y: p.y, width: width, height: height, hMargin: editKeyGap, vMargin: editKeyGap,
114+
radius: editKeyCornerRadius,
115+
background: background,
116+
pressedBackground: pressedBackground,
117+
foreground: foreground,
118+
shadow: getShadow(colorScheme),
119+
action: GestureAction(
120+
onTap: {
121+
action()
122+
}))
123+
)
146124
}
147125
}

uipanel/Key.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ struct BackspaceView: View {
102102
let y: CGFloat
103103
let width: CGFloat
104104
let height: CGFloat
105+
var hMargin: CGFloat? = nil
106+
var vMargin: CGFloat? = nil
107+
var radius: CGFloat? = nil
105108

106109
var body: some View {
107110
Image(systemName: "delete.left")
@@ -110,6 +113,9 @@ struct BackspaceView: View {
110113
.frame(height: height * 0.4)
111114
.keyProperties(
112115
x: x, y: y, width: width, height: height,
116+
hMargin: hMargin ?? columnGap,
117+
vMargin: vMargin ?? rowGap,
118+
radius: radius ?? keyCornerRadius,
113119
background: getFunctionBackground(colorScheme),
114120
pressedBackground: getNormalBackground(colorScheme),
115121
foreground: getNormalForeground(colorScheme),

uipanel/KeyModifier.swift

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ struct KeyModifier: ViewModifier {
3939
let y: CGFloat
4040
let width: CGFloat
4141
let height: CGFloat
42+
let hMargin: CGFloat
43+
let vMargin: CGFloat
44+
let radius: CGFloat
4245
let background: Color
4346
let pressedBackground: Color
4447
let foreground: Color
@@ -57,9 +60,9 @@ struct KeyModifier: ViewModifier {
5760
} else {
5861
content
5962
}
60-
}.frame(width: width - columnGap, height: height - rowGap)
63+
}.frame(width: width - hMargin, height: height - rowGap)
6164
.background(isPressed ? pressedBackground : background)
62-
.cornerRadius(keyCornerRadius)
65+
.cornerRadius(radius)
6366
.foregroundColor(isPressed ? pressedForeground : foreground)
6467
.shadow(color: shadow, radius: 0, x: 0, y: 1)
6568
.condition(topRight != nil) {
@@ -75,7 +78,7 @@ struct KeyModifier: ViewModifier {
7578
.onChanged { value in
7679
let bubbleX = x + width / 2
7780
let bubbleY = y + height / 2
78-
let bubbleWidth = width - columnGap
81+
let bubbleWidth = width - hMargin
7982
let bubbleHeight = height - rowGap
8083

8184
if !isPressed { // touch start
@@ -187,15 +190,17 @@ extension View {
187190

188191
func keyProperties(
189192
x: CGFloat = 0, y: CGFloat = 0,
190-
width: CGFloat, height: CGFloat, background: Color, pressedBackground: Color, foreground: Color,
191-
shadow: Color, action: GestureAction, pressedForeground: Color? = nil,
193+
width: CGFloat, height: CGFloat, hMargin: CGFloat = columnGap, vMargin: CGFloat = rowGap,
194+
radius: CGFloat = keyCornerRadius, background: Color, pressedBackground: Color,
195+
foreground: Color, shadow: Color, action: GestureAction, pressedForeground: Color? = nil,
192196
pressedView: (any View)? = nil, topRight: String? = nil, bubbleLabel: String? = nil,
193197
swipeUpLabel: String? = nil
194198
) -> some View {
195199
self.modifier(
196200
KeyModifier(
197-
x: x, y: y, width: width, height: height, background: background,
198-
pressedBackground: pressedBackground,
201+
x: x, y: y, width: width, height: height, hMargin: hMargin, vMargin: vMargin,
202+
radius: radius,
203+
background: background, pressedBackground: pressedBackground,
199204
foreground: foreground, pressedForeground: pressedForeground ?? foreground,
200205
shadow: shadow, action: action, pressedView: pressedView, topRight: topRight,
201206
bubbleLabel: bubbleLabel, swipeUpLabel: swipeUpLabel

0 commit comments

Comments
 (0)