Skip to content

Commit 0a1df79

Browse files
committed
add option to disable project folder rename, fix nullref if try to rename after added project, disable grid navigation keys during cell edit, validate project rename better and restore old value if failed, #build
1 parent 118a7dd commit 0a1df79

File tree

6 files changed

+94
-17
lines changed

6 files changed

+94
-17
lines changed

Diff for: UnityLauncherPro/App.config

+3
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@
5757
<setting name="askNameForQuickProject" serializeAs="String">
5858
<value>True</value>
5959
</setting>
60+
<setting name="enableProjectRename" serializeAs="String">
61+
<value>False</value>
62+
</setting>
6063
</UnityLauncherPro.Properties.Settings>
6164
</userSettings>
6265
</configuration>

Diff for: UnityLauncherPro/MainWindow.xaml

+1
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,7 @@
657657
<CheckBox x:Name="chkShowGitBranchColumn" Content="Show git branch column" Foreground="{DynamicResource ButtonForeground}" Checked="ChkShowGitBranchColumn_CheckedChanged" Unchecked="ChkShowGitBranchColumn_CheckedChanged" Margin="0,0,0,3" ToolTip="Shows column for project git branch info"/>
658658
<CheckBox x:Name="chkShowMissingFolderProjects" Content="Show projects that don't exist on disk" Foreground="{DynamicResource ButtonForeground}" Checked="ChkShowMissingFolderProjects_CheckedChanged" Unchecked="ChkShowMissingFolderProjects_CheckedChanged" Margin="0,0,0,3" ToolTip="List in recent projects, even if the project folder is missing"/>
659659
<CheckBox x:Name="chkAllowSingleInstanceOnly" Content="Allow single instance only" Foreground="{DynamicResource ButtonForeground}" Checked="ChkAllowSingleInstanceOnly_CheckedChanged" Unchecked="ChkAllowSingleInstanceOnly_CheckedChanged" Margin="0,0,0,3" ToolTip="Activates already running instance, instead of starting new exe (not working if app is minized to taskbar)"/>
660+
<CheckBox x:Name="chkEnableProjectRename" Content="Enable Project Renaming (Not recommended for beginners)" Foreground="{DynamicResource ButtonForeground}" Margin="0,0,0,3" ToolTip="Can use F2 to rename project folders, Note that project will disappears from list on refresh, unless you open it (since its new path, not in unity recent projects registry)" Checked="ChkEnableProjectRename_Checked" Unchecked="ChkEnableProjectRename_Checked"/>
660661
<CheckBox x:Name="chkAskNameForQuickProject" Content="Ask name for New Project" Foreground="{DynamicResource ButtonForeground}" Checked="ChkAskNameForQuickProject_Checked" Unchecked="ChkAskNameForQuickProject_Checked" Margin="0,0,0,3" ToolTip="If disabled, uses automatic quick project naming"/>
661662

662663
<StackPanel Grid.Row="3" Orientation="Horizontal">

Diff for: UnityLauncherPro/MainWindow.xaml.cs

+70-15
Original file line numberDiff line numberDiff line change
@@ -179,10 +179,10 @@ void FilterRecentProjects()
179179
ICollectionView collection = CollectionViewSource.GetDefaultView(projectsSource);
180180
collection.Filter = ProjectFilter;
181181
// set first row selected, if only 1 row
182-
//if (gridRecent.Items.Count == 1)
183-
//{
184-
// gridRecent.SelectedIndex = 0;
185-
//}
182+
if (gridRecent.Items.Count == 1)
183+
{
184+
gridRecent.SelectedIndex = 0;
185+
}
186186
}
187187

188188
void FilterUpdates()
@@ -243,6 +243,7 @@ void LoadSettings()
243243
chkAllowSingleInstanceOnly.IsChecked = Properties.Settings.Default.AllowSingleInstanceOnly;
244244
txtRootFolderForNewProjects.Text = Properties.Settings.Default.newProjectsRoot;
245245
chkAskNameForQuickProject.IsChecked = Properties.Settings.Default.askNameForQuickProject;
246+
chkEnableProjectRename.IsChecked = Properties.Settings.Default.enableProjectRename;
246247

