mirror of
https://gitlab.com/cc-ru/ocelot/ocelot-desktop.git
synced 2025-12-20 02:59:19 +01:00
Support non-BMP codepoints in TextInput
This commit is contained in:
parent
349280d802
commit
90a01493db
@ -22,7 +22,7 @@ class TextInput(val initialText: String = "") extends Widget with MouseHandler w
|
|||||||
override protected val HoverAnimationColorActive: Color = ColorScheme("TextInputBackgroundActive")
|
override protected val HoverAnimationColorActive: Color = ColorScheme("TextInputBackgroundActive")
|
||||||
|
|
||||||
// model
|
// model
|
||||||
private val _text: Text = Text(initialText.toCharArray)
|
private val _text: Text = Text(initialText.codePoints().toArray)
|
||||||
private val cursor: Cursor = Cursor()
|
private val cursor: Cursor = Cursor()
|
||||||
|
|
||||||
// view
|
// view
|
||||||
@ -50,11 +50,11 @@ class TextInput(val initialText: String = "") extends Widget with MouseHandler w
|
|||||||
def validator(text: String): Boolean = true
|
def validator(text: String): Boolean = true
|
||||||
final def isInputValid: Boolean = validator(text)
|
final def isInputValid: Boolean = validator(text)
|
||||||
|
|
||||||
def text: String = _text.chars.mkString
|
def text: String = new String(_text.chars, 0, _text.chars.length)
|
||||||
def text_=(value: String): Unit = _text.chars = value.toCharArray
|
def text_=(value: String): Unit = _text.chars = value.codePoints().toArray
|
||||||
|
|
||||||
protected var placeholder: Array[Char] = "".toCharArray
|
protected var placeholder: Array[Int] = Array.empty
|
||||||
def placeholder_=(value: String): Unit = placeholder = value.toCharArray
|
def placeholder_=(value: String): Unit = placeholder = value.codePoints().toArray
|
||||||
|
|
||||||
def focus(): Unit = {
|
def focus(): Unit = {
|
||||||
if (!isFocused) {
|
if (!isFocused) {
|
||||||
@ -82,7 +82,7 @@ class TextInput(val initialText: String = "") extends Widget with MouseHandler w
|
|||||||
|
|
||||||
protected def font: Font = Font.NormalFont
|
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.
|
* Calculates given text width in pixels.
|
||||||
@ -90,7 +90,7 @@ class TextInput(val initialText: String = "") extends Widget with MouseHandler w
|
|||||||
* @param to exclusive
|
* @param to exclusive
|
||||||
*/
|
*/
|
||||||
//noinspection SameParameterValue
|
//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
|
var width = 0
|
||||||
for (index <- (from max 0) until (to min chars.length)) {
|
for (index <- (from max 0) until (to min chars.length)) {
|
||||||
width += font.charWidth(chars(index))
|
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 = {
|
private def writeString(string: String): Unit = {
|
||||||
val (lhs, rhs) = _text.chars.splitAt(cursor.position)
|
val (lhs, rhs) = _text.chars.splitAt(cursor.position)
|
||||||
val array = string.toCharArray
|
val array = string.codePoints().toArray
|
||||||
_text.chars = lhs ++ array ++ rhs
|
_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)
|
val (lhs, rhs) = _text.chars.splitAt(cursor.position)
|
||||||
_text.chars = lhs ++ Array(char) ++ rhs
|
_text.chars = lhs ++ Array(codePoint) ++ rhs
|
||||||
cursor.position += 1
|
cursor.position += 1
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -267,6 +267,6 @@ class TextInput(val initialText: String = "") extends Widget with MouseHandler w
|
|||||||
}
|
}
|
||||||
|
|
||||||
object TextInput {
|
object TextInput {
|
||||||
case class Text(var chars: Array[Char])
|
case class Text(var chars: Array[Int])
|
||||||
case class Cursor(var position: Int = 0)
|
case class Cursor(var position: Int = 0)
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user