Widow – Blame information for rev 14

Subversion Repositories:
Rev:
Rev Author Line No. Line
9 office 1 using System;
2 using System.Collections.Generic;
3 using System.Collections.ObjectModel;
4 using System.Collections.Specialized;
1 office 5 using System.ComponentModel;
9 office 6 using System.Linq;
1 office 7 using System.Runtime.CompilerServices;
8 using System.Xml.Serialization;
9 using Windows.Annotations;
10  
11 namespace Windows
12 {
13 [XmlRoot(Namespace = "urn:widow-windows-schema", ElementName = "Windows")]
9 office 14 public class Windows : INotifyPropertyChanged, IDisposable
1 office 15 {
16 #region Public Enums, Properties and Fields
17  
18 [XmlElement(ElementName = "Window")]
9 office 19 public ObservableCollection<Window> Window
1 office 20 {
21 get => _window;
22 set
23 {
24 if (Equals(value, _window))
25 {
26 return;
27 }
28  
11 office 29 _window.CollectionChanged -= Window_CollectionChanged;
12 office 30  
11 office 31 _window.Clear();
9 office 32 _windows.Clear();
33 foreach (var window in value)
34 {
11 office 35 _window.Add(window);
36  
14 office 37 var windowHash = new WindowHash(window.Title, window.Class);
38  
39 if (_windows.ContainsKey(windowHash))
11 office 40 {
41 continue;
42 }
43  
14 office 44 _windows.Add(windowHash, window);
9 office 45 }
46  
11 office 47 _window.CollectionChanged += Window_CollectionChanged;
1 office 48 OnPropertyChanged();
49 }
50 }
51  
52 #endregion
53  
54 #region Private Delegates, Events, Enums, Properties, Indexers and Fields
55  
11 office 56 private readonly ObservableCollection<Window> _window = new ObservableCollection<Window>();
1 office 57  
14 office 58 private readonly Dictionary<WindowHash, Window> _windows = new Dictionary<WindowHash, Window>();
9 office 59  
1 office 60 #endregion
61  
62 #region Constructors, Destructors and Finalizers
63  
64 [UsedImplicitly]
65 public Windows()
66 {
11 office 67 _window.CollectionChanged += Window_CollectionChanged;
1 office 68 }
69  
9 office 70 public void Dispose()
71 {
11 office 72 Window.CollectionChanged -= Window_CollectionChanged;
9 office 73 }
74  
1 office 75 #endregion
76  
77 #region Interface
78  
79 public event PropertyChangedEventHandler PropertyChanged;
80  
81 #endregion
82  
9 office 83 #region Event Handlers
84  
11 office 85 private void Window_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
9 office 86 {
87 if (e.OldItems != null)
88 {
89 foreach (var window in e.OldItems.OfType<Window>())
90 {
14 office 91 var windowHash = new WindowHash(window.Title, window.Class);
92  
93 if (_windows.ContainsKey(windowHash))
9 office 94 {
14 office 95 _windows.Remove(windowHash);
9 office 96 }
97 }
98 }
99  
100  
101 if (e.NewItems != null)
102 {
103 foreach (var window in e.NewItems.OfType<Window>())
104 {
14 office 105 var windowHash = new WindowHash(window.Title, window.Class);
106  
107 if (!_windows.ContainsKey(windowHash))
9 office 108 {
14 office 109 _windows.Add(windowHash, window);
9 office 110 }
111 }
112 }
113 }
114  
115 #endregion
116  
117 #region Public Methods
118  
14 office 119 public bool TryGetWindow(WindowHash hash, out Window window)
9 office 120 {
14 office 121 return _windows.TryGetValue(hash, out window);
9 office 122 }
123  
124 #endregion
125  
1 office 126 #region Private Methods
127  
128 [NotifyPropertyChangedInvocator]
129 protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
130 {
131 PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
132 }
133  
134 #endregion
135 }
136 }