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 "Remember last folder in file pickers" setting, enabled by default #70

Merged
merged 6 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 2 additions & 0 deletions src/Shared.Core/KoikatuAPIBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public static bool EnableDebugLogging
}

private static ConfigEntry<bool> EnableDebugLoggingSetting { get; set; }
internal static ConfigEntry<bool> RememberFilePickerFolder { get; set; }

internal static KoikatuAPI Instance { get; private set; }
internal static new ManualLogSource Logger { get; private set; }
Expand Down Expand Up @@ -84,6 +85,7 @@ private void BaseAwake()
Logger = base.Logger;

EnableDebugLoggingSetting = Config.Bind("Debug", "Show debug messages", false, "Enables display of additional log messages when certain events are triggered within KKAPI. Useful for plugin devs to understand when controller messages are fired. Changes take effect after game restart.");
RememberFilePickerFolder = Config.Bind("General", "Remember last folder in file pickers", true, "If true, file picker dialogs will remember the last opened folder and open already inside of it. If false, the dialogs will always open in the default folder.\n\nWarning: This setting only applies to plugins that use the file picker through this API.");

Logger.LogDebug($"Game version {GetGameVersion()} running under {System.Threading.Thread.CurrentThread.CurrentCulture.Name} culture");

Expand Down
15 changes: 14 additions & 1 deletion src/Shared.Core/Utilities/OpenFileDialog.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;

Expand All @@ -24,6 +25,12 @@ public class OpenFileDialog
/// </summary>
public const OpenSaveFileDialgueFlags MultiFileFlags = SingleFileFlags | OpenSaveFileDialgueFlags.OFN_ALLOWMULTISELECT;

/// <summary>
/// Dictionary storing last opened path per initialDir.
/// Key is the initialDir, value is the last opened path.
/// </summary>
private static readonly Dictionary<string, string> LastOpenedPaths = new Dictionary<string, string>();

/// <inheritdoc cref="ShowDialog(string,string,string,string,OpenSaveFileDialgueFlags,string,IntPtr)"/>
public static string[] ShowDialog(string title, string initialDir, string filter, string defaultExt, OpenSaveFileDialgueFlags flags, IntPtr owner = default)
{
Expand Down Expand Up @@ -66,7 +73,10 @@ public static string[] ShowDialog(string title, string initialDir, string filter
ofn.filter = filter.Replace("|", "\0") + "\0";
ofn.fileTitle = new String(new char[MAX_FILE_LENGTH]);
ofn.maxFileTitle = ofn.fileTitle.Length;
ofn.initialDir = initialDir;
if (LastOpenedPaths.TryGetValue(initialDir, out string lastOpenedPath) && KoikatuAPI.RememberFilePickerFolder.Value)
ofn.initialDir = lastOpenedPath;
else
ofn.initialDir = initialDir;
ManlyMarco marked this conversation as resolved.
Show resolved Hide resolved
ofn.title = title;
ofn.flags = (int)flags;
if (defaultExt != null) ofn.defExt = defaultExt;
Expand All @@ -89,6 +99,7 @@ public static string[] ShowDialog(string title, string initialDir, string filter
// Save and restore working directory since GetOpenFileName changes it for no good reason
var currentWorkingDirectory = Environment.CurrentDirectory;
var run = true;

ManlyMarco marked this conversation as resolved.
Show resolved Hide resolved
BepInEx.ThreadingHelper.Instance.StartAsyncInvoke(() =>
{
// Brute force seems to be the only viable way to prevent the current directory from getting changed
Expand All @@ -103,6 +114,7 @@ public static string[] ShowDialog(string title, string initialDir, string filter

run = false;
Environment.CurrentDirectory = currentWorkingDirectory;

ManlyMarco marked this conversation as resolved.
Show resolved Hide resolved
UnityEngine.Application.runInBackground = currentRunInBackground;

if (success)
Expand All @@ -122,6 +134,7 @@ public static string[] ShowDialog(string title, string initialDir, string filter
file = Marshal.PtrToStringAuto(ofn.file);
}

LastOpenedPaths[initialDir] = Path.GetDirectoryName(selectedFilesList[0]);
if (selectedFilesList.Count == 1)
{
// Only one file selected with full path
Expand Down