Skip to content

Commit 8af4d16

Browse files
committed
refactoring
1 parent 9f8d12a commit 8af4d16

File tree

5 files changed

+47
-91
lines changed

5 files changed

+47
-91
lines changed

Properties/Settings.Designer.cs

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Properties/Settings.settings

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<Value Profile="(Default)">Apache2.4</Value>
1010
</Setting>
1111
<Setting Name="FastCgiAddress" Type="System.String" Scope="Application">
12-
<Value Profile="(Default)">127.0.0.1:9999</Value>
12+
<Value Profile="(Default)" />
1313
</Setting>
1414
<Setting Name="HttpServerProcessPath" Type="System.String" Scope="Application">
1515
<Value Profile="(Default)" />

src/app.config

+6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@
1313
<setting name="HttpServerServiceName" serializeAs="String">
1414
<value>Apache2.4</value>
1515
</setting>
16+
<setting name="FastCgiAddress" serializeAs="String">
17+
<value />
18+
</setting>
19+
<setting name="HttpServerProcessPath" serializeAs="String">
20+
<value />
21+
</setting>
1622
</PhpVersionSwitcher.Properties.Settings>
1723
</applicationSettings>
1824
</configuration>

src/ui/MainForm.cs

+25-54
Original file line numberDiff line numberDiff line change
@@ -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
}

src/ui/ProcessMenu.cs

+14-35
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,31 @@
1-
using System;
2-
using System.Windows.Forms;
1+
using System.Windows.Forms;
32
using PhpVersionSwitcher.Properties;
43

54
namespace PhpVersionSwitcher
65
{
76
class ProcessMenu : ToolStripMenuItem
87
{
9-
private readonly MainForm form;
10-
private readonly IProcessManager processManager;
8+
public IProcessManager ProcessManager { get; private set; }
9+
public ToolStripItem StartItem { get; private set; }
10+
public ToolStripItem StopItem { get; private set; }
11+
public ToolStripItem RestartItem { get; private set; }
1112

12-
private ToolStripItem startItem;
13-
private ToolStripItem stopItem;
14-
private ToolStripItem restartItem;
15-
16-
public ProcessMenu(MainForm form, IProcessManager processManager)
17-
: base(processManager.Name)
13+
public ProcessMenu(IProcessManager processManager) : base(processManager.Name)
1814
{
19-
this.form = form;
20-
this.processManager = processManager;
21-
22-
this.startItem = this.DropDownItems.Add("Start", Resources.Start, this.startItem_Clicked);
23-
this.stopItem = this.DropDownItems.Add("Stop", Resources.Stop, this.stopItem_Clicked);
24-
this.restartItem = this.DropDownItems.Add("Restart", Resources.Restart, this.restartItem_Clicked);
15+
this.ProcessManager = processManager;
16+
this.StartItem = this.DropDownItems.Add("Start", Resources.Start);
17+
this.StopItem = this.DropDownItems.Add("Stop", Resources.Stop);
18+
this.RestartItem = this.DropDownItems.Add("Restart", Resources.Restart);
2519
this.Refresh();
2620
}
2721

2822
public void Refresh()
2923
{
30-
bool running = this.processManager.IsRunning();
24+
bool running = this.ProcessManager.IsRunning();
3125
this.Image = running ? Resources.Start : Resources.Stop;
32-
this.startItem.Enabled = !running;
33-
this.stopItem.Enabled = running;
34-
this.restartItem.Enabled = running;
35-
}
36-
37-
private void startItem_Clicked(object sender, EventArgs e)
38-
{
39-
this.form.Attempt("HTTP server to start", this.processManager.Start);
40-
}
41-
42-
private void stopItem_Clicked(object sender, EventArgs e)
43-
{
44-
this.form.Attempt("HTTP server to stop", this.processManager.Stop);
45-
}
46-
47-
private void restartItem_Clicked(object sender, EventArgs e)
48-
{
49-
this.form.Attempt("HTTP server to restart", this.processManager.Restart);
26+
this.StartItem.Enabled = !running;
27+
this.StopItem.Enabled = running;
28+
this.RestartItem.Enabled = running;
5029
}
5130
}
5231
}

0 commit comments

Comments
 (0)