-
Notifications
You must be signed in to change notification settings - Fork 15
/
CandidatesView.swift
198 lines (186 loc) · 7.34 KB
/
CandidatesView.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
//
// FireCandidatesView.swift
// Fire
//
// Created by 虚幻 on 2019/9/16.
// Copyright © 2019 qwertyyb. All rights reserved.
//
import SwiftUI
import Defaults
func getShownCode(candidate: Candidate, origin: String) -> String {
if candidate.type == CandidateType.py || !candidate.code.hasPrefix(origin) {
return "(\(candidate.code))"
}
if candidate.code.hasPrefix(origin) {
return candidate.code.count > origin.count
? "~\(String(candidate.code.suffix(candidate.code.count - origin.count)))"
: ""
}
return ""
}
struct CandidateView: View {
var candidate: Candidate
var index: Int
var origin: String
var selected: Bool = false
@Default(.themeConfig) private var themeConfig
@Default(.wubiCodeTip) private var wubiCodeTip
@Environment(\.colorScheme) var colorScheme
var body: some View {
let indexColor = selected
? themeConfig[colorScheme].selectedIndexColor
: themeConfig[colorScheme].candidateIndexColor
let textColor = selected
? themeConfig[colorScheme].selectedTextColor
: themeConfig[colorScheme].candidateTextColor
let codeColor = selected
? themeConfig[colorScheme].selectedCodeColor
: themeConfig[colorScheme].candidateCodeColor
return HStack(alignment: .center, spacing: 2) {
Text("\(index + 1).")
.foregroundColor(Color(indexColor))
Text(candidate.text)
.foregroundColor(Color(textColor))
if wubiCodeTip {
Text(getShownCode(candidate: candidate, origin: origin))
.foregroundColor(Color(codeColor))
}
}
// .fixedSize()
.onTapGesture {
NotificationCenter.default.post(
name: Fire.candidateSelected,
object: nil,
userInfo: [
"candidate": candidate,
"index": index
]
)
}
}
}
struct CandidatesView: View {
var candidates: [Candidate]
var origin: String
var hasPrev: Bool = false
var hasNext: Bool = false
@Default(.candidatesDirection) private var direction
@Default(.themeConfig) private var themeConfig
@Default(.showCodeInWindow) private var showCodeInWindow
@Environment(\.colorScheme) var colorScheme
var _candidatesView: some View {
ForEach(Array(candidates.enumerated()), id: \.element) { (index, candidate) -> CandidateView in
CandidateView(
candidate: candidate,
index: index,
origin: origin,
selected: index == 0
)
}
}
var _indicator: some View {
if direction == CandidatesDirection.horizontal {
return AnyView(VStack(spacing: 0) {
Image("arrowUp")
.renderingMode(.template)
.resizable()
.frame(width: 10, height: 10, alignment: .center)
.onTapGesture {
if !hasPrev { return }
NotificationCenter.default.post(
name: Fire.prevPageBtnTapped,
object: nil
)
}
.foregroundColor(Color(hasPrev
? themeConfig[colorScheme].pageIndicatorColor
: themeConfig[colorScheme].pageIndicatorDisabledColor
))
Image("arrowDown")
.renderingMode(.template)
.resizable()
.frame(width: 10, height: 10, alignment: .center)
.onTapGesture {
if !hasNext { return }
print("next")
NotificationCenter.default.post(
name: Fire.nextPageBtnTapped,
object: nil
)
}
.foregroundColor(Color(hasNext
? themeConfig[colorScheme].pageIndicatorColor
: themeConfig[colorScheme].pageIndicatorDisabledColor
))
})
}
return AnyView(HStack(spacing: 4) {
Image("arrowUp")
.renderingMode(.template)
.resizable()
.frame(width: 10, height: 10, alignment: .center)
.rotationEffect(Angle(degrees: -90), anchor: .center)
.onTapGesture {
if !hasPrev { return }
NotificationCenter.default.post(
name: Fire.prevPageBtnTapped,
object: nil
)
}
Image("arrowDown")
.renderingMode(.template)
.resizable()
.frame(width: 10, height: 10, alignment: .center)
.rotationEffect(Angle(degrees: -90), anchor: .center)
.onTapGesture {
if !hasNext { return }
print("next")
NotificationCenter.default.post(
name: Fire.nextPageBtnTapped,
object: nil
)
}
})
}
var body: some View {
VStack(alignment: .leading, spacing: CGFloat( themeConfig[colorScheme].originCandidatesSpace), content: {
if showCodeInWindow {
Text(origin)
.foregroundColor(Color(themeConfig[colorScheme].originCodeColor))
.fixedSize()
}
if direction == CandidatesDirection.horizontal {
HStack(alignment: .center, spacing: CGFloat(themeConfig[colorScheme].candidateSpace)) {
_candidatesView
_indicator
}
.fixedSize()
} else {
VStack(alignment: .leading, spacing: CGFloat(themeConfig[colorScheme].candidateSpace)) {
_candidatesView
_indicator
}
.fixedSize()
}
})
.padding(.top, CGFloat(themeConfig[colorScheme].windowPaddingTop))
.padding(.bottom, CGFloat(themeConfig[colorScheme].windowPaddingBottom))
.padding(.leading, CGFloat(themeConfig[colorScheme].windowPaddingLeft))
.padding(.trailing, CGFloat(themeConfig[colorScheme].windowPaddingRight))
.fixedSize()
.font(.system(size: CGFloat(themeConfig[colorScheme].fontSize)))
.background(Color(themeConfig[colorScheme].windowBackgroundColor))
.cornerRadius(CGFloat(themeConfig[colorScheme].windowBorderRadius), antialiased: true)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
CandidatesView(candidates: [
Candidate(code: "a", text: "工", type: CandidateType.wb),
Candidate(code: "ab", text: "戈", type: CandidateType.wb),
Candidate(code: "abc", text: "啊", type: CandidateType.wb),
Candidate(code: "abcg", text: "阿", type: CandidateType.wb),
Candidate(code: "addd", text: "吖", type: CandidateType.wb)
], origin: "a")
}
}