Skip to content

Commit 52722ce

Browse files
authored
Merge pull request #258 from enisn/xf/advanced-slider-bindableproperties
AdvancedSlider Bindable Properties
2 parents d04213d + 308b5af commit 52722ce

File tree

3 files changed

+161
-96
lines changed

3 files changed

+161
-96
lines changed

src/InputKit.Maui/Shared/Controls/AdvancedSlider.cs

Lines changed: 73 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
using InputKit.Shared.Abstraction;
22
using InputKit.Shared.Configuration;
3-
using Microsoft.Maui;
4-
using Microsoft.Maui.Controls;
5-
using Microsoft.Maui.Graphics;
6-
using System;
7-
using System.Diagnostics;
83

94
namespace InputKit.Shared.Controls;
105

11-
public partial class AdvancedSlider : StackLayout, IValidatable
6+
public class AdvancedSlider : StackLayout, IValidatable
127
{
138
public static GlobalSetting GlobalSetting { get; private set; } = new GlobalSetting
149
{
@@ -22,15 +17,7 @@ public partial class AdvancedSlider : StackLayout, IValidatable
2217
Label lblValue = new Label { FontSize = GlobalSetting.FontSize, FontFamily = GlobalSetting.FontFamily, InputTransparent = true, TextColor = GlobalSetting.TextColor, };
2318
Label lblMinValue = new Label { FontSize = GlobalSetting.FontSize, FontFamily = GlobalSetting.FontFamily, TextColor = GlobalSetting.TextColor, };
2419
Label lblMaxValue = new Label { FontSize = GlobalSetting.FontSize, FontFamily = GlobalSetting.FontFamily, TextColor = GlobalSetting.TextColor, };
25-
26-
private string _valueSuffix;
27-
private string _valuePrefix;
2820
private Color _textColor;
29-
private double _stepValue = 1;
30-
private string _minValuePrefix;
31-
private string _maxValuePrefix;
32-
private string _minValueSuffix;
33-
private string _maxValueSuffix;
3421

3522
public AdvancedSlider()
3623
{
@@ -56,6 +43,11 @@ public AdvancedSlider()
5643
});
5744

5845
slider.ValueChanged += Slider_ValueChanged;
46+
47+
lblValue.SizeChanged += (s, e) =>
48+
{
49+
UpdateView();
50+
};
5951
}
6052

6153
private void Slider_ValueChanged(object sender, ValueChangedEventArgs e)
@@ -72,14 +64,6 @@ private void Slider_ValueChanged(object sender, ValueChangedEventArgs e)
7264
UpdateView();
7365
}
7466

75-
protected override void OnSizeAllocated(double width, double height)
76-
{
77-
base.OnSizeAllocated(width, height);
78-
UpdateValueText();
79-
UpdateView();
80-
UpdateMinMaxValueText();
81-
}
82-
8367
/// <summary>
8468
/// Value of slider which user selected
8569
/// </summary>
@@ -88,62 +72,58 @@ protected override void OnSizeAllocated(double width, double height)
8872
/// <summary>
8973
/// Title of slider, It'll be shown tp of slider
9074
/// </summary>
91-
public string Title { get => lblTitle.Text; set { lblTitle.Text = value; lblTitle.IsVisible = !string.IsNullOrEmpty(value); } }
75+
public string Title { get => (string)GetValue(TitleProperty); set => SetValue(TitleProperty, value); }
9276

9377
/// <summary>
9478
/// It will be displayed start of value
9579
/// </summary>
96-
public string ValueSuffix { get => _valueSuffix; set { _valueSuffix = value; UpdateValueText(); } }
80+
public string ValueSuffix { get => (string)GetValue(ValueSuffixProperty); set => SetValue(ValueSuffixProperty, value); }
9781

9882
/// <summary>
9983
/// It'll be displayed end of value
10084
/// </summary>
101-
public string ValuePrefix { get => _valuePrefix; set { _valuePrefix = value; UpdateValueText(); } }
85+
public string ValuePrefix { get => (string)GetValue(ValuePrefixProperty); set => SetValue(ValuePrefixProperty, value); }
10286

10387
/// <summary>
10488
/// This will be displayed start of MinValue Text if <see cref="DisplayMinMaxValue"/> is true/>
10589
/// </summary>
106-
public string MinValuePrefix { get => _minValuePrefix; set { _minValuePrefix = value; UpdateMinMaxValueText(); } }
90+
public string MinValuePrefix { get => (string)GetValue(MinValuePrefixProperty); set => SetValue(MinValuePrefixProperty, value); }
10791

10892
/// <summary>
10993
/// This will be displayed start of MaxValue Text if <see cref="DisplayMinMaxValue"/> is true/>
11094
/// </summary>
111-
public string MaxValuePrefix { get => _maxValuePrefix; set { _maxValuePrefix = value; UpdateMinMaxValueText(); } }
95+
public string MaxValuePrefix { get => (string)GetValue(MaxValuePrefixProperty); set => SetValue(MaxValuePrefixProperty, value); }
11296

