Skip to content

Commit

Permalink
textcore: more api for scrolling and nav in base
Browse files Browse the repository at this point in the history
  • Loading branch information
rcoreilly committed Feb 17, 2025
1 parent 527ad80 commit a3662f1
Show file tree
Hide file tree
Showing 13 changed files with 804 additions and 451 deletions.
8 changes: 4 additions & 4 deletions core/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type Text struct {
Type TextTypes

// Links is the list of links in the text.
Links []rich.LinkRec
Links []rich.Hyperlink

// richText is the conversion of the HTML text source.
richText rich.Text
Expand Down Expand Up @@ -223,7 +223,7 @@ func (tx *Text) Init() {
// tx.paintText.UpdateColors(s.FontRender()) TODO(text):
})

tx.HandleTextClick(func(tl *rich.LinkRec) {
tx.HandleTextClick(func(tl *rich.Hyperlink) {
system.TheApp.OpenURL(tl.URL)
})
tx.OnFocusLost(func(e events.Event) {
Expand Down Expand Up @@ -282,7 +282,7 @@ func (tx *Text) Init() {

// findLink finds the text link at the given scene-local position. If it
// finds it, it returns it and its bounds; otherwise, it returns nil.
func (tx *Text) findLink(pos image.Point) (*rich.LinkRec, image.Rectangle) {
func (tx *Text) findLink(pos image.Point) (*rich.Hyperlink, image.Rectangle) {
if tx.paintText == nil || len(tx.Links) == 0 {
return nil, image.Rectangle{}
}
Expand All @@ -301,7 +301,7 @@ func (tx *Text) findLink(pos image.Point) (*rich.LinkRec, image.Rectangle) {

// HandleTextClick handles click events such that the given function will be called
// on any links that are clicked on.
func (tx *Text) HandleTextClick(openLink func(tl *rich.LinkRec)) {
func (tx *Text) HandleTextClick(openLink func(tl *rich.Hyperlink)) {
tx.OnClick(func(e events.Event) {
tl, _ := tx.findLink(e.Pos())
if tl == nil {
Expand Down
24 changes: 20 additions & 4 deletions core/typegen.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion text/_texteditor/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ func (ed *Editor) Paste() {
func (ed *Editor) InsertAtCursor(txt []byte) {
if ed.HasSelection() {
tbe := ed.deleteSelection()
ed.CursorPos = tbe.AdjustPos(ed.CursorPos, lines.AdjustPosDelStart) // move to start if in reg
ed.CursorPos = tbe.AdjustPos(ed.CursorPos, textpos.AdjustPosDelStart) // move to start if in reg
}
tbe := ed.Buffer.insertText(ed.CursorPos, txt, EditSignal)
if tbe == nil {
Expand Down
94 changes: 88 additions & 6 deletions text/lines/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,31 @@ func (ls *Lines) IsValidLine(ln int) bool {
return ls.isValidLine(ln)
}

// ValidPos returns a position based on given pos that is valid.
func (ls *Lines) ValidPos(pos textpos.Pos) textpos.Pos {
ls.Lock()
defer ls.Unlock()

n := ls.numLines()
if n == 0 {
return textpos.Pos{}
}
if pos.Line < 0 {
pos.Line = 0
}
if pos.Line >= n {
pos.Line = n - 1
}
llen := len(ls.lines[pos.Line])
if pos.Char < 0 {
pos.Char = 0
}
if pos.Char > llen {
pos.Char = llen // end of line is valid
}
return pos
}

// Line returns a (copy of) specific line of runes.
func (ls *Lines) Line(ln int) []rune {
ls.Lock()
Expand Down Expand Up @@ -296,6 +321,26 @@ func (ls *Lines) IsValidPos(pos textpos.Pos) error {
return ls.isValidPos(pos)
}

// PosToView returns the view position in terms of ViewLines and Char
// offset into that view line for given source line, char position.
func (ls *Lines) PosToView(vid int, pos textpos.Pos) textpos.Pos {
ls.Lock()
defer ls.Unlock()
vw := ls.view(vid)
return ls.posToView(vw, pos)
}

// PosFromView returns the original source position from given
// view position in terms of ViewLines and Char offset into that view line.
// If the Char position is beyond the end of the line, it returns the
// end of the given line.
func (ls *Lines) PosFromView(vid int, pos textpos.Pos) textpos.Pos {
ls.Lock()
defer ls.Unlock()
vw := ls.view(vid)
return ls.posFromView(vw, pos)
}

// Region returns a Edit representation of text between start and end positions.
// returns nil if not a valid region. sets the timestamp on the Edit to now.
func (ls *Lines) Region(st, ed textpos.Pos) *textpos.Edit {
Expand Down Expand Up @@ -538,6 +583,43 @@ func (ls *Lines) MoveUp(vw *view, pos textpos.Pos, steps, col int) textpos.Pos {
return ls.moveUp(vw, pos, steps, col)
}

// PosHistorySave saves the cursor position in history stack of cursor positions.
// Tracks across views. Returns false if position was on same line as last one saved.
func (ls *Lines) PosHistorySave(pos textpos.Pos) bool {
ls.Lock()
defer ls.Unlock()
if ls.posHistory == nil {
ls.posHistory = make([]textpos.Pos, 0, 1000)
}
sz := len(ls.posHistory)
if sz > 0 {
if ls.posHistory[sz-1].Line == pos.Line {
return false
}
}
ls.posHistory = append(ls.posHistory, pos)
// fmt.Printf("saved pos hist: %v\n", pos)
return true
}

// PosHistoryLen returns the length of the position history stack.
func (ls *Lines) PosHistoryLen() int {
ls.Lock()
defer ls.Unlock()
return len(ls.posHistory)
}

// PosHistoryAt returns the position history at given index.
// returns false if not a valid index.
func (ls *Lines) PosHistoryAt(idx int) (textpos.Pos, bool) {
ls.Lock()
defer ls.Unlock()
if idx < 0 || idx >= len(ls.posHistory) {
return textpos.Pos{}, false
}
return ls.posHistory[idx], true
}

///////// Edit helpers

// InComment returns true if the given text position is within
Expand Down Expand Up @@ -842,18 +924,18 @@ func (ls *Lines) SetLineColor(ln int, color image.Image) {
ls.lineColors[ln] = color
}

// HasLineColor checks if given line has a line color set
func (ls *Lines) HasLineColor(ln int) bool {
// LineColor returns the line color for given line, and bool indicating if set.
func (ls *Lines) LineColor(ln int) (image.Image, bool) {
ls.Lock()
defer ls.Unlock()
if ln < 0 {
return false
return nil, false
}
if ls.lineColors == nil {
return false
return nil, false
}
_, has := ls.lineColors[ln]
return has
clr, has := ls.lineColors[ln]
return clr, has
}

// DeleteLineColor deletes the line color at the given line.
Expand Down
17 changes: 0 additions & 17 deletions text/lines/move.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,20 +122,3 @@ func (ls *Lines) moveUp(vw *view, pos textpos.Pos, steps, col int) textpos.Pos {
dp := ls.posFromView(vw, nvp)
return dp
}

// SavePosHistory saves the cursor position in history stack of cursor positions.
// Tracks across views. Returns false if position was on same line as last one saved.
func (ls *Lines) SavePosHistory(pos textpos.Pos) bool {
if ls.posHistory == nil {
ls.posHistory = make([]textpos.Pos, 0, 1000)
}
sz := len(ls.posHistory)
if sz > 0 {
if ls.posHistory[sz-1].Line == pos.Line {
return false
}
}
ls.posHistory = append(ls.posHistory, pos)
// fmt.Printf("saved pos hist: %v\n", pos)
return true
}
10 changes: 5 additions & 5 deletions text/rich/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ package rich

import "cogentcore.org/core/text/textpos"

// LinkRec represents a hyperlink within shaped text.
type LinkRec struct {
// Hyperlink represents a hyperlink within shaped text.
type Hyperlink struct {
// Label is the text label for the link.
Label string

Expand All @@ -24,8 +24,8 @@ type LinkRec struct {
}

// GetLinks gets all the links from the source.
func (tx Text) GetLinks() []LinkRec {
var lks []LinkRec
func (tx Text) GetLinks() []Hyperlink {
var lks []Hyperlink
n := len(tx)
for si := range n {
sp := RuneToSpecial(tx[si][0])
Expand All @@ -35,7 +35,7 @@ func (tx Text) GetLinks() []LinkRec {
lr := tx.SpecialRange(si)
ls := tx[lr.Start:lr.End]
s, _ := tx.Span(si)
lk := LinkRec{}
lk := Hyperlink{}
lk.URL = s.URL
sr, _ := tx.Range(lr.Start)
_, er := tx.Range(lr.End)
Expand Down
20 changes: 9 additions & 11 deletions text/rich/typegen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions text/shaped/lines.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ type Lines struct {
Direction rich.Directions

// Links holds any hyperlinks within shaped text.
Links []rich.LinkRec
Links []rich.Hyperlink

// Color is the default fill color to use for inking text.
Color color.Color
Expand Down Expand Up @@ -121,7 +121,7 @@ func (ls *Lines) String() string {
}

// GetLinks gets the links for these lines, which are cached in Links.
func (ls *Lines) GetLinks() []rich.LinkRec {
func (ls *Lines) GetLinks() []rich.Hyperlink {
if ls.Links != nil {
return ls.Links
}
Expand Down
Loading

0 comments on commit a3662f1

Please sign in to comment.