comparison Chronosv2/source/Presentation/Controls/HeaderNotification.xaml.cs @ 10:443821e55f06

Initial cleaned up add from Codeplex files
author stevenh7776 stevenhollidge@hotmail.com
date Tue, 21 Feb 2012 17:25:44 +0700
parents
children
comparison
equal deleted inserted replaced
9:904a9faadf8b 10:443821e55f06
1 using System;
2 using System.Collections.Generic;
3 using System.Windows;
4 using System.Windows.Controls;
5 using System.Windows.Input;
6 using System.Windows.Threading;
7
8 namespace Chronos.Presentation.Controls
9 {
10 [TemplateVisualState(Name = HIDDEN_STATENAME, GroupName = NOTIFICATION_STATEGROUP)]
11 [TemplateVisualState(Name = VISIBLE_STATENAME, GroupName = NOTIFICATION_STATEGROUP)]
12 public partial class HeaderNotification
13 : UserControl
14 {
15 #region · Constants ·
16
17 private const string NOTIFICATION_STATEGROUP = "NotificationStateGroup";
18 private const string HIDDEN_STATENAME = "HiddenState";
19 private const string VISIBLE_STATENAME = "VisibleState";
20 private readonly static TimeSpan TRANSITION_TIMEOUT = TimeSpan.FromMilliseconds(600);
21
22 #endregion
23
24 #region · Dependency Properties ·
25
26 /// <summary>
27 /// Identifies the MessageText dependency property.
28 /// </summary>
29 public static readonly DependencyProperty MessageTextProperty =
30 DependencyProperty.Register("MessageText", typeof(String), typeof(HeaderNotification),
31 new FrameworkPropertyMetadata(String.Empty, FrameworkPropertyMetadataOptions.None, new PropertyChangedCallback(OnMessageTextChanged)));
32
33 #endregion
34
35 #region · Dependency Properties Callback Handlers ·
36
37 private static void OnMessageTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
38 {
39 if (d != null)
40 {
41 if (e.NewValue != null)
42 {
43 HeaderNotification element = d as HeaderNotification;
44
45 element.ShowNotification(e.NewValue as String);
46 }
47 }
48 }
49
50 #endregion
51
52 #region · Fields ·
53
54 private readonly Object syncObject = new Object();
55 private Queue<InteractiveMessage> messages;
56 private InteractiveMessage currentMessage;
57 private DispatcherTimer stateTimer;
58
59 #endregion
60
61 #region · Properties ·
62
63 public string MessageText
64 {
65 get { return (String)base.GetValue(HeaderNotification.MessageTextProperty); }
66 set { base.SetValue(HeaderNotification.MessageTextProperty, value); }
67 }
68
69 #endregion
70
71 #region · Constructors ·
72
73 public HeaderNotification()
74 {
75 InitializeComponent();
76
77 // set up
78 this.messages = new Queue<InteractiveMessage>();
79 this.stateTimer = new DispatcherTimer();
80 this.stateTimer.Interval = TRANSITION_TIMEOUT;
81 this.stateTimer.Tick += new EventHandler(StateTimer_Tick);
82 }
83
84 #endregion
85
86 #region · Methods ·
87
88 public void ShowNotification(string notification)
89 {
90 // basic checks
91 if (String.IsNullOrWhiteSpace(notification))
92 {
93 return;
94 }
95
96 // show or enque message
97 lock (syncObject)
98 {
99 // if no items are queued then show the message, else enque
100 var message = new InteractiveMessage() { Message = notification };
101
102 if (this.messages.Count == 0 && this.currentMessage == null)
103 {
104 ShowMessage(message);
105 }
106 else
107 {
108 messages.Enqueue(message);
109 }
110 }
111 }
112
113 #endregion
114
115 #region · Event Handlers ·
116
117 private void StateTimer_Tick(object sender, EventArgs e)
118 {
119 this.stateTimer.Stop();
120 this.ProcessQueue();
121 }
122
123 private void CloseButton_Click(object sender, RoutedEventArgs e)
124 {
125 // basic check
126 if (stateTimer.IsEnabled)
127 {
128 return;
129 }
130
131 // we stop the timers and start transitioning
132 VisualStateManager.GoToState(this, HIDDEN_STATENAME, true);
133
134 // and transition
135 this.stateTimer.Start();
136 }
137
138 private void Header_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
139 {
140 // basic check
141 if (this.stateTimer.IsEnabled)
142 {
143 return;
144 }
145
146 // we stop the timers and start transitioning
147 VisualStateManager.GoToState(this, HIDDEN_STATENAME, true);
148
149 // and transition
150 this.stateTimer.Start();
151 }
152
153 #endregion
154
155 #region · Helpers ·
156
157 private void ProcessQueue()
158 {
159 lock (syncObject)
160 {
161 if (messages.Count == 0)
162 {
163 currentMessage = null;
164 }
165 else
166 {
167 ShowMessage(messages.Dequeue());
168 }
169 }
170 }
171
172 private void ShowMessage(InteractiveMessage message)
173 {
174 this.HeaderText.Text = message.Message;
175 VisualStateManager.GoToState(this, VISIBLE_STATENAME, true);
176 this.currentMessage = message;
177 }
178
179 #endregion
180
181 #region · Internal Class ·
182
183 class InteractiveMessage
184 {
185 #region · Properties ·
186
187 public string Message
188 {
189 get;
190 set;
191 }
192
193 #endregion
194 }
195
196 #endregion
197 }
198 }