From 04b853bee0a39a2f5d77437f2f5f73bb28f1a1ac Mon Sep 17 00:00:00 2001 From: Justin Do Date: Tue, 5 Dec 2023 15:17:06 +0700 Subject: [PATCH] update logic on slider and segmented control --- ui/cryptomaterial/segmented_control.go | 8 ++++++++ ui/cryptomaterial/slide_action.go | 4 ++++ ui/cryptomaterial/slider.go | 3 +++ 3 files changed, 15 insertions(+) diff --git a/ui/cryptomaterial/segmented_control.go b/ui/cryptomaterial/segmented_control.go index a64277693..769aad40a 100644 --- a/ui/cryptomaterial/segmented_control.go +++ b/ui/cryptomaterial/segmented_control.go @@ -33,6 +33,8 @@ type SegmentedControl struct { slideAction *SlideAction slideActionTitle *SlideAction segmentType SegmentType + + IsAllowsCycle bool } func (t *Theme) SegmentedControl(segmentTitles []string, segmentType SegmentType) *SegmentedControl { @@ -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++ @@ -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-- diff --git a/ui/cryptomaterial/slide_action.go b/ui/cryptomaterial/slide_action.go index 16724aa69..5688b460a 100644 --- a/ui/cryptomaterial/slide_action.go +++ b/ui/cryptomaterial/slide_action.go @@ -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 @@ -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 { @@ -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 { @@ -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, diff --git a/ui/cryptomaterial/slider.go b/ui/cryptomaterial/slider.go index 3fe556864..d92ae4df2 100644 --- a/ui/cryptomaterial/slider.go +++ b/ui/cryptomaterial/slider.go @@ -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,