public void SetupNotificationIcon() { // Create ContextMenu contextMenu = new ContextMenu(); //Create MenuItems miNIOnline = new MenuItem { Text = "Who's Online?", Name="miNIOnline"}; miNIUserName = new MenuItem { Text = "Change Username", Name = nameof(miNIUserName) }; miNIQuit = new MenuItem { Text = "Quit", Name = "miNIQuit" }; // Attach events to MenuItems miNIQuit.Click += (s, e) => { this.Close(); }; miNIUserName.Click += (s, e) => { var userNameWindow = new UserNameWindow(); Action<string>checkHandler = null; checkHandler = async(newUserName) => { if (!string.IsNullOrEmpty(newUserName)) { USER_NAME = newUserName; await MKTwitch.ChangeUser(USER_NAME); } userNameWindow.Check -= checkHandler; }; userNameWindow.Check += checkHandler; userNameWindow.ShowDialog(); }; miNIOnline.Click += (s,e) => { ShowOnlineUsers(); }; contextMenu.MenuItems.AddRange(new MenuItem[] { miNIOnline,miNIUserName, miNIQuit }); notifyIcon = new NotifyIcon() { Icon = Properties.Resources._48_twitch, Text="Twitch Alert", Visible=true}; notifyIcon.DoubleClick += (s, e) => { ShowOnlineUsers(); }; notifyIcon.ContextMenu = contextMenu; }
/// <summary> /// Opens UserName entry window. /// If the name is a valid Twitch user name then change MKTwitch /// over to this new userName. If the name entered is not a valid /// Twitch user then no changes are made in the MKTwitch class /// </summary> /// <returns>bool indicating whether the MKTwitch user name was actually changed</returns> private void GetUserName() { userNameWindow = new UserNameWindow(); Action<string> newUserHandler = null; newUserHandler = async (newUserName) => { if (string.IsNullOrEmpty(newUserName)) return; // If the entered username is already the current one or doesn't exist then do nothing. // NOTE: If username is invalid then maybe a MessageBox saying so or the UserNameWindow // should be re-opened? if (newUserName == USER_NAME || !MKTwitch.UserExists(newUserName)) return; USER_NAME = newUserName; if (MKTwitch.IsStarted) { MKTwitch.CancelPopupCycle = true; notifyIcon.Text = $"TwitchAlert ({USER_NAME})\nRetrieving information..."; try { miNIUserName.Enabled = false; await MKTwitch.ChangeUser(USER_NAME); } catch(Exception ex) { System.Windows.MessageBox.Show(ex.Message,"TwitchAlert"); } finally { miNIUserName.Enabled = true; } notifyIcon.Text = $"TwitchAlert ({USER_NAME})\nFollowing {MKTwitch.followedUsers.Count} ({MKTwitch.followedUsers.Count(i => i.IsStreaming)} Online)"; } userNameWindow.NewUser -= newUserHandler; userNameWindow = null; }; userNameWindow.NewUser += newUserHandler; userNameWindow.ShowDialog(); }