Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to disable automatic resize of main window #4011

Merged
merged 12 commits into from
Sep 13, 2024
2 changes: 1 addition & 1 deletion src/BizHawk.Client.Common/Api/Classes/EmuClientApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public void SetWindowSize(int size)
if (size == 1 || size == 2 || size == 3 || size == 4 || size == 5 || size == 10)
{
_config.SetWindowScaleFor(Emulator.SystemId, size);
_mainForm.FrameBufferResized();
_mainForm.FrameBufferResized(forceWindowResize: true);
_displayManager.OSD.AddMessage($"Window size set to {size}x");
}
else
Expand Down
3 changes: 2 additions & 1 deletion src/BizHawk.Client.Common/IMainFormForApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ public interface IMainFormForApi
/// <remarks>only referenced from <c>EmuClientApi</c></remarks>
void FrameAdvance(bool discardApiHawkSurfaces = true);

void FrameBufferResized();
/// <param name="forceWindowResize">Override <see cref="Common.Config.ResizeWithFramebuffer"/></param>
void FrameBufferResized(bool forceWindowResize = false);

void FrameSkipMessage();

Expand Down
11 changes: 9 additions & 2 deletions src/BizHawk.Client.Common/config/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,10 @@ public void SetWindowScaleFor(string sysID, int windowScale)
public bool MainFormStayOnTop { get; set; }
public bool StartPaused { get; set; }
public bool StartFullscreen { get; set; }
public int MainWndx { get; set; } = -1; // Negative numbers will be ignored
public int MainWndy { get; set; } = -1;
public int? MainWndx { get; set; }
public int? MainWndy { get; set; }
public int? MainWindowWidth { get; set; }
public int? MainWindowHeight { get; set; }
public bool RunInBackground { get; set; } = true;
public bool AcceptBackgroundInput { get; set; }
public bool AcceptBackgroundInputControllerOnly { get; set; }
Expand Down Expand Up @@ -274,6 +276,11 @@ public void SetWindowScaleFor(string sysID, int windowScale)
public int DispCropRight { get; set; } = 0;
public int DispCropBottom { get; set; } = 0;

/// <summary>
/// Automatically resize main window when framebuffer size changes (default behavior)
/// </summary>
public bool ResizeWithFramebuffer { get; set; } = true;

// Sound options
public ESoundOutputMethod SoundOutputMethod { get; set; } = HostCapabilityDetector.HasXAudio2 ? ESoundOutputMethod.XAudio2 : ESoundOutputMethod.OpenAL;

Expand Down
3 changes: 2 additions & 1 deletion src/BizHawk.Client.EmuHawk/IMainFormForTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ public interface IMainFormForTools : IDialogController
void FrameAdvance(bool discardApiHawkSurfaces = true);

/// <remarks>only referenced from <see cref="LuaConsole"/></remarks>
void FrameBufferResized();
/// <param name="forceWindowResize">Override <see cref="Common.Config.ResizeWithFramebuffer"/></param>
void FrameBufferResized(bool forceWindowResize = false);

/// <remarks>only referenced from <see cref="BasicBot"/></remarks>
bool LoadQuickSave(int slot, bool suppressOSD = false);
Expand Down
13 changes: 13 additions & 0 deletions src/BizHawk.Client.EmuHawk/MainForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 12 additions & 2 deletions src/BizHawk.Client.EmuHawk/MainForm.Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -615,15 +615,25 @@ private void ViewSubMenu_DropDownOpened(object sender, EventArgs e)
private void WindowSizeSubMenu_DropDownOpened(object sender, EventArgs e)
{
var windowScale = Config.GetWindowScaleFor(Emulator.SystemId);
foreach (ToolStripMenuItem item in WindowSizeSubMenu.DropDownItems)
foreach (ToolStripMenuItem item in WindowSizeSubMenu.DropDownItems.OfType<ToolStripMenuItem>())
YoshiRulz marked this conversation as resolved.
Show resolved Hide resolved
{
item.Checked = (int) item.Tag == windowScale;
if (item.Tag is int itemScale)
{
item.Checked = itemScale == windowScale && Config.ResizeWithFramebuffer;
}
}
DisableResizeWithFramebufferMenuItem.Checked = !Config.ResizeWithFramebuffer;
}

private void WindowSize_Click(object sender, EventArgs e)
{
Config.SetWindowScaleFor(Emulator.SystemId, (int) ((ToolStripMenuItem) sender).Tag);
FrameBufferResized(forceWindowResize: true);
}

private void DisableResizeWithFramebufferMenuItem_Click(object sender, EventArgs e)
YoshiRulz marked this conversation as resolved.
Show resolved Hide resolved
{
Config.ResizeWithFramebuffer = !DisableResizeWithFramebufferMenuItem.Checked;
FrameBufferResized();
}

