Merge pull request #1116 from meshtastic/shortNameEmojiFix

Filter variation selectors from Short Name field
This commit is contained in:
Garth Vander Houwen 2025-02-28 18:16:09 -08:00 committed by GitHub
commit f0390d12bf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 18 additions and 2 deletions

View file

@ -90,4 +90,15 @@ extension String {
let end = index(start, offsetBy: range.upperBound - range.lowerBound)
return String(self[start ..< end])
}
// Filter out variation selectors from the string
var withoutVariationSelectors: String {
return self.unicodeScalars
.filter { scalar in
return !scalar.properties.isVariationSelector
}
.compactMap { UnicodeScalar($0) }
.map { String($0) }
.joined()
}
}

View file

@ -75,11 +75,16 @@ struct UserConfig: View {
TextField("Short Name", text: $shortName)
.foregroundColor(.gray)
.onChange(of: shortName) {
var totalBytes = shortName.utf8.count
let newValue = shortName.withoutVariationSelectors
let totalBytes = newValue.utf8.count
// Only mess with the value if it is too big
if totalBytes > 4 {
// If too long, drop the last thing entered
shortName = String(shortName.dropLast())
totalBytes = shortName.utf8.count
} else if shortName != newValue {
// If not too long, make sure the stripped
// variant is placed back in text field if necessary
shortName = newValue
}
}
.foregroundColor(.gray)