Skip to content

Commit f58362d

Browse files
committed
new project: add context menu to templates (delete and explore)
1 parent 7d60f57 commit f58362d

2 files changed

Lines changed: 62 additions & 6 deletions

File tree

‎UnityLauncherPro/NewProject.xaml‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -187,11 +187,21 @@
187187
<DataTemplate>
188188
<StackPanel Orientation="Vertical" Margin="3">
189189
<Grid>
190-
<Image Source="{Binding PreviewImage}"
191-
Margin="0,0,0,5"
192-
ToolTip="{Binding Description}"
193-
Stretch="Uniform"
194-
HorizontalAlignment="Stretch"/>
190+
<Image Source="{Binding PreviewImage}"
191+
Margin="0,0,0,5"
192+
ToolTip="{Binding Description}"
193+
Stretch="Uniform"
194+
HorizontalAlignment="Stretch">
195+
<Image.ContextMenu>
196+
<ContextMenu DataContext="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}">
197+
<MenuItem Header="Delete template"
198+
IsEnabled="{Binding IsDownloaded}"
199+
Click="DeleteTemplate_Click" />
200+
<MenuItem Header="Open in Explorer"
201+
Click="OpenTemplateInExplorer_Click" />
202+
</ContextMenu>
203+
</Image.ContextMenu>
204+
</Image>
195205
<Button x:Name="btnDownloadTemplate"
196206
ToolTip="Download Template"
197207
Content="📥"

‎UnityLauncherPro/NewProject.xaml.cs‎

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1048,6 +1048,53 @@ private void listOnlineTemplates_SelectionChanged(object sender, SelectionChange
10481048
UpdateCreateButtonsEnabledState();
10491049
}
10501050

1051+
private void DeleteTemplate_Click(object sender, RoutedEventArgs e)
1052+
{
1053+
if (!(sender is MenuItem menuItem) || !(menuItem.DataContext is OnlineTemplateItem template)) return;
1054+
if (string.IsNullOrEmpty(template.TarBallURL)) return;
1055+
1056+
string templatesPath = Path.Combine(
1057+
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
1058+
"UnityHub", "Templates");
1059+
string fileName;
1060+
1061+
try
1062+
{
1063+
fileName = Path.GetFileName(new Uri(template.TarBallURL).LocalPath);
1064+
}
1065+
catch
1066+
{
1067+
return;
1068+
}
1069+
1070+
if (string.IsNullOrEmpty(fileName)) return;
1071+
1072+
string filePath = Path.Combine(templatesPath, fileName);
1073+
try
1074+
{
1075+
if (File.Exists(filePath)) File.Delete(filePath);
1076+
template.IsDownloaded = false;
1077+
1078+
if (listOnlineTemplates.SelectedItem == template)
1079+
{
1080+
btnCreateNewProject.Content = "Download Template First >";
1081+
}
1082+
}
1083+
catch (Exception ex)
1084+
{
1085+
MessageBox.Show(this, "Could not delete the template:\n" + ex.Message,
1086+
"Delete template", MessageBoxButton.OK, MessageBoxImage.Error);
1087+
}
1088+
}
1089+
1090+
private void OpenTemplateInExplorer_Click(object sender, RoutedEventArgs e)
1091+
{
1092+
string templatesPath = Path.Combine(
1093+
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
1094+
"UnityHub", "Templates");
1095+
Tools.LaunchExplorer(templatesPath);
1096+
}
1097+
10511098
private async void btnDownloadTemplate_Click(object sender, RoutedEventArgs e)
10521099
{
10531100
var button = sender as Button;
@@ -1361,6 +1408,5 @@ private void txtTokenInput_PasswordChanged(object sender, RoutedEventArgs e)
13611408
btnAuthorizeToken.IsEnabled = tokenSeemsOK;
13621409
}
13631410

1364-
13651411
} // class NewProject
13661412
} // namespace UnityLauncherPro

0 commit comments

Comments
 (0)