2022-06-06 20:06:41 +00:00
using Microsoft.Win32 ;
namespace UI_WinForms.Components ;
public partial class LoaderStart : UserControl
{
private bool areRecentProjectsVisible = false ;
public LoaderStart ( )
{
this . InitializeComponent ( ) ;
}
private RegistryKey I18NCommanderKey = > Registry . CurrentUser . OpenSubKey ( "Software" , RegistryKeyPermissionCheck . ReadWriteSubTree ) ! . CreateSubKey ( "I18N Commander" , RegistryKeyPermissionCheck . ReadWriteSubTree ) ;
private List < string > RecentProjects
{
get
{
using var regKey = this . I18NCommanderKey ;
var recentProjectsValue = regKey . GetValue ( "recentProjects" ) ;
return recentProjectsValue switch
{
string [ ] list = > new List < string > ( list ) ,
_ = > new List < string > ( ) ,
} ;
}
set
{
using var regKey = this . I18NCommanderKey ;
regKey . SetValue ( "recentProjects" , value , RegistryValueKind . MultiString ) ;
}
}
2022-06-07 18:44:17 +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 ;
}
var recentProjects = this . 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 ;
}
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
2022-06-07 18:44:17 +00:00
// 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-06 20:06:41 +00:00
// TODO: Handle cancel event
2022-06-07 18:44:17 +00:00
// TODO: Apply chosen project's path
LoaderStart . UpdateRecentProjectsWithPruning ( string . Empty ) ;
2022-06-06 20:06:41 +00:00
}
private void openRecentProject ( object? sender )
{
if ( sender is null )
return ;
if ( sender is ToolStripItem item )
{
var path = ( item . Tag as string ) ! ;
2022-06-07 18:44:17 +00:00
LoaderStart . UpdateRecentProjectsWithPruning ( path ) ;
2022-06-06 20:06:41 +00:00
// TODO: Open the project
}
}
private void contextMenuRecentProjects_Closing ( object sender , ToolStripDropDownClosingEventArgs e )
{
this . areRecentProjectsVisible = false ;
}
}