4 changed files with 300 additions and 108 deletions
@ -1,22 +1,34 @@ |
|||||
<UserControl xmlns="https://github.com/avaloniaui"> |
<UserControl xmlns="https://github.com/avaloniaui"> |
||||
<StackPanel Orientation="Vertical" Gap="4"> |
<StackPanel Orientation="Vertical" |
||||
<TextBlock Classes="h1">ToolTip</TextBlock> |
Gap="4"> |
||||
<TextBlock Classes="h2">A control which pops up a hint when a control is hovered</TextBlock> |
<TextBlock Classes="h1">ToolTip</TextBlock> |
||||
|
<TextBlock Classes="h2">A control which pops up a hint when a control is hovered</TextBlock> |
||||
|
|
||||
<StackPanel Orientation="Horizontal" |
<StackPanel Orientation="Horizontal" |
||||
Margin="0,16,0,0" |
Margin="0,16,0,0" |
||||
HorizontalAlignment="Center" |
HorizontalAlignment="Center" |
||||
Gap="16"> |
Gap="16"> |
||||
<Border Background="{StyleResource ThemeAccentBrush}" |
<CheckBox IsChecked="{Binding ElementName=Border, Path=(ToolTip.IsOpen)}" |
||||
Padding="48,48,48,48"> |
Content="Show ToolTip" /> |
||||
<ToolTip.Tip> |
<Border Name="Border" |
||||
<StackPanel> |
Background="{StyleResource ThemeAccentBrush}" |
||||
<TextBlock Classes="h1">ToolTip</TextBlock> |
Margin="5" |
||||
<TextBlock Classes="h2">A control which pops up a hint when a control is hovered</TextBlock> |
Padding="50" |
||||
</StackPanel> |
ToolTip.Placement="Bottom"> |
||||
</ToolTip.Tip> |
<ToolTip.Tip> |
||||
<TextBlock>Hover Here</TextBlock> |
<StackPanel> |
||||
</Border> |
<TextBlock Classes="h1">ToolTip</TextBlock> |
||||
|
<TextBlock Classes="h2">A control which pops up a hint when a control is hovered</TextBlock> |
||||
|
</StackPanel> |
||||
|
</ToolTip.Tip> |
||||
|
<TextBlock>Hover Here</TextBlock> |
||||
|
</Border> |
||||
|
<Border Background="{StyleResource ThemeAccentBrush}" |
||||
|
Margin="5" |
||||
|
Padding="50" |
||||
|
ToolTip.Tip="Another tip"> |
||||
|
<TextBlock>And Here</TextBlock> |
||||
|
</Border> |
||||
|
</StackPanel> |
||||
</StackPanel> |
</StackPanel> |
||||
</StackPanel> |
|
||||
</UserControl> |
</UserControl> |
||||
@ -0,0 +1,98 @@ |
|||||
|
using System; |
||||
|
using Avalonia.Input; |
||||
|
using Avalonia.Threading; |
||||
|
|
||||
|
namespace Avalonia.Controls |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Handeles <see cref="ToolTip"/> interaction with controls.
|
||||
|
/// </summary>
|
||||
|
internal sealed class ToolTipService |
||||
|
{ |
||||
|
public static ToolTipService Instance { get; } = new ToolTipService(); |
||||
|
|
||||
|
private DispatcherTimer _timer; |
||||
|
|
||||
|
private ToolTipService() { } |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// called when the <see cref="ToolTip.TipProperty"/> property changes on a control.
|
||||
|
/// </summary>
|
||||
|
/// <param name="e">The event args.</param>
|
||||
|
internal void TipChanged(AvaloniaPropertyChangedEventArgs e) |
||||
|
{ |
||||
|
var control = (Control)e.Sender; |
||||
|
|
||||
|
if (e.OldValue != null) |
||||
|
{ |
||||
|
control.PointerEnter -= ControlPointerEnter; |
||||
|
control.PointerLeave -= ControlPointerLeave; |
||||
|
} |
||||
|
|
||||
|
if (e.NewValue != null) |
||||
|
{ |
||||
|
control.PointerEnter += ControlPointerEnter; |
||||
|
control.PointerLeave += ControlPointerLeave; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Called when the pointer enters a control with an attached tooltip.
|
||||
|
/// </summary>
|
||||
|
/// <param name="sender">The event sender.</param>
|
||||
|
/// <param name="e">The event args.</param>
|
||||
|
private void ControlPointerEnter(object sender, PointerEventArgs e) |
||||
|
{ |
||||
|
StopTimer(); |
||||
|
|
||||
|
var control = (Control)sender; |
||||
|
var showDelay = ToolTip.GetShowDelay(control); |
||||
|
if (showDelay == 0) |
||||
|
{ |
||||
|
Open(control); |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
StartShowTimer(showDelay, control); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Called when the pointer leaves a control with an attached tooltip.
|
||||
|
/// </summary>
|
||||
|
/// <param name="sender">The event sender.</param>
|
||||
|
/// <param name="e">The event args.</param>
|
||||
|
private void ControlPointerLeave(object sender, PointerEventArgs e) |
||||
|
{ |
||||
|
var control = (Control)sender; |
||||
|
Close(control); |
||||
|
} |
||||
|
|
||||
|
private void StartShowTimer(int showDelay, Control control) |
||||
|
{ |
||||
|
_timer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(showDelay) }; |
||||
|
_timer.Tick += (o, e) => Open(control); |
||||
|
_timer.Start(); |
||||
|
} |
||||
|
|
||||
|
private void Open(Control control) |
||||
|
{ |
||||
|
StopTimer(); |
||||
|
|
||||
|
ToolTip.SetIsOpen(control, true); |
||||
|
} |
||||
|
|
||||
|
private void Close(Control control) |
||||
|
{ |
||||
|
StopTimer(); |
||||
|
|
||||
|
ToolTip.SetIsOpen(control, false); |
||||
|
} |
||||
|
|
||||
|
private void StopTimer() |
||||
|
{ |
||||
|
_timer?.Stop(); |
||||
|
_timer = null; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue