Skip to content

Commit eb00de0

Browse files
committed
new project dialog: fix errors if changed selected editor during template loading, set focus to grid at start (so can use arrow keys up/down)
1 parent ce7337a commit eb00de0

File tree

2 files changed

+58
-52
lines changed

2 files changed

+58
-52
lines changed

UnityLauncherPro/NewProject.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
</Grid.ColumnDefinitions>
1717
<StackPanel Margin="10,3">
1818
<Label Content="Unity Version" Foreground="{DynamicResource ThemeButtonForeground}" Margin="0,0,0,3" Padding="5,5,5,3" />
19-
<DataGrid x:Name="gridAvailableVersions" KeyboardNavigation.TabNavigation = "None" SelectionMode="Single" Height="230" Margin="0" HeadersVisibility="None" AutoGenerateColumns="False" IsSynchronizedWithCurrentItem="True" Foreground="{DynamicResource ThemeButtonForeground}" Background="{DynamicResource ThemeMainBackgroundColor}" SelectionChanged="GridAvailableVersions_SelectionChanged" IsTabStop="True" TabIndex="1" Loaded="GridAvailableVersions_Loaded" EnableRowVirtualization="False" VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Disabled" PreviewMouseDoubleClick="gridAvailableVersions_PreviewMouseDoubleClick" CanUserAddRows="False" CanUserReorderColumns="False" CanUserResizeColumns="False" CanUserSortColumns="False" CanUserResizeRows="False" CanUserDeleteRows="False">
19+
<DataGrid x:Name="gridAvailableVersions" KeyboardNavigation.TabNavigation = "None" SelectionMode="Single" Height="230" Margin="0" HeadersVisibility="None" AutoGenerateColumns="False" IsSynchronizedWithCurrentItem="True" Foreground="{DynamicResource ThemeButtonForeground}" Background="{DynamicResource ThemeMainBackgroundColor}" SelectionChanged="GridAvailableVersions_SelectionChanged" IsTabStop="True" TabIndex="0" Loaded="GridAvailableVersions_Loaded" EnableRowVirtualization="False" VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Disabled" PreviewMouseDoubleClick="gridAvailableVersions_PreviewMouseDoubleClick" CanUserAddRows="False" CanUserReorderColumns="False" CanUserResizeColumns="False" CanUserSortColumns="False" CanUserResizeRows="False" CanUserDeleteRows="False">
2020
<DataGrid.Columns>
2121
<DataGridTextColumn Header="Version" Binding="{Binding Version}" IsReadOnly="True" CanUserResize="False" MinWidth="80" />
2222
<DataGridTextColumn Header="Platforms" Binding="{Binding PlatformsCombined}" IsReadOnly="True" CanUserResize="False" MinWidth="270" />

UnityLauncherPro/NewProject.xaml.cs

Lines changed: 57 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,21 @@ public NewProject(string unityVersion, string suggestedName, string targetFolder
108108
}
109109

110110
// select projectname text so can overwrite if needed
111-
txtNewProjectName.Focus();
112-
txtNewProjectName.SelectAll();
113111
newProjectName = txtNewProjectName.Text;
114112

113+
if (nameIsLocked)
114+
{
115+
Tools.SetFocusToGrid(gridAvailableVersions);
116+
}
117+
else
118+
{
119+
txtNewProjectName.Focus();
120+
txtNewProjectName.SelectAll();
121+
}
122+
115123
isInitializing = false;
124+
125+
116126
} // NewProject
117127

118128
private void LoadSettings()
@@ -505,79 +515,76 @@ private void btnCreateMissingFolder_Click(object sender, RoutedEventArgs e)
505515
}
506516
}
507517

