Exemple #1
0
        private void MainForm_Load(object sender, EventArgs e)
        {
            // handle document list changed or document activated
            App.Instance.Subscribe(
                x => x.Documents,
                x => x.Document,
                (documents, document) =>
            {
                replaceAllOpenedFilesItem.Enabled               =
                    replaceAllCurrentFileItem.Enabled           =
                        replaceNextItem.Enabled                 =
                            clearHighlightsItem.Enabled         =
                                findNextItem.Enabled            =
                                    findPreviousItem.Enabled    =
                                        openedFilesItem.Enabled =
                                            documents.Count > 0;

                // remove next siblings of menu separator
                var removedItems = mainToolbar.Items.OfType <ToolStripItem>().SkipWhile(x => x != editItem).Skip(1).ToArray();

                foreach (var item in removedItems)
                {
                    mainToolbar.Items.Remove(item);
                }

                // render document buttons
                var fileIndex = 0;
                foreach (var d in documents.Values.ToArray())
                {
                    // add separator before each file button
                    mainToolbar.Items.Add(new ToolStripSeparator());

                    var item = new ToolStripButton
                    {
                        // add shortcut for file buttons
                        // shortcut indexes should be sorted from 1-0
                        // no shortcut for 11th file and above
                        Text        = (fileIndex == 9 ? "&0. " : fileIndex < 9 ? $"&{fileIndex + 1}. " : "") + Path.GetFileName(d.FullPath).Ellipsis(15),
                        Checked     = d == document,
                        ToolTipText = d.FullPath
                    };

                    item.Click += delegate
                    {
                        App.Instance.ActivateDocument(d);
                    };

                    item.MouseDown += (s, me) =>
                    {
                        if (me.Button != MouseButtons.Middle)
                        {
                            return;
                        }

                        App.Instance.CloseDocument(d);

                        if (d.Editor != null)
                        {
                            editorPanel.Controls.Remove(d.Editor);
                        }
                    };

                    mainToolbar.Items.Add(item);

                    fileIndex++;
                }

                if (document != null)
                {
                    if (document.Editor == null)
                    {
                        // disable panel for configuring
                        var editor = new Scintilla
                        {
                            Dock = DockStyle.Fill
                        };
                        editor.Text = document.Contents;
                        editor.EmptyUndoBuffer();
                        editor.TextChanged += delegate
                        {
                            App.Instance.DocumentChanged(document, editor.Text);
                        };
                        editor.UpdateUI += delegate
                        {
                            if (!editor.Enabled)
                            {
                                return;
                            }
                            App.Instance.UpdateUi();
                        };
                        editorPanel.Controls.Add(editor);

                        // re-enable panel after editor is configured
                        editor.Configure(false);

                        App.Instance.EditorAdded(document, editor);
                    }

                    // show active document and hide each other
                    foreach (Control control in editorPanel.Controls)
                    {
                        control.Visible = control == document.Editor;
                    }
                }
            });

            App.Instance.Subscribe(
                x => x.Project,
                x => x.Document,
                x => x.Document?.Editor?.CurrentPosition ?? 0,
                async(project, document, position) =>
            {
                projectOptionsItem.Enabled = project != null;

                var projectStatus     = project == null ? null : Path.GetFileName(project.FullPath) + " | ";
                string documentStatus = null;

                if (document != null)
                {
                    documentStatus = $"{Path.GetFileName(document.FullPath)}";
                    // find section from current position
                    var sectionName = await App.Instance.FindSection(position);
                    if (!string.IsNullOrWhiteSpace(sectionName))
                    {
                        documentStatus += " » " + sectionName;
                    }
                }
                else
                {
                    documentStatus = "No document loaded";
                }

                statusLabel.Text = projectStatus + documentStatus;
            });

            // handle logging
            App.Instance.Subscribe(
                x => x.Logs.Count,
                logCount =>
            {
                if (logCount > 0)
                {
                    UpdateOutput(() =>
                    {
                        var start = logTextBox.TextLength;
                        foreach (var logEntry in App.Instance.PullLogs())
                        {
                            if (logEntry.Contents.IgnoreCaseEqual("<clear>"))
                            {
                                logTextBox.ClearAll();
                                logTextBox.RemoveAllData();
                                start = 0;
                                continue;
                            }

                            logTextBox.StartStyling(start);
                            var date = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss : ");
                            logTextBox.AppendText(date);
                            logTextBox.SetStyling(date.Length, Style.Default);
                            var entryStart = logTextBox.TextLength;
                            logTextBox.AppendText(logEntry.Contents);

                            start += date.Length;

                            if (logEntry.Formatter == null)
                            {
                                logTextBox.SetStyling(logEntry.Contents.Length, Style.Default);
                            }
                            else
                            {
                                logEntry.Formatter.Invoke(logTextBox, entryStart);
                            }

                            start += logEntry.Contents.Length;
                            logTextBox.AppendText("\r\n");
                            logTextBox.Format(start, 2, Style.Default);
                            start += 2;
                        }

                        logTextBox.GotoPosition(logTextBox.TextLength);
                    });
                }
            });

            // handle menu states for current document
            App.Instance.Subscribe(
                x => x.Document?.Editor,
                editor =>
            {
                diagramItem.Enabled                                         =
                    quickSearchItem.Enabled                                 =
                        inlineEditItem.Enabled                              =
                            showTemplatesItem.Enabled                       =
                                goBackItem.Enabled                          =
                                    closeItem.Enabled                       =
                                        intelliSenseItem.Enabled            =
                                            currentFileItem.Enabled         =
                                                gotoDeclarationItem.Enabled =
                                                    gotoLineItem.Enabled    =
                                                        editor != null;
            }
                );


            inlineEditTextBox.KeyDown += (s, ee) =>
            {
                // dont close inline panel if user try to close autocomplete popup
                if (ee.KeyCode == Keys.Escape && !inlineEditTextBox.AutoCActive)
                {
                    _inlineEditData.Close();
                }
            };

            inlineEditTextBox.TextChanged += delegate
            {
                _inlineEditData?.Change(inlineEditTextBox.Text);
            };

            inlineEditTextBox.Leave += delegate
            {
                _inlineEditData.Close();
            };

            App.Instance.Subscribe(
                x => x.InlineEdit,
                inlineEditData =>
            {
                if (inlineEditData != null)
                {
                    _inlineEditData = inlineEditData;
                    inlineEditTextBox.AddData(0, 0, EditorExtraDataType.InlineEdit, inlineEditData);
                    inlineEditTextBox.Text  = inlineEditData.Text;
                    _inlineEditData.Changed = false;
                    inlineEditPanel.Show();
                    inlineEditTextBox.Focus();
                }
                else
                {
                    inlineEditPanel.Hide();
                    inlineEditTextBox.EmptyUndoBuffer();
                }
            });

            if (CommandLineArgs?.Any() ?? false)
            {
                foreach (var commandLineArg in CommandLineArgs)
                {
                    if (commandLineArg.EndsWith(".inip", StringComparison.OrdinalIgnoreCase))
                    {
                        // project file
                        App.Instance.LoadProject(commandLineArg);
                    }
                    else
                    {
                        App.Instance.LoadDocument(commandLineArg);
                    }
                }
            }
        }