Expand Down
59 changes: 33 additions & 26 deletions src/BizHawk.Client.EmuHawk/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,23 +60,17 @@ private void MainForm_Load(object sender, EventArgs e)
{
UpdateWindowTitle();

ToolStripItem[] CreateWindowSizeFactorSubmenus()
YoshiRulz marked this conversation as resolved.
Show resolved Hide resolved
for (int i = 1; i <= WINDOW_SCALE_MAX; i++)
{
var items = new ToolStripItem[WINDOW_SCALE_MAX];
for (int i = 1; i <= WINDOW_SCALE_MAX; i++)
long quotient = Math.DivRem(i, 10, out long remainder);
var temp = new ToolStripMenuItemEx
{
long quotient = Math.DivRem(i, 10, out long remainder);
var temp = new ToolStripMenuItemEx
{
Tag = i,
Text = $"{(quotient > 0 ? quotient : "")}&{remainder}x"
};
temp.Click += this.WindowSize_Click;
items[i - 1] = temp;
}
return items;
Tag = i,
Text = $"{(quotient > 0 ? quotient : "")}&{remainder}x"
};
temp.Click += this.WindowSize_Click;
WindowSizeSubMenu.DropDownItems.Insert(i - 1, temp);
}
WindowSizeSubMenu.DropDownItems.AddRange(CreateWindowSizeFactorSubmenus());

foreach (var (appliesTo, coreNames) in Config.CorePickerUIData)
{
Expand Down Expand Up @@ -619,9 +613,17 @@ _argParser.SocketAddress is var (socketIP, socketPort)
Config.MainWndy = 0;
}

if (Config.MainWndx != -1 && Config.MainWndy != -1 && Config.SaveWindowPosition)
if (Config.SaveWindowPosition)
{
Location = new Point(Config.MainWndx, Config.MainWndy);
if (Config.MainWndx is int x && Config.MainWndy is int y)
Copy link
Member

@YoshiRulz YoshiRulz Sep 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was it necessary to make these nullable to implement this new feature? (edit: I see it's the last commit, so I'm guessing that's a no.) If you could continue using -1 as the sentinel value instead of null, I think that would be preferable. Otherwise, try Point? instead of 2 int?s.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not necessary to the PR, no. I just made this change because negative values including -1 are valid window positions

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possibly related to #3544?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not directly, tool window position saving is handled elsewhere

{
Location = new Point(x, y);
}

if (Config.MainWindowWidth is int width && Config.MainWindowHeight is int height && !Config.ResizeWithFramebuffer)
{
Size = new(width, height);
}
}

if (Config.MainFormStayOnTop) TopMost = true;
Expand Down Expand Up @@ -1372,8 +1374,13 @@ public void TakeScreenshot(string path)
AddOnScreenMessage($"{fi.Name} saved.");
}

public void FrameBufferResized()
/// <param name="forceWindowResize">Override <see cref="Common.Config.ResizeWithFramebuffer"/></param>
public void FrameBufferResized(bool forceWindowResize = false)
YoshiRulz marked this conversation as resolved.
Show resolved Hide resolved
{
if (!Config.ResizeWithFramebuffer && !forceWindowResize)
{
return;
}
// run this entire thing exactly twice, since the first resize may adjust the menu stacking
for (int i = 0; i < 2; i++)
{
Expand Down Expand Up @@ -2399,20 +2406,20 @@ private void SaveConfig(string path = "")
{
if (Config.SaveWindowPosition)
{
if (Config.MainWndx != -32000) // When minimized location is -32000, don't save this into the config file!
if (WindowState is FormWindowState.Normal)
{
Config.MainWndx = Location.X;
}

if (Config.MainWndy != -32000)
{
Config.MainWndy = Location.Y;
Config.MainWindowWidth = Width;
Config.MainWindowHeight = Height;
}
}
else
{
Config.MainWndx = -1;
Config.MainWndy = -1;
Config.MainWndx = null;
Config.MainWndy = null;
Config.MainWindowWidth = null;
Config.MainWindowHeight = null;
}

Config.LastWrittenFrom = VersionInfo.MainVersion;
Expand Down Expand Up @@ -2568,7 +2575,7 @@ private void IncreaseWindowSize()
Config.SetWindowScaleFor(Emulator.SystemId, windowScale);
}
AddOnScreenMessage($"Screensize set to {windowScale}x");
FrameBufferResized();
FrameBufferResized(forceWindowResize: true);
}

private void DecreaseWindowSize()
Expand All @@ -2580,7 +2587,7 @@ private void DecreaseWindowSize()
Config.SetWindowScaleFor(Emulator.SystemId, windowScale);
}
AddOnScreenMessage($"Screensize set to {windowScale}x");
FrameBufferResized();
FrameBufferResized(forceWindowResize: true);
}

private static readonly int[] SpeedPercents = { 1, 3, 6, 12, 25, 50, 75, 100, 150, 200, 300, 400, 800, 1600, 3200, 6400 };
Expand Down