Skip to content

Commit

Permalink
update slider and clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeBoy committed Dec 2, 2023
1 parent cd5ff2e commit 4eb951b
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 37 deletions.
34 changes: 17 additions & 17 deletions ui/cryptomaterial/segmented_control.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ type SegmentedControl struct {
mu sync.Mutex

isSwipeActionEnabled bool
sliceAction *SliceAction
sliceActionTitle *SliceAction
slideAction *SlideAction
slideActionTitle *SlideAction
segmentType SegmentType
}

Expand All @@ -47,18 +47,18 @@ func (t *Theme) SegmentedControl(segmentTitles []string, segmentType SegmentType
rightNavBtn: t.NewClickable(false),
isSwipeActionEnabled: true,
segmentType: segmentType,
sliceAction: NewSliceAction(),
sliceActionTitle: NewSliceAction(),
slideAction: NewSliceAction(),
slideActionTitle: NewSliceAction(),
}

sc.sliceAction.Draged(func(dragDirection SwipeDirection) {
sc.slideAction.Draged(func(dragDirection SwipeDirection) {
isNext := dragDirection == SwipeLeft
sc.handleActionEvent(isNext)
})

sc.sliceActionTitle.SetDragEffect(50)
sc.slideActionTitle.SetDragEffect(50)

sc.sliceActionTitle.Draged(func(dragDirection SwipeDirection) {
sc.slideActionTitle.Draged(func(dragDirection SwipeDirection) {
isNext := dragDirection == SwipeLeft
sc.handleActionEvent(isNext)
})
Expand All @@ -84,12 +84,12 @@ func (sc *SegmentedControl) Layout(gtx C, body func(gtx C) D) D {
}),
layout.Rigid(func(gtx C) D {
return layout.Inset{Top: values.MarginPadding16}.Layout(gtx, func(gtx C) D {
if sc.isSwipeActionEnabled {
return sc.sliceAction.DragLayout(gtx, func(gtx C) D {
return sc.sliceAction.TransformLayout(gtx, body)
})
if !sc.isSwipeActionEnabled {
return body(gtx)
}
return body(gtx)
return sc.slideAction.DragLayout(gtx, func(gtx C) D {
return sc.slideAction.TransformLayout(gtx, body)
})
})
}),
)
Expand All @@ -106,7 +106,7 @@ func (sc *SegmentedControl) GroupTileLayout(gtx C) D {
Border: Border{Radius: Radius(8)},
}.Layout(gtx,
layout.Rigid(func(gtx C) D {
return sc.sliceActionTitle.DragLayout(gtx, func(gtx C) D {
return sc.slideActionTitle.DragLayout(gtx, func(gtx C) D {
return sc.list.Layout(gtx, len(sc.segmentTitles), func(gtx C, i int) D {
isSelectedSegment := sc.SelectedIndex() == i
return layout.Center.Layout(gtx, func(gtx C) D {
Expand Down Expand Up @@ -247,16 +247,16 @@ func (sc *SegmentedControl) handleActionEvent(isNext bool) {
} else {
sc.selectedIndex++
}
sc.sliceAction.PushLeft()
sc.sliceActionTitle.PushLeft()
sc.slideAction.PushLeft()
sc.slideActionTitle.PushLeft()
} else {
if sc.selectedIndex == 0 {
sc.selectedIndex = l
} else {
sc.selectedIndex--
}
sc.sliceAction.PushRight()
sc.sliceActionTitle.PushRight()
sc.slideAction.PushRight()
sc.slideActionTitle.PushRight()
}
sc.changed = true
}
18 changes: 9 additions & 9 deletions ui/cryptomaterial/slide_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const (

type Dragged func(dragDirection SwipeDirection)

type SliceAction struct {
type SlideAction struct {
Duration time.Duration
IsReverse bool
push int
Expand All @@ -46,26 +46,26 @@ type SliceAction struct {
isPushing bool
}

func NewSliceAction() *SliceAction {
return &SliceAction{
func NewSliceAction() *SlideAction {
return &SlideAction{
Duration: defaultDuration,
dragEffect: defaultdragEffect,
}
}

// PushLeft pushes the existing widget to the left.
func (s *SliceAction) PushLeft() { s.push = 1 }
func (s *SlideAction) PushLeft() { s.push = 1 }

// PushRight pushes the existing widget to the right.
func (s *SliceAction) PushRight() { s.push = -1 }
func (s *SlideAction) PushRight() { s.push = -1 }

func (s *SliceAction) SetDragEffect(offset int) { s.dragEffect = offset }
func (s *SlideAction) SetDragEffect(offset int) { s.dragEffect = offset }

func (s *SliceAction) Draged(drag Dragged) {
func (s *SlideAction) Draged(drag Dragged) {
s.draged = drag
}

func (s *SliceAction) DragLayout(gtx C, w layout.Widget) D {
func (s *SlideAction) DragLayout(gtx C, w layout.Widget) D {
if gtx.Queue != nil {
for _, event := range s.drag.Events(gtx.Metric, gtx.Queue, gesture.Horizontal) {
switch event.Type {
Expand Down Expand Up @@ -109,7 +109,7 @@ func (s *SliceAction) DragLayout(gtx C, w layout.Widget) D {
return dims
}

func (s *SliceAction) TransformLayout(gtx C, w layout.Widget) D {
func (s *SlideAction) TransformLayout(gtx C, w layout.Widget) D {
if s.push != 0 {
s.next = nil
s.lastCall = s.nextCall
Expand Down
25 changes: 14 additions & 11 deletions ui/cryptomaterial/slider.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type Slider struct {
ButtonBackgroundColor color.NRGBA
IndicatorBackgroundColor color.NRGBA
SelectedIndicatorColor color.NRGBA // this is a full color no opacity
sliceAction *SliceAction
slideAction *SlideAction
}

var m4 = values.MarginPadding4
Expand All @@ -44,13 +44,13 @@ func (t *Theme) Slider() *Slider {
ButtonBackgroundColor: values.TransparentColor(values.TransparentWhite, 0.2),
IndicatorBackgroundColor: values.TransparentColor(values.TransparentWhite, 0.2),
SelectedIndicatorColor: t.Color.White,
sliceAction: NewSliceAction(),
slideAction: NewSliceAction(),
}

sl.card = sl.t.Card()
sl.card.Radius = Radius(8)

sl.sliceAction.Draged(func(dragDirection SwipeDirection) {
sl.slideAction.Draged(func(dragDirection SwipeDirection) {
isNext := dragDirection == SwipeLeft
sl.handleActionEvent(isNext)
})
Expand All @@ -63,7 +63,7 @@ func (s *Slider) GetSelectedIndex() int {
return s.selected
}

func (s *Slider) getSliceItems(items []layout.Widget) []*sliderItem {
func (s *Slider) sliderItems(items []layout.Widget) []*sliderItem {
slideItems := make([]*sliderItem, 0)
for _, item := range items {
slideItems = append(slideItems, &sliderItem{
Expand All @@ -78,7 +78,7 @@ func (s *Slider) getSliceItems(items []layout.Widget) []*sliderItem {
func (s *Slider) Layout(gtx C, items []layout.Widget) D {
// set slider items once since layout is drawn multiple times per sec.
if !s.isSliderItemsSet {
s.slideItems = s.getSliceItems(items)
s.slideItems = s.sliderItems(items)
s.isSliderItemsSet = true
}

Expand All @@ -87,10 +87,10 @@ func (s *Slider) Layout(gtx C, items []layout.Widget) D {
}

s.handleClickEvent()
return s.sliceAction.DragLayout(gtx, func(gtx C) D {
return s.slideAction.DragLayout(gtx, func(gtx C) D {
return layout.Stack{Alignment: layout.S}.Layout(gtx,
layout.Expanded(func(gtx C) D {
return s.sliceAction.TransformLayout(gtx, s.slideItems[s.selected].widgetItem)
return s.slideAction.TransformLayout(gtx, s.slideItems[s.selected].widgetItem)
}),
layout.Stacked(func(gtx C) D {
return layout.Inset{
Expand Down Expand Up @@ -192,30 +192,33 @@ func (s *Slider) handleClickEvent() {
lastSelected := s.selected
s.selected = i
if lastSelected < i {
s.sliceAction.PushLeft()
s.slideAction.PushLeft()
} else {
s.sliceAction.PushRight()
s.slideAction.PushRight()
}
break
}
}
}

func (s *Slider) handleActionEvent(isNext bool) {
if len(s.slideItems) == 1 {
return
}
l := len(s.slideItems) - 1 // index starts at 0
if isNext {
if s.selected == l {
s.selected = 0
} else {
s.selected++
}
s.sliceAction.PushLeft()
s.slideAction.PushLeft()
} else {
if s.selected == 0 {
s.selected = l
} else {
s.selected--
}
s.sliceAction.PushRight()
s.slideAction.PushRight()
}
}

0 comments on commit 4eb951b

Please sign in to comment.