comparison Chronosv2/source/Presentation/Windows/VirtualDesktop.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 /*
2 The MIT License
3
4 Copyright (c) 2009-2010. Carlos Guzmán Álvarez. http://chronoswpf.codeplex.com/
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 THE SOFTWARE.
23 */
24
25 using System;
26 using System.Collections.ObjectModel;
27 using System.Linq;
28 using System.Windows;
29 using System.Windows.Threading;
30 using Chronos.Extensions.Windows;
31 using Chronos.Presentation.Core.ViewModel;
32 using Chronos.Presentation.Core.VirtualDesktops;
33 using Chronos.Presentation.Core.Windows;
34 using Chronos.Presentation.Windows.Controls;
35 using NLog;
36 using nRoute.Components.Messaging;
37
38 namespace Chronos.Presentation.Windows
39 {
40 /// <summary>
41 /// Virtual desktop class
42 /// </summary>
43 public sealed class VirtualDesktop
44 : DispatcherObject, IVirtualDesktop
45 {
46 #region · Logger ·
47
48 private static Logger Logger = LogManager.GetCurrentClassLogger();
49
50 #endregion
51
52 #region · Fields ·
53
54 private Desktop desktop;
55 private ObservableCollection<INavigationViewModel> activeWindows;
56 private ReadOnlyObservableCollection<INavigationViewModel> activeWindowsWrapper;
57 private bool hasBeenActivated;
58
59 #endregion
60
61 #region · Properties ·
62
63 public Guid Id
64 {
65 get
66 {
67 if (this.desktop != null)
68 {
69 return this.desktop.Id;
70 }
71
72 return Guid.Empty;
73 }
74 }
75
76 /// <summary>
77 /// Gets the list of active Windows
78 /// </summary>
79 public ReadOnlyObservableCollection<INavigationViewModel> ActiveWindows
80 {
81 get
82 {
83 if (this.activeWindowsWrapper == null)
84 {
85 this.activeWindowsWrapper = new ReadOnlyObservableCollection<INavigationViewModel>(this.Windows);
86 }
87
88 return this.activeWindowsWrapper;
89 }
90 }
91
92 #endregion
93
94 #region · Private Properties ·
95
96 private ObservableCollection<INavigationViewModel> Windows
97 {
98 get
99 {
100 if (this.activeWindows == null)
101 {
102 this.activeWindows = new ObservableCollection<INavigationViewModel>();
103 }
104
105 return this.activeWindows;
106 }
107 }
108
109 #endregion
110
111 #region · Constructors ·
112
113 /// <summary>
114 /// Initializes a new instance of <see cref="VirtualDesktop"/> class
115 /// </summary>
116 internal VirtualDesktop(Desktop desktop)
117 {
118 this.desktop = desktop;
119 }
120
121 #endregion
122
123 #region · Desktop Actions ·
124
125 /// <summary>
126 /// Activates the desktop instance
127 /// </summary>
128 public void Activate()
129 {
130 this.InvokeAsynchronouslyInBackground(
131 () =>
132 {
133 if (!this.hasBeenActivated)
134 {
135 this.Load();
136 this.hasBeenActivated = true;
137
138 this.desktop.Activate();
139 }
140
141 this.desktop.Visibility = Visibility.Visible;
142
143 Channel<ActiveDesktopChangedInfo>.Public.OnNext(new ActiveDesktopChangedInfo(this), true);
144 });
145 }
146
147 /// <summary>
148 /// Deactivates the desktop instance
149 /// </summary>
150 public void Deactivate()
151 {
152 this.Invoke(
153 () =>
154 {
155 this.desktop.Visibility = Visibility.Hidden;
156 });
157 }
158
159 /// <summary>
160 /// Shows the desktop
161 /// </summary>
162 public void ShowDesktop()
163 {
164 this.InvokeAsynchronouslyInBackground
165 (
166 () =>
167 {
168 this.desktop.Children
169 .OfType<WindowElement>()
170 .ToList()
171 .ForEach(window => window.WindowState = WindowState.Minimized);
172 }
173 );
174 }
175
176 /// <summary>
177 /// Saves desktop contents to disk
178 /// </summary>
179 public void Save()
180 {
181 this.Invoke(
182 () =>
183 {
184 DesktopSerializer.Save(this.desktop, this.GetXamlFilename());
185 });
186 }
187
188 #endregion
189
190 #region · Window Methods ·
191
192 /// <summary>
193 /// Shows the given window instance
194 /// </summary>
195 public void Show(IWindow window)
196 {
197 this.InvokeAsynchronouslyInBackground
198 (
199 () =>
200 {
201 WindowElement element = window as WindowElement;
202
203 this.desktop.AddElement(element);
204
205 element.Show();
206
207 this.InvokeAsynchronouslyInBackground
208 (
209 () =>
210 {
211 if (element.DataContext is INavigationViewModel)
212 {
213 INavigationViewModel windowViewModel = element.DataContext as INavigationViewModel;
214
215 ((IClosableViewModel)windowViewModel).Title = element.Title;
216
217 this.Windows.Add(windowViewModel);
218 }
219 }
220 );
221 }
222 );
223 }
224
225 /// <summary>
226 /// Restores the window with the given Id
227 /// </summary>
228 /// <param name="id"></param>
229 public void Restore(Guid id)
230 {
231 this.InvokeAsynchronouslyInBackground
232 (
233 () =>
234 {
235 IWindow window = this.desktop.Children.OfType<IWindow>().Where(wc => wc.Id == id).SingleOrDefault();
236
237 if (window != null)
238 {
239 window.WindowState = WindowState.Normal;
240 window.Activate();
241 }
242 }
243 );
244 }
245
246 #endregion
247
248 #region · Shortcut Methods ·
249
250 /// <summary>
251 /// Creates a new shortcut with the given title and target
252 /// </summary>
253 /// <param name="title"></param>
254 /// <param name="target"></param>
255 public void CreateShortcut<T>(string title, string target) where T : IShortcutViewModel, new()
256 {
257 this.CreateShortcut<T>(title, target, new Point(0, 0));
258 }
259
260 /// <summary>
261 /// Creates a new shortcut with the given title and target
262 /// </summary>
263 /// <param name="title"></param>
264 /// <param name="target"></param>
265 public void CreateShortcut<T>(string title, string target, Point point) where T : IShortcutViewModel, new()
266 {
267 T shortcut = new T
268 {
269 Title = title,
270 Target = target
271 };
272
273 ShortcutElement element = new ShortcutElement
274 {
275 DataContext = shortcut
276 };
277
278 this.Show(element, point);
279 }
280
281 #endregion
282
283 #region · IDesktopElement Methods ·
284
285 public void Show<T>() where T : IDesktopElement, new()
286 {
287 this.Show(new T());
288 }
289
290 public void Show<T>(Point position) where T : IDesktopElement, new()
291 {
292 this.Show(new T(), position);
293 }
294
295 public void Show(IDesktopElement element)
296 {
297 this.InvokeAsynchronously(
298 () =>
299 {
300 DesktopElement desktopElement = element as DesktopElement;
301
302 this.desktop.AddElement(desktopElement);
303 });
304 }
305
306 public void Show(IDesktopElement element, Point position)
307 {
308 this.InvokeAsynchronously(
309 () =>
310 {
311 DesktopElement desktopElement = element as DesktopElement;
312
313 desktopElement.StartupLocation = StartupPosition.Manual;
314
315 this.desktop.AddElement(desktopElement, position);
316 });
317 }
318
319 #endregion
320
321 #region · Common Methods ·
322
323 /// <summary>
324 /// Closes all
325 /// </summary>
326 public void CloseAll()
327 {
328 this.Invoke(
329 () =>
330 {
331 this.desktop.Children
332 .OfType<DesktopElement>()
333 .ToList()
334 .ForEach(e => this.Close(e.Id));
335
336 this.hasBeenActivated = false;
337 });
338 }
339
340 /// <summary>
341 /// Closes the element with the given Id
342 /// </summary>
343 /// <param name="id">The id.</param>
344 public void Close(Guid id)
345 {
346 DesktopElement instance = this.desktop.Children.OfType<DesktopElement>().Where(x => x.Id == id).FirstOrDefault();
347
348 if (instance != null)
349 {
350 if (instance is WindowElement)
351 {
352 this.Windows.Remove((instance as WindowElement).DataContext as INavigationViewModel);
353 }
354
355 instance.Close();
356 }
357 }
358
359 #endregion
360
361 #region · Private Methods ·
362
363 /// <summary>
364 /// Loads desktop contents from disk
365 /// </summary>
366 private void Load()
367 {
368 this.Invoke(
369 () =>
370 {
371 DesktopSerializer.Load(this.desktop, this.GetXamlFilename());
372 });
373 }
374
375 private string GetXamlFilename()
376 {
377 return String.Format("{0}.xaml", this.desktop.Name);
378 }
379
380 private void RemoveElement(DesktopElement element)
381 {
382 this.InvokeAsynchronouslyInBackground(
383 () =>
384 {
385 this.desktop.RemoveElement(element);
386 });
387 }
388
389 #endregion
390 }
391 }