diff options
| author | Jean-Philippe Bruyère <jp_bruyere@hotmail.com> | 2021-06-07 15:05:30 +0200 |
|---|---|---|
| committer | Jean-Philippe Bruyère <jp_bruyere@hotmail.com> | 2021-06-07 15:05:30 +0200 |
| commit | 263de03914a3de5d55c4508c322e32d36caf2a87 (patch) | |
| tree | c279464365fd7683744fa59d01b242e4b3ab17b9 /Samples/ShowCase | |
| parent | f738cf0f78c57750cce8fd5744825b0591f24bb8 (diff) | |
suggestion in IML editor, editor base classe for ShowCase and LogAnalyzerSimplifiedTabView
Diffstat (limited to 'Samples/ShowCase')
| -rw-r--r-- | Samples/ShowCase/ShowCase.cs | 279 | ||||
| -rw-r--r-- | Samples/ShowCase/ui/showcase.crow | 56 |
2 files changed, 60 insertions, 275 deletions
diff --git a/Samples/ShowCase/ShowCase.cs b/Samples/ShowCase/ShowCase.cs index ed5d8575..95d15a85 100644 --- a/Samples/ShowCase/ShowCase.cs +++ b/Samples/ShowCase/ShowCase.cs @@ -1,4 +1,4 @@ -// Copyright (c) 2013-2019 Bruyère Jean-Philippe <jp_bruyere@hotmail.com> +// Copyright (c) 2013-2021 Bruyère Jean-Philippe <jp_bruyere@hotmail.com> // // This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT) @@ -13,10 +13,11 @@ using System.Diagnostics; using Crow.Text; using System.Collections.Generic; using Encoding = System.Text.Encoding; +using Samples; namespace ShowCase { - class Showcase : SampleBase + class Showcase : SampleBaseForEditor { static void Main () { @@ -28,42 +29,17 @@ namespace ShowCase using (Showcase app = new Showcase ()) { //app.Theme = @"C:\Users\Jean-Philippe\source\Crow\Themes\TestTheme"; + CurrentProgramInstance = app; app.Run (); } } - public Container crowContainer; + public Container crowContainer; - public string CurrentDir { - get => Configuration.Global.Get<string> (nameof (CurrentDir)); - set { - if (CurrentDir == value) - return; - Configuration.Global.Set (nameof (CurrentDir), value); - NotifyValueChanged (CurrentDir); - } - } - public string CurrentFile { - get => Configuration.Global.Get<string> (nameof (CurrentFile)); - set { - if (CurrentFile == value) - return; - Configuration.Global.Set (nameof (CurrentFile), value); - NotifyValueChanged (CurrentFile); - } - } - - public Command CMDNew, CMDOpen, CMDSave, CMDSaveAs, CMDQuit, CMDShowLeftPane, - CMDUndo, CMDRedo, CMDCut, CMDCopy, CMDPaste, CMDHelp, CMDAbout, CMDOptions; - - const string _defaultFileName = "unnamed.txt"; - string source = "", origSource; - TextBox editor; Stopwatch reloadChrono = new Stopwatch (); - public new bool IsDirty => source != origSource; - public string Source { + public override string Source { get => source; set { if (source == value) @@ -75,153 +51,8 @@ namespace ShowCase 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)) { - 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<char> tmp = stackalloc char[source.Length + (change.ChangedText.Length - change.Length)]; - ReadOnlySpan<char> 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 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); - - } - void onNewFile () { - if (IsDirty) { - MessageBox mb = MessageBox.ShowModal (this, MessageBox.Type.YesNo, "Current file has unsaved changes, are you sure?"); - mb.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 (@"<FileDialog Width='60%' Height='50%' Caption='Save as ...' CurrentDirectory='" + - dir + "' SelectedFile='" + - Path.GetFileName(CurrentFile) + "' OkClicked='saveFileDialog_OkClicked'/>").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 = @"<Label Text='Hello World' Background='MediumSeaGreen' Margin='10'/>"; - disableTextChangedEvent = false; - resetUndoRedo (); - if (!string.IsNullOrEmpty (CurrentFile)) - CurrentFile = Path.Combine (Path.GetDirectoryName (CurrentFile), "newfile.crow"); - else - CurrentFile = Path.Combine (CurrentDir, "newfile.crow"); - } - - void save () { - using (Stream s = new FileStream(CurrentFile, FileMode.Create)) { - s.WriteByte (0xEF); - s.WriteByte (0xBB); - s.WriteByte (0xBF); - byte [] buff = Encoding.UTF8.GetBytes (source); - s.Write (buff, 0, buff.Length); - } - origSource = source; - NotifyValueChanged ("IsDirty", IsDirty); - CMDSave.CanExecute = false; - } - - void reloadFromFile () { - hideError (); - disableTextChangedEvent = true; - if (File.Exists (CurrentFile)) { - using (Stream s = new FileStream (CurrentFile, FileMode.Open)) { - using (StreamReader sr = new StreamReader (s)) - Source = origSource = sr.ReadToEnd (); - } - } - disableTextChangedEvent = false; - resetUndoRedo (); - } void reloadFromSource () { hideError (); Widget g = null; @@ -241,12 +72,6 @@ namespace ShowCase } } - void resetUndoRedo () { - undoStack.Clear (); - redoStack.Clear (); - CMDUndo.CanExecute = false; - CMDRedo.CanExecute = false; - } void showError (Exception ex) { NotifyValueChanged ("ErrorMessage", ex); NotifyValueChanged ("ShowError", true); @@ -255,70 +80,12 @@ namespace ShowCase NotifyValueChanged ("ShowError", false); } - public void goUpDirClick (object sender, MouseButtonEventArgs e) - { - string root = Directory.GetDirectoryRoot (CurrentDir); - if (CurrentDir == root) - return; - CurrentDir = Directory.GetParent (CurrentDir).FullName; - } - - void Dv_SelectedItemChanged (object sender, SelectionChangeEventArgs e) - { - FileSystemInfo fi = e.NewValue as FileSystemInfo; - if (fi == null) - return; - if (fi is DirectoryInfo) - return; - if (IsDirty) { - MessageBox mb = MessageBox.ShowModal (this, MessageBox.Type.YesNo, "Current file has unsaved changes, are you sure?"); - mb.Yes += (mbsender, mbe) => { CurrentFile = fi.FullName; reloadFromFile (); }; - return; - } - CurrentFile = fi.FullName; - reloadFromFile (); - } - void onTextChanged (object sender, TextChangeEventArgs e) { - if (disableTextChangedEvent) - return; - applyChange (e.Change); - } - void applyChange (TextChange change) { - undoStack.Push (change.Inverse (source)); - redoStack.Clear (); - CMDUndo.CanExecute = true; - CMDRedo.CanExecute = false; - 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) { - if (e.Key == Glfw.Key.W) { - if (Shift) - CMDRedo.Execute (); - else - CMDUndo.Execute (); - } else if (e.Key == Glfw.Key.S) { - onSave (); - } - } - } protected override void OnInitialized () { - initCommands (); - base.OnInitialized (); - if (string.IsNullOrEmpty (CurrentDir)) - CurrentDir = Path.Combine (Directory.GetCurrentDirectory (), "Interfaces"); - Load ("#ShowCase.showcase.crow").DataSource = this; crowContainer = FindByName ("CrowContainer") as Container; editor = FindByName ("tb") as TextBox; @@ -341,29 +108,7 @@ namespace ShowCase reloadChrono.Reset (); } - public DbgEvtType RecordedEvents { - get => Configuration.Global.Get<DbgEvtType> (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<DbgEvtType> (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 => !DbgLogger.ConsoleOutput; set { @@ -383,16 +128,6 @@ namespace ShowCase NotifyValueChanged (DebugLogFilePath); } } - bool debugLogRecording; - public bool DebugLogRecording { - get => debugLogRecording; - set { - if (debugLogRecording == value) - return; - debugLogRecording = value; - NotifyValueChanged(debugLogRecording); - } - } public override bool OnKeyDown (Key key) { diff --git a/Samples/ShowCase/ui/showcase.crow b/Samples/ShowCase/ui/showcase.crow index 529c4772..1236b705 100644 --- a/Samples/ShowCase/ui/showcase.crow +++ b/Samples/ShowCase/ui/showcase.crow @@ -6,7 +6,57 @@ Background="Jet" MouseEnter="{Background=Grey}" MouseLeave="{Background=Jet}" /> <TextBox Text="{²CurrentDir}" Margin="2"/> </HorizontalStack> - <DirectoryView Margin="1" Name="dv" CurrentDirectory="{CurrentDir}" SelectedItemChanged="Dv_SelectedItemChanged"/> + <DirectoryView Margin="1" Name="dv" CurrentDirectory="{CurrentDir}" SelectedItemChanged="Dv_SelectedItemChanged"> + <Template> + <TreeView IsRoot="true" Name="treeView" Data="{./FileSystemEntries}" Background="{./Background}" + SelectedItemChanged="./onSelectedItemChanged"> + <ItemTemplate DataType="System.IO.FileInfo"> + <ListItem CornerRadius="2" Margin="0" Height="Fit" Width="Stretched" + ContextCommands="{GetCommands}" + Selected="{Background=${ControlHighlight}}" + Unselected="{Background=Transparent}"> + <HorizontalStack> + <Image Margin="1" Width="14" Height="14" Path="#Crow.Icons.file.svg"/> + <Label Text="{Name}" Width="Stretched"/> + </HorizontalStack> + </ListItem> + </ItemTemplate> + <ItemTemplate DataType="System.IO.DirectoryInfo" Data="GetFileSystemInfosOrdered"> + <ListItem ContextCommands="{GetCommands}" + Selected="{/exp.Background=${ControlHighlight}}" + Unselected="{/exp.Background=Transparent}"> + <Expandable Name="exp" Caption="{Name}" MouseDoubleClick="/onClickForExpand"> + <Template> + <VerticalStack> + <Border CornerRadius="2" Margin="0" Height="Fit" MouseDoubleClick="./onClickForExpand" + Foreground="Transparent" + MouseEnter="{Foreground=DimGrey}" + MouseLeave="{Foreground=Transparent}"> + <HorizontalStack Background="{./Background}" Spacing="1"> + <Image Margin="1" Width="9" Height="9" Focusable="true" MouseDown="./onClickForExpand" + Path="{./Image}" + Visible="{./IsExpandable}" + SvgSub="{./IsExpanded}" + MouseEnter="{Background=LightGrey}" + MouseLeave="{Background=Transparent}"/> + <Image Margin="1" Width="16" Height="16" + Path="#Crow.Icons.folder.svg" SvgSub="{./IsExpanded}"/> + <Label Text="{./Caption}"/> + </HorizontalStack> + </Border> + <Container Name="Content" Visible="false"/> + </VerticalStack> + </Template> + <HorizontalStack Height="Fit"> + <Widget Width="12" Height="10"/> + <VerticalStack Height="Fit" Name="ItemsContainer"/> + </HorizontalStack> + </Expandable> + </ListItem> + </ItemTemplate> + </TreeView> + </Template> + </DirectoryView> </VerticalStack> <Splitter Width="6" /> <VerticalStack> @@ -31,9 +81,9 @@ <Button Style="IcoButton" Command="{CMDNew}" /> <Button Style="IcoButton" Command="{CMDSave}" /> <Button Style="IcoButton" Command="{CMDSaveAs}" /> - <!--<Button Style="IcoButton" Command="{CMDUndo}" /> + <Button Style="IcoButton" Command="{CMDUndo}" /> <Button Style="IcoButton" Command="{CMDRedo}" /> - <Button Style="IcoButton" Command="{CMDCut}" /> + <!--<Button Style="IcoButton" Command="{CMDCut}" /> <Button Style="IcoButton" Command="{CMDCopy}" /> <Button Style="IcoButton" Command="{CMDPaste}" />--> <Popper IsVisible="{DebugLoggingEnabled}" Fit="true"> |