diff --git a/ntfysh_client/MainForm.cs b/ntfysh_client/MainForm.cs index bd1bf9f..7aeb861 100644 --- a/ntfysh_client/MainForm.cs +++ b/ntfysh_client/MainForm.cs @@ -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); diff --git a/ntfysh_client/MainForm.resx b/ntfysh_client/MainForm.resx index dbf516e..85f0136 100644 --- a/ntfysh_client/MainForm.resx +++ b/ntfysh_client/MainForm.resx @@ -67,7 +67,7 @@ iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABGd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McUxMjKuBrGB9DIgPxqmARhwOUBD3sD4WA0QERGRBHLvCwoKygLpm6KiohIw DVBNz5HY2L0AtHkOEE8F4okgvoyMjDSSJsIGsLOzuwKFfnBycppJS0vLAJ39BUQTbQA6RnYBMkY3YAVI diff --git a/ntfysh_client/Program.cs b/ntfysh_client/Program.cs index 5841c89..a64b094 100644 --- a/ntfysh_client/Program.cs +++ b/ntfysh_client/Program.cs @@ -1,25 +1,46 @@ using System; -using System.Collections.Generic; +using System.Diagnostics; +using System.IO; using System.Linq; -using System.Threading.Tasks; +using System.Reflection; using System.Windows.Forms; namespace ntfysh_client { static class Program { - private static readonly NotificationListener NotificationListener = new NotificationListener(); + private static readonly NotificationListener NotificationListener = new(); /// /// The main entry point for the application. /// [STAThread] - static void Main() + private static void Main(string[] args) { + args = args.Select(a => a.ToLower()).ToArray(); + + if (args.Contains("-h") || args.Contains("--help")) + { + MessageBox.Show("Help:\n -h\n --help\n\nStart in tray:\n -t\n --start-in-tray\n\nAllow multiple instances:\n -m\n --allow-multiple-instances", "Help Menu", MessageBoxButtons.OK, MessageBoxIcon.Information); + return; + } + + bool startInTray = args.Contains("-t") || args.Contains("--start-in-tray"); + bool allowMultipleInstances = args.Contains("-m") || args.Contains("--allow-multiple-instances"); + + if (Process.GetProcessesByName(Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly()!.Location)).Length > 1) + { + if (!allowMultipleInstances) + { + MessageBox.Show("Another instance is already running.\n\nUse -m or --allow-multiple-instances if you wish to start a second duplicate instance.\n\nThis instance will now close.", "Multiple Instances", MessageBoxButtons.OK, MessageBoxIcon.Warning); + return; + } + } + Application.SetHighDpiMode(HighDpiMode.SystemAware); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); - Application.Run(new MainForm(NotificationListener)); + Application.Run(new MainForm(NotificationListener, startInTray)); } } }