mirror of
https://gitlab.com/cc-ru/ocelot/ocelot-desktop.git
synced 2025-12-20 02:59:19 +01:00
Reformat TextInput
This commit is contained in:
parent
cbd3927cc5
commit
51c4494628
@ -8,7 +8,7 @@ import ocelot.desktop.ui.UiHandler
|
|||||||
import ocelot.desktop.ui.event.handlers.MouseHandler
|
import ocelot.desktop.ui.event.handlers.MouseHandler
|
||||||
import ocelot.desktop.ui.event.sources.KeyEvents
|
import ocelot.desktop.ui.event.sources.KeyEvents
|
||||||
import ocelot.desktop.ui.event.{DragEvent, KeyEvent, MouseEvent}
|
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.contextmenu.{ContextMenu, ContextMenuEntry}
|
||||||
import ocelot.desktop.ui.widget.traits.HoverAnimation
|
import ocelot.desktop.ui.widget.traits.HoverAnimation
|
||||||
import ocelot.desktop.util.NumberUtils.ExtendedInt
|
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 =>
|
case event @ KeyEvent(KeyEvent.State.Press | KeyEvent.State.Repeat, Keyboard.KEY_LEFT, _) if isFocused =>
|
||||||
handleKeyMovement(selection match {
|
handleKeyMovement(selection match {
|
||||||
case Some(Selection.Ordered(start, _)) if !KeyEvents.isShiftDown => start
|
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
|
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 =>
|
case event @ KeyEvent(KeyEvent.State.Press | KeyEvent.State.Repeat, Keyboard.KEY_RIGHT, _) if isFocused =>
|
||||||
handleKeyMovement(selection match {
|
handleKeyMovement(selection match {
|
||||||
case Some(Selection.Ordered(_, end)) if !KeyEvents.isShiftDown => end
|
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
|
case _ => cursor.position + 1
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -257,7 +259,7 @@ class TextInput(val initialText: String = "") extends Widget with MouseHandler w
|
|||||||
} else {
|
} else {
|
||||||
(cursor.position + 1).clamped(0, _text.chars.length)
|
(cursor.position + 1).clamped(0, _text.chars.length)
|
||||||
}
|
}
|
||||||
|
|
||||||
deleteRange(cursor.position, end)
|
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.
|
* 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
|
* 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.
|
* 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
|
* If `punctuationBoundaries` is enabled, a contiguous punctuation sequence is treated as a word for the purposes of
|
||||||
* boundary search.
|
* boundary search.
|
||||||
*
|
*
|
||||||
* May return the `initialPosition` if it's already at the boundary.
|
* 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
|
import Character.isLetterOrDigit
|
||||||
|
|
||||||
val start = initialPosition.clamped(0, _text.chars.length)
|
val start = initialPosition.clamped(0, _text.chars.length)
|
||||||
|
|
||||||
val indices = if (forward) {
|
val indices = if (forward) {
|
||||||
@ -359,23 +366,25 @@ class TextInput(val initialText: String = "") extends Widget with MouseHandler w
|
|||||||
} else {
|
} else {
|
||||||
start.to(0, -1)
|
start.to(0, -1)
|
||||||
}
|
}
|
||||||
|
|
||||||
val isBoundaryCodepoints: (Int, Int) => Boolean = if (wordStart) {
|
val isBoundaryCodepoints: (Int, Int) => Boolean = if (wordStart) {
|
||||||
(prev, current) => (
|
(prev, current) =>
|
||||||
isLetterOrDigit(current) && !isLetterOrDigit(prev)
|
(
|
||||||
|
isLetterOrDigit(current) && !isLetterOrDigit(prev)
|
||||||
|| punctuationBoundaries && isPunctuation(current) && !isPunctuation(prev)
|
|| punctuationBoundaries && isPunctuation(current) && !isPunctuation(prev)
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
(prev, current) => (
|
(prev, current) =>
|
||||||
!isLetterOrDigit(current) && isLetterOrDigit(prev)
|
(
|
||||||
|
!isLetterOrDigit(current) && isLetterOrDigit(prev)
|
||||||
|| punctuationBoundaries && !isPunctuation(current) && isPunctuation(prev)
|
|| punctuationBoundaries && !isPunctuation(current) && isPunctuation(prev)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
def isBoundary(idx: Int): Boolean = {
|
def isBoundary(idx: Int): Boolean = {
|
||||||
idx == 0 || idx == _text.chars.length || isBoundaryCodepoints(_text.chars(idx - 1), _text.chars(idx))
|
idx == 0 || idx == _text.chars.length || isBoundaryCodepoints(_text.chars(idx - 1), _text.chars(idx))
|
||||||
}
|
}
|
||||||
|
|
||||||
indices.find(isBoundary).getOrElse(indices.end)
|
indices.find(isBoundary).getOrElse(indices.end)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -404,10 +413,10 @@ class TextInput(val initialText: String = "") extends Widget with MouseHandler w
|
|||||||
deleteRange(start, end)
|
deleteRange(start, end)
|
||||||
cursor.position = start
|
cursor.position = start
|
||||||
}
|
}
|
||||||
|
|
||||||
selection = None
|
selection = None
|
||||||
}
|
}
|
||||||
|
|
||||||
private def deleteRange(start: Int, end: Int): Unit = {
|
private def deleteRange(start: Int, end: Int): Unit = {
|
||||||
_text.chars = _text.chars.take(start) ++ _text.chars.drop(end)
|
_text.chars = _text.chars.take(start) ++ _text.chars.drop(end)
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user