247248
// update optional grid columns, hidden or visible
248249
gridRecent.Columns[4].Visibility = (bool)chkShowLauncherArgumentsColumn.IsChecked ? Visibility.Visible : Visibility.Collapsed;
@@ -450,7 +451,9 @@ private void BtnAddProjectFolder_Click(object sender, RoutedEventArgs e)
450451
// add to list
451452
projectsSource.Insert(0, p);
452453
gridRecent.Items.Refresh();
454+
Tools.SetFocusToGrid(gridRecent); // force focus
453455
gridRecent.SelectedIndex = 0;
456+
454457
}
455458
}
456459

@@ -523,6 +526,7 @@ private void OnWindowKeyDown(object sender, KeyEventArgs e)
523526
case Key.Down:
524527
break;
525528
case Key.F2: // edit arguments or project name
529+
if (chkEnableProjectRename.IsChecked == false) return; //if rename not enabled
526530
// if in first cell (or no cell)
527531
var cell = gridRecent.CurrentCell;
528532
if (cell.Column.DisplayIndex == 0)
@@ -796,29 +800,36 @@ private void GridRecent_PreviewKeyDown(object sender, KeyEventArgs e)
796800
switch (e.Key)
797801
{
798802
case Key.Home: // override home
803+
// if edit mode, dont override keys
804+
if (IsEditingCell(gridRecent) == true) return;
799805
gridRecent.SelectedIndex = 0;
800806
gridRecent.ScrollIntoView(gridRecent.SelectedItem);
801807
e.Handled = true;
802808
break;
803809
case Key.End: // override end
810+
// if edit mode, dont override keys
811+
if (IsEditingCell(gridRecent) == true) return;
804812
gridRecent.SelectedIndex = gridRecent.Items.Count - 1;
805813
gridRecent.ScrollIntoView(gridRecent.SelectedItem);
806814
e.Handled = true;
807815
break;
808816
case Key.F5: // refresh projects
817+
// if edit mode, dont override keys
818+
if (IsEditingCell(gridRecent) == true) return;
809819
UpdateUnityInstallationsList();
810820
RefreshRecentProjects();
811821
break;
812822
case Key.Tab:
823+
// if edit mode, dont override keys
824+
if (IsEditingCell(gridRecent) == true) return;
813825
if ((Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
814826
{
815827
e.Handled = true;
816828
}
817829
break;
818830
case Key.Return:
819-
// cancel if editing cell
820-
IEditableCollectionView itemsView = gridRecent.Items;
821-
if (itemsView.IsAddingNew || itemsView.IsEditingItem) return;
831+
// if edit mode, dont override keys
832+
if (IsEditingCell(gridRecent) == true) return;
822833
e.Handled = true;
823834
var proj = GetSelectedProject();
824835
Tools.LaunchProject(proj);
@@ -828,12 +839,19 @@ private void GridRecent_PreviewKeyDown(object sender, KeyEventArgs e)
828839
}
829840
}
830841

842+
bool IsEditingCell(DataGrid targetGrid)
843+
{
844+
IEditableCollectionView itemsView = targetGrid.Items;
845+
var res = itemsView.IsAddingNew || itemsView.IsEditingItem;
846+
return res;
847+
}
848+
831849
private void DataGridUnitys_PreviewKeyDown(object sender, KeyEventArgs e)
832850
{
833851
if (e.Key == Key.Return)
834852
{
835853
e.Handled = true;
836-
// TODO launchunity
854+
// TODO launch unity?
837855
}
838856
}
839857

@@ -1077,17 +1095,29 @@ private void GridRecent_CellEditEnding(object sender, DataGridCellEditEndingEven
10771095
{
10781096
// restore read only
10791097
e.Column.IsReadOnly = true;
1080-
// TODO validate filename
1098+
10811099
if (string.IsNullOrEmpty(newcellValue))
10821100
{
1083-
Console.WriteLine("Invalid new project name: " + newcellValue);
1101+
Console.WriteLine("Project name is null: " + newcellValue);
1102+
return;
1103+
}
1104+
1105+
// cannot allow / or \ or . as last character (otherwise might have issues going parent folder?)
1106+
if (newcellValue.EndsWith("\\") || newcellValue.EndsWith("/") || newcellValue.EndsWith("."))
1107+
{
1108+
Console.WriteLine("Project name cannot end with / or \\ or . ");
10841109
return;
10851110
}
10861111

1112+
// get new path
10871113
var newPath = Path.Combine(Directory.GetParent(path).ToString(), newcellValue);
10881114

1089-
//Console.WriteLine("Old folder: " + path);
1090-
//Console.WriteLine("New folder: " + newPath);
1115+
// check if has invalid characters for full path
1116+
if (newPath.IndexOfAny(Path.GetInvalidPathChars()) >= 0)
1117+
{
1118+
Console.WriteLine("Invalid project path: " + newPath);
1119+
return;
1120+
}
10911121

10921122
// check if same as before (need to replace mismatch slashes)
10931123
if (path.Replace('/', '\\') == newPath.Replace('/', '\\'))
@@ -1103,13 +1133,15 @@ private void GridRecent_CellEditEnding(object sender, DataGridCellEditEndingEven
11031133
return;
11041134
}
11051135

1106-
// try rename project folder
1136+
// try rename project folder by moving directory to new name
11071137
Directory.Move(path, newPath);
11081138

1109-
// check if success
1139+
// check if move was success
11101140
if (Directory.Exists(newPath))
11111141
{
1112-
proj.Path = newPath;
1142+
// force ending edit (otherwise only ends on enter or esc)
1143+
gridRecent.CommitEdit(DataGridEditingUnit.Row, true);
1144+
11131145
// TODO save to registry (otherwise not listed in recent projects, unless opened)
11141146
}
11151147
else
@@ -1160,6 +1192,13 @@ private void TxtSearchBoxUnity_TextChanged(object sender, TextChangedEventArgs e
11601192

11611193
private void GridRecent_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
11621194
{
1195+
// cancel if editing cell, because often try to double click to edit
1196+
if (IsEditingCell(gridRecent)) return;
1197+
1198+
// cancel run if double click arguments editable field
1199+
var currentColumnCell = gridRecent.CurrentCell.Column.DisplayIndex;
1200+
if (currentColumnCell == 4) return;
1201+
11631202
Tools.LaunchProject(GetSelectedProject());
11641203
}
11651204

@@ -1371,6 +1410,22 @@ private void BtnOpenCrashLogs_Click(object sender, RoutedEventArgs e)
13711410
}
13721411
}
13731412
}
1413+
1414+
// reorder grid item by index
1415+
public void MoveRecentGridItem(int to)
1416+
{
1417+
var source = (Project)gridRecent.Items[gridRecent.SelectedIndex];
1418+
projectsSource.RemoveAt(gridRecent.SelectedIndex);
1419+
projectsSource.Insert(to, source);
1420+
gridRecent.Items.Refresh();
1421+
gridRecent.SelectedIndex = to;
1422+
}
1423+
1424+
private void ChkEnableProjectRename_Checked(object sender, RoutedEventArgs e)
1425+
{
1426+
Properties.Settings.Default.enableProjectRename = (bool)chkEnableProjectRename.IsChecked;
1427+
Properties.Settings.Default.Save();
1428+
}
13741429
} // class
13751430
} //namespace
13761431

