Skip to content

Commit

Permalink
Persistence of settings window size/position (#2319)
Browse files Browse the repository at this point in the history
should be good enough imo
  • Loading branch information
pizzaboxer committed Sep 12, 2024
1 parent 47cf9e7 commit 83e3c48
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 1 deletion.
4 changes: 3 additions & 1 deletion Bloxstrap/LaunchHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,9 @@ public static void LaunchSettings()
if (interlock.IsAcquired)
{
bool showAlreadyRunningWarning = Process.GetProcessesByName(App.ProjectName).Length > 1;
new UI.Elements.Settings.MainWindow(showAlreadyRunningWarning).Show();

var window = new UI.Elements.Settings.MainWindow(showAlreadyRunningWarning);
window.Show();
}
else
{
Expand Down
2 changes: 2 additions & 0 deletions Bloxstrap/Models/State.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public class State

public AppState Studio { get; set; } = new();

public WindowState SettingsWindow { get; set; } = new();

public List<string> ModManifest { get; set; } = new();
}
}
13 changes: 13 additions & 0 deletions Bloxstrap/Models/WindowState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Bloxstrap.Models
{
public class WindowState
{
public double Width { get; set; }

public double Height { get; set; }

public double Left { get; set; }

public double Top { get; set; }
}
}
34 changes: 34 additions & 0 deletions Bloxstrap/UI/Elements/Settings/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ namespace Bloxstrap.UI.Elements.Settings
/// </summary>
public partial class MainWindow : INavigationWindow
{
private Models.WindowState _state => App.State.Prop.SettingsWindow;

public MainWindow(bool showAlreadyRunningWarning)
{
var viewModel = new MainWindowViewModel();
Expand All @@ -33,6 +35,30 @@ public MainWindow(bool showAlreadyRunningWarning)

if (showAlreadyRunningWarning)
ShowAlreadyRunningSnackbar();

LoadState();
}

public void LoadState()
{
if (_state.Left > SystemParameters.VirtualScreenWidth)
_state.Left = 0;

if (_state.Top > SystemParameters.VirtualScreenHeight)
_state.Top = 0;

if (_state.Width > 0)
this.Width = _state.Width;

if (_state.Height > 0)
this.Height = _state.Height;

if (_state.Left > 0 && _state.Top > 0)
{
this.WindowStartupLocation = WindowStartupLocation.Manual;
this.Left = _state.Left;
this.Top = _state.Top;
}
}

private async void ShowAlreadyRunningSnackbar()
Expand Down Expand Up @@ -66,6 +92,14 @@ private void WpfUiWindow_Closing(object sender, CancelEventArgs e)
if (result != MessageBoxResult.Yes)
e.Cancel = true;
}

_state.Width = this.Width;
_state.Height = this.Height;

_state.Top = this.Top;
_state.Left = this.Left;

App.State.Save();

if (!e.Cancel)
App.Terminate();
Expand Down

0 comments on commit 83e3c48

Please sign in to comment.