Skip to content

Commit a24cfb4

Browse files
michael-hawkerSergio0694
authored andcommitted
Fix Surface Dial Helpers to function as well as before
Add RotationResolutionInDegrees customization Still some issues with Menu deregistration tracked in #3774
1 parent fc89232 commit a24cfb4

File tree

3 files changed

+39
-24
lines changed

3 files changed

+39
-24
lines changed

Microsoft.Toolkit.Uwp.SampleApp/SamplePages/SurfaceDialTextbox/SurfaceDialTextboxCode.bind

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
<TextBox HorizontalAlignment="Left"
1313
VerticalAlignment="Top"
1414
Margin="0,0,0,10"
15-
Text="0"
15+
Text="50"
1616
Width="250">
1717
<ui:TextBoxExtensions.SurfaceDialOptions>
1818
<ui:SurfaceDialOptions
1919
StepValue="1"
20-
ForceMenuItem="True"
20+
RotationResolutionInDegrees="12"
2121
EnableHapticFeedback="True"
2222
EnableMinMaxValue="True"
2323
MinValue="0"

Microsoft.Toolkit.Uwp.UI/Extensions/TextBox/SurfaceDialOptions.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Windows.UI.Input;
66
using Windows.UI.Xaml;
77
using Windows.UI.Xaml.Controls;
8+
using Windows.UI.Xaml.Input;
89

