Skip to content

Commit 07ca2a2

Browse files
jsuarezruizhartez
andcommitted
Stepper Handlers (#517)
* StepperHandlers * Fixed Stepper handler broken tests * Remove duplicate searchbar * Add IStepper interface Co-authored-by: E.Z. Hart <hartez@gmail.com>
1 parent 1d8b317 commit 07ca2a2

22 files changed

+551
-21
lines changed

src/Compatibility/Core/src/Android/Renderers/StepperRenderer.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public StepperRenderer(Context context) : base(context)
1616
AutoPackage = false;
1717
}
1818

19+
[PortHandler]
1920
protected override LinearLayout CreateNativeControl()
2021
{
2122
return new LinearLayout(Context)
@@ -26,6 +27,7 @@ protected override LinearLayout CreateNativeControl()
2627
};
2728
}
2829

30+
[PortHandler]
2931
protected override void OnElementChanged(ElementChangedEventArgs<Stepper> e)
3032
{
3133
base.OnElementChanged(e);

src/Compatibility/Core/src/Android/StepperRendererManager.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
namespace Microsoft.Maui.Controls.Compatibility.Platform.Android
77
{
8+
[PortHandler]
89
public static class StepperRendererManager
910
{
1011
public static void CreateStepperButtons<TButton>(IStepperRenderer renderer, out TButton downButton, out TButton upButton)

src/Compatibility/Core/src/AppHostBuilderExtensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public static IAppHostBuilder RegisterCompatibilityRenderers(this IAppHostBuilde
1818
typeof(Page) ,
1919
typeof(Label) ,
2020
typeof(Slider),
21+
typeof(Stepper),
2122
typeof(Switch)
2223
};
2324

src/Compatibility/Core/src/iOS/Renderers/StepperRenderer.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,26 +65,31 @@ protected override void OnElementPropertyChanged(object sender, PropertyChangedE
6565
UpdateIncrement();
6666
}
6767

68+
[PortHandler]
6869
void OnValueChanged(object sender, EventArgs e)
6970
{
7071
((IElementController)Element).SetValueFromRenderer(Stepper.ValueProperty, Control.Value);
7172
}
7273

74+
[PortHandler]
7375
void UpdateIncrement()
7476
{
7577
Control.StepValue = Element.Increment;
7678
}
7779

80+
[PortHandler]
7881
void UpdateMaximum()
7982
{
8083
Control.MaximumValue = Element.Maximum;
8184
}
8285

86+
[PortHandler]
8387
void UpdateMinimum()
8488
{
8589
Control.MinimumValue = Element.Minimum;
8690
}
8791

92+
[PortHandler]
8893
void UpdateValue()
8994
{
9095
if (Control.Value != Element.Value)

src/Controls/samples/Controls.Sample/Pages/MainPage.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,16 +109,16 @@ void SetupMauiLayout()
109109
searchBar.Text = "A search query";
110110
verticalStack.Add(searchBar);
111111

112-
var searchBar = new SearchBar();
113-
searchBar.Text = "A search query";
114-
verticalStack.Add(searchBar);
115-
116112
var placeholderSearchBar = new SearchBar();
117113
placeholderSearchBar.Placeholder = "Placeholder";
118114
verticalStack.Add(placeholderSearchBar);
119115

120116
verticalStack.Add(new Slider());
121117

118+
verticalStack.Add(new Stepper());
119+
verticalStack.Add(new Stepper { BackgroundColor = Color.IndianRed });
120+
verticalStack.Add(new Stepper { Minimum = 0, Maximum = 10, Value = 5 });
121+
122122
verticalStack.Add(new Switch());
123123
verticalStack.Add(new Switch() { OnColor = Color.Green });
124124
verticalStack.Add(new Switch() { ThumbColor = Color.Yellow });

src/Controls/src/Core/Stepper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Microsoft.Maui.Controls
66
{
7-
public class Stepper : View, IElementConfiguration<Stepper>
7+
public class Stepper : View, IElementConfiguration<Stepper>, IStepper
88
{
99
public static readonly BindableProperty MaximumProperty = BindableProperty.Create(nameof(Maximum), typeof(double), typeof(Stepper), 100.0,
1010
validateValue: (bindable, value) => (double)value > ((Stepper)bindable).Minimum,

src/Core/src/Core/IRange.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
namespace Microsoft.Maui
2+
{
3+
/// <summary>
4+
/// Provides functionality to select a value from a range of values.
5+
/// </summary>
6+
public interface IRange : IView
7+
{
8+
/// <summary>
9+
/// Gets or sets the minimum selectable value.
10+
/// </summary>
11+
double Minimum { get; }
12+
13+
/// <summary>
14+
/// Gets or sets the maximum selectable value.
15+
/// </summary>
16+
double Maximum { get; }
17+
18+
/// <summary>
19+
/// Gets or sets the current value.
20+
/// </summary>
21+
double Value { get; set; }
22+
}
23+
}

src/Core/src/Core/ISlider.cs

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,8 @@ namespace Microsoft.Maui
33
/// <summary>
44
/// Represents a View that inputs a linear value.
55
/// </summary>
6-
public interface ISlider : IView
6+
public interface ISlider : IView, IRange
77
{
8-
/// <summary>
9-
/// Gets or sets the minimum selectable value for the Slider.
10-
/// </summary>
11-
double Minimum { get; }
12-
13-
/// <summary>
14-
/// Gets or sets the maximum selectable value for the Slider.
15-
/// </summary>
16-
double Maximum { get; }
17-
18-
/// <summary>
19-
/// Gets or sets the current value.
20-
/// </summary>
21-
double Value { get; set; }
22-
238
/// <summary>
249
/// Gets or sets the color of the portion of the slider track that contains the minimum value of the slider.
2510
/// </summary>

src/Core/src/Core/IStepper.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace Microsoft.Maui
2+
{
3+
/// <summary>
4+
/// Represents a View that consists of two buttons labeled with minus and plus signs.
5+
/// Use a Stepper for selecting a numeric value from a range of values.
6+
/// </summary>
7+
public interface IStepper : IView, IRange
8+
{
9+
/// <summary>
10+
/// Gets the increment by which Value is increased or decreased.
11+
/// </summary>
12+
double Increment { get; }
13+
}
14+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
using System;
2+
using Android.Widget;
3+
using Android.Views;
4+
using AButton = Android.Widget.Button;
5+
using AOrientation = Android.Widget.Orientation;
6+
7+
namespace Microsoft.Maui.Handlers
8+
{
9+
public partial class StepperHandler : AbstractViewHandler<IStepper, LinearLayout>, IStepperHandler
10+
{
11+
AButton? _downButton;
12+
AButton? _upButton;
13+
14+
IStepper? IStepperHandler.VirtualView => VirtualView;
15+
16+
AButton? IStepperHandler.UpButton => _upButton;
17+
18+
AButton? IStepperHandler.DownButton => _downButton;
19+
20+
protected override LinearLayout CreateNativeView()
21+
{
22+
var stepperLayout = new LinearLayout(Context)
23+
{
24+
Orientation = AOrientation.Horizontal,
25+
Focusable = true,
26+
DescendantFocusability = DescendantFocusability.AfterDescendants
27+
};
28+
29+
StepperHandlerManager.CreateStepperButtons(this, out _downButton, out _upButton);
30+
31+
if (_downButton != null)
32+
stepperLayout.AddView(_downButton, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.MatchParent));
33+
34+
if (_upButton != null)
35+
stepperLayout.AddView(_upButton, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.MatchParent));
36+
37+
return stepperLayout;
38+
}
39+
40+
public static void MapMinimum(StepperHandler handler, IStepper stepper)
41+
{
42+
handler.TypedNativeView?.UpdateMinimum(stepper);
43+
}
44+
45+
public static void MapMaximum(StepperHandler handler, IStepper stepper)
46+
{
47+
handler.TypedNativeView?.UpdateMaximum(stepper);
48+
}
49+
50+
public static void MapIncrement(StepperHandler handler, IStepper stepper)
51+
{
52+
handler.TypedNativeView?.UpdateIncrement(stepper);
53+
}
54+
55+
public static void MapValue(StepperHandler handler, IStepper stepper)
56+
{
57+
handler.TypedNativeView?.UpdateValue(stepper);
58+
}
59+
60+
AButton IStepperHandler.CreateButton()
61+
{
62+
if (Context == null)
63+
throw new ArgumentException("Context is null or empty", nameof(Context));
64+
65+
var button = new AButton(Context);
66+
button.SetHeight((int)Context.ToPixels(10.0));
67+
return button;
68+
}
69+
}
70+
}

0 commit comments

Comments
 (0)