Skip to content

Commit feed81f

Browse files
Copilotmattleibow
andcommitted
Add Delay and Duration properties to ToolTip functionality
Co-authored-by: mattleibow <1096616+mattleibow@users.noreply.github.com>
1 parent ce05f9f commit feed81f

File tree

3 files changed

+152
-1
lines changed

3 files changed

+152
-1
lines changed

src/Controls/src/Core/ToolTipProperties.cs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ public class ToolTipProperties
88
public static readonly BindableProperty TextProperty =
99
BindableProperty.CreateAttached("Text", typeof(string), typeof(ToolTipProperties), defaultValue: null, propertyChanged: OnToolTipPropertyChanged);
1010

11+
/// <summary>Bindable property for attached property <c>Delay</c>.</summary>
12+
public static readonly BindableProperty DelayProperty =
13+
BindableProperty.CreateAttached("Delay", typeof(int?), typeof(ToolTipProperties), defaultValue: null, propertyChanged: OnToolTipPropertyChanged);
14+
15+
/// <summary>Bindable property for attached property <c>Duration</c>.</summary>
16+
public static readonly BindableProperty DurationProperty =
17+
BindableProperty.CreateAttached("Duration", typeof(int?), typeof(ToolTipProperties), defaultValue: null, propertyChanged: OnToolTipPropertyChanged);
18+
1119
static void OnToolTipPropertyChanged(BindableObject bindable, object oldValue, object newValue)
1220
{
1321
if (bindable is IElement element)
@@ -24,14 +32,36 @@ public static void SetText(BindableObject bindable, object value)
2432
bindable.SetValue(TextProperty, value);
2533
}
2634

35+
public static int? GetDelay(BindableObject bindable)
36+
{
37+
return (int?)bindable.GetValue(DelayProperty);
38+
}
39+
40+
public static void SetDelay(BindableObject bindable, int? value)
41+
{
42+
bindable.SetValue(DelayProperty, value);
43+
}
44+
45+
public static int? GetDuration(BindableObject bindable)
46+
{
47+
return (int?)bindable.GetValue(DurationProperty);
48+
}
49+
50+
public static void SetDuration(BindableObject bindable, int? value)
51+
{
52+
bindable.SetValue(DurationProperty, value);
53+
}
54+
2755
internal static ToolTip? GetToolTip(BindableObject bindable)
2856
{
2957
if (!bindable.IsSet(TextProperty))
3058
return null;
3159

3260
return new ToolTip()
3361
{
34-
Content = GetText(bindable)
62+
Content = GetText(bindable),
63+
Delay = GetDelay(bindable),
64+
Duration = GetDuration(bindable)
3565
};
3666
}
3767
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
using Microsoft.Maui.Controls;
2+
using Xunit;
3+
4+
namespace Microsoft.Maui.Controls.Core.UnitTests
5+
{
6+
public class ToolTipPropertiesTests : BaseTestFixture
7+
{
8+
[Fact]
9+
public void GetText_ReturnsCorrectValue()
10+
{
11+
var button = new Button();
12+
var text = "Test tooltip";
13+
14+
ToolTipProperties.SetText(button, text);
15+
var result = ToolTipProperties.GetText(button);
16+
17+
Assert.Equal(text, result);
18+
}
19+
20+
[Fact]
21+
public void GetDelay_ReturnsCorrectValue()
22+
{
23+
var button = new Button();
24+
var delay = 1000;
25+
26+
ToolTipProperties.SetDelay(button, delay);
27+
var result = ToolTipProperties.GetDelay(button);
28+
29+
Assert.Equal(delay, result);
30+
}
31+
32+
[Fact]
33+
public void GetDuration_ReturnsCorrectValue()
34+
{
35+
var button = new Button();
36+
var duration = 5000;
37+
38+
ToolTipProperties.SetDuration(button, duration);
39+
var result = ToolTipProperties.GetDuration(button);
40+
41+
Assert.Equal(duration, result);
42+
}
43+
44+
[Fact]
45+
public void GetDelay_ReturnsNullWhenNotSet()
46+
{
47+
var button = new Button();
48+
var result = ToolTipProperties.GetDelay(button);
49+
50+
Assert.Null(result);
51+
}
52+
53+
[Fact]
54+
public void GetDuration_ReturnsNullWhenNotSet()
55+
{
56+
var button = new Button();
57+
var result = ToolTipProperties.GetDuration(button);
58+
59+
Assert.Null(result);
60+
}
61+
62+
[Fact]
63+
public void GetToolTip_IncludesDelayAndDuration()
64+
{
65+
var button = new Button();
66+
var text = "Test tooltip";
67+
var delay = 1000;
68+
var duration = 5000;
69+
70+
ToolTipProperties.SetText(button, text);
71+
ToolTipProperties.SetDelay(button, delay);
72+
ToolTipProperties.SetDuration(button, duration);
73+
74+
var tooltip = ToolTipProperties.GetToolTip(button);
75+
76+
Assert.NotNull(tooltip);
77+
Assert.Equal(text, tooltip.Content);
78+
Assert.Equal(delay, tooltip.Delay);
79+
Assert.Equal(duration, tooltip.Duration);
80+
}
81+
82+
[Fact]
83+
public void GetToolTip_ReturnsNullWhenTextNotSet()
84+
{
85+
var button = new Button();
86+
87+
ToolTipProperties.SetDelay(button, 1000);
88+
ToolTipProperties.SetDuration(button, 5000);
89+
90+
var tooltip = ToolTipProperties.GetToolTip(button);
91+
92+
Assert.Null(tooltip);
93+
}
94+
95+
[Fact]
96+
public void GetToolTip_WithNullDelayAndDuration()
97+
{
98+
var button = new Button();
99+
var text = "Test tooltip";
100+
101+
ToolTipProperties.SetText(button, text);
102+
103+
var tooltip = ToolTipProperties.GetToolTip(button);
104+
105+
Assert.NotNull(tooltip);
106+
Assert.Equal(text, tooltip.Content);
107+
Assert.Null(tooltip.Delay);
108+
Assert.Null(tooltip.Duration);
109+
}
110+
}
111+
}

src/Core/src/Primitives/ToolTip.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,15 @@ public partial class ToolTip
1010
/// ToolTip content.
1111
/// </summary>
1212
public object? Content { get; set; }
13+
14+
/// <summary>
15+
/// Gets or sets the delay (in milliseconds) before the tooltip is shown.
16+
/// </summary>
17+
public int? Delay { get; set; }
18+
19+
/// <summary>
20+
/// Gets or sets the duration (in milliseconds) that the tooltip is shown.
21+
/// </summary>
22+
public int? Duration { get; set; }
1323
}
1424
}

0 commit comments

Comments
 (0)