Skip to content

Commit

Permalink
Merge branch 'main' of github.com:oligo/gio-view
Browse files Browse the repository at this point in the history
  • Loading branch information
oligo committed Jul 31, 2024
2 parents bf52ce1 + 67f1e43 commit 0f107d8
Show file tree
Hide file tree
Showing 7 changed files with 389 additions and 60 deletions.
25 changes: 22 additions & 3 deletions editor/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package editor

import (
"bytes"
"io"
"unicode/utf8"

Expand All @@ -11,9 +12,6 @@ import (

// editBuffer implements a gap buffer for text editing.
type editBuffer struct {
// pos is the byte position for Read and ReadRune.
pos int

// The gap start and end in bytes.
gapstart, gapend int
text []byte
Expand Down Expand Up @@ -129,3 +127,24 @@ func (e *editBuffer) prepend(caret int, s string) {
e.gapstart += len(s)
e.changed = e.changed || len(s) > 0
}

func (e *editBuffer) countLinesBeforeOffset(byteOffset int64) int {
cnt := 0

if byteOffset >= int64(len(e.text)) {
byteOffset = int64(len(e.text))
}

offset := 0
for {
index := bytes.IndexByte(e.text[offset:byteOffset], '\n')
if index == -1 {
break
}
offset += index
cnt++
offset++ // Move past the newline
}

return cnt
}
56 changes: 40 additions & 16 deletions editor/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,17 @@ type imeState struct {

type selectionAction int

type LineInfo struct {
// line number starting from 1.
LineNum int
// offset in the cross axis.
YOffset int
// offset of the start rune the line.
Start int
// offset of the end rune the line.
End int
}

const (
selectionExtend selectionAction = iota
selectionClear
Expand Down Expand Up @@ -248,6 +259,9 @@ func (e *Editor) processPointerEvent(gtx layout.Context, ev event.Event) (Editor
Y: int(math.Round(float64(evt.Position.Y))),
})
gtx.Execute(key.FocusCmd{Tag: e})
if !e.ReadOnly {
gtx.Execute(key.SoftKeyboardCmd{Show: true})
}
if e.scroller.State() != gesture.StateFlinging {
e.scrollCaret = true
}
Expand All @@ -271,8 +285,8 @@ func (e *Editor) processPointerEvent(gtx layout.Context, ev event.Event) (Editor
e.text.MoveWord(1, selectionExtend)
e.dragging = false
case evt.NumClicks >= 3:
e.text.MoveStart(selectionClear)
e.text.MoveEnd(selectionExtend)
e.text.MoveLineStart(selectionClear)
e.text.MoveLineEnd(selectionExtend)
e.dragging = false
}
}
Expand Down Expand Up @@ -333,8 +347,8 @@ func (e *Editor) processKey(gtx layout.Context) (EditorEvent, bool) {
key.Filter{Focus: e, Name: key.NameDeleteBackward, Optional: key.ModShortcutAlt | key.ModShift},
key.Filter{Focus: e, Name: key.NameDeleteForward, Optional: key.ModShortcutAlt | key.ModShift},

key.Filter{Focus: e, Name: key.NameHome, Optional: key.ModShift},
key.Filter{Focus: e, Name: key.NameEnd, Optional: key.ModShift},
key.Filter{Focus: e, Name: key.NameHome, Optional: key.ModShortcut | key.ModShift},
key.Filter{Focus: e, Name: key.NameEnd, Optional: key.ModShortcut | key.ModShift},
key.Filter{Focus: e, Name: key.NamePageDown, Optional: key.ModShift},
key.Filter{Focus: e, Name: key.NamePageUp, Optional: key.ModShift},
key.Filter{Focus: e, Name: key.NameTab},
Expand All @@ -355,7 +369,7 @@ func (e *Editor) processKey(gtx layout.Context) (EditorEvent, bool) {
case key.FocusEvent:
// Reset IME state.
e.ime.imeState = imeState{}
if ke.Focus {
if ke.Focus && !e.ReadOnly {
gtx.Execute(key.SoftKeyboardCmd{Show: true})
}
case key.Event:
Expand Down Expand Up @@ -397,7 +411,7 @@ func (e *Editor) processKey(gtx layout.Context) (EditorEvent, bool) {
case e.SingleLine:
s = strings.ReplaceAll(s, "\n", " ")
}
moves = e.replace(ke.Range.Start, ke.Range.End, s, true)
moves += e.replace(ke.Range.Start, ke.Range.End, s, true)
adjust += utf8.RuneCountInString(ke.Text) - moves
// Reset caret xoff.
e.text.MoveCaret(0, 0)
Expand Down Expand Up @@ -481,6 +495,10 @@ func (e *Editor) command(gtx layout.Context, k key.Event) (EditorEvent, bool) {
}
}
}
case key.NameHome:
e.text.MoveTextStart(selAct)
case key.NameEnd:
e.text.MoveTextEnd(selAct)
}
return nil, false
}
Expand Down Expand Up @@ -542,14 +560,9 @@ func (e *Editor) command(gtx layout.Context, k key.Event) (EditorEvent, bool) {
case key.NamePageDown:
e.text.MovePages(+1, selAct)
case key.NameHome:
e.text.MoveStart(selAct)
e.text.MoveLineStart(selAct)
case key.NameEnd:
e.text.MoveEnd(selAct)
case key.NameTab:
if !e.ReadOnly {
// soft tab as 4 spaces
e.Insert(" ")
}
e.text.MoveLineEnd(selAct)
}
return nil, false
}
Expand Down Expand Up @@ -601,7 +614,7 @@ func (e *Editor) Update(gtx layout.Context) (EditorEvent, bool) {
// Layout lays out the editor using the provided textMaterial as the paint material
// for the text glyphs+caret and the selectMaterial as the paint material for the
// selection rectangle.
func (e *Editor) Layout(gtx layout.Context, lt *text.Shaper, font font.Font, size unit.Sp, textMaterial, selectMaterial op.CallOp) layout.Dimensions {
func (e *Editor) Layout(gtx layout.Context, lt *text.Shaper, font font.Font, size unit.Sp, textMaterial, selectMaterial op.CallOp, lineMaterial op.CallOp) layout.Dimensions {
for {
_, ok := e.Update(gtx)
if !ok {
Expand All @@ -610,7 +623,7 @@ func (e *Editor) Layout(gtx layout.Context, lt *text.Shaper, font font.Font, siz
}

e.text.Layout(gtx, lt, font, size)
return e.layout(gtx, textMaterial, selectMaterial)
return e.layout(gtx, textMaterial, selectMaterial, lineMaterial)
}

// updateSnippet queues a key.SnippetCmd if the snippet content or position
Expand Down Expand Up @@ -656,7 +669,7 @@ func (e *Editor) updateSnippet(gtx layout.Context, start, end int) {
gtx.Execute(key.SnippetCmd{Tag: e, Snippet: newSnip})
}

func (e *Editor) layout(gtx layout.Context, textMaterial, selectMaterial op.CallOp) layout.Dimensions {
func (e *Editor) layout(gtx layout.Context, textMaterial, selectMaterial op.CallOp, lineMaterial op.CallOp) layout.Dimensions {
// Adjust scrolling for new viewport and layout.
e.text.ScrollRel(0, 0)

Expand Down Expand Up @@ -691,6 +704,7 @@ func (e *Editor) layout(gtx layout.Context, textMaterial, selectMaterial op.Call
if e.Len() > 0 {
e.paintSelection(gtx, selectMaterial)
e.paintText(gtx, textMaterial)
e.paintLineHighlight(gtx, lineMaterial)
}
if gtx.Enabled() {
e.paintCaret(gtx, textMaterial)
Expand Down Expand Up @@ -726,6 +740,11 @@ func (e *Editor) paintCaret(gtx layout.Context, material op.CallOp) {
e.text.PaintCaret(gtx, material)
}

func (e *Editor) paintLineHighlight(gtx layout.Context, material op.CallOp) {
e.initBuffer()
e.text.paintLineHighlight(gtx, material)
}

// Len is the length of the editor contents, in runes.
func (e *Editor) Len() int {
e.initBuffer()
Expand Down Expand Up @@ -1071,6 +1090,11 @@ func (e *Editor) UpdateTextStyles(styles []*TextStyle) {
e.textStyles = styles
}

func (e *Editor) VisibleLines() ([]*LineInfo, error) {
e.initBuffer()
return e.text.VisibleLines()
}

func max(a, b int) int {
if a > b {
return a
Expand Down
Loading

0 comments on commit 0f107d8

Please sign in to comment.