comparison Chronosv2/source/Presentation/Windows/VirtualDesktopManager.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.Generic;
27 using System.Collections.ObjectModel;
28 using System.Diagnostics;
29 using System.Linq;
30 using System.Windows;
31 using System.Windows.Controls;
32 using System.Windows.Threading;
33 using Chronos.Extensions.Windows;
34 using Chronos.Presentation.Core.ViewModel;
35 using Chronos.Presentation.Core.VirtualDesktops;
36 using Chronos.Presentation.Core.Windows;
37 using Chronos.Presentation.Windows.Controls;
38 using NLog;
39 using nRoute.Components.Composition;
40 using nRoute.Services;
41
42 namespace Chronos.Presentation.Windows
43 {
44 [MapService(typeof(IVirtualDesktopManager),
45 InitializationMode=InitializationMode.OnDemand,
46 Lifetime=InstanceLifetime.Singleton)]
47 public sealed class VirtualDesktopManager
48 : DispatcherObject, IVirtualDesktopManager
49 {
50 #region · Logger ·
51
52 private static Logger Logger = LogManager.GetCurrentClassLogger();
53
54 #endregion
55
56 #region · Attached Properties ·
57
58 /// <summary>
59 /// Identifies the IsDesktopCanvas dependency property.
60 /// </summary>
61 public static readonly DependencyProperty IsDesktopProperty =
62 DependencyProperty.RegisterAttached("IsDesktop", typeof(bool), typeof(VirtualDesktopManager),
63 new FrameworkPropertyMetadata((bool)false,
64 new PropertyChangedCallback(OnIsDesktop)));
65
66 /// <summary>
67 /// Identifies the IsModalContainer dependency property.
68 /// </summary>
69 public static readonly DependencyProperty IsIsModalContainerProperty =
70 DependencyProperty.RegisterAttached("IsModalContainer", typeof(bool), typeof(VirtualDesktopManager),
71 new FrameworkPropertyMetadata((bool)false,
72 new PropertyChangedCallback(OnIsModalContainer)));
73
74 #endregion
75
76 #region · Attached Property Get/Set Methods ·
77
78 /// <summary>
79 /// Gets the value of the IsDesktopCanvas attached property
80 /// </summary>
81 /// <param name="d"></param>
82 /// <returns></returns>
83 public static bool GetIsDesktop(DependencyObject d)
84 {
85 return (bool)d.GetValue(IsDesktopProperty);
86 }
87
88 /// <summary>
89 /// Sets the value of the IsDesktopCanvas attached property
90 /// </summary>
91 /// <param name="d"></param>
92 /// <param name="value"></param>
93 public static void SetIsDesktop(DependencyObject d, bool value)
94 {
95 d.SetValue(IsDesktopProperty, value);
96 }
97
98 /// <summary>
99 /// Gets the value of the IsModalContainer attached property
100 /// </summary>
101 /// <param name="d"></param>
102 /// <returns></returns>
103 public static bool GetIsModalContainer(DependencyObject d)
104 {
105 return (bool)d.GetValue(IsIsModalContainerProperty);
106 }
107
108 /// <summary>
109 /// Sets the value of the IsModalContainer attached property
110 /// </summary>
111 /// <param name="d"></param>
112 /// <param name="value"></param>
113 public static void SetIsModalContainer(DependencyObject d, bool value)
114 {
115 d.SetValue(IsIsModalContainerProperty, value);
116 }
117
118 #endregion
119
120 #region · Attached Properties Callbacks ·
121
122 private static void OnIsDesktop(DependencyObject d, DependencyPropertyChangedEventArgs e)
123 {
124 if (d != null)
125 {
126 IVirtualDesktopManager service;
127
128 if (ServiceLocator.TryGetService<IVirtualDesktopManager>(out service))
129 {
130 service.RegisterDesktop(d);
131 }
132 }
133 }
134
135 private static void OnIsModalContainer(DependencyObject d, DependencyPropertyChangedEventArgs e)
136 {
137 if (d != null)
138 {
139 IVirtualDesktopManager service;
140
141 if (ServiceLocator.TryGetService<IVirtualDesktopManager>(out service))
142 {
143 service.RegisterModalContainer(d);
144 }
145 }
146 }
147
148 #endregion
149
150 #region · Fields ·
151
152 private List<IVirtualDesktop> virtualDesktops;
153 private IVirtualDesktop activeDesktop;
154
155 #endregion
156
157 #region · Properties ·
158
159 public ReadOnlyObservableCollection<INavigationViewModel> ActiveDesktopWindows
160 {
161 get { return this.ActiveDesktop.ActiveWindows; }
162 }
163
164 public bool HasDesktopActive
165 {
166 get { return (this.ActiveDesktop != null); }
167 }
168
169 #endregion
170
171 #region · Private Properties ·
172
173 private List<IVirtualDesktop> Desktops
174 {
175 get
176 {
177 if (this.virtualDesktops == null)
178 {
179 this.virtualDesktops = new List<IVirtualDesktop>();
180 }
181
182 return this.virtualDesktops;
183 }
184 }
185
186 private IVirtualDesktop ActiveDesktop
187 {
188 get { return this.activeDesktop; }
189 set
190 {
191 if (this.activeDesktop != value)
192 {
193 this.activeDesktop = value;
194 }
195 }
196 }
197
198 #endregion
199
200 #region · Constructors ·
201
202 public VirtualDesktopManager()
203 {
204 }
205
206 #endregion
207
208 #region · Virtual Desktop Actions ·
209
210 /// <summary>
211 /// Activates the default desktop
212 /// </summary>
213 public void ActivateDefaultDesktop()
214 {
215 Logger.Debug("Activar el escritorio virtual por defecto");
216
217 Debug.Assert(this.Desktops.Count > 0, "There are no desktop registered");
218
219 this.ActiveDesktop = this.Desktops.First();
220 this.ActiveDesktop.Activate();
221 }
222
223 /// <summary>
224 /// Switches the active desktop
225 /// </summary>
226 public void SwitchDesktop()
227 {
228 Logger.Debug("Cambiar el escritorio virtual activo");
229
230 Debug.Assert(this.Desktops.Count > 0, "There are no desktop registered");
231
232 if (!this.HasDesktopActive)
233 {
234 this.ActivateDefaultDesktop();
235 }
236 else
237 {
238 IVirtualDesktop currentDesktop = this.ActiveDesktop;
239
240 this.Desktops
241 .ToList()
242 .ForEach(vd => vd.Deactivate());
243
244 this.ActiveDesktop = null;
245
246 IVirtualDesktop desktop = this.Desktops.Where(vd => vd.Id != currentDesktop.Id).FirstOrDefault();
247
248 if (desktop != null)
249 {
250 this.ActiveDesktop = desktop;
251 this.ActiveDesktop.Activate();
252 }
253 else
254 {
255 this.ActivateDefaultDesktop();
256 }
257 }
258 }
259
260 /// <summary>
261 /// Shows the desktop
262 /// </summary>
263 public void ShowDesktop()
264 {
265 Logger.Debug("Mostrar el escritorio virtual activo");
266
267 this.VerifyActiveDesktop();
268
269 this.ActiveDesktop.ShowDesktop();
270 }
271
272 /// <summary>
273 /// Saves the active desktop to disk
274 /// </summary>
275 public void SaveCurrentDesktop()
276 {
277 Logger.Debug("Guardar los cambios del escritorio virtual activo");
278
279 this.VerifyActiveDesktop();
280
281 this.ActiveDesktop.Save();
282 }
283
284 public void SaveAllDesktops()
285 {
286 Logger.Debug("Guardar los cambios de todos los escritorios virtuales");
287
288 this.Desktops
289 .ToList()
290 .ForEach(vd => vd.Save());
291 }
292
293 #endregion
294
295 #region · Window Methods ·
296
297 /// <summary>
298 /// Shows the given window instance
299 /// </summary>
300 /// <param name="element">A <see cref="WindowElement"/> instance</param>
301 public void Show(IWindow window)
302 {
303 Logger.Debug<IWindow>("Mostrar una nueva ventana ({0})", window);
304
305 this.VerifyActiveDesktop();
306
307 this.ActiveDesktop.Show(window);
308 }
309
310 /// <summary>
311 /// Shows the dialog.
312 /// </summary>
313 /// <param name="window">The window.</param>
314 /// <returns></returns>
315 public DialogResult ShowDialog(IModalVindow window)
316 {
317 Logger.Debug<IModalVindow>("Mostrar una nueva ventana modal ({0})", window);
318
319 DialogResult dialogResult = DialogResult.None;
320
321 this.Invoke
322 (
323 delegate
324 {
325 try
326 {
327 dialogResult = window.ShowDialog();
328 }
329 finally
330 {
331 if (window != null)
332 {
333 window.Close();
334 window = null;
335 }
336 }
337 }
338 );
339
340 return dialogResult;
341 }
342
343 /// <summary>
344 /// Closes the current dialog
345 /// </summary>
346 public void CloseDialog()
347 {
348 Logger.Debug("Cierre de la ventana modal activa");
349
350 this.VerifyActiveDesktop();
351
352 this.Invoke(
353 delegate
354 {
355 WindowElement window = WindowElement.ModalContainerPanel.Children.OfType<WindowElement>().FirstOrDefault();
356
357 if (window != null)
358 {
359 window.Hide();
360 }
361 });
362 }
363
364 /// <summary>
365 /// Restores the window with the given Id
366 /// </summary>
367 /// <param name="id"></param>
368 public void Restore(Guid id)
369 {
370 this.VerifyActiveDesktop();
371
372 this.ActiveDesktop.Restore(id);
373 }
374
375 #endregion
376
377 #region · Shortcut Methods ·
378
379 /// <summary>
380 /// Creates a new shortcut with the given title and target
381 /// </summary>
382 /// <param name="title"></param>
383 /// <param name="target"></param>
384 public void CreateShortcut<T>(string title, string target)
385 where T : IShortcutViewModel, new()
386 {
387 Logger.Debug("Crear un nuevo acceso directo en el escritorio virtual activo ({0} - {1})", title, target);
388
389 this.VerifyActiveDesktop();
390
391 this.ActiveDesktop.CreateShortcut<T>(title, target);
392 }
393
394 /// <summary>
395 /// Creates a new shortcut with the given title and target
396 /// </summary>
397 /// <param name="title"></param>
398 /// <param name="target"></param>
399 public void CreateShortcut<T>(string title, string target, Point position)
400 where T : IShortcutViewModel, new()
401 {
402 Logger.Debug("Crear un nuevo acceso directo en el escritorio virtual activo ({0} - {1})", title, target);
403
404 this.VerifyActiveDesktop();
405
406 this.ActiveDesktop.CreateShortcut<T>(title, target, position);
407 }
408
409 #endregion
410
411 #region · IDesktopElement Methods ·
412
413 public void Show<T>()
414 where T : IDesktopElement, new()
415 {
416 Logger.Debug("Mostrar un nuevo elemento en el escritorio virtual activo ({0})", typeof(T));
417
418 this.VerifyActiveDesktop();
419
420 this.ActiveDesktop.Show<T>();
421 }
422
423 public void Show<T>(Point position)
424 where T : IDesktopElement, new()
425 {
426 Logger.Debug("Mostrar un nuevo elemento en el escritorio virtual activo ({0})", typeof(T));
427
428 this.VerifyActiveDesktop();
429
430 this.ActiveDesktop.Show<T>(position);
431 }
432
433 public void Show(IDesktopElement instance)
434 {
435 Logger.Debug("Mostrar un nuevo elemento en el escritorio virtual activo ({0})", instance.GetType());
436
437 this.VerifyActiveDesktop();
438
439 this.ActiveDesktop.Show(instance);
440 }
441
442 public void Show(IDesktopElement instance, Point position)
443 {
444 Logger.Debug("Mostrar un nuevo elemento en el escritorio virtual activo ({0})", instance.GetType());
445
446 this.VerifyActiveDesktop();
447
448 this.ActiveDesktop.Show(instance, position);
449 }
450
451 #endregion
452
453 #region · Common Methods ·
454
455 /// <summary>
456 /// Closes the given element with the given Id
457 /// </summary>
458 /// <param name="id">The id.</param>
459 public void Close(Guid id)
460 {
461 Logger.Debug("Cerrar un elemento en el escritorio virtual activo ({0})", id);
462
463 this.VerifyActiveDesktop();
464
465 this.ActiveDesktop.Close(id);
466 }
467
468 /// <summary>
469 /// Closes all
470 /// </summary>
471 public void CloseAll()
472 {
473 Logger.Debug("Cerrar todos los elementos en el escritorio virtual activo");
474
475 this.VerifyActiveDesktop();
476
477 this.Desktops
478 .ToList()
479 .ForEach(vd => vd.CloseAll());
480
481 this.DeactivateAll();
482 }
483
484 #endregion
485
486 #region · Desktop registration Methods ·
487
488 /// <summary>
489 /// Attach the given dependency object as a Desktop to the Window manager
490 /// </summary>
491 /// <param name="desktop"></param>
492 public void RegisterDesktop(DependencyObject d)
493 {
494 Logger.Debug("Registro de escritorio virtual ({0})", d);
495
496 Debug.Assert(d != null, "do");
497 Debug.Assert(d is Desktop, "do is not an instance of Desktop");
498
499 Desktop desktop = d as Desktop;
500
501 if (desktop != null)
502 {
503 if (this.Desktops.Where(vd => vd.Id == desktop.Id).Count() == 0)
504 {
505 this.Desktops.Add(new VirtualDesktop(desktop));
506 }
507 }
508 }
509
510 /// <summary>
511 /// Attach the given dependency object as a Modal Container to the Window manager
512 /// </summary>
513 /// <param name="desktop"></param>
514 public void RegisterModalContainer(DependencyObject d)
515 {
516 Logger.Debug("Registro de contenedor para ventanas modales ({0})", d);
517
518 Panel container = d as Panel;
519
520 if (container != null)
521 {
522 WindowElement.ModalContainerPanel = container;
523 }
524 }
525
526 #endregion
527
528 #region · Private Methods ·
529
530 private void VerifyActiveDesktop()
531 {
532 if (!this.HasDesktopActive)
533 {
534 this.ActivateDefaultDesktop();
535 }
536 }
537
538 private void DeactivateAll()
539 {
540 this.Desktops
541 .AsParallel()
542 .ForAll(vd => vd.Deactivate());
543 }
544
545 #endregion
546 }
547 }