Widow – Blame information for rev 9

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