-
Notifications
You must be signed in to change notification settings - Fork 12
/
KeyView.swift
123 lines (111 loc) · 3.58 KB
/
KeyView.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
//
// KeyView.swift
// my41
//
// Created by Miroslav Perovic on 5.1.21..
// Copyright © 2021 iPera. All rights reserved.
//
import SwiftUI
//import AttributedText
struct CalcKey: Hashable {
var shiftText: AttributedString
var upperText: AttributedString
var lowerText: AttributedString?
var shiftButton: Bool = false
var enter: Bool = false
var keyCode: Int
func execute(pressed: Bool) {
cpu.keyWithCode(Bits8(keyCode), pressed: pressed)
}
}
struct KeyView: View {
var key: CalcKey
var width: CGFloat
var height: CGFloat
@State private var pressed = false
private let shiftButtonBackground = Gradient(stops: [
.init(color: Color(red: 0.7490, green: 0.4901, blue: 0.1765), location: 0.00),
.init(color: Color(red: 0.7176, green: 0.4549, blue: 0.1765), location: 0.10),
.init(color: Color(red: 0.6745, green: 0.4235, blue: 0.0549), location: 0.49),
.init(color: Color(red: 0.6078, green: 0.3961, blue: 0.08235), location: 0.49),
.init(color: Color(red: 0.5804, green: 0.3961, blue: 0.1294), location: 0.90),
.init(color: Color(red: 0.4784, green: 0.2745, blue: 0.0471), location: 1.00)
])
private let buttonBackground = Gradient(stops: [
.init(color: Color(white: 0.50), location: 0.00),
.init(color: Color(white: 0.42), location: 0.12),
.init(color: Color(white: 0.30), location: 0.49),
.init(color: Color(white: 0.27), location: 0.49),
.init(color: Color(white: 0.20), location: 0.98),
.init(color: Color(white: 0.17), location: 1.00)
])
#if os(iOS)
let generator = UINotificationFeedbackGenerator()
#endif
var body: some View {
GeometryReader { geometry in
VStack (spacing: 0) {
Text(key.shiftText)
.frame(width: geometry.size.width, height: geometry.size.height / 3)
.padding(.bottom, 5)
Button(action: {
#if os(iOS)
generator.notificationOccurred(.success)
#endif
}, label: {
VStack (alignment: .center, spacing: 0) {
Text(key.upperText)
if let text = key.lowerText {
Text(text)
.padding(.top, 5)
}
}
.frame(width: width, height: height)
.background(
LinearGradient(
gradient: key.shiftButton ? shiftButtonBackground : buttonBackground,
startPoint: .top,
endPoint: .bottom
)
)
.overlay(
RoundedRectangle(cornerRadius: 3.0)
.stroke(Color.black, lineWidth: 1.0)
)
})
.onLongPressGesture(minimumDuration: .infinity, maximumDistance: .infinity, pressing: { pressing in
pressed = pressing
key.execute(pressed: pressed)
}, perform: {})
.opacity(pressed ? 0.75 : 1.0)
.scaleEffect(pressed ? 0.9 : 1.0)
}
}
}
}
struct KeyView_Previews: PreviewProvider {
//#if os(iOS)
// static var keys = Keys()
//#elseif os(macOS)
// static var keys = ModeKeys()
//#endif
static var keys = Keys()
static let width: CGFloat = 375 / 5
static var previews: some View {
GeometryReader { geometry in
let width = (geometry.size.width - 40) / 5
KeyView(key: keys.modeKeys[0], width: width, height: 20)
.frame(width: width, height: 20)
}
GeometryReader { geometry in
let width = (geometry.size.width - 40) / 5
KeyView(key: keys.keys8[2], width: width, height: width * 0.7813)
.frame(width: width, height: width * 0.7813)
}
GeometryReader { geometry in
let width = (geometry.size.width - 40) / 5
KeyView(key: keys.keys3[0], width: width, height: width * 0.7813)
.frame(width: width, height: width * 0.7813)
}
}
}