Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding conditional tooltip class #4202

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions Microsoft.Toolkit.Uwp.UI/Extensions/ConditionalToolTip.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace Microsoft.Toolkit.Uwp.UI.Extensions
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We started flattening our namespace, so all extensions moved to just be in the UI domain.

{
/// <summary>
/// Provides attached dependency properties that allow a <see cref="UIElement"/>
/// to conditionally enable or disable its tooltip.
/// </summary>
public class ConditionalToolTip : DependencyObject
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can just be a partial class off our existing UIElementExtensions class, it doesn't need to be its own DependencyObject.

{
/// <summary>
/// Attached <see cref="DependencyProperty"/> for enabling or disabling the tooltip for a <see cref="UIElement"/>.
/// </summary>
public static readonly DependencyProperty IsToolTipEnabledProperty = DependencyProperty.RegisterAttached(
"IsToolTipEnabled",
typeof(bool),
typeof(ConditionalToolTip),
new PropertyMetadata(true, OnIsToolTipEnabledChanged));

/// <summary>
/// Attached <see cref="DependencyProperty"/> for binding for the content to display in the tooltip for a <see cref="UIElement"/>.
/// </summary>
public static readonly DependencyProperty ToolTipContentProperty = DependencyProperty.RegisterAttached(
"ToolTipContent",
typeof(object),
typeof(ConditionalToolTip),
new PropertyMetadata(null, OnContentChanged));

/// <summary>
/// Sets the bool determining if the tooltip is enabled for the specified <see cref="UIElement"/>.
/// </summary>
/// <param name="element">The <see cref="UIElement"/> from which to set the associated IsToolTipEnabled value.</param>
/// <param name="value">The bool value to assign.</param>
public static void SetIsToolTipEnabled(UIElement element, bool value) => element.SetValue(IsToolTipEnabledProperty, value);

/// <summary>
/// Gets the bool determing if the tooltip is enabled for the specified <see cref="UIElement"/>.
/// </summary>
/// <param name="element">The <see cref="UIElement"/> from which to get the associated IsToolTipEnabled value.</param>
/// <returns>True if the conditional tooltip is enabled. False, otherwise.</returns>
public static bool GetIsToolTipEnabled(UIElement element) => (bool)element.GetValue(IsToolTipEnabledProperty);

/// <summary>
/// Sets the content of the conditional tooltip associated with the specified <see cref="UIElement"/>.
/// </summary>
/// <param name="element">The <see cref="UIElement"/> from which to set the associated conditional tooltip content.</param>
/// <param name="value">The content to assign.</param>
public static void SetToolTipContent(UIElement element, object value) => element.SetValue(ToolTipContentProperty, value);

/// <summary>
/// Gets the content of the conditional tooltip associated with the specified <see cref="UIElement"/>.
/// </summary>
/// <param name="element">The <see cref="UIElement"/> from which to get the associated conditional tooltip content.</param>
/// <returns>The conditional tooltip's content.</returns>
public static object GetToolTipContent(UIElement element) => (object)element.GetValue(ToolTipContentProperty);

private static void OnIsToolTipEnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is UIElement element)
{
ToolTipService.SetToolTip(d, e.NewValue is true
? GetToolTipContent(element)
: null);
}
}

private static void OnContentChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is UIElement element && GetIsToolTipEnabled(element))
{
ToolTipService.SetToolTip(d, e.NewValue);
}
}
}
}