113 lines
3.7 KiB
C#
113 lines
3.7 KiB
C#
using Microsoft.Win32;
|
|
|
|
namespace UI_WinForms.Components;
|
|
|
|
public partial class LoaderStart : UserControl
|
|
{
|
|
private bool areRecentProjectsVisible = false;
|
|
|
|
public LoaderStart()
|
|
{
|
|
this.InitializeComponent();
|
|
}
|
|
|
|
private static RegistryKey I18NCommanderKey => Registry.CurrentUser.OpenSubKey("Software", RegistryKeyPermissionCheck.ReadWriteSubTree)!.CreateSubKey("I18N Commander", RegistryKeyPermissionCheck.ReadWriteSubTree);
|
|
|
|
#region Recent Projects
|
|
|
|
/// <summary>
|
|
/// Gets or sets the list of recent projects without any extras e.g. it does not prune the list of projects.
|
|
/// </summary>
|
|
private static List<string> RecentProjects
|
|
{
|
|
get
|
|
{
|
|
using var regKey = LoaderStart.I18NCommanderKey;
|
|
var recentProjectsValue = regKey.GetValue("recentProjects");
|
|
return recentProjectsValue switch
|
|
{
|
|
string[] list => new List<string>(list),
|
|
_ => new List<string>(),
|
|
};
|
|
}
|
|
|
|
set
|
|
{
|
|
using var regKey = LoaderStart.I18NCommanderKey;
|
|
regKey.SetValue("recentProjects", value, RegistryValueKind.MultiString);
|
|
}
|
|
}
|
|
|
|
private static void UpdateRecentProjectsWithPruning(string latestUsedProjectPath)
|
|
{
|
|
var previousRecentList = LoaderStart.RecentProjects;
|
|
previousRecentList.Insert(0, latestUsedProjectPath);
|
|
if (previousRecentList.Count > 6)
|
|
previousRecentList = previousRecentList.Take(6).ToList();
|
|
|
|
LoaderStart.RecentProjects = previousRecentList;
|
|
}
|
|
|
|
#endregion
|
|
|
|
private void buttonOpen_Click(object sender, EventArgs e)
|
|
{
|
|
if(this.areRecentProjectsVisible)
|
|
{
|
|
this.areRecentProjectsVisible = false;
|
|
this.contextMenuRecentProjects.Close();
|
|
return;
|
|
}
|
|
|
|
var recentProjects = LoaderStart.RecentProjects;
|
|
this.contextMenuRecentProjects.Items.Clear();
|
|
this.contextMenuRecentProjects.Items.Add("Browse for project...", Resources.Icons.icons8_browse_folder_512, (innerSender, args) => this.BrowseForProject());
|
|
foreach (var recentProject in recentProjects)
|
|
{
|
|
var fileInfo = new FileInfo(recentProject);
|
|
var item = this.contextMenuRecentProjects.Items.Add($"{fileInfo.Directory.GetDirectories().Last()}/{fileInfo.Name}", Resources.Icons.icons8_document_512, (innerSender, args) => this.OpenRecentProject(innerSender));
|
|
item.Tag = recentProject;
|
|
}
|
|
|
|
var button = (sender as Button)!;
|
|
this.contextMenuRecentProjects.Show(button, 0, button.Height);
|
|
this.areRecentProjectsVisible = true;
|
|
}
|
|
|
|
private void buttonNew_Click(object sender, EventArgs e)
|
|
{
|
|
// TODO: Browse for new project's location
|
|
|
|
// TODO: Apply new project's path
|
|
LoaderStart.UpdateRecentProjectsWithPruning(string.Empty);
|
|
|
|
// TODO: Open the project
|
|
}
|
|
|
|
private void BrowseForProject()
|
|
{
|
|
// TODO: Browse for project
|
|
// TODO: Handle cancel event
|
|
|
|
// TODO: Apply chosen project's path
|
|
LoaderStart.UpdateRecentProjectsWithPruning(string.Empty);
|
|
|
|
// TODO: Open the project
|
|
}
|
|
|
|
private void OpenRecentProject(object? sender)
|
|
{
|
|
if (sender is not ToolStripItem item)
|
|
return;
|
|
|
|
var path = (item.Tag as string)!;
|
|
LoaderStart.UpdateRecentProjectsWithPruning(path);
|
|
|
|
// TODO: Open the project
|
|
}
|
|
|
|
private void contextMenuRecentProjects_Closing(object sender, ToolStripDropDownClosingEventArgs e)
|
|
{
|
|
this.areRecentProjectsVisible = false;
|
|
}
|
|
} |