Support non-BMP codepoints in TextInput

This commit is contained in:
UnicornFreedom 2025-08-21 13:22:59 +02:00
parent 349280d802
commit 90a01493db
No known key found for this signature in database
GPG Key ID: B4ED0DB6B940024F

View File

@ -22,7 +22,7 @@ class TextInput(val initialText: String = "") extends Widget with MouseHandler w
override protected val HoverAnimationColorActive: Color = ColorScheme("TextInputBackgroundActive")
// model
private val _text: Text = Text(initialText.toCharArray)
private val _text: Text = Text(initialText.codePoints().toArray)
private val cursor: Cursor = Cursor()
// view
@ -50,11 +50,11 @@ class TextInput(val initialText: String = "") extends Widget with MouseHandler w
def validator(text: String): Boolean = true
final def isInputValid: Boolean = validator(text)
def text: String = _text.chars.mkString
def text_=(value: String): Unit = _text.chars = value.toCharArray
def text: String = new String(_text.chars, 0, _text.chars.length)
def text_=(value: String): Unit = _text.chars = value.codePoints().toArray
protected var placeholder: Array[Char] = "".toCharArray
def placeholder_=(value: String): Unit = placeholder = value.toCharArray
protected var placeholder: Array[Int] = Array.empty
def placeholder_=(value: String): Unit = placeholder = value.codePoints().toArray
def focus(): Unit = {
if (!isFocused) {
@ -82,7 +82,7 @@ class TextInput(val initialText: String = "") extends Widget with MouseHandler w
protected def font: Font = Font.NormalFont
private def charWidth(c: Char): Int = font.charWidth(c)
private def charWidth(codePoint: Int): Int = font.charWidth(codePoint)
/**
* Calculates given text width in pixels.
@ -90,7 +90,7 @@ class TextInput(val initialText: String = "") extends Widget with MouseHandler w
* @param to exclusive
*/
//noinspection SameParameterValue
private def charsWidth(chars: Array[Char], from: Int, to: Int): Int = {
private def charsWidth(chars: Array[Int], from: Int, to: Int): Int = {
var width = 0
for (index <- (from max 0) until (to min chars.length)) {
width += font.charWidth(chars(index))
@ -175,14 +175,14 @@ class TextInput(val initialText: String = "") extends Widget with MouseHandler w
private def writeString(string: String): Unit = {
val (lhs, rhs) = _text.chars.splitAt(cursor.position)
val array = string.toCharArray
val array = string.codePoints().toArray
_text.chars = lhs ++ array ++ rhs
cursor.position += string.length
cursor.position += array.length
}
private def writeChar(char: Char): Unit = {
private def writeChar(codePoint: Int): Unit = {
val (lhs, rhs) = _text.chars.splitAt(cursor.position)
_text.chars = lhs ++ Array(char) ++ rhs
_text.chars = lhs ++ Array(codePoint) ++ rhs
cursor.position += 1
}
@ -267,6 +267,6 @@ class TextInput(val initialText: String = "") extends Widget with MouseHandler w
}
object TextInput {
case class Text(var chars: Array[Char])
case class Text(var chars: Array[Int])
case class Cursor(var position: Int = 0)
}