From 47a986270df6acb1df2103f7d43155f6e86c1747 Mon Sep 17 00:00:00 2001 From: NaBian <836904362@qq.com> Date: Fri, 15 Nov 2019 01:17:37 +0800 Subject: [PATCH] refactor PreviewSlider --- .../Controls/PreviewSliderDemoCtl.xaml | 17 +- .../Controls/Input/DateTimePicker.cs | 44 +- .../Controls/Input/TimePicker.cs | 44 +- .../Controls/Slider/PreviewSlider.cs | 122 +++- .../Themes/Basic/Colors/Colors.xaml | 26 +- .../Themes/Styles/Base/SliderBaseStyle.xaml | 675 ++++++++++-------- 6 files changed, 535 insertions(+), 393 deletions(-) diff --git a/src/Shared/HandyControlDemo_Shared/UserControl/Controls/PreviewSliderDemoCtl.xaml b/src/Shared/HandyControlDemo_Shared/UserControl/Controls/PreviewSliderDemoCtl.xaml index 8fb39b03..5044f9cf 100644 --- a/src/Shared/HandyControlDemo_Shared/UserControl/Controls/PreviewSliderDemoCtl.xaml +++ b/src/Shared/HandyControlDemo_Shared/UserControl/Controls/PreviewSliderDemoCtl.xaml @@ -4,10 +4,17 @@ xmlns:hc="https://handyorg.github.io/handycontrol" Background="{DynamicResource RegionBrush}"> - - - - + + + + + + + + + + diff --git a/src/Shared/HandyControl_Shared/Controls/Input/DateTimePicker.cs b/src/Shared/HandyControl_Shared/Controls/Input/DateTimePicker.cs index 843a7d3b..17daef50 100644 --- a/src/Shared/HandyControl_Shared/Controls/Input/DateTimePicker.cs +++ b/src/Shared/HandyControl_Shared/Controls/Input/DateTimePicker.cs @@ -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 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) { diff --git a/src/Shared/HandyControl_Shared/Controls/Input/TimePicker.cs b/src/Shared/HandyControl_Shared/Controls/Input/TimePicker.cs index d3679040..4a6b84e8 100644 --- a/src/Shared/HandyControl_Shared/Controls/Input/TimePicker.cs +++ b/src/Shared/HandyControl_Shared/Controls/Input/TimePicker.cs @@ -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 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) { diff --git a/src/Shared/HandyControl_Shared/Controls/Slider/PreviewSlider.cs b/src/Shared/HandyControl_Shared/Controls/Slider/PreviewSlider.cs index c5efece0..e47a6416 100644 --- a/src/Shared/HandyControl_Shared/Controls/Slider/PreviewSlider.cs +++ b/src/Shared/HandyControl_Shared/Controls/Slider/PreviewSlider.cs @@ -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); } /// @@ -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(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; } } } diff --git a/src/Shared/HandyControl_Shared/Themes/Basic/Colors/Colors.xaml b/src/Shared/HandyControl_Shared/Themes/Basic/Colors/Colors.xaml index 8b35c0c5..94c75909 100644 --- a/src/Shared/HandyControl_Shared/Themes/Basic/Colors/Colors.xaml +++ b/src/Shared/HandyControl_Shared/Themes/Basic/Colors/Colors.xaml @@ -2,20 +2,20 @@ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:system="clr-namespace:System;assembly=mscorlib"> - #73a5ff - #5477f5 + #326cf3 + #326cf3 - #ff8792 - #ff4c9f + #db3340 + #db3340 - #ffd180 - #ff9100 + #e9af20 + #e9af20 - #4dd0e1 + #00bcd4 #00bcd4 - #A5D6A7 - #4CAF50 + #2db84d + #2db84d #212121 #757575 @@ -29,14 +29,14 @@ #ffffff #eeeeee White - #73a5ff - #5477f5 + #326cf3 + #326cf3 White #f5f5f5 - #ff5722 - #d43f3a + #f8491e + #f8491e #20000000 #40000000 diff --git a/src/Shared/HandyControl_Shared/Themes/Styles/Base/SliderBaseStyle.xaml b/src/Shared/HandyControl_Shared/Themes/Styles/Base/SliderBaseStyle.xaml index aac15bce..9d71bc2a 100644 --- a/src/Shared/HandyControl_Shared/Themes/Styles/Base/SliderBaseStyle.xaml +++ b/src/Shared/HandyControl_Shared/Themes/Styles/Base/SliderBaseStyle.xaml @@ -9,322 +9,8 @@ - - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -922,4 +963,6 @@ + + \ No newline at end of file