Skip to content

Commit

Permalink
Add TextGridTextStyle
Browse files Browse the repository at this point in the history
enable the text style to be passed in as part of the TextGridStyle.
this will allow font options to be passed down
  • Loading branch information
mgazza committed Sep 13, 2023
1 parent 90192cc commit ad4bc9f
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion widget/textgrid.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,15 @@ type TextGridStyle interface {
BackgroundColor() color.Color
}

// TextGridTextStyle defines the text style that can be applied to a TextGrid cell.
type TextGridTextStyle interface {
TextStyle() *fyne.TextStyle
}

// CustomTextGridStyle is a utility type for those not wanting to define their own style types.
type CustomTextGridStyle struct {
FGColor, BGColor color.Color
Text *fyne.TextStyle
}

// TextColor is the color a cell should use for the text.
Expand All @@ -62,6 +68,11 @@ func (c *CustomTextGridStyle) BackgroundColor() color.Color {
return c.BGColor
}

// TextStyle is the color a cell should use for the background.
func (c *CustomTextGridStyle) TextStyle() *fyne.TextStyle {
return c.Text
}

// TextGrid is a monospaced grid of characters.
// This is designed to be used by a text editor, code preview or terminal emulator.
type TextGrid struct {
Expand Down Expand Up @@ -365,14 +376,27 @@ func (t *textGridRenderer) setCellRune(str rune, pos int, style, rowStyle TextGr

text := t.objects[pos*2+1].(*canvas.Text)
text.TextSize = theme.TextSize()
textStyle := text.TextStyle

fg := theme.ForegroundColor()
if style != nil && style.TextColor() != nil {
fg = style.TextColor()
} else if rowStyle != nil && rowStyle.TextColor() != nil {
fg = rowStyle.TextColor()
}

if ts, ok := style.(TextGridTextStyle); style != nil && ok {
if stylePtr := ts.TextStyle(); stylePtr != nil {
textStyle = *stylePtr
}
} else if ts, ok := rowStyle.(TextGridTextStyle); rowStyle != nil && ok {
if stylePtr := ts.TextStyle(); stylePtr != nil {
textStyle = *stylePtr
}
}

newStr := string(str)
if text.Text != newStr || text.Color != fg {
if text.Text != newStr || text.Color != fg || textStyle != text.TextStyle {
text.Text = newStr
text.Color = fg
t.refresh(text)
Expand Down

0 comments on commit ad4bc9f

Please sign in to comment.