From c61d432fd6c136c82214a2ed426444b8d5fd819c Mon Sep 17 00:00:00 2001 From: adelikat Date: Mon, 20 Feb 2017 10:42:48 -0600 Subject: [PATCH] Don't position tools to last saved position if that position is not visible on screen --- .../config/ToolDialogSettings.cs | 23 ++++++++++++++++++- BizHawk.Client.EmuHawk/tools/Cheats/Cheats.cs | 2 +- .../tools/TI83/TI83KeyPad.cs | 2 +- BizHawk.Client.EmuHawk/tools/ToolHelpers.cs | 6 +++++ BizHawk.Client.EmuHawk/tools/ToolManager.cs | 9 +++++++- .../tools/Watch/RamSearch.cs | 2 +- .../tools/Watch/RamWatch.cs | 2 +- 7 files changed, 40 insertions(+), 6 deletions(-) diff --git a/BizHawk.Client.Common/config/ToolDialogSettings.cs b/BizHawk.Client.Common/config/ToolDialogSettings.cs index b8ebc0ee48..530d0ced44 100644 --- a/BizHawk.Client.Common/config/ToolDialogSettings.cs +++ b/BizHawk.Client.Common/config/ToolDialogSettings.cs @@ -1,6 +1,8 @@ -using System.Drawing; +using System; using System.Collections.Generic; +using System.Drawing; using System.Linq; + using Newtonsoft.Json; namespace BizHawk.Client.Common @@ -56,6 +58,25 @@ namespace BizHawk.Client.Common } } + /// + /// Represents the top left corner coordinate, if Wndx and Wndy form a valid point + /// Throws an InvalidOperationException if Wndx or Wndy is null + /// It is expected to check for this before using this property + /// + [JsonIgnore] + public Point TopLeft + { + get + { + if (_wndx.HasValue && _wndy.HasValue) + { + return new Point(_wndx.Value, _wndy.Value); + } + + throw new InvalidOperationException("TopLeft can not be used when one of the coordinates is null"); + } + } + public int? Width { get; set; } public int? Height { get; set; } diff --git a/BizHawk.Client.EmuHawk/tools/Cheats/Cheats.cs b/BizHawk.Client.EmuHawk/tools/Cheats/Cheats.cs index 1094dbf494..682c6808d4 100644 --- a/BizHawk.Client.EmuHawk/tools/Cheats/Cheats.cs +++ b/BizHawk.Client.EmuHawk/tools/Cheats/Cheats.cs @@ -212,7 +212,7 @@ namespace BizHawk.Client.EmuHawk _defaultWidth = Size.Width; _defaultHeight = Size.Height; - if (Settings.UseWindowPosition) + if (Settings.UseWindowPosition && IsOnScreen(Settings.TopLeft)) { Location = Settings.WindowPosition; } diff --git a/BizHawk.Client.EmuHawk/tools/TI83/TI83KeyPad.cs b/BizHawk.Client.EmuHawk/tools/TI83/TI83KeyPad.cs index 3838f50491..c8bc53a532 100644 --- a/BizHawk.Client.EmuHawk/tools/TI83/TI83KeyPad.cs +++ b/BizHawk.Client.EmuHawk/tools/TI83/TI83KeyPad.cs @@ -25,7 +25,7 @@ namespace BizHawk.Client.EmuHawk private void TI83KeyPad_Load(object sender, EventArgs e) { - if (Global.Config.TI83KeypadSettings.UseWindowPosition) + if (Global.Config.TI83KeypadSettings.UseWindowPosition && IsOnScreen(Global.Config.TI83KeypadSettings.TopLeft)) { Location = Global.Config.TI83KeypadSettings.WindowPosition; } diff --git a/BizHawk.Client.EmuHawk/tools/ToolHelpers.cs b/BizHawk.Client.EmuHawk/tools/ToolHelpers.cs index c382329865..a1c92dc8ae 100644 --- a/BizHawk.Client.EmuHawk/tools/ToolHelpers.cs +++ b/BizHawk.Client.EmuHawk/tools/ToolHelpers.cs @@ -9,6 +9,7 @@ using BizHawk.Emulation.Common.IEmulatorExtensions; using BizHawk.Client.Common; using BizHawk.Client.EmuHawk.WinFormExtensions; +using System.Drawing; namespace BizHawk.Client.EmuHawk { @@ -131,5 +132,10 @@ namespace BizHawk.Client.EmuHawk { Owner = floatingWindow ? null : GlobalWin.MainForm; } + + protected bool IsOnScreen(Point topLeft) + { + return ToolManager.IsOnScreen(topLeft); + } } } diff --git a/BizHawk.Client.EmuHawk/tools/ToolManager.cs b/BizHawk.Client.EmuHawk/tools/ToolManager.cs index 6749e2d55c..6c11766a70 100644 --- a/BizHawk.Client.EmuHawk/tools/ToolManager.cs +++ b/BizHawk.Client.EmuHawk/tools/ToolManager.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Drawing; using System.IO; using System.Linq; using System.Reflection; @@ -244,7 +245,7 @@ namespace BizHawk.Client.EmuHawk RefreshSettings(form, dest, settings, idx); - if (settings.UseWindowPosition) + if (settings.UseWindowPosition && IsOnScreen(settings.TopLeft)) { form.StartPosition = FormStartPosition.Manual; form.Location = settings.WindowPosition; @@ -364,6 +365,12 @@ namespace BizHawk.Client.EmuHawk return false; } + public static bool IsOnScreen(Point topLeft) + { + return Screen.AllScreens.Any( + screen => screen.WorkingArea.Contains(topLeft)); + } + /// /// Returns true if an instance of T exists /// diff --git a/BizHawk.Client.EmuHawk/tools/Watch/RamSearch.cs b/BizHawk.Client.EmuHawk/tools/Watch/RamSearch.cs index b3b81dd2ac..39e5a9ea8c 100644 --- a/BizHawk.Client.EmuHawk/tools/Watch/RamSearch.cs +++ b/BizHawk.Client.EmuHawk/tools/Watch/RamSearch.cs @@ -233,7 +233,7 @@ namespace BizHawk.Client.EmuHawk _defaultWidth = Size.Width; _defaultHeight = Size.Height; - if (Settings.UseWindowPosition) + if (Settings.UseWindowPosition && IsOnScreen(Settings.TopLeft)) { Location = Settings.WindowPosition; } diff --git a/BizHawk.Client.EmuHawk/tools/Watch/RamWatch.cs b/BizHawk.Client.EmuHawk/tools/Watch/RamWatch.cs index 8cb3ca8055..c4a49b3026 100644 --- a/BizHawk.Client.EmuHawk/tools/Watch/RamWatch.cs +++ b/BizHawk.Client.EmuHawk/tools/Watch/RamWatch.cs @@ -464,7 +464,7 @@ namespace BizHawk.Client.EmuHawk _defaultWidth = Size.Width; _defaultHeight = Size.Height; - if (Settings.UseWindowPosition) + if (Settings.UseWindowPosition && IsOnScreen(Settings.TopLeft)) { Location = Settings.WindowPosition; }