committed by
GitHub
38 changed files with 505 additions and 261 deletions
@ -1,5 +1,5 @@ |
|||||
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> |
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> |
||||
<ItemGroup> |
<ItemGroup> |
||||
<PackageReference Include="Moq" Version="4.7.25" /> |
<PackageReference Include="Moq" Version="4.7.99" /> |
||||
</ItemGroup> |
</ItemGroup> |
||||
</Project> |
</Project> |
||||
|
|||||
@ -1,22 +1,41 @@ |
|||||
<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" |
<Grid RowDefinitions="Auto,Auto" |
||||
|
ColumnDefinitions="Auto,Auto" |
||||
Margin="0,16,0,0" |
Margin="0,16,0,0" |
||||
HorizontalAlignment="Center" |
HorizontalAlignment="Center"> |
||||
Gap="16"> |
<Border Grid.Column="0" |
||||
<Border Background="{StyleResource ThemeAccentBrush}" |
Grid.Row="1" |
||||
Padding="48,48,48,48"> |
Background="{StyleResource ThemeAccentBrush}" |
||||
<ToolTip.Tip> |
Margin="5" |
||||
<StackPanel> |
Padding="50" |
||||
<TextBlock Classes="h1">ToolTip</TextBlock> |
ToolTip.Tip="This is a ToolTip"> |
||||
<TextBlock Classes="h2">A control which pops up a hint when a control is hovered</TextBlock> |
<TextBlock>Hover Here</TextBlock> |
||||
</StackPanel> |
</Border> |
||||
</ToolTip.Tip> |
<CheckBox Grid.Column="1" |
||||
<TextBlock>Hover Here</TextBlock> |
Margin="5" |
||||
</Border> |
Grid.Row="0" |
||||
|
IsChecked="{Binding ElementName=Border, Path=(ToolTip.IsOpen)}" |
||||
|
Content="ToolTip Open" /> |
||||
|
<Border Name="Border" |
||||
|
Grid.Column="1" |
||||
|
Grid.Row="1" |
||||
|
Background="{StyleResource ThemeAccentBrush}" |
||||
|
Margin="5" |
||||
|
Padding="50" |
||||
|
ToolTip.Placement="Bottom"> |
||||
|
<ToolTip.Tip> |
||||
|
<StackPanel> |
||||
|
<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>ToolTip bottom placement</TextBlock> |
||||
|
</Border> |
||||
|
</Grid> |
||||
</StackPanel> |
</StackPanel> |
||||
</StackPanel> |
|
||||
</UserControl> |
</UserControl> |
||||
@ -0,0 +1,27 @@ |
|||||
|
// Copyright (c) The Avalonia Project. All rights reserved.
|
||||
|
// Licensed under the MIT license. See licence.md file in the project root for full license information.
|
||||
|
|
||||
|
using System; |
||||
|
|
||||
|
namespace Avalonia.Controls.Templates |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Defines an element that has a <see cref="DataTemplates"/> collection.
|
||||
|
/// </summary>
|
||||
|
public interface IDataTemplateHost |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Gets the data templates for the element.
|
||||
|
/// </summary>
|
||||
|
DataTemplates DataTemplates { get; } |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Gets a value indicating whether <see cref="DataTemplates"/> is initialized.
|
||||
|
/// </summary>
|
||||
|
/// <remarks>
|
||||
|
/// The <see cref="DataTemplates"/> property may be lazily initialized, if so this property
|
||||
|
/// indicates whether it has been initialized.
|
||||
|
/// </remarks>
|
||||
|
bool IsDataTemplatesInitialized { get; } |
||||
|
} |
||||
|
} |
||||
@ -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