Skip to content

Commit

Permalink
Merge minimise to system tray bug fix and setup display improvements.
Browse files Browse the repository at this point in the history
Fixes Swagger/AlpacaDeviceAPI_v1.yaml conflict
  • Loading branch information
Peter-Simpson committed Oct 16, 2022
2 parents b517d77 + 6f21381 commit 02444d5
Show file tree
Hide file tree
Showing 5 changed files with 3,388 additions and 7,337 deletions.
100 changes: 50 additions & 50 deletions Remote Server/ServerForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

162 changes: 107 additions & 55 deletions Remote Server/ServerForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,13 @@ public partial class ServerForm : Form
internal const string USE_UTC_TIME_IN_LOGS_PROFILENAME = "Use UTC Time In Logs"; public const bool USE_UTC_TIME_IN_LOGS_ENABLED_DEFAULT = false;
internal const string MINIMISE_TO_SYSTEM_TRAY_PROFILENAME = "Minimise To System Tray"; public const bool MINIMISE_TO_SYSTEM_TRAY_DEFAULT = false;
internal const string CONFIRM_EXIT_PROFILENAME = "Confirm Exit"; public const bool CONFIRM_EXIT_DEFAULT = false;
internal const string MINIMISE_ON_START_PROFILENAME = "Minimise On Start"; public const bool MINIMISE_ON_START_DEFAULT = false;

// Minimise behaviour strings
internal const string MINIMISE_TO_SYSTEM_TRAY_TEXT = "Minimise to system tray";
internal const string MINIMISE_TO_TASK_BAR_TEXT = "Minimise to task bar";
internal const string MINIMISE_TO_SYSTEM_TRAY_KEY = "Minimise to system tray";
internal const string MINIMISE_TO_SYSTEM_TRAY_DESCRIPTION = "Service like behaviour - Minimises to the system tray and is hidden from ALT/TAB when minimised.";
internal const string MINIMISE_TO_TASK_BAR_KEY = "Minimise to task bar";
internal const string MINIMISE_TO_TASK_BAR_DESCRIPTION = "Normal application behaviour - Minimises to the task bar and can be restored using ALT/TAB.";

//Device profile persistence constants
internal const string DEVICE_SUBFOLDER_NAME = "Device";
Expand Down Expand Up @@ -260,6 +263,7 @@ public partial class ServerForm : Form
internal static bool UseUtcTimeInLogs;
internal static bool MinimiseToSystemTray;
internal static bool ConfirmExit;
internal static bool StartMinimised;

#endregion

Expand Down Expand Up @@ -355,8 +359,10 @@ public ServerForm()
this.Resize += ServerForm_Resize;
notifyIcon.MouseDoubleClick += NotifyIcon_MouseDoubleClick;
notifyIcon.Click += NotifyIcon_Click;
systemTrayMenuItems.Items["exit"].Click += SystemTrayCloseServer_Click;
systemTrayMenuItems.Items["Title"].Click += SystemTrayTitle_Click; ;
systemTrayMenuItems.Items["Exit"].Click += SystemTrayCloseServer_Click;
systemTrayMenuItems.Items["Title"].Click += SystemTrayTitle_Click;

this.FormClosing += ServerForm_FormClosing;
ServerForm_Resize(this, new EventArgs()); // Move controls to their correct positions

// Check whether this device already has an Alpaca unique ID, if not, create one
Expand Down Expand Up @@ -396,13 +402,22 @@ private void ServerForm_Load(object sender, EventArgs e)

