forked from gitextensions/gitextensions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWindowPositionManager.cs
201 lines (175 loc) · 7.29 KB
/
WindowPositionManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
using GitExtUtils.GitUI;
namespace GitUI
{
internal interface IWindowPositionManager
{
/// <summary>
/// Retrieves a persisted position for the given <paramref name="form"/>.
/// </summary>
/// <param name="form">The form to look the position for.</param>
/// <returns>The form's persisted position; otherwise <see langword="null"/>.</returns>
WindowPosition? LoadPosition(Form form);
/// <summary>
/// Save the position of a form to the user settings. Hides the window
/// as a side-effect.
/// </summary>
/// <param name="form">The form to save the position for.</param>
void SavePosition(Form form);
}
internal sealed class WindowPositionManager : IWindowPositionManager
{
private static WindowPositionList? _windowPositionList;
public static Point FitWindowOnScreen(Rectangle calculatedWindowBounds, IEnumerable<Rectangle> workingArea)
{
// Ensure the window with its new size and location will be accessible to the user
// If all fails, we'll display the window the (0, 0)
Point location = Point.Empty;
foreach (Rectangle screen in workingArea)
{
bool isDisplayed = IsDisplayedOn10Percent(screen, calculatedWindowBounds);
if (isDisplayed)
{
location = calculatedWindowBounds.Location;
break;
}
}
return location;
}
private static bool IsDisplayedOn10Percent(Rectangle screen, Rectangle window)
{
if (screen.IsEmpty || window.IsEmpty)
{
return false;
}
// We insist that any window to cover at least 10% of a screen realestate both horizontally and vertically
// However, check if the window is smaller than the minimum presence requirement.
// If so, adjust the requirements to the size of the window.
const float MinimumScreenPresence = 0.1f; // 10%
int requiredHeight = Math.Min((int)(screen.Height * MinimumScreenPresence), window.Height);
int requireWidth = Math.Min((int)(screen.Width * MinimumScreenPresence), window.Width);
Point p;
if (screen.Contains(window.Location))
{
p = new Point(window.Left + requireWidth, window.Top + requiredHeight);
bool leftTop = screen.Contains(p);
if (leftTop)
{
Debug.WriteLine($"{screen.ToString()} contains {p} (L, T)");
return true;
}
}
if (screen.Contains(new Point(window.Left + (window.Width / 2), window.Top)))
{
p = new Point(window.Left + (window.Width / 2) - requireWidth, window.Top + requiredHeight);
bool middleTop = screen.Contains(p);
if (middleTop)
{
Debug.WriteLine($"{screen.ToString()} contains {p} (W/2-, T)");
return true;
}
p = new Point(window.Left + (window.Width / 2) + requireWidth, window.Top + requiredHeight);
middleTop = screen.Contains(p);
if (middleTop)
{
Debug.WriteLine($"{screen.ToString()} contains {p} (W/2+, T)");
return true;
}
}
if (screen.Contains(new Point(window.Right, window.Top)))
{
p = new Point(window.Right - requireWidth, window.Top + requiredHeight);
bool rightTop = screen.Contains(p);
if (rightTop)
{
Debug.WriteLine($"{screen.ToString()} contains {p} (R, T)");
return true;
}
}
return false;
}
/// <summary>
/// Retrieves a persisted position for the given <paramref name="form"/>.
/// </summary>
/// <param name="form">The form to look the position for.</param>
/// <returns>The form's persisted position; otherwise <see langword="null"/>.</returns>
public WindowPosition? LoadPosition(Form form)
{
try
{
_windowPositionList ??= WindowPositionList.Load();
var pos = _windowPositionList?.Get(form.GetType().Name);
if (pos is not null && !pos.Rect.IsEmpty)
{
return pos;
}
}
catch
{
// TODO: how to restore a corrupted config?
}
return null;
}
/// <summary>
/// Save the position of a form to the user settings. Hides the window
/// as a side-effect.
/// </summary>
/// <param name="form">The form to save the position for.</param>
public void SavePosition(Form form)
{
try
{
var rectangle = form.WindowState == FormWindowState.Normal
? form.DesktopBounds
: form.RestoreBounds;
var formWindowState = form.WindowState == FormWindowState.Maximized
? FormWindowState.Maximized
: FormWindowState.Normal;
if (_windowPositionList is null)
{
_windowPositionList = WindowPositionList.Load();
if (_windowPositionList is null)
{
return;
}
}
var name = form.GetType().Name;
WindowPosition? windowPosition = _windowPositionList.Get(name);
var windowCentred = form.StartPosition == FormStartPosition.CenterParent;
// Don't save location when we center modal form
if (windowPosition is not null && form.Owner is not null && windowCentred)
{
if (rectangle.Width <= windowPosition.Rect.Width && rectangle.Height <= windowPosition.Rect.Height)
{
rectangle.Location = windowPosition.Rect.Location;
}
}
WindowPosition position = new(rectangle, DpiUtil.DpiX, formWindowState, name);
_windowPositionList.AddOrUpdate(position);
_windowPositionList.Save();
}
catch
{
// TODO: how to restore a corrupted config?
}
}
internal TestAccessor GetTestAccessor()
{
return new TestAccessor(this);
}
internal readonly struct TestAccessor
{
private readonly WindowPositionManager _windowPositionManager;
internal TestAccessor(WindowPositionManager windowPositionManager)
{
_windowPositionManager = windowPositionManager;
}
public static bool IsDisplayedOn10Percent(Rectangle screen, Rectangle window)
=> WindowPositionManager.IsDisplayedOn10Percent(screen, window);
}
}
}