1818using System . Windows . Input ;
1919using System . Windows . Media ;
2020using System . Windows . Shell ;
21+ using UnityLauncherPro . Helpers ;
2122
2223namespace UnityLauncherPro
2324{
@@ -91,6 +92,7 @@ void Start()
9192 myMutex = new Mutex ( true , appName , out aIsNewInstance ) ;
9293 if ( ! aIsNewInstance )
9394 {
95+ // NOTE doesnt work if its minized to tray
9496 ActivateOtherWindow ( ) ;
9597 App . Current . Shutdown ( ) ;
9698 }
@@ -142,8 +144,7 @@ private static void ActivateOtherWindow()
142144 if ( other != IntPtr . Zero )
143145 {
144146 SetForegroundWindow ( other ) ;
145- if ( IsIconic ( other ) )
146- OpenIcon ( other ) ;
147+ if ( IsIconic ( other ) ) OpenIcon ( other ) ;
147148 }
148149 }
149150
@@ -192,7 +193,8 @@ void HandleCommandLineLaunch()
192193 {
193194 // try launching it
194195 var proc = Tools . LaunchProject ( proj ) ;
195- proj . Process = proc ;
196+ //proj.Process = proc;
197+ //ProcessHandler.Add(proj, proc);
196198 }
197199
198200 // quit after launch if enabled in settings
@@ -828,30 +830,8 @@ private void BtnLaunchProject_Click(object sender, RoutedEventArgs e)
828830 {
829831 var proj = GetSelectedProject ( ) ;
830832 var proc = Tools . LaunchProject ( proj , gridRecent ) ;
831- proj . Process = proc ;
832833
833- // subscribe to process exit, so that can update proj details row (if it was changed in Unity)
834- proj . Process . Exited += ( object o , EventArgs ea ) =>
835- {
836- Console . WriteLine ( "Unity closed, update this project: " + proj ) ;
837-
838- // call update on main thread, TODO move this to Tools?
839- this . Dispatcher . Invoke ( ( ) =>
840- {
841- //Console.WriteLine("got project "+gridRecent.Items.Contains(proj));
842- var index = projectsSource . IndexOf ( proj ) ;
843- var tempProj = projectsSource [ index ] ;
844- tempProj . Modified = Tools . GetLastModifiedTime ( proj . Path ) ;
845- tempProj . Version = Tools . GetProjectVersion ( proj . Path ) ;
846- tempProj . GITBranch = Tools . ReadGitBranchInfo ( proj . Path ) ;
847- tempProj . TargetPlatform = Tools . GetTargetPlatform ( proj . Path ) ;
848- projectsSource [ index ] = tempProj ;
849-
850- // TODO no need to update every item though..
851- gridRecent . Items . Refresh ( ) ;
852-
853- } ) ;
854- } ;
834+ //ProcessHandler.Add(proj, proc);
855835
856836 Tools . SetFocusToGrid ( gridRecent ) ;
857837 }
@@ -963,7 +943,7 @@ private void TxtSearchBox_PreviewKeyDown(object sender, KeyEventArgs e)
963943 case Key . Return : // open selected project
964944 var proj = GetSelectedProject ( ) ;
965945 var proc = Tools . LaunchProject ( proj ) ;
966- proj . Process = proc ;
946+ //ProcessHandler.Add(proj, proc) ;
967947 break ;
968948 case Key . Tab :
969949 case Key . Up :
@@ -1055,7 +1035,7 @@ private void GridRecent_PreviewKeyDown(object sender, KeyEventArgs e)
10551035 e . Handled = true ;
10561036 var proj = GetSelectedProject ( ) ;
10571037 var proc = Tools . LaunchProject ( proj ) ;
1058- proj . Process = proc ;
1038+ //ProcessHandler.Add(proj, proc) ;
10591039 break ;
10601040 default :
10611041 break ;
@@ -1441,7 +1421,7 @@ private void GridRecent_PreviewMouseDoubleClick(object sender, MouseButtonEventA
14411421
14421422 var proj = GetSelectedProject ( ) ;
14431423 var proc = Tools . LaunchProject ( proj ) ;
1444- proj . Process = proc ;
1424+ //ProcessHandler.Add(proj, proc) ;
14451425 }
14461426
14471427 private void DataGridUnitys_PreviewMouseDoubleClick ( object sender , MouseButtonEventArgs e )
@@ -1727,16 +1707,18 @@ private void MenuItemKillProcess_Click(object sender, RoutedEventArgs e)
17271707 void KillSelectedProcess ( object sender , ExecutedRoutedEventArgs e )
17281708 {
17291709 var proj = GetSelectedProject ( ) ;
1730- if ( proj . Process != null )
1710+ var proc = ProcessHandler . Get ( proj . Path ) ;
1711+ if ( proj != null && proc != null )
17311712 {
17321713 try
17331714 {
1734- proj . Process . Kill ( ) ;
1715+ proc . Kill ( ) ;
17351716 }
17361717 catch ( Exception )
17371718 {
17381719 }
1739- proj . Process = null ;
1720+ //proc.Dispose(); // NOTE cannot dispose, otherwise process.Exited event is not called
1721+ proj = null ;
17401722 }
17411723 }
17421724
@@ -1745,7 +1727,8 @@ private void GridRecent_ContextMenuOpening(object sender, ContextMenuEventArgs e
17451727 if ( tabControl . SelectedIndex == 0 )
17461728 {
17471729 var proj = GetSelectedProject ( ) ;
1748- menuItemKillProcess . IsEnabled = proj . Process != null ;
1730+ var proc = ProcessHandler . Get ( proj . Path ) ;
1731+ menuItemKillProcess . IsEnabled = proc != null ;
17491732 }
17501733 }
17511734
@@ -1892,7 +1875,8 @@ private void MenuStartWebGLServer_Click(object sender, RoutedEventArgs e)
18921875 int port = rnd . Next ( 50000 , 61000 ) ;
18931876
18941877 // take process id from unity, if have it (then webserver closes automatically when unity is closed)
1895- int pid = proj . Process == null ? - 1 : proj . Process . Id ;
1878+ var proc = ProcessHandler . Get ( proj . Path ) ;
1879+ int pid = proc == null ? - 1 : proc . Id ;
18961880 var param = "\" " + webExe + "\" \" " + buildPath + "\" " + port + ( pid == - 1 ? "" : " " + pid ) ; // server exe path, build folder and port
18971881
18981882 // then open browser
@@ -2242,5 +2226,27 @@ private void BtnResources_Click(object sender, RoutedEventArgs e)
22422226 {
22432227 Tools . OpenURL ( resourcesURL ) ;
22442228 }
2229+
2230+ public void ProcessExitedCallBack ( Project proj )
2231+ {
2232+ Console . WriteLine ( "Process Exited: " + proj . Path ) ;
2233+ //var index = projectsSource.IndexOf(proj); // this fails since proj has changed after refresh (timestamp or other data)
2234+
2235+ // FIXME nobody likes extra loops.. but only 40 items to find correct project?
2236+ for ( int i = 0 , len = projectsSource . Count ; i < len ; i ++ )
2237+ {
2238+ if ( projectsSource [ i ] . Path == proj . Path )
2239+ {
2240+ var tempProj = projectsSource [ i ] ;
2241+ tempProj . Modified = Tools . GetLastModifiedTime ( proj . Path ) ;
2242+ tempProj . Version = Tools . GetProjectVersion ( proj . Path ) ;
2243+ tempProj . GITBranch = Tools . ReadGitBranchInfo ( proj . Path ) ;
2244+ tempProj . TargetPlatform = Tools . GetTargetPlatform ( proj . Path ) ;
2245+ projectsSource [ i ] = tempProj ;
2246+ gridRecent . Items . Refresh ( ) ;
2247+ break ;
2248+ }
2249+ }
2250+ }
22452251 } // class
22462252} //namespace
0 commit comments