Merge branch 'tcell-events' into feature/tcell-events-vuong

pull/232/head
Simon Roberts 3 years ago
commit fb6624c362
No known key found for this signature in database
GPG Key ID: 0F30F99E6B771FD4

@ -269,7 +269,7 @@ func (v *View) writeRune(x, y int, ch rune) error {
copy(v.lines[y][x+1:], v.lines[y][x:])
}
v.lines[y][x] = cell{
style: v.g.Style(v.FgColor, v.BgColor),
style: v.Style,
chr: ch,
}

@ -44,11 +44,11 @@ type Gui struct {
// BgColor and FgColor allow to configure the background and foreground
// colors of the GUI.
BgColor, FgColor tcell.Color
Style tcell.Style
// SelBgColor and SelFgColor allow to configure the background and
// foreground colors of the frame of the current view.
SelBgColor, SelFgColor tcell.Color
SelStyle tcell.Style
// If Highlight is true, Sel{Bg,Fg}Colors will be used to draw the
// frame of the current view.
@ -95,8 +95,8 @@ func NewGui() (*Gui, error) {
g.maxX, g.maxY = g.screen.Size()
g.BgColor, g.FgColor = tcell.ColorDefault, tcell.ColorDefault
g.SelBgColor, g.SelFgColor = tcell.ColorDefault, tcell.ColorDefault
g.Style = tcell.StyleDefault
g.SelStyle = tcell.StyleDefault
return g, nil
}
@ -178,8 +178,8 @@ func (g *Gui) SetView(name string, x0, y0, x1, y1 int) (*View, error) {
}
v := newView(name, x0, y0, x1, y1, g)
v.BgColor, v.FgColor = g.BgColor, g.FgColor
v.SelBgColor, v.SelFgColor = g.SelBgColor, g.SelFgColor
v.Style = g.Style
v.SelStyle = g.SelStyle
g.views = append(g.views, v)
return v, ErrUnknownView
}
@ -471,76 +471,13 @@ func (g *Gui) handleEvent(ev tcell.Event) error {
}
}
// TODO: delete termbox compat
func (g *Gui) fixColor(c tcell.Color) tcell.Color {
if c == tcell.ColorDefault {
return c
}
c = tcell.PaletteColor(int(c) & 0xff)
// switch g.outputMode {
// case OutputNormal:
// c = tcell.PaletteColor(int(c) & 0xf)
// case Output256:
// c = tcell.PaletteColor(int(c) & 0xff)
// case Output216:
// c = tcell.PaletteColor(int(c)%216 + 16)
// case OutputGrayscale:
// c %= tcell.PaletteColor(int(c)%24 + 232)
// default:
// c = tcell.ColorDefault
// }
return c
}
func (g *Gui) Style(fg, bg tcell.Color) tcell.Style {
return tcell.StyleDefault.Foreground(fg).Background(bg)
}
/*
func (g *Gui) MkColor(color Attribute) tcell.Color {
if color == ColorDefault {
return tcell.ColorDefault
} else {
return g.fixColor(tcell.PaletteColor(int(color)&0x1ff - 1))
}
}
// TODO: delete termbox compat
func (g *Gui) MkStyle(fg, bg Attribute) tcell.Style {
st := tcell.StyleDefault
if fg != ColorDefault {
f := tcell.PaletteColor(int(fg)&0x1ff - 1)
f = g.fixColor(f)
st = st.Foreground(f)
}
if bg != ColorDefault {
b := tcell.PaletteColor(int(bg)&0x1ff - 1)
b = g.fixColor(b)
st = st.Background(b)
}
// TODO: fixme
// if (fg|bg)&AttrBold != 0 {
// st = st.Bold(true)
// }
// if (fg|bg)&AttrUnderline != 0 {
// st = st.Underline(true)
// }
// if (fg|bg)&AttrReverse != 0 {
// st = st.Reverse(true)
// }
return st
}
*/
// flush updates the gui, re-drawing frames and buffers.
func (g *Gui) flush() error {
// termbox.Clear(termbox.Attribute(g.FgColor), termbox.Attribute(g.BgColor))
st := g.Style(g.FgColor, g.BgColor)
w, h := g.screen.Size() // TODO: merge with maxX, maxY below
for row := 0; row < h; row++ {
for col := 0; col < w; col++ {
g.screen.SetContent(col, row, ' ', nil, st)
g.screen.SetContent(col, row, ' ', nil, g.Style)
}
}
@ -560,9 +497,10 @@ func (g *Gui) flush() error {
}
for _, v := range g.views {
if v.Frame {
st := g.Style(v.FgColor, v.BgColor)
// var fgColor, bgColor Attribute
st := g.Style
if g.Highlight && v == g.currentView {
st = g.Style(g.SelFgColor, g.SelBgColor)
st = g.SelStyle
}
if err := g.drawFrameEdges(v, st); err != nil {

@ -31,11 +31,11 @@ type View struct {
// BgColor and FgColor allow to configure the background and foreground
// colors of the View.
BgColor, FgColor tcell.Color
Style tcell.Style
// SelBgColor and SelFgColor are used to configure the background and
// foreground colors of the selected line, when it is highlighted.
SelBgColor, SelFgColor tcell.Color // TODO: merge to tcell.Style
SelStyle tcell.Style
// If Editable is true, keystrokes will be added to the view's internal
// buffer at the cursor position.
@ -150,10 +150,10 @@ func (v *View) setRune(x, y int, ch rune, st tcell.Style) error {
}
if v.Mask != 0 {
st = v.g.Style(v.FgColor, v.BgColor)
st = v.Style
ch = v.Mask
} else if v.Highlight && ry == rcy {
st = v.g.Style(v.SelFgColor, v.SelBgColor)
st = v.SelStyle
}
v.g.SetRune(v.x0+x+1, v.y0+y+1, ch, st)
@ -242,7 +242,7 @@ func (v *View) parseInput(ch rune) []cell {
if err != nil {
for _, r := range v.ei.runes() {
c := cell{
style: v.g.Style(v.FgColor, v.BgColor),
style: v.Style,
chr: r,
}
cells = append(cells, c)
@ -343,11 +343,12 @@ func (v *View) draw() error {
st := c.style
fgColor, bgColor, _ := c.style.Decompose()
vfgColor, vbgColor, _ := v.Style.Decompose()
if fgColor == tcell.ColorDefault {
st = st.Foreground(v.FgColor)
st = st.Foreground(vfgColor)
}
if bgColor == tcell.ColorDefault {
st = st.Background(v.BgColor)
st = st.Background(vbgColor)
}
if err := v.setRune(x, y, c.chr, st); err != nil {
return err
@ -399,10 +400,9 @@ func (v *View) Clear() {
// clearRunes erases all the cells in the view.
func (v *View) clearRunes() {
maxX, maxY := v.Size()
st := v.g.Style(v.FgColor, v.BgColor)
for x := 0; x < maxX; x++ {
for y := 0; y < maxY; y++ {
v.g.SetRune(v.x0+x+1, v.y0+y+1, ' ', st)
v.g.SetRune(v.x0+x+1, v.y0+y+1, ' ', v.Style)
}
}
}

@ -29,12 +29,12 @@ func (ui *UI) GetGocui() *gocui.Gui {
// SetFgColor sets the foreground color
func (ui *UI) SetFgColor(fgColor tcell.Color) {
ui.g.FgColor = fgColor
ui.g.Style = ui.g.Style.Foreground(fgColor)
}
// SetBgColor sets the background color
func (ui *UI) SetBgColor(bgColor tcell.Color) {
ui.g.BgColor = bgColor
ui.g.Style = ui.g.Style.Background(bgColor)
}
// SetInputEsc enables the escape key

@ -217,28 +217,29 @@ func (view *View) SetWrap(enabled bool) error {
// SetFgColor sets the foreground color
func (view *View) SetFgColor(color tcell.Color) {
if view.HasBacking() {
view.backing.FgColor = color
view.backing.Style = view.backing.Style.Foreground(color)
}
}
// SetBgColor sets the background color
func (view *View) SetBgColor(color tcell.Color) {
if view.HasBacking() {
view.backing.BgColor = color
// view.backing.BgColor = color
view.backing.Style = view.backing.Style.Background(color)
}
}
// SetSelFgColor sets the foreground color for selection
func (view *View) SetSelFgColor(color tcell.Color) {
if view.HasBacking() {
view.backing.SelFgColor = color
view.backing.SelStyle = view.backing.SelStyle.Foreground(color)
}
}
// SetSelBgColor sets the background color for selection
func (view *View) SetSelBgColor(color tcell.Color) {
if view.HasBacking() {
view.backing.SelBgColor = color
view.backing.SelStyle = view.backing.SelStyle.Background(color)
}
}

Loading…
Cancel
Save