Default to preventing multiple instances

Allow multiple instances if overridden with command line parameter
Allow starting minimised to tray with command line parameter
This commit is contained in:
Alexander Horner
2022-12-08 19:04:43 +00:00
parent 2a0d41759b
commit c4291e6f44
3 changed files with 58 additions and 13 deletions

View File

@@ -13,17 +13,43 @@ namespace ntfysh_client
public partial class MainForm : Form
{
private readonly NotificationListener _notificationListener;
private bool _startInTray;
private bool _trueExit;
public MainForm(NotificationListener notificationListener)
public MainForm(NotificationListener notificationListener, bool startInTray = false)
{
_notificationListener = notificationListener;
_startInTray = startInTray;
_notificationListener.OnNotificationReceive += OnNotificationReceive;
_notificationListener.OnConnectionMultiAttemptFailure += OnConnectionMultiAttemptFailure;
_notificationListener.OnConnectionCredentialsFailure += OnConnectionCredentialsFailure;
InitializeComponent();
}
private void MainForm_Load(object sender, EventArgs e) => LoadTopics();
protected override void SetVisibleCore(bool value)
{
if (_startInTray)
{
_startInTray = false;
/*
* TODO This little workaround prevents the window from appearing with a flash, but the taskbar icon appears for a moment.
*
* TODO This is because we must call SetVisibleCore(true) for the initial load events in the MainForm to fire, which is what triggers the listener
*/
Opacity = 0;
base.SetVisibleCore(true);
base.SetVisibleCore(false);
Opacity = 1;
return;
}
base.SetVisibleCore(value);
}
private void OnNotificationReceive(object sender, NotificationReceiveEventArgs e)
{
@@ -42,8 +68,6 @@ namespace ntfysh_client
MessageBox.Show($"Connecting to topic ID '{topic.TopicId}' on server '{topic.ServerUrl}' failed because {reason}.\n\nThis topic ID will be ignored and you will not receive notifications for it until you correct the credentials.", "Connection Authentication Failure", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
private void MainForm_Load(object sender, EventArgs e) => LoadTopics();
private void subscribeNewTopic_Click(object sender, EventArgs e)
{
using SubscribeDialog dialog = new SubscribeDialog(notificationTopics);