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