Skip to content

Commit

Permalink
update logic on slider and segmented control
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeBoy committed Dec 5, 2023
1 parent aaca032 commit 04b853b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
8 changes: 8 additions & 0 deletions ui/cryptomaterial/segmented_control.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ type SegmentedControl struct {
slideAction *SlideAction
slideActionTitle *SlideAction
segmentType SegmentType

IsAllowsCycle bool
}

func (t *Theme) SegmentedControl(segmentTitles []string, segmentType SegmentType) *SegmentedControl {
Expand Down Expand Up @@ -243,6 +245,9 @@ func (sc *SegmentedControl) handleActionEvent(isNext bool) {
l := len(sc.segmentTitles) - 1 // index starts at 0
if isNext {
if sc.selectedIndex == l {
if !sc.IsAllowsCycle {
return
}
sc.selectedIndex = 0
} else {
sc.selectedIndex++
Expand All @@ -251,6 +256,9 @@ func (sc *SegmentedControl) handleActionEvent(isNext bool) {
sc.slideActionTitle.PushLeft()
} else {
if sc.selectedIndex == 0 {
if !sc.IsAllowsCycle {
return
}
sc.selectedIndex = l
} else {
sc.selectedIndex--
Expand Down
4 changes: 4 additions & 0 deletions ui/cryptomaterial/slide_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ func (s *SlideAction) DragLayout(gtx C, w layout.Widget) D {
return dims
}

// TransformLayout perform transition effects between 2 widgets
func (s *SlideAction) TransformLayout(gtx C, w layout.Widget) D {
if s.push != 0 {
s.next = nil
Expand All @@ -125,6 +126,7 @@ func (s *SlideAction) TransformLayout(gtx C, w layout.Widget) D {
s.t0 = now
}

// Calculate the duration of transition effects
if s.offset != 0 {
duration := s.Duration
if duration == 0 {
Expand All @@ -146,6 +148,7 @@ func (s *SlideAction) TransformLayout(gtx C, w layout.Widget) D {
op.InvalidateOp{}.Add(gtx.Ops)
}

// Record the widget presentation
var dims layout.Dimensions
{
if s.next == nil {
Expand All @@ -171,6 +174,7 @@ func (s *SlideAction) TransformLayout(gtx C, w layout.Widget) D {
reverse = -1
}

// Implement transition effects for widgets
if s.offset > 0 {
defer op.Offset(image.Point{
X: int(float32(dims.Size.X)*(offset-1)) * reverse,
Expand Down
3 changes: 3 additions & 0 deletions ui/cryptomaterial/slider.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ func (s *Slider) Layout(gtx C, items []layout.Widget) D {
return s.slideAction.TransformLayout(gtx, s.slideItems[s.selected].widgetItem)
}),
layout.Stacked(func(gtx C) D {
if len(s.slideItems) == 1 {
return D{}
}
return layout.Inset{
Right: values.MarginPadding15,
Left: values.MarginPadding15,
Expand Down

0 comments on commit 04b853b

Please sign in to comment.