Skip to content

Commit

Permalink
add hoverable and tooltip widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
codemaestro64 committed Feb 26, 2021
1 parent 4dd9d6b commit 91bfcb5
Show file tree
Hide file tree
Showing 6 changed files with 331 additions and 100 deletions.
61 changes: 57 additions & 4 deletions ui/decredmaterial/hoverable.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,62 @@
package decredmaterial
package decredmaterial

import (
"image"

"gioui.org/f32"
"gioui.org/io/pointer"
"gioui.org/layout"
"gioui.org/op"
)

type Hoverable struct {
hovered bool
hovered bool
position *f32.Point
}

func (t *Theme) Hoverable() *Hoverable {

}
return &Hoverable{}
}

func (h *Hoverable) Hovered() bool {
return h.hovered
}

func (h *Hoverable) Position() *f32.Point {
return h.position
}

func (h *Hoverable) update(gtx C) {
for _, e := range gtx.Events(h) {
ev, ok := e.(pointer.Event)
if !ok {
continue
}

switch ev.Type {
case pointer.Enter:
h.hovered = true
h.position = &ev.Position
case pointer.Leave:
h.hovered = false
h.position = &f32.Point{}
}
}
}

func (h *Hoverable) Layout(gtx C) D {
h.update(gtx)

defer op.Save(gtx.Ops).Load()

pointer.PassOp{Pass: true}.Add(gtx.Ops)
pointer.Rect(image.Rectangle{Max: gtx.Constraints.Min}).Add(gtx.Ops)
pointer.InputOp{
Tag: h,
Types: pointer.Enter | pointer.Leave,
}.Add(gtx.Ops)

return layout.Dimensions{
Size: gtx.Constraints.Min,
}
}
2 changes: 1 addition & 1 deletion ui/decredmaterial/modal.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (t *Theme) Modal() *Modal {
func (m *Modal) Layout(gtx layout.Context, widgets []func(gtx C) D, margin int) layout.Dimensions {
dims := layout.Stack{}.Layout(gtx,
layout.Expanded(func(gtx C) D {
fillMax(gtx, m.overlayColor)
fillMax(gtx, m.overlayColor, CornerRadius{})
return m.button.Layout(gtx)
}),
layout.Stacked(func(gtx C) D {
Expand Down
12 changes: 8 additions & 4 deletions ui/decredmaterial/theme.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,14 +168,18 @@ func toPointF(p image.Point) f32.Point {
return f32.Point{X: float32(p.X), Y: float32(p.Y)}
}

func fillMax(gtx layout.Context, col color.NRGBA) D {
func fillMax(gtx layout.Context, col color.NRGBA, radius CornerRadius) D {
cs := gtx.Constraints
d := image.Point{X: cs.Max.X, Y: cs.Max.Y}
st := op.Save(gtx.Ops)
track := image.Rectangle{
Max: image.Point{X: d.X, Y: d.Y},
track := f32.Rectangle{
Max: f32.Point{X: float32(d.X), Y: float32(d.Y)},
}
clip.Rect(track).Add(gtx.Ops)

clip.RRect{
Rect: track,
NE: radius.NE, NW: radius.NW, SE: radius.SE, SW: radius.SW,
}.Add(gtx.Ops)
paint.Fill(gtx.Ops, col)
st.Load()

Expand Down
96 changes: 96 additions & 0 deletions ui/decredmaterial/tooltip.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package decredmaterial

import (
"image"
"image/color"

"gioui.org/layout"
"gioui.org/widget"
"gioui.org/f32"
"gioui.org/op"
"gioui.org/unit"

"github.com/planetdecred/godcr/ui/values"
)

type Tooltip struct {
textLabel Label
position Position
hoverable *Hoverable
card Card
hasCalculatedContentDims bool
contentDims image.Point
borderColor color.NRGBA
}

func (t *Theme) Tooltip(text string, position Position) *Tooltip {
return &Tooltip{
textLabel: t.Caption(text),
position: position,
hoverable: t.Hoverable(),
card: t.Card(),
borderColor: t.Color.Hint,
}
}

func (t *Tooltip) SetText(text string) {
t.textLabel.Text = text
}

func (t *Tooltip) update(gtx C) {
if t.hoverable.Hovered() {
t.layoutTooltip(gtx, t.hoverable.Position())
}
}

func (t *Tooltip) calculateContentDims(gtx C) {
dims := t.layoutText(gtx)
t.contentDims = dims.Size
}

func (t *Tooltip) layoutText(gtx C) D {
border := widget.Border{
Color: t.borderColor,
CornerRadius: values.MarginPadding5,
Width: values.MarginPadding1,
}

return layout.Stack{}.Layout(gtx,
layout.Stacked(func(gtx C) D {
return border.Layout(gtx, func(gtx C) D {
return t.card.Layout(gtx, func(gtx C) D {
return layout.UniformInset(values.MarginPadding10).Layout(gtx, t.textLabel.Layout)
})
})
}),
)

}

func (t *Tooltip) layoutTooltip(gtx C, hoverPosition *f32.Point) D {
inset := layout.Inset{}
switch t.position {
case Left:
inset.Left = unit.Dp(-float32(t.contentDims.X) - hoverPosition.X)
inset.Top = unit.Dp(hoverPosition.Y + 15)
}

m := op.Record(gtx.Ops)
dims := inset.Layout(gtx, t.layoutText)
op.Defer(gtx.Ops, m.Stop())
return dims
}

func (t *Tooltip) Layout(gtx C, wdgt layout.Widget) D {
if !t.hasCalculatedContentDims {
gtx.Constraints.Min = gtx.Constraints.Max
t.calculateContentDims(gtx)
t.hasCalculatedContentDims = true
}
t.update(gtx)

dims := wdgt(gtx)
gtx.Constraints.Min = dims.Size
t.hoverable.Layout(gtx)
return dims
}
Loading

0 comments on commit 91bfcb5

Please sign in to comment.