zntg/ui/tooltip.go

65 lines
1.6 KiB
Go
Raw Normal View History

2020-05-15 14:39:53 +00:00
package ui
import (
"image/color"
"opslag.de/schobers/geom"
"opslag.de/schobers/zntg"
)
const tooltipBorderThickness = 1
const tooltipHorizontalPadding = 6
const tooltipVerticalPadding = 2
const tooltipMouseDistance = 12
const uiDefaultTooltipOverlay = "ui-default-tooltip"
type Tooltip struct {
OverlayBase
2020-05-15 14:39:53 +00:00
Text string
}
func (t *Tooltip) FontName(ctx Context) string {
var name = t.Font.Name
if len(name) == 0 {
name = ctx.Style().Fonts.Tooltip
}
return name
}
func (t *Tooltip) Handle(Context, Event) bool { return false }
2020-05-15 14:39:53 +00:00
func (t *Tooltip) Render(ctx Context) {
if len(t.Text) == 0 {
return
}
fontName := t.FontName(ctx)
size := ctx.Fonts().Font(fontName).Measure(t.Text)
offset := t.Offset()
mouse := ctx.MousePosition().Sub(offset)
width := size.Dx() + 2*tooltipBorderThickness + 2*tooltipHorizontalPadding
height := size.Dy() + 2*tooltipBorderThickness + 2*tooltipVerticalPadding
left := mouse.X + tooltipMouseDistance
top := mouse.Y + tooltipMouseDistance
if left+width > t.bounds.Max.X {
left = mouse.X - tooltipMouseDistance - width
}
if top+height > t.bounds.Max.Y {
top = mouse.Y - tooltipMouseDistance - height
}
bounds := geom.RectRelF32(left, top, width, height)
almostBlack := zntg.MustHexColor("#000000bf")
almostWhite := zntg.MustHexColor("ffffffbf")
ctx.Renderer().FillRectangle(bounds, almostBlack)
ctx.Renderer().Rectangle(bounds, almostWhite, 1)
bottomLeft := bounds.Min.Add2D(tooltipBorderThickness+tooltipHorizontalPadding, tooltipBorderThickness+tooltipVerticalPadding)
ctx.Fonts().Text(fontName, bottomLeft, color.White, t.Text)
}