forked from gitextensions/gitextensions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGitExtensionsForm.cs
182 lines (150 loc) · 6.29 KB
/
GitExtensionsForm.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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using GitExtUtils.GitUI;
using GitUI.Interops.DwmApi;
using GitUI.Theming;
using ResourceManager;
namespace GitUI
{
// NOTE do not make this class abstract as it breaks the WinForms designer in VS
/// <summary>Base class for a Git Extensions <see cref="Form"/>.</summary>
/// <remarks>Includes support for font, hotkey, icon, translation, and position restore.</remarks>
public class GitExtensionsForm : GitExtensionsFormBase
{
private IWindowPositionManager _windowPositionManager = new WindowPositionManager();
private Func<IReadOnlyList<Rectangle>> _getScreensWorkingArea = () => Screen.AllScreens.Select(screen => screen.WorkingArea).ToArray();
private bool _needsPositionRestore;
/// <summary>Creates a new <see cref="GitExtensionsForm"/> without position restore.</summary>
public GitExtensionsForm()
: this(enablePositionRestore: false)
{
}
/// <summary>Creates a new <see cref="GitExtensionsForm"/> indicating position restore.</summary>
/// <param name="enablePositionRestore">Indicates whether the <see cref="Form"/>'s position
/// will be restored upon being re-opened.</param>
protected GitExtensionsForm(bool enablePositionRestore)
{
var needsPositionSave = enablePositionRestore;
_needsPositionRestore = enablePositionRestore;
FormClosing += GitExtensionsForm_FormClosing;
Button cancelButton = new();
cancelButton.Click += CancelButtonClick;
CancelButton = cancelButton;
if (ThemeModule.IsDarkTheme)
{
// Warning: This call freezes the CI in AppVeyor, however dark theme is not used on build machines
DwmApi.UseImmersiveDarkMode(Handle, true);
}
void GitExtensionsForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (!needsPositionSave)
{
return;
}
needsPositionSave = false;
_windowPositionManager.SavePosition(this);
TaskbarProgress.Clear();
}
}
public virtual void CancelButtonClick(object sender, EventArgs e)
{
Close();
}
protected override void OnLoad(EventArgs e)
{
RestorePosition();
// Should be called after restoring position
base.OnLoad(e);
if (!IsDesignMode)
{
OnRuntimeLoad(e);
}
}
/// <summary>Invoked at runtime during the <see cref="OnLoad"/> method.</summary>
/// <remarks>In particular, this method is not invoked when running in a designer.</remarks>
protected virtual void OnRuntimeLoad(EventArgs e)
{
}
/// <summary>
/// Restores the position of a form from the user settings. Does
/// nothing if there is no entry for the form in the settings, or the
/// setting would be invisible on the current display configuration.
/// </summary>
protected virtual void RestorePosition()
{
if (!_needsPositionRestore)
{
return;
}
if (WindowState == FormWindowState.Minimized)
{
// TODO: do we still need to assert when restored it is shown on the correct monitor?
return;
}
var position = _windowPositionManager.LoadPosition(this);
if (position is null)
{
return;
}
_needsPositionRestore = false;
IReadOnlyList<Rectangle> workingArea = _getScreensWorkingArea();
if (!workingArea.Any(screen => screen.IntersectsWith(position.Rect)))
{
if (position.State == FormWindowState.Maximized)
{
WindowState = FormWindowState.Maximized;
}
return;
}
SuspendLayout();
var windowCentred = StartPosition == FormStartPosition.CenterParent;
StartPosition = FormStartPosition.Manual;
if (FormBorderStyle == FormBorderStyle.Sizable ||
FormBorderStyle == FormBorderStyle.SizableToolWindow)
{
Size = DpiUtil.Scale(position.Rect.Size, originalDpi: position.DeviceDpi);
}
if (Owner is null || !windowCentred)
{
Point calculatedLocation = DpiUtil.Scale(position.Rect.Location, originalDpi: position.DeviceDpi);
DesktopLocation = WindowPositionManager.FitWindowOnScreen(new Rectangle(calculatedLocation, Size), workingArea);
}
else
{
// Calculate location for modal form with parent
Point calculatedLocation = new(
Owner.Left + (Owner.Width / 2) - (Width / 2),
Owner.Top + (Owner.Height / 2) - (Height / 2));
Location = WindowPositionManager.FitWindowOnScreen(new Rectangle(calculatedLocation, Size), workingArea);
}
if (WindowState != position.State)
{
WindowState = position.State;
}
ResumeLayout();
}
// This is a base class for many forms, which have own GetTestAccessor() methods. This has to be unique
internal GitExtensionsFormTestAccessor GetGitExtensionsFormTestAccessor() => new(this);
internal readonly struct GitExtensionsFormTestAccessor
{
private readonly GitExtensionsForm _form;
public GitExtensionsFormTestAccessor(GitExtensionsForm form)
{
_form = form;
}
public IWindowPositionManager WindowPositionManager
{
get => _form._windowPositionManager;
set => _form._windowPositionManager = value;
}
public Func<IReadOnlyList<Rectangle>> GetScreensWorkingArea
{
get => _form._getScreensWorkingArea;
set => _form._getScreensWorkingArea = value;
}
}
}
}