Skip to content

Commit 789dc04

Browse files
committed
variable enter color
1 parent a1884a2 commit 789dc04

File tree

7 files changed

+91
-21
lines changed

7 files changed

+91
-21
lines changed

keyboard/KeyboardViewController.swift

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ class KeyboardViewController: UIInputViewController, FcitxProtocol {
2727
var hostingController: UIHostingController<VirtualKeyboardView>!
2828
var removedBySlide = ""
2929

30+
private func updateTextIsEmpty() {
31+
let text =
32+
(textDocumentProxy.documentContextBeforeInput ?? "")
33+
+ (textDocumentProxy.selectedText ?? "")
34+
+ (textDocumentProxy.documentContextAfterInput ?? "")
35+
virtualKeyboardView.setTextIsEmpty(text.isEmpty)
36+
}
37+
3038
override func updateViewConstraints() {
3139
super.updateViewConstraints()
3240

@@ -99,6 +107,7 @@ class KeyboardViewController: UIInputViewController, FcitxProtocol {
99107

100108
override func textDidChange(_ textInput: UITextInput?) {
101109
// The app has just changed the document's contents, the document context has been updated.
110+
updateTextIsEmpty()
102111
}
103112

104113
public func keyPressed(_ key: String, _ code: String) {
@@ -143,17 +152,18 @@ class KeyboardViewController: UIInputViewController, FcitxProtocol {
143152
}
144153
case "Backspace":
145154
textDocumentProxy.deleteBackward()
155+
updateTextIsEmpty()
146156
case "End":
147157
let textAfter = textDocumentProxy.documentContextAfterInput ?? ""
148158
textDocumentProxy.adjustTextPosition(byCharacterOffset: lengthOfFirstLine(textAfter))
149159
case "Enter":
150-
textDocumentProxy.insertText("\n") // \r doesn't work in Safari address bar.
160+
commitString("\n") // \r doesn't work in Safari address bar.
151161
case "Home":
152162
let textBefore = textDocumentProxy.documentContextBeforeInput ?? ""
153163
textDocumentProxy.adjustTextPosition(byCharacterOffset: -lengthOfLastLine(textBefore))
154164
default:
155165
if !key.isEmpty {
156-
textDocumentProxy.insertText(key)
166+
commitString(key)
157167
}
158168
}
159169
}
@@ -165,6 +175,7 @@ class KeyboardViewController: UIInputViewController, FcitxProtocol {
165175

166176
public func commitString(_ commit: String) {
167177
textDocumentProxy.insertText(commit)
178+
updateTextIsEmpty()
168179
}
169180

170181
public func setPreedit(_ preedit: String, _ caret: Int) {
@@ -175,6 +186,7 @@ class KeyboardViewController: UIInputViewController, FcitxProtocol {
175186
if let text = textDocumentProxy.selectedText {
176187
UIPasteboard.general.string = text
177188
textDocumentProxy.deleteBackward()
189+
updateTextIsEmpty()
178190
}
179191
}
180192

@@ -186,7 +198,7 @@ class KeyboardViewController: UIInputViewController, FcitxProtocol {
186198

187199
public func paste() {
188200
if let text = UIPasteboard.general.string {
189-
textDocumentProxy.insertText(text)
201+
commitString(text)
190202
}
191203
}
192204

@@ -208,12 +220,13 @@ class KeyboardViewController: UIInputViewController, FcitxProtocol {
208220
for _ in 0..<newRemoval.count {
209221
textDocumentProxy.deleteBackward()
210222
}
223+
updateTextIsEmpty()
211224
} else {
212225
let refillCount = min(step, removedBySlide.count)
213226
let index = removedBySlide.index(removedBySlide.startIndex, offsetBy: refillCount)
214227
let refill = String(removedBySlide[..<index])
215228
removedBySlide = String(removedBySlide[index...])
216-
textDocumentProxy.insertText(refill)
229+
commitString(refill)
217230
}
218231
}
219232
}

uipanel/CandidateBar.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ struct CandidateBarView: View {
3939
let rowItemCount: [Int]
4040
let batch: Int
4141
let scrollEnd: Bool
42+
let enterLabel: String
43+
let enterHighlight: Bool
44+
let hasPreedit: Bool
4245
@Binding var expanded: Bool
4346
@Binding var pendingScroll: Int
4447
@State private var visibleRows = Set<Int>()
@@ -172,7 +175,8 @@ struct CandidateBarView: View {
172175
BackspaceView(width: keyWidth, height: keyHeight)
173176

174177
EnterView(
175-
label: NSLocalizedString("return", comment: ""), width: keyWidth, height: keyHeight)
178+
label: enterLabel, width: keyWidth, height: keyHeight, cr: hasPreedit,
179+
disable: false, highlight: enterHighlight)
176180
}
177181
} else {
178182
Rectangle().frame(width: 1, height: barHeight * expandDividerRatio).foregroundColor(

uipanel/Key.swift

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ struct KeyModifier: ViewModifier {
2828
let background: Color
2929
let pressedBackground: Color
3030
let foreground: Color
31+
let pressedForeground: Color
3132
let shadow: Color
3233
let action: GestureAction
3334
let pressedView: (any View)?
@@ -43,7 +44,7 @@ struct KeyModifier: ViewModifier {
4344
}.frame(width: width - columnGap, height: height - rowGap)
4445
.background(isPressed ? pressedBackground : background)
4546
.cornerRadius(keyCornerRadius)
46-
.foregroundColor(foreground)
47+
.foregroundColor(isPressed ? pressedForeground : foreground)
4748
.shadow(color: shadow, radius: 0, x: 0, y: 1)
4849
.condition(topRight != nil) {
4950
$0.overlay(
@@ -150,12 +151,13 @@ extension View {
150151

151152
func keyProperties(
152153
width: CGFloat, height: CGFloat, background: Color, pressedBackground: Color, foreground: Color,
153-
shadow: Color, action: GestureAction, pressedView: (any View)? = nil, topRight: String? = nil
154+
shadow: Color, action: GestureAction, pressedForeground: Color? = nil,
155+
pressedView: (any View)? = nil, topRight: String? = nil
154156
) -> some View {
155157
self.modifier(
156158
KeyModifier(
157159
width: width, height: height, background: background, pressedBackground: pressedBackground,
158-
foreground: foreground,
160+
foreground: foreground, pressedForeground: pressedForeground ?? foreground,
159161
shadow: shadow, action: action, pressedView: pressedView, topRight: topRight
160162
)
161163
)
@@ -363,17 +365,42 @@ struct EnterView: View {
363365
let label: String
364366
let width: CGFloat
365367
let height: CGFloat
368+
let cr: Bool
369+
let disable: Bool
370+
let highlight: Bool
366371

367372
var body: some View {
368-
Button {
369-
virtualKeyboardView.resetLayerIfNotLocked()
370-
client.keyPressed("\r", "Enter")
371-
} label: {
372-
Text(label)
373-
.commonContentStyle(
374-
width: width, height: height, background: getFunctionBackground(colorScheme),
375-
foreground: getNormalForeground(colorScheme))
376-
}.commonContainerStyle(width: width, height: height, shadow: getShadow(colorScheme))
373+
VStack {
374+
if cr {
375+
Image(systemName: "return")
376+
.resizable()
377+
.aspectRatio(contentMode: .fit)
378+
.frame(height: height * 0.4)
379+
} else {
380+
Text(label)
381+
}
382+
}
383+
.keyProperties(
384+
width: width, height: height,
385+
background: !cr && !disable && highlight
386+
? highlightBackground : getFunctionBackground(colorScheme),
387+
pressedBackground: !cr && disable
388+
? getFunctionBackground(colorScheme) : getNormalBackground(colorScheme),
389+
foreground: !cr && disable
390+
? disabledForeground
391+
: (!cr && highlight ? highlightForeground : getNormalForeground(colorScheme)),
392+
shadow: getShadow(colorScheme),
393+
action: GestureAction(
394+
onTap: {
395+
// When !cr && disable, still allow key press, because text empty detection is not reliable.
396+
// e.g. In WeChat when the first line is empty and caret is there and the second line is not empty,
397+
// it says text is empty but should be sendable.
398+
virtualKeyboardView.resetLayerIfNotLocked()
399+
client.keyPressed("\r", "Enter")
400+
}
401+
),
402+
pressedForeground: !cr && !disable && highlight ? getNormalForeground(colorScheme) : nil
403+
)
377404
}
378405
}
379406

uipanel/Keyboard.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ struct KeyboardView: View {
1818
let lock: Bool
1919
let spaceLabel: String
2020
let enterLabel: String
21+
let textIsEmpty: Bool
22+
let enterHighlight: Bool
23+
let hasPreedit: Bool
2124
@State private var defaultRows = [[String: Any]]()
2225
@State private var shiftRows = [[String: Any]]()
2326

@@ -81,7 +84,9 @@ struct KeyboardView: View {
8184
case "globe":
8285
GlobeView(width: keyWidth, height: height)
8386
case "enter":
84-
EnterView(label: enterLabel, width: keyWidth, height: height)
87+
EnterView(
88+
label: enterLabel, width: keyWidth, height: height, cr: hasPreedit,
89+
disable: textIsEmpty && enterHighlight, highlight: enterHighlight)
8590
case "shift":
8691
ShiftView(
8792
state: layer == "shift" ? (lock ? .capslock : .shift) : .normal, width: keyWidth,

uipanel/VirtualKeyboard.swift

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,16 @@ class ViewModel: ObservableObject {
3131
@Published var inputMethods = [InputMethod]()
3232
@Published var spaceLabel = ""
3333
@Published var enterLabel = ""
34+
@Published var enterHighlight = false
35+
@Published var textIsEmpty = false
3436
@Published var layer = "default"
3537
@Published var lock = false
3638

3739
@Published var frame = CGRect()
3840
@Published var menuItems = [MenuItem]()
3941
@Published var showMenu = false
42+
43+
var hasPreedit: Bool { !preedit.isEmpty || hasClientPreedit }
4044
}
4145

4246
public struct VirtualKeyboardView: View {
@@ -57,6 +61,9 @@ public struct VirtualKeyboardView: View {
5761
highlighted: viewModel.highlighted,
5862
rowItemCount: viewModel.rowItemCount,
5963
batch: viewModel.batch, scrollEnd: viewModel.scrollEnd,
64+
enterLabel: viewModel.enterLabel,
65+
enterHighlight: viewModel.enterHighlight,
66+
hasPreedit: viewModel.hasPreedit,
6067
expanded: $viewModel.expanded,
6168
pendingScroll: $viewModel.pendingScroll)
6269
}
@@ -70,7 +77,10 @@ public struct VirtualKeyboardView: View {
7077
KeyboardView(
7178
width: width, layer: viewModel.layer, lock: viewModel.lock,
7279
spaceLabel: viewModel.spaceLabel,
73-
enterLabel: viewModel.enterLabel)
80+
enterLabel: viewModel.enterLabel,
81+
textIsEmpty: viewModel.textIsEmpty,
82+
enterHighlight: viewModel.enterHighlight,
83+
hasPreedit: viewModel.hasPreedit)
7484
}
7585
}.background(colorScheme == .dark ? darkBackground : lightBackground)
7686
if viewModel.showMenu {
@@ -151,7 +161,12 @@ public struct VirtualKeyboardView: View {
151161
}
152162
}
153163

164+
public func setTextIsEmpty(_ isEmpty: Bool) {
165+
viewModel.textIsEmpty = isEmpty
166+
}
167+
154168
public func setReturnKeyType(_ type: UIReturnKeyType?) {
169+
viewModel.enterHighlight = true
155170
switch type {
156171
case .done:
157172
viewModel.enterLabel = NSLocalizedString("done", comment: "")
@@ -165,6 +180,7 @@ public struct VirtualKeyboardView: View {
165180
viewModel.enterLabel = NSLocalizedString("send", comment: "")
166181
default:
167182
viewModel.enterLabel = NSLocalizedString("return", comment: "")
183+
viewModel.enterHighlight = false
168184
}
169185
}
170186

@@ -186,7 +202,7 @@ public struct VirtualKeyboardView: View {
186202
}
187203

188204
func slideBackspace(_ step: Int) {
189-
if !viewModel.preedit.isEmpty || viewModel.hasClientPreedit || !viewModel.candidates.isEmpty {
205+
if viewModel.hasPreedit || !viewModel.candidates.isEmpty {
190206
if step == 0 {
191207
client.resetInput()
192208
}

uipanel/ui.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,8 @@ let darkFunctionBackground = Color(
3131
let darkShadow = Color(.sRGB, red: 37 / 255.0, green: 37 / 255.0, blue: 37 / 255.0, opacity: 1)
3232
let darkHighlightBackground = Color(
3333
.sRGB, red: 67 / 255.0, green: 67 / 255.0, blue: 70 / 255.0, opacity: 1)
34+
35+
let disabledForeground = Color.gray
36+
let highlightForeground = Color.white
37+
let highlightBackground = Color(
38+
.sRGB, red: 0, green: 122 / 255.0, blue: 1, opacity: 1)

0 commit comments

Comments
 (0)