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

Restore window position without default instance #3878

Merged
merged 1 commit into from
Aug 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions GUI/Main/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,9 @@ protected override void OnLoad(EventArgs e)
}

// We need a config object to get the window geometry, but we don't need the registry lock yet
configuration = CurrentInstance != null
? GUIConfiguration.LoadOrCreateConfiguration(
Path.Combine(CurrentInstance.CkanDir(), "GUIConfig.xml"),
CurrentInstance.game)
// No instance, just get the defaults
: new GUIConfiguration();
configuration = GUIConfigForInstance(
// Find the most recently used instance if no default instance
CurrentInstance ?? InstanceWithNewestGUIConfig(manager.Instances.Values));

// This must happen before Shown, and it depends on the configuration
SetStartPosition();
Expand All @@ -182,6 +179,22 @@ protected override void OnLoad(EventArgs e)
base.OnLoad(e);
}

private const string GUIConfigFilename = "GUIConfig.xml";

private static string GUIConfigPath(GameInstance inst)
=> Path.Combine(inst.CkanDir(), GUIConfigFilename);

private static GUIConfiguration GUIConfigForInstance(GameInstance inst)
=> inst == null ? new GUIConfiguration()
: GUIConfiguration.LoadOrCreateConfiguration(GUIConfigPath(inst),
inst.game);

private static GameInstance InstanceWithNewestGUIConfig(IEnumerable<GameInstance> instances)
=> instances.Where(inst => inst.Valid)
.OrderByDescending(inst => File.GetLastWriteTime(GUIConfigPath(inst)))
.ThenBy(inst => inst.Name)
.FirstOrDefault();

/// <summary>
/// Form.Visible says true even when the form hasn't shown yet.
/// This value will tell the truth.
Expand Down Expand Up @@ -379,9 +392,7 @@ private void CurrentInstanceUpdated(bool allowRepoUpdate)
registry.BuildTagIndex(ManageMods.mainModList.ModuleTags);

configuration?.Save();
configuration = GUIConfiguration.LoadOrCreateConfiguration(
Path.Combine(CurrentInstance.CkanDir(), "GUIConfig.xml"),
CurrentInstance.game);
configuration = GUIConfigForInstance(CurrentInstance);

if (!configuration.CheckForUpdatesOnLaunchNoNag && AutoUpdate.CanUpdate)
{
Expand Down
Loading