Apricot.Balloon.OnKeyDown C# (CSharp) Method

OnKeyDown() protected method

protected OnKeyDown ( System.Windows.Input.KeyEventArgs e ) : void
e System.Windows.Input.KeyEventArgs
return void
        protected override void OnKeyDown(KeyEventArgs e)
        {
            base.OnKeyDown(e);

            if (e.Key == Key.Up)
            {
                if (this.messageCollection.Count > 0 && this.historyPoint.HasValue && this.historyPoint.Value < this.messageCollection.Count)
                {
                    if (this.enableFilter)
                    {
                        Message message = this.messageCollection[this.historyPoint.Value];

                        if (message.HasAttachments)
                        {
                            double interval = GetNextInterval(message.Attachments, this.nextThresholdScore, 1, true);

                            if (this.nextThresholdScore - interval < this.minScore)
                            {
                                this.thresholdQueue.Enqueue(this.minScore - this.nextThresholdScore);
                                this.nextThresholdScore = this.minScore;
                            }
                            else
                            {
                                this.thresholdQueue.Enqueue(-interval);
                                this.nextThresholdScore -= interval;
                            }
                        }
                    }
                    else
                    {
                        if (this.selectedPosition.HasValue)
                        {
                            this.selectedPositionQueue.Enqueue(-1);
                        }
                        else
                        {
                            this.scrollQueue.Enqueue(-1);
                        }
                    }
                }
            }
            else if (e.Key == Key.Down)
            {
                if (this.messageCollection.Count > 0 && this.historyPoint.HasValue && this.historyPoint.Value < this.messageCollection.Count)
                {
                    if (this.enableFilter)
                    {
                        Message message = this.messageCollection[this.historyPoint.Value];

                        if (message.HasAttachments)
                        {
                            double interval = GetNextInterval(message.Attachments, this.nextThresholdScore, 1, false);

                            if (this.nextThresholdScore + interval > this.maxScore)
                            {
                                this.thresholdQueue.Enqueue(this.maxScore - this.nextThresholdScore);
                                this.nextThresholdScore = this.maxScore;
                            }
                            else
                            {
                                this.thresholdQueue.Enqueue(interval);
                                this.nextThresholdScore += interval;
                            }
                        }
                    }
                    else
                    {
                        if (this.selectedPosition.HasValue)
                        {
                            this.selectedPositionQueue.Enqueue(1);
                        }
                        else
                        {
                            this.scrollQueue.Enqueue(1);
                        }
                    }
                }
            }
            else if (e.Key == Key.PageUp)
            {
                if (this.messageCollection.Count > 0 && this.historyPoint.HasValue && this.historyPoint.Value < this.messageCollection.Count)
                {
                    if (this.selectedPosition.HasValue)
                    {
                        if (this.targetScrollPosition > 0 && this.targetScrollPosition < this.numberOfLines)
                        {
                            this.selectedPositionQueue.Enqueue(-this.targetScrollPosition);
                        }
                        else
                        {
                            this.selectedPositionQueue.Enqueue(-this.numberOfLines);
                        }
                    }
                    else
                    {
                        this.scrollQueue.Enqueue(-this.numberOfLines);
                    }
                }
            }
            else if (e.Key == Key.PageDown || e.Key == Key.Space)
            {
                if (this.messageCollection.Count > 0 && this.historyPoint.HasValue && this.historyPoint.Value < this.messageCollection.Count)
                {
                    double lines = this.numberOfLines;

                    if (this.selectedPosition.HasValue)
                    {
                        Message message = this.messageCollection[this.historyPoint.Value];

                        if (message.Attachments.Count > (this.selectedPosition.Value + this.numberOfLines) && message.Attachments.Count - (this.selectedPosition.Value + this.numberOfLines) < this.numberOfLines)
                        {
                            double x = 0;
                            int messageLines = 0;
                            bool isBreaked = true;
                            bool isReseted = true;

                            foreach (object o in message)
                            {
                                string inline = o as string;
                                Brush brush = this.textBrush;
                                Dictionary<int, int> dictionary = new Dictionary<int, int>();
                                StringBuilder lineStringBuilder = new StringBuilder();

                                if (inline == null)
                                {
                                    Entry entry = o as Entry;

                                    if (entry == null)
                                    {
                                        inline = o.ToString();
                                    }
                                    else
                                    {
                                        inline = entry.Title;
                                        brush = this.linkBrush;
                                    }
                                }

                                foreach (System.Text.RegularExpressions.Match match in System.Text.RegularExpressions.Regex.Matches(inline, @"[\p{IsBasicLatin}-[\s]]+\s?"))
                                {
                                    dictionary.Add(match.Index, match.Length);
                                }

                                for (int i = 0; i < inline.Length; i++)
                                {
                                    int length;

                                    if (dictionary.TryGetValue(i, out length) && x + Math.Ceiling(new FormattedText(String.Concat(lineStringBuilder.ToString(), inline.Substring(i, length)), CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface(this.FontFamily, this.FontStyle, this.FontWeight, this.FontStretch), this.FontSize, brush).WidthIncludingTrailingWhitespace) > this.maxMessageSize.Width && !isReseted)
                                    {
                                        lineStringBuilder.Clear();
                                        x = 0;
                                        messageLines++;
                                        isBreaked = true;
                                    }

                                    lineStringBuilder.Append(inline[i]);

                                    if (lineStringBuilder.ToString().EndsWith(Environment.NewLine, StringComparison.Ordinal))
                                    {
                                        lineStringBuilder.Remove(lineStringBuilder.ToString().LastIndexOf(Environment.NewLine, StringComparison.Ordinal), Environment.NewLine.Length);

                                        if (lineStringBuilder.Length > 0)
                                        {
                                            lineStringBuilder.Remove(0, lineStringBuilder.Length);
                                        }

                                        x = 0;
                                        messageLines++;
                                        isBreaked = true;
                                        isReseted = true;
                                    }
                                    else if (x + Math.Ceiling(new FormattedText(lineStringBuilder.ToString(), CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface(this.FontFamily, this.FontStyle, this.FontWeight, this.FontStretch), this.FontSize, brush).WidthIncludingTrailingWhitespace) > this.maxMessageSize.Width)
                                    {
                                        if (lineStringBuilder.Length - 1 > 0)
                                        {
                                            lineStringBuilder.Remove(0, lineStringBuilder.Length - 1);
                                        }

                                        x = 0;
                                        messageLines++;
                                        isBreaked = true;
                                        isReseted = true;
                                    }
                                    else
                                    {
                                        isReseted = false;
                                    }
                                }

                                if (lineStringBuilder.Length > 0)
                                {
                                    x += Math.Ceiling(new FormattedText(lineStringBuilder.ToString(), CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface(this.FontFamily, this.FontStyle, this.FontWeight, this.FontStretch), this.FontSize, brush).WidthIncludingTrailingWhitespace);
                                    isBreaked = false;
                                    isReseted = false;
                                }
                            }

                            if (isBreaked)
                            {
                                messageLines++;
                            }

                            lines = messageLines + this.selectedPosition.Value - this.targetScrollPosition + message.Attachments.Count - (this.selectedPosition.Value + this.numberOfLines);
                        }

                        this.selectedPositionQueue.Enqueue(lines);
                    }
                    else
                    {
                        this.scrollQueue.Enqueue(lines);
                    }
                }
            }
            else if (e.Key == Key.Home)
            {
                if (this.messageCollection.Count > 0 && this.historyPoint.HasValue && this.historyPoint.Value < this.messageCollection.Count)
                {
                    if (this.selectedPosition.HasValue)
                    {
                        Message message = this.messageCollection[this.historyPoint.Value];
                        double x = 0;
                        int messageLines = 0;
                        bool isBreaked = true;
                        bool isReseted = true;

                        foreach (object o in message)
                        {
                            string inline = o as string;
                            Brush brush = this.textBrush;
                            Dictionary<int, int> dictionary = new Dictionary<int, int>();
                            StringBuilder lineStringBuilder = new StringBuilder();

                            if (inline == null)
                            {
                                Entry entry = o as Entry;

                                if (entry == null)
                                {
                                    inline = o.ToString();
                                }
                                else
                                {
                                    inline = entry.Title;
                                    brush = this.linkBrush;
                                }
                            }

                            foreach (System.Text.RegularExpressions.Match match in System.Text.RegularExpressions.Regex.Matches(inline, @"[\p{IsBasicLatin}-[\s]]+\s?"))
                            {
                                dictionary.Add(match.Index, match.Length);
                            }

                            for (int i = 0; i < inline.Length; i++)
                            {
                                int length;

                                if (dictionary.TryGetValue(i, out length) && x + Math.Ceiling(new FormattedText(String.Concat(lineStringBuilder.ToString(), inline.Substring(i, length)), CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface(this.FontFamily, this.FontStyle, this.FontWeight, this.FontStretch), this.FontSize, brush).WidthIncludingTrailingWhitespace) > this.maxMessageSize.Width && !isReseted)
                                {
                                    lineStringBuilder.Clear();
                                    x = 0;
                                    messageLines++;
                                    isBreaked = true;
                                }

                                lineStringBuilder.Append(inline[i]);

                                if (lineStringBuilder.ToString().EndsWith(Environment.NewLine, StringComparison.Ordinal))
                                {
                                    lineStringBuilder.Remove(lineStringBuilder.ToString().LastIndexOf(Environment.NewLine, StringComparison.Ordinal), Environment.NewLine.Length);

                                    if (lineStringBuilder.Length > 0)
                                    {
                                        lineStringBuilder.Remove(0, lineStringBuilder.Length);
                                    }

                                    x = 0;
                                    messageLines++;
                                    isBreaked = true;
                                    isReseted = true;
                                }
                                else if (x + Math.Ceiling(new FormattedText(lineStringBuilder.ToString(), CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface(this.FontFamily, this.FontStyle, this.FontWeight, this.FontStretch), this.FontSize, brush).WidthIncludingTrailingWhitespace) > this.maxMessageSize.Width)
                                {
                                    if (lineStringBuilder.Length - 1 > 0)
                                    {
                                        lineStringBuilder.Remove(0, lineStringBuilder.Length - 1);
                                    }

                                    x = 0;
                                    messageLines++;
                                    isBreaked = true;
                                    isReseted = true;
                                }
                                else
                                {
                                    isReseted = false;
                                }
                            }

                            if (lineStringBuilder.Length > 0)
                            {
                                x += Math.Ceiling(new FormattedText(lineStringBuilder.ToString(), CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface(this.FontFamily, this.FontStyle, this.FontWeight, this.FontStretch), this.FontSize, brush).WidthIncludingTrailingWhitespace);
                                isBreaked = false;
                                isReseted = false;
                            }
                        }

                        if (isBreaked)
                        {
                            messageLines++;
                        }

                        this.selectedPositionQueue.Enqueue(-(messageLines + this.selectedPosition.Value));
                    }
                    else
                    {
                        this.scrollQueue.Enqueue(-this.targetScrollPosition);
                    }
                }
            }
            else if (e.Key == Key.End)
            {
                if (this.messageCollection.Count > 0 && this.historyPoint.HasValue && this.historyPoint.Value < this.messageCollection.Count)
                {
                    Message message = this.messageCollection[this.historyPoint.Value];
                    double x = 0;
                    int messageLines = 0;
                    bool isBreaked = true;
                    bool isReseted = true;

                    foreach (object o in message)
                    {
                        string inline = o as string;
                        Brush brush = this.textBrush;
                        Dictionary<int, int> dictionary = new Dictionary<int, int>();
                        StringBuilder lineStringBuilder = new StringBuilder();

                        if (inline == null)
                        {
                            Entry entry = o as Entry;

                            if (entry == null)
                            {
                                inline = o.ToString();
                            }
                            else
                            {
                                inline = entry.Title;
                                brush = this.linkBrush;
                            }
                        }

                        foreach (System.Text.RegularExpressions.Match match in System.Text.RegularExpressions.Regex.Matches(inline, @"[\p{IsBasicLatin}-[\s]]+\s?"))
                        {
                            dictionary.Add(match.Index, match.Length);
                        }

                        for (int i = 0; i < inline.Length; i++)
                        {
                            int length;

                            if (dictionary.TryGetValue(i, out length) && x + Math.Ceiling(new FormattedText(String.Concat(lineStringBuilder.ToString(), inline.Substring(i, length)), CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface(this.FontFamily, this.FontStyle, this.FontWeight, this.FontStretch), this.FontSize, brush).WidthIncludingTrailingWhitespace) > this.maxMessageSize.Width && !isReseted)
                            {
                                lineStringBuilder.Clear();
                                x = 0;
                                messageLines++;
                                isBreaked = true;
                            }

                            lineStringBuilder.Append(inline[i]);

                            if (lineStringBuilder.ToString().EndsWith(Environment.NewLine, StringComparison.Ordinal))
                            {
                                lineStringBuilder.Remove(lineStringBuilder.ToString().LastIndexOf(Environment.NewLine, StringComparison.Ordinal), Environment.NewLine.Length);

                                if (lineStringBuilder.Length > 0)
                                {
                                    lineStringBuilder.Remove(0, lineStringBuilder.Length);
                                }

                                x = 0;
                                messageLines++;
                                isBreaked = true;
                                isReseted = true;
                            }
                            else
                            {
                                if (x + Math.Ceiling(new FormattedText(lineStringBuilder.ToString(), CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface(this.FontFamily, this.FontStyle, this.FontWeight, this.FontStretch), this.FontSize, brush).WidthIncludingTrailingWhitespace) > this.maxMessageSize.Width)
                                {
                                    if (lineStringBuilder.Length - 1 > 0)
                                    {
                                        lineStringBuilder.Remove(0, lineStringBuilder.Length - 1);
                                    }

                                    x = 0;
                                    messageLines++;
                                    isBreaked = true;
                                    isReseted = true;
                                }
                                else
                                {
                                    isReseted = false;
                                }
                            }
                        }

                        if (lineStringBuilder.Length > 0)
                        {
                            x += Math.Ceiling(new FormattedText(lineStringBuilder.ToString(), CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface(this.FontFamily, this.FontStyle, this.FontWeight, this.FontStretch), this.FontSize, brush).WidthIncludingTrailingWhitespace);
                            isBreaked = false;
                            isReseted = false;
                        }
                    }

                    if (isBreaked)
                    {
                        messageLines++;
                    }

                    if (this.selectedPosition.HasValue)
                    {
                        this.selectedPositionQueue.Enqueue(messageLines + message.Attachments.Count - this.selectedPosition.Value);
                    }
                    else
                    {
                        this.scrollQueue.Enqueue(messageLines + message.Attachments.Count - this.targetScrollPosition);
                    }
                }
            }
            else if (e.Key == Key.Escape)
            {
                this.sourceOpacity = this.Canvas.Opacity;
                this.targetOpacity = 0;
                this.sourceScaleX = this.ScaleTransform.ScaleX;
                this.sourceScaleY = this.ScaleTransform.ScaleY;
                this.targetScaleX = this.targetScaleY = 0;
                this.popupStep = 0;
            }
            else if (e.Key == Key.Back)
            {
                Back();
            }
            else if ((Keyboard.Modifiers & (ModifierKeys.Control | ModifierKeys.Shift)) == (ModifierKeys.Control | ModifierKeys.Shift) && !this.enableFilter)
            {
                if (this.messageCollection.Count > 0 && this.historyPoint.HasValue && this.historyPoint.Value < this.messageCollection.Count)
                {
                    Message message = this.messageCollection[this.historyPoint.Value];
                    var query = from entry in message.Attachments where entry.Score.HasValue select entry.Score.Value;

                    if (query.Any())
                    {
                        List<double> scoreList = query.ToList();

                        this.enableFilter = false;
                        this.maxScore = scoreList.Max<double>();
                        this.minScore = scoreList.Min<double>();
                        this.isReversed = false;

                        for (int i = 0; i < scoreList.Count - 1; i++)
                        {
                            if (scoreList[i] != scoreList[i + 1])
                            {
                                this.enableFilter = true;

                                break;
                            }
                        }

                        if (this.enableFilter)
                        {
                            int counter = 0;

                            for (int i = 0; i < scoreList.Count - 1; i++)
                            {
                                if (scoreList[i] >= scoreList[i + 1])
                                {
                                    counter++;
                                }
                            }

                            if (counter == scoreList.Count - 1)
                            {
                                this.isReversed = true;
                            }
                        }

                        if (this.thresholdScore > this.maxScore || this.thresholdScore < this.minScore)
                        {
                            if (this.isReversed)
                            {
                                this.thresholdScore = this.minScore;
                            }
                            else
                            {
                                this.thresholdScore = this.maxScore;
                            }
                        }
                    }
                }
            }
        }