Skip to content

Commit eeb13bc

Browse files
committed
add Kill Process context menu for selected project and shortcut (Alt+Q) , #build
1 parent 479d507 commit eeb13bc

File tree

4 files changed

+77
-25
lines changed

4 files changed

+77
-25
lines changed

UnityLauncherPro/Data/Project.cs

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Diagnostics;
23

34
namespace UnityLauncherPro
45
{
@@ -10,5 +11,6 @@ public class Project
1011
public DateTime? Modified { set; get; }
1112
public string Arguments { set; get; }
1213
public string GITBranch { set; get; }
14+
public Process Process; // launched unity exe
1315
}
1416
}

UnityLauncherPro/MainWindow.xaml

+4-2
Original file line numberDiff line numberDiff line change
@@ -301,10 +301,11 @@
301301
<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}" TabIndex="10" />
302302
<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" TabIndex="11"/>
303303

304-
<DataGrid x:Name="gridRecent" SelectionMode="Single" Margin="4,30,2,42" CanUserAddRows="False" 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" Loaded="GridRecent_Loaded" TabIndex="2" CellEditEnding="GridRecent_CellEditEnding" PreviewMouseDoubleClick="GridRecent_PreviewMouseDoubleClick" BeginningEdit="GridRecent_BeginningEdit" >
304+
<DataGrid x:Name="gridRecent" SelectionMode="Single" Margin="4,30,2,42" CanUserAddRows="False" 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" Loaded="GridRecent_Loaded" TabIndex="2" CellEditEnding="GridRecent_CellEditEnding" PreviewMouseDoubleClick="GridRecent_PreviewMouseDoubleClick" BeginningEdit="GridRecent_BeginningEdit" ContextMenuOpening="GridRecent_ContextMenuOpening" >
305305

306306
<DataGrid.CommandBindings>
307307
<CommandBinding Command="ApplicationCommands.Copy" Executed="CopyRowFolderToClipBoard" CanExecute="CanExecute_Copy"/>
308+
<CommandBinding Command="{x:Static local:MainWindow.KillProcessCommand}" Executed="KillSelectedProcess"/>
308309
</DataGrid.CommandBindings>
309310

310311
<DataGrid.Columns>
@@ -332,8 +333,9 @@
332333
<ContextMenu>
333334
<MenuItem x:Name="menuItemCopyVersion" Header="Copy Unity Version" Click="MenuItemCopyVersion_Click" />
334335
<MenuItem x:Name="menuItemCopyPath" Header="Copy Project Path" Click="MenuItemCopyPath_Click" />
335-
<!--<Separator />-->
336336
<MenuItem x:Name="menuItemShowProjectInExplorer" Header="Show in Explorer" Click="MenuItemShowProjectInExplorer_Click" />
337+
<Separator/>
338+
<MenuItem x:Name="menuItemKillProcess" Header="Kill Process" Click="MenuItemKillProcess_Click" />
337339
</ContextMenu>
338340
</DataGrid.ContextMenu>
339341

UnityLauncherPro/MainWindow.xaml.cs

+51-9
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,11 @@ public partial class MainWindow : Window
3232
[DllImport("user32")]
3333
static extern bool OpenIcon(IntPtr hWnd);
3434

35-
//Project[] projectsSource;
35+
// datagrid sources
3636
List<Project> projectsSource;
37-
38-
3937
Updates[] updatesSource;
4038
UnityInstallation[] unityInstallationsSource;
39+
4140
public static Dictionary<string, string> unityInstalledVersions = new Dictionary<string, string>();
4241
const string contextRegRoot = "Software\\Classes\\Directory\\Background\\shell";
4342
public static readonly string launcherArgumentsFile = "LauncherArguments.txt";
@@ -154,7 +153,8 @@ void HandleCommandLineLaunch()
154153
else
155154
{
156155
// try launching it
157-
Tools.LaunchProject(proj);
156+
var proc = Tools.LaunchProject(proj);
157+
proj.Process = proc;
158158
}
159159

