Exemplo n.º 1
0
 private void NotifyIcon_DoubleClick(object sender, EventArgs e)
 {
     configurationUpdater.ShowConfigurationForm();
 }
        public TrayApplicationContext(
            ILogger<TrayApplicationContext> logger,
            TrayContextMenuStrip contextMenu,
            NotifyIconManager notifyIconManager,
            AppState appState,
            StartupManager startupManager,
            ConfigurationUpdater configurationUpdater
        )
        {
            if (notifyIconManager == null)
            {
                throw new ArgumentNullException(nameof(notifyIconManager));
            }

            if (appState == null)
            {
                throw new ArgumentNullException(nameof(appState));
            }

            if (startupManager == null)
            {
                throw new ArgumentNullException(nameof(startupManager));
            }

            if (configurationUpdater == null)
            {
                throw new ArgumentNullException(nameof(configurationUpdater));
            }

            this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
            this.contextMenu = contextMenu ?? throw new ArgumentNullException(nameof(contextMenu));

            notifyIconManager.NotifyIcon.ContextMenuStrip = contextMenu;

            appState.OnMachineStateChange += (object _, MachineStateChangeEventArgs __) => UpdateContextMenu();

            appState.OnConfigurationChange += (object _, ConfigurationChangeEventArgs e) =>
            {
                if (e.NewConfiguration.StartWithWindows != startupManager.IsEnabled())
                {
                    if (e.NewConfiguration.StartWithWindows)
                    {
                        startupManager.EnableStartup();
                    }
                    else
                    {
                        startupManager.DisableStartup();
                    }
                }

                if (e.NewConfiguration.ShowTrayIcon)
                {
                    notifyIconManager.ShowIcon();
                }
                else
                {
                    notifyIconManager.HideIcon();
                }

                CreateContextMenu();
            };

            if (appState.Configuration.ShowTrayIcon)
            {
                notifyIconManager.ShowIcon();
            }
            else if (!Program.IsAutoStarting())
            {
                // If the program is not auto-starting and the tray icon is not visible the user launched the
                // application manually and it was not already running.  We will show the configuration under the
                // assumption they are looking for it.
                configurationUpdater.ShowConfigurationForm();
            }

            CreateContextMenu();
        }