mirror of
https://github.com/vsariola/sointu.git
synced 2025-07-21 06:24:32 -04:00
refactor(tracker/gioui): bind tracker.Int to NumericUpDown on Layout
This commit is contained in:
parent
289bfb0605
commit
beef8fe1e0
@ -23,8 +23,9 @@ import (
|
||||
"gioui.org/text"
|
||||
)
|
||||
|
||||
type NumberInput struct {
|
||||
Int tracker.Int
|
||||
type NumericUpDown struct {
|
||||
DpPerStep unit.Dp
|
||||
|
||||
dragStartValue int
|
||||
dragStartXY float32
|
||||
clickDecrease gesture.Click
|
||||
@ -41,36 +42,19 @@ type NumericUpDownStyle struct {
|
||||
Width unit.Dp
|
||||
Height unit.Dp
|
||||
TextSize unit.Sp
|
||||
DpPerStep unit.Dp
|
||||
Font font.Font
|
||||
}
|
||||
|
||||
type NumericUpDown struct {
|
||||
NumberInput *NumberInput
|
||||
Tooltip component.Tooltip
|
||||
Theme *Theme
|
||||
Font font.Font
|
||||
NumericUpDownStyle
|
||||
func NewNumericUpDown() *NumericUpDown {
|
||||
return &NumericUpDown{DpPerStep: unit.Dp(8)}
|
||||
}
|
||||
|
||||
func NewNumberInput(v tracker.Int) *NumberInput {
|
||||
return &NumberInput{Int: v}
|
||||
}
|
||||
|
||||
func NumUpDown(th *Theme, number *NumberInput, tooltip string) NumericUpDown {
|
||||
return NumericUpDown{
|
||||
NumberInput: number,
|
||||
Theme: th,
|
||||
Tooltip: Tooltip(th, tooltip),
|
||||
NumericUpDownStyle: th.NumericUpDown,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *NumericUpDown) Update(gtx layout.Context) {
|
||||
func (s *NumericUpDown) Update(gtx layout.Context, v tracker.Int) {
|
||||
// handle dragging
|
||||
pxPerStep := float32(gtx.Dp(s.DpPerStep))
|
||||
for {
|
||||
ev, ok := gtx.Event(pointer.Filter{
|
||||
Target: s.NumberInput,
|
||||
Target: s,
|
||||
Kinds: pointer.Press | pointer.Drag | pointer.Release,
|
||||
})
|
||||
if !ok {
|
||||
@ -79,46 +63,54 @@ func (s *NumericUpDown) Update(gtx layout.Context) {
|
||||
if e, ok := ev.(pointer.Event); ok {
|
||||
switch e.Kind {
|
||||
case pointer.Press:
|
||||
s.NumberInput.dragStartValue = s.NumberInput.Int.Value()
|
||||
s.NumberInput.dragStartXY = e.Position.X - e.Position.Y
|
||||
s.dragStartValue = v.Value()
|
||||
s.dragStartXY = e.Position.X - e.Position.Y
|
||||
case pointer.Drag:
|
||||
var deltaCoord float32
|
||||
deltaCoord = e.Position.X - e.Position.Y - s.NumberInput.dragStartXY
|
||||
s.NumberInput.Int.SetValue(s.NumberInput.dragStartValue + int(deltaCoord/pxPerStep+0.5))
|
||||
deltaCoord = e.Position.X - e.Position.Y - s.dragStartXY
|
||||
v.SetValue(s.dragStartValue + int(deltaCoord/pxPerStep+0.5))
|
||||
}
|
||||
}
|
||||
}
|
||||
// handle decrease clicks
|
||||
for ev, ok := s.NumberInput.clickDecrease.Update(gtx.Source); ok; ev, ok = s.NumberInput.clickDecrease.Update(gtx.Source) {
|
||||
for ev, ok := s.clickDecrease.Update(gtx.Source); ok; ev, ok = s.clickDecrease.Update(gtx.Source) {
|
||||
if ev.Kind == gesture.KindClick {
|
||||
s.NumberInput.Int.Add(-1)
|
||||
v.Add(-1)
|
||||
}
|
||||
}
|
||||
// handle increase clicks
|
||||
for ev, ok := s.NumberInput.clickIncrease.Update(gtx.Source); ok; ev, ok = s.NumberInput.clickIncrease.Update(gtx.Source) {
|
||||
for ev, ok := s.clickIncrease.Update(gtx.Source); ok; ev, ok = s.clickIncrease.Update(gtx.Source) {
|
||||
if ev.Kind == gesture.KindClick {
|
||||
s.NumberInput.Int.Add(1)
|
||||
v.Add(1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s NumericUpDown) Layout(gtx C) D {
|
||||
if s.Tooltip.Text.Text != "" {
|
||||
return s.NumberInput.tipArea.Layout(gtx, s.Tooltip, s.actualLayout)
|
||||
func (s *NumericUpDown) Widget(v tracker.Int, th *Theme, st *NumericUpDownStyle, tooltip string) func(gtx C) D {
|
||||
return func(gtx C) D {
|
||||
return s.Layout(gtx, v, th, st, tooltip)
|
||||
}
|
||||
return s.actualLayout(gtx)
|
||||
}
|
||||
|
||||
func (s *NumericUpDown) actualLayout(gtx C) D {
|
||||
s.Update(gtx)
|
||||
gtx.Constraints = layout.Exact(image.Pt(gtx.Dp(s.Width), gtx.Dp(s.Height)))
|
||||
width := gtx.Dp(s.ButtonWidth)
|
||||
height := gtx.Dp(s.Height)
|
||||
func (s *NumericUpDown) Layout(gtx C, v tracker.Int, th *Theme, st *NumericUpDownStyle, tooltip string) D {
|
||||
s.Update(gtx, v)
|
||||
if tooltip != "" {
|
||||
return s.tipArea.Layout(gtx, Tooltip(th, tooltip), func(gtx C) D {
|
||||
return s.actualLayout(gtx, v, th, st)
|
||||
})
|
||||
}
|
||||
return s.actualLayout(gtx, v, th, st)
|
||||
}
|
||||
|
||||
func (s *NumericUpDown) actualLayout(gtx C, v tracker.Int, th *Theme, st *NumericUpDownStyle) D {
|
||||
gtx.Constraints = layout.Exact(image.Pt(gtx.Dp(st.Width), gtx.Dp(st.Height)))
|
||||
width := gtx.Dp(st.ButtonWidth)
|
||||
height := gtx.Dp(st.Height)
|
||||
return layout.Background{}.Layout(gtx,
|
||||
func(gtx C) D {
|
||||
defer clip.UniformRRect(image.Rectangle{Max: gtx.Constraints.Min}, gtx.Dp(s.CornerRadius)).Push(gtx.Ops).Pop()
|
||||
paint.Fill(gtx.Ops, s.BgColor)
|
||||
event.Op(gtx.Ops, s.NumberInput) // register drag inputs, if not hitting the clicks
|
||||
defer clip.UniformRRect(image.Rectangle{Max: gtx.Constraints.Min}, gtx.Dp(st.CornerRadius)).Push(gtx.Ops).Pop()
|
||||
paint.Fill(gtx.Ops, st.BgColor)
|
||||
event.Op(gtx.Ops, s) // register drag inputs, if not hitting the clicks
|
||||
return D{Size: gtx.Constraints.Min}
|
||||
},
|
||||
func(gtx C) D {
|
||||
@ -128,25 +120,25 @@ func (s *NumericUpDown) actualLayout(gtx C) D {
|
||||
return layout.Background{}.Layout(gtx,
|
||||
func(gtx C) D {
|
||||
defer clip.Rect(image.Rectangle{Max: gtx.Constraints.Min}).Push(gtx.Ops).Pop()
|
||||
s.NumberInput.clickDecrease.Add(gtx.Ops)
|
||||
s.clickDecrease.Add(gtx.Ops)
|
||||
return D{Size: gtx.Constraints.Min}
|
||||
},
|
||||
func(gtx C) D { return s.Theme.Icon(icons.ContentRemove).Layout(gtx, s.IconColor) },
|
||||
func(gtx C) D { return th.Icon(icons.ContentRemove).Layout(gtx, st.IconColor) },
|
||||
)
|
||||
}),
|
||||
layout.Flexed(1, func(gtx C) D {
|
||||
paint.ColorOp{Color: s.TextColor}.Add(gtx.Ops)
|
||||
return widget.Label{Alignment: text.Middle}.Layout(gtx, s.Theme.Material.Shaper, s.Font, s.TextSize, strconv.Itoa(s.NumberInput.Int.Value()), op.CallOp{})
|
||||
paint.ColorOp{Color: st.TextColor}.Add(gtx.Ops)
|
||||
return widget.Label{Alignment: text.Middle}.Layout(gtx, th.Material.Shaper, st.Font, st.TextSize, strconv.Itoa(v.Value()), op.CallOp{})
|
||||
}),
|
||||
layout.Rigid(func(gtx C) D {
|
||||
gtx.Constraints = layout.Exact(image.Pt(width, height))
|
||||
return layout.Background{}.Layout(gtx,
|
||||
func(gtx C) D {
|
||||
defer clip.Rect(image.Rectangle{Max: gtx.Constraints.Min}).Push(gtx.Ops).Pop()
|
||||
s.NumberInput.clickIncrease.Add(gtx.Ops)
|
||||
s.clickIncrease.Add(gtx.Ops)
|
||||
return D{Size: gtx.Constraints.Min}
|
||||
},
|
||||
func(gtx C) D { return s.Theme.Icon(icons.ContentAdd).Layout(gtx, s.IconColor) },
|
||||
func(gtx C) D { return th.Icon(icons.ContentAdd).Layout(gtx, st.IconColor) },
|
||||
)
|
||||
}),
|
||||
)
|
||||
|
Reference in New Issue
Block a user