160160
// quit after launch if enabled in settings
@@ -641,7 +641,9 @@ private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
641641

642642
private void BtnLaunchProject_Click(object sender, RoutedEventArgs e)
643643
{
644-
Tools.LaunchProject(GetSelectedProject());
644+
var proj = GetSelectedProject();
645+
var proc = Tools.LaunchProject(proj);
646+
proj.Process = proc;
645647
Tools.SetFocusToGrid(gridRecent);
646648
}
647649

@@ -741,7 +743,8 @@ private void TxtSearchBox_PreviewKeyDown(object sender, KeyEventArgs e)
741743
{
742744
case Key.Return: // open selected project
743745
var proj = GetSelectedProject();
744-
if (proj != null) Tools.LaunchProject(proj);
746+
var proc = Tools.LaunchProject(proj);
747+
proj.Process = proc;
745748
break;
746749
case Key.Tab:
747750
case Key.Up:
@@ -832,7 +835,8 @@ private void GridRecent_PreviewKeyDown(object sender, KeyEventArgs e)
832835
if (IsEditingCell(gridRecent) == true) return;
833836
e.Handled = true;
834837
var proj = GetSelectedProject();
835-
Tools.LaunchProject(proj);
838+
var proc = Tools.LaunchProject(proj);
839+
proj.Process = proc;
836840
break;
837841
default:
838842
break;
@@ -1090,6 +1094,7 @@ private void GridRecent_CellEditEnding(object sender, DataGridCellEditEndingEven
10901094
TextBox t = e.EditingElement as TextBox;
10911095
string newcellValue = t.Text.ToString();
10921096

1097+
10931098
// check if we edited project name, or launcher arguments
10941099
if (e.Column.DisplayIndex == 0)
10951100
{
@@ -1150,7 +1155,7 @@ private void GridRecent_CellEditEnding(object sender, DataGridCellEditEndingEven
11501155
}
11511156

11521157
}
1153-
else // it was launcher arguments
1158+
else // edit launcher arguments
11541159
{
11551160

11561161
string projSettingsFolder = "ProjectSettings";
@@ -1199,7 +1204,9 @@ private void GridRecent_PreviewMouseDoubleClick(object sender, MouseButtonEventA
11991204
var currentColumnCell = gridRecent.CurrentCell.Column.DisplayIndex;
12001205
if (currentColumnCell == 4) return;
12011206

1202-
Tools.LaunchProject(GetSelectedProject());
1207+
var proj = GetSelectedProject();
1208+
var proc = Tools.LaunchProject(proj);
1209+
proj.Process = proc;
12031210
}
12041211

12051212
private void DataGridUnitys_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
@@ -1426,6 +1433,41 @@ private void ChkEnableProjectRename_Checked(object sender, RoutedEventArgs e)
14261433
Properties.Settings.Default.enableProjectRename = (bool)chkEnableProjectRename.IsChecked;
14271434
Properties.Settings.Default.Save();
14281435
}
1436+
1437+
private void MenuItemKillProcess_Click(object sender, RoutedEventArgs e)
1438+
{
1439+
if (tabControl.SelectedIndex == 0)
1440+
{
1441+
KillSelectedProcess(null, null);
1442+
}
1443+
}
1444+
1445+
void KillSelectedProcess(object sender, ExecutedRoutedEventArgs e)
1446+
{
1447+
var proj = GetSelectedProject();
1448+
if (proj.Process != null)
1449+
{
1450+
proj.Process.Kill();
1451+
proj.Process = null;
1452+
}
1453+
}
1454+
1455+
private void GridRecent_ContextMenuOpening(object sender, ContextMenuEventArgs e)
1456+
{
1457+
if (tabControl.SelectedIndex == 0)
1458+
{
1459+
var proj = GetSelectedProject();
1460+
menuItemKillProcess.IsEnabled = proj.Process != null;
1461+
}
1462+
}
1463+
1464+
// add alt+Q shortcut for killing process
1465+
// https://stackoverflow.com/a/29817712/5452781
1466+
public static readonly RoutedCommand KillProcessCommand = new RoutedUICommand("None", "KillProcessCommand", typeof(MainWindow), new InputGestureCollection(new InputGesture[]
1467+
{
1468+
new KeyGesture(Key.Q, ModifierKeys.Alt)
1469+
}));
1470+
14291471
} // class
14301472
} //namespace
14311473