910
namespace Microsoft.Toolkit.Uwp.UI
1011
{
@@ -18,12 +19,6 @@ public sealed class SurfaceDialOptions : DependencyObject
1819
/// </summary>
1920
internal static SurfaceDialOptions Default { get; } = new();
2021

21-
/// <summary>
22-
/// Gets or sets a value indicating whether new menu items shouldn't be added automatically.
23-
/// This should be set to <see langword="true"/> if you provide the <see cref="RadialController"/> yourself.
24-
/// </summary>
25-
public bool ForceMenuItem { get; set; }
26-
2722
/// <summary>
2823
/// Gets or sets the default icon of the menu item that gets added.
2924
/// This will be visible if a user opens their Surface Dial menu by long-pressing the device.
@@ -32,7 +27,7 @@ public sealed class SurfaceDialOptions : DependencyObject
3227
public RadialControllerMenuKnownIcon Icon { get; set; } = RadialControllerMenuKnownIcon.Ruler;
3328

3429
/// <summary>
35-
/// Gets or sets the amount the <see cref="TextBox"/> will be modified for each rotation step on the Surface Dial.
30+
/// Gets or sets the amount the <see cref="TextBox"/> value will be modified for each <see cref="RotationResolutionInDegrees"/> step on the Surface Dial.
3631
/// This can be any double value.
3732
/// </summary>
3833
public double StepValue { get; set; }
@@ -43,6 +38,11 @@ public sealed class SurfaceDialOptions : DependencyObject
4338
/// </summary>
4439
public bool EnableHapticFeedback { get; set; } = true;
4540

41+
/// <summary>
42+
/// Gets or sets the <see cref="RadialController.RotationResolutionInDegrees"/> property for the extension which is the amount the dial needs to rotate to trigger a change. Default is 10.
43+
/// </summary>
44+
public double RotationResolutionInDegrees { get; set; } = 10;
45+
4646
/// <summary>
4747
/// Gets or sets the minimum value the <see cref="TextBox"/> can have when modifying it using a Surface Dial.
4848
/// Default is -100.0.

Microsoft.Toolkit.Uwp.UI/Extensions/TextBox/TextBoxExtensions.SurfaceDial.cs

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,12 @@ public static bool IsSurfaceDialOptionsSupported
7474
/// This helper class will do everything for you, but if you want to control the Menu Items and/or wish to use the same Surface Dial instance
7575
/// This is the property for the static controller so you can access it if needed.
7676
/// </summary>
77-
public static RadialController? Controller
77+
public static RadialController Controller
7878
{
79-
get => _controller;
79+
get
80+
{
81+
return _controller ??= RadialController.CreateForCurrentView();
82+
}
8083
set => _controller = value;
8184
}
8285

@@ -99,7 +102,9 @@ private static void Controller_RotationChanged(RadialController sender, RadialCo
99102

100103
if (double.TryParse(text, out double number))
101104
{
102-
number += args.RotationDeltaInDegrees * options.StepValue;
105+
// We only care about the sign of RotationDeltaInDegrees to determine if we're going up/down
106+
// The value is controlled by the StepValue independent of when we should call the rotation changed event.
107+
number += Math.Sign(args.RotationDeltaInDegrees) * options.StepValue;
103108

104109
if (options.EnableMinMaxValue)
105110
{
@@ -131,10 +136,17 @@ private static void OnSurfaceDialOptionsPropertyChanged(DependencyObject d, Depe
131136
return;
132137
}
133138

139+
// Initialize our RadialController once.
140+
_controller ??= RadialController.CreateForCurrentView();
141+
134142
textBox.GotFocus -= TextBox_GotFocus_SurfaceDial;
135143
textBox.LostFocus -= TextBox_LostFocus_SurfaceDial;
136-
textBox.GotFocus += TextBox_GotFocus_SurfaceDial;
137-
textBox.LostFocus += TextBox_LostFocus_SurfaceDial;
144+
145+
if (e.NewValue is not null)
146+
{
147+
textBox.GotFocus += TextBox_GotFocus_SurfaceDial;
148+
textBox.LostFocus += TextBox_LostFocus_SurfaceDial;
149+
}
138150
}
139151

140152
/// <summary>
@@ -152,7 +164,7 @@ private static void TextBox_LostFocus_SurfaceDial(object sender, RoutedEventArgs
152164

153165
SurfaceDialOptions? options = GetSurfaceDialOptions(_textBox) ?? SurfaceDialOptions.Default;
154166

155-
if (options.ForceMenuItem)
167+
if (_stepTextMenuItem is not null)
156168
{
157169
_controller.Menu.Items.Remove(_stepTextMenuItem);
158170
}
@@ -176,7 +188,8 @@ private static void TextBox_GotFocus_SurfaceDial(object sender, RoutedEventArgs
176188
{
177189
_textBox = sender as TextBox;
178190

179-
if (_textBox is null)
191+
if (_textBox is null ||
192+
_controller is null)
180193
{
181194
return;
182195
}
@@ -186,19 +199,21 @@ private static void TextBox_GotFocus_SurfaceDial(object sender, RoutedEventArgs
186199
return;
187200
}
188201

189-
_controller ??= RadialController.CreateForCurrentView();
202+
if (_controller is not null)
203+
{
204+
_controller.RotationChanged -= Controller_RotationChanged;
205+
_controller.ButtonClicked -= Controller_ButtonClicked;
206+
}
190207

191208
SurfaceDialOptions? options = GetSurfaceDialOptions(_textBox) ?? SurfaceDialOptions.Default;
192209

193-
if (options.ForceMenuItem)
194-
{
195-
_stepTextMenuItem = RadialControllerMenuItem.CreateFromKnownIcon("Step Text Box", options.Icon);
196-
_controller.Menu.Items.Add(_stepTextMenuItem);
197-
_controller.Menu.SelectMenuItem(_stepTextMenuItem);
198-
}
210+
_stepTextMenuItem ??= RadialControllerMenuItem.CreateFromKnownIcon("Step Text Box", options.Icon);
211+
212+
_controller.Menu.Items.Add(_stepTextMenuItem);
213+
_controller.Menu.SelectMenuItem(_stepTextMenuItem);
199214

200215
_controller.UseAutomaticHapticFeedback = options.EnableHapticFeedback;
201-
_controller.RotationResolutionInDegrees = 1;
216+
_controller.RotationResolutionInDegrees = options.RotationResolutionInDegrees;
202217
_controller.RotationChanged += Controller_RotationChanged;
203218

204219
if (options.EnableTapToNextControl)

0 commit comments

Comments
 (0)