11397
/// <summary>
11498
/// This will be displayed end of MinValue Text if <see cref="DisplayMinMaxValue"/> is true/>
11599
/// </summary>
116-
public string MinValueSuffix { get => _minValueSuffix; set { _minValueSuffix = value; UpdateMinMaxValueText(); } }
100+
public string MinValueSuffix { get => (string)GetValue(MinValueSuffixProperty); set => SetValue(MinValueSuffixProperty, value); }
117101

118102
/// <summary>
119103
/// This will be displayed end of MaxValue Text if <see cref="DisplayMinMaxValue"/> is"true/>
120104
/// </summary>
121-
public string MaxValueSuffix { get => _maxValueSuffix; set { _maxValueSuffix = value; UpdateMinMaxValueText(); } }
105+
public string MaxValueSuffix { get => (string)GetValue(MaxValueSuffixProperty); set => SetValue(MaxValueSuffixProperty, value); }
122106

123107
/// <summary>
124108
/// Minimum value, user can slide
125109
/// </summary>
126-
public double MinValue { get => slider.Minimum; set { slider.Minimum = value; UpdateMinMaxValueText(); } }
110+
public double MinValue { get => (double)GetValue(MinValueProperty); set => SetValue(MinValueProperty, value); }
127111

128112
/// <summary>
129113
/// Maximum value, user can slide
130114
/// </summary>
131-
public double MaxValue { get => slider.Maximum; set { slider.Maximum = value; UpdateMinMaxValueText(); } }
115+
public double MaxValue { get => (double)GetValue(MaxValueProperty); set => SetValue(MaxValueProperty, value); }
132116

133117
/// <summary>
134118
/// Slider Increase number
135119
/// </summary>
136-
public double StepValue { get => _stepValue; set { _stepValue = value; UpdateValueText(); UpdateView(); } }
120+
public double StepValue { get => (double)GetValue(StepValueProperty); set => SetValue(StepValueProperty, value); }
137121

138122
/// <summary>
139123
/// Visibility of Min value and Max value at right and left
140124
/// </summary>
141-
public bool DisplayMinMaxValue
142-
{
143-
get => lblMinValue.IsVisible && lblMaxValue.IsVisible;
125+
public bool DisplayMinMaxValue { get => (bool)GetValue(DisplayMinMaxValueProperty); set => SetValue(DisplayMinMaxValueProperty, value); }
144126

145-
set { lblMaxValue.IsVisible = value; lblMinValue.IsVisible = value; }
146-
}
147127

148128
/// <summary>
149129
/// Text color of labels
@@ -163,7 +143,7 @@ public Color TextColor
163143
/// <summary>
164144
/// This is not available for this control
165145
/// </summary>
166-
public bool IsRequired { get; set; }
146+
public bool IsRequired { get => (bool)GetValue(IsRequiredProperty); set => SetValue(IsRequiredProperty, value); }
167147

