I18NCommander/I18N Commander/UI WinForms/Components/LoaderStart.cs

118 lines
3.9 KiB
C#
Raw Normal View History

2022-06-12 14:19:32 +00:00
using System.ComponentModel;
2022-06-06 20:06:41 +00:00
using Microsoft.Win32;
namespace UI_WinForms.Components;
2022-06-12 14:19:32 +00:00
[DefaultEvent(nameof(LoadProject))]
2022-06-06 20:06:41 +00:00
public partial class LoaderStart : UserControl
{
private bool areRecentProjectsVisible = false;
public LoaderStart()
{
this.InitializeComponent();
}
2022-06-07 18:46:39 +00:00
private static RegistryKey I18NCommanderKey => Registry.CurrentUser.OpenSubKey("Software", RegistryKeyPermissionCheck.ReadWriteSubTree)!.CreateSubKey("I18N Commander", RegistryKeyPermissionCheck.ReadWriteSubTree);
2022-06-06 20:06:41 +00:00
2022-06-07 18:46:39 +00:00
#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
2022-06-06 20:06:41 +00:00
{
get
{
2022-06-07 18:46:39 +00:00
using var regKey = LoaderStart.I18NCommanderKey;
2022-06-06 20:06:41 +00:00
var recentProjectsValue = regKey.GetValue("recentProjects");
return recentProjectsValue switch
{
string[] list => new List<string>(list),
_ => new List<string>(),
};
}
set
{
2022-06-07 18:46:39 +00:00
using var regKey = LoaderStart.I18NCommanderKey;
regKey.SetValue("recentProjects", value.ToArray(), RegistryValueKind.MultiString);
2022-06-06 20:06:41 +00:00
}
}
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
2022-06-06 20:06:41 +00:00
private void buttonOpen_Click(object sender, EventArgs e)
{
if(this.areRecentProjectsVisible)
{
this.areRecentProjectsVisible = false;
this.contextMenuRecentProjects.Close();
return;
}
2022-06-07 18:46:39 +00:00
var recentProjects = LoaderStart.RecentProjects;
2022-06-06 20:06:41 +00:00
this.contextMenuRecentProjects.Items.Clear();
2022-06-07 18:46:39 +00:00
this.contextMenuRecentProjects.Items.Add("Browse for project...", Resources.Icons.icons8_browse_folder_512, (innerSender, args) => this.BrowseForProject());
2022-06-06 20:06:41 +00:00
foreach (var recentProject in recentProjects)
{
var fileInfo = new FileInfo(recentProject);
2022-06-07 18:46:39 +00:00
var item = this.contextMenuRecentProjects.Items.Add($"{fileInfo.Directory.GetDirectories().Last()}/{fileInfo.Name}", Resources.Icons.icons8_document_512, (innerSender, args) => this.OpenRecentProject(innerSender));
2022-06-06 20:06:41 +00:00
item.Tag = recentProject;
}
var button = (sender as Button)!;
this.contextMenuRecentProjects.Show(button, 0, button.Height);
this.areRecentProjectsVisible = true;
}
2022-06-07 18:46:21 +00:00
private void buttonNew_Click(object sender, EventArgs e)
2022-06-06 20:06:41 +00:00
{
2022-06-07 18:46:21 +00:00
// TODO: Browse for new project's location
// TODO: Apply new project's path
LoaderStart.UpdateRecentProjectsWithPruning(string.Empty);
2022-06-06 20:06:41 +00:00
// TODO: Open the project
2022-06-07 18:46:21 +00:00
}
2022-06-07 18:46:39 +00:00
private void BrowseForProject()
{
// TODO: Browse for project
2022-06-06 20:06:41 +00:00
// TODO: Handle cancel event
// TODO: Apply chosen project's path
LoaderStart.UpdateRecentProjectsWithPruning(string.Empty);
2022-06-07 18:46:39 +00:00
// TODO: Open the project
2022-06-06 20:06:41 +00:00
}
2022-06-07 18:46:39 +00:00
private void OpenRecentProject(object? sender)
2022-06-06 20:06:41 +00:00
{
2022-06-07 18:46:39 +00:00
if (sender is not ToolStripItem item)
2022-06-06 20:06:41 +00:00
return;
2022-06-07 18:46:39 +00:00
var path = (item.Tag as string)!;
LoaderStart.UpdateRecentProjectsWithPruning(path);
2022-06-07 18:46:39 +00:00
// TODO: Open the project
2022-06-06 20:06:41 +00:00
}
private void contextMenuRecentProjects_Closing(object sender, ToolStripDropDownClosingEventArgs e)
{
this.areRecentProjectsVisible = false;
}
2022-06-12 14:19:32 +00:00
[Category("Settings"), Description("When the user chooses a project to load.")]
public event EventHandler<string>? LoadProject;
2022-06-06 20:06:41 +00:00
}