From 263de03914a3de5d55c4508c322e32d36caf2a87 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jean-Philippe=20Bruy=C3=A8re?= Date: Mon, 7 Jun 2021 15:05:30 +0200 Subject: [PATCH] suggestion in IML editor, editor base classe for ShowCase and LogAnalyzer --- Crow/Crow.csproj | 2 +- Crow/src/Interface.cs | 7 +- Crow/src/Text/TextChange.cs | 2 + Crow/src/Widgets/Label.cs | 2 +- Samples/BasicTests/BasicTests.cs | 2 +- Samples/BindingTest/Program.cs | 1 + .../DebugLogAnalyzer/src/DebugInterface.cs | 1 + Samples/DebugLogAnalyzer/src/Program.cs | 291 +--------------- Samples/ShowCase/ShowCase.cs | 279 +--------------- Samples/ShowCase/ui/showcase.crow | 56 +++- Samples/common/src/Editor.cs | 120 ++++++- .../src/Extensions.cs | 11 +- Samples/common/src/ImlParsing/Syntax.cs | 14 +- Samples/common/src/ImlParsing/TokenType.cs | 4 +- Samples/common/src/ImlParsing/XmlSource.cs | 13 +- Samples/common/src/SampleBase.cs | 6 +- Samples/common/src/SampleBaseForEditor.cs | 312 ++++++++++++++++++ .../Experimental/DockWindow.template | 39 --- .../Experimental/multiColorPick.crow | 2 +- Samples/common/ui/Interfaces/Wrapper/1.1.crow | 2 +- Samples/common/ui/icons/event.png | Bin 0 -> 241 bytes Samples/common/ui/icons/property.png | Bin 0 -> 257 bytes .../common/ui/templates/Suggestions.template | 8 +- 23 files changed, 537 insertions(+), 637 deletions(-) rename Samples/{DebugLogAnalyzer => common}/src/Extensions.cs (53%) create mode 100644 Samples/common/src/SampleBaseForEditor.cs delete mode 100644 Samples/common/ui/Interfaces/Experimental/DockWindow.template create mode 100755 Samples/common/ui/icons/event.png create mode 100755 Samples/common/ui/icons/property.png diff --git a/Crow/Crow.csproj b/Crow/Crow.csproj index 083b1989..29aca878 100644 --- a/Crow/Crow.csproj +++ b/Crow/Crow.csproj @@ -23,7 +23,7 @@ True true $(NoWarn);1591;1587;1570;1572;1573;1574 - DESIGN_MODE;_MEASURE_TIME;_DEBUG_HIGHLIGHT_FOCUS + DESIGN_MODE;MEASURE_TIME;_DEBUG_HIGHLIGHT_FOCUS false false diff --git a/Crow/src/Interface.cs b/Crow/src/Interface.cs index 3466b33b..d39121b0 100644 --- a/Crow/src/Interface.cs +++ b/Crow/src/Interface.cs @@ -100,6 +100,7 @@ namespace Crow public Interface (int width, int height, IntPtr glfwWindowHandle) : this (width, height, false, false) { hWin = glfwWindowHandle; + PerformanceMeasure.InitMeasures (); } public Interface (int width = 800, int height = 600, bool startUIThread = true, bool createSurface = true) { @@ -110,14 +111,14 @@ namespace Crow if (createSurface) initSurface (); + PerformanceMeasure.InitMeasures (); + if (startUIThread) { Thread t = new Thread (InterfaceThread) { IsBackground = true }; t.Start (); - } - - PerformanceMeasure.InitMeasures (); + } } #endregion diff --git a/Crow/src/Text/TextChange.cs b/Crow/src/Text/TextChange.cs index 696699c1..75948da8 100644 --- a/Crow/src/Text/TextChange.cs +++ b/Crow/src/Text/TextChange.cs @@ -14,6 +14,8 @@ namespace Crow.Text public readonly string ChangedText; public int End => Start + Length; + + public int CharDiff => ChangedText.Length - Length; public TextChange (int position, int length, string changedText) { Start = position; Length = length; diff --git a/Crow/src/Widgets/Label.cs b/Crow/src/Widgets/Label.cs index 47082698..17544ec7 100644 --- a/Crow/src/Widgets/Label.cs +++ b/Crow/src/Widgets/Label.cs @@ -396,7 +396,7 @@ namespace Crow } if (!string.IsNullOrEmpty (_text)) { - Foreground.SetAsSource (IFace, gr); + Foreground?.SetAsSource (IFace, gr); TextExtents extents; Span bytes = stackalloc byte[128]; diff --git a/Samples/BasicTests/BasicTests.cs b/Samples/BasicTests/BasicTests.cs index 954d4879..13bb41fe 100644 --- a/Samples/BasicTests/BasicTests.cs +++ b/Samples/BasicTests/BasicTests.cs @@ -5,7 +5,7 @@ using System.Linq; using Crow; using Glfw; -namespace tests +namespace Samples { public class BasicTests : SampleBase { diff --git a/Samples/BindingTest/Program.cs b/Samples/BindingTest/Program.cs index 469e3473..019e3ce6 100644 --- a/Samples/BindingTest/Program.cs +++ b/Samples/BindingTest/Program.cs @@ -4,6 +4,7 @@ using System; using System.Runtime.CompilerServices; using Crow; +using Samples; namespace BindingTest { diff --git a/Samples/DebugLogAnalyzer/src/DebugInterface.cs b/Samples/DebugLogAnalyzer/src/DebugInterface.cs index 26a1eddf..1653f163 100644 --- a/Samples/DebugLogAnalyzer/src/DebugInterface.cs +++ b/Samples/DebugLogAnalyzer/src/DebugInterface.cs @@ -5,6 +5,7 @@ using System; using Crow.Cairo; using System.Threading; +using Samples; namespace Crow { diff --git a/Samples/DebugLogAnalyzer/src/Program.cs b/Samples/DebugLogAnalyzer/src/Program.cs index 9c2e7117..8cee874a 100644 --- a/Samples/DebugLogAnalyzer/src/Program.cs +++ b/Samples/DebugLogAnalyzer/src/Program.cs @@ -12,12 +12,13 @@ using System.Collections.Generic; using Encoding = System.Text.Encoding; using Crow.DebugLogger; using System.Linq; +using Samples; namespace DebugLogAnalyzer { - public class Program : SampleBase + public class Program : SampleBaseForEditor { - public static Program CurrentProgramInstance; + static void Main (string [] args) { DbgLogger.IncludeEvents = DbgEvtType.None; @@ -29,13 +30,8 @@ namespace DebugLogAnalyzer } } protected override void OnInitialized () { - initCommands (); - base.OnInitialized (); - if (string.IsNullOrEmpty (CurrentDir)) - CurrentDir = Path.Combine (Directory.GetCurrentDirectory (), "Interfaces"); - Load ("#Dbg.main.crow").DataSource = this; //crowContainer = FindByName ("CrowContainer") as Container; editor = FindByName ("tb") as TextBox; @@ -92,25 +88,11 @@ namespace DebugLogAnalyzer NotifyValueChanged (searchWidget); } } - - - - public Command CMDNew, CMDOpen, CMDSave, CMDSaveAs, CMDQuit, CMDShowLeftPane, - CMDUndo, CMDRedo, CMDCut, CMDCopy, CMDPaste, CMDHelp, CMDAbout, CMDOptions, - CMDGotoParentEvent, CMDEventHistoryForward, CMDEventHistoryBackward; + public Command CMDGotoParentEvent, CMDEventHistoryForward, CMDEventHistoryBackward; public CommandGroup EventCommands, DirectoryCommands; - public CommandGroup EditorCommands => new CommandGroup (CMDUndo, CMDRedo, CMDCut, CMDCopy, CMDPaste, CMDSave, CMDSaveAs); - void initCommands () + protected override void initCommands () { - CMDNew = new Command ("New", new Action (onNewFile), "#Icons.blank-file.svg"); - CMDSave = new Command ("Save", new Action (onSave), "#Icons.save.svg", false); - CMDSaveAs = new Command ("Save As...", new Action (onSaveAs), "#Icons.save.svg"); - CMDQuit = new Command ("Quit", new Action (() => base.Quit ()), "#Icons.exit.svg"); - CMDUndo = new Command ("Undo", new Action (undo),"#Icons.undo.svg", false); - CMDRedo = new Command ("Redo", new Action (redo),"#Icons.redo.svg", false); - CMDCut = new Command ("Cut", new Action (() => cut ()), "#Icons.scissors.svg", false); - CMDCopy = new Command ("Copy", new Action (() => copy ()), "#Icons.copy-file.svg", false); - CMDPaste= new Command ("Paste", new Action (() => paste ()), "#Icons.paste-on-document.svg", false); + base.initCommands (); CMDGotoParentEvent = new Command("parent", ()=> { CurrentEvent = CurrentEvent?.parentEvent; }, null, false); CMDEventHistoryBackward = new Command("back.", currentEventHistoryGoBack, null, false); @@ -212,29 +194,7 @@ namespace DebugLogAnalyzer } } public List CurWidgetRootEvents => curWidget == null? new List() : curWidget.RootEvents; - public DbgEvtType RecordedEvents { - get => Configuration.Global.Get (nameof (RecordedEvents)); - set { - if (RecordedEvents == value) - return; - Configuration.Global.Set (nameof (RecordedEvents), value); - if (DebugLogRecording) - DbgLogger.IncludeEvents = RecordedEvents; - NotifyValueChanged(RecordedEvents); - } - } - public DbgEvtType DiscardedEvents { - get => Configuration.Global.Get (nameof (DiscardedEvents)); - set { - if (DiscardedEvents == value) - return; - Configuration.Global.Set (nameof (DiscardedEvents), value); - if (DebugLogRecording) - DbgLogger.DiscardEvents = DiscardedEvents; - NotifyValueChanged(DiscardedEvents); - } - } - public bool DebugLoggingEnabled => DbgLogger.IsEnabled; + public bool DebugLogToFile { get => Configuration.Global.Get (nameof(DebugLogToFile)); set { @@ -253,15 +213,6 @@ namespace DebugLogAnalyzer NotifyValueChanged (DebugLogFilePath); } }*/ - public bool DebugLogRecording { - get => debugLogRecording; - set { - if (debugLogRecording == value) - return; - debugLogRecording = value; - NotifyValueChanged(debugLogRecording); - } - } public bool DebugLogOnStartup { get => Configuration.Global.Get (nameof(DebugLogOnStartup)); set { @@ -273,45 +224,8 @@ namespace DebugLogAnalyzer } - const string _defaultFileName = "unnamed.txt"; - string source = "", origSource; - TextBox editor; - Stack undoStack = new Stack (); - Stack redoStack = new Stack (); - TextSpan selection; Exception currentException; - public string CurrentDir { - get => Configuration.Global.Get (nameof (CurrentDir)); - set { - if (CurrentDir == value) - return; - Configuration.Global.Set (nameof (CurrentDir), value); - NotifyValueChanged (CurrentDir); - } - } - public string CurrentFile { - get => Configuration.Global.Get (nameof (CurrentFile)); - set { - if (CurrentFile == value) - return; - Configuration.Global.Set (nameof (CurrentFile), value); - NotifyValueChanged (CurrentFile); - } - } - public new bool IsDirty => source != origSource; - public string Source { - get => source; - set { - if (source == value) - return; - source = value; - CMDSave.CanExecute = IsDirty; - NotifyValueChanged (source); - NotifyValueChanged ("IsDirty", IsDirty); - } - } - string SelectedText => - selection.IsEmpty ? "" : Source.AsSpan (selection.Start, selection.Length).ToString (); + public Exception CurrentException { get => currentException; set { @@ -326,195 +240,6 @@ namespace DebugLogAnalyzer public bool ShowError => currentException != null; public string CurrentExceptionMSG => currentException == null ? "" : currentException.Message; - - - void undo () { - if (undoStack.TryPop (out TextChange tch)) { - redoStack.Push (tch.Inverse (source)); - CMDRedo.CanExecute = true; - apply (tch); - editor.SetCursorPosition (tch.End + tch.ChangedText.Length); - } - if (undoStack.Count == 0) - CMDUndo.CanExecute = false; - } - void redo () { - if (redoStack.TryPop (out TextChange tch)) { - undoStack.Push (tch.Inverse (source)); - CMDUndo.CanExecute = true; - apply (tch); - editor.SetCursorPosition (tch.End + tch.ChangedText.Length); - } - if (redoStack.Count == 0) - CMDRedo.CanExecute = false; - } - void cut () { - copy (); - applyChange (new TextChange (selection.Start, selection.Length, "")); - } - void copy () { - Clipboard = SelectedText; - } - void paste () { - applyChange (new TextChange (selection.Start, selection.Length, Clipboard)); - } - bool disableTextChangedEvent = false; - void apply (TextChange change) { - Span tmp = stackalloc char[source.Length + (change.ChangedText.Length - change.Length)]; - ReadOnlySpan src = source.AsSpan (); - src.Slice (0, change.Start).CopyTo (tmp); - if (!string.IsNullOrEmpty (change.ChangedText)) - change.ChangedText.AsSpan ().CopyTo (tmp.Slice (change.Start)); - src.Slice (change.End).CopyTo (tmp.Slice (change.Start + change.ChangedText.Length)); - disableTextChangedEvent = true; - Source = tmp.ToString (); - disableTextChangedEvent = false; - } - void applyChange (TextChange change) { - undoStack.Push (change.Inverse (source)); - redoStack.Clear (); - CMDUndo.CanExecute = true; - CMDRedo.CanExecute = false; - apply (change); - } - - void resetUndoRedo () { - undoStack.Clear (); - redoStack.Clear (); - CMDUndo.CanExecute = false; - CMDRedo.CanExecute = false; - } - - void onNewFile () { - if (IsDirty) { - MessageBox.ShowModal (this, MessageBox.Type.YesNo, "Current file has unsaved changes, are you sure?").Yes += (sender, e) => newFile (); - } else - newFile (); - } - void onSave () - { - if (!File.Exists (CurrentFile)) { - onSaveAs (); - return; - } - save (); - } - void onSaveAs () - { - string dir = Path.GetDirectoryName (CurrentFile); - if (string.IsNullOrEmpty (dir)) - dir = Directory.GetCurrentDirectory (); - LoadIMLFragment (@"").DataSource = this; - } - void saveFileDialog_OkClicked (object sender, EventArgs e) - { - FileDialog fd = sender as FileDialog; - - if (string.IsNullOrEmpty (fd.SelectedFileFullPath)) - return; - - if (File.Exists(fd.SelectedFileFullPath)) { - MessageBox mb = MessageBox.ShowModal (this, MessageBox.Type.YesNo, "File exists, overwrite?"); - mb.Yes += (sender2, e2) => { - CurrentFile = fd.SelectedFileFullPath; - save (); - }; - return; - } - - CurrentFile = fd.SelectedFileFullPath; - save (); - } - - void newFile() - { - disableTextChangedEvent = true; - Source = @"