Add helper for centering dialog relative to a `Control`

This commit is contained in:
YoshiRulz 2024-11-28 07:18:11 +10:00
parent a39689d62d
commit c3bd723c36
No known key found for this signature in database
GPG Key ID: C4DE31C245353FB7
4 changed files with 30 additions and 15 deletions

View File

@ -74,6 +74,18 @@ namespace BizHawk.Client.EmuHawk
return menu;
}
public static void CenterOn(this Form form, Control logicalParent)
=> form.CenterOn((logicalParent is Form ? logicalParent : logicalParent.Parent)
.ChildPointToScreen(logicalParent) + logicalParent.HalfSize());
public static void CenterOn(this Form form, Point point)
{
// not asserting `form` is closed at this point, but we could
point -= form.HalfSize();
form.StartPosition = FormStartPosition.Manual;
form.Location = point;
}
public static Point ChildPointToScreen(this Control control, Control child)
{
return control.PointToScreen(new Point(child.Location.X, child.Location.Y));
@ -131,6 +143,9 @@ namespace BizHawk.Client.EmuHawk
public static IEnumerable<Control> Controls(this Control control)
=> control.Controls.Cast<Control>();
public static Size HalfSize(this Control c)
=> new(c.Width / 2, c.Height / 2);
public static IEnumerable<TabPage> TabPages(this TabControl tabControl)
{
return tabControl.TabPages.Cast<TabPage>();

View File

@ -92,7 +92,7 @@
this.Controls.Add(this.OkBtn);
this.Controls.Add(this.CancelBtn);
this.Name = "GreenzoneSettings";
this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
this.Text = "Default Savestate History Settings";
this.Load += new System.EventHandler(this.GreenzoneSettings_Load);
this.ResumeLayout(false);

View File

@ -207,7 +207,7 @@ namespace BizHawk.Client.EmuHawk
this.Controls.Add(this.CancelBtn);
this.MinimumSize = new System.Drawing.Size(150, 311);
this.Name = "MovieHeaderEditor";
this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
this.Text = "Header Info";
this.Load += new System.EventHandler(this.MovieHeaderEditor_Load);
this.ResumeLayout(false);

View File

@ -953,24 +953,23 @@ namespace BizHawk.Client.EmuHawk
private void HeaderMenuItem_Click(object sender, EventArgs e)
{
new MovieHeaderEditor(CurrentTasMovie, Config)
{
Owner = Owner,
Location = this.ChildPointToScreen(TasView)
}.Show();
MovieHeaderEditor form = new(CurrentTasMovie, Config) { Owner = this.Owner/*uhh*/ };
form.CenterOn(TasView);
form.Show();
}
private void StateHistorySettingsMenuItem_Click(object sender, EventArgs e)
{
new GreenzoneSettings(
GreenzoneSettings form = new(
DialogController,
new ZwinderStateManagerSettings(CurrentTasMovie.TasStateManager.Settings),
(s, k) => { CurrentTasMovie.TasStateManager.UpdateSettings(s, k); },
false)
{
Location = this.ChildPointToScreen(TasView),
Owner = Owner
}.ShowDialog();
Owner = this.Owner, // uhh
};
form.CenterOn(TasView);
form.ShowDialog();
}
private void CommentsMenuItem_Click(object sender, EventArgs e)
@ -987,15 +986,16 @@ namespace BizHawk.Client.EmuHawk
private void DefaultStateSettingsMenuItem_Click(object sender, EventArgs e)
{
new GreenzoneSettings(
GreenzoneSettings form = new(
DialogController,
new ZwinderStateManagerSettings(Config.Movies.DefaultTasStateManagerSettings),
(s, k) => { Config.Movies.DefaultTasStateManagerSettings = s; },
true)
{
Location = this.ChildPointToScreen(TasView),
Owner = Owner
}.ShowDialog();
Owner = this.Owner, // uhh
};
form.CenterOn(TasView);
form.ShowDialog();
}
private void SettingsSubMenu_DropDownOpened(object sender, EventArgs e)