给 Slider 设置 tooltip

This commit is contained in:
polarboy
2024-07-28 18:53:49 +08:00
parent 8446ca08af
commit eeb37f3296
6 changed files with 95 additions and 14 deletions
@@ -127,6 +127,7 @@ internal class PopupShadowLayer : LiteWindow, IShadowDecorator
private void TargetPopupPositionChanged(object? sender, PixelPointEventArgs e)
{
SetupShadowRenderer();
SetupPositionAndSize();
}
+48 -5
View File
@@ -285,6 +285,21 @@ public class Slider : RangeBase
_pointerPressDispose = this.AddDisposableHandler(PointerPressedEvent, TrackPressed, RoutingStrategies.Tunnel);
_pointerReleaseDispose = this.AddDisposableHandler(PointerReleasedEvent, TrackReleased, RoutingStrategies.Tunnel);
_pointerMovedDispose = this.AddDisposableHandler(PointerMovedEvent, TrackMoved, RoutingStrategies.Tunnel);
if (_track is not null) {
if (!IsRangeMode) {
if (_track.StartSliderThumb is not null) {
ToolTip.SetTip(_track.StartSliderThumb, FormatValue(Value));
}
} else {
if (_track.StartSliderThumb is not null) {
ToolTip.SetTip(_track.StartSliderThumb, FormatValue(RangeValue.StartValue));
}
if (_track.EndSliderThumb is not null) {
ToolTip.SetTip(_track.EndSliderThumb, FormatValue(RangeValue.EndValue));
}
}
}
}
/// <inheritdoc />
@@ -406,6 +421,12 @@ public class Slider : RangeBase
private void TrackReleased(object? sender, PointerReleasedEventArgs e)
{
if (_graspedThumb is not null) {
ToolTip.SetIsCustomHide(_graspedThumb, false);
if (!_graspedThumb.Bounds.Contains(e.GetPosition(_track))) {
ToolTip.SetIsOpen(_graspedThumb, false);
}
}
_isDragging = false;
_graspedThumb = null;
}
@@ -416,6 +437,10 @@ public class Slider : RangeBase
var posOnTrack = e.GetCurrentPoint(_track);
_graspedThumb = GetEffectiveMoveThumb(posOnTrack.Position);
MoveToPoint(posOnTrack);
if (_graspedThumb is not null) {
ToolTip.SetIsCustomHide(_graspedThumb, true);
}
_isDragging = true;
}
}
@@ -439,19 +464,19 @@ public class Slider : RangeBase
var calcVal = Math.Abs(invert - logicalPos);
var range = Maximum - Minimum;
var finalValue = calcVal * range + Minimum;
finalValue = IsSnapToTickEnabled ? SnapToTick(finalValue) : finalValue;
if (!IsRangeMode) {
SetCurrentValue(ValueProperty, IsSnapToTickEnabled ? SnapToTick(finalValue) : finalValue);
SetCurrentValue(ValueProperty, finalValue);
} else {
var targetValue = IsSnapToTickEnabled ? SnapToTick(finalValue) : finalValue;
var currentRangeValue = RangeValue;
if (sliderThumb == _track.StartSliderThumb) {
currentRangeValue.StartValue = targetValue;
currentRangeValue.StartValue = finalValue;
} else {
currentRangeValue.EndValue = targetValue;
currentRangeValue.EndValue = finalValue;
}
SetCurrentValue(RangeValueProperty, currentRangeValue);
}
if (sliderThumb is not null && !sliderThumb.IsFocused) {
sliderThumb.Focus();
}
@@ -507,9 +532,27 @@ public class Slider : RangeBase
if (change.Property == OrientationProperty) {
UpdatePseudoClasses(change.GetNewValue<Orientation>());
} else if (change.Property == ValueProperty) {
if (_track is not null && _track.StartSliderThumb is not null) {
ToolTip.SetTip(_track.StartSliderThumb, FormatValue(Value));
}
} else if (change.Property == RangeValueProperty) {
if (_track is not null) {
if (_track.StartSliderThumb is not null) {
ToolTip.SetTip(_track.StartSliderThumb, FormatValue(RangeValue.StartValue));
}
if (_track.EndSliderThumb is not null) {
ToolTip.SetTip(_track.EndSliderThumb, FormatValue(RangeValue.EndValue));
}
}
}
}
private string FormatValue(double value)
{
return $"{Math.Round(value)}";
}
/// <summary>
/// Called when user start dragging the <see cref="SliderThumb"/>.
/// </summary>
@@ -1,4 +1,5 @@
using AtomUI.Styling;
using Avalonia.Controls;
using Avalonia.Controls.Templates;
using Avalonia.Data;
using Avalonia.Input;
@@ -26,6 +27,8 @@ internal class SliderTheme : ControlTheme
{
Name = StartThumbPart,
};
ToolTip.SetPlacement(startSliderThumb, PlacementMode.Top);
ToolTip.SetShowDelay(startSliderThumb, 50);
startSliderThumb.RegisterInNameScope(scope);
var endSliderThumb = new SliderThumb
@@ -33,6 +36,8 @@ internal class SliderTheme : ControlTheme
Name = EndThumbPart,
};
endSliderThumb.RegisterInNameScope(scope);
ToolTip.SetPlacement(endSliderThumb, PlacementMode.Top);
ToolTip.SetShowDelay(endSliderThumb, 50);
var sliderTrack = new SliderTrack
{
+2 -2
View File
@@ -278,8 +278,8 @@ public class SliderTrack : Control, IControlCustomStyle
sender.GetValue(MaximumProperty));
return new SliderRangeValue()
{
StartValue = Math.Min(startValue, endValue),
EndValue = Math.Max(startValue, endValue),
StartValue = startValue,
EndValue = endValue,
};
}
return sender.GetValue(RangeValueProperty);
+33 -6
View File
@@ -1,8 +1,5 @@
using System.Reflection;
using AtomUI.ColorSystem;
using AtomUI.Controls.MotionScene;
using AtomUI.Controls.Utils;
using AtomUI.MotionScene;
using AtomUI.Reflection;
using AtomUI.Styling;
using AtomUI.Utils;
@@ -21,12 +18,12 @@ namespace AtomUI.Controls;
[PseudoClasses(StdPseudoClass.Open)]
public class ToolTip : TemplatedControl,
IShadowMaskInfoProvider,
IControlCustomStyle
IShadowMaskInfoProvider,
IControlCustomStyle
{
#region 公共属性定义
/// <summary>
/// <summary>
/// Defines the <see cref="Content"/> property.
/// </summary>
public static readonly StyledProperty<object?> ContentProperty =
@@ -43,6 +40,12 @@ public class ToolTip : TemplatedControl,
/// </summary>
public static readonly AttachedProperty<bool> IsOpenProperty =
AvaloniaProperty.RegisterAttached<ToolTip, Control, bool>("IsOpen");
/// <summary>
/// 自己手动关闭哦
/// </summary>
public static readonly AttachedProperty<bool> IsCustomHideProperty =
AvaloniaProperty.RegisterAttached<ToolTip, Control, bool>("IsCustomHide", false);
/// <summary>
/// Defines the ToolTip.PresetColor attached property.
@@ -337,6 +340,16 @@ public class ToolTip : TemplatedControl,
{
element.SetValue(IsShowArrowProperty, flag);
}
public static bool GetIsCustomHide(Control element)
{
return element.GetValue(IsCustomHideProperty);
}
public static void SetIsCustomHide(Control element, bool flag)
{
element.SetValue(IsCustomHideProperty, flag);
}
/// <summary>
/// 箭头是否始终指向居中位置
@@ -529,6 +542,20 @@ public class ToolTip : TemplatedControl,
}
}
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
{
base.OnPropertyChanged(change);
if (change.Property == ContentProperty) {
if (_arrowDecoratedBox is not null) {
if (_arrowDecoratedBox.Child is TextBlock textBlock) {
if (change.NewValue is string text) {
textBlock.Text = text;
}
}
}
}
}
private static void IsShowArrowChanged(AvaloniaPropertyChangedEventArgs e)
{
var control = (Control)e.Sender;
@@ -66,6 +66,9 @@ internal sealed class ToolTipService : IDisposable
case RawPointerEventType.MiddleButtonDown:
case RawPointerEventType.XButton1Down:
case RawPointerEventType.XButton2Down:
if (_tipControl is not null && ToolTip.GetIsCustomHide(_tipControl)) {
break;
}
ClearTip();
break;
}
@@ -216,7 +219,9 @@ internal sealed class ToolTipService : IDisposable
private void Close(Control control)
{
ToolTip.SetIsOpen(control, false);
if (!ToolTip.GetIsCustomHide(control)) {
ToolTip.SetIsOpen(control, false);
}
}
private void StopTimer()