Skip to content

Commit c568cc4

Browse files
committedFeb 26, 2023
Projects: Fix adding existing project and creating new project (select correct just added row, fix red path color, fix null ref if refresh) fixes #125
1 parent 67bfea5 commit c568cc4

File tree

2 files changed

+32
-16
lines changed

2 files changed

+32
-16
lines changed
 

‎UnityLauncherPro/MainWindow.xaml.cs

+11-5
Original file line numberDiff line numberDiff line change
@@ -731,7 +731,7 @@ public void RefreshRecentProjects()
731731
// take currently selected project row
732732
lastSelectedProjectIndex = gridRecent.SelectedIndex;
733733
// rescan recent projects
734-
projectsSource = GetProjects.Scan(getGitBranch: (bool)chkShowGitBranchColumn.IsChecked, getPlasticBranch: (bool)chkCheckPlasticBranch.IsChecked, getArguments: (bool)chkShowLauncherArgumentsColumn.IsChecked, showMissingFolders: (bool)chkShowMissingFolderProjects.IsChecked, showTargetPlatform: (bool)chkShowPlatform.IsChecked, AllProjectPaths: Properties.Settings.Default.projectPaths);
734+
projectsSource = GetProjects.Scan(getGitBranch: (bool)chkShowGitBranchColumn.IsChecked, getPlasticBranch: (bool)chkCheckPlasticBranch.IsChecked, getArguments: (bool)chkShowLauncherArgumentsColumn.IsChecked, showMissingFolders: (bool)chkShowMissingFolderProjects.IsChecked, showTargetPlatform: (bool)chkShowPlatform.IsChecked, AllProjectPaths: Settings.Default.projectPaths);
735735
gridRecent.ItemsSource = projectsSource;
736736

737737
// fix sorting on refresh
@@ -787,9 +787,13 @@ private void BtnAddProjectFolder_Click(object sender, RoutedEventArgs e)
787787
if (string.IsNullOrEmpty(folder) == false)
788788
{
789789
var proj = GetNewProjectData(folder);
790-
AddNewProjectToList(proj);
790+
//AddNewProjectToList(proj);
791+
Tools.AddProjectToHistory(proj.Path);
791792
// clear search, so can see added project
792793
txtSearchBox.Text = "";
794+
RefreshRecentProjects();
795+
// NOTE 0 works for sort-by-date only
796+
Tools.SetFocusToGrid(gridRecent, 0);
793797
}
794798
}
795799

@@ -799,7 +803,7 @@ Project GetNewProjectData(string folder)
799803
p.Path = folder;
800804
p.Title = Path.GetFileName(folder);
801805
p.Version = Tools.GetProjectVersion(folder);
802-
p.Arguments = Tools.ReadCustomProjectData(folder, MainWindow.launcherArgumentsFile);
806+
p.Arguments = Tools.ReadCustomProjectData(folder, launcherArgumentsFile);
803807
if ((bool)chkShowPlatform.IsChecked == true) p.TargetPlatform = Tools.GetTargetPlatform(folder);
804808
if ((bool)chkShowGitBranchColumn.IsChecked == true) p.GITBranch = Tools.ReadGitBranchInfo(folder);
805809
return p;
@@ -808,9 +812,11 @@ Project GetNewProjectData(string folder)
808812
void AddNewProjectToList(Project proj)
809813
{
810814
projectsSource.Insert(0, proj);
811-
gridRecent.Items.Refresh();
812-
Tools.SetFocusToGrid(gridRecent); // force focus
813815
gridRecent.SelectedIndex = 0;
816+
Tools.SetFocusToGrid(gridRecent);
817+
// force refresh
818+
txtSearchBox.Text = proj.Title;
819+
txtSearchBox.Text = "";
814820
}
815821

816822
private void BtnClose_Click(object sender, RoutedEventArgs e)

‎UnityLauncherPro/Tools.cs

+21-11
Original file line numberDiff line numberDiff line change
@@ -1167,14 +1167,14 @@ public static bool SaveCustomProjectData(string projectPath, string customFile,
11671167

11681168
public static bool HasFocus(DependencyObject obj, Control control, bool checkChildren)
11691169
{
1170-
var oFocused = System.Windows.Input.FocusManager.GetFocusedElement(obj) as DependencyObject;
1170+
var oFocused = FocusManager.GetFocusedElement(obj) as DependencyObject;
11711171
if (!checkChildren)
11721172
return oFocused == control;
11731173
while (oFocused != null)
11741174
{
11751175
if (oFocused == control)
11761176
return true;
1177-
oFocused = System.Windows.Media.VisualTreeHelper.GetParent(oFocused);
1177+
oFocused = VisualTreeHelper.GetParent(oFocused);
11781178
}
11791179
return false;
11801180
}
@@ -1201,23 +1201,32 @@ public static void SetFocusToGrid(DataGrid targetGrid, int index = -1)
12011201
if (row == null)
12021202
{
12031203
targetGrid.UpdateLayout();
1204-
// scroll to view if outside
1205-
targetGrid.ScrollIntoView(targetGrid.Items[index]);
1206-
row = (DataGridRow)targetGrid.ItemContainerGenerator.ContainerFromIndex(index);
1204+
if (index < targetGrid.Items.Count)
1205+
{
1206+
// scroll selected into view
1207+
targetGrid.ScrollIntoView(targetGrid.Items[index]);
1208+
row = (DataGridRow)targetGrid.ItemContainerGenerator.ContainerFromIndex(index);
1209+
}
1210+
else
1211+
{
1212+
Console.WriteLine("selected row out of bounds: " + index);
1213+
}
12071214
}
12081215
// NOTE does this causes move below?
12091216
//row.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
1210-
row.MoveFocus(new TraversalRequest(FocusNavigationDirection.Up)); // works better than Up
1211-
1212-
row.Focus();
1213-
Keyboard.Focus(row);
1217+
if (row != null)
1218+
{
1219+
row.MoveFocus(new TraversalRequest(FocusNavigationDirection.Up)); // works better than Up
1220+
row.Focus();
1221+
Keyboard.Focus(row);
1222+
}
12141223
}
12151224

12161225
public static string BrowseForOutputFolder(string title, string initialDirectory = null)
12171226
{
12181227
// https://stackoverflow.com/a/50261723/5452781
12191228
// Create a "Save As" dialog for selecting a directory (HACK)
1220-
var dialog = new Microsoft.Win32.SaveFileDialog();
1229+
var dialog = new SaveFileDialog();
12211230
if (initialDirectory != null) dialog.InitialDirectory = initialDirectory;
12221231
dialog.Title = title;
12231232
dialog.Filter = "Project Folder|*.Folder"; // Prevents displaying files
@@ -1297,11 +1306,12 @@ public static Project FastCreateProject(string version, string baseFolder, strin
12971306
// launch empty project
12981307
var proj = new Project();
12991308
proj.Title = projectName;
1300-
proj.Path = Path.Combine(baseFolder, newPath);
1309+
proj.Path = Path.Combine(baseFolder, newPath).Replace("\\", "/");
13011310
proj.Version = version;
13021311
proj.TargetPlatforms = platformsForThisUnity;
13031312
proj.TargetPlatform = platform;
13041313
proj.Modified = DateTime.Now;
1314+
proj.folderExists = true; // have to set this value, so item is green on list
13051315

13061316
var proc = LaunchProject(proj, null, useInitScript);
13071317
ProcessHandler.Add(proj, proc);

0 commit comments

Comments
 (0)