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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
|
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
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.Commands;
using Tango.Core.EventArguments;
using Tango.Core.ExtensionMethods;
namespace Tango.Touch.Controls
{
public class TouchNotificationBar : ContentControl
{
private const double HEIGHT_RATIO = 0.75;
private const double DRAGPATH_HEIGH = 20.0;
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 Border _border_drag;
private Grid _grid_container;
private Grid _grid_mask;
private ItemsControl _items_control;
private Grid _notification_counter_grid;
private LightTouchScrollViewer _scrollViewer;
#region Dep_Properties
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, (d, e) => (d as TouchNotificationBar).OnNotificationsChanged()));
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 CurrentMinHeight
{
get { return (double)GetValue(CurrentMinHeightProperty); }
set { SetValue(CurrentMinHeightProperty, value); }
}
public static readonly DependencyProperty CurrentMinHeightProperty =
DependencyProperty.Register("CurrentMinHeight", typeof(double), typeof(TouchNotificationBar), new PropertyMetadata(0.0));
public bool IsExpanded
{
get { return (bool)GetValue(IsExpandedProperty); }
set { SetValue(IsExpandedProperty, value); }
}
public static readonly DependencyProperty IsExpandedProperty =
DependencyProperty.Register("IsExpanded", typeof(bool), typeof(TouchNotificationBar), new PropertyMetadata(false));
public RelayCommand NotificationPressedCommand
{
get { return (RelayCommand)GetValue(NotificationPressedCommandProperty); }
set { SetValue(NotificationPressedCommandProperty, value); }
}
public static readonly DependencyProperty NotificationPressedCommandProperty =
DependencyProperty.Register("NotificationPressedCommand", typeof(RelayCommand), typeof(TouchNotificationBar), new PropertyMetadata(null));
public Visibility NotificationBarVisibility
{
get { return (Visibility)GetValue(NotificationBarVisibilityProperty); }
set { SetValue(NotificationBarVisibilityProperty, value); }
}
public static readonly DependencyProperty NotificationBarVisibilityProperty =
DependencyProperty.Register("NotificationBarVisibility", typeof(Visibility), typeof(TouchNotificationBar), new PropertyMetadata(Visibility.Visible));
#endregion
#region constructors
static TouchNotificationBar()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(TouchNotificationBar), new FrameworkPropertyMetadata(typeof(TouchNotificationBar)));
}
public TouchNotificationBar()
{
IsExpanded = false;
Loaded += TouchNotificationBar_Loaded;
NotificationPressedCommand = new RelayCommand(() =>
{
if (IsExpanded && !_scrollViewer.IsAfterScrolling)
{
CloseNotifications();
}
});
}
#endregion
#region Override_Base
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
_border_notifications = GetTemplateChild("PART_BorderNotifications") as Border;
_border_drag = GetTemplateChild("PART_BorderDrag") as Border;
_grid_container = GetTemplateChild("PART_Grid_Container") as Grid;
_grid_mask = GetTemplateChild("PART_GridMask") as Grid;
_items_control = GetTemplateChild("PART_ItemsControl") as ItemsControl;
_notification_counter_grid = GetTemplateChild("PART_NotificationCounterGrid") as Grid;
_scrollViewer = GetTemplateChild("PART_ScrollViewer") as LightTouchScrollViewer;
_border_drag.RegisterForPreviewMouseOrTouchDown(GridNotificationMouseDown);
_border_drag.RegisterForMouseOrTouchMove(GridNotificationMouseMove);
_border_drag.RegisterForPreviewMouseOrTouchUp(GridNotificationMouseUp);
_border_drag.SizeChanged += Drag_Border_SizeChanged;
}
#endregion
private FrameworkElement GetLastNotification()
{
if (_items_control == null) return null;
return _items_control.ItemContainerGenerator.ContainerFromIndex(0) as FrameworkElement;
}
private void TouchNotificationBar_Loaded(object sender, RoutedEventArgs e)
{
_border_drag.MaxHeight = ActualHeight;
}
private void Drag_Border_SizeChanged(object sender, SizeChangedEventArgs e)
{
_grid_mask.Opacity = _border_drag.ActualHeight / ActualHeight;
if (e.NewSize.Height > CurrentMinHeight && CurrentMinHeight > 0)
{
_notification_counter_grid.Visibility = Visibility.Hidden;
}
else if (Notifications.Count > 1 && CurrentMinHeight > 0)
{
_notification_counter_grid.Visibility = Visibility.Visible;
}
}
/// <summary>
/// Called when has notifications items or all items removed
/// </summary>
private void OnHasNotificationsChanged()
{
if (HasNotifications)
{
UpdateMinHeightByDispalyedItem();
}
else
{
CurrentMinHeight = 0;
if (IsExpanded)
{
CloseNotifications();
}
_border_notifications.Height = CurrentMinHeight;
_grid_container.MinHeight = 0;
_grid_container.Height = 0;
}
}
private async void UpdateMinHeightByDispalyedItem()
{
var last = GetLastNotification();
if (last != null)
{
last.RegisterForLoadedOrNow((x, y) =>
{
CurrentMinHeight = last.ActualHeight;
_border_notifications.Height = last.ActualHeight;
_grid_container.MinHeight = last.ActualHeight;
_grid_container.Height = CurrentMinHeight;
});
}
else if (Notifications.Count > 0)
{
await Task.Delay(500);
UpdateMinHeightByDispalyedItem();
}
}
/// <summary>
/// Callback function on Notifications property
/// </summary>
private void OnNotificationsChanged()
{
if (Notifications is INotifyCollectionChanged)
{
(Notifications as INotifyCollectionChanged).CollectionChanged += (_, e) =>
{
this.BeginInvoke(() =>
{
if (Notifications.Count > 0)
{
UpdateMinHeightByDispalyedItem();
}
if (IsExpanded == false && Notifications.Count > 1)
{
_notification_counter_grid.Visibility = Visibility.Visible;
}
else
{
_notification_counter_grid.Visibility = Visibility.Hidden;
}
});
};
}
}
#region Mouse handlers
private void OpenNotifications()
{
_border_drag.StartDoubleAnimation(Border.HeightProperty, TimeSpan.FromSeconds(0.2), _border_drag.MaxHeight, null, null, 1, () =>
{
IsExpanded = true;
});
_border_notifications.StartDoubleAnimation(Border.HeightProperty, TimeSpan.FromSeconds(0.2), ActualHeight * HEIGHT_RATIO, null, null, 1);
}
private void CloseNotifications()
{
_border_notifications.StartDoubleAnimation(Border.HeightProperty, TimeSpan.FromSeconds(0.2), CurrentMinHeight, null, 1, null, () =>
{
_border_notifications.BeginAnimation(Border.HeightProperty, null);
_border_notifications.Height = CurrentMinHeight;
});
_border_drag.StartDoubleAnimation(Border.HeightProperty, TimeSpan.FromSeconds(0.2), CurrentMinHeight, null, 1, null, () =>
{
_border_drag.BeginAnimation(Border.HeightProperty, null);
_border_drag.Height = double.NaN;
});
ThicknessAnimation tAni = new ThicknessAnimation();
tAni.Duration = TimeSpan.FromSeconds(0.3);
tAni.To = new Thickness(0);
_border_notifications.BeginAnimation(Border.BorderThicknessProperty, tAni);
IsExpanded = false;
Task.Delay(400).ContinueWith((x) =>
{
Dispatcher.BeginInvoke(new Action(() =>
{
_scrollViewer.ScrollToTop();
}));
});
}
private void GridNotificationMouseUp(object sender, MouseOrTouchEventArgs e)
{
if (_isMouseDown)
{
_isMouseDown = false;
if (_isMoving)
{
if (_touchDevice != null)
{
_border_drag.ReleaseTouchCapture(_touchDevice);
_touchDevice = null;
}
else
{
_border_drag.ReleaseMouseCapture();
}
e.Handled = true;
_isMoving = false;
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();
}
}
_scrollViewer.DisableScrolling = false;
}
}
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_drag.CaptureTouch(e.TouchDevice);
}
else
{
Mouse.Capture(_border_drag);
}
_border_drag.Height = Math.Max(e.Location.Y + _mouseDownLocation.Y, CurrentMinHeight);
_border_notifications.Height = Math.Min(_border_drag.Height * HEIGHT_RATIO, ActualHeight * HEIGHT_RATIO);
_border_drag.BeginAnimation(Border.HeightProperty, null);
_border_notifications.BeginAnimation(Border.HeightProperty, null);
_border_notifications.BeginAnimation(Border.BorderThicknessProperty, null);
if (_border_notifications.ActualHeight > CurrentMinHeight)
{
_border_notifications.BorderThickness = new Thickness(0, 0, 0, DRAGPATH_HEIGH);
}
else
{
_border_notifications.BorderThickness = new Thickness(0);
}
}
}
}
private void GridNotificationMouseDown(object sender, MouseOrTouchEventArgs e)
{
if (!IsExpanded || e.OriginalSource == _border_drag || e.OriginalSource == _border_notifications)
{
_mouseDownLocation = new Point(e.Location.X, _border_drag.ActualHeight - e.Location.Y);
_downLocation = e.Location;
_last_mouse_y = _downLocation.Y;
_last_delta_measure_time = DateTime.Now;
_last_manipulation_delta = 0;
_scrollViewer.DisableScrolling = true;
_isMouseDown = true;
}
}
#endregion
}
}
|