Skip to content

Commit 6845919

Browse files
authored
Merge pull request #778 from zhangqifan/fix/pincode-paste-support
fix: improve paste support for PIN code input fields
2 parents e07f2f7 + a2628ed commit 6845919

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

Xcodes/Frontend/SignIn/PinCodeTextView.swift

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,34 @@ class PinCodeTextView: NSControl, NSTextFieldDelegate {
149149

150150
let newFieldText = field.stringValue
151151

152+
// Handle pasting multiple characters (e.g., pasting "123456" from clipboard)
153+
if newFieldText.count > 1 {
154+
// Filter to alphanumeric characters only
155+
let validCharacters = newFieldText.filter { $0.isLetter || $0.isNumber }
156+
157+
// Always start from the first field and clear previous content
158+
var newCode = Array(repeating: Character?.none, count: numberOfDigits)
159+
for (offset, character) in validCharacters.enumerated() {
160+
if offset < numberOfDigits {
161+
newCode[offset] = character
162+
}
163+
}
164+
165+
// Update all fields at once to avoid triggering didSet multiple times
166+
code = newCode
167+
168+
// Move focus to next empty field or the last field if all are filled
169+
let nextEmptyIndex = code.firstIndex(where: { $0 == nil }) ?? numberOfDigits - 1
170+
if nextEmptyIndex < characterViews.count {
171+
window?.makeFirstResponder(characterViews[nextEmptyIndex])
172+
} else {
173+
resignFirstResponder()
174+
}
175+
176+
return
177+
}
178+
179+
// Handle single character input
152180
let lastCharacter: Character?
153181
if newFieldText.isEmpty {
154182
lastCharacter = nil

0 commit comments

Comments
 (0)