Diff for: UnityLauncherPro/Properties/Settings.Designer.cs

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: UnityLauncherPro/Properties/Settings.settings

+3
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,8 @@
5050
<Setting Name="askNameForQuickProject" Type="System.Boolean" Scope="User">
5151
<Value Profile="(Default)">True</Value>
5252
</Setting>
53+
<Setting Name="enableProjectRename" Type="System.Boolean" Scope="User">
54+
<Value Profile="(Default)">False</Value>
55+
</Setting>
5356
</Settings>
5457
</SettingsFile>

Diff for: UnityLauncherPro/Tools.cs

+5-2
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,10 @@ public static void LaunchProject(Project proj)
192192
{
193193
Console.WriteLine(e);
194194
}
195+
196+
// move as first, since its opened, disabled for now, more used to it staying in place..
197+
// MainWindow wnd = (MainWindow)Application.Current.MainWindow;
198+
// wnd.MoveRecentGridItem(0);
195199
}
196200

197201
static bool CheckCrashBackupScene(string projectPath)
@@ -806,8 +810,7 @@ public static void FastCreateProject(string version, string baseFolder, string p
806810
var p = new Project();
807811
p.Path = Path.Combine(baseFolder, newPath);
808812
p.Version = version;
809-
Tools.LaunchProject(p);
810-
813+
LaunchProject(p);
811814
} // FastCreateProject
812815

813816

0 commit comments

Comments
 (0)