Skip to content

Commit 4a2a538

Browse files
committed
Added support for loading main app window hidden.
Added support for hiding and restoring window to/from tray.
1 parent 24fec7c commit 4a2a538

File tree

4 files changed

+66
-11
lines changed

4 files changed

+66
-11
lines changed

Source/Forms/WatForm.Designer.cs

Lines changed: 10 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Source/Forms/WatForm.cs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,37 @@
22
using ScriptFUSION.WarframeAlertTracker.Warframe;
33
using System;
44
using System.Collections.Generic;
5+
using System.Diagnostics;
56
using System.Linq;
67
using System.Windows.Forms;
78
using ScriptFUSION.WarframeAlertTracker.Alerts;
89
using ScriptFUSION.WarframeAlertTracker.Controls;
910

1011
namespace ScriptFUSION.WarframeAlertTracker.Forms {
1112
public partial class WatForm : Form {
13+
private FormWindowState lastWindowState;
14+
1215
private WatApplication Application { get; }
1316

17+
/// <summary>
18+
/// Gets or sets the last non-minimized window state.
19+
/// </summary>
20+
private FormWindowState LastWindowState
21+
{
22+
get => lastWindowState;
23+
set
24+
{
25+
Debug.Assert(value != FormWindowState.Minimized);
26+
27+
lastWindowState = value;
28+
}
29+
}
30+
1431
internal WatForm(WatApplication application) {
1532
InitializeComponent();
1633

1734
Application = application;
35+
LastWindowState = WindowState;
1836

1937
application.CurrentWorldState.Update +=
2038
async worldState => OnWorldStateUpdate(worldState.Fissures, await application.SolNodes);
@@ -26,9 +44,10 @@ internal WatForm(WatApplication application) {
2644
trayIcon.Icon = Icon;
2745
}
2846

29-
private void ToggleWindowVisibility() {
47+
public void ToggleWindowVisibility() {
3048
if (Visible = !Visible) {
31-
Activate();
49+
// Restore previous state.
50+
WindowState = LastWindowState;
3251
}
3352
}
3453

@@ -43,6 +62,19 @@ private void OnAlertsUpdate(AlertCollection alertsCollection) {
4362
alertsCount.Text = Application.CurrentWorldState.CurrentState.WorldStateObjects.Count(alertsCollection.Matches).ToString();
4463
}
4564

65+
private void WatForm_Resize(object sender, EventArgs e) {
66+
ShowInTaskbar = WindowState != FormWindowState.Minimized;
67+
68+
if (WindowState == FormWindowState.Minimized) {
69+
// Sync form visibility with minimized state.
70+
Visible = false;
71+
}
72+
else {
73+
// Record previous non-minimized state.
74+
LastWindowState = WindowState;
75+
}
76+
}
77+
4678
private void alerts_Click(object sender, EventArgs e) {
4779
var alertCollection = Application.Settings.Alerts.Clone();
4880

Source/Forms/WatForm.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,7 @@
129129
<metadata name="trayMenu.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
130130
<value>333, 17</value>
131131
</metadata>
132+
<metadata name="$this.TrayHeight" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
133+
<value>57</value>
134+
</metadata>
132135
</root>

Source/WatApplication.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,24 @@
88

99
namespace ScriptFUSION.WarframeAlertTracker {
1010
internal sealed class WatApplication : ApplicationContext {
11+
private Form mainForm;
12+
13+
/// <remarks>
14+
/// Hides the base implementation so the base Form is always null. This is needed to stop
15+
/// Application.RunMessageLoopInner forcing the form to be visible, which is undesirable for our purposes since
16+
/// we provide an option to start the application hidden.
17+
/// </remarks>
18+
public new Form MainForm
19+
{
20+
get => mainForm;
21+
set
22+
{
23+
mainForm = value;
24+
25+
value.HandleDestroyed += delegate { if (!value.RecreatingHandle) ExitThread(); };
26+
}
27+
}
28+
1129
public CurrentWorldState CurrentWorldState { get; } = new CurrentWorldState(new WorldStateDownloader(Downloader));
1230

1331
public Settings Settings { get; } = Settings.Default;
@@ -22,6 +40,7 @@ internal sealed class WatApplication : ApplicationContext {
2240

2341
public WatApplication() {
2442
MainForm = new WatForm(this);
43+
if (!Settings.LoadHidden) MainForm.Show();
2544

2645
new Notifier(this);
2746
CurrentWorldState.DownloadIndefinitely();

0 commit comments

Comments
 (0)