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 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
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
13 changes: 12 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 (KoikatuAPI.RememberFilePickerFolder.Value)
LastOpenedPaths.TryGetValue(initialDir, out ofn.initialDir);
if (string.IsNullOrEmpty(ofn.initialDir))
ofn.initialDir = initialDir;
ofn.title = title;
ofn.flags = (int)flags;
if (defaultExt != null) ofn.defExt = defaultExt;
Expand Down Expand Up @@ -122,6 +132,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