javafx: improve some text component performance

This commit is contained in:
Bixilon 2023-03-09 11:28:54 +01:00
parent 4a546ff349
commit 6de93b3acb
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
2 changed files with 9 additions and 7 deletions

View File

@ -30,6 +30,7 @@ import javafx.scene.Node
import javafx.scene.paint.Color import javafx.scene.paint.Color
import javafx.scene.text.Text import javafx.scene.text.Text
import javafx.util.Duration import javafx.util.Duration
import java.util.concurrent.atomic.AtomicInteger
open class TextComponent( open class TextComponent(
@ -117,21 +118,23 @@ open class TextComponent(
override fun getJavaFXText(nodes: ObservableList<Node>): ObservableList<Node> { override fun getJavaFXText(nodes: ObservableList<Node>): ObservableList<Node> {
val text = Text(this.message) val text = Text(this.message)
this.color?.let { val color = this.color
if (ErosProfileManager.selected.text.colored) { if (color == null) {
text.fill = Color.rgb(it.red, it.green, it.blue)
}
} ?: let {
text.styleClass += "text-default-color" text.styleClass += "text-default-color"
} else {
if (ErosProfileManager.selected.text.colored) {
text.fill = Color.rgb(color.red, color.green, color.blue)
}
} }
if (FormattingCodes.OBFUSCATED in formatting) { if (FormattingCodes.OBFUSCATED in formatting) {
// ToDo: This is just slow // ToDo: This is just slow
val obfuscatedTimeline = if (ErosProfileManager.selected.text.obfuscated) { val obfuscatedTimeline = if (ErosProfileManager.selected.text.obfuscated) {
val index = AtomicInteger()
Timeline( Timeline(
KeyFrame(Duration.millis(50.0), { KeyFrame(Duration.millis(50.0), {
val chars = text.text.toCharArray() val chars = text.text.toCharArray()
for (i in chars.indices) { for (i in chars.indices) {
chars[i] = ProtocolDefinition.OBFUSCATED_CHARS.random() chars[i] = ProtocolDefinition.OBFUSCATED_CHARS[index.getAndIncrement() % ProtocolDefinition.OBFUSCATED_CHARS.size]
} }
text.text = String(chars) text.text = String(chars)
}), }),

View File

@ -28,7 +28,6 @@ import kotlin.test.assertSame
internal class ChatComponentTest { internal class ChatComponentTest {
@Test @Test
fun testEmpty() { fun testEmpty() {
assertSame(ChatComponent.EMPTY, "".chat()) assertSame(ChatComponent.EMPTY, "".chat())