diff options
| author | Jean-Philippe Bruyère <jp_bruyere@hotmail.com> | 2021-09-15 19:26:39 +0000 |
|---|---|---|
| committer | Jean-Philippe Bruyère <jp_bruyere@hotmail.com> | 2021-09-15 19:26:39 +0000 |
| commit | 8b343e10f1a65b07988e15ea76596b16a03adadc (patch) | |
| tree | fc12eee11de644d429e6fb67516c46be82669377 /Samples/common/src | |
| parent | 5dcfd372592d5d21193d1dc5a58c385b2e8a6a7c (diff) | |
hosted command
Diffstat (limited to 'Samples/common/src')
| -rw-r--r-- | Samples/common/src/Editor.cs | 84 | ||||
| -rw-r--r-- | Samples/common/src/SampleBase.cs | 30 | ||||
| -rw-r--r-- | Samples/common/src/SampleBaseForEditor.cs | 34 |
3 files changed, 82 insertions, 66 deletions
diff --git a/Samples/common/src/Editor.cs b/Samples/common/src/Editor.cs index c093d345..e56914f8 100644 --- a/Samples/common/src/Editor.cs +++ b/Samples/common/src/Editor.cs @@ -19,7 +19,7 @@ namespace Crow XmlSource source; object TokenMutex = new object(); - void parse () { + void parse () { XmlSource tmp = new XmlSource(_text); lock(TokenMutex) source = tmp; @@ -41,7 +41,7 @@ namespace Crow if (suggestions == null || suggestions.Count == 0) hideOverlay (); else - showOverlay (); + showOverlay (); } } bool suggestionsActive => overlay != null && overlay.IsVisible; @@ -58,9 +58,9 @@ namespace Crow m.GetCustomAttribute<XmlIgnoreAttribute>() == null); } MemberInfo getCrowTypeMember (string crowTypeName, string memberName) { - Type crowType = IML.Instantiator.GetWidgetTypeFromName (crowTypeName); + Type crowType = IML.Instantiator.GetWidgetTypeFromName (crowTypeName); return crowType.GetMember (memberName, BindingFlags.Public | BindingFlags.Instance).FirstOrDefault (); - } + } public override void OnTextChanged(object sender, TextChangeEventArgs e) { @@ -68,7 +68,7 @@ namespace Crow //Task.Run(()=>parse()); parse(); - + if (!disableSuggestions && HasFocus) tryGetSuggestions (); @@ -77,7 +77,7 @@ namespace Crow void tryGetSuggestions () { if (!currentLoc.HasValue) return; - int pos = lines.GetAbsolutePosition (CurrentLoc.Value); + int pos = lines.GetAbsolutePosition (CurrentLoc.Value); currentToken = source.FindTokenIncludingPosition (pos); currentNode = source.FindNodeIncludingPosition (pos); Console.WriteLine ($"Current Token: {currentToken} Current Node: {currentNode}"); @@ -109,7 +109,7 @@ namespace Crow else if (pi.PropertyType == typeof (Measure)) Suggestions = (new string[] {"Stretched", "Fit"}). Where (s => s.StartsWith (currentToken.AsString (_text), StringComparison.OrdinalIgnoreCase)).ToList (); - else if (pi.PropertyType == typeof (Fill)) + else if (pi.PropertyType == typeof (Fill)) Suggestions = EnumsNET.Enums.GetValues<Colors> () .Where (s => s.ToString().StartsWith (currentToken.AsString (_text), StringComparison.OrdinalIgnoreCase)).ToList (); } @@ -123,7 +123,7 @@ namespace Crow Suggestions = Enum.GetNames (pi.PropertyType).ToList (); else if (pi.PropertyType == typeof(bool)) Suggestions = new List<string> (new string[] {"true", "false"}); - else if (pi.PropertyType == typeof (Fill)) + else if (pi.PropertyType == typeof (Fill)) Suggestions = EnumsNET.Enums.GetValues<Colors> ().ToList (); else if (pi.PropertyType == typeof (Measure)) Suggestions = new List<string> (new string[] {"Stretched", "Fit"}); @@ -131,10 +131,10 @@ namespace Crow } } } - } - } else if (currentToken.Type != TokenType.AttributeValueClose && - currentToken.Type != TokenType.EmptyElementClosing && - currentToken.Type != TokenType.ClosingSign && + } + } else if (currentToken.Type != TokenType.AttributeValueClose && + currentToken.Type != TokenType.EmptyElementClosing && + currentToken.Type != TokenType.ClosingSign && currentNode is ElementStartTagSyntax eltStartTag) { if (currentToken.Type == TokenType.AttributeName) Suggestions = getAllCrowTypeMembers (eltStartTag.NameToken.Value.AsString (_text)) @@ -157,39 +157,39 @@ namespace Crow overlay = IFace.LoadIMLFragment<ListBox>(@" <ListBox Style='suggestionsListBox' Data='{Suggestions}' UseLoadingThread='False' > <ItemTemplate> - <ListItem Height='Fit' Margin='0' Focusable='false' HorizontalAlignment='Left' + <ListItem Height='Fit' Margin='0' Focusable='false' HorizontalAlignment='Left' Selected = '{Background=${ControlHighlight}}' Unselected = '{Background=Transparent}'> <Label Text='{}' HorizontalAlignment='Left' /> - </ListItem> + </ListItem> </ItemTemplate> <ItemTemplate DataType='System.Reflection.MemberInfo'> - <ListItem Height='Fit' Margin='0' Focusable='false' HorizontalAlignment='Left' + <ListItem Height='Fit' Margin='0' Focusable='false' HorizontalAlignment='Left' Selected = '{Background=${ControlHighlight}}' Unselected = '{Background=Transparent}'> <HorizontalStack> <Image Picture='{GetIcon}' Width='16' Height='16'/> <Label Text='{Name}' HorizontalAlignment='Left' /> </HorizontalStack> - </ListItem> - </ItemTemplate> + </ListItem> + </ItemTemplate> <ItemTemplate DataType='Colors'> - <ListItem Height='Fit' Margin='0' Focusable='false' HorizontalAlignment='Left' + <ListItem Height='Fit' Margin='0' Focusable='false' HorizontalAlignment='Left' Selected = '{Background=${ControlHighlight}}' Unselected = '{Background=Transparent}'> <HorizontalStack> <Widget Background='{}' Width='20' Height='14'/> <Label Text='{}' HorizontalAlignment='Left' /> </HorizontalStack> - </ListItem> + </ListItem> </ItemTemplate> </ListBox> "); overlay.DataSource = this; - overlay.Loaded += (sender, arg) => (sender as ListBox).SelectedIndex = 0; + overlay.Loaded += (sender, arg) => (sender as ListBox).SelectedIndex = 0; } else overlay.IsVisible = true; - overlay.RegisterForLayouting(LayoutingType.Sizing); + overlay.RegisterForLayouting(LayoutingType.Sizing); } } void hideOverlay () { @@ -197,7 +197,7 @@ namespace Crow return; overlay.IsVisible = false; } - void completeToken () { + void completeToken () { string selectedSugg = overlay.SelectedItem is MemberInfo mi ? mi.Name : overlay.SelectedItem?.ToString (); if (selectedSugg == null) @@ -208,15 +208,15 @@ namespace Crow update (new TextChange (currentToken.End, 0, selectedSugg)); else if (currentToken.Type == TokenType.AttributeName && currentNode is AttributeSyntax attrib) { if (attrib.ValueToken.HasValue) { - TextChange tc = new TextChange (currentToken.Start, currentToken.Length, selectedSugg); + TextChange tc = new TextChange (currentToken.Start, currentToken.Length, selectedSugg); update (tc); selectionStart = lines.GetLocation (attrib.ValueToken.Value.Start + tc.CharDiff + 1); CurrentLoc = lines.GetLocation (attrib.ValueToken.Value.End + tc.CharDiff - 1); } else { update (new TextChange (currentToken.Start, currentToken.Length, selectedSugg + "=\"\"")); MoveLeft (); - } - } else + } + } else update (new TextChange (currentToken.Start, currentToken.Length, selectedSugg)); hideOverlay (); } @@ -224,7 +224,7 @@ namespace Crow hideOverlay (); base.onMouseDown (sender, e); } - + public override void onKeyDown(object sender, KeyEventArgs e) { TextSpan selection = Selection; @@ -264,7 +264,7 @@ namespace Crow disableSuggestions = true; if (IFace.Shift) { - for (int l = lineStart; l <= lineEnd; l++) { + for (int l = lineStart; l <= lineEnd; l++) { if (Text[lines[l].Start] == '\t') update (new TextChange (lines[l].Start, 1, "")); else if (Char.IsWhiteSpace (Text[lines[l].Start])) { @@ -276,10 +276,10 @@ namespace Crow } }else{ - for (int l = lineStart; l <= lineEnd; l++) - update (new TextChange (lines[l].Start, 0, "\t")); + for (int l = lineStart; l <= lineEnd; l++) + update (new TextChange (lines[l].Start, 0, "\t")); } - + selectionStart = new CharLocation (lineStart, 0); CurrentLoc = new CharLocation (lineEnd, lines[lineEnd].Length); @@ -288,7 +288,7 @@ namespace Crow return; } base.onKeyDown(sender, e); - } + } protected override void drawContent (Context gr) { try { @@ -297,7 +297,7 @@ namespace Crow base.drawContent (gr); return; } - + Rectangle cb = ClientRectangle; fe = gr.FontExtents; double lineHeight = fe.Ascent + fe.Descent; @@ -359,7 +359,7 @@ namespace Crow TextExtents extents; int tokPtr = 0; Token tok = source.Tokens[tokPtr]; - bool multilineToken = false; + bool multilineToken = false; ReadOnlySpan<char> buff = sourceBytes; @@ -369,7 +369,7 @@ namespace Crow if (multilineToken) { if (tok.End < lines[i].End) {//last incomplete line of multiline token - buff = sourceBytes.Slice (lines[i].Start, tok.End - lines[i].Start); + buff = sourceBytes.Slice (lines[i].Start, tok.End - lines[i].Start); } else {//print full line buff = sourceBytes.Slice (lines[i].Start, lines[i].Length); } @@ -399,7 +399,7 @@ namespace Crow gr.SetSource(Colors.DarkSlateBlue); }else { gr.SetSource(Colors.Red); - } + } } int size = buff.Length * 4 + 1; @@ -414,7 +414,7 @@ namespace Crow gr.MoveTo (pixX, lineHeight * y + fe.Ascent); gr.ShowText (bytes.Slice (0, encodedBytes)); pixX += extents.XAdvance; - x += buff.Length; + x += buff.Length; } if (multilineToken) { @@ -432,7 +432,7 @@ namespace Crow if (HasFocus && selectionNotEmpty) { RectangleD lineRect = new RectangleD (cb.X, lineHeight * y + cb.Top, pixX, lineHeight); RectangleD selRect = lineRect; - + if (i >= selStart.Line && i <= selEnd.Line) { if (selStart.Line == selEnd.Line) { selRect.X = selStart.VisualCharXPosition + cb.X; @@ -478,7 +478,7 @@ namespace Crow x = 0; pixX = 0; - + y++; @@ -492,13 +492,13 @@ namespace Crow continue; } else if (tok2.Type == TokenType.WhiteSpace) { x += tok2.Length; - pixX += spacePixelWidth * tok2.Length;*/ - } + pixX += spacePixelWidth * tok2.Length;*/ + } //gr.Translate (ScrollX, ScrollY); } } catch { - + } - } + } } }
\ No newline at end of file diff --git a/Samples/common/src/SampleBase.cs b/Samples/common/src/SampleBase.cs index 0622c2cf..b44bb8f5 100644 --- a/Samples/common/src/SampleBase.cs +++ b/Samples/common/src/SampleBase.cs @@ -70,11 +70,18 @@ namespace Samples new ActionCommand("File command three", (sender) => showMsgBox (sender)) ); public ActionCommand SingleCommand => new ActionCommand("Single command 1", (sender) => showMsgBox (sender), "#Icons.gavel.svg"); - public ToggleCommand CMDToggleBoolVal => new ToggleCommand ("Toggle", this, "BoolVal", "#Icons.gavel.svg", true); - public ToggleCommand CMDToggleBoolValField => new ToggleCommand ("ToggleField", this, "boolVal", "#Icons.gavel.svg", true); + public ToggleCommand CMDToggleBoolVal, CMDToggleBoolValField; + + public ActionCommand CMDHosted; void initCommands() { + CMDToggleBoolVal = new ToggleCommand (this, "Toggle", new Binding<bool> ("BoolVal"), "#Icons.gavel.svg", null, true); + CMDToggleBoolValField = new ToggleCommand (this, "ToggleField", new Binding<bool> ("boolVal"), "#Icons.gavel.svg", null, true); + CMDHosted = new ActionCommand (this, "Hosted command", + () => MessageBox.ShowModal(this, MessageBox.Type.Information, "hosted command triggered"), "#Icons.binding.svg", + new KeyBinding (Key.F1, Modifier.Super), + new Binding<bool> ("CanExecute")); Commands = new CommandGroup("commands msg boxes", new ActionCommand("Action 1", () => MessageBox.ShowModal(this, MessageBox.Type.Information, "context menu 1 clicked")), new ActionCommand("Action two", () => MessageBox.ShowModal(this, MessageBox.Type.Information, "context menu 2 clicked"), null, false), @@ -339,7 +346,7 @@ namespace Samples string curSources = ""; - public bool boolVal = true; + public bool boolVal = true, canExecute; public string CurSources { get => curSources; @@ -358,7 +365,16 @@ namespace Samples return; boolVal = value; NotifyValueChanged(boolVal); - Console.WriteLine ($"boolVal => {value}"); + } + } + public bool CanExecute + { + get => canExecute; + set { + if (canExecute == value) + return; + canExecute = value; + NotifyValueChanged(canExecute); } } public Color AllWidgetBackground { @@ -387,10 +403,10 @@ namespace Samples base.processDrawing(ctx); } - public override bool OnKeyDown(Key key) + public override bool OnKeyDown(KeyEventArgs e) { - switch (key) + switch (e.Key) { case Key.F5: Load("Interfaces/Divers/testFileDialog.crow").DataSource = this; @@ -407,7 +423,7 @@ namespace Samples DbgLogger.Save(this); return true; } - return base.OnKeyDown(key); + return base.OnKeyDown(e); } } }
\ No newline at end of file diff --git a/Samples/common/src/SampleBaseForEditor.cs b/Samples/common/src/SampleBaseForEditor.cs index 47b93103..e1cadc0a 100644 --- a/Samples/common/src/SampleBaseForEditor.cs +++ b/Samples/common/src/SampleBaseForEditor.cs @@ -27,11 +27,11 @@ namespace Samples if (string.IsNullOrEmpty (CurrentDir)) CurrentDir = Path.Combine (Directory.GetCurrentDirectory (), "Interfaces"); - + } protected const string _defaultFileName = "unnamed.txt"; - protected string source = "", origSource; + protected string source = "", origSource; protected TextBox editor; bool debugLogRecording; @@ -53,7 +53,7 @@ namespace Samples Configuration.Global.Set (nameof (CurrentFile), value); NotifyValueChanged (CurrentFile); } - } + } public virtual string Source { get => source; set { @@ -71,8 +71,8 @@ namespace Samples get => Configuration.Global.Get<DbgEvtType> (nameof (RecordedEvents)); set { if (RecordedEvents == value) - return; - Configuration.Global.Set (nameof (RecordedEvents), value); + return; + Configuration.Global.Set (nameof (RecordedEvents), value); if (DebugLogRecording) DbgLogger.IncludeEvents = RecordedEvents; NotifyValueChanged(RecordedEvents); @@ -102,7 +102,7 @@ namespace Samples get => Configuration.Global.Get<bool> (nameof(DebugLogToFile)); set { if (DebugLogToFile == value) - return; + return; Configuration.Global.Set (nameof(DebugLogToFile), value); NotifyValueChanged(DebugLogToFile); DbgLogger.ConsoleOutput = !value; @@ -134,7 +134,7 @@ namespace Samples public CommandGroup EditorCommands => new CommandGroup (CMDUndo, CMDRedo, CMDCut, CMDCopy, CMDPaste, CMDSave, CMDSaveAs); protected virtual void initCommands () { - CMDNew = new ActionCommand ("New", new Action (onNewFile), "#Icons.blank-file.svg"); + CMDNew = new ActionCommand ("New", new Action (onNewFile), "#Icons.blank-file.svg"); CMDSave = new ActionCommand ("Save", new Action (onSave), "#Icons.save.svg", false); CMDSaveAs = new ActionCommand ("Save As...", new Action (onSaveAs), "#Icons.save.svg"); CMDQuit = new ActionCommand ("Quit", new Action (() => base.Quit ()), "#Icons.exit.svg"); @@ -149,7 +149,7 @@ namespace Samples protected Stack<TextChange> undoStack = new Stack<TextChange> (); protected Stack<TextChange> redoStack = new Stack<TextChange> (); protected TextSpan selection; - protected string SelectedText => + protected string SelectedText => selection.IsEmpty ? "" : Source.AsSpan (selection.Start, selection.Length).ToString (); protected void undo () { @@ -176,7 +176,7 @@ namespace Samples undoStack.Clear (); redoStack.Clear (); CMDUndo.CanExecute = false; - CMDRedo.CanExecute = false; + CMDRedo.CanExecute = false; } protected void cut () { @@ -186,11 +186,11 @@ namespace Samples protected void copy () { Clipboard = SelectedText; } - protected void paste () { + protected void paste () { applyChange (new TextChange (selection.Start, selection.Length, Clipboard)); } protected void onNewFile () { - if (IsDirty) { + if (IsDirty) { MessageBox.ShowModal (this, MessageBox.Type.YesNo, "Current file has unsaved changes, are you sure?").Yes += (sender, e) => newFile (); } else newFile (); @@ -257,7 +257,7 @@ namespace Samples CMDSave.CanExecute = false; } - protected void reloadFromFile () { + protected void reloadFromFile () { disableTextChangedEvent = true; if (File.Exists (CurrentFile)) { using (Stream s = new FileStream (CurrentFile, FileMode.Open)) { @@ -267,7 +267,7 @@ namespace Samples } disableTextChangedEvent = false; resetUndoRedo (); - } + } protected bool disableTextChangedEvent = false; protected void apply (TextChange change) { Span<char> tmp = stackalloc char[source.Length + (change.ChangedText.Length - change.Length)]; @@ -278,8 +278,8 @@ namespace Samples src.Slice (change.End).CopyTo (tmp.Slice (change.Start + change.ChangedText.Length)); disableTextChangedEvent = true; Source = tmp.ToString (); - disableTextChangedEvent = false; - } + disableTextChangedEvent = false; + } protected void applyChange (TextChange change) { undoStack.Push (change.Inverse (source)); redoStack.Clear (); @@ -319,8 +319,8 @@ namespace Samples return; applyChange (e.Change); } - - protected void onSelectedTextChanged (object sender, EventArgs e) { + + protected void onSelectedTextChanged (object sender, EventArgs e) { selection = (sender as Label).Selection; Console.WriteLine($"selection:{selection.Start} length:{selection.Length}"); CMDCut.CanExecute = CMDCopy.CanExecute = !selection.IsEmpty; |