Skip to content

Commit

Permalink
Fixed issues with saving window resize and move (vrcx-team#699)
Browse files Browse the repository at this point in the history
  • Loading branch information
Myrkie authored and Natsumi-sama committed Dec 4, 2023
1 parent c0a4f8e commit 3c00b65
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions Dotnet/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

using System;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using CefSharp;
Expand All @@ -19,6 +18,7 @@ public partial class MainForm : WinformBase
public static MainForm Instance;
private static NLog.Logger jslogger = NLog.LogManager.GetLogger("Javascript");
public ChromiumWebBrowser Browser;
private readonly Timer _saveTimer;
private int LastLocationX;
private int LastLocationY;
private int LastSizeWidth;
Expand All @@ -29,6 +29,11 @@ public MainForm()
Instance = this;
InitializeComponent();


// adding a 5s delay here to avoid excessive writes to disk
_saveTimer = new Timer();
_saveTimer.Interval = 5000;
_saveTimer.Tick += SaveTimer_Tick;
try
{
var location = Assembly.GetExecutingAssembly().Location;
Expand Down Expand Up @@ -133,8 +138,15 @@ private void MainForm_Resize(object sender, System.EventArgs e)
}
LastSizeWidth = Size.Width;
LastSizeHeight = Size.Height;

_saveTimer.Start();
}

private void SaveTimer_Tick(object sender, EventArgs e)
{
SaveWindowState();
_saveTimer.Stop();
}

private void MainForm_Move(object sender, System.EventArgs e)
{
if (WindowState != FormWindowState.Normal)
Expand All @@ -143,6 +155,8 @@ private void MainForm_Move(object sender, System.EventArgs e)
}
LastLocationX = Location.X;
LastLocationY = Location.Y;

_saveTimer.Start();
}

private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
Expand All @@ -154,14 +168,21 @@ private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
Hide();
}
}

private void MainForm_FormClosed(object sender, FormClosedEventArgs e)


private void SaveWindowState()
{
VRCXStorage.Instance.Set("VRCX_LocationX", LastLocationX.ToString());
VRCXStorage.Instance.Set("VRCX_LocationY", LastLocationY.ToString());
VRCXStorage.Instance.Set("VRCX_SizeWidth", LastSizeWidth.ToString());
VRCXStorage.Instance.Set("VRCX_SizeHeight", LastSizeHeight.ToString());
VRCXStorage.Instance.Set("VRCX_WindowState", ((int)WindowState).ToString());
VRCXStorage.Instance.Flush();
}

private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
{
SaveWindowState();
}

private void TrayIcon_MouseClick(object sender, MouseEventArgs e)
Expand Down Expand Up @@ -195,6 +216,7 @@ private void TrayMenu_DevTools_Click(object sender, System.EventArgs e)

private void TrayMenu_Quit_Click(object sender, System.EventArgs e)
{
SaveWindowState();
Application.Exit();
}
}
Expand Down

0 comments on commit 3c00b65

Please sign in to comment.