TabContainer & Theme fixes to better show focused state of TabContainer and MenuBar

This commit is contained in:
Luke I. Wilson 2021-04-07 21:06:36 -05:00
parent eb17dfec2e
commit cdeb3be359
4 changed files with 38 additions and 27 deletions

View File

@ -21,7 +21,7 @@ var (
) )
var theme = ui.Theme{ var theme = ui.Theme{
"StatusBar": tcell.Style{}.Foreground(tcell.ColorBlack).Background(tcell.ColorSilver), "StatusBar": tcell.Style{}.Foreground(tcell.ColorBlack).Background(tcell.ColorLightGray),
} }
var ( var (

View File

@ -152,7 +152,12 @@ func (b *MenuBar) CursorRight() {
// Draw renders the MenuBar and its sub-menus. // Draw renders the MenuBar and its sub-menus.
func (b *MenuBar) Draw(s tcell.Screen) { func (b *MenuBar) Draw(s tcell.Screen) {
normalStyle := b.Theme.GetOrDefault("MenuBar") var normalStyle tcell.Style
if b.focused {
normalStyle = b.Theme.GetOrDefault("MenuBarFocused")
} else {
normalStyle = b.Theme.GetOrDefault("MenuBar")
}
// Draw menus based on whether b.focused and which is selected // Draw menus based on whether b.focused and which is selected
DrawRect(s, b.x, b.y, b.width, 1, ' ', normalStyle) DrawRect(s, b.x, b.y, b.width, 1, ' ', normalStyle)
@ -160,7 +165,8 @@ func (b *MenuBar) Draw(s tcell.Screen) {
for i, item := range b.menus { for i, item := range b.menus {
sty := normalStyle sty := normalStyle
if b.focused && b.selected == i { if b.focused && b.selected == i {
sty = b.Theme.GetOrDefault("MenuBarSelected") // Use special style for selected item fg, bg, attr := normalStyle.Decompose()
sty = tcell.Style{}.Foreground(bg).Background(fg).Attributes(attr)
} }
str := fmt.Sprintf(" %s ", item.Name) str := fmt.Sprintf(" %s ", item.Name)

View File

@ -89,23 +89,29 @@ func (c *TabContainer) GetTab(idx int) *Tab {
// Draw will draws the border of the BoxContainer, then it draws its child component. // Draw will draws the border of the BoxContainer, then it draws its child component.
func (c *TabContainer) Draw(s tcell.Screen) { func (c *TabContainer) Draw(s tcell.Screen) {
var styFocused tcell.Style
if c.focused {
styFocused = c.Theme.GetOrDefault("TabContainerFocused")
} else {
styFocused = c.Theme.GetOrDefault("TabContainer")
}
// Draw outline // Draw outline
DrawRectOutlineDefault(s, c.x, c.y, c.width, c.height, c.Theme.GetOrDefault("TabContainer")) DrawRectOutlineDefault(s, c.x, c.y, c.width, c.height, styFocused)
combinedTabLength := 0 combinedTabLength := 0
for _, tab := range c.children { for i := range c.children {
combinedTabLength += len(tab.Name) + 2 // 2 for padding combinedTabLength += len(c.children[i].Name) + 2 // 2 for padding
} }
combinedTabLength += len(c.children) - 1 // add for spacing between tabs combinedTabLength += len(c.children) - 1 // add for spacing between tabs
// Draw tabs // Draw tabs
col := c.x + c.width/2 - combinedTabLength/2 - 1 // Starting column col := c.x + c.width/2 - combinedTabLength/2 // Starting column
for i, tab := range c.children { for i, tab := range c.children {
var sty tcell.Style sty := styFocused
if c.selected == i { if c.selected == i {
sty = c.Theme.GetOrDefault("TabSelected") fg, bg, attr := styFocused.Decompose()
} else { sty = tcell.Style{}.Foreground(bg).Background(fg).Attributes(attr)
sty = c.Theme.GetOrDefault("Tab")
} }
var dirty bool var dirty bool
@ -121,7 +127,7 @@ func (c *TabContainer) Draw(s tcell.Screen) {
str := fmt.Sprintf(" %s ", name) str := fmt.Sprintf(" %s ", name)
DrawStr(s, c.x+col, c.y, str, sty) DrawStr(s, col, c.y, str, sty)
col += len(str) + 1 // Add one for spacing between tabs col += len(str) + 1 // Add one for spacing between tabs
} }

View File

@ -30,18 +30,17 @@ func (theme *Theme) GetOrDefault(key string) tcell.Style {
// DefaultTheme uses only the first 16 colors present in most colored terminals. // DefaultTheme uses only the first 16 colors present in most colored terminals.
var DefaultTheme = Theme{ var DefaultTheme = Theme{
"Normal": tcell.Style{}.Foreground(tcell.ColorSilver).Background(tcell.ColorBlack), "Normal": tcell.Style{}.Foreground(tcell.ColorSilver).Background(tcell.ColorBlack),
"Button": tcell.Style{}.Foreground(tcell.ColorBlack).Background(tcell.ColorWhite), "Button": tcell.Style{}.Foreground(tcell.ColorBlack).Background(tcell.ColorSilver),
"InputField": tcell.Style{}.Foreground(tcell.ColorSilver).Background(tcell.ColorBlack), "InputField": tcell.Style{}.Foreground(tcell.ColorSilver).Background(tcell.ColorBlack),
"MenuBar": tcell.Style{}.Foreground(tcell.ColorBlack).Background(tcell.ColorSilver), "MenuBar": tcell.Style{}.Foreground(tcell.ColorBlack).Background(tcell.ColorDarkGray),
"MenuBarSelected": tcell.Style{}.Foreground(tcell.ColorSilver).Background(tcell.ColorBlack), "MenuBarFocused": tcell.Style{}.Foreground(tcell.ColorBlack).Background(tcell.ColorLightGray),
"Menu": tcell.Style{}.Foreground(tcell.ColorBlack).Background(tcell.ColorSilver), "Menu": tcell.Style{}.Foreground(tcell.ColorBlack).Background(tcell.ColorSilver),
"MenuSelected": tcell.Style{}.Foreground(tcell.ColorSilver).Background(tcell.ColorBlack), "MenuSelected": tcell.Style{}.Foreground(tcell.ColorSilver).Background(tcell.ColorBlack),
"Tab": tcell.Style{}.Foreground(tcell.ColorSilver).Background(tcell.ColorBlack), "TabContainer": tcell.Style{}.Foreground(tcell.ColorGray).Background(tcell.ColorBlack),
"TabContainer": tcell.Style{}.Foreground(tcell.ColorSilver).Background(tcell.ColorBlack), "TabContainerFocused": tcell.Style{}.Foreground(tcell.ColorSilver).Background(tcell.ColorBlack),
"TabSelected": tcell.Style{}.Foreground(tcell.ColorBlack).Background(tcell.ColorSilver),
"TextEdit": tcell.Style{}.Foreground(tcell.ColorSilver).Background(tcell.ColorBlack), "TextEdit": tcell.Style{}.Foreground(tcell.ColorSilver).Background(tcell.ColorBlack),
"TextEditColumn": tcell.Style{}.Foreground(tcell.ColorBlack).Background(tcell.ColorGray), "TextEditColumn": tcell.Style{}.Foreground(tcell.ColorBlack).Background(tcell.ColorGray),
"TextEditSelected": tcell.Style{}.Foreground(tcell.ColorBlack).Background(tcell.ColorSilver), "TextEditSelected": tcell.Style{}.Foreground(tcell.ColorBlack).Background(tcell.ColorSilver),
"Window": tcell.Style{}.Foreground(tcell.ColorBlack).Background(tcell.ColorSilver), "Window": tcell.Style{}.Foreground(tcell.ColorBlack).Background(tcell.ColorDarkGray),
"WindowHeader": tcell.Style{}.Foreground(tcell.ColorBlack).Background(tcell.ColorWhite), "WindowHeader": tcell.Style{}.Foreground(tcell.ColorBlack).Background(tcell.ColorSilver),
} }