diff options
| author | Mirta <mirta@twine-s.com> | 2020-12-30 16:39:52 +0200 |
|---|---|---|
| committer | Mirta <mirta@twine-s.com> | 2020-12-30 16:39:52 +0200 |
| commit | 00a491d93733d4625ad329b2ba8237f445364b3f (patch) | |
| tree | 4b24c6fa78d7648f4bb7cefafa464bb0b063fec4 /Software/Visual_Studio/Tango.SharedUI/Controls/NavigationControl.cs | |
| parent | 124ad4150f80c6846fdee41dbbda9848c105f6e5 (diff) | |
| download | Tango-00a491d93733d4625ad329b2ba8237f445364b3f.tar.gz Tango-00a491d93733d4625ad329b2ba8237f445364b3f.zip | |
merge
Diffstat (limited to 'Software/Visual_Studio/Tango.SharedUI/Controls/NavigationControl.cs')
| -rw-r--r-- | Software/Visual_Studio/Tango.SharedUI/Controls/NavigationControl.cs | 366 |
1 files changed, 111 insertions, 255 deletions
diff --git a/Software/Visual_Studio/Tango.SharedUI/Controls/NavigationControl.cs b/Software/Visual_Studio/Tango.SharedUI/Controls/NavigationControl.cs index cefdbbfd6..b3869daff 100644 --- a/Software/Visual_Studio/Tango.SharedUI/Controls/NavigationControl.cs +++ b/Software/Visual_Studio/Tango.SharedUI/Controls/NavigationControl.cs @@ -1,10 +1,8 @@ using System; using System.Collections.Generic; using System.Collections.ObjectModel; -using System.Diagnostics; using System.Linq; using System.Text; -using System.Threading; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; @@ -13,7 +11,6 @@ using System.Windows.Markup; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Threading; -using Tango.Core; using Tango.SharedUI.Helpers; namespace Tango.SharedUI.Controls @@ -21,17 +18,7 @@ namespace Tango.SharedUI.Controls [ContentProperty(nameof(Elements))] public class NavigationControl : UserControl { - private event Action NavigationCompleted; - public event EventHandler<FrameworkElement> SelectedElementChanged; - private Thread _navigationThread; - private bool _preventSelectedObjectNavigation; - private ProducerConsumerQueue<NavigationQueueItem> _navigationQueue; - - private class NavigationQueueItem - { - public NavigationElement FromElement { get; set; } - public NavigationElement ToElement { get; set; } - } + private Action _onCompleted; #region Transition Types @@ -186,24 +173,6 @@ namespace Tango.SharedUI.Controls #region Properties - - - public object SelectedObject - { - get { return (object)GetValue(SelectedObjectProperty); } - set { SetValue(SelectedObjectProperty, value); } - } - public static readonly DependencyProperty SelectedObjectProperty = - DependencyProperty.Register("SelectedObject", typeof(object), typeof(NavigationControl), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, (d, e) => (d as NavigationControl).OnSelectedObjectChanged())); - - private void OnSelectedObjectChanged() - { - if (SelectedObject != null && !_preventSelectedObjectNavigation) - { - NavigateTo(SelectedObject); - } - } - public ObservableCollection<FrameworkElement> Elements { get { return (ObservableCollection<FrameworkElement>)GetValue(ElementsProperty); } @@ -218,7 +187,7 @@ namespace Tango.SharedUI.Controls set { SetValue(SelectedElementProperty, value); } } public static readonly DependencyProperty SelectedElementProperty = - DependencyProperty.Register("SelectedElement", typeof(FrameworkElement), typeof(NavigationControl), new FrameworkPropertyMetadata(null, (d, e) => (d as NavigationControl).OnSelectedElementChanged(e.OldValue as FrameworkElement, e.NewValue as FrameworkElement))); + DependencyProperty.Register("SelectedElement", typeof(FrameworkElement), typeof(NavigationControl), new PropertyMetadata(null, (d, e) => (d as NavigationControl).OnSelectedElementChanged(e.OldValue as FrameworkElement, e.NewValue as FrameworkElement))); public TransitionTypes TransitionType { @@ -252,13 +221,7 @@ namespace Tango.SharedUI.Controls public static readonly DependencyProperty KeepElementsAttachedProperty = DependencyProperty.Register("KeepElementsAttached", typeof(bool), typeof(NavigationControl), new PropertyMetadata(false)); - public bool GalleryMode - { - get { return (bool)GetValue(GalleryModeProperty); } - set { SetValue(GalleryModeProperty, value); } - } - public static readonly DependencyProperty GalleryModeProperty = - DependencyProperty.Register("GalleryMode", typeof(bool), typeof(NavigationControl), new PropertyMetadata(false)); + public int SelectedIndex { @@ -316,9 +279,7 @@ namespace Tango.SharedUI.Controls /// <returns></returns> public static String GetNavigationName(FrameworkElement element) { - var name = element.GetValue(NavigationName).ToStringSafe(); - if (String.IsNullOrEmpty(name)) name = element.GetType().Name; - return name; + return element.GetValue(NavigationName).ToStringSafe(); } #endregion @@ -334,11 +295,6 @@ namespace Tango.SharedUI.Controls Content = _grid; Elements = new ObservableCollection<FrameworkElement>(); Loaded += NavigationControl_Loaded; - - _navigationQueue = new ProducerConsumerQueue<NavigationQueueItem>(); - _navigationThread = new Thread(NavigationThread); - _navigationThread.IsBackground = true; - _navigationThread.Start(); } #endregion @@ -391,8 +347,6 @@ namespace Tango.SharedUI.Controls } else { - Elements.ToList().ForEach(x => x.FocusVisualStyle = null); - var toRemove = _grid.Children.OfType<NavigationElement>().ToList().Where(x => !Elements.Contains(x.Element)).ToList(); var toAdd = Elements.Where(x => !_grid.Children.OfType<NavigationElement>().Select(y => y.Element).ToList().Contains(x)).ToList(); @@ -454,181 +408,135 @@ namespace Tango.SharedUI.Controls private void Navigate(NavigationElement fromElement, NavigationElement toElement) { - _navigationQueue.BlockEnqueue(new NavigationQueueItem() + if (toElement == null || toElement == fromElement) { - FromElement = fromElement, - ToElement = toElement, - }); - } + _onCompleted?.Invoke(); + } - private void NavigationThread() - { - try + if (fromElement != null) { - while (true) - { - var item = _navigationQueue.BlockDequeue(); - - var navigationCompleted = false; - - NavigationElement fromElement = item.FromElement; - NavigationElement toElement = item.ToElement; - - Dispatcher.BeginInvoke(new Action(() => - { - if (toElement == null || toElement == fromElement) - { - //navigationCompleted = true; - Debug.WriteLine("NavigationControl: THIS MIGHT CAUSE PROBLEMS !!"); - } - - if (fromElement != null) - { - DoubleAnimation toAnimation = new DoubleAnimation(); - toAnimation.Duration = TransitionDuration; - - DoubleAnimation fromAnimation = new DoubleAnimation(); - fromAnimation.Duration = TransitionDuration; - - int fromIndex = Elements.IndexOf(fromElement.Element); - int toIndex = Elements.IndexOf(toElement.Element); - - fromElement.Reset(); - toElement.Reset(); + DoubleAnimation toAnimation = new DoubleAnimation(); + toAnimation.Duration = TransitionDuration; - fromAnimation.Completed += (_, __) => - { - fromElement.Deactivate(!KeepElementsAttached); - }; + DoubleAnimation fromAnimation = new DoubleAnimation(); + fromAnimation.Duration = TransitionDuration; - bool completed = false; - - toAnimation.Completed += (_, __) => - { - if (!completed) - { - completed = true; - - INavigationView fromNavigationView = fromElement.Element as INavigationView; - INavigationView toNavigationView = toElement.Element as INavigationView; - - if (fromNavigationView != null) - { - fromNavigationView.OnNavigatedFrom(); - } - if (toNavigationView != null) - { - toNavigationView.OnNavigatedTo(); - } + int fromIndex = Elements.IndexOf(fromElement.Element); + int toIndex = Elements.IndexOf(toElement.Element); - INavigationViewModel fromVM = fromElement.Element.DataContext as INavigationViewModel; - INavigationViewModel toVM = toElement.Element.DataContext as INavigationViewModel; + fromElement.Reset(); + toElement.Reset(); - if (fromVM != null && fromVM != toVM) fromVM.OnNavigatedFrom(); - if (toVM != null) toVM.OnNavigatedTo(); + fromAnimation.Completed += (_, __) => + { + fromElement.Deactivate(!KeepElementsAttached); + }; - navigationCompleted = true; - } - }; + bool completed = false; - switch (TransitionType) - { - case TransitionTypes.Fade: - fromAnimation.From = 1; - fromAnimation.To = 0; - - toAnimation.From = 0; - toAnimation.To = 1; + toAnimation.Completed += (_, __) => + { + if (!completed) + { + completed = true; - fromElement.AnimateOpacity(fromAnimation); - toElement.AnimateOpacity(toAnimation); + INavigationView fromNavigationView = fromElement.Element as INavigationView; + INavigationView toNavigationView = toElement.Element as INavigationView; - break; - case TransitionTypes.Zoom: - fromAnimation.From = 1; - fromAnimation.To = 0; + if (fromNavigationView != null) + { + fromNavigationView.OnNavigatedFrom(); + } + if (toNavigationView != null) + { + toNavigationView.OnNavigatedTo(); + } - toAnimation.From = 0; - toAnimation.To = 1; + INavigationViewModel fromVM = fromElement.Element.DataContext as INavigationViewModel; + INavigationViewModel toVM = toElement.Element.DataContext as INavigationViewModel; - fromElement.AnimateScale(fromAnimation); - toElement.AnimateScale(toAnimation); + if (fromVM != null && fromVM != toVM) fromVM.OnNavigatedFrom(); + if (toVM != null) toVM.OnNavigatedTo(); - break; - case TransitionTypes.Slide: + _onCompleted?.Invoke(); + } + }; - if (toIndex > fromIndex || GalleryMode) - { - fromAnimation.From = 0; - fromAnimation.To = -ActualWidth; + switch (TransitionType) + { + case TransitionTypes.Fade: + fromAnimation.From = 1; + fromAnimation.To = 0; - toAnimation.From = ActualWidth; - toAnimation.To = 0; - } - else - { - fromAnimation.From = 0; - fromAnimation.To = ActualWidth; + toAnimation.From = 0; + toAnimation.To = 1; - toAnimation.From = -ActualWidth; - toAnimation.To = 0; - } + fromElement.AnimateOpacity(fromAnimation); + toElement.AnimateOpacity(toAnimation); - fromElement.AnimateTranslateX(fromAnimation); - toElement.AnimateTranslateX(toAnimation); + break; + case TransitionTypes.Zoom: + fromAnimation.From = 1; + fromAnimation.To = 0; - break; - } + toAnimation.From = 0; + toAnimation.To = 1; - if (TransitionAlwaysFades && TransitionType != TransitionTypes.Fade) - { - DoubleAnimation fromFadeAnimation = new DoubleAnimation(); - fromFadeAnimation.From = 1; - fromFadeAnimation.To = 0; - fromFadeAnimation.Duration = TransitionDuration; + fromElement.AnimateScale(fromAnimation); + toElement.AnimateScale(toAnimation); - DoubleAnimation toFadeAnimation = new DoubleAnimation(); - toFadeAnimation.From = 0; - toFadeAnimation.To = 1; - toFadeAnimation.Duration = TransitionDuration; + break; + case TransitionTypes.Slide: - fromElement.AnimateOpacity(fromFadeAnimation); - toElement.AnimateOpacity(toFadeAnimation); - } + if (toIndex > fromIndex) + { + fromAnimation.From = 0; + fromAnimation.To = -ActualWidth; - fromElement.Activate(); - toElement.Activate(); + toAnimation.From = ActualWidth; + toAnimation.To = 0; } else { - toElement.Activate(); - toElement.Reset(); - - INavigationViewModel toVM = toElement.Element.DataContext as INavigationViewModel; - if (toVM != null) toVM.OnNavigatedTo(); + fromAnimation.From = 0; + fromAnimation.To = ActualWidth; - navigationCompleted = true; + toAnimation.From = -ActualWidth; + toAnimation.To = 0; } - })); - for (int i = 0; i < 30; i++) - { - if (navigationCompleted) - { - break; - } - Thread.Sleep(100); - } + fromElement.AnimateTranslateX(fromAnimation); + toElement.AnimateTranslateX(toAnimation); - Dispatcher.BeginInvoke(new Action(() => - { - NavigationCompleted?.Invoke(); - })); + break; } + + if (TransitionAlwaysFades && TransitionType != TransitionTypes.Fade) + { + DoubleAnimation fromFadeAnimation = new DoubleAnimation(); + fromFadeAnimation.From = 1; + fromFadeAnimation.To = 0; + fromFadeAnimation.Duration = TransitionDuration; + + DoubleAnimation toFadeAnimation = new DoubleAnimation(); + toFadeAnimation.From = 0; + toFadeAnimation.To = 1; + toFadeAnimation.Duration = TransitionDuration; + + fromElement.AnimateOpacity(fromFadeAnimation); + toElement.AnimateOpacity(toFadeAnimation); + } + + fromElement.Activate(); + toElement.Activate(); } - catch + else { - + toElement.Activate(); + toElement.Reset(); + + INavigationViewModel toVM = toElement.Element.DataContext as INavigationViewModel; + if (toVM != null) toVM.OnNavigatedTo(); } } @@ -639,14 +547,6 @@ namespace Tango.SharedUI.Controls protected virtual void OnSelectedElementChanged(FrameworkElement fromElement, FrameworkElement toElement) { Navigate(GetElementContainer(fromElement), GetElementContainer(toElement)); - SelectedElementChanged?.Invoke(this, toElement); - - if (toElement != null) - { - _preventSelectedObjectNavigation = true; - SelectedObject = GetNavigationName(toElement); - _preventSelectedObjectNavigation = false; - } } #endregion @@ -668,67 +568,27 @@ namespace Tango.SharedUI.Controls return NavigateTo(navigationName, null); } - public FrameworkElement NavigateTo(Object obj) - { - return NavigateTo(obj.ToString(), null); - } - public FrameworkElement NavigateTo(String navigationName, Action onCompleted = null) { - Action completed = null; - - completed = () => - { - try - { - onCompleted?.Invoke(); - } - catch { } - NavigationCompleted -= completed; - }; - - NavigationCompleted += completed; + _onCompleted = onCompleted; var element = Elements.SingleOrDefault(x => GetNavigationName(x) == navigationName || x.GetType().Name == navigationName); - Task.Factory.StartNew(() => + if (element != null) { - Thread.Sleep(10); - - Dispatcher.BeginInvoke(new Action(async () => + if (SelectedElement == element) { - if (element != null) - { - if (SelectedElement == element) - { - await Task.Delay((int)TransitionDuration.TimeSpan.TotalMilliseconds * 2); - NavigationCompleted?.Invoke(); - } - else - { - SelectedElement = element; - } - } - })); - }); + _onCompleted?.Invoke(); + } + else + { + SelectedElement = element; + } + } return element; } - public Task<FrameworkElement> NavigateToAsync(String navigationName) - { - TaskCompletionSource<FrameworkElement> source = new TaskCompletionSource<FrameworkElement>(); - - FrameworkElement view = null; - - view = NavigateTo(navigationName, () => - { - source.SetResult(view); - }); - - return source.Task; - } - public FrameworkElement GetElement(String navigationName) { return Elements.SingleOrDefault(x => GetNavigationName(x) == navigationName || x.GetType().Name == navigationName); @@ -741,11 +601,6 @@ namespace Tango.SharedUI.Controls return element; } - public String GetSelectedElementNavigationName() - { - return GetNavigationName(SelectedElement); - } - /// <remarks> /// This method needs to be called in order for // the element to print visibly at the correct size. @@ -768,6 +623,7 @@ namespace Tango.SharedUI.Controls catch { } } } + #endregion } } |