168148
/// <summary>
169149
/// this always true, because this control value can not be null
@@ -178,6 +158,19 @@ public Color TextColor
178158
#region BindableProperties
179159
public static readonly BindableProperty ValueProperty = BindableProperty.Create(nameof(Value), typeof(double), typeof(AdvancedSlider), 0.0, BindingMode.TwoWay, propertyChanged: (bo, ov, nv) => (bo as AdvancedSlider).slider.Value = (double)nv);
180160
public static readonly BindableProperty TextColorProperty = BindableProperty.Create(nameof(TextColor), typeof(Color), typeof(AdvancedSlider), Colors.Gray, propertyChanged: (bo, ov, nv) => (bo as AdvancedSlider).TextColor = (Color)nv);
161+
public static readonly BindableProperty StepValueProperty = BindableProperty.Create(nameof(StepValue), typeof(double), typeof(AdvancedSlider), 1d, propertyChanged: (bo, ov, nv) => (bo as AdvancedSlider).OnStepValueChanged((double)ov, (double)nv));
162+
public static readonly BindableProperty IsRequiredProperty = BindableProperty.Create(nameof(IsRequired), typeof(bool), typeof(AdvancedSlider), false);
163+
public static readonly BindableProperty DisplayMinMaxValueProperty = BindableProperty.Create(nameof(StepValue), typeof(bool), typeof(AdvancedSlider), false, propertyChanged: (bo, ov, nv) => (bo as AdvancedSlider).OnDisplayMinMaxValueChanged((bool)nv));
164+
public static readonly BindableProperty MaxValueProperty = BindableProperty.Create(nameof(MaxValue), typeof(double), typeof(AdvancedSlider), 1d, propertyChanged: (bo, ov, nv) => (bo as AdvancedSlider).OnMaxValueChanged((double)nv));
165+
public static readonly BindableProperty MinValueProperty = BindableProperty.Create(nameof(MinValue), typeof(double), typeof(AdvancedSlider), 0d, propertyChanged: (bo, ov, nv) => (bo as AdvancedSlider).OnMinValueChanged((double)nv));
166+
public static readonly BindableProperty MaxValueSuffixProperty = BindableProperty.Create(nameof(MaxValueSuffix), typeof(string), typeof(AdvancedSlider), string.Empty, propertyChanged: (bo, ov, nv) => (bo as AdvancedSlider).UpdateMinMaxValueText());
167+
public static readonly BindableProperty MinValueSuffixProperty = BindableProperty.Create(nameof(MinValueSuffix), typeof(string), typeof(AdvancedSlider), string.Empty, propertyChanged: (bo, ov, nv) => (bo as AdvancedSlider).UpdateMinMaxValueText());
168+
public static readonly BindableProperty MaxValuePrefixProperty = BindableProperty.Create(nameof(MaxValuePrefix), typeof(string), typeof(AdvancedSlider), string.Empty, propertyChanged: (bo, ov, nv) => (bo as AdvancedSlider).UpdateMinMaxValueText());
169+
public static readonly BindableProperty MinValuePrefixProperty = BindableProperty.Create(nameof(MinValuePrefix), typeof(string), typeof(AdvancedSlider), string.Empty, propertyChanged: (bo, ov, nv) => (bo as AdvancedSlider).UpdateMinMaxValueText());
170+
public static readonly BindableProperty ValuePrefixProperty = BindableProperty.Create(nameof(ValuePrefix), typeof(string), typeof(AdvancedSlider), string.Empty, propertyChanged: (bo, ov, nv) => (bo as AdvancedSlider).UpdateValueText());
171+
public static readonly BindableProperty ValueSuffixProperty = BindableProperty.Create(nameof(ValueSuffix), typeof(string), typeof(AdvancedSlider), string.Empty, propertyChanged: (bo, ov, nv) => (bo as AdvancedSlider).UpdateValueText());
172+
public static readonly BindableProperty TitleProperty = BindableProperty.Create(nameof(Title), typeof(string), typeof(AdvancedSlider), string.Empty, propertyChanged: (bo, ov, nv) => (bo as AdvancedSlider).OnTitleChanged((string)nv));
173+
181174
#endregion
182175

183176
/// <summary>
@@ -189,21 +182,23 @@ public Color TextColor
189182
/// It's not available for this control
190183
/// </summary>
191184
public void DisplayValidation() { }
192-
void UpdateMinMaxValueText()
185+
186+
protected void UpdateMinMaxValueText()
193187
{
194188
lblMinValue.Text = $"{MinValuePrefix}{MinValue}{MinValueSuffix}";
195189
lblMaxValue.Text = $"{MaxValuePrefix}{MaxValue}{MaxValueSuffix}";
196190
}
197-
void UpdateValueText()
191+
192+
protected void UpdateValueText()
198193
{
199194
lblValue.Text = $"{ValuePrefix} {Value} {ValueSuffix}";
200195
}
201-
void UpdateView()
196+
197+
protected void UpdateView()
202198
{
203199
var totalLength = MaxValue - MinValue;
204200
var normalizedValue = Value - MinValue;
205201

206-
207202
// TODO: Keep animation disabled until resolution of https://github.com/dotnet/maui/issues/3353
208203

209204
lblValue.TranslationX = normalizedValue * ((slider.Width - 30) / totalLength);
@@ -215,4 +210,40 @@ void UpdateView()
215210
// 40 //Latency
216211
// );
217212
}
213+
214+
protected virtual void OnStepValueChanged(double oldValue, double newValue)
215+
{
216+
UpdateValueText();
217+
UpdateView();
218+
}
219+
220+
protected virtual void OnDisplayMinMaxValueChanged(bool newValue)
221+
{
222+
lblMaxValue.IsVisible = newValue;
223+
lblMinValue.IsVisible = newValue;
224+
}
225+
226+
protected virtual void OnMaxValueChanged(double newValue)
227+
{
228+
slider.Maximum = newValue;
229+
UpdateMinMaxValueText();
230+
}
231+
protected virtual void OnMinValueChanged(double newValue)
232+
{
233+
slider.Minimum = newValue;
234+
UpdateMinMaxValueText();
235+
}
236+
237+
protected virtual void OnTitleChanged(string newValue)
238+
{
239+
lblTitle.Text = newValue;
240+
lblTitle.IsVisible = !string.IsNullOrEmpty(newValue);
241+
}
242+
243+
protected override void OnSizeAllocated(double width, double height)
244+
{
245+
base.OnSizeAllocated(width, height);
246+
UpdateValueText();
247+
UpdateMinMaxValueText();
248+
}
218249
}

0 commit comments

Comments
 (0)