@@ -8,14 +8,13 @@ namespace PhpVersionSwitcher
8
8
{
9
9
public partial class MainForm : Form
10
10
{
11
- private List < ProcessMenu > submenus ;
11
+ private List < ProcessMenu > processMenus ;
12
12
private WaitingForm waitingForm ;
13
- private ToolStripMenuItem activeVersionItem ;
14
13
private VersionsManager phpVersions ;
15
14
16
15
public MainForm ( )
17
16
{
18
- this . submenus = new List < ProcessMenu > ( ) ;
17
+ this . processMenus = new List < ProcessMenu > ( ) ;
19
18
this . waitingForm = new WaitingForm ( ) ;
20
19
21
20
IProcessManager server = null ;
@@ -25,19 +24,19 @@ public MainForm()
25
24
if ( Settings . Default . HttpServerServiceName . Trim ( ) . Length > 0 )
26
25
{
27
26
server = new ServiceManager ( Settings . Default . HttpServerServiceName ) ;
28
- this . submenus . Add ( new ProcessMenu ( this , server ) ) ;
27
+ this . RegisterProcessManager ( server ) ;
29
28
}
30
29
31
30
if ( Settings . Default . HttpServerProcessPath . Trim ( ) . Length > 0 )
32
31
{
33
32
server = new ProcessManager ( Settings . Default . HttpServerProcessPath ) ;
34
- this . submenus . Add ( new ProcessMenu ( this , server ) ) ;
33
+ this . RegisterProcessManager ( server ) ;
35
34
}
36
35
37
36
if ( Settings . Default . FastCgiAddress . Trim ( ) . Length > 0 )
38
37
{
39
38
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 ) ;
41
40
}
42
41
}
43
42
catch ( Exception ex )
@@ -58,43 +57,44 @@ public MainForm()
58
57
59
58
private void InitializeMainMenu ( )
60
59
{
60
+ this . notifyIconMenu . Items . Clear ( ) ;
61
61
var activeVersion = this . phpVersions . GetActive ( ) ;
62
62
var versions = this . phpVersions . GetAvailable ( ) ;
63
63
64
- this . notifyIconMenu . Items . Clear ( ) ;
65
64
foreach ( var version in versions )
66
65
{
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 ( ) =>
71
69
{
72
- this . SetActiveItem ( item ) ;
73
- }
70
+ await this . phpVersions . SwitchTo ( version ) ;
71
+ } ) ;
74
72
75
73
this . notifyIconMenu . Items . Add ( item ) ;
76
74
}
77
75
78
76
this . notifyIconMenu . Items . Add ( new ToolStripSeparator ( ) ) ;
79
77
80
- foreach ( var menu in this . submenus )
78
+ foreach ( var menu in this . processMenus )
81
79
{
80
+ menu . Refresh ( ) ;
82
81
this . notifyIconMenu . Items . Add ( menu ) ;
83
82
}
84
83
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 ( ) ) ;
88
85
}
89
86
90
- private void SetActiveItem ( ToolStripMenuItem item )
87
+ private void RegisterProcessManager ( IProcessManager pm )
91
88
{
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 ) ;
95
95
}
96
96
97
- public async void Attempt ( string description , Func < Task > action )
97
+ private async void Attempt ( string description , Func < Task > action )
98
98
{
99
99
this . notifyIconMenu . Enabled = false ;
100
100
this . waitingForm . description . Text = @"Waiting for " + description + @"..." ;
@@ -109,50 +109,21 @@ public async void Attempt(string description, Func<Task> action)
109
109
}
110
110
catch ( ProcessException ex )
111
111
{
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 ) ;
113
114
if ( dialogResult != DialogResult . Retry ) break ;
114
115
}
115
116
}
116
117
117
- this . RefreshSubMenus ( ) ;
118
+ this . InitializeMainMenu ( ) ;
118
119
this . waitingForm . Hide ( ) ;
119
120
this . notifyIconMenu . Enabled = true ;
120
121
}
121
122
122
- private void RefreshSubMenus ( )
123
- {
124
- foreach ( var menu in this . submenus )
125
- {
126
- menu . Refresh ( ) ;
127
- }
128
- }
129
-
130
123
private void ShowFatalError ( string message )
131
124
{
132
125
MessageBox . Show ( message , "Fatal error" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
133
126
Application . Exit ( ) ;
134
127
}
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
- }
157
128
}
158
129
}
0 commit comments