/// <summary> /// When a message is received in the room. /// </summary> /// <param name="sender"> /// The sender. /// </param> /// <param name="id">The message Id</param> /// <param name="from"> /// The from. /// </param> /// <param name="message"> /// The message. /// </param> /// <param name="receiveTime"> /// The receive Time. /// </param> /// <param name="messageType"> /// The message type. /// </param> private void RoomOnMessageReceived(object sender, string id, User @from, string message, DateTime receiveTime, LobbyMessageType messageType) { var theFrom = from; var theMessage = message; var therTime = receiveTime; var themType = messageType; var theId = id; if (string.IsNullOrWhiteSpace(theFrom.UserName)) { theFrom.UserName = "******"; } if (theFrom.UserName.Equals("octgn-gap", StringComparison.InvariantCultureIgnoreCase)) { var cl = theMessage.IndexOf(':'); theFrom.UserName = "******" + theMessage.Substring(0, cl) + "]"; theMessage = theMessage.Substring(cl + 1, theMessage.Length - cl - 1).Trim(); } if (theMessage.ToLowerInvariant().Contains(Program.LobbyClient.Me.UserName.ToLowerInvariant())) { Sounds.PlayMessageSound(); } Dispatcher.Invoke( new Action( () => { // Got mostly from http://stackoverflow.com/questions/13456441/wpf-richtextboxs-conditionally-scroll keepScrolledToBottom = false; var offset = Chat.VerticalOffset + Chat.ViewportHeight; // How far the scroll bar is up from the bottom if (Math.Abs(offset - Chat.ExtentHeight) <= 15) { // We should auto scroll keepScrolledToBottom = true; this.shouldDisplayMissedMessagesBreak = false; //Chat.ScrollToEnd(); } else { var contentIsLargerThatViewport = Chat.ExtentHeight > Chat.ViewportHeight; // How far the scroll bar is up from the bottom if (Math.Abs(Chat.VerticalOffset - 0) < 15 && contentIsLargerThatViewport) { // We should auto scroll keepScrolledToBottom = true; this.shouldDisplayMissedMessagesBreak = false; //Chat.ScrollToEnd(); } else { // We shouldn't auto scroll, instead show the missed message thingy if (!this.shouldDisplayMissedMessagesBreak) { var missed = new MissedMessagesBreak(); ChatRowGroup.Rows.Add(missed); this.shouldDisplayMissedMessagesBreak = true; } } } var ctr = new ChatTableRow(theFrom, theId, theMessage, therTime, themType); //var ctr = new ChatTableRow { User = theFrom, Message = theMessage, MessageDate = therTime, MessageType = themType }; ctr.OnMouseUsernameEnter += ChatTableRow_MouseEnter; ctr.OnMouseUsernameLeave += ChatTableRow_MouseLeave; ChatRowGroup.Rows.Add(ctr); if (ChatRowGroup.Rows.Count > Prefs.MaxChatHistory) { var remlist = ChatRowGroup.Rows.Take(ChatRowGroup.Rows.Count - Prefs.MaxChatHistory).ToArray(); foreach (var r in remlist) { ChatRowGroup.Rows.Remove(r); } } })); Dispatcher.BeginInvoke(new Action( () => { if (keepScrolledToBottom && IsLoadingHistory == false) { Chat.ScrollToEnd(); } })); }
/// <summary> /// When a message is received in the room. /// </summary> /// <param name="sender"> /// The sender. /// </param> /// <param name="from"> /// The from. /// </param> /// <param name="message"> /// The message. /// </param> /// <param name="receiveTime"> /// The receive Time. /// </param> /// <param name="messageType"> /// The message type. /// </param> private void RoomOnMessageReceived(object sender, User @from, string message, DateTime receiveTime, LobbyMessageType messageType) { var theFrom = from; var theMessage = message; var therTime = receiveTime; var themType = messageType; if (string.IsNullOrWhiteSpace(theFrom.UserName)) { theFrom.UserName = "******"; } if (theFrom.UserName.Equals("octgn-gap", StringComparison.InvariantCultureIgnoreCase)) { var cl = theMessage.IndexOf(':'); theFrom.UserName = "******" + theMessage.Substring(0, cl) + "]"; theMessage = theMessage.Substring(cl + 1, theMessage.Length - cl - 1).Trim(); } if (theMessage.ToLowerInvariant().Contains("@" + Program.LobbyClient.Me.UserName.ToLowerInvariant())) { Sounds.PlayMessageSound(); } Dispatcher.Invoke( new Action( () => { // Got mostly from http://stackoverflow.com/questions/13456441/wpf-richtextboxs-conditionally-scroll var rtbatbottom = false; var offset = Chat.VerticalOffset + Chat.ViewportHeight; if (Math.Abs(offset - Chat.ExtentHeight) <= double.Epsilon) { rtbatbottom = true; justScrolledToBottom = false; //Chat.ScrollToEnd(); } else { var contentIsLargerThatViewport = Chat.ExtentHeight > Chat.ViewportHeight; if (Math.Abs(Chat.VerticalOffset - 0) < double.Epsilon && contentIsLargerThatViewport) { rtbatbottom = true; justScrolledToBottom = false; //Chat.ScrollToEnd(); } else { if (!justScrolledToBottom) { var missed = new MissedMessagesBreak(); ChatRowGroup.Rows.Add(missed); justScrolledToBottom = true; } } } //if (Math.Abs(dVer - 0) > double.Epsilon) //{ // if (Math.Abs(dVer + dViewport - dExtent) < double.Epsilon) // { // rtbatbottom = true; // justScrolledToBottom = false; // } // else // { // if (!justScrolledToBottom) // { // var missed = new MissedMessagesBreak(); // ChatRowGroup.Rows.Add(missed); // justScrolledToBottom = true; // } // } //} var ctr = new ChatTableRow(theFrom, theMessage, therTime, themType); //var ctr = new ChatTableRow { User = theFrom, Message = theMessage, MessageDate = therTime, MessageType = themType }; ctr.OnMouseUsernameEnter += ChatTableRow_MouseEnter; ctr.OnMouseUsernameLeave += ChatTableRow_MouseLeave; ChatRowGroup.Rows.Add(ctr); if (rtbatbottom) { Chat.ScrollToEnd(); } if (ChatRowGroup.Rows.Count > Prefs.MaxChatHistory) { var remlist = ChatRowGroup.Rows.Take(ChatRowGroup.Rows.Count - Prefs.MaxChatHistory).ToArray(); foreach (var r in remlist) { ChatRowGroup.Rows.Remove(r); } } })); }
/// <summary> /// When a message is received in the room. /// </summary> /// <param name="sender"> /// The sender. /// </param> /// <param name="id">The message Id</param> /// <param name="from"> /// The from. /// </param> /// <param name="message"> /// The message. /// </param> /// <param name="receiveTime"> /// The receive Time. /// </param> /// <param name="messageType"> /// The message type. /// </param> private void RoomOnMessageReceived(object sender, string id, User @from, string message, DateTime receiveTime, LobbyMessageType messageType) { var theFrom = from; var theMessage = message; var therTime = receiveTime; var themType = messageType; var theId = id; if (string.IsNullOrWhiteSpace(theFrom.UserName)) { theFrom.UserName = "******"; } if (theFrom.UserName.Equals("octgn-gap", StringComparison.InvariantCultureIgnoreCase)) { var cl = theMessage.IndexOf(':'); theFrom.UserName = "******" + theMessage.Substring(0, cl) + "]"; theMessage = theMessage.Substring(cl + 1, theMessage.Length - cl - 1).Trim(); } if (theMessage.ToLowerInvariant().Contains("@" + Program.LobbyClient.Me.UserName.ToLowerInvariant())) { Sounds.PlayMessageSound(); } Dispatcher.Invoke( new Action( () => { // Got mostly from http://stackoverflow.com/questions/13456441/wpf-richtextboxs-conditionally-scroll var rtbatbottom = false; var offset = Chat.VerticalOffset + Chat.ViewportHeight; if (Math.Abs(offset - Chat.ExtentHeight) <= double.Epsilon) { rtbatbottom = true; justScrolledToBottom = false; //Chat.ScrollToEnd(); } else { var contentIsLargerThatViewport = Chat.ExtentHeight > Chat.ViewportHeight; if (Math.Abs(Chat.VerticalOffset - 0) < double.Epsilon && contentIsLargerThatViewport) { rtbatbottom = true; justScrolledToBottom = false; //Chat.ScrollToEnd(); } else { if (!justScrolledToBottom) { var missed = new MissedMessagesBreak(); ChatRowGroup.Rows.Add(missed); justScrolledToBottom = true; } } } //if (Math.Abs(dVer - 0) > double.Epsilon) //{ // if (Math.Abs(dVer + dViewport - dExtent) < double.Epsilon) // { // rtbatbottom = true; // justScrolledToBottom = false; // } // else // { // if (!justScrolledToBottom) // { // var missed = new MissedMessagesBreak(); // ChatRowGroup.Rows.Add(missed); // justScrolledToBottom = true; // } // } //} //if ( // ChatRowGroup.Rows.OfType<ChatTableRow>() // .Any(x => // x.Id != null // && x.Id.Equals(theId, StringComparison.InvariantCultureIgnoreCase) // && x.Message.Equals(message) // )) //{ // return; //} var ctr = new ChatTableRow(theFrom, theId, theMessage, therTime, themType); //var ctr = new ChatTableRow { User = theFrom, Message = theMessage, MessageDate = therTime, MessageType = themType }; ctr.OnMouseUsernameEnter += ChatTableRow_MouseEnter; ctr.OnMouseUsernameLeave += ChatTableRow_MouseLeave; ChatRowGroup.Rows.Add(ctr); if (rtbatbottom) { Chat.ScrollToEnd(); } if (ChatRowGroup.Rows.Count > Prefs.MaxChatHistory) { var remlist = ChatRowGroup.Rows.Take(ChatRowGroup.Rows.Count - Prefs.MaxChatHistory).ToArray(); foreach (var r in remlist) { ChatRowGroup.Rows.Remove(r); } } })); }