mirror of
https://github.com/bcicen/ctop.git
synced 2024-08-30 18:23:19 +00:00
add optional tooltip to menu widget
This commit is contained in:
parent
f11a705b8b
commit
09566a4043
1
menus.go
1
menus.go
@ -129,6 +129,7 @@ func ColumnsMenu() MenuFn {
|
|||||||
m.Selectable = true
|
m.Selectable = true
|
||||||
m.SortItems = false
|
m.SortItems = false
|
||||||
m.BorderLabel = "Columns"
|
m.BorderLabel = "Columns"
|
||||||
|
//m.SubText = "Enabled Columns"
|
||||||
|
|
||||||
rebuild := func() {
|
rebuild := func() {
|
||||||
// get padding for right alignment of enabled status
|
// get padding for right alignment of enabled status
|
||||||
|
@ -11,13 +11,14 @@ type Padding [2]int // x,y padding
|
|||||||
type Menu struct {
|
type Menu struct {
|
||||||
ui.Block
|
ui.Block
|
||||||
SortItems bool // enable automatic sorting of menu items
|
SortItems bool // enable automatic sorting of menu items
|
||||||
|
Selectable bool // whether menu is navigable
|
||||||
SubText string // optional text to display before items
|
SubText string // optional text to display before items
|
||||||
TextFgColor ui.Attribute
|
TextFgColor ui.Attribute
|
||||||
TextBgColor ui.Attribute
|
TextBgColor ui.Attribute
|
||||||
Selectable bool
|
|
||||||
cursorPos int
|
cursorPos int
|
||||||
items Items
|
items Items
|
||||||
padding Padding
|
padding Padding
|
||||||
|
toolTip *ToolTip
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewMenu() *Menu {
|
func NewMenu() *Menu {
|
||||||
@ -71,13 +72,9 @@ func (m *Menu) SetCursor(s string) (success bool) {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sort menu items(if enabled) and re-calculate window size
|
// SetToolTip sets an optional tooltip string to show at bottom of screen
|
||||||
func (m *Menu) refresh() {
|
func (m *Menu) SetToolTip(lines ...string) {
|
||||||
if m.SortItems {
|
m.toolTip = NewToolTip(lines...)
|
||||||
sort.Sort(m.items)
|
|
||||||
}
|
|
||||||
m.calcSize()
|
|
||||||
ui.Render(m)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Menu) SelectedItem() Item {
|
func (m *Menu) SelectedItem() Item {
|
||||||
@ -117,6 +114,10 @@ func (m *Menu) Buffer() ui.Buffer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if m.toolTip != nil {
|
||||||
|
buf.Merge(m.toolTip.Buffer())
|
||||||
|
}
|
||||||
|
|
||||||
return buf
|
return buf
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -134,6 +135,15 @@ func (m *Menu) Down() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Sort menu items(if enabled) and re-calculate window size
|
||||||
|
func (m *Menu) refresh() {
|
||||||
|
if m.SortItems {
|
||||||
|
sort.Sort(m.items)
|
||||||
|
}
|
||||||
|
m.calcSize()
|
||||||
|
ui.Render(m)
|
||||||
|
}
|
||||||
|
|
||||||
// Set width and height based on menu items
|
// Set width and height based on menu items
|
||||||
func (m *Menu) calcSize() {
|
func (m *Menu) calcSize() {
|
||||||
m.Width = 7 // minimum width
|
m.Width = 7 // minimum width
|
||||||
|
55
widgets/menu/tooltip.go
Normal file
55
widgets/menu/tooltip.go
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
package menu
|
||||||
|
|
||||||
|
import (
|
||||||
|
ui "github.com/gizak/termui"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ToolTip struct {
|
||||||
|
ui.Block
|
||||||
|
Lines []string
|
||||||
|
TextFgColor ui.Attribute
|
||||||
|
TextBgColor ui.Attribute
|
||||||
|
padding Padding
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewToolTip(lines ...string) *ToolTip {
|
||||||
|
t := &ToolTip{
|
||||||
|
Block: *ui.NewBlock(),
|
||||||
|
Lines: lines,
|
||||||
|
TextFgColor: ui.ThemeAttr("menu.text.fg"),
|
||||||
|
TextBgColor: ui.ThemeAttr("menu.text.bg"),
|
||||||
|
padding: Padding{2, 1},
|
||||||
|
}
|
||||||
|
t.BorderFg = ui.ThemeAttr("menu.border.fg")
|
||||||
|
t.BorderLabelFg = ui.ThemeAttr("menu.label.fg")
|
||||||
|
t.X = 1
|
||||||
|
t.Align()
|
||||||
|
return t
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *ToolTip) Buffer() ui.Buffer {
|
||||||
|
var cell ui.Cell
|
||||||
|
buf := t.Block.Buffer()
|
||||||
|
|
||||||
|
y := t.Y + t.padding[1]
|
||||||
|
|
||||||
|
for n, line := range t.Lines {
|
||||||
|
x := t.X + t.padding[0]
|
||||||
|
for _, ch := range line {
|
||||||
|
cell = ui.Cell{Ch: ch, Fg: t.TextFgColor, Bg: t.TextBgColor}
|
||||||
|
buf.Set(x, y+n, cell)
|
||||||
|
x++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return buf
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set width and height based on screen size
|
||||||
|
func (t *ToolTip) Align() {
|
||||||
|
t.Width = ui.TermWidth() - (t.padding[0] * 2)
|
||||||
|
t.Height = len(t.Lines) + (t.padding[1] * 2)
|
||||||
|
t.Y = ui.TermHeight() - t.Height
|
||||||
|
|
||||||
|
t.Block.Align()
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user