518+
// Add this static field to the class (near other static fields)
519+
private static readonly HttpClient _httpClient = new HttpClient();
520+
508521
private async Task LoadOnlineTemplatesAsync(string baseVersion, CancellationToken cancellationToken = default)
509522
{
510523
try
511524
{
512-
using (var client = new HttpClient())
513-
{
514-
client.DefaultRequestHeaders.Add("Accept", "application/json");
525+
_httpClient.DefaultRequestHeaders.Remove("Accept");
526+
_httpClient.DefaultRequestHeaders.Add("Accept", "application/json");
515527

516-
var graphqlJson = "{\"query\":\"fragment TemplateEntity on Template { __typename name packageName description type buildPlatforms renderPipeline previewImage { url } versions { name tarball { url } } } query HUB__getTemplates($limit: Int! $skip: Int! $orderBy: TemplateOrder! $supportedUnityEditorVersions: [String!]!) { getTemplates(limit: $limit skip: $skip orderBy: $orderBy supportedUnityEditorVersions: $supportedUnityEditorVersions) { edges { node { ...TemplateEntity } } } }\",\"variables\":{\"limit\":40,\"skip\":0,\"orderBy\":\"WEIGHTED_DESC\",\"supportedUnityEditorVersions\":[\"" + baseVersion + "\"]}}";
528+
var graphqlJson = "{\"query\":\"fragment TemplateEntity on Template { __typename name packageName description type buildPlatforms renderPipeline previewImage { url } versions { name tarball { url } } } query HUB__getTemplates($limit: Int! $skip: Int! $orderBy: TemplateOrder! $supportedUnityEditorVersions: [String!]!) { getTemplates(limit: $limit skip: $skip orderBy: $orderBy supportedUnityEditorVersions: $supportedUnityEditorVersions) { edges { node { ...TemplateEntity } } } }\",\"variables\":{\"limit\":40,\"skip\":0,\"orderBy\":\"WEIGHTED_DESC\",\"supportedUnityEditorVersions\":[\"" + baseVersion + "\"]}}";
517529

518-
var content = new StringContent(graphqlJson, Encoding.UTF8, "application/json");
530+
var content = new StringContent(graphqlJson, Encoding.UTF8, "application/json");
519531

520-
// Check for cancellation before making request
521-
if (cancellationToken.IsCancellationRequested) return;
532+
// Check for cancellation before making request
533+
if (cancellationToken.IsCancellationRequested) return;
534+
535+
var response = await _httpClient.PostAsync("https://live-platform-api.prd.ld.unity3d.com/graphql", content, cancellationToken);
522536

523-
var response = await client.PostAsync("https://live-platform-api.prd.ld.unity3d.com/graphql", content, cancellationToken);
537+
// Check for cancellation after request
538+
if (cancellationToken.IsCancellationRequested) return;
524539

525-
// Check for cancellation after request
540+
if (response.IsSuccessStatusCode)
541+
{
542+
var responseString = await response.Content.ReadAsStringAsync();
543+
544+
// Check for cancellation before parsing
526545
if (cancellationToken.IsCancellationRequested) return;
527546

528-
if (response.IsSuccessStatusCode)
529-
{
530-
var responseString = await response.Content.ReadAsStringAsync();
547+
var templates = ParseTemplatesFromJson(responseString);
531548

532-
// Check for cancellation before parsing
549+
// Download preview images to bypass WPF's ClickOnce security check on HTTPS URIs
550+
foreach (var template in templates)
551+
{
533552
if (cancellationToken.IsCancellationRequested) return;
534553

535-
var templates = ParseTemplatesFromJson(responseString);
536-
537-
// Download preview images to bypass WPF's ClickOnce security check on HTTPS URIs
538-
foreach (var template in templates)
554+
try
539555
{
540-
if (cancellationToken.IsCancellationRequested) return;
541-
542-
try
556+
if (!string.IsNullOrEmpty(template.PreviewImageURL) && !template.PreviewImageURL.StartsWith("pack://"))
543557
{
544-
if (!string.IsNullOrEmpty(template.PreviewImageURL) && !template.PreviewImageURL.StartsWith("pack://"))
545-
{
546-
var imageData = await client.GetByteArrayAsync(template.PreviewImageURL);
547-
var bitmap = new BitmapImage();
548-
bitmap.BeginInit();
549-
bitmap.CacheOption = BitmapCacheOption.OnLoad;
550-
bitmap.StreamSource = new MemoryStream(imageData);
551-
bitmap.EndInit();
552-
if (bitmap.CanFreeze) bitmap.Freeze();
553-
template.PreviewImage = bitmap;
554-
}
555-
}
556-
catch
557-
{
558-
// Failed to download preview image, leave PreviewImage as null
558+
var imageData = await _httpClient.GetByteArrayAsync(template.PreviewImageURL);
559+
var bitmap = new BitmapImage();
560+
bitmap.BeginInit();
561+
bitmap.CacheOption = BitmapCacheOption.OnLoad;
562+
bitmap.StreamSource = new MemoryStream(imageData);
563+
bitmap.EndInit();
564+
if (bitmap.CanFreeze) bitmap.Freeze();
565+
template.PreviewImage = bitmap;
559566
}
560567
}
561-
562-
// Update UI on dispatcher thread only if not cancelled
563-
if (!cancellationToken.IsCancellationRequested)
568+
catch
564569
{
565-
Dispatcher.Invoke(() =>
566-
{
567-
// Only set ItemsSource, don't touch Items
568-
listOnlineTemplates.ItemsSource = templates;
569-
});
570+
// Failed to download preview image, leave PreviewImage as null
570571
}
571572
}
572-
else
573+
574+
// Update UI on dispatcher thread only if not cancelled
575+
if (!cancellationToken.IsCancellationRequested)
573576
{
574-
Console.WriteLine($"GraphQL request failed: {response.StatusCode}");
575-
if (!cancellationToken.IsCancellationRequested)
577+
Dispatcher.Invoke(() =>
576578
{
577-
//LoadFallbackTemplates();
578-
}
579+
// Only set ItemsSource, don't touch Items
580+
listOnlineTemplates.ItemsSource = templates;
581+
});
579582
}
580583
}
584+
else
585+
{
586+
Console.WriteLine($"GraphQL request failed: {response.StatusCode}");
587+
}
581588
}
582589
catch (OperationCanceledException)
583590
{
@@ -589,7 +596,6 @@ private async Task LoadOnlineTemplatesAsync(string baseVersion, CancellationToke
589596
if (!cancellationToken.IsCancellationRequested)
590597
{
591598
Console.WriteLine($"Error loading online templates: {ex.Message}");
592-
//LoadFallbackTemplates();
593599
}
594600
}
595601
}

0 commit comments

Comments
 (0)