Skip to content

Commit 0fcea66

Browse files
committed
Dedup enumerations of Screen.AllScreens w/ safe helper method
fixes 2211a41, 05f06ae, adf7449, and 7a97ab4
1 parent 7a97ab4 commit 0fcea66

File tree

5 files changed

+23
-11
lines changed

5 files changed

+23
-11
lines changed

src/BizHawk.Client.EmuHawk/Extensions/ControlExtensions.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,7 @@ public static DialogResult ShowDialogOnScreen(this Form form)
8787
var topLeft = new Point(
8888
Math.Max(0, form.Location.X),
8989
Math.Max(0, form.Location.Y));
90-
var screen = Screen.AllScreens.Select(static s => s.WorkingArea)
91-
.FirstOrNull(a => a.Contains(topLeft))
90+
var screen = DrawingExtensions.BoundsOfDisplayContaining(topLeft)
9291
?? default; //TODO is zeroed the correct fallback value? --yoshi
9392
var w = screen.Right - form.Bounds.Right;
9493
var h = screen.Bottom - form.Bounds.Bottom;

src/BizHawk.Client.EmuHawk/Extensions/DrawingExtensions.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
#nullable enable
22

33
using System.Drawing;
4+
using System.Linq;
5+
using System.Windows.Forms;
6+
7+
using BizHawk.Common.CollectionExtensions;
48

59
namespace BizHawk.Client.EmuHawk
610
{
711
public static class DrawingExtensions
812
{
13+
#pragma warning disable RCS1224 // don't want extension on nonspecific `Point`
14+
public static Rectangle? BoundsOfDisplayContaining(Point p)
15+
#pragma warning restore RCS1224
16+
=> Screen.AllScreens.Select(static scr => scr.WorkingArea)
17+
.FirstOrNull(rect => rect.Contains(p));
18+
919
public static T GetMutableCopy<T>(this T b)
1020
where T : Brush
1121
=> (T) b.Clone();

src/BizHawk.Client.EmuHawk/tools/TAStudio/BookmarksBranchesBox.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -622,8 +622,9 @@ private void BranchView_PointedCellChanged(object sender, InputRoll.CellEventArg
622622
}
623623

624624
location.Y = Math.Max(0, location.Y);
625-
var screen = Screen.AllScreens.First(s => s.WorkingArea.Contains(location));
626-
var h = screen.WorkingArea.Bottom - bottom;
625+
var screen = DrawingExtensions.BoundsOfDisplayContaining(location)
626+
?? default; //TODO is zeroed the correct fallback value? --yoshi
627+
var h = screen.Bottom - bottom;
627628

628629
if (h < 0)
629630
{

src/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.ListView.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -910,12 +910,17 @@ private void TasView_MouseUp(object sender, MouseEventArgs e)
910910
var bottomRight = new Point(
911911
topLeft.X + RightClickMenu.Width,
912912
topLeft.Y + RightClickMenu.Height);
913-
var screen = Screen.AllScreens.First(s => s.WorkingArea.Contains(topLeft));
913+
var screen = DrawingExtensions.BoundsOfDisplayContaining(topLeft)
914+
?? default; //TODO is zeroed the correct fallback value? --yoshi
914915
// if we don't fully fit, move to the other side of the pointer
915-
if (bottomRight.X > screen.WorkingArea.Right)
916+
if (bottomRight.X > screen.Right)
917+
{
916918
offset.X -= RightClickMenu.Width;
917-
if (bottomRight.Y > screen.WorkingArea.Bottom)
919+
}
920+
if (bottomRight.Y > screen.Bottom)
921+
{
918922
offset.Y -= RightClickMenu.Height;
923+
}
919924
topLeft.Offset(offset);
920925
// if the screen is insultingly tiny, best we can do is avoid negative pos
921926
RightClickMenu.Show(

src/BizHawk.Client.EmuHawk/tools/ToolManager.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -447,10 +447,7 @@ public bool IsLoaded(Type toolType)
447447
=> _tools.Find(t => t.GetType() == toolType)?.IsActive is true;
448448

449449
public bool IsOnScreen(Point topLeft)
450-
{
451-
return Screen.AllScreens.Any(
452-
screen => screen.WorkingArea.Contains(topLeft));
453-
}
450+
=> DrawingExtensions.BoundsOfDisplayContaining(topLeft) is not null;
454451

455452
/// <summary>
456453
/// Returns true if an instance of T exists

0 commit comments

Comments
 (0)