Skip to content

Commit

Permalink
Merge pull request #245 from trimble-oss/fix/Slider_decouple
Browse files Browse the repository at this point in the history
Modify Slider labels
  • Loading branch information
rthanga1 authored Sep 20, 2023
2 parents b595976 + e150acc commit 46849e8
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 7 deletions.
2 changes: 2 additions & 0 deletions DemoApp/DemoApp/Views/SliderSamplePage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
ShowToolTip="{Binding ShowTooltip}"
IsEnabled="{Binding IsEnabled}"
StepValue="1"
TickValue="10"
RightIconSource="placeholder.png"
LeftIconSource="placeholder.png"
Padding="0,15" />
Expand All @@ -47,6 +48,7 @@
ShowToolTip="{Binding ShowTooltip}"
IsEnabled="{Binding IsEnabled}"
StepValue="1"
TickValue="1"
RightIconSource="placeholder.png"
LeftIconSource="placeholder.png"
Padding="0,15" />
Expand Down
49 changes: 42 additions & 7 deletions Trimble.Modus.Components/Controls/TMSlider/SliderCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,23 @@ public abstract class SliderCore : Grid
#endregion

#region Bindable Property
public static BindableProperty MinimumValueProperty = BindableProperty.Create(nameof(MinimumValue), typeof(double), typeof(SliderCore), .0, propertyChanged: OnMinimumMaximumValuePropertyChanged);
public static BindableProperty MinimumValueProperty = BindableProperty.Create(nameof(MinimumValue), typeof(double), typeof(SliderCore), .0, propertyChanged: OnMinimumMaximumValuePropertyChanged);
public static BindableProperty MaximumValueProperty = BindableProperty.Create(nameof(MaximumValue), typeof(double), typeof(SliderCore), 1.0, propertyChanged: OnMinimumMaximumValuePropertyChanged);
public static BindableProperty StepValueProperty = BindableProperty.Create(nameof(StepValue), typeof(double), typeof(SliderCore), 0.01, defaultBindingMode: BindingMode.TwoWay, propertyChanged: OnMinimumMaximumValuePropertyChanged);
public static BindableProperty TickValueProperty = BindableProperty.Create(nameof(TickValue), typeof(double), typeof(SliderCore), 10.0, defaultBindingMode: BindingMode.TwoWay, coerceValue: TickCoerceValue);
public static BindableProperty SizeProperty = BindableProperty.Create(nameof(Size), typeof(SliderSize), typeof(SliderCore), SliderSize.Medium, defaultBindingMode: BindingMode.TwoWay, propertyChanged: OnLayoutPropertyChanged);
public static BindableProperty TitleProperty = BindableProperty.Create(nameof(Title), typeof(string), typeof(SliderCore), null, propertyChanged: OnTitleTextPropertyChanged);
public static BindableProperty LeftTextProperty = BindableProperty.Create(nameof(LeftText), typeof(string), typeof(SliderCore), null);
public static BindableProperty RightTextProperty = BindableProperty.Create(nameof(RightText), typeof(string), typeof(SliderCore), null);
public static BindableProperty LeftIconProperty = BindableProperty.Create(nameof(LeftIconSource), typeof(ImageSource), typeof(SliderCore), null, propertyChanged: OnLeftIconSourceChanged);
public static BindableProperty RightIconProperty = BindableProperty.Create(nameof(RightIconSource), typeof(ImageSource), typeof(SliderCore), null, propertyChanged: OnRightIconSourceChanged);
public static BindableProperty ShowStepsProperty = BindableProperty.Create(nameof(ShowSteps), typeof(Boolean), typeof(SliderCore), false, defaultBindingMode: BindingMode.TwoWay, propertyChanged: OnShowStepsPropertyChanged);
public static BindableProperty ShowToolTipProperty= BindableProperty.Create(nameof(ShowToolTip), typeof(Boolean), typeof(SliderCore), false, defaultBindingMode: BindingMode.TwoWay, propertyChanged: OnShowToolTipPropertyChanged);
public static BindableProperty ShowToolTipProperty = BindableProperty.Create(nameof(ShowToolTip), typeof(Boolean), typeof(SliderCore), false, defaultBindingMode: BindingMode.TwoWay, propertyChanged: OnShowToolTipPropertyChanged);
#endregion

#region Property change methods
static void OnTitleTextPropertyChanged(BindableObject bindable, object oldValue, object newValue)
=> ((SliderCore)bindable).OnTitleTextPropertyChanged((string) newValue);
=> ((SliderCore)bindable).OnTitleTextPropertyChanged((string)newValue);
static void OnShowStepsPropertyChanged(BindableObject bindable, object oldValue, object newValue)
=> ((SliderCore)bindable).OnShowStepsPropertyChanged();
static void OnShowToolTipPropertyChanged(BindableObject bindable, object oldValue, object newValue)
Expand Down Expand Up @@ -111,6 +112,11 @@ public double StepValue
get => (double)GetValue(StepValueProperty);
set => SetValue(StepValueProperty, value);
}
public double TickValue
{
get => (double)GetValue(TickValueProperty);
set => SetValue(TickValueProperty, value);
}
#endregion

#region UI Elements
Expand Down Expand Up @@ -181,6 +187,7 @@ public double StepValue
};
internal AbsoluteLayout SliderContainer = new AbsoluteLayout();
internal StackLayout SliderHolderLayout = new StackLayout();
private bool alternateValue = true;
#endregion

#region Constructor
Expand Down Expand Up @@ -292,15 +299,20 @@ protected void BuildStepper()
StepContainer.Children.Clear();

StepContainer.WidthRequest = SliderContainer.Width - _thumbSize;
for (var i = MinimumValue; StepValue != 0 && i < MaximumValue; i += StepValue)
for (var i = MinimumValue; TickValue != 0 && i < MaximumValue; i += (MaximumValue - MinimumValue) / TickValue)
{
var stack = SliderHelper.CreateStepLabelContainer();
var box = SliderHelper.CreateStepLine(Size);
box.VerticalOptions = LayoutOptions.Start;
stack.Children.Add(box);

var label = SliderHelper.CreateStepLabel(Size);
label.Text = i.ToString();
stack.Children.Add(label);
if (!alternateValue)
{
var label = SliderHelper.CreateStepLabel(Size);
label.Text = Math.Round(i, 2).ToString();
stack.Children.Add(label);
}
alternateValue = !alternateValue;
StepContainer.Children.Add(stack);
}
}
Expand Down Expand Up @@ -384,5 +396,28 @@ protected void SetValueLabelBinding(Label label, BindableProperty bindableProper
/// <param name="newValue"></param>
protected abstract void OnTitleTextPropertyChanged(string newValue);
#endregion
#region Private Methods
private static object TickCoerceValue(BindableObject bindable, object value)
{
var slider = (bindable as TMSlider);
return CoerceValue((double)value);

}
private static object CoerceValue(double value)
{
if (value < 1 && value > 0)
{
return 1;
}
if (value > 50)
{
return 50;
}
else
{
return 0;
}
}
#endregion
}
}

0 comments on commit 46849e8

Please sign in to comment.