Skip to content

Commit e8056d8

Browse files
committed
Cherry-pick minimum changes to presentation libraries
1 parent 24e71d9 commit e8056d8

31 files changed

+2313
-412
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright (c) .NET Foundation and Contributors (https://dotnetfoundation.org/ & https://stride3d.net) and Silicon Studio Corp. (https://www.siliconstudio.co.jp)
2+
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.
3+
4+
using System.Globalization;
5+
using Avalonia;
6+
using Stride.Core.Presentation.Avalonia.Internal;
7+
8+
namespace Stride.Core.Presentation.Avalonia.Converters;
9+
10+
/// <seealso cref="OrMulti"/>
11+
/// <seealso cref="XOrMulti"/>
12+
public sealed class AndMulti : MultiValueConverterBase<AndMulti>
13+
{
14+
public override object Convert(IList<object?> values, Type targetType, object? parameter, CultureInfo culture)
15+
{
16+
if (values.Count < 2) return AvaloniaProperty.UnsetValue;
17+
18+
return values.All(x => x is true).Box();
19+
}
20+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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 System.Globalization;
5+
using Avalonia;
6+
using Stride.Core.Presentation.Avalonia.Internal;
7+
8+
namespace Stride.Core.Presentation.Avalonia.Converters;
9+
10+
/// <summary>
11+
/// This converter will convert a boolean to the object given in parameter if its true,
12+
/// and to <see cref="AvaloniaProperty.UnsetValue"/> if it's false.
13+
/// <see cref="ConvertBack"/> is supported and will return whether the given object is different from
14+
/// <see cref="AvaloniaProperty.UnsetValue"/>.
15+
/// </summary>
16+
public sealed class BoolToParam : ValueConverterBase<BoolToParam>
17+
{
18+
/// <inheritdoc/>
19+
public override object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
20+
{
21+
var result = ConverterHelper.ConvertToBoolean(value, culture);
22+
return result ? parameter : AvaloniaProperty.UnsetValue;
23+
}
24+
25+
/// <inheritdoc/>
26+
public override object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
27+
{
28+
var result = value != AvaloniaProperty.UnsetValue;
29+
return result.Box();
30+
}
31+
}

0 commit comments

Comments
 (0)