@@ -8,14 +8,13 @@ namespace PhpVersionSwitcher
88{
99 public partial class MainForm : Form
1010 {
11- private List < ProcessMenu > submenus ;
11+ private List < ProcessMenu > processMenus ;
1212 private WaitingForm waitingForm ;
13- private ToolStripMenuItem activeVersionItem ;
1413 private VersionsManager phpVersions ;
1514
1615 public MainForm ( )
1716 {
18- this . submenus = new List < ProcessMenu > ( ) ;
17+ this . processMenus = new List < ProcessMenu > ( ) ;
1918 this . waitingForm = new WaitingForm ( ) ;
2019
2120 IProcessManager server = null ;
@@ -25,19 +24,19 @@ public MainForm()
2524 if ( Settings . Default . HttpServerServiceName . Trim ( ) . Length > 0 )
2625 {
2726 server = new ServiceManager ( Settings . Default . HttpServerServiceName ) ;
28- this . submenus . Add ( new ProcessMenu ( this , server ) ) ;
27+ this . RegisterProcessManager ( server ) ;
2928 }
3029
3130 if ( Settings . Default . HttpServerProcessPath . Trim ( ) . Length > 0 )
3231 {
3332 server = new ProcessManager ( Settings . Default . HttpServerProcessPath ) ;
34- this . submenus . Add ( new ProcessMenu ( this , server ) ) ;
33+ this . RegisterProcessManager ( server ) ;
3534 }
3635
3736 if ( Settings . Default . FastCgiAddress . Trim ( ) . Length > 0 )
3837 {
3938 server = new ProcessManager ( Settings . Default . PhpDir + "\\ active\\ php-cgi.exe" , "-b " + Settings . Default . FastCgiAddress ) ;
40- this . submenus . Add ( new ProcessMenu ( this , server ) ) ;
39+ this . RegisterProcessManager ( server ) ;
4140 }
4241 }
4342 catch ( Exception ex )
@@ -58,43 +57,44 @@ public MainForm()
5857
5958 private void InitializeMainMenu ( )
6059 {
60+ this . notifyIconMenu . Items . Clear ( ) ;
6161 var activeVersion = this . phpVersions . GetActive ( ) ;
6262 var versions = this . phpVersions . GetAvailable ( ) ;
6363
64- this . notifyIconMenu . Items . Clear ( ) ;
6564 foreach ( var version in versions )
6665 {
67- var item = new ToolStripMenuItem ( version . ToString ( ) , null , this . version_Clicked ) ;
68- item . Tag = version ;
69-
70- if ( activeVersion != null && version . Equals ( activeVersion ) )
66+ var item = new ToolStripMenuItem ( version ) ;
67+ item . Checked = version . Equals ( activeVersion ) ;
68+ item . Click += ( sender , args ) => this . Attempt ( "PHP version to change" , async ( ) =>
7169 {
72- this . SetActiveItem ( item ) ;
73- }
70+ await this . phpVersions . SwitchTo ( version ) ;
71+ } ) ;
7472
7573 this . notifyIconMenu . Items . Add ( item ) ;
7674 }
7775
7876 this . notifyIconMenu . Items . Add ( new ToolStripSeparator ( ) ) ;
7977
80- foreach ( var menu in this . submenus )
78+ foreach ( var menu in this . processMenus )
8179 {
80+ menu . Refresh ( ) ;
8281 this . notifyIconMenu . Items . Add ( menu ) ;
8382 }
8483
85- this . notifyIconMenu . Items . Add ( new ToolStripSeparator ( ) ) ;
86- this . notifyIconMenu . Items . Add ( "Refresh" , null , this . refresh_Clicked ) ;
87- this . notifyIconMenu . Items . Add ( "Close" , null , this . close_Click ) ;
84+ this . notifyIconMenu . Items . Add ( "Close" , null , ( sender , args ) => Application . Exit ( ) ) ;
8885 }
8986
90- private void SetActiveItem ( ToolStripMenuItem item )
87+ private void RegisterProcessManager ( IProcessManager pm )
9188 {
92- if ( this . activeVersionItem != null ) this . activeVersionItem . Checked = false ;
93- this . activeVersionItem = item ;
94- this . activeVersionItem . Checked = true ;
89+ var menu = new ProcessMenu ( pm ) ;
90+ menu . StartItem . Click += ( sender , args ) => this . Attempt ( pm . Name + " to start" , pm . Start ) ;
91+ menu . StopItem . Click += ( sender , args ) => this . Attempt ( pm . Name + " to stop" , pm . Stop ) ;
92+ menu . RestartItem . Click += ( sender , args ) => this . Attempt ( pm . Name + " to restart" , pm . Restart ) ;
93+
94+ this . processMenus . Add ( menu ) ;
9595 }
9696
97- public async void Attempt ( string description , Func < Task > action )
97+ private async void Attempt ( string description , Func < Task > action )
9898 {
9999 this . notifyIconMenu . Enabled = false ;
100100 this . waitingForm . description . Text = @"Waiting for " + description + @"..." ;
@@ -109,50 +109,21 @@ public async void Attempt(string description, Func<Task> action)
109109 }
110110 catch ( ProcessException ex )
111111 {
112- var dialogResult = MessageBox . Show ( "Unable to " + ex . Operation + " " + ex . Name + "." , "Operation failed" , MessageBoxButtons . RetryCancel , MessageBoxIcon . Error ) ;
112+ var msg = "Unable to " + ex . Operation + " " + ex . Name + "." ;
113+ var dialogResult = MessageBox . Show ( msg , "Operation failed" , MessageBoxButtons . RetryCancel , MessageBoxIcon . Error ) ;
113114 if ( dialogResult != DialogResult . Retry ) break ;
114115 }
115116 }
116117
117- this . RefreshSubMenus ( ) ;
118+ this . InitializeMainMenu ( ) ;
118119 this . waitingForm . Hide ( ) ;
119120 this . notifyIconMenu . Enabled = true ;
120121 }
121122
122- private void RefreshSubMenus ( )
123- {
124- foreach ( var menu in this . submenus )
125- {
126- menu . Refresh ( ) ;
127- }
128- }
129-
130123 private void ShowFatalError ( string message )
131124 {
132125 MessageBox . Show ( message , "Fatal error" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
133126 Application . Exit ( ) ;
134127 }
135-
136- private void version_Clicked ( object sender , EventArgs e )
137- {
138- var menuItem = ( ToolStripMenuItem ) sender ;
139- var version = ( Version ) menuItem . Tag ;
140-
141- this . Attempt ( "PHP version to change" , async ( ) =>
142- {
143- await this . phpVersions . SwitchTo ( version ) ;
144- this . SetActiveItem ( menuItem ) ;
145- } ) ;
146- }
147-
148- private void refresh_Clicked ( object sender , EventArgs e )
149- {
150- this . InitializeMainMenu ( ) ;
151- }
152-
153- private void close_Click ( object sender , EventArgs e )
154- {
155- Application . Exit ( ) ;
156- }
157128 }
158129}
0 commit comments