refactor(tracker/gioui): Popup in same style as other widgets

This commit is contained in:
5684185+vsariola@users.noreply.github.com
2025-06-24 20:25:52 +03:00
parent 18d198d764
commit b4ec136ab1
4 changed files with 63 additions and 57 deletions

View File

@ -13,69 +13,51 @@ import (
"gioui.org/unit"
)
type PopupStyle struct {
Visible *bool
SurfaceColor color.NRGBA
ShadowColor color.NRGBA
ShadowN unit.Dp
ShadowE unit.Dp
ShadowW unit.Dp
ShadowS unit.Dp
SE, SW, NW, NE unit.Dp
}
type (
PopupStyle struct {
Color color.NRGBA
CornerRadii struct {
SE, SW, NW, NE unit.Dp
}
Shadow struct {
Color color.NRGBA
N, E, W, S unit.Dp
}
}
func Popup(th *Theme, visible *bool) PopupStyle {
return PopupStyle{
Visible: visible,
SurfaceColor: th.Popup.Bg,
ShadowColor: th.Popup.Shadow,
ShadowN: unit.Dp(2),
ShadowE: unit.Dp(2),
ShadowS: unit.Dp(2),
ShadowW: unit.Dp(2),
SE: unit.Dp(6),
SW: unit.Dp(6),
NW: unit.Dp(6),
NE: unit.Dp(6),
PopupWidget struct {
Style *PopupStyle
Visible *bool
}
)
func Popup(th *Theme, visible *bool) PopupWidget {
return PopupWidget{
Style: &th.Popup.Dialog,
Visible: visible,
}
}
func (s PopupStyle) Layout(gtx C, contents layout.Widget) D {
func (s PopupWidget) Layout(gtx C, contents layout.Widget) D {
s.update(gtx)
if !*s.Visible {
return D{}
}
for {
event, ok := gtx.Event(pointer.Filter{
Target: s.Visible,
Kinds: pointer.Press,
})
if !ok {
break
}
e, ok := event.(pointer.Event)
if !ok {
continue
}
switch e.Kind {
case pointer.Press:
*s.Visible = false
}
}
bg := func(gtx C) D {
rrect := clip.RRect{
Rect: image.Rectangle{Max: gtx.Constraints.Min},
SE: gtx.Dp(s.SE),
SW: gtx.Dp(s.SW),
NW: gtx.Dp(s.NW),
NE: gtx.Dp(s.NE),
SE: gtx.Dp(s.Style.CornerRadii.SE),
SW: gtx.Dp(s.Style.CornerRadii.SW),
NW: gtx.Dp(s.Style.CornerRadii.NW),
NE: gtx.Dp(s.Style.CornerRadii.NE),
}
rrect2 := rrect
rrect2.Rect.Min = rrect2.Rect.Min.Sub(image.Pt(gtx.Dp(s.ShadowW), gtx.Dp(s.ShadowN)))
rrect2.Rect.Max = rrect2.Rect.Max.Add(image.Pt(gtx.Dp(s.ShadowE), gtx.Dp(s.ShadowS)))
paint.FillShape(gtx.Ops, s.ShadowColor, rrect2.Op(gtx.Ops))
paint.FillShape(gtx.Ops, s.SurfaceColor, rrect.Op(gtx.Ops))
rrect2.Rect.Min = rrect2.Rect.Min.Sub(image.Pt(gtx.Dp(s.Style.Shadow.W), gtx.Dp(s.Style.Shadow.N)))
rrect2.Rect.Max = rrect2.Rect.Max.Add(image.Pt(gtx.Dp(s.Style.Shadow.E), gtx.Dp(s.Style.Shadow.S)))
paint.FillShape(gtx.Ops, s.Style.Shadow.Color, rrect2.Op(gtx.Ops))
paint.FillShape(gtx.Ops, s.Style.Color, rrect.Op(gtx.Ops))
area := clip.Rect(image.Rect(-1e6, -1e6, 1e6, 1e6)).Push(gtx.Ops)
event.Op(gtx.Ops, s.Visible)
area.Pop()
@ -94,4 +76,24 @@ func (s PopupStyle) Layout(gtx C, contents layout.Widget) D {
return dims
}
func (s *PopupWidget) update(gtx C) {
for {
event, ok := gtx.Event(pointer.Filter{
Target: s.Visible,
Kinds: pointer.Press,
})
if !ok {
break
}
e, ok := event.(pointer.Event)
if !ok {
continue
}
switch e.Kind {
case pointer.Press:
*s.Visible = false
}
}
}
var dummyTag bool