MCPcopy Create free account
hub / github.com/cogentcore/core / autoScroll

Method autoScroll

core/textfield.go:1452–1556  ·  view source on GitHub ↗

autoScroll scrolls the starting position to keep the cursor visible

()

Source from the content-addressed store, hash-verified

1450
1451// autoScroll scrolls the starting position to keep the cursor visible
1452func (tf *TextField) autoScroll() {
1453 sz := &tf.Geom.Size
1454 icsz := tf.iconsSize()
1455 availSz := sz.Actual.Content.Sub(icsz)
1456 tf.configTextSize(availSz)
1457 n := len(tf.editText)
1458 tf.cursorPos = math32.ClampInt(tf.cursorPos, 0, n)
1459
1460 if tf.hasWordWrap() { // does not scroll
1461 tf.startPos = 0
1462 tf.endPos = n
1463 if len(tf.renderAll.Spans) != tf.numLines {
1464 tf.NeedsLayout()
1465 }
1466 return
1467 }
1468 st := &tf.Styles
1469
1470 if n == 0 || tf.Geom.Size.Actual.Content.X <= 0 {
1471 tf.cursorPos = 0
1472 tf.endPos = 0
1473 tf.startPos = 0
1474 return
1475 }
1476 maxw := tf.effSize.X
1477 if maxw < 0 {
1478 return
1479 }
1480 tf.charWidth = int(maxw / st.UnitContext.Dots(units.UnitCh)) // rough guess in chars
1481 if tf.charWidth < 1 {
1482 tf.charWidth = 1
1483 }
1484
1485 // first rationalize all the values
1486 if tf.endPos == 0 || tf.endPos > n { // not init
1487 tf.endPos = n
1488 }
1489 if tf.startPos >= tf.endPos {
1490 tf.startPos = max(0, tf.endPos-tf.charWidth)
1491 }
1492
1493 inc := int(math32.Ceil(.1 * float32(tf.charWidth)))
1494 inc = max(4, inc)
1495
1496 // keep cursor in view with buffer
1497 startIsAnchor := true
1498 if tf.cursorPos < (tf.startPos + inc) {
1499 tf.startPos -= inc
1500 tf.startPos = max(tf.startPos, 0)
1501 tf.endPos = tf.startPos + tf.charWidth
1502 tf.endPos = min(n, tf.endPos)
1503 } else if tf.cursorPos > (tf.endPos - inc) {
1504 tf.endPos += inc
1505 tf.endPos = min(tf.endPos, n)
1506 tf.startPos = tf.endPos - tf.charWidth
1507 tf.startPos = max(0, tf.startPos)
1508 startIsAnchor = false
1509 }

Callers 1

RenderMethod · 0.95

Calls 9

iconsSizeMethod · 0.95
configTextSizeMethod · 0.95
hasWordWrapMethod · 0.95
charPosMethod · 0.95
ClampIntFunction · 0.92
CeilFunction · 0.92
SubMethod · 0.45
NeedsLayoutMethod · 0.45
DotsMethod · 0.45

Tested by

no test coverage detected