Widow – Diff between revs 9 and 11

Subversion Repositories:
Rev:
Show entire fileIgnore whitespace
Rev 9 Rev 11
Line 1... Line 1...
1 using System; 1 using System;
-   2 using System.Linq;
2 using System.Threading; 3 using System.Threading;
3 using System.Threading.Tasks; 4 using System.Threading.Tasks;
-   5 using System.Threading.Tasks.Dataflow;
-   6 using Windows;
4 using Widow.Properties; 7 using Widow.Properties;
Line 5... Line 8...
5   8  
6 namespace Widow 9 namespace Widow
7 { 10 {
8 public class WindowManipulation : IDisposable 11 public class WindowManipulation : IDisposable
-   12 {
-   13 #region Public Events & Delegates
-   14  
-   15 public event EventHandler<WindowManipulatedEventArgs> WindowManipulated;
-   16  
-   17 #endregion
9 { 18  
Line 10... Line 19...
10 #region Public Enums, Properties and Fields 19 #region Public Enums, Properties and Fields
Line 11... Line 20...
11   20  
Line 19... Line 28...
19   28  
Line 20... Line 29...
20 #region Private Delegates, Events, Enums, Properties, Indexers and Fields 29 #region Private Delegates, Events, Enums, Properties, Indexers and Fields
Line 21... Line 30...
21   30  
Line 22... Line 31...
22 private MainForm Form { get; } 31 private MainForm Form { get; }
Line 23... Line 32...
23   32  
Line -... Line 33...
-   33 private Windows.Windows WindowsToManipulate { get; }
-   34  
-   35 private CancellationTokenSource CancellationTokenSource { get; set; }
-   36  
-   37 private Task ApplyEveryTask { get; }
-   38  
24 private Windows.Windows Windows { get; } 39 private BufferBlock<Window> WindowsBufferBlock { get; }
Line 25... Line 40...
25   40  
Line 26... Line 41...
26 private CancellationTokenSource CancellationTokenSource { get; set; } 41 private ActionBlock<Window> WindowsActionBlock { get; }
27   42  
28 private Task ApplyEveryTask { get; } 43 private IDisposable WindowsLink { get; set; }
-   44  
-   45 #endregion
-   46  
-   47 #region Constructors, Destructors and Finalizers
29   48  
30 #endregion 49 public WindowManipulation()
Line 31... Line 50...
31   50 {
32 #region Constructors, Destructors and Finalizers 51 CancellationTokenSource = new CancellationTokenSource();
33   52 WindowsBufferBlock = new BufferBlock<Window>();
Line 34... Line 53...
34 public WindowManipulation() 53 WindowsActionBlock = new ActionBlock<Window>(ManipulateWindows);
35 { 54 WindowsLink = WindowsBufferBlock.LinkTo(WindowsActionBlock);
Line 36... Line 55...
36 CancellationTokenSource = new CancellationTokenSource(); 55  
37 ApplyEveryTask = ApplyEvery(CancellationTokenSource.Token); 56 ApplyEveryTask = ApplyEvery(CancellationTokenSource.Token);
38 } 57 }
39   58  
Line 40... Line 59...
40 public WindowManipulation(MainForm mainForm) : this() 59 public WindowManipulation(MainForm mainForm) : this()
41 { 60 {
42 Form = mainForm; 61 Form = mainForm;
43   62  
44 Form.WindowCreated += Form_WindowCreated; 63 Form.WindowCreated += Form_WindowCreated;
45 } 64 }
46   65  
Line 60... Line 79...
60 } 79 }
61 } 80 }
Line 62... Line 81...
62   81  
63 public void Dispose() 82 public void Dispose()
-   83 {
-   84 WindowsLink?.Dispose();
-   85 WindowsLink = null;
64 { 86  
65 Form.WindowCreated -= Form_WindowCreated; 87 Form.WindowCreated -= Form_WindowCreated;
66 CancellationTokenSource.Cancel(); 88 CancellationTokenSource.Cancel();
67 ApplyEveryTask.Wait(); 89 ApplyEveryTask.Wait();
68 CancellationTokenSource.Dispose(); 90 CancellationTokenSource.Dispose();
Line 71... Line 93...
71   93  
Line 72... Line 94...
72 #endregion 94 #endregion
Line 73... Line 95...
73   95  
74 #region Event Handlers 96 #region Event Handlers
75   97  
76 private void Form_WindowCreated(object sender, WindowCreatedEventArgs e) 98 private async void Form_WindowCreated(object sender, WindowCreatedEventArgs e)
77 { 99 {
78 if (!OnWindowCreate) 100 if (!OnWindowCreate)
Line 79... Line 101...
79 { 101 {
80 return; 102 return;
81 } 103 }
82   104  
Line 83... Line 105...
83 if (!Windows.Contains(e.Name)) 105 if (!WindowsToManipulate.Contains(e.Title))
84 { 106 {
Line 85... Line 107...
85 return; 107 return;
Line 86... Line 108...
86 } 108 }
Line 87... Line 109...
87   109  
88 Apply(); 110 await Apply();
-   111 }
-   112  
-   113 #endregion
-   114  
-   115 #region Public Methods
-   116  
-   117 public async Task Apply()
-   118 {
-   119 await Task.WhenAll(WindowsToManipulate.Window.Select(window => WindowsBufferBlock.SendAsync(window)));
89 } 120 }
90   121  
91 #endregion -  
92   122 #endregion
93 #region Public Methods 123  
94   124 #region Private Methods
95 public void Apply() 125  
Line 129... Line 159...
129 if (window.IgnoreHeight) 159 if (window.IgnoreHeight)
130 { 160 {
131 height = rect.Top - rect.Bottom; 161 height = rect.Top - rect.Bottom;
132 } 162 }
Line 133... Line 163...
133   163  
-   164 var success = Natives.MoveWindow(hWnd, left, top, width, height, true);
-   165  
134 Natives.MoveWindow(hWnd, left, top, width, height, true); 166 WindowManipulated?.Invoke(this, new WindowManipulatedEventArgs(success, window));
135 } 167 }
Line 136... Line -...
136 } -  
137   -  
138 #endregion -  
139   -  
140 #region Private Methods 168 }
141   169  
142 private async Task ApplyEvery(CancellationToken cancellationToken) 170 private async Task ApplyEvery(CancellationToken cancellationToken)
143 { 171 {
144 do 172 do
Line 149... Line 177...
149 continue; 177 continue;
150 } 178 }
Line 151... Line 179...
151   179  
Line 152... Line 180...
152 await Task.Delay(ApplyEveryTime, cancellationToken); 180 await Task.Delay(ApplyEveryTime, cancellationToken);
153   181  
154 Apply(); 182 await Apply();
Line 155... Line 183...
155 } while (!cancellationToken.IsCancellationRequested); 183 } while (!cancellationToken.IsCancellationRequested);
156 } 184 }