private void ListView_MouseMove(object sender, MouseEventArgs e)
        {
            var info = ListView.HitTest(e.X, e.Y);

            if (info.SubItem?.Tag != null)
            {
                ToolTipSingleton.Show(info.SubItem.Tag, ListView, e.X, e.Y);
            }
            else
            {
                ToolTipSingleton.Remove(ListView);
            }
        }
        public ContextProcessInvocationListControl(Control container, DiagContext context)
        {
            Context = context;

            ListView = new MyListView()
            {
                View               = View.Details,
                Parent             = container,
                HeaderStyle        = ColumnHeaderStyle.Nonclickable,
                GridLines          = true,
                AllowColumnReorder = false,
                FullRowSelect      = false,
                Width              = 1200,
                BorderStyle        = BorderStyle.FixedSingle,
                ShowItemToolTips   = true,
                MultiSelect        = false,
                HideSelection      = false,
            };

            ListView.MouseMove  += ListView_MouseMove;
            ListView.MouseLeave += (s, a) => ToolTipSingleton.Remove(ListView);
            ListView.MouseUp    += ListView_MouseUp;

            var fix = 40 + 60 + 60 + 140;

            ListView.Columns.Add("#", 40);
            ListView.Columns.Add("time", 60);

            ListView.Columns.Add("topic", (ListView.Width - SystemInformation.VerticalScrollBarWidth - 4 - fix) / 3 * 2 / 3).TextAlign   = HorizontalAlignment.Right;
            ListView.Columns.Add("process", (ListView.Width - SystemInformation.VerticalScrollBarWidth - 4 - fix) / 3 * 2 / 3).TextAlign = HorizontalAlignment.Left;
            ListView.Columns.Add("kind", 60).TextAlign = HorizontalAlignment.Left;
            ListView.Columns.Add("type", (ListView.Width - SystemInformation.VerticalScrollBarWidth - 4 - fix) / 3 * 2 / 3).TextAlign = HorizontalAlignment.Left;

            ListView.Columns.Add("IN", 140).TextAlign = HorizontalAlignment.Right;
            ListView.Columns.Add("+", (ListView.Width - SystemInformation.VerticalScrollBarWidth - 4 - fix) / 3 * 1 / 5).TextAlign       = HorizontalAlignment.Right;
            ListView.Columns.Add("-", (ListView.Width - SystemInformation.VerticalScrollBarWidth - 4 - fix) / 3 * 1 / 5).TextAlign       = HorizontalAlignment.Right;
            ListView.Columns.Add("store", (ListView.Width - SystemInformation.VerticalScrollBarWidth - 4 - fix) / 3 * 1 / 5).TextAlign   = HorizontalAlignment.Right;
            ListView.Columns.Add("pending", (ListView.Width - SystemInformation.VerticalScrollBarWidth - 4 - fix) / 3 * 1 / 5).TextAlign = HorizontalAlignment.Right;
            ListView.Columns.Add("OUT", (ListView.Width - SystemInformation.VerticalScrollBarWidth - 4 - fix) / 3 * 1 / 5).TextAlign     = HorizontalAlignment.Right;

            ListView.MouseDoubleClick += ListView_MouseDoubleClick;

            _processStatUpdaterTimer = new System.Threading.Timer((state) => UpdateProcessStats());
            _processStatUpdaterTimer.Change(500, System.Threading.Timeout.Infinite);

            context.WholePlaybook.OnProcessInvoked += OnProcessInvoked;

            ListView.ItemSelectionChanged += ListView_ItemSelectionChanged;
        }
        private void ListView_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            var list = sender as ListView;
            var info = list.HitTest(e.X, e.Y);

            if (info.Item?.Tag is TrackedProcessInvocation process)
            {
                var relevantRowUids = Context.Index.GetProcessRowMap(process.InvocationUid);

                var finishedRows     = new HashSet <int>();
                var currentProcesses = new Dictionary <int, TrackedProcessInvocation>();

                var rows = new Dictionary <int, TrackedRow>();
                Context.Index.EnumerateThroughRowEvents(e =>
                {
                    if (relevantRowUids.Contains(e.RowUid) && !finishedRows.Contains(e.RowUid))
                    {
                        if (e is RowCreatedEvent rce)
                        {
                            var creatorProc = Context.WholePlaybook.ProcessList[rce.ProcessInvocationUid];
                            var row         = new TrackedRow()
                            {
                                Uid             = rce.RowUid,
                                CreatorProcess  = creatorProc,
                                PreviousProcess = null,
                            };

                            if (creatorProc == process)
                            {
                                row.NewValues = new Dictionary <string, object>(rce.Values, StringComparer.OrdinalIgnoreCase);
                            }
                            else
                            {
                                row.PreviousValues = new Dictionary <string, object>(rce.Values, StringComparer.OrdinalIgnoreCase);
                            }

                            currentProcesses[row.Uid] = creatorProc;

                            row.AllEvents.Add(rce);
                            rows.Add(row.Uid, row);
                        }
                        else if (e is RowValueChangedEvent rvce)
                        {
                            var row = rows[rvce.RowUid];
                            row.AllEvents.Add(rvce);

                            var values = currentProcesses[row.Uid] == process
                                ? row.NewValues
                                : row.PreviousValues;

                            foreach (var kvp in rvce.Values)
                            {
                                if (kvp.Value == null)
                                {
                                    values.Remove(kvp.Key);
                                }
                                else
                                {
                                    values[kvp.Key] = kvp.Value;
                                }
                            }
                        }
                        else if (e is RowOwnerChangedEvent roce)
                        {
                            var newProc = roce.NewProcessInvocationUid != null
                                ? Context.WholePlaybook.ProcessList[roce.NewProcessInvocationUid.Value]
                                : null;

                            var row = rows[roce.RowUid];
                            row.AllEvents.Add(roce);

                            var currentProcess = currentProcesses[row.Uid];

                            if (newProc == process)
                            {
                                row.NewValues       = new Dictionary <string, object>(row.PreviousValues, StringComparer.OrdinalIgnoreCase);
                                row.PreviousProcess = currentProcess;
                            }
                            else if (currentProcess == process)
                            {
                                finishedRows.Add(row.Uid);
                                row.NextProcess = newProc;
                            }

                            currentProcesses[row.Uid] = newProc;
                        }

                        return(finishedRows.Count < relevantRowUids.Count);
                    }

                    return(true);
                }, DiagnosticsEventKind.RowCreated, DiagnosticsEventKind.RowValueChanged, DiagnosticsEventKind.RowOwnerChanged);

                if (rows.Values.Count > 0)
                {
                    using (var form = new Form())
                    {
                        form.FormBorderStyle = FormBorderStyle.Sizable;
                        form.WindowState     = FormWindowState.Normal;
                        form.StartPosition   = FormStartPosition.Manual;
                        form.Bounds          = new Rectangle(Screen.PrimaryScreen.Bounds.Left + 100, Screen.PrimaryScreen.Bounds.Top + 100, Screen.PrimaryScreen.Bounds.Width - 200, Screen.PrimaryScreen.Bounds.Height - 200);
                        form.KeyPreview      = true;
                        form.KeyPress       += (s, e) =>
                        {
                            if (e.KeyChar == (char)Keys.Escape)
                            {
                                form.Close();
                            }
                        };

                        var control = new ProcessRowListControl(form, process, rows.Values.ToList());
                        control.Updater.RefreshStarted += (sender, args) =>
                        {
                            form.Text = "LOADING...";
                        };
                        control.Updater.RefreshFinished += (sender, args) =>
                        {
                            form.Text = "Process output: " + process.Name;
                        };

                        ToolTipSingleton.Remove(ListView);
                        form.ShowDialog();
                    }
                }
            }
        }