aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Touch/Controls/TouchNotificationBar.cs
blob: b58158fd0050c89ce84ad034860c4659212e5c47 (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
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 System.Windows.Threading;
using Tango.Core.EventArguments;

namespace Tango.Touch.Controls
{
    public class TouchNotificationBar : ContentControl
    {
        private bool _isMouseDown;
        private Point _mouseDownLocation;
        private bool _isMoving;
        private Point _downLocation;
        private TouchDevice _touchDevice;
        private double _last_manipulation_delta;
        private DateTime _last_delta_measure_time;
        private double _last_mouse_y;


        private Border border_notifications;
        private Grid grid_container;

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

        public String ItemExpandedPropertyPath
        {
            get { return (String)GetValue(ItemExpandedPropertyPathProperty); }
            set { SetValue(ItemExpandedPropertyPathProperty, value); }
        }
        public static readonly DependencyProperty ItemExpandedPropertyPathProperty =
            DependencyProperty.Register("ItemExpandedPropertyPath", typeof(String), typeof(TouchNotificationBar), new PropertyMetadata(null));

        public double MinNotificationHeight
        {
            get { return (double)GetValue(MinNotificationHeightProperty); }
            set { SetValue(MinNotificationHeightProperty, value); }
        }
        public static readonly DependencyProperty MinNotificationHeightProperty =
            DependencyProperty.Register("MinNotificationHeight", typeof(double), typeof(TouchNotificationBar), new PropertyMetadata(60.0));

        public double MaxNotificationHeight
        {
            get { return (double)GetValue(MaxNotificationHeightProperty); }
            set { SetValue(MaxNotificationHeightProperty, value); }
        }
        public static readonly DependencyProperty MaxNotificationHeightProperty =
            DependencyProperty.Register("MaxNotificationHeight", typeof(double), typeof(TouchNotificationBar), new PropertyMetadata(100.0));

        public Thickness NotificationPadding
        {
            get { return (Thickness)GetValue(NotificationPaddingProperty); }
            set { SetValue(NotificationPaddingProperty, value); }
        }
        public static readonly DependencyProperty NotificationPaddingProperty =
            DependencyProperty.Register("NotificationPadding", typeof(Thickness), typeof(TouchNotificationBar), new PropertyMetadata(new Thickness(0)));

        public bool HasNotifications
        {
            get { return (bool)GetValue(HasNotificationsProperty); }
            set { SetValue(HasNotificationsProperty, value); }
        }
        public static readonly DependencyProperty HasNotificationsProperty =
            DependencyProperty.Register("HasNotifications", typeof(bool), typeof(TouchNotificationBar), new PropertyMetadata(false, (d, e) => (d as TouchNotificationBar).OnHasNotificationsChanged()));

        public IList Notifications
        {
            get { return (IList)GetValue(NotificationsProperty); }
            set { SetValue(NotificationsProperty, value); }
        }
        public static readonly DependencyProperty NotificationsProperty =
            DependencyProperty.Register("Notifications", typeof(IList), typeof(TouchNotificationBar), new PropertyMetadata(null));

        public DataTemplate NotificationTemplate
        {
            get { return (DataTemplate)GetValue(NotificationTemplateProperty); }
            set { SetValue(NotificationTemplateProperty, value); }
        }
        public static readonly DependencyProperty NotificationTemplateProperty =
            DependencyProperty.Register("NotificationTemplate", typeof(DataTemplate), typeof(TouchNotificationBar), new PropertyMetadata(null));

        public double NotificationHeight
        {
            get { return (double)GetValue(NotificationHeightProperty); }
            set { SetValue(NotificationHeightProperty, value); }
        }
        public static readonly DependencyProperty NotificationHeightProperty =
            DependencyProperty.Register("NotificationHeight", typeof(double), typeof(TouchNotificationBar), new PropertyMetadata(0.0));


        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            border_notifications = GetTemplateChild("PART_BorderNotifications") as Border;
            grid_container = GetTemplateChild("PART_Grid_Container") as Grid;

            //border_notifications.IsManipulationEnabled = true;
            border_notifications.RegisterForPreviewMouseOrTouchDown(GridNotificationMouseDown);
            border_notifications.RegisterForMouseOrTouchMove(GridNotificationMouseMove);
            border_notifications.RegisterForPreviewMouseOrTouchUp(GridNotificationMouseUp);

            border_notifications.SizeChanged += Border_notifications_SizeChanged;
        }

        private void Border_notifications_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            double maxPadding = 150;
            NotificationHeight = (border_notifications.ActualHeight / ActualHeight) * maxPadding;
        }

        private void OnHasNotificationsChanged()
        {
            DoubleAnimation ani = new DoubleAnimation();
            ani.Duration = TimeSpan.FromSeconds(0.2);

            if (HasNotifications)
            {
                ani.To = MinNotificationHeight;
            }
            else
            {
                ani.To = 0;
                CloseNotifications();
            }

            grid_container.BeginAnimation(Grid.MinHeightProperty, ani);
        }

        private void OpenNotifications()
        {
            DoubleAnimation ani = new DoubleAnimation();
            ani.DecelerationRatio = 1;
            ani.Duration = TimeSpan.FromSeconds(0.2);

            if (double.IsNaN(border_notifications.Height))
            {
                border_notifications.Height = border_notifications.ActualHeight;
            }

            border_notifications.MaxHeight = this.ActualHeight;
            ani.To = this.ActualHeight;
            border_notifications.BeginAnimation(Grid.HeightProperty, ani);

            if (ItemExpandedPropertyPath != null)
            {
                foreach (var item in Notifications)
                {
                    item.SetPropertyValueByPath(ItemExpandedPropertyPath, true);
                }
            }
        }

        private void CloseNotifications()
        {
            DoubleAnimation ani = new DoubleAnimation();
            ani.Duration = TimeSpan.FromSeconds(0.2);
            ani.AccelerationRatio = 1;

            ani.From = border_notifications.ActualHeight;
            ani.To = MinNotificationHeight;

            ani.Completed += (_, __) =>
            {
                border_notifications.BorderThickness = new Thickness(0);
                border_notifications.BeginAnimation(Border.HeightProperty, null);
                border_notifications.Height = double.NaN;
            };

            border_notifications.BeginAnimation(Border.MaxHeightProperty, ani);
            border_notifications.BeginAnimation(Border.HeightProperty, ani);

            ThicknessAnimation tAni = new ThicknessAnimation();
            tAni.Duration = TimeSpan.FromSeconds(0.3);
            tAni.To = new Thickness(0);
            border_notifications.BeginAnimation(Border.BorderThicknessProperty, tAni);

            if (ItemExpandedPropertyPath != null)
            {
                foreach (var item in Notifications)
                {
                    item.SetPropertyValueByPath(ItemExpandedPropertyPath, false);
                }
            }
        }

        private void GridNotificationMouseUp(object sender, MouseOrTouchEventArgs e)
        {
            _isMouseDown = false;

            if (_isMoving)
            {
                if (_touchDevice != null)
                {
                    border_notifications.ReleaseTouchCapture(_touchDevice);
                    _touchDevice = null;
                }
                else
                {
                    border_notifications.ReleaseMouseCapture();
                }

                e.Handled = true;
                _isMoving = false;
                DoubleAnimation ani = new DoubleAnimation();
                ani.Duration = TimeSpan.FromSeconds(0.2);

                if ((e.GetPosition(this).Y > ActualHeight / 2 && _last_manipulation_delta > 0) || _last_manipulation_delta > 20)
                {
                    OpenNotifications();
                }
                else if ((e.GetPosition(this).Y < ActualHeight / 2 && _last_manipulation_delta < 0) || _last_manipulation_delta < 20)
                {
                    CloseNotifications();
                }
            }
        }

        private void GridNotificationMouseMove(object sender, MouseOrTouchEventArgs e)
        {
            if (_isMouseDown)
            {
                if (Math.Abs(e.Location.Y - _downLocation.Y) > 5)
                {
                    _isMoving = true;
                }

                if (_isMoving)
                {
                    DateTime curTime = DateTime.Now;

                    if (curTime > _last_delta_measure_time.AddMilliseconds(10))
                    {
                        _last_delta_measure_time = curTime;

                        double delta = e.Location.Y - _last_mouse_y;

                        if (delta != 0)
                        {
                            _last_manipulation_delta = delta;
                        }

                        _last_mouse_y = e.Location.Y;
                    }

                    if (e.TouchDevice != null)
                    {
                        border_notifications.CaptureTouch(e.TouchDevice);
                    }
                    else
                    {
                        Mouse.Capture(border_notifications);
                    }
                    border_notifications.Height = Math.Max(e.Location.Y + _mouseDownLocation.Y, MinNotificationHeight);
                    border_notifications.MaxHeight = Math.Max(e.Location.Y + _mouseDownLocation.Y, MinNotificationHeight);
                    border_notifications.BeginAnimation(Border.HeightProperty, null);
                    border_notifications.BeginAnimation(Border.MaxHeightProperty, null);

                    border_notifications.BeginAnimation(Border.BorderThicknessProperty, null);
                    if (border_notifications.ActualHeight > MinNotificationHeight)
                    {
                        border_notifications.BorderThickness = new Thickness(0, 0, 0, 20);
                    }
                    else
                    {
                        border_notifications.BorderThickness = new Thickness(0);
                    }
                }
            }
        }

        private void GridNotificationMouseDown(object sender, MouseOrTouchEventArgs e)
        {
            _mouseDownLocation = new Point(e.Location.X, border_notifications.MaxHeight - e.Location.Y);
            _downLocation = e.Location;
            _last_mouse_y = _downLocation.Y;
            _last_delta_measure_time = DateTime.Now;
            _last_manipulation_delta = 0;
            _isMouseDown = true;
        }
    }
}