Widow – Blame information for rev 8
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | using System; |
8 | office | 2 | using System.Threading; |
3 | using System.Threading.Tasks; |
||
1 | office | 4 | |
5 | namespace Widow |
||
6 | { |
||
7 | office | 7 | public class WindowRescale : IDisposable |
1 | office | 8 | { |
9 | #region Public Enums, Properties and Fields |
||
10 | |||
11 | public bool OnWindowCreate { get; set; } |
||
12 | |||
8 | office | 13 | public int ApplyEveryTime { get; set; } |
1 | office | 14 | |
8 | office | 15 | public bool ApplyEveryTimeEnabled { get; set; } |
16 | |||
1 | office | 17 | #endregion |
18 | |||
19 | #region Private Delegates, Events, Enums, Properties, Indexers and Fields |
||
20 | |||
8 | office | 21 | private MainForm Form { get; } |
22 | |||
1 | office | 23 | private Windows.Windows Windows { get; } |
24 | |||
8 | office | 25 | private CancellationTokenSource CancellationTokenSource { get; set; } |
26 | |||
27 | private Task ApplyEveryTask { get; } |
||
28 | |||
1 | office | 29 | #endregion |
30 | |||
31 | #region Constructors, Destructors and Finalizers |
||
32 | |||
7 | office | 33 | public WindowRescale() |
1 | office | 34 | { |
8 | office | 35 | CancellationTokenSource = new CancellationTokenSource(); |
36 | ApplyEveryTask = ApplyEvery(CancellationTokenSource.Token); |
||
1 | office | 37 | } |
38 | |||
7 | office | 39 | public WindowRescale(MainForm mainForm) : this() |
1 | office | 40 | { |
6 | office | 41 | Form = mainForm; |
1 | office | 42 | |
43 | Form.WindowCreated += Form_WindowCreated; |
||
44 | } |
||
45 | |||
7 | office | 46 | public WindowRescale(MainForm mainForm, Windows.Windows windows) : this(mainForm) |
1 | office | 47 | { |
48 | Windows = windows; |
||
49 | } |
||
50 | |||
51 | public void Dispose() |
||
52 | { |
||
53 | Form.WindowCreated -= Form_WindowCreated; |
||
8 | office | 54 | CancellationTokenSource.Cancel(); |
55 | ApplyEveryTask.Wait(); |
||
56 | CancellationTokenSource.Dispose(); |
||
57 | CancellationTokenSource = null; |
||
1 | office | 58 | } |
59 | |||
60 | #endregion |
||
61 | |||
62 | #region Event Handlers |
||
63 | |||
64 | private void Form_WindowCreated(object sender, WindowCreatedEventArgs e) |
||
65 | { |
||
66 | if (!OnWindowCreate) |
||
67 | { |
||
68 | return; |
||
69 | } |
||
70 | |||
71 | foreach (var window in Windows.Window) |
||
72 | { |
||
73 | if (window.Name != e.Name) |
||
74 | { |
||
75 | continue; |
||
76 | } |
||
77 | |||
78 | var hWnd = Helpers.FindWindowByTitle(window.Name); |
||
79 | if (hWnd == IntPtr.Zero) |
||
80 | { |
||
81 | continue; |
||
82 | } |
||
83 | |||
84 | Natives.MoveWindow(hWnd, window.Left, window.Top, window.Width, window.Height, true); |
||
85 | } |
||
86 | } |
||
87 | |||
88 | #endregion |
||
7 | office | 89 | |
90 | #region Public Methods |
||
91 | |||
92 | public void Apply() |
||
93 | { |
||
94 | foreach (var window in Windows.Window) |
||
95 | { |
||
96 | var hWnd = Helpers.FindWindowByTitle(window.Name); |
||
97 | if (hWnd == IntPtr.Zero) |
||
98 | { |
||
99 | continue; |
||
100 | } |
||
101 | |||
102 | Natives.MoveWindow(hWnd, window.Left, window.Top, window.Width, window.Height, true); |
||
103 | } |
||
104 | } |
||
105 | |||
106 | #endregion |
||
8 | office | 107 | |
108 | #region Private Methods |
||
109 | |||
110 | private async Task ApplyEvery(CancellationToken cancellationToken) |
||
111 | { |
||
112 | do |
||
113 | { |
||
114 | if (!ApplyEveryTimeEnabled) |
||
115 | { |
||
116 | await Task.Delay(1000, cancellationToken); |
||
117 | continue; |
||
118 | } |
||
119 | |||
120 | await Task.Delay(ApplyEveryTime, cancellationToken); |
||
121 | |||
122 | Apply(); |
||
123 | } while (!cancellationToken.IsCancellationRequested); |
||
124 | } |
||
125 | |||
126 | #endregion |
||
1 | office | 127 | } |
128 | } |