diff options
| author | Jean-Philippe Bruyère <jp_bruyere@hotmail.com> | 2021-03-26 17:07:42 +0100 |
|---|---|---|
| committer | Jean-Philippe Bruyère <jp_bruyere@hotmail.com> | 2021-03-26 17:07:42 +0100 |
| commit | 3d7463e06f0cf2e98707564f80947dc973a710bc (patch) | |
| tree | 0a9cff06ed94b434d2c21f740ac51dda09ca20a3 /Samples | |
| parent | 10e39117bc3957fc9e1e748f704be5568a7b1299 (diff) | |
Splitter simplification, source header in src/Text, Table wip
Diffstat (limited to 'Samples')
| -rw-r--r-- | Samples/ShowCase/ShowCase.cs | 64 | ||||
| -rw-r--r-- | Samples/ShowCase/ui/showcase.crow | 13 |
2 files changed, 55 insertions, 22 deletions
diff --git a/Samples/ShowCase/ShowCase.cs b/Samples/ShowCase/ShowCase.cs index 60e9ea36..0628695f 100644 --- a/Samples/ShowCase/ShowCase.cs +++ b/Samples/ShowCase/ShowCase.cs @@ -56,27 +56,31 @@ namespace ShowCase CMDUndo, CMDRedo, CMDCut, CMDCopy, CMDPaste, CMDHelp, CMDAbout, CMDOptions; const string _defaultFileName = "unnamed.txt"; - string source = ""; - int dirtyUndoLevel; + string source = "", origSource; TextBox editor; Stopwatch reloadChrono = new Stopwatch (); - public new bool IsDirty { get { return undoStack.Count != dirtyUndoLevel; } } + public new bool IsDirty => source != origSource; public string Source { get => source; set { if (source == value) return; source = value; + CMDSave.CanExecute = IsDirty; if (!reloadChrono.IsRunning) reloadChrono.Restart (); NotifyValueChanged (source); + NotifyValueChanged ("IsDirty", IsDirty); } } public CommandGroup EditorCommands => new CommandGroup (CMDUndo, CMDRedo, CMDCut, CMDCopy, CMDPaste, CMDSave, CMDSaveAs); Stack<TextChange> undoStack = new Stack<TextChange> (); Stack<TextChange> redoStack = new Stack<TextChange> (); + TextSpan selection; + string SelectedText => + selection.IsEmpty ? "" : Source.AsSpan (selection.Start, selection.Length).ToString (); void undo () { if (undoStack.TryPop (out TextChange tch)) { @@ -98,6 +102,16 @@ namespace ShowCase 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<char> tmp = stackalloc char[source.Length + (change.ChangedText.Length - change.Length)]; @@ -108,8 +122,7 @@ namespace ShowCase src.Slice (change.End).CopyTo (tmp.Slice (change.Start + change.ChangedText.Length)); disableTextChangedEvent = true; Source = tmp.ToString (); - disableTextChangedEvent = false; - NotifyValueChanged ("IsDirty", IsDirty); + disableTextChangedEvent = false; } void initCommands () @@ -120,9 +133,9 @@ namespace ShowCase CMDQuit = new Command (new Action (() => base.Quit ())) { Caption = "Quit", Icon = "#Icons.exit.svg", CanExecute = true }; CMDUndo = new Command (new Action (undo)) { Caption = "Undo", Icon = "#Icons.undo.svg", CanExecute = false }; CMDRedo = new Command (new Action (redo)) { Caption = "Redo", Icon = "#Icons.redo.svg", CanExecute = false }; - CMDCut = new Command (new Action (() => Quit ())) { Caption = "Cut", Icon = "#Icons.scissors.svg", CanExecute = false }; - CMDCopy = new Command (new Action (() => Quit ())) { Caption = "Copy", Icon = "#Icons.copy-file.svg", CanExecute = false }; - CMDPaste = new Command (new Action (() => Quit ())) { Caption = "Paste", Icon = "#Icons.paste-on-document.svg", CanExecute = false }; + CMDCut = new Command (new Action (() => cut ())) { Caption = "Cut", Icon = "#Icons.scissors.svg", CanExecute = false }; + CMDCopy = new Command (new Action (() => copy ())) { Caption = "Copy", Icon = "#Icons.copy-file.svg", CanExecute = false }; + CMDPaste = new Command (new Action (() => paste ())) { Caption = "Paste", Icon = "#Icons.paste-on-document.svg", CanExecute = false }; } void onNewFile () { @@ -190,8 +203,9 @@ namespace ShowCase byte [] buff = Encoding.UTF8.GetBytes (source); s.Write (buff, 0, buff.Length); } - dirtyUndoLevel = undoStack.Count; + origSource = source; NotifyValueChanged ("IsDirty", IsDirty); + CMDSave.CanExecute = false; } void reloadFromFile () { @@ -200,7 +214,7 @@ namespace ShowCase if (File.Exists (CurrentFile)) { using (Stream s = new FileStream (CurrentFile, FileMode.Open)) { using (StreamReader sr = new StreamReader (s)) - Source = sr.ReadToEnd (); + Source = origSource = sr.ReadToEnd (); } } disableTextChangedEvent = false; @@ -235,8 +249,7 @@ namespace ShowCase undoStack.Clear (); redoStack.Clear (); CMDUndo.CanExecute = false; - CMDRedo.CanExecute = false; - dirtyUndoLevel = 0; + CMDRedo.CanExecute = false; } void showError (Exception ex) { NotifyValueChanged ("ErrorMessage", (object)ex.Message); @@ -274,18 +287,31 @@ namespace ShowCase void onTextChanged (object sender, TextChangeEventArgs e) { if (disableTextChangedEvent) return; - undoStack.Push (e.Change.Inverse (source)); + applyChange (e.Change); + } + void applyChange (TextChange change) { + undoStack.Push (change.Inverse (source)); redoStack.Clear (); CMDUndo.CanExecute = true; CMDRedo.CanExecute = false; - apply (e.Change); + apply (change); + } + + 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; } void textView_KeyDown (object sender, Crow.KeyEventArgs e) { - if (Ctrl && e.Key == Glfw.Key.W) { - if (Shift) - CMDRedo.Execute (); - else - CMDUndo.Execute (); + if (Ctrl) { + if (e.Key == Glfw.Key.W) { + if (Shift) + CMDRedo.Execute (); + else + CMDUndo.Execute (); + } else if (e.Key == Glfw.Key.S) { + onSave (); + } } } diff --git a/Samples/ShowCase/ui/showcase.crow b/Samples/ShowCase/ui/showcase.crow index 6217373c..5b0e467c 100644 --- a/Samples/ShowCase/ui/showcase.crow +++ b/Samples/ShowCase/ui/showcase.crow @@ -19,6 +19,9 @@ <Button Style="IcoButton" Command="{CMDSaveAs}" /> <Button Style="IcoButton" Command="{CMDUndo}" /> <Button Style="IcoButton" Command="{CMDRedo}" /> + <Button Style="IcoButton" Command="{CMDCut}" /> + <Button Style="IcoButton" Command="{CMDCopy}" /> + <Button Style="IcoButton" Command="{CMDPaste}" /> <Widget Width="Stretched"/> <Label Text="Line:" Foreground="Grey"/> <Label Text="{../../tb.CurrentLine}" Margin="2"/> @@ -26,8 +29,9 @@ <Label Text="{../../tb.CurrentColumn}" Margin="2"/> </HorizontalStack> <HorizontalStack> - <TextBox Name="tb" Text="{Source}" Multiline="true" Font="consolas, 12" Focusable="true" Height="Stretched" Width="Stretched" - TextChanged="onTextChanged" KeyDown="textView_KeyDown" ContextCommands="{ContextCommands}"/> + <TextBox Name="tb" Text="{Source}" Multiline="true" Font="consolas, 12" Focusable="true" Height="Stretched" Width="Stretched" + TextChanged="onTextChanged" KeyDown="textView_KeyDown" ContextCommands="{EditorCommands}"/> + <!--SelectionChanged="onSelectedTextChanged"--> <ScrollBar Value="{²../tb.ScrollY}" LargeIncrement="{../tb.PageHeight}" SmallIncrement="1" CursorRatio="{../tb.ChildHeightRatio}" Maximum="{../tb.MaxScrollY}" /> @@ -35,7 +39,10 @@ <ScrollBar Style="HScrollBar" Value="{²../tb.ScrollX}" LargeIncrement="{../tb.PageWidth}" SmallIncrement="1" CursorRatio="{../tb.ChildWidthRatio}" Maximum="{../tb.MaxScrollX}" /> - <Label Text="{CurrentFile}" Width="Stretched"/> + <HorizontalStack Height="Fit"> + <Widget Width="10" Height="10" Background="RoyalBlue" Visible="{IsDirty}"/> + <Label Text="{CurrentFile}" Width="Stretched"/> + </HorizontalStack> <Label Visible="{ShowError}" Text="{ErrorMessage}" Background="Red" Foreground="White" Width="Stretched" Margin="2" Multiline="true"/> </VerticalStack> |