From 51c44946282da276815b2ce3d641b88dcad2efa1 Mon Sep 17 00:00:00 2001 From: Fingercomp Date: Fri, 5 Sep 2025 00:25:56 +0300 Subject: [PATCH] Reformat TextInput --- .../ocelot/desktop/ui/widget/TextInput.scala | 47 +++++++++++-------- 1 file changed, 28 insertions(+), 19 deletions(-) diff --git a/src/main/scala/ocelot/desktop/ui/widget/TextInput.scala b/src/main/scala/ocelot/desktop/ui/widget/TextInput.scala index c25b1b6..6fb63ca 100644 --- a/src/main/scala/ocelot/desktop/ui/widget/TextInput.scala +++ b/src/main/scala/ocelot/desktop/ui/widget/TextInput.scala @@ -8,7 +8,7 @@ import ocelot.desktop.ui.UiHandler import ocelot.desktop.ui.event.handlers.MouseHandler import ocelot.desktop.ui.event.sources.KeyEvents import ocelot.desktop.ui.event.{DragEvent, KeyEvent, MouseEvent} -import ocelot.desktop.ui.widget.TextInput.{Cursor, Selection, Text, isPunctuation} +import ocelot.desktop.ui.widget.TextInput.{isPunctuation, Cursor, Selection, Text} import ocelot.desktop.ui.widget.contextmenu.{ContextMenu, ContextMenuEntry} import ocelot.desktop.ui.widget.traits.HoverAnimation import ocelot.desktop.util.NumberUtils.ExtendedInt @@ -209,7 +209,8 @@ class TextInput(val initialText: String = "") extends Widget with MouseHandler w case event @ KeyEvent(KeyEvent.State.Press | KeyEvent.State.Repeat, Keyboard.KEY_LEFT, _) if isFocused => handleKeyMovement(selection match { case Some(Selection.Ordered(start, _)) if !KeyEvents.isShiftDown => start - case _ if KeyEvents.isControlDown => findWordBoundary(cursor.position - 1, forward = false, wordStart = true, punctuationBoundaries = false) + case _ if KeyEvents.isControlDown => + findWordBoundary(cursor.position - 1, forward = false, wordStart = true, punctuationBoundaries = false) case _ => cursor.position - 1 }) @@ -218,7 +219,8 @@ class TextInput(val initialText: String = "") extends Widget with MouseHandler w case event @ KeyEvent(KeyEvent.State.Press | KeyEvent.State.Repeat, Keyboard.KEY_RIGHT, _) if isFocused => handleKeyMovement(selection match { case Some(Selection.Ordered(_, end)) if !KeyEvents.isShiftDown => end - case _ if KeyEvents.isControlDown => findWordBoundary(cursor.position + 1, forward = true, wordStart = false, punctuationBoundaries = false) + case _ if KeyEvents.isControlDown => + findWordBoundary(cursor.position + 1, forward = true, wordStart = false, punctuationBoundaries = false) case _ => cursor.position + 1 }) @@ -257,7 +259,7 @@ class TextInput(val initialText: String = "") extends Widget with MouseHandler w } else { (cursor.position + 1).clamped(0, _text.chars.length) } - + deleteRange(cursor.position, end) } @@ -340,18 +342,23 @@ class TextInput(val initialText: String = "") extends Widget with MouseHandler w /** * Finds a word boundary closest to the `initialPosition` going in the specified direction. - * + * * If `wordStart` is `true`, the word boundary begins a word. Otherwise, it ends a word (one past the last character * of the word). If no boundary is found, the function returns the furthest index in the specified direction. - * + * * If `punctuationBoundaries` is enabled, a contiguous punctuation sequence is treated as a word for the purposes of * boundary search. - * + * * May return the `initialPosition` if it's already at the boundary. */ - private def findWordBoundary(initialPosition: Int, forward: Boolean, wordStart: Boolean, punctuationBoundaries: Boolean): Int = { + private def findWordBoundary( + initialPosition: Int, + forward: Boolean, + wordStart: Boolean, + punctuationBoundaries: Boolean, + ): Int = { import Character.isLetterOrDigit - + val start = initialPosition.clamped(0, _text.chars.length) val indices = if (forward) { @@ -359,23 +366,25 @@ class TextInput(val initialText: String = "") extends Widget with MouseHandler w } else { start.to(0, -1) } - + val isBoundaryCodepoints: (Int, Int) => Boolean = if (wordStart) { - (prev, current) => ( - isLetterOrDigit(current) && !isLetterOrDigit(prev) + (prev, current) => + ( + isLetterOrDigit(current) && !isLetterOrDigit(prev) || punctuationBoundaries && isPunctuation(current) && !isPunctuation(prev) - ) + ) } else { - (prev, current) => ( - !isLetterOrDigit(current) && isLetterOrDigit(prev) + (prev, current) => + ( + !isLetterOrDigit(current) && isLetterOrDigit(prev) || punctuationBoundaries && !isPunctuation(current) && isPunctuation(prev) - ) + ) } def isBoundary(idx: Int): Boolean = { idx == 0 || idx == _text.chars.length || isBoundaryCodepoints(_text.chars(idx - 1), _text.chars(idx)) } - + indices.find(isBoundary).getOrElse(indices.end) } @@ -404,10 +413,10 @@ class TextInput(val initialText: String = "") extends Widget with MouseHandler w deleteRange(start, end) cursor.position = start } - + selection = None } - + private def deleteRange(start: Int, end: Int): Unit = { _text.chars = _text.chars.take(start) ++ _text.chars.drop(end) }