UnityLauncherPro/Tools.cs

+20-14
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,11 @@ public static void ExploreProjectFolder(Project proj)
144144
}
145145

146146
// NOTE holding alt key (when using alt+o) brings up unity project selector
147-
public static void LaunchProject(Project proj)
147+
public static Process LaunchProject(Project proj)
148148
{
149149
// validate
150-
if (proj == null) return;
151-
if (Directory.Exists(proj.Path) == false) return;
150+
if (proj == null) return null;
151+
if (Directory.Exists(proj.Path) == false) return null;
152152

153153
Console.WriteLine("launch project " + proj.Title + " " + proj.Version);
154154

@@ -164,23 +164,24 @@ public static void LaunchProject(Project proj)
164164
var cancelLaunch = CheckCrashBackupScene(proj.Path);
165165
if (cancelLaunch == true)
166166
{
167-
return;
167+
return null;
168168
}
169169

170170
var unityExePath = GetUnityExePath(proj.Version);
171171
if (unityExePath == null)
172172
{
173173
DisplayUpgradeDialog(proj, null);
174-
return;
174+
return null;
175175
}
176176

177177
// SetStatus("Launching project in Unity " + version);
178178

179+
Process newProcess = new Process();
180+
179181
try
180182
{
181-
Process myProcess = new Process();
182183
var cmd = "\"" + unityExePath + "\"";
183-
myProcess.StartInfo.FileName = cmd;
184+
newProcess.StartInfo.FileName = cmd;
184185

185186
var unitycommandlineparameters = " -projectPath " + "\"" + proj.Path + "\"";
186187

@@ -190,8 +191,8 @@ public static void LaunchProject(Project proj)
190191
unitycommandlineparameters += " " + customArguments;
191192
}
192193

193-
myProcess.StartInfo.Arguments = unitycommandlineparameters;
194-
myProcess.Start();
194+
newProcess.StartInfo.Arguments = unitycommandlineparameters;
195+
newProcess.Start();
195196

196197
if (Properties.Settings.Default.closeAfterProject)
197198
{
@@ -206,6 +207,9 @@ public static void LaunchProject(Project proj)
206207
// move as first, since its opened, disabled for now, more used to it staying in place..
207208
// MainWindow wnd = (MainWindow)Application.Current.MainWindow;
208209
// wnd.MoveRecentGridItem(0);
210+
211+
return newProcess;
212+
209213
}
210214

211215
static bool CheckCrashBackupScene(string projectPath)
@@ -588,7 +592,8 @@ public static void DisplayUpgradeDialog(Project proj, MainWindow owner)
588592

589593
// inject new version for this item
590594
proj.Version = upgradeToVersion;
591-
LaunchProject(proj);
595+
var proc = LaunchProject(proj);
596+
proj.Process = proc;
592597
}
593598
else
594599
{
@@ -820,10 +825,11 @@ public static void FastCreateProject(string version, string baseFolder, string p
820825
CreateEmptyProjectFolder(newPath, version);
821826

822827
// launch empty project
823-
var p = new Project();
824-
p.Path = Path.Combine(baseFolder, newPath);
825-
p.Version = version;
826-
LaunchProject(p);
828+
var proj = new Project();
829+
proj.Path = Path.Combine(baseFolder, newPath);
830+
proj.Version = version;
831+
var proc = LaunchProject(proj);
832+
proj.Process = proc;
827833
} // FastCreateProject
828834

829835

0 commit comments

Comments
 (0)