|
| 1 | +// Copyright (c) .NET Foundation and Contributors (https://dotnetfoundation.org/ & https://stride3d.net) |
| 2 | +// Distributed under the MIT license. See the LICENSE.md file in the project root for more information. |
| 3 | + |
| 4 | +using Avalonia; |
| 5 | +using Avalonia.Controls; |
| 6 | +using Avalonia.Data; |
| 7 | +using Avalonia.Input; |
| 8 | +using Avalonia.Xaml.Interactivity; |
| 9 | + |
| 10 | +namespace Stride.Core.Presentation.Avalonia.Behaviors; |
| 11 | + |
| 12 | +/// <summary> |
| 13 | +/// Allows the bind the <see cref="ToolTip.TipProperty"/> of a control to a particular target property when the attached control is hovered by the mouse. |
| 14 | +/// This behavior can be used to display the same message that the tool-tip in a status bar, for example. |
| 15 | +/// </summary> |
| 16 | +/// <remarks>This behavior can be used to display the tool tip of some controls in another place, such as a status bar.</remarks> |
| 17 | +public class BindCurrentToolTipStringBehavior : Behavior<Control> |
| 18 | +{ |
| 19 | + /// <summary> |
| 20 | + /// Identifies the <see cref="ToolTipTarget"/> dependency property. |
| 21 | + /// </summary> |
| 22 | + public static readonly StyledProperty<string?> ToolTipTargetProperty = |
| 23 | + AvaloniaProperty.Register<BindCurrentToolTipStringBehavior, string?>(nameof(ToolTipTarget), defaultBindingMode: BindingMode.TwoWay); |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// Identifies the <see cref="DefaultValue"/> dependency property. |
| 27 | + /// </summary> |
| 28 | + public static readonly StyledProperty<string?> DefaultValueProperty = |
| 29 | + AvaloniaProperty.Register<BindCurrentToolTipStringBehavior, string?>(nameof(DefaultValue), defaultBindingMode: BindingMode.TwoWay); |
| 30 | + |
| 31 | + /// <summary> |
| 32 | + /// Gets or sets the tool tip text of the control when the mouse is over the control, or <see cref="DefaultValue"/> otherwise. This property should usually be bound. |
| 33 | + /// </summary> |
| 34 | + public string? ToolTipTarget |
| 35 | + { |
| 36 | + get => GetValue(ToolTipTargetProperty); |
| 37 | + set => SetValue(ToolTipTargetProperty, value); |
| 38 | + } |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// Gets or sets the default value to set when the mouse is not over the control. |
| 42 | + /// </summary> |
| 43 | + public string? DefaultValue |
| 44 | + { |
| 45 | + get => GetValue(DefaultValueProperty); |
| 46 | + set => SetValue(DefaultValueProperty, value); |
| 47 | + } |
| 48 | + |
| 49 | + /// <inheritdoc/> |
| 50 | + protected override void OnAttached() |
| 51 | + { |
| 52 | + base.OnAttached(); |
| 53 | + if (AssociatedObject is not null) |
| 54 | + { |
| 55 | + AssociatedObject.PointerEntered += MouseEnter; |
| 56 | + AssociatedObject.PointerExited += MouseLeave; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + /// <inheritdoc/> |
| 61 | + protected override void OnDetaching() |
| 62 | + { |
| 63 | + if (AssociatedObject is not null) |
| 64 | + { |
| 65 | + AssociatedObject.PointerEntered -= MouseEnter; |
| 66 | + AssociatedObject.PointerExited -= MouseLeave; |
| 67 | + } |
| 68 | + base.OnDetaching(); |
| 69 | + } |
| 70 | + |
| 71 | + private void MouseEnter(object? sender, PointerEventArgs e) |
| 72 | + { |
| 73 | + if (AssociatedObject is not null) |
| 74 | + { |
| 75 | + SetCurrentValue(ToolTipTargetProperty, ToolTip.GetTip(AssociatedObject)); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + private void MouseLeave(object? sender, PointerEventArgs e) |
| 80 | + { |
| 81 | + SetCurrentValue(ToolTipTargetProperty, DefaultValue); |
| 82 | + } |
| 83 | +} |
0 commit comments