Exemplo n.º 1
0
        public TrayAppContext()
        {
            NotifyIcon_ShowContextMenu = typeof(NotifyIcon).GetMethod("ShowContextMenu", BindingFlags.NonPublic | BindingFlags.Instance);

            // Upgrade settings first
            var settings = Properties.Settings.Default;

            if (!settings.SettingsUpgraded)
            {
                settings.Upgrade();
                settings.SettingsUpgraded = true;
                settings.Save();
            }

            loginMenuItem = new MenuItem(Properties.Resources.LogIn, (sender, e) =>
            {
                promptLogin();
            })
            {
                Visible = false
            };
            refreshMenuItem = new MenuItem(Properties.Resources.RefreshNow, (sender, e) =>
            {
                updateNotifications();
            });
            muteMenuItem = new MenuItem(Properties.Resources.TempMute, (sender, e) =>
            {
                muted = !muted;
                muteMenuItem.Checked = muted;
            })
            {
                Visible = settings.EnableBalloons
            };

            appContextMenu = new ContextMenu(new MenuItem[] {
                loginMenuItem,
                refreshMenuItem,
                muteMenuItem,
                new MenuItem(Properties.Resources.Settings, (sender, e) =>
                {
                    var settingsForm              = new SettingsForm();
                    settingsForm.SettingsApplied += settingsForm_SettingsApplied;
                    settingsForm.LoggingOut      += settingsForm_LoggingOut;
                    settingsForm.Show();
                }),
                new MenuItem(Properties.Resources.Exit, (sender, e) => {
                    client.SaveCredentials();
                    Application.Exit();
                })
            });

            setupNotificationsPopup();

            refreshTimer.Interval = settings.RefreshInterval;
            refreshTimer.Tick    += refreshTimer_Tick;

            // Must do this true/false charade to get context menus associated for some reason
            countIcon.ContextMenu = appContextMenu;
            countIcon.Visible     = true;
            countIcon.Visible     = false;
            mainIcon.ContextMenu  = appContextMenu;
            mainIcon.Text         = Application.ProductName;
            mainIcon.Visible      = true;
            mainIcon.Visible      = false;

            mainIcon.MouseDown          += notifyIcon_MouseDown;
            mainIcon.MouseClick         += notifyIcon_MouseClick;
            mainIcon.MouseDoubleClick   += notifyIcon_MouseDoubleClick;
            countIcon.MouseDown         += notifyIcon_MouseDown;
            countIcon.MouseClick        += notifyIcon_MouseClick;
            countIcon.MouseDoubleClick  += notifyIcon_MouseDoubleClick;
            countIcon.BalloonTipClicked += countIcon_BalloonTipClicked;

            // If no cookies available, show login form
            //CredentialStore.ClearCredentials();
            if (!CredentialStore.CredentialsAvailable())
            {
                promptLogin();
            }
            else
            {
                finishSetup();
            }
        }