Skip to content
Merged
3 changes: 2 additions & 1 deletion base/box.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,8 @@ func (b Box) MonoFace(Face gfx.MonoFace) Box {

// Ellipsis keeps a Text to one line and ends it with an ellipsis when it does not
// fit the width it gets, instead of clipping it. It is Paragraph(value, 1).
// In a Page, hovering truncated text shows its complete value in a tooltip.
// A page with EllipsisTooltips, or an EllipsisTooltip on the Text or around it,
// shows the complete value in a tooltip while the pointer rests on the text.
func (b Box) Ellipsis() Box {
n := b.N()
n.Flags |= flagWrap
Expand Down
148 changes: 148 additions & 0 deletions base/ellipsis_tooltip.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
package base

import (
"slices"

"github.com/vivaui/viva/gfx"
)

// What EllipsisTooltip and EllipsisTooltipOn set on an element, in one byte so
// that painting checks a single field. The low bits are the switch, ellipsisTipOn
// or ellipsisTipOff; the bits above are 1 + the side EllipsisTooltipOn gave.
// Zero in either part follows what the element is inside.
const (
ellipsisTipOn uint8 = 1 + iota
ellipsisTipOff

ellipsisTipSwitch uint8 = 3
ellipsisTipSideShift = 2
)

// ellipsisState is what the text being painted does with the tooltips of
// shortened text: whether EllipsisTooltip's show, and 1 + the side every one
// of them opens on, OverflowTooltip's too; a zero side opens them below.
type ellipsisState struct {
on bool
side uint8
}

// popupFrom is the element a Popup was declared in, which it takes no room in
// but follows for EllipsisTooltip when it hangs off no element.
type popupFrom struct{ popup, from int32 }

// EllipsisTooltip turns on, or with false off, the tooltip that shows the
// complete value of text shortened with an ellipsis, for the element and what
// it holds: a Text cut by Ellipsis or MaxLines, and the labels components
// shorten themselves, such as Table cells, Select values and menu items. It
// overrides PageConfig.EllipsisTooltips and an EllipsisTooltip further out. A
// Popup follows the element it hangs off (BelowBox and the like) or, placed by
// a rectangle or a point, the element it was declared in.
//
// ui.Text(c, path).Ellipsis().EllipsisTooltip(true)
// ui.Table(c, &table, spec).EllipsisTooltip(true)
func (b Box) EllipsisTooltip(on bool) Box {
b.N()
tip := ellipsisTipOff
if on {
tip = ellipsisTipOn
}
x := b.f.Extra(b.i)
x.ellipsisTip = x.ellipsisTip&^ellipsisTipSwitch | tip
b.f.ellipsisSet = true
return b
}

// EllipsisTooltipOn opens the tooltips of the shortened text in the element and
// what it holds beside side of the text rather than below it, as TooltipOn does
// for a Tooltip: those EllipsisTooltip shows and those OverflowTooltip gives. A
// list in a sidebar on the window's left opens them to its right, over the
// content beside it, and a status bar along the window's bottom opens them
// above it. It does not turn them on. The nearest EllipsisTooltipOn decides, and
// a Popup follows it as it follows EllipsisTooltip. Without room on side, a
// tooltip opens on the opposite one.
//
// ui.Column(c).EllipsisTooltipOn(ui.TooltipRight).Children(func() { … })
func (b Box) EllipsisTooltipOn(side TooltipSide) Box {
b.N()
x := b.f.Extra(b.i)
x.ellipsisTip = x.ellipsisTip&ellipsisTipSwitch | (uint8(side)+1)<<ellipsisTipSideShift
b.f.ellipsisSet = true
return b
}

// under is the state inside an element given set, from state outside it.
func (state ellipsisState) under(set uint8) ellipsisState {
if tip := set & ellipsisTipSwitch; tip != 0 {
state.on = tip == ellipsisTipOn
}
if side := set >> ellipsisTipSideShift; side != 0 {
state.side = side
}
return state
}

// setEllipsis sets what is painted next does with the tooltips of shortened
// text, and returns what it did. Components see it on the canvas: as the hover
// hint owner, which is zero while EllipsisTooltip's are off, and the hint side.
func (f *Frame) setEllipsis(state ellipsisState) ellipsisState {
was := f.ellipsis
if state != was && f.page != nil && f.Surface != nil {
f.ellipsis = state
if state.on != was.on {
var owner gfx.Scope
if state.on {
owner = f.page.root
}
f.Surface.SetHoverHintOwner(owner)
}
if state.side != was.side {
f.Surface.SetHoverHintSide(state.side)
}
}
return was
}

// restoreHoverHints gives the canvas back the hover hint owner and side it had
// before the frame painted, those of the page whose pane holds this one.
func (f *Frame) restoreHoverHints(owner gfx.Scope, side uint8) {
f.Surface.SetHoverHintOwner(owner)
f.Surface.SetHoverHintSide(side)
}

// ellipsisAt is the state element i paints in: the nearest EllipsisTooltip and
// EllipsisTooltipOn on it or on what it is inside, or else the page's setting.
func (f *Frame) ellipsisAt(i int32) ellipsisState {
var tip, side uint8
for steps := 0; i >= 0 && steps < len(f.Nodes) && (tip == 0 || side == 0); steps++ {
n := &f.Nodes[i]
set := f.Exts[n.Ext].ellipsisTip
if tip == 0 {
tip = set & ellipsisTipSwitch
}
if side == 0 {
side = set >> ellipsisTipSideShift
}
switch {
case n.Parent >= 0:
i = n.Parent
case n.AnchorRef > 0:
i = n.AnchorRef - 1
default:
i = f.popupParent(i)
}
}
state := ellipsisState{on: f.page != nil && f.page.config.EllipsisTooltips}
return state.under(tip | side<<ellipsisTipSideShift)
}

// popupParent is the element Popup i was declared in, or -1 for the root and
// for a popup declared outside every container.
func (f *Frame) popupParent(i int32) int32 {
at, found := slices.BinarySearchFunc(f.popupFrom, i, func(p popupFrom, i int32) int {
return int(p.popup - i)
})
if !found {
return -1
}
return f.popupFrom[at].from
}
24 changes: 22 additions & 2 deletions base/frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ type Extra struct {
Draws int32 // 1 + the index of the element's Draw and DrawOver callbacks in frame.draws, or 0 for none
tooltip string // what Tooltip says over the element; empty for none
overflowTip string // full text already shortened by the application
ellipsisTip uint8 // EllipsisTooltip's switch and EllipsisTooltipOn's side (see ellipsis_tooltip.go); zero follows what the element is inside
tooltipSide uint8 // 1 + the TooltipSide TooltipOn opens the element's tip on; 0 is below
addons int32 // 1 + the index of the field's icon and button in frame.addons, or 0
focusGroup FocusAxis // FocusGroup: the arrow keys that move the focus between the controls inside; zero is none
Expand Down Expand Up @@ -266,6 +267,7 @@ type Frame struct {
Images []gfx.Bitmap // what each Image element draws, indexed by its aux
Exts []Extra // exts[0] is never written: an element without extras points at it
Floats []int32 // the Popups, which belong to no container
popupFrom []popupFrom // where each Popup was declared, in declaration order
hidden []int32 // the elements Hide was called on, taken out of the page at End
States []stateLooks // what Hover, Pressed and Focus declared, by element
Draws []drawHooks // what Draw and DrawOver declared, by element
Expand All @@ -279,6 +281,8 @@ type Frame struct {
Stops []gfx.GradientStop // their stops, in the order declared
paintStops []gfx.GradientStop // the stops of the gradient being painted, faded
Opacity float32 // the product of the opacities of the elements being painted
ellipsis ellipsisState // what the text being painted does with the tooltips of shortened text
ellipsisSet bool // an element was given EllipsisTooltip or EllipsisTooltipOn: End works out each Popup's
popupLayer uint16 // the canvas layer the Popups are painted on
nativeMark int // how many native views the canvas had when the page began
Binds []bindDecl // the bound controls, see bind.go
Expand Down Expand Up @@ -362,6 +366,8 @@ func (View *View) Begin(Surface *gfx.Canvas, Palette Theme, bounds Rect) *Frame
clear(f.Binds) // a binding holds pointers into the model: not past the frame
f.Binds = f.Binds[:0]
f.Floats = f.Floats[:0]
f.popupFrom = f.popupFrom[:0]
f.ellipsisSet = false
f.hidden = f.hidden[:0]
f.States, f.Gradients, f.Stops = f.States[:0], f.Gradients[:0], f.Stops[:0]
clear(f.Draws) // a callback holds what it captured: not past the frame it was declared in
Expand Down Expand Up @@ -432,20 +438,32 @@ func (f *Frame) End() {
f.Scrolls.sweep()
f.reportSplitters()
if f.page != nil {
previous := f.Surface.SetHoverHintOwner(f.page.root)
defer f.Surface.SetHoverHintOwner(previous)
// Truncated text shows its complete value only where the page or an
// EllipsisTooltip turned the tooltips on, beside the side EllipsisTooltipOn
// gave; components read both from the canvas.
previous, side := f.Surface.SetHoverHintOwner(0), f.Surface.SetHoverHintSide(0)
defer f.restoreHoverHints(previous, side)
f.ellipsis = ellipsisState{}
f.setEllipsis(ellipsisState{on: f.page.config.EllipsisTooltips})
}
f.PaintChild(0)
f.raiseOverNativeViews()
if f.popupLayer != 0 {
f.Surface.PushLayer(f.popupLayer)
}
for _, index := range f.Floats {
if f.ellipsisSet {
f.setEllipsis(f.ellipsisAt(index))
}
f.PaintChild(index)
}
f.ellipsis = ellipsisState{}
if f.popupLayer != 0 {
f.Surface.PopLayer()
}
if f.page != nil {
f.page.hoverText = f.page.hoverTextOn(f.Surface)
}
if f.tooltips || f.page != nil {
f.paintTooltip()
} else if f.paintTooltipFn != nil {
Expand Down Expand Up @@ -485,6 +503,7 @@ func (f *Frame) trim() {
f.Draws = trimFlexCapacity(f.Draws)
f.Binds = trimFlexCapacity(f.Binds)
f.Floats = trimFlexCapacity(f.Floats)
f.popupFrom = trimFlexCapacity(f.popupFrom)
f.hidden = trimFlexCapacity(f.hidden)
f.Lines = trimFlexCapacity(f.Lines)
f.Ellipses = trimFlexCapacity(f.Ellipses)
Expand Down Expand Up @@ -563,6 +582,7 @@ func (f *Frame) Popup() Box {
b := f.Box(kindContainer)
f.Parent = saved
f.Floats = append(f.Floats, b.i)
f.popupFrom = append(f.popupFrom, popupFrom{popup: b.i, from: saved})
return b
}

Expand Down
23 changes: 12 additions & 11 deletions base/memo.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,16 @@ type memoCell[T comparable] struct {

// memoEntry is what a page keeps for one Memo between paints.
type memoEntry struct {
cell any // *memoCell[T] of the value the part was last declared with
themeEpoch uint32 // the page's theme when the part was last painted
Opacity float32 // the inherited opacity then
scale float32 // the canvas's pixel scale then
fonts uint32 // the canvas's font generation then
epoch uint32 // the paint the part was last declared or copied in
Rect Rect // where it was
bounds Rect // the page's bounds then
Span gfx.Span // what it painted
cell any // *memoCell[T] of the value the part was last declared with
themeEpoch uint32 // the page's theme when the part was last painted
Opacity float32 // the inherited opacity then
tips ellipsisState // what the tooltips of shortened text did then
scale float32 // the canvas's pixel scale then
fonts uint32 // the canvas's font generation then
epoch uint32 // the paint the part was last declared or copied in
Rect Rect // where it was
bounds Rect // the page's bounds then
Span gfx.Span // what it painted
painted bool
held bool // one of its controls was hovered, pressed or focused
stale bool // its copy failed: declare it in the next try
Expand Down Expand Up @@ -205,7 +206,7 @@ func (p *Page) memoPaint(f *Frame, i int32, before bool) bool {
canvas := f.Surface
rect := f.Nodes[i].Rect
if box.copy {
if entry.themeEpoch != p.themeEpoch || entry.Opacity != f.Opacity {
if entry.themeEpoch != p.themeEpoch || entry.Opacity != f.Opacity || entry.tips != f.ellipsis {
entry.stale = true
p.retry = true
return true
Expand All @@ -231,7 +232,7 @@ func (p *Page) memoPaint(f *Frame, i int32, before bool) bool {
entry.Span = canvas.SpanSince(box.before)
entry.listens = len(canvas.HitsSince(box.before)) > 0
entry.Rect, entry.bounds, entry.painted, entry.stale = rect, f.bounds, true, false
entry.themeEpoch, entry.Opacity = p.themeEpoch, f.Opacity
entry.themeEpoch, entry.Opacity, entry.tips = p.themeEpoch, f.Opacity, f.ellipsis
hovered, pressed, focused := canvas.Hovered(), canvas.Pressed(), canvas.Focused()
for _, hit := range canvas.HitsSince(box.before) {
if !hit.ID.IsZero() && (hit.ID == hovered || hit.ID == pressed || hit.ID == focused) {
Expand Down
19 changes: 16 additions & 3 deletions base/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,17 @@ type PageConfig struct {
// lists, trees and tables share one look without repeating Bars on each. Zero fields
// keep Viva's defaults; a ScrollbarSize on the page or on one bar still wins.
BarStyle ScrollBarStyle
// EllipsisTooltips shows the complete value of text shortened with an
// ellipsis in a tooltip while the pointer rests on it: a Text cut by Ellipsis
// or MaxLines, and the labels components shorten themselves, such as Table
// cells, Select values and menu items. It is off by default, which paints
// such text as before; Box.EllipsisTooltip turns it on or off for part of the
// page. The tooltip opens below the text, or beside the side
// Box.EllipsisTooltipOn gives. Over the text, this tooltip takes the place of
// the Tooltip of the element the text is in. Only the pointer shows it, not
// keyboard focus; it is not in the accessibility tree, where a control keeps
// its complete name and value whatever its drawing shortens.
EllipsisTooltips bool
// Invalidator wakes the window the page is drawn in when an Effect of the page
// finishes. Configure uses it instead of the app.Config's; a page the application
// draws into a window of its own (Draw, not Paint) needs it, since only Config
Expand Down Expand Up @@ -312,7 +323,7 @@ type Page struct {
pointer gfx.Point // where the pointer was at the last pointer event
pointerIn bool // a pointer event put it there, and no exit has taken it away
hoverArea gfx.ID // the scroll area or self-scrolling table the pointer is on
hoverText gfx.HoverHint // changes wake a page even when text has no pointer target
hoverText gfx.HoverHint // the hint under the pointer at the last paint or pointer event; changes wake a page even when text has no pointer target
pointerDown bool
selects []*SelectState // the Selects of the last paint, which Wheel scrolls
labels []string // scratch for Choice's labels
Expand Down Expand Up @@ -609,7 +620,10 @@ func (p *Page) onPointer(event gfx.PointerEvent) gfx.EventResult {
p.hoverArea = p.hoverPlaceAt(p.pointer.X, p.pointer.Y)
}
entered := wasArea != p.hoverArea
text := p.HoveredText()
var text gfx.HoverHint
if p.View.Frame != nil {
text = p.hoverTextOn(p.View.Frame.Surface)
}
textChanged := !text.SameText(p.hoverText)
p.hoverText = text
var result gfx.EventResult
Expand Down Expand Up @@ -877,7 +891,6 @@ func (p *Page) paintOnce(Surface *gfx.Canvas, bounds Rect, View func(C)) {
View(&p.ctx)
p.declareEditMenus(&p.ctx)
f.End()
p.hoverText = p.HoveredText()
if p.settleContainers() {
p.retry = true
}
Expand Down
29 changes: 27 additions & 2 deletions base/paint.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,26 @@ import (
controlstyle "github.com/vivaui/viva/style"
)

// PaintChild paints element i and what it holds.
func (f *Frame) PaintChild(i int32) {
if f.Exts[f.Nodes[i].Ext].ellipsisTip != 0 {
f.paintWithEllipsisTip(i)
return
}
f.paintElement(i)
}

// paintWithEllipsisTip paints element i under the EllipsisTooltip and
// EllipsisTooltipOn it sets, for its subtree only. The setting is restored
// here, not by a deferred call in paintElement, whose other defers would stop
// being open-coded.
func (f *Frame) paintWithEllipsisTip(i int32) {
was := f.setEllipsis(f.ellipsis.under(f.Exts[f.Nodes[i].Ext].ellipsisTip))
f.paintElement(i)
f.setEllipsis(was)
}

func (f *Frame) paintElement(i int32) {
n := &f.Nodes[i]
if !n.FocusScope.IsZero() {
// A menu's page controls paint in the menu's focus scope, so a press on
Expand Down Expand Up @@ -213,7 +232,13 @@ func (f *Frame) PaintChild(i int32) {
f.runDraw(n, hooks.under)
}
for c := n.First; c >= 0; c = f.Nodes[c].Next {
f.PaintChild(c)
// PaintChild written out: it does not inline, and this loop paints
// nearly every element.
if f.Exts[f.Nodes[c].Ext].ellipsisTip != 0 {
f.paintWithEllipsisTip(c)
} else {
f.paintElement(c)
}
}
if n.OutOfFlow&absChildren != 0 {
f.paintAbsolute(i)
Expand Down Expand Up @@ -383,7 +408,7 @@ func (f *Frame) paintText(n *Node) {
f.paintStrike(line, bounds, style, alignment)
}
}
if n.TextDecor&textTruncated != 0 && f.page != nil && f.Exts[n.Ext].overflowTip == "" {
if n.TextDecor&textTruncated != 0 && f.ellipsis.on && f.Exts[n.Ext].overflowTip == "" {
r.Height = min(r.Height, float32(n.LineCount)*lh)
f.Surface.HoverHint(f.page.root, r, n.Text)
}
Expand Down
2 changes: 1 addition & 1 deletion base/text_wrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (f *Frame) Wrap(Value string) Box { return f.Paragraph(Value, math.MaxInt32

// MaxLines makes a Text a paragraph of at most n lines, the last ending with an
// ellipsis when the text needs more (n of one is Ellipsis); n of zero or less
// takes as many lines as the width needs.
// takes as many lines as the width needs. EllipsisTooltip shows what it cut.
func (b Box) MaxLines(n int) Box {
node := b.N()
node.Flags |= flagWrap
Expand Down
Loading
Loading