-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
codemaestro64
committed
Feb 26, 2021
1 parent
4dd9d6b
commit 91bfcb5
Showing
6 changed files
with
331 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.