try
{
// Bring this form to the front of the screen
this.WindowState = FormWindowState.Minimized;
this.Show();
this.WindowState = FormWindowState.Normal;

// Ensure that the system tray icon is not visible when the application starts
notifyIcon.Visible = false;
// Start minimised or bring for to the top of the Z order
if (StartMinimised) // Start minimised
{
this.WindowState = FormWindowState.Minimized;
}
else // Start normally
{
// Bring this form to the front of the screen
this.WindowState = FormWindowState.Minimized;
this.Show();
this.WindowState = FormWindowState.Normal;

// Ensure that the system tray icon is not visible when the application starts
notifyIcon.Visible = false;
this.BringToFront();
}
}
catch (Exception ex)
{
Expand Down Expand Up @@ -431,22 +446,21 @@ private void Form1_FormClosed(object sender, FormClosedEventArgs e)

#region Utility methods

private void CloseServer()
/// <summary>
/// Restore the server form when minimised
/// </summary>
private void RestoreForm()
{
// Check whether the server is configured to ask for shutdown confirmation
if (ConfirmExit) // Confirmation is required
{
// Ask the user whether they want to close the remote server
DialogResult result = MessageBox.Show("Are you sure you want to close the Remote Server?", "ASCOM Remote Server", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
// Show the form
this.Show();

// An OK result means the user wants to shut down the Remote Server so close the server form, otherwise do nothing
if (result == DialogResult.OK)
this.Close();
}
else // No confirmation is required so go ahead and shut down the application.
{
this.Close();
}
// Restore to the window state in use before the application was minimised
this.WindowState = formWindowState;

// Hide the system tray icon
notifyIcon.Visible = false;

this.BringToFront();
}

private uint GetServerTransactionID()
Expand Down Expand Up @@ -1534,6 +1548,7 @@ public static void ReadProfile()
UseUtcTimeInLogs = driverProfile.GetValue<bool>(USE_UTC_TIME_IN_LOGS_PROFILENAME, string.Empty, USE_UTC_TIME_IN_LOGS_ENABLED_DEFAULT);
MinimiseToSystemTray = driverProfile.GetValue<bool>(MINIMISE_TO_SYSTEM_TRAY_PROFILENAME, string.Empty, MINIMISE_TO_SYSTEM_TRAY_DEFAULT);
ConfirmExit = driverProfile.GetValue<bool>(CONFIRM_EXIT_PROFILENAME, string.Empty, CONFIRM_EXIT_DEFAULT);
StartMinimised = driverProfile.GetValue<bool>(MINIMISE_ON_START_PROFILENAME, string.Empty, MINIMISE_ON_START_DEFAULT);

// Set the next log roll-over time using the persisted roll-over time value
SetNextRolloverTime();
Expand Down Expand Up @@ -1652,6 +1667,7 @@ public static void WriteProfile()
driverProfile.SetValueInvariant<bool>(USE_UTC_TIME_IN_LOGS_PROFILENAME, string.Empty, UseUtcTimeInLogs);
driverProfile.SetValueInvariant<bool>(MINIMISE_TO_SYSTEM_TRAY_PROFILENAME, string.Empty, MinimiseToSystemTray);
driverProfile.SetValueInvariant<bool>(CONFIRM_EXIT_PROFILENAME, string.Empty, ConfirmExit);
driverProfile.SetValueInvariant<bool>(MINIMISE_ON_START_PROFILENAME, string.Empty, StartMinimised);

// Update the next roll-over time in case the time has changed
//TL.LogMessage("WriteProfile", $"NextRolloverTime Before: {NextRolloverTime}");
Expand Down Expand Up @@ -1692,26 +1708,6 @@ public static void WriteProfile()

#region Form Event handlers

/// <summary>
/// Handle a click to the system tray context menu title
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void SystemTrayTitle_Click(object sender, EventArgs e)
{
RestoreForm();
}

/// <summary>
/// Handle click on the system tray exit menu item
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void SystemTrayCloseServer_Click(object sender, EventArgs e)
{
CloseServer();
}

private void BtnSetup_Click(object sender, EventArgs e)
{
bool apiEnabled; // Local variables to hold the current server state
Expand Down Expand Up @@ -1772,7 +1768,7 @@ private void BtnSetup_Click(object sender, EventArgs e)
/// <param name="e"></param>
private void BtnExit_Click(object sender, EventArgs e)
{
CloseServer();
this.Close(); // CloseServer();
}

private void BtnConnectDevices_Click(object sender, EventArgs e)
Expand Down Expand Up @@ -1804,6 +1800,30 @@ private void ChkLogRequestsAndResponses_CheckedChanged(object sender, EventArgs
WriteProfile();
}

/// <summary>
/// Handle the form closing event, cancelling the close if the user says "no" whenasked
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ServerForm_FormClosing(object sender, FormClosingEventArgs e)
{
// Check whether the server is configured to ask for shutdown confirmation
if (ConfirmExit) // Confirmation is required
{
// Ask the user whether they want to close the remote server
DialogResult result = MessageBox.Show("Are you sure you want to close the Remote Server?", "ASCOM Remote Server", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);

// An OK result means the user wants to shut down the Remote Server so allow the form to close, otherwise cancel the close.
if (result != DialogResult.OK)
e.Cancel = true;
}
else // No confirmation is required so go ahead and let the application shut down.
{
// No action required
}

}

/// <summary>
/// Lays out server form controls to appear visually pleasing at different sizes.
/// </summary>
Expand Down Expand Up @@ -1878,19 +1898,39 @@ private void ServerForm_Resize(object sender, EventArgs e)
// Control Group 6 - Exit button
BtnExit.Location = new Point(controlCentrePosition, BtnSetup.Top + controlSpacing + 2);

formWindowState = this.WindowState; // Save the current form state for restoration later
// Save the current form state so it can be restored after the application is next minimised
formWindowState = this.WindowState;

// Show the application in the task bar
this.ShowInTaskbar = true;

// Show the form in ALT/TAB
this.FormBorderStyle = FormBorderStyle.Sizable;
}
else // WIndow is minimised so minimise to system tray if configured to do so
{
// Test whether the application should minimise to the task bar or to the system tray
if (MinimiseToSystemTray) // Minimise to system tray
{
Hide(); // Hide the application
notifyIcon.Visible = true; // Make the system tray icon visible
// Hide the application
this.Hide();

// Hide the application from the task bar
this.ShowInTaskbar = false;

// Hide the form from ALT/TAB
this.FormBorderStyle = FormBorderStyle.SizableToolWindow;

// Make the system tray icon visible
notifyIcon.Visible = true;
}
else // Minimise to task bar
{
// This is normal application behaviour so no action required
// Show the application in the task bar
this.ShowInTaskbar = true;

// Show the form in ALT/TAB
this.FormBorderStyle = FormBorderStyle.Sizable;
}
}
}
Expand All @@ -1904,14 +1944,26 @@ private void NotifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
{
RestoreForm();
}
private void RestoreForm()

/// <summary>
/// Handle a click to the system tray context menu title
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void SystemTrayTitle_Click(object sender, EventArgs e)
{
Show(); // Show the form
this.WindowState = formWindowState; // Restore to the window state in use before the application was minimised
notifyIcon.Visible = false; // Hide the system tray icon
//this.BringToFront();
RestoreForm();
}

/// <summary>
/// Handle click on the system tray exit menu item
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void SystemTrayCloseServer_Click(object sender, EventArgs e)
{
this.Close();
}

/// <summary>
/// System tray icon single so update context menu
Expand Down
Loading

0 comments on commit 02444d5

Please sign in to comment.