aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Touch/Controls/TouchNavigationLinks.cs
blob: 5fb4496e07798e3b6a6143f69663896e9707c6ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Tango.SharedUI.Helpers;

namespace Tango.Touch.Controls
{
    [TemplatePart(Name = PART_Rectangle, Type = typeof(Rectangle))]
    public class TouchNavigationLinks : ListBox
    {
        private const string PART_Rectangle = "PART_Rectangle";
        private Line _line;
        private Object _lastSelectedItem;
        
        public int LineThickness
        {
            get { return (int)GetValue(LineThicknessProperty); }
            set { SetValue(LineThicknessProperty, value); }
        }

        // Using a DependencyProperty as the backing store for LineThickness.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty LineThicknessProperty =
            DependencyProperty.Register("LineThickness", typeof(int), typeof(TouchNavigationLinks), new PropertyMetadata(3));

        
        static TouchNavigationLinks()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(TouchNavigationLinks), new FrameworkPropertyMetadata(typeof(TouchNavigationLinks)));
        }

        public TouchNavigationLinks()
        {
            SelectionChanged += NavigationLinks_SelectionChanged;
            Loaded += (_, __) => MoveLineToSelectedItem();
        }

        public override void OnApplyTemplate()
        {
            _line = GetTemplateChild(PART_Rectangle) as Line;
        }

        private void NavigationLinks_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            MoveLineToSelectedItem();
        }

        private void MoveLineToSelectedItem()
        {
            if (SelectedItem != null )
            {
                if(SelectedItem is UIElement && ((UIElement)SelectedItem).IsEnabled == false)
                    return;
                var container = ItemContainerGenerator.ContainerFromItem(SelectedItem) as FrameworkElement;
                
                ContentPresenter presenter = container.FindChild<ContentPresenter>().FindChild<ContentPresenter>();
                
                if (presenter != null)
                {
                    Point relativePoint = presenter.TransformToAncestor(this).Transform(new Point(0, 0));
                    double offset = this.Padding.Left ;
                    double width = presenter.ActualWidth;

                    if (_lastSelectedItem != null && Items.IndexOf(SelectedItem) > Items.IndexOf(_lastSelectedItem))
                    {
                        DoubleAnimation aniX2 = new DoubleAnimation();
                        aniX2.To = relativePoint.X + width - offset;
                        aniX2.Duration = TimeSpan.FromSeconds(0.2);
                        _line.BeginAnimation(Line.X2Property, aniX2);

                        DoubleAnimation aniX1 = new DoubleAnimation();
                        aniX1.To = relativePoint.X - offset;
                        aniX1.BeginTime = TimeSpan.FromSeconds(0.2);
                        aniX1.Duration = TimeSpan.FromSeconds(0.1);
                        _line.BeginAnimation(Line.X1Property, aniX1);
                    }
                    else
                    {
                        DoubleAnimation aniX1 = new DoubleAnimation();
                        aniX1.To = relativePoint.X - offset;
                        aniX1.Duration = TimeSpan.FromSeconds(0.2);
                        _line.BeginAnimation(Line.X1Property, aniX1);

                        DoubleAnimation aniX2 = new DoubleAnimation();
                        aniX2.To = relativePoint.X + width - offset;
                        aniX2.BeginTime = TimeSpan.FromSeconds(0.2);
                        aniX2.Duration = TimeSpan.FromSeconds(0.1);
                        _line.BeginAnimation(Line.X2Property, aniX2);
                    }
                }
                _lastSelectedItem = SelectedItem;
            }
          
            _lastSelectedItem = SelectedItem;
        }
    }
}