Skip to content

Commit 95d4b3c

Browse files
committed
mainwindow: fix arrowkeys for next/prev row. UpgradeDialog: add open release notes, add download in browser, add keyboard usage, add pick nearest version
1 parent fa6210a commit 95d4b3c

File tree

5 files changed

+478
-60
lines changed

5 files changed

+478
-60
lines changed

UnityLauncherPro/MainWindow.xaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
</Style>
7373

7474
<!-- datagrid hide selected cell borders -->
75-
<Style TargetType="{x:Type DataGridCell}">
75+
<!--<Style TargetType="{x:Type DataGridCell}">
7676
<Setter Property="BorderBrush" Value="Transparent" />
7777
<Setter Property="FocusVisualStyle" Value="{x:Null}" />
7878
<Setter Property="Margin" Value="0,0.5,0,0.5" />
@@ -84,7 +84,7 @@
8484
<Setter Property="Background" Value="Transparent" />
8585
</Trigger>
8686
</Style.Triggers>
87-
</Style>
87+
</Style>-->
8888

8989
<!-- custom buttons -->
9090
<Style x:Key="CustomButton" TargetType="Button">
@@ -291,7 +291,7 @@
291291
<Button Style="{StaticResource CustomButton}" ToolTip="Add existing project" x:Name="btnAddProjectFolder" Content="Add Project.." Height="22" Width="78" HorizontalAlignment="Right" VerticalAlignment="Top" Background="#FF3F3F46" Foreground="#FFC1C1C1" Margin="0,4,34,0" Click="BtnAddProjectFolder_Click" BorderBrush="{x:Null}" />
292292
<Button Style="{StaticResource CustomButton}" ToolTip="Refresh list (F5)" x:Name="btnRefreshProjectList" Content="" Height="22" Width="22" HorizontalAlignment="Right" VerticalAlignment="Top" FontSize="16" Background="#FF3F3F46" Foreground="#FFC1C1C1" Margin="0,4,3,0" Padding="1,-2,1,1" BorderBrush="{x:Null}" Click="BtnRefreshProjectList_Click"/>
293293

294-
<DataGrid x:Name="gridRecent" SelectionMode="Single" Margin="4,30,2,42" Background="{x:Null}" BorderBrush="{x:Null}" ColumnHeaderStyle="{StaticResource HeaderStyle}" Padding="0" HorizontalScrollBarVisibility="Disabled" HeadersVisibility="Column" Foreground="#FFD8D8D8" HorizontalGridLinesBrush="#4C000000" VerticalGridLinesBrush="#19000000" AutoGenerateColumns="False">
294+
<DataGrid x:Name="gridRecent" SelectionMode="Single" KeyboardNavigation.TabNavigation="Once" Margin="4,30,2,42" Background="{x:Null}" BorderBrush="{x:Null}" ColumnHeaderStyle="{StaticResource HeaderStyle}" Padding="0" HorizontalScrollBarVisibility="Disabled" HeadersVisibility="Column" Foreground="#FFD8D8D8" HorizontalGridLinesBrush="#4C000000" VerticalGridLinesBrush="#19000000" AutoGenerateColumns="False" PreviewKeyDown="GridRecent_PreviewKeyDown">
295295
<DataGrid.Columns>
296296
<DataGridTextColumn Binding="{Binding Title}" ClipboardContentBinding="{x:Null}" Header="Project" IsReadOnly="True" Width="150"/>
297297
<DataGridTextColumn Binding="{Binding Version}" ClipboardContentBinding="{x:Null}" Header="Version" IsReadOnly="True" Width="72">

UnityLauncherPro/MainWindow.xaml.cs

Lines changed: 13 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ void Start()
7373
notifyIcon.Icon = new Icon(System.Windows.Application.GetResourceStream(new Uri("pack://application:,,,/Images/icon.ico")).Stream);
7474
notifyIcon.MouseClick += new System.Windows.Forms.MouseEventHandler(NotifyIcon_MouseClick);
7575

76+
gridRecent.Focus();
77+
gridRecent.SelectedIndex = 0;
78+
7679
}
7780

7881
void NotifyIcon_MouseClick(object sender, System.Windows.Forms.MouseEventArgs e)
@@ -217,6 +220,10 @@ private void OnWindowKeyDown(object sender, KeyEventArgs e)
217220
txtSearchBoxUpdates.Text = "";
218221
}
219222
break;
223+
case Key.Up:
224+
break;
225+
case Key.Down:
226+
break;
220227
default: // any key
221228

222229
// activate searchbar if not active and we are in tab#1
@@ -522,48 +529,15 @@ void DisplayUpgradeDialog(Project proj)
522529
{
523530
Console.WriteLine("results = " + results);
524531
}
532+
}
525533

526-
527-
/*
528-
// display upgrade dialog (version selector)
529-
Form2 upgradeDialog = new Form2();
530-
Form2.currentVersion = currentVersion;
531-
532-
// check what user selected
533-
var results = upgradeDialog.ShowDialog(this);
534-
switch (results)
535-
{
536-
case DialogResult.Ignore: // view release notes page
537-
Tools.OpenReleaseNotes(currentVersion);
538-
// display window again for now..
539-
DisplayUpgradeDialog(currentVersion, projectPath, launchProject, commandLineArguments);
540-
break;
541-
case DialogResult.Cancel: // cancelled
542-
SetStatus("Cancelled project upgrade");
543-
break;
544-
case DialogResult.Retry: // download and install missing version
545-
SetStatus("Download and install missing version " + currentVersion);
546-
string url = Tools.GetUnityReleaseURL(currentVersion);
547-
if (string.IsNullOrEmpty(url) == false)
548-
{
549-
DownloadInBrowser(url, currentVersion);
550-
}
551-
else
552-
{
553-
SetStatus("Failed getting Unity Installer URL");
554-
}
555-
break;
556-
case DialogResult.Yes: // upgrade
557-
SetStatus("Upgrading project to " + Form2.currentVersion);
558-
if (launchProject == true) LaunchProject(projectPath, Form2.currentVersion);
559-
break;
560-
default:
561-
Console.WriteLine("Unknown DialogResult: " + results);
562-
break;
563-
}
564-
upgradeDialog.Close();*/
534+
// need to manually move into next/prev rows? https://stackoverflow.com/a/11652175/5452781
535+
private void GridRecent_PreviewKeyDown(object sender, KeyEventArgs e)
536+
{
537+
Tools.HandleDataGridScrollKeys(sender, e);
565538
}
566539

540+
567541
} // class
568542
} //namespace
569543

UnityLauncherPro/Tools.cs

Lines changed: 244 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Diagnostics;
34
using System.IO;
5+
using System.Linq;
6+
using System.Net;
47
using System.Text;
8+
using System.Text.RegularExpressions;
9+
using System.Windows.Controls;
10+
using System.Windows.Input;
511

612
namespace UnityLauncherPro
713
{
@@ -149,6 +155,244 @@ public static bool LaunchExe(string path)
149155
}
150156

151157

158+
public static string GetUnityReleaseURL(string version)
159+
{
160+
string url = "";
161+
if (VersionIsArchived(version))
162+
{
163+
// remove f#
164+
version = Regex.Replace(version, @"f.", "", RegexOptions.IgnoreCase);
165+
166+
string padding = "unity-";
167+
string whatsnew = "whats-new";
168+
169+
if (version.Contains("5.6")) padding = "";
170+
if (version.Contains("2017.1")) whatsnew = "whatsnew";
171+
if (version.Contains("2018.2")) whatsnew = "whatsnew";
172+
if (version.Contains("2018.3")) padding = "";
173+
if (version.Contains("2018.1")) whatsnew = "whatsnew"; // doesnt work
174+
if (version.Contains("2017.4.")) padding = ""; // doesnt work for all versions
175+
if (version.Contains("2018.4.")) padding = "";
176+
if (version.Contains("2019")) padding = "";
177+
url = "https://unity3d.com/unity/" + whatsnew + "/" + padding + version;
178+
}
179+
else
180+
if (VersionIsPatch(version))
181+
{
182+
url = "https://unity3d.com/unity/qa/patch-releases/" + version;
183+
}
184+
else
185+
if (VersionIsBeta(version))
186+
{
187+
url = "https://unity3d.com/unity/beta/" + version;
188+
}
189+
else
190+
if (VersionIsAlpha(version))
191+
{
192+
url = "https://unity3d.com/unity/alpha/" + version;
193+
}
194+
195+
Console.WriteLine(url);
196+
197+
return url;
198+
}
199+
200+
// if version contains *f* its archived version
201+
public static bool VersionIsArchived(string version)
202+
{
203+
return version.Contains("f");
204+
}
205+
206+
public static bool VersionIsPatch(string version)
207+
{
208+
return version.Contains("p");
209+
}
210+
211+
public static bool VersionIsBeta(string version)
212+
{
213+
return version.Contains("b");
214+
}
215+
216+
public static bool VersionIsAlpha(string version)
217+
{
218+
return version.Contains("a");
219+
}
220+
221+
// open release notes page in browser
222+
public static bool OpenReleaseNotes(string version)
223+
{
224+
bool result = false;
225+
var url = Tools.GetUnityReleaseURL(version);
226+
if (string.IsNullOrEmpty(url) == false)
227+
{
228+
Process.Start(url);
229+
result = true;
230+
}
231+
else
232+
{
233+
}
234+
return result;
235+
}
236+
237+
public static void DownloadInBrowser(string url, string version)
238+
{
239+
string exeURL = ParseDownloadURLFromWebpage(version);
240+
241+
if (string.IsNullOrEmpty(exeURL) == false)
242+
{
243+
//SetStatus("Download installer in browser: " + exeURL);
244+
Process.Start(exeURL);
245+
}
246+
else // not found
247+
{
248+
//SetStatus("Error> Cannot find installer executable ... opening website instead");
249+
url = "https://unity3d.com/get-unity/download/archive";
250+
Process.Start(url + "#installer-not-found---version-" + version);
251+
}
252+
}
253+
254+
// parse Unity installer exe from release page
255+
// thanks to https://github.com/softfruit
256+
static string ParseDownloadURLFromWebpage(string version)
257+
{
258+
string url = "";
259+
260+
using (WebClient client = new WebClient())
261+
{
262+
// get correct page url
263+
string website = "https://unity3d.com/get-unity/download/archive";
264+
if (Tools.VersionIsPatch(version)) website = "https://unity3d.com/unity/qa/patch-releases";
265+
if (Tools.VersionIsBeta(version)) website = "https://unity3d.com/unity/beta/" + version;
266+
if (Tools.VersionIsAlpha(version)) website = "https://unity3d.com/unity/alpha/" + version;
267+
268+
// download html
269+
string sourceHTML = client.DownloadString(website);
270+
string[] lines = sourceHTML.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);
271+
272+
// patch version download assistant finder
273+
if (Tools.VersionIsPatch(version))
274+
{
275+
for (int i = 0; i < lines.Length; i++)
276+
{
277+
if (lines[i].Contains("UnityDownloadAssistant-" + version + ".exe"))
278+
{
279+
int start = lines[i].IndexOf('"') + 1;
280+
int end = lines[i].IndexOf('"', start);
281+
url = lines[i].Substring(start, end - start);
282+
break;
283+
}
284+
}
285+
}
286+
else if (Tools.VersionIsArchived(version))
287+
{
288+
// archived version download assistant finder
289+
for (int i = 0; i < lines.Length; i++)
290+
{
291+
// find line where full installer is (from archive page)
292+
if (lines[i].Contains("UnitySetup64-" + version))
293+
{
294+
// take previous line, which contains download assistant url
295+
string line = lines[i - 1];
296+
int start = line.IndexOf('"') + 1;
297+
int end = line.IndexOf('"', start);
298+
url = @"https://unity3d.com" + line.Substring(start, end - start);
299+
break;
300+
}
301+
}
302+
}
303+
else // alpha or beta version download assistant finder
304+
{
305+
for (int i = 0; i < lines.Length; i++)
306+
{
307+
if (lines[i].Contains("UnityDownloadAssistant.exe"))
308+
{
309+
int start = lines[i].IndexOf('"') + 1;
310+
int end = lines[i].IndexOf('"', start);
311+
url = lines[i].Substring(start, end - start) + "#version=" + version;
312+
break;
313+
}
314+
}
315+
}
316+
}
317+
318+
// didnt find installer
319+
if (string.IsNullOrEmpty(url))
320+
{
321+
//SetStatus("Cannot find UnityDownloadAssistant.exe for this version.");
322+
}
323+
return url;
324+
}
325+
326+
327+
public static string FindNearestVersion(string currentVersion, List<string> allAvailable)
328+
{
329+
if (currentVersion.Contains("2019"))
330+
{
331+
return FindNearestVersionFromSimilarVersions(currentVersion, allAvailable.Where(x => x.Contains("2019")));
332+
}
333+
if (currentVersion.Contains("2018"))
334+
{
335+
return FindNearestVersionFromSimilarVersions(currentVersion, allAvailable.Where(x => x.Contains("2018")));
336+
}
337+
if (currentVersion.Contains("2017"))
338+
{
339+
return FindNearestVersionFromSimilarVersions(currentVersion, allAvailable.Where(x => x.Contains("2017")));
340+
}
341+
return FindNearestVersionFromSimilarVersions(currentVersion, allAvailable.Where(x => !x.Contains("2017")));
342+
}
343+
344+
private static string FindNearestVersionFromSimilarVersions(string version, IEnumerable<string> allAvailable)
345+
{
346+
Dictionary<string, string> stripped = new Dictionary<string, string>();
347+
var enumerable = allAvailable as string[] ?? allAvailable.ToArray();
348+
349+
foreach (var t in enumerable)
350+
{
351+
stripped.Add(new Regex("[a-zA-z]").Replace(t, "."), t);
352+
}
353+
354+
var comparableVersion = new Regex("[a-zA-z]").Replace(version, ".");
355+
if (!stripped.ContainsKey(comparableVersion))
356+
{
357+
stripped.Add(comparableVersion, version);
358+
}
359+
360+
var comparables = stripped.Keys.OrderBy(x => x).ToList();
361+
var actualIndex = comparables.IndexOf(comparableVersion);
362+
363+
if (actualIndex < stripped.Count - 1) return stripped[comparables[actualIndex + 1]];
364+
return null;
365+
}
366+
367+
// https://stackoverflow.com/a/1619103/5452781
368+
public static KeyValuePair<TKey, TValue> GetEntry<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key)
369+
{
370+
return new KeyValuePair<TKey, TValue>(key, dictionary[key]);
371+
}
372+
373+
public static void HandleDataGridScrollKeys(object sender, KeyEventArgs e)
374+
{
375+
DataGrid grid = sender as DataGrid;
376+
switch (e.Key)
377+
{
378+
case Key.Up:
379+
if (grid.SelectedIndex > 0)
380+
{
381+
grid.SelectedIndex--;
382+
}
383+
else
384+
{
385+
grid.SelectedIndex = grid.Items.Count - 1;
386+
}
387+
e.Handled = true;
388+
break;
389+
case Key.Down:
390+
grid.SelectedIndex = ++grid.SelectedIndex % grid.Items.Count;
391+
e.Handled = true;
392+
break;
393+
}
394+
grid.ScrollIntoView(grid.Items[grid.SelectedIndex]);
395+
}
152396

153397
} // class
154398
} // namespace

0 commit comments

Comments
 (0)