Skip to content

Commit

Permalink
cursor setting
Browse files Browse the repository at this point in the history
  • Loading branch information
aarzilli committed Sep 29, 2023
1 parent ce02c8e commit 1cf8a07
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 41 deletions.
4 changes: 2 additions & 2 deletions _examples/richtext/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,9 @@ func updatefn(w *nucular.Window) {
switch selected {
case 0:
c.Align(richtext.Align(align))
c.SetStyle(richtext.TextStyle{Face: header})
c.SetStyle(richtext.TextStyle{Face: header, Cursor: font.TextCursor})
c.Text("Vispa Teresa\n")
c.SetStyle(richtext.TextStyle{Face: proportional})
c.SetStyle(richtext.TextStyle{Face: proportional, Cursor: font.TextCursor})
c.Text("\n")
c.Text("La vispa Teresa\navea tra l'erbetta\na volo sorpresa\ngentil farfalletta\n\n")
c.Text("E tutta giuliva\nstringendola viva\ngridava a distesa\nl'ho presa! l'ho presa!\n\n")
Expand Down
14 changes: 14 additions & 0 deletions command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type Command struct {
CircleFilled CircleFilled
Image Image
Text Text
Cursor font.Cursor
}

type CommandKind uint8
Expand All @@ -43,6 +44,7 @@ const (
CircleFilledCmd
ImageCmd
TextCmd
CursorCmd
)

type Line struct {
Expand Down Expand Up @@ -179,3 +181,15 @@ func (b *Buffer) DrawImage(r rect.Rect, img *image.RGBA) {
cmd.Image.Img = img
b.Commands = append(b.Commands, cmd)
}

func (b *Buffer) Cursor(r rect.Rect, cursor font.Cursor) {
if !r.Intersect(&b.Clip) {
return
}

var cmd Command
cmd.Kind = CursorCmd
cmd.Rect = r
cmd.Cursor = cursor
b.Commands = append(b.Commands, cmd)
}
11 changes: 11 additions & 0 deletions font/cursor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package font

type Cursor uint8

const (
DefaultCursor Cursor = iota
NoCursor
TextCursor
PointerCursor
ProgressCursor
)
14 changes: 14 additions & 0 deletions gio.go
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,12 @@ func (ctx *context) Draw(ops *op.Ops, size image.Point, perf bool, perfString st
drawText(ops, charAtlas, icmd.Text.Face, icmd.Text.Foreground, icmd.Rect, icmd.Text.String)
stack.Pop()

case command.CursorCmd:
stack := gioclip.Rect(icmd.Rect.Rectangle()).Push(ops)
c := font2pointerCursor[icmd.Cursor]
c.Add(ops)
stack.Pop()

default:
panic(UnknownCommandErr)
}
Expand All @@ -589,6 +595,14 @@ func (ctx *context) Draw(ops *op.Ops, size image.Point, perf bool, perfString st
return len(ctx.cmds)
}

var font2pointerCursor = map[font.Cursor]pointer.Cursor{
font.DefaultCursor: pointer.CursorDefault,
font.NoCursor: pointer.CursorNone,
font.TextCursor: pointer.CursorText,
font.PointerCursor: pointer.CursorPointer,
font.ProgressCursor: pointer.CursorProgress,
}

func drawText(ops *op.Ops, charAtlas map[charAtlasKey]map[rune]renderedGlyph, fontFace font.Face, foreground color.RGBA, rect rect.Rect, str string) {
m := charAtlas[charAtlasKey{fontFace, foreground}]
if m == nil {
Expand Down
27 changes: 1 addition & 26 deletions masterwindow.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,7 @@ func (w *masterWindowCommon) drawChanged() bool {
pcmd := &w.prevCmds[i]

switch cmds[i].Kind {
case command.ScissorCmd:
if *pcmd != *cmd {
return true
}

case command.LineCmd:
case command.ScissorCmd, command.LineCmd, command.TriangleFilledCmd, command.CircleFilledCmd, command.ImageCmd, command.TextCmd, command.CursorCmd:
if *pcmd != *cmd {
return true
}
Expand All @@ -210,26 +205,6 @@ func (w *masterWindowCommon) drawChanged() bool {
return true
}

case command.TriangleFilledCmd:
if *pcmd != *cmd {
return true
}

case command.CircleFilledCmd:
if *pcmd != *cmd {
return true
}

case command.ImageCmd:
if *pcmd != *cmd {
return true
}

case command.TextCmd:
if *pcmd != *cmd {
return true
}

default:
panic(UnknownCommandErr)
}
Expand Down
37 changes: 26 additions & 11 deletions richtext/draw.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,22 +201,22 @@ func (rtxt *RichText) drawRows(w *nucular.Window, viewporth int) *Ctor {
s := rtxt.Sel.S - line.off[i]
chunk1 := chunk.sub(0, s)
w1 := line.chunkWidthEx(i, 0, chunk1, rtxt.adv)
drawChunk(w, out, &p, chunk1, siter.styleSel, w1, line.h, line.asc)
drawChunk(w, out, &p, chunk1, siter.styleSel, w1, line.h, line.asc, rowSpacing)

if chunkrng.contains(rtxt.Sel.E) {
e := rtxt.Sel.E - line.off[i]
chunk2 := chunk.sub(s, e)
w2 := line.chunkWidthEx(i, s, chunk2, rtxt.adv)
rtxt.drawSelectedChunk(w, out, &p, chunk2, siter.styleSel, w2, line.h, line.asc)
rtxt.drawSelectedChunk(w, out, &p, chunk2, siter.styleSel, w2, line.h, line.asc, rowSpacing)

chunk3 := chunk.sub(e, chunk.len())
w3 := line.chunkWidthEx(i, e, chunk3, rtxt.adv)
drawChunk(w, out, &p, chunk3, siter.styleSel, w3, line.h, line.asc)
drawChunk(w, out, &p, chunk3, siter.styleSel, w3, line.h, line.asc, rowSpacing)
insel = selAfter
} else {
chunk2 := chunk.sub(s, chunk.len())
w2 := line.chunkWidthEx(i, s, chunk2, rtxt.adv)
rtxt.drawSelectedChunk(w, out, &p, chunk2, siter.styleSel, w2, line.h, line.asc)
rtxt.drawSelectedChunk(w, out, &p, chunk2, siter.styleSel, w2, line.h, line.asc, rowSpacing)
insel = selInside
}
} else {
Expand All @@ -228,17 +228,17 @@ func (rtxt *RichText) drawRows(w *nucular.Window, viewporth int) *Ctor {
e := rtxt.Sel.E - line.off[i]
chunk1 := chunk.sub(0, e)
w1 := line.chunkWidthEx(i, 0, chunk1, rtxt.adv)
rtxt.drawSelectedChunk(w, out, &p, chunk1, siter.styleSel, w1, line.h, line.asc)
rtxt.drawSelectedChunk(w, out, &p, chunk1, siter.styleSel, w1, line.h, line.asc, rowSpacing)

chunk2 := chunk.sub(e, chunk.len())
w2 := line.chunkWidthEx(i, e, chunk2, rtxt.adv)
drawChunk(w, out, &p, chunk2, siter.styleSel, w2, line.h, line.asc)
drawChunk(w, out, &p, chunk2, siter.styleSel, w2, line.h, line.asc, rowSpacing)
insel = selAfter
} else if chunkrng.S >= rtxt.Sel.E {
insel = selAfter
simpleDrawChunk = true
} else {
rtxt.drawSelectedChunk(w, out, &p, chunk, siter.styleSel, line.w[i], line.h, line.asc)
rtxt.drawSelectedChunk(w, out, &p, chunk, siter.styleSel, line.w[i], line.h, line.asc, rowSpacing)
}

case selAfter:
Expand All @@ -249,7 +249,7 @@ func (rtxt *RichText) drawRows(w *nucular.Window, viewporth int) *Ctor {
}

if simpleDrawChunk {
drawChunk(w, out, &p, chunk, siter.styleSel, line.w[i], line.h, line.asc)
drawChunk(w, out, &p, chunk, siter.styleSel, line.w[i], line.h, line.asc, rowSpacing)
if insel == selTick && (rtxt.flags&ShowTick != 0) && (rtxt.wasFocused || (rtxt.flags&Keyboard == 0 && rtxt.flags&Editable == 0)) && chunkrng.contains(rtxt.Sel.S) {
x := p.X - line.w[i] + line.chunkWidth(i, rtxt.Sel.S-line.off[i], rtxt.adv)
rtxt.drawTick(w, out, image.Point{x, p.Y}, line.h, siter.styleSel.Color, lineidx)
Expand Down Expand Up @@ -323,13 +323,13 @@ func (rtxt *RichText) drawTick(w *nucular.Window, out *command.Buffer, p image.P
out.StrokeLine(image.Point{p.X, p.Y}, image.Point{p.X, p.Y + lineh}, linethick, color)
}

func (rtxt *RichText) drawSelectedChunk(w *nucular.Window, out *command.Buffer, p *image.Point, chunk chunk, styleSel styleSel, width, lineh, lineasc int) {
func (rtxt *RichText) drawSelectedChunk(w *nucular.Window, out *command.Buffer, p *image.Point, chunk chunk, styleSel styleSel, width, lineh, lineasc int, rowSpacing int) {
styleSel.BgColor = rtxt.selColor
styleSel.Color = styleSel.SelFgColor
drawChunk(w, out, p, chunk, styleSel, width, lineh, lineasc)
drawChunk(w, out, p, chunk, styleSel, width, lineh, lineasc, rowSpacing)
}

func drawChunk(w *nucular.Window, out *command.Buffer, p *image.Point, chunk chunk, styleSel styleSel, width, lineh, lineasc int) {
func drawChunk(w *nucular.Window, out *command.Buffer, p *image.Point, chunk chunk, styleSel styleSel, width, lineh, lineasc int, rowSpacing int) {
if chunk.isspacing() {
if debugDrawBoundingBoxes && width > 0 {
yoff := alignBaseline(lineh, lineasc, styleSel.Face)
Expand All @@ -340,6 +340,10 @@ func drawChunk(w *nucular.Window, out *command.Buffer, p *image.Point, chunk chu
r := rect.Rect{X: p.X, Y: p.Y, H: lineh, W: width}
out.FillRect(r, 0, styleSel.BgColor)
}
if styleSel.Cursor != 0 {
b2 := rect.Rect{X: p.X, Y: p.Y, H: lineh + rowSpacing, W: width}
out.Cursor(b2, styleSel.Cursor)
}
} else {
r := rect.Rect{X: p.X, Y: p.Y, H: lineh, W: width}

Expand Down Expand Up @@ -374,6 +378,17 @@ func drawChunk(w *nucular.Window, out *command.Buffer, p *image.Point, chunk chu
y := p.Y + lineasc + m.Descent.Ceil() - m.Ascent.Ceil()/2
out.StrokeLine(image.Point{p.X, y}, image.Point{p.X + width, y}, linethick, styleSel.Color)
}

b2 := r
b2.H += rowSpacing

if styleSel.isLink && styleSel.Cursor == 0 {
out.Cursor(b2, font.PointerCursor)
}

if styleSel.Cursor != 0 {
out.Cursor(b2, styleSel.Cursor)
}
}

p.X += width
Expand Down
5 changes: 3 additions & 2 deletions richtext/rtxt.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,9 @@ func (chunk chunk) str() string {
}

type TextStyle struct {
Face font.Face
Flags FaceFlags
Face font.Face
Cursor font.Cursor
Flags FaceFlags

Color, SelFgColor, BgColor color.RGBA // foreground color, selected foreground color, background color

Expand Down
2 changes: 2 additions & 0 deletions shiny.go
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,8 @@ func (ctx *context) Draw(wimg *image.RGBA) int {
if perfUpdate {
txttim += time.Since(t0)
}
case command.CursorCmd:
// not supported by shiny
default:
panic(UnknownCommandErr)
}
Expand Down
1 change: 1 addition & 0 deletions text.go
Original file line number Diff line number Diff line change
Expand Up @@ -1409,6 +1409,7 @@ func (d *drawableTextEditor) Draw(z *nstyle.Style, out *command.Buffer) {
state := d.State
style := d.Style
bounds := d.Bounds
out.Cursor(d.Area, font.TextCursor)
font := z.Font
area := d.Area
row_height := d.RowHeight
Expand Down

0 comments on commit 1cf8a07

Please sign in to comment.