refactor PreviewSlider
This commit is contained in:
1 parent
0546207f1a
commit
47a986270d
6 files changed
+535
-393
No files matched your search
@@ -40,7 +40,7 @@ namespace HandyControl.Controls
|
||||
|
||||
private ButtonBase _dropDownButton;
|
||||
|
||||
private Popup _popUp;
|
||||
private Popup _popup;
|
||||
|
||||
private bool _disablePopupReopen;
|
||||
|
||||
@@ -135,9 +135,9 @@ namespace HandyControl.Controls
|
||||
var dp = d as DateTimePicker;
|
||||
|
||||
var newValue = (bool)e.NewValue;
|
||||
if (dp?._popUp != null && dp._popUp.IsOpen != newValue)
|
||||
if (dp?._popup != null && dp._popup.IsOpen != newValue)
|
||||
{
|
||||
dp._popUp.IsOpen = newValue;
|
||||
dp._popup.IsOpen = newValue;
|
||||
if (newValue)
|
||||
{
|
||||
dp._originalSelectedDateTime = dp.SelectedDateTime;
|
||||
@@ -275,12 +275,12 @@ namespace HandyControl.Controls
|
||||
public override void OnApplyTemplate()
|
||||
{
|
||||
if (DesignerProperties.GetIsInDesignMode(this)) return;
|
||||
if (_popUp != null)
|
||||
if (_popup != null)
|
||||
{
|
||||
_popUp.PreviewMouseLeftButtonDown -= PopUp_PreviewMouseLeftButtonDown;
|
||||
_popUp.Opened -= PopUp_Opened;
|
||||
_popUp.Closed -= PopUp_Closed;
|
||||
_popUp.Child = null;
|
||||
_popup.PreviewMouseLeftButtonDown -= PopupPreviewMouseLeftButtonDown;
|
||||
_popup.Opened -= PopupOpened;
|
||||
_popup.Closed -= PopupClosed;
|
||||
_popup.Child = null;
|
||||
}
|
||||
|
||||
if (_dropDownButton != null)
|
||||
@@ -298,20 +298,20 @@ namespace HandyControl.Controls
|
||||
|
||||
base.OnApplyTemplate();
|
||||
|
||||
_popUp = GetTemplateChild(ElementPopup) as Popup;
|
||||
_popup = GetTemplateChild(ElementPopup) as Popup;
|
||||
_dropDownButton = GetTemplateChild(ElementButton) as Button;
|
||||
_textBox = GetTemplateChild(ElementTextBox) as WatermarkTextBox;
|
||||
|
||||
CheckNull();
|
||||
|
||||
_popUp.PreviewMouseLeftButtonDown += PopUp_PreviewMouseLeftButtonDown;
|
||||
_popUp.Opened += PopUp_Opened;
|
||||
_popUp.Closed += PopUp_Closed;
|
||||
_popUp.Child = _calendarWithClock;
|
||||
_popup.PreviewMouseLeftButtonDown += PopupPreviewMouseLeftButtonDown;
|
||||
_popup.Opened += PopupOpened;
|
||||
_popup.Closed += PopupClosed;
|
||||
_popup.Child = _calendarWithClock;
|
||||
|
||||
if (IsDropDownOpen)
|
||||
{
|
||||
_popUp.IsOpen = true;
|
||||
_popup.IsOpen = true;
|
||||
}
|
||||
|
||||
_dropDownButton.Click += DropDownButton_Click;
|
||||
@@ -397,7 +397,7 @@ namespace HandyControl.Controls
|
||||
|
||||
private void CheckNull()
|
||||
{
|
||||
if (_dropDownButton == null || _popUp == null || _textBox == null)
|
||||
if (_dropDownButton == null || _popup == null || _textBox == null)
|
||||
throw new Exception();
|
||||
}
|
||||
|
||||
@@ -411,7 +411,7 @@ namespace HandyControl.Controls
|
||||
_calendarWithClock.Confirmed += CalendarWithClock_Confirmed;
|
||||
}
|
||||
|
||||
private void CalendarWithClock_Confirmed() => TogglePopUp();
|
||||
private void CalendarWithClock_Confirmed() => TogglePopup();
|
||||
|
||||
private void CalendarWithClock_SelectedDateTimeChanged(object sender, FunctionEventArgs<DateTime?> e) => SelectedDateTime = e.Info;
|
||||
|
||||
@@ -468,7 +468,7 @@ namespace HandyControl.Controls
|
||||
{
|
||||
if ((Keyboard.Modifiers & ModifierKeys.Alt) == ModifierKeys.Alt)
|
||||
{
|
||||
TogglePopUp();
|
||||
TogglePopup();
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -504,7 +504,7 @@ namespace HandyControl.Controls
|
||||
return _isHandlerSuspended != null && _isHandlerSuspended.ContainsKey(property);
|
||||
}
|
||||
|
||||
private void PopUp_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
|
||||
private void PopupPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
|
||||
{
|
||||
if (sender is Popup popup && !popup.StaysOpen)
|
||||
{
|
||||
@@ -515,7 +515,7 @@ namespace HandyControl.Controls
|
||||
}
|
||||
}
|
||||
|
||||
private void PopUp_Opened(object sender, EventArgs e)
|
||||
private void PopupOpened(object sender, EventArgs e)
|
||||
{
|
||||
if (!IsDropDownOpen)
|
||||
{
|
||||
@@ -527,7 +527,7 @@ namespace HandyControl.Controls
|
||||
OnPickerOpened(new RoutedEventArgs());
|
||||
}
|
||||
|
||||
private void PopUp_Closed(object sender, EventArgs e)
|
||||
private void PopupClosed(object sender, EventArgs e)
|
||||
{
|
||||
if (IsDropDownOpen)
|
||||
{
|
||||
@@ -542,9 +542,9 @@ namespace HandyControl.Controls
|
||||
OnPickerClosed(new RoutedEventArgs());
|
||||
}
|
||||
|
||||
private void DropDownButton_Click(object sender, RoutedEventArgs e) => TogglePopUp();
|
||||
private void DropDownButton_Click(object sender, RoutedEventArgs e) => TogglePopup();
|
||||
|
||||
private void TogglePopUp()
|
||||
private void TogglePopup()
|
||||
{
|
||||
if (IsDropDownOpen)
|
||||
{
|
||||
|
||||
@@ -38,7 +38,7 @@ namespace HandyControl.Controls
|
||||
|
||||
private ButtonBase _dropDownButton;
|
||||
|
||||
private Popup _popUp;
|
||||
private Popup _popup;
|
||||
|
||||
private bool _disablePopupReopen;
|
||||
|
||||
@@ -147,9 +147,9 @@ namespace HandyControl.Controls
|
||||
var dp = d as TimePicker;
|
||||
|
||||
var newValue = (bool)e.NewValue;
|
||||
if (dp?._popUp != null && dp._popUp.IsOpen != newValue)
|
||||
if (dp?._popup != null && dp._popup.IsOpen != newValue)
|
||||
{
|
||||
dp._popUp.IsOpen = newValue;
|
||||
dp._popup.IsOpen = newValue;
|
||||
if (newValue)
|
||||
{
|
||||
dp._originalSelectedTime = dp.SelectedTime;
|
||||
@@ -357,12 +357,12 @@ namespace HandyControl.Controls
|
||||
public override void OnApplyTemplate()
|
||||
{
|
||||
if (DesignerProperties.GetIsInDesignMode(this)) return;
|
||||
if (_popUp != null)
|
||||
if (_popup != null)
|
||||
{
|
||||
_popUp.PreviewMouseLeftButtonDown -= PopUp_PreviewMouseLeftButtonDown;
|
||||
_popUp.Opened -= PopUp_Opened;
|
||||
_popUp.Closed -= PopUp_Closed;
|
||||
_popUp.Child = null;
|
||||
_popup.PreviewMouseLeftButtonDown -= PopupPreviewMouseLeftButtonDown;
|
||||
_popup.Opened -= PopupOpened;
|
||||
_popup.Closed -= PopupClosed;
|
||||
_popup.Child = null;
|
||||
}
|
||||
|
||||
if (_dropDownButton != null)
|
||||
@@ -380,20 +380,20 @@ namespace HandyControl.Controls
|
||||
|
||||
base.OnApplyTemplate();
|
||||
|
||||
_popUp = GetTemplateChild(ElementPopup) as Popup;
|
||||
_popup = GetTemplateChild(ElementPopup) as Popup;
|
||||
_dropDownButton = GetTemplateChild(ElementButton) as Button;
|
||||
_textBox = GetTemplateChild(ElementTextBox) as WatermarkTextBox;
|
||||
|
||||
CheckNull();
|
||||
|
||||
_popUp.PreviewMouseLeftButtonDown += PopUp_PreviewMouseLeftButtonDown;
|
||||
_popUp.Opened += PopUp_Opened;
|
||||
_popUp.Closed += PopUp_Closed;
|
||||
_popUp.Child = Clock;
|
||||
_popup.PreviewMouseLeftButtonDown += PopupPreviewMouseLeftButtonDown;
|
||||
_popup.Opened += PopupOpened;
|
||||
_popup.Closed += PopupClosed;
|
||||
_popup.Child = Clock;
|
||||
|
||||
if (IsDropDownOpen)
|
||||
{
|
||||
_popUp.IsOpen = true;
|
||||
_popup.IsOpen = true;
|
||||
}
|
||||
|
||||
_dropDownButton.Click += DropDownButton_Click;
|
||||
@@ -450,7 +450,7 @@ namespace HandyControl.Controls
|
||||
|
||||
private void CheckNull()
|
||||
{
|
||||
if (_dropDownButton == null || _popUp == null || _textBox == null)
|
||||
if (_dropDownButton == null || _popup == null || _textBox == null)
|
||||
throw new Exception();
|
||||
}
|
||||
|
||||
@@ -504,7 +504,7 @@ namespace HandyControl.Controls
|
||||
{
|
||||
if ((Keyboard.Modifiers & ModifierKeys.Alt) == ModifierKeys.Alt)
|
||||
{
|
||||
TogglePopUp();
|
||||
TogglePopup();
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -540,7 +540,7 @@ namespace HandyControl.Controls
|
||||
return _isHandlerSuspended != null && _isHandlerSuspended.ContainsKey(property);
|
||||
}
|
||||
|
||||
private void PopUp_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
|
||||
private void PopupPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
|
||||
{
|
||||
if (sender is Popup popup && !popup.StaysOpen)
|
||||
{
|
||||
@@ -553,9 +553,9 @@ namespace HandyControl.Controls
|
||||
|
||||
private void Clock_SelectedTimeChanged(object sender, FunctionEventArgs<DateTime?> e) => SelectedTime = e.Info;
|
||||
|
||||
private void Clock_Confirmed() => TogglePopUp();
|
||||
private void Clock_Confirmed() => TogglePopup();
|
||||
|
||||
private void PopUp_Opened(object sender, EventArgs e)
|
||||
private void PopupOpened(object sender, EventArgs e)
|
||||
{
|
||||
if (!IsDropDownOpen)
|
||||
{
|
||||
@@ -567,7 +567,7 @@ namespace HandyControl.Controls
|
||||
OnClockOpened(new RoutedEventArgs());
|
||||
}
|
||||
|
||||
private void PopUp_Closed(object sender, EventArgs e)
|
||||
private void PopupClosed(object sender, EventArgs e)
|
||||
{
|
||||
if (IsDropDownOpen)
|
||||
{
|
||||
@@ -582,9 +582,9 @@ namespace HandyControl.Controls
|
||||
OnClockClosed(new RoutedEventArgs());
|
||||
}
|
||||
|
||||
private void DropDownButton_Click(object sender, RoutedEventArgs e) => TogglePopUp();
|
||||
private void DropDownButton_Click(object sender, RoutedEventArgs e) => TogglePopup();
|
||||
|
||||
private void TogglePopUp()
|
||||
private void TogglePopup()
|
||||
{
|
||||
if (IsDropDownOpen)
|
||||
{
|
||||
|
||||
@@ -2,22 +2,28 @@
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Controls.Primitives;
|
||||
using System.Windows.Data;
|
||||
using System.Windows.Documents;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media;
|
||||
using HandyControl.Data;
|
||||
|
||||
using HandyControl.Interactivity;
|
||||
|
||||
namespace HandyControl.Controls
|
||||
{
|
||||
[TemplatePart(Name = PreviewContentKey, Type = typeof(ContentControl))]
|
||||
[TemplatePart(Name = TracKKey, Type = typeof(Track))]
|
||||
[TemplatePart(Name = TrackKey, Type = typeof(Track))]
|
||||
[TemplatePart(Name = ThumbKey, Type = typeof(FrameworkElement))]
|
||||
public class PreviewSlider : Slider
|
||||
{
|
||||
private const string PreviewContentKey = "PART_ContentBorder";
|
||||
private AdornerContainer _adorner;
|
||||
|
||||
private const string TracKKey = "PART_Track";
|
||||
private const string TrackKey = "PART_Track";
|
||||
|
||||
private ContentControl _previewContent;
|
||||
private const string ThumbKey = "PART_Thumb";
|
||||
|
||||
private FrameworkElement _previewContent;
|
||||
|
||||
private FrameworkElement _thumb;
|
||||
|
||||
private TranslateTransform _transform;
|
||||
|
||||
@@ -38,13 +44,28 @@ namespace HandyControl.Controls
|
||||
set => SetValue(PreviewContentProperty, value);
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty PreviewPositionProperty = DependencyProperty.Register(
|
||||
"PreviewPosition", typeof(double), typeof(PreviewSlider), new PropertyMetadata(ValueBoxes.Double0Box));
|
||||
public static readonly DependencyProperty PreviewContentOffsetProperty = DependencyProperty.Register(
|
||||
"PreviewContentOffset", typeof(double), typeof(PreviewSlider), new PropertyMetadata(9.0));
|
||||
|
||||
public double PreviewContentOffset
|
||||
{
|
||||
get => (double) GetValue(PreviewContentOffsetProperty);
|
||||
set => SetValue(PreviewContentOffsetProperty, value);
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty PreviewPositionProperty = DependencyProperty.RegisterAttached(
|
||||
"PreviewPosition", typeof(double), typeof(PreviewSlider), new FrameworkPropertyMetadata(ValueBoxes.Double0Box, FrameworkPropertyMetadataOptions.Inherits));
|
||||
|
||||
public static void SetPreviewPosition(DependencyObject element, double value)
|
||||
=> element.SetValue(PreviewPositionProperty, value);
|
||||
|
||||
public static double GetPreviewPosition(DependencyObject element)
|
||||
=> (double) element.GetValue(PreviewPositionProperty);
|
||||
|
||||
public double PreviewPosition
|
||||
{
|
||||
get => (double) GetValue(PreviewPositionProperty);
|
||||
set => SetValue(PreviewPositionProperty, value);
|
||||
get => GetPreviewPosition(_previewContent);
|
||||
set => SetPreviewPosition(_previewContent, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -67,26 +88,97 @@ namespace HandyControl.Controls
|
||||
{
|
||||
base.OnMouseMove(e);
|
||||
|
||||
var p = e.GetPosition(_track);
|
||||
_transform.X = p.X - _previewContent.ActualWidth/2;
|
||||
if (_previewContent == null) return;
|
||||
var p = e.GetPosition(_adorner);
|
||||
|
||||
if (Orientation == Orientation.Horizontal)
|
||||
{
|
||||
var pos = (e.GetPosition(this).X - _thumb.ActualWidth / 2) / _track.ActualWidth * Maximum;
|
||||
if (pos > Maximum || pos < 0)
|
||||
{
|
||||
if (_thumb.IsMouseCaptureWithin)
|
||||
{
|
||||
PreviewPosition = Value;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
_transform.X = p.X - _previewContent.ActualWidth / 2;
|
||||
_transform.Y = TranslatePoint(new Point(), _adorner).Y - _previewContent.ActualHeight - PreviewContentOffset;
|
||||
|
||||
PreviewPosition = _thumb.IsMouseCaptureWithin ? Value : pos;
|
||||
}
|
||||
else
|
||||
{
|
||||
var pos = Maximum - (e.GetPosition(this).Y - _thumb.ActualHeight / 2) / _track.ActualHeight * Maximum;
|
||||
if (pos > Maximum || pos < 0)
|
||||
{
|
||||
if (_thumb.IsMouseCaptureWithin)
|
||||
{
|
||||
PreviewPosition = Value;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
_transform.X = TranslatePoint(new Point(), _adorner).X - _previewContent.ActualWidth - PreviewContentOffset;
|
||||
_transform.Y = p.Y - _previewContent.ActualHeight / 2;
|
||||
|
||||
PreviewPosition = _thumb.IsMouseCaptureWithin ? Value : pos;
|
||||
}
|
||||
|
||||
PreviewPosition = p.X / _track.ActualWidth * Maximum;
|
||||
RaiseEvent(new FunctionEventArgs<double>(PreviewPositionChangedEvent, this)
|
||||
{
|
||||
Info = PreviewPosition
|
||||
});
|
||||
}
|
||||
|
||||
protected override void OnMouseEnter(MouseEventArgs e)
|
||||
{
|
||||
base.OnMouseEnter(e);
|
||||
|
||||
if (_adorner == null)
|
||||
{
|
||||
var layer = AdornerLayer.GetAdornerLayer(this);
|
||||
if (layer == null) return;
|
||||
_adorner = new AdornerContainer(layer)
|
||||
{
|
||||
Child = _previewContent
|
||||
};
|
||||
layer.Add(_adorner);
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnMouseLeave(MouseEventArgs e)
|
||||
{
|
||||
base.OnMouseLeave(e);
|
||||
|
||||
var layer = AdornerLayer.GetAdornerLayer(this);
|
||||
if (layer == null) return;
|
||||
layer.Remove(_adorner);
|
||||
_adorner.Child = null;
|
||||
_adorner = null;
|
||||
}
|
||||
|
||||
public override void OnApplyTemplate()
|
||||
{
|
||||
base.OnApplyTemplate();
|
||||
_previewContent = Template.FindName(PreviewContentKey, this) as ContentControl;
|
||||
_track = Template.FindName(TracKKey, this) as Track;
|
||||
|
||||
var contentControl = new ContentControl
|
||||
{
|
||||
DataContext = this
|
||||
};
|
||||
contentControl.SetBinding(ContentControl.ContentProperty, new Binding(PreviewContentProperty.Name) { Source = this });
|
||||
_previewContent = contentControl;
|
||||
|
||||
_track = Template.FindName(TrackKey, this) as Track;
|
||||
_thumb = Template.FindName(ThumbKey, this) as FrameworkElement;
|
||||
|
||||
if (_previewContent != null)
|
||||
{
|
||||
_transform = new TranslateTransform();
|
||||
_previewContent.RenderTransform = _transform;
|
||||
_previewContent.HorizontalAlignment = HorizontalAlignment.Left;
|
||||
_previewContent.VerticalAlignment = VerticalAlignment.Top;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user