mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-04 17:05:38 +00:00
widget: move Editor caret information to sub-struct
In preparation for maintaining the caret position. Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
+26
-23
@@ -47,12 +47,15 @@ type Editor struct {
|
|||||||
shapes []line
|
shapes []line
|
||||||
dims layout.Dimensions
|
dims layout.Dimensions
|
||||||
requestFocus bool
|
requestFocus bool
|
||||||
caretOn bool
|
|
||||||
caretScroll bool
|
|
||||||
|
|
||||||
// carXOff is the offset to the current caret
|
caret struct {
|
||||||
// position when moving between lines.
|
on bool
|
||||||
carXOff fixed.Int26_6
|
scroll bool
|
||||||
|
|
||||||
|
// xoff is the offset to the current caret
|
||||||
|
// position when moving between lines.
|
||||||
|
xoff fixed.Int26_6
|
||||||
|
}
|
||||||
|
|
||||||
scroller gesture.Scroll
|
scroller gesture.Scroll
|
||||||
scrollOff image.Point
|
scrollOff image.Point
|
||||||
@@ -149,7 +152,7 @@ func (e *Editor) processPointer(gtx layout.Context) {
|
|||||||
})
|
})
|
||||||
e.requestFocus = true
|
e.requestFocus = true
|
||||||
if e.scroller.State() != gesture.StateFlinging {
|
if e.scroller.State() != gesture.StateFlinging {
|
||||||
e.caretScroll = true
|
e.caret.scroll = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -180,11 +183,11 @@ func (e *Editor) processKey(gtx layout.Context) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if e.command(ke) {
|
if e.command(ke) {
|
||||||
e.caretScroll = true
|
e.caret.scroll = true
|
||||||
e.scroller.Stop()
|
e.scroller.Stop()
|
||||||
}
|
}
|
||||||
case key.EditEvent:
|
case key.EditEvent:
|
||||||
e.caretScroll = true
|
e.caret.scroll = true
|
||||||
e.scroller.Stop()
|
e.scroller.Stop()
|
||||||
e.append(ke.Text)
|
e.append(ke.Text)
|
||||||
}
|
}
|
||||||
@@ -204,10 +207,10 @@ func (e *Editor) command(k key.Event) bool {
|
|||||||
e.Delete(1)
|
e.Delete(1)
|
||||||
case key.NameUpArrow:
|
case key.NameUpArrow:
|
||||||
line, _, carX, _ := e.layoutCaret()
|
line, _, carX, _ := e.layoutCaret()
|
||||||
e.carXOff = e.moveToLine(carX+e.carXOff, line-1)
|
e.caret.xoff = e.moveToLine(carX+e.caret.xoff, line-1)
|
||||||
case key.NameDownArrow:
|
case key.NameDownArrow:
|
||||||
line, _, carX, _ := e.layoutCaret()
|
line, _, carX, _ := e.layoutCaret()
|
||||||
e.carXOff = e.moveToLine(carX+e.carXOff, line+1)
|
e.caret.xoff = e.moveToLine(carX+e.caret.xoff, line+1)
|
||||||
case key.NameLeftArrow:
|
case key.NameLeftArrow:
|
||||||
e.Move(-1)
|
e.Move(-1)
|
||||||
case key.NameRightArrow:
|
case key.NameRightArrow:
|
||||||
@@ -268,8 +271,8 @@ func (e *Editor) layout(gtx layout.Context) layout.Dimensions {
|
|||||||
// Adjust scrolling for new viewport and layout.
|
// Adjust scrolling for new viewport and layout.
|
||||||
e.scrollRel(0, 0)
|
e.scrollRel(0, 0)
|
||||||
|
|
||||||
if e.caretScroll {
|
if e.caret.scroll {
|
||||||
e.caretScroll = false
|
e.caret.scroll = false
|
||||||
e.scrollToCaret()
|
e.scrollToCaret()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -307,7 +310,7 @@ func (e *Editor) layout(gtx layout.Context) layout.Dimensions {
|
|||||||
pointer.Rect(r).Add(gtx.Ops)
|
pointer.Rect(r).Add(gtx.Ops)
|
||||||
e.scroller.Add(gtx.Ops)
|
e.scroller.Add(gtx.Ops)
|
||||||
e.clicker.Add(gtx.Ops)
|
e.clicker.Add(gtx.Ops)
|
||||||
e.caretOn = false
|
e.caret.on = false
|
||||||
if e.focused {
|
if e.focused {
|
||||||
now := gtx.Now
|
now := gtx.Now
|
||||||
dt := now.Sub(e.blinkStart)
|
dt := now.Sub(e.blinkStart)
|
||||||
@@ -318,7 +321,7 @@ func (e *Editor) layout(gtx layout.Context) layout.Dimensions {
|
|||||||
redraw := op.InvalidateOp{At: nextBlink}
|
redraw := op.InvalidateOp{At: nextBlink}
|
||||||
redraw.Add(gtx.Ops)
|
redraw.Add(gtx.Ops)
|
||||||
}
|
}
|
||||||
e.caretOn = e.focused && (!blinking || dt%timePerBlink < timePerBlink/2)
|
e.caret.on = e.focused && (!blinking || dt%timePerBlink < timePerBlink/2)
|
||||||
}
|
}
|
||||||
|
|
||||||
return layout.Dimensions{Size: e.viewSize, Baseline: e.dims.Baseline}
|
return layout.Dimensions{Size: e.viewSize, Baseline: e.dims.Baseline}
|
||||||
@@ -337,7 +340,7 @@ func (e *Editor) PaintText(gtx layout.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (e *Editor) PaintCaret(gtx layout.Context) {
|
func (e *Editor) PaintCaret(gtx layout.Context) {
|
||||||
if !e.caretOn {
|
if !e.caret.on {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
carWidth := fixed.I(gtx.Px(unit.Dp(1)))
|
carWidth := fixed.I(gtx.Px(unit.Dp(1)))
|
||||||
@@ -384,7 +387,7 @@ func (e *Editor) Text() string {
|
|||||||
// SetText replaces the contents of the editor.
|
// SetText replaces the contents of the editor.
|
||||||
func (e *Editor) SetText(s string) {
|
func (e *Editor) SetText(s string) {
|
||||||
e.rr = editBuffer{}
|
e.rr = editBuffer{}
|
||||||
e.carXOff = 0
|
e.caret.xoff = 0
|
||||||
e.prepend(s)
|
e.prepend(s)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -509,14 +512,14 @@ func (e *Editor) invalidate() {
|
|||||||
// direction to delete: positive is forward, negative is backward.
|
// direction to delete: positive is forward, negative is backward.
|
||||||
func (e *Editor) Delete(runes int) {
|
func (e *Editor) Delete(runes int) {
|
||||||
e.rr.deleteRunes(runes)
|
e.rr.deleteRunes(runes)
|
||||||
e.carXOff = 0
|
e.caret.xoff = 0
|
||||||
e.invalidate()
|
e.invalidate()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Insert inserts text at the caret, moving the caret forward.
|
// Insert inserts text at the caret, moving the caret forward.
|
||||||
func (e *Editor) Insert(s string) {
|
func (e *Editor) Insert(s string) {
|
||||||
e.append(s)
|
e.append(s)
|
||||||
e.caretScroll = true
|
e.caret.scroll = true
|
||||||
e.invalidate()
|
e.invalidate()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -530,7 +533,7 @@ func (e *Editor) append(s string) {
|
|||||||
|
|
||||||
func (e *Editor) prepend(s string) {
|
func (e *Editor) prepend(s string) {
|
||||||
e.rr.prepend(s)
|
e.rr.prepend(s)
|
||||||
e.carXOff = 0
|
e.caret.xoff = 0
|
||||||
e.invalidate()
|
e.invalidate()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -555,7 +558,7 @@ func (e *Editor) movePages(pages int) {
|
|||||||
y2 += h
|
y2 += h
|
||||||
carLine2++
|
carLine2++
|
||||||
}
|
}
|
||||||
e.carXOff = e.moveToLine(carX+e.carXOff, carLine2)
|
e.caret.xoff = e.moveToLine(carX+e.caret.xoff, carLine2)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Editor) moveToLine(carX fixed.Int26_6, carLine2 int) fixed.Int26_6 {
|
func (e *Editor) moveToLine(carX fixed.Int26_6, carLine2 int) fixed.Int26_6 {
|
||||||
@@ -610,7 +613,7 @@ func (e *Editor) moveToLine(carX fixed.Int26_6, carLine2 int) fixed.Int26_6 {
|
|||||||
// backward.
|
// backward.
|
||||||
func (e *Editor) Move(distance int) {
|
func (e *Editor) Move(distance int) {
|
||||||
e.rr.move(distance)
|
e.rr.move(distance)
|
||||||
e.carXOff = 0
|
e.caret.xoff = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Editor) moveStart() {
|
func (e *Editor) moveStart() {
|
||||||
@@ -621,7 +624,7 @@ func (e *Editor) moveStart() {
|
|||||||
e.rr.caret -= s
|
e.rr.caret -= s
|
||||||
x -= layout[i].Advance
|
x -= layout[i].Advance
|
||||||
}
|
}
|
||||||
e.carXOff = -x
|
e.caret.xoff = -x
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Editor) moveEnd() {
|
func (e *Editor) moveEnd() {
|
||||||
@@ -640,7 +643,7 @@ func (e *Editor) moveEnd() {
|
|||||||
x += adv
|
x += adv
|
||||||
}
|
}
|
||||||
a := align(e.Alignment, l.Width, e.viewSize.X)
|
a := align(e.Alignment, l.Width, e.viewSize.X)
|
||||||
e.carXOff = l.Width + a - x
|
e.caret.xoff = l.Width + a - x
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Editor) scrollToCaret() {
|
func (e *Editor) scrollToCaret() {
|
||||||
|
|||||||
Reference in New Issue
Block a user