using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace AppCore.Shell.Diagnostics
{
internal partial class ExceptionNotificationControl : UserControl
{
public ExceptionNotificationControl()
{
AC.App.Ready += this.App_Ready;
}
private int Count
{
get
{
return this._exceptions.Aggregate(0, (int i, ExceptionViewModel model) => i + model.Count);
}
}
internal void OnFirstChanceException(Exception ex, bool isFatal = false)
{
Type exType = ex.GetType();
string exMessage = ex.Message;
ExceptionViewModel exceptionViewModel = this._exceptions.FirstOrDefault((ExceptionViewModel x) => x.Exception.GetType() == exType && x.Exception.Message == exMessage);
if (exceptionViewModel == null)
{
this._exceptions.Insert(0, new ExceptionViewModel(ex));
}
else
{
ExceptionViewModel exceptionViewModel2 = exceptionViewModel;
int count = exceptionViewModel2.Count;
exceptionViewModel2.Count = count + 1;
}
Action action = delegate
{
int count2 = this.Count;
this.TitleTextBox.Text = "本程序发生了" + ((count2 > 1) ? (" " + count2.ToString() + " 次") : "") + (isFatal ? "致命" : string.Empty) + "异常";
if (isFatal)
{
this.TipIcon.Fill = Brushes.Red;
}
this.ContentTextBox.Text = this._exceptions.First<ExceptionViewModel>().TypeName + ((count2 > 1) ? " 等。" : "") + Environment.NewLine + "点击此处查看。如需查看内部异常,请向下滚动。";
};
if (this._isInitialized && !this._isLoaded)
{
action();
this.Show();
}
else if (this._isLoaded && this._isClosing)
{
action();
((Storyboard)base.FindResource("LoadStoryboard")).Begin();
}
else if (this._isLoaded)
{
action();
}
}
private void App_Ready(object sender, System.EventArgs e)
{
this.InitializeComponent();
this.ExceptionListBox.ItemsSource = this._exceptions;
this._isInitialized = true;
string text = null;
string text2 = null;
int count = this.Count;
if (count == 0)
{
long elapsedMilliseconds = ((App)Application.Current).Stopwatcher.ElapsedMilliseconds;
text = "本次启动热耗时 " + elapsedMilliseconds.ToString() + " 毫秒";
foreach (KeyValuePair<long, string> keyValuePair in ExceptionNotificationControl.StartupTimeTipDictionary)
{
if (elapsedMilliseconds < keyValuePair.Key)
{
text2 = keyValuePair.Value;
break;
}
}
}
else
{
switch (count)
{
case 1:
text = "启动期间只发生了 1 次异常";
text2 = "你击败了全国 90% 的开发者,请继续保持!";
break;
case 2:
case 3:
text = "启动期间发生了 " + count.ToString() + " 次异常";
text2 = "你击败了全国 " + (10 - count).ToString() + "0% 的开发者,请继续保持!";
break;
case 4:
case 5:
text = "启动期间发生了 " + count.ToString() + " 次异常";
text2 = "你只击败了全国 " + ((7 - count) * 19).ToString("####") + "% 的开发者,建议优化!";
break;
case 6:
case 7:
case 8:
case 9:
case 10:
text = "启动期间居然发生了 " + count.ToString() + " 次异常";
text2 = "你在全国开发者中排名 " + ((double)(12 - count) * 3.3).ToString("####") + "%,请立即优化!";
break;
default:
text = "启动期间居然发生了 " + count.ToString() + " 次异常";
text2 = "你在全国开发者中处于垫底的 " + (50 / count).ToString("####") + "%,再不优化就废了!";
break;
}
text2 = text2 + Environment.NewLine + this._exceptions.Last<ExceptionViewModel>().TypeName + " 等。";
}
this.TitleTextBox.Text = text;
this.ContentTextBox.Text = text2;
this.Show();
}
[DebuggerStepThrough]
private void Show()
{
this._isLoaded = true;
this._isClosing = false;
((Storyboard)base.FindResource("LoadStoryboard")).Begin();
}
private void Hide()
{
this._isLoaded = false;
this._isClosing = false;
this.TipIcon.Fill = Brushes.Green;
}
private void CloseButton_Click(object sender, RoutedEventArgs e)
{
this._isClosing = true;
((Storyboard)base.FindResource("UnloadStoryboard")).Begin();
}
private void UnloadStoryboard_Completed(object sender, System.EventArgs e)
{
this._exceptions.Clear();
this.DetailPanel.Visibility = Visibility.Collapsed;
this.Hide();
}
private void ToastPanel_MouseDown(object sender, MouseButtonEventArgs e)
{
this.ToastPanel.CaptureMouse();
this.PerformPressing();
}
private void ToastPanel_MouseUp(object sender, MouseButtonEventArgs e)
{
bool isMouseCaptured = this.ToastPanel.IsMouseCaptured;
this.PerformReleasing();
this.ToastPanel.ReleaseMouseCapture();
if (isMouseCaptured && new Rect(new Size(this.ToastPanel.ActualWidth, base.ActualHeight)).Contains(e.GetPosition(this.ToastPanel)))
{
this.PerformClick();
}
}
private void ToastPanel_StylusDown(object sender, StylusDownEventArgs e)
{
this.ToastPanel.CaptureStylus();
this.PerformPressing();
}
private void ToastPanel_StylusUp(object sender, StylusEventArgs e)
{
bool isStylusCaptured = this.ToastPanel.IsStylusCaptured;
this.PerformReleasing();
this.ToastPanel.ReleaseStylusCapture();
if (isStylusCaptured && new Rect(new Size(this.ToastPanel.ActualWidth, base.ActualHeight)).Contains(e.GetPosition(this.ToastPanel)))
{
this.PerformClick();
}
}
private void PerformPressing()
{
((Storyboard)base.FindResource("PressStoryboard")).Begin();
}
private void PerformReleasing()
{
((Storyboard)base.FindResource("ReleaseStoryboard")).Begin();
}
private void PerformClick()
{
this.DetailPanel.Visibility = ((this.DetailPanel.Visibility == Visibility.Visible) ? Visibility.Collapsed : Visibility.Visible);
this.ExceptionListBox.SelectedIndex = -1;
}
private void OnMouseEnter(object sender, MouseEventArgs e)
{
((Storyboard)base.FindResource("PressStoryboard")).Begin();
}
private readonly ObservableCollection<ExceptionViewModel> _exceptions = new ObservableCollection<ExceptionViewModel>();
private static readonly Dictionary<long, string> StartupTimeTipDictionary = new Dictionary<long, string>
{
{ 100L, "神级电脑,膜拜!" },
{ 500L, "就这个成绩,粉丝们爱不释手!" },
{ 800L, "优化水平,赞一个!" },
{ 900L, "坚持这个启动速度,还有希望!" },
{ 1000L, "临界点快到了,一定保持啊,再慢就没人喜欢了!" },
{ 1100L, "糟糕,越过了启动临界点,趁现在还有救,优化吧!" },
{ 1200L, "已经开始变慢了,注意优化!看看能从输出窗口能发现些什么。" },
{ 1300L, "已经开始变慢了,注意优化!别急,还有戏……" },
{ 1400L, "有点慢……不过,随你喜欢……" },
{ 1500L, "建议着手调查初始化过程中的主要耗时点,并进行针对性优化!" },
{ 2000L, "此时间已经影响到了用户体验,请立即优化初始化过程!" },
{ 2500L, "此时间已经严重影响到了用户体验,赶紧优化,别犹豫!" },
{ 3000L, "我知道这不是你本意,看看是否发生了过多的第一次机会异常!" },
{ 4000L, "请点击这里看看,调查一下各模块启动时间。决不容许这么慢!" },
{ 5000L, "请立即点开这里看看,务必调查各模块启动时间。决不容许再慢下去!" },
{ 6000L, "坐下,身体放宽,按F5之后喝杯茶再回来吧!" },
{ 7000L, "默哀三分钟……" },
{ 8000L, "为你电脑的性能担忧,为你的代码担忧!" },
{ 9000L, "收尸时间已到,请上交给国家……" },
{ 10000L, "神级电脑,膜拜!" },
{ 15000L, "冷启动吗?还是要注意下啊!" },
{ long.MaxValue, "你玩儿我呢,别以为打个断点就想骗启动速度!" }
};
private bool _isInitialized;
private bool _isLoaded;
private bool _isClosing;
}
}