Skip to content

Commit 8c1e8e6

Browse files
committed
feat: implement vertical tooltip placement for RangeSelector control
1 parent 1d48e68 commit 8c1e8e6

File tree

5 files changed

+101
-14
lines changed

5 files changed

+101
-14
lines changed

components/RangeSelector/src/RangeSelector.Input.Drag.cs

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ private double DragThumbVertical(Thumb? thumb, double min, double max, double ne
111111
var ttHeight = _toolTip.ActualHeight / 2;
112112

113113
Canvas.SetTop(_toolTip, thumbCenter - ttHeight);
114+
UpdateToolTipPositionForVertical();
114115
}
115116

116117
// Invert: top position (0) = Maximum, bottom position (DragWidth) = Minimum
@@ -130,23 +131,33 @@ private void Thumb_DragStarted(Thumb thumb)
130131

131132
if (_toolTip != null)
132133
{
133-
_toolTip.Visibility = Visibility.Visible;
134-
_toolTip.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
135-
136-
if (isHorizontal)
134+
if (!isHorizontal && VerticalToolTipPlacement == VerticalToolTipPlacement.None)
137135
{
138-
var thumbCenter = _absolutePosition + (thumb.Width / 2);
139-
Canvas.SetLeft(_toolTip, thumbCenter - (_toolTip.ActualWidth / 2));
136+
_toolTip.Visibility = Visibility.Collapsed;
140137
}
141138
else
142139
{
143-
var thumbCenter = _absolutePosition + (thumb.Height / 2);
144-
Canvas.SetTop(_toolTip, thumbCenter - (_toolTip.ActualHeight / 2));
145-
}
146-
147-
if (_toolTipText != null)
148-
{
149-
UpdateToolTipText(this, _toolTipText, useMin ? RangeStart : RangeEnd);
140+
_toolTip.Visibility = Visibility.Visible;
141+
142+
// Update tooltip text first so Measure gets accurate size
143+
if (_toolTipText != null)
144+
{
145+
UpdateToolTipText(this, _toolTipText, useMin ? RangeStart : RangeEnd);
146+
}
147+
148+
_toolTip.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
149+
150+
if (isHorizontal)
151+
{
152+
var thumbCenter = _absolutePosition + (thumb.Width / 2);
153+
Canvas.SetLeft(_toolTip, thumbCenter - (_toolTip.DesiredSize.Width / 2));
154+
}
155+
else
156+
{
157+
var thumbCenter = _absolutePosition + (thumb.Height / 2);
158+
Canvas.SetTop(_toolTip, thumbCenter - (_toolTip.DesiredSize.Height / 2));
159+
UpdateToolTipPositionForVertical();
160+
}
150161
}
151162
}
152163

components/RangeSelector/src/RangeSelector.Properties.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,16 @@ public partial class RangeSelector : Control
6969
typeof(RangeSelector),
7070
new PropertyMetadata(DefaultStepFrequency));
7171

72+
/// <summary>
73+
/// Identifies the <see cref="VerticalToolTipPlacement"/> property.
74+
/// </summary>
75+
public static readonly DependencyProperty VerticalToolTipPlacementProperty =
76+
DependencyProperty.Register(
77+
nameof(VerticalToolTipPlacement),
78+
typeof(VerticalToolTipPlacement),
79+
typeof(RangeSelector),
80+
new PropertyMetadata(VerticalToolTipPlacement.Right));
81+
7282
/// <summary>
7383
/// Gets or sets the absolute minimum value of the range.
7484
/// </summary>
@@ -141,6 +151,19 @@ public Orientation Orientation
141151
set => SetValue(OrientationProperty, value);
142152
}
143153

154+
/// <summary>
155+
/// Gets or sets the placement of the tooltip for the vertical range selector.
156+
/// This property only takes effect when <see cref="Orientation"/> is set to <see cref="Orientation.Vertical"/>.
157+
/// </summary>
158+
/// <value>
159+
/// The placement of the tooltip. Default is <see cref="VerticalToolTipPlacement.Right"/>.
160+
/// </value>
161+
public VerticalToolTipPlacement VerticalToolTipPlacement
162+
{
163+
get => (VerticalToolTipPlacement)GetValue(VerticalToolTipPlacementProperty);
164+
set => SetValue(VerticalToolTipPlacementProperty, value);
165+
}
166+
144167
private static void OrientationChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
145168
{
146169
var rangeSelector = d as RangeSelector;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
namespace CommunityToolkit.WinUI.Controls;
6+
7+
/// <summary>
8+
/// Enumeration used to determine the placement of the tooltip
9+
/// for the vertical RangeSelector.
10+
/// </summary>
11+
public enum VerticalToolTipPlacement
12+
{
13+
/// <summary>
14+
/// Tooltip is placed to the right of the thumb.
15+
/// </summary>
16+
Right,
17+
18+
/// <summary>
19+
/// Tooltip is placed to the left of the thumb.
20+
/// </summary>
21+
Left,
22+
23+
/// <summary>
24+
/// Tooltip is not displayed.
25+
/// </summary>
26+
None
27+
}

components/RangeSelector/src/RangeSelector.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,4 +329,30 @@ private void RangeSelector_IsEnabledChanged(object sender, DependencyPropertyCha
329329
{
330330
VisualStateManager.GoToState(this, IsEnabled ? NormalState : DisabledState, true);
331331
}
332+
333+
private void UpdateToolTipPositionForVertical()
334+
{
335+
if (_toolTip == null || _containerCanvas == null)
336+
{
337+
return;
338+
}
339+
340+
// Offset to position tooltip beside the thumb
341+
const double toolTipOffset = 52;
342+
343+
switch (VerticalToolTipPlacement)
344+
{
345+
case VerticalToolTipPlacement.Right:
346+
Canvas.SetLeft(_toolTip, toolTipOffset);
347+
break;
348+
case VerticalToolTipPlacement.Left:
349+
_toolTip.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
350+
var toolTipWidth = _toolTip.DesiredSize.Width;
351+
Canvas.SetLeft(_toolTip, -toolTipWidth - (toolTipOffset - _containerCanvas.ActualWidth));
352+
break;
353+
case VerticalToolTipPlacement.None:
354+
_toolTip.Visibility = Visibility.Collapsed;
355+
break;
356+
}
357+
}
332358
}

components/RangeSelector/src/RangeSelector.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@
262262
<Setter Target="MaxThumb.Margin" Value="7,0,0,0" />
263263
<Setter Target="MaxThumb.HorizontalAlignment" Value="Center" />
264264
<Setter Target="MaxThumb.VerticalAlignment" Value="Top" />
265-
<Setter Target="ToolTip.Margin" Value="52,0,0,0" />
265+
<Setter Target="ToolTip.Margin" Value="0" />
266266
</VisualState.Setters>
267267
</VisualState>
268268
</VisualStateGroup>

0 commit comments

Comments
 (0)