From cdeb3be359b5e7209001485c1e64fc34d76ba76f Mon Sep 17 00:00:00 2001 From: "Luke I. Wilson" Date: Wed, 7 Apr 2021 21:06:36 -0500 Subject: [PATCH] TabContainer & Theme fixes to better show focused state of TabContainer and MenuBar --- main.go | 2 +- ui/menu.go | 10 ++++++++-- ui/tabcontainer.go | 24 +++++++++++++++--------- ui/theme.go | 29 ++++++++++++++--------------- 4 files changed, 38 insertions(+), 27 deletions(-) diff --git a/main.go b/main.go index b655b30..df2ba7e 100644 --- a/main.go +++ b/main.go @@ -21,7 +21,7 @@ var ( ) var theme = ui.Theme{ - "StatusBar": tcell.Style{}.Foreground(tcell.ColorBlack).Background(tcell.ColorSilver), + "StatusBar": tcell.Style{}.Foreground(tcell.ColorBlack).Background(tcell.ColorLightGray), } var ( diff --git a/ui/menu.go b/ui/menu.go index 5f8c1c4..cf33dd2 100644 --- a/ui/menu.go +++ b/ui/menu.go @@ -152,7 +152,12 @@ func (b *MenuBar) CursorRight() { // Draw renders the MenuBar and its sub-menus. 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 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 { sty := normalStyle 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) diff --git a/ui/tabcontainer.go b/ui/tabcontainer.go index d0c12f9..5759e6e 100644 --- a/ui/tabcontainer.go +++ b/ui/tabcontainer.go @@ -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. 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 - 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 - for _, tab := range c.children { - combinedTabLength += len(tab.Name) + 2 // 2 for padding + for i := range c.children { + combinedTabLength += len(c.children[i].Name) + 2 // 2 for padding } combinedTabLength += len(c.children) - 1 // add for spacing between 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 { - var sty tcell.Style + sty := styFocused if c.selected == i { - sty = c.Theme.GetOrDefault("TabSelected") - } else { - sty = c.Theme.GetOrDefault("Tab") + fg, bg, attr := styFocused.Decompose() + sty = tcell.Style{}.Foreground(bg).Background(fg).Attributes(attr) } var dirty bool @@ -121,7 +127,7 @@ func (c *TabContainer) Draw(s tcell.Screen) { 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 } diff --git a/ui/theme.go b/ui/theme.go index 5188df1..78ae46b 100644 --- a/ui/theme.go +++ b/ui/theme.go @@ -29,19 +29,18 @@ func (theme *Theme) GetOrDefault(key string) tcell.Style { // DefaultTheme uses only the first 16 colors present in most colored terminals. var DefaultTheme = Theme{ - "Normal": tcell.Style{}.Foreground(tcell.ColorSilver).Background(tcell.ColorBlack), - "Button": tcell.Style{}.Foreground(tcell.ColorBlack).Background(tcell.ColorWhite), - "InputField": tcell.Style{}.Foreground(tcell.ColorSilver).Background(tcell.ColorBlack), - "MenuBar": tcell.Style{}.Foreground(tcell.ColorBlack).Background(tcell.ColorSilver), - "MenuBarSelected": tcell.Style{}.Foreground(tcell.ColorSilver).Background(tcell.ColorBlack), - "Menu": tcell.Style{}.Foreground(tcell.ColorBlack).Background(tcell.ColorSilver), - "MenuSelected": tcell.Style{}.Foreground(tcell.ColorSilver).Background(tcell.ColorBlack), - "Tab": tcell.Style{}.Foreground(tcell.ColorSilver).Background(tcell.ColorBlack), - "TabContainer": 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), - "TextEditColumn": tcell.Style{}.Foreground(tcell.ColorBlack).Background(tcell.ColorGray), - "TextEditSelected": tcell.Style{}.Foreground(tcell.ColorBlack).Background(tcell.ColorSilver), - "Window": tcell.Style{}.Foreground(tcell.ColorBlack).Background(tcell.ColorSilver), - "WindowHeader": tcell.Style{}.Foreground(tcell.ColorBlack).Background(tcell.ColorWhite), + "Normal": tcell.Style{}.Foreground(tcell.ColorSilver).Background(tcell.ColorBlack), + "Button": tcell.Style{}.Foreground(tcell.ColorBlack).Background(tcell.ColorSilver), + "InputField": tcell.Style{}.Foreground(tcell.ColorSilver).Background(tcell.ColorBlack), + "MenuBar": tcell.Style{}.Foreground(tcell.ColorBlack).Background(tcell.ColorDarkGray), + "MenuBarFocused": tcell.Style{}.Foreground(tcell.ColorBlack).Background(tcell.ColorLightGray), + "Menu": tcell.Style{}.Foreground(tcell.ColorBlack).Background(tcell.ColorSilver), + "MenuSelected": tcell.Style{}.Foreground(tcell.ColorSilver).Background(tcell.ColorBlack), + "TabContainer": tcell.Style{}.Foreground(tcell.ColorGray).Background(tcell.ColorBlack), + "TabContainerFocused": 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), + "TextEditSelected": 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.ColorSilver), }