From 8d2378386b657b823f62257d326d4c0683e0f4c3 Mon Sep 17 00:00:00 2001 From: Tig Date: Sun, 16 Jun 2024 22:03:00 -0700 Subject: [PATCH] Updated MultiNotepad --- Terminal.Gui/Views/Shortcut.cs | 40 ++++-------- UICatalog/Scenarios/Notepad.cs | 106 +++++++++++++++---------------- UICatalog/Scenarios/Shortcuts.cs | 2 +- 3 files changed, 63 insertions(+), 85 deletions(-) diff --git a/Terminal.Gui/Views/Shortcut.cs b/Terminal.Gui/Views/Shortcut.cs index fb66cf305..0042d08c4 100644 --- a/Terminal.Gui/Views/Shortcut.cs +++ b/Terminal.Gui/Views/Shortcut.cs @@ -126,7 +126,7 @@ public class Shortcut : View SetHelpViewDefaultLayout (); SetKeyViewDefaultLayout (); - SetColorScheme (); + SetColors (); } // Helper to set Width consistently @@ -312,22 +312,10 @@ public class Shortcut : View // When the Shortcut is clicked, we want to invoke the Command and Set focus var view = sender as View; - if (view != CommandView) - { - CommandView.InvokeCommand (Command.Accept); - e.Handled = true; - } - if (!e.Handled) { // If the subview (likely CommandView) didn't handle the mouse click, invoke the command. - bool? handled = false; - handled = InvokeCommand (Command.Accept); - - if (handled.HasValue) - { - e.Handled = handled.Value; - } + e.Handled = InvokeCommand (Command.Accept) == true; } if (CanFocus) @@ -335,7 +323,6 @@ public class Shortcut : View SetFocus (); } - e.Handled = true; } #region Command @@ -426,12 +413,6 @@ public class Shortcut : View return; - void CommandViewTextChanged (object sender, StateEventArgs e) - { - Title = _commandView.Text; - ShowHide (); - } - void CommandViewAccept (object sender, CancelEventArgs e) { // When the CommandView fires its Accept event, we want to act as though the @@ -440,8 +421,6 @@ public class Shortcut : View { e.Cancel = true; } - - //e.Cancel = true; } } } @@ -643,7 +622,7 @@ public class Shortcut : View break; case KeyBindingScope.HotKey: - handled = _commandView.InvokeCommand (Command.HotKey) == true; + _commandView.InvokeCommand (Command.HotKey); handled = false; break; @@ -655,6 +634,8 @@ public class Shortcut : View if (base.OnAccept () is false) { Action?.Invoke (); + + return true; } } @@ -681,11 +662,14 @@ public class Shortcut : View set { base.ColorScheme = value; - SetColorScheme (); + SetColors (); } } - public void SetColorScheme () + /// + /// + /// + internal void SetColors () { // Border should match superview. Border.ColorScheme = SuperView?.ColorScheme; @@ -721,14 +705,14 @@ public class Shortcut : View /// public override bool OnEnter (View view) { - SetColorScheme (); + SetColors (); return base.OnEnter (view); } /// public override bool OnLeave (View view) { - SetColorScheme (); + SetColors (); return base.OnLeave (view); } diff --git a/UICatalog/Scenarios/Notepad.cs b/UICatalog/Scenarios/Notepad.cs index ba182e0ed..dd962bfc1 100644 --- a/UICatalog/Scenarios/Notepad.cs +++ b/UICatalog/Scenarios/Notepad.cs @@ -11,9 +11,7 @@ namespace UICatalog.Scenarios; public class Notepad : Scenario { private TabView _focusedTabView; -#if V2_STATUSBAR - private StatusItem _lenStatusItem; -#endif + public Shortcut LenShortcut { get; private set; } private int _numNewTabs = 1; private TabView _tabView; @@ -41,11 +39,11 @@ public class Notepad : Scenario | KeyCode.CtrlMask | KeyCode.AltMask ), - new ("_Open", "", () => Open ()), - new ("_Save", "", () => Save ()), + new ("_Open", "", Open), + new ("_Save", "", Save), new ("Save _As", "", () => SaveAs ()), - new ("_Close", "", () => Close ()), - new ("_Quit", "", () => Quit ()) + new ("_Close", "", Close), + new ("_Quit", "", Quit) } ), new ( @@ -68,35 +66,31 @@ public class Notepad : Scenario split.LineStyle = LineStyle.None; top.Add (split); -#if V2_STATUSBAR - _lenStatusItem = new (KeyCode.CharMask, "Len: ", null); + LenShortcut = new (Key.Empty, "Len: ", null); - var statusBar = new StatusBar ( - new [] - { - new ( - Application.QuitKey, - $"{Application.QuitKey} to Quit", - () => Quit () - ), - - // These shortcut keys don't seem to work correctly in linux - //new StatusItem(Key.CtrlMask | Key.N, "~^O~ Open", () => Open()), - //new StatusItem(Key.CtrlMask | Key.N, "~^N~ New", () => New()), - - new (KeyCode.CtrlMask | KeyCode.S, "~^S~ Save", () => Save ()), - new (KeyCode.CtrlMask | KeyCode.W, "~^W~ Close", () => Close ()), - _lenStatusItem + var statusBar = new StatusBar (new [] { + new (Application.QuitKey, $"Quit", Quit), + new Shortcut(Key.F2, "Open", Open), + new Shortcut(Key.F1, "New", New), + new (Key.F3, "Save", Save), + new (Key.F6, "Close", Close), + LenShortcut } - ); + ) + { + AlignmentModes = AlignmentModes.IgnoreFirstOrLast + }; top.Add (statusBar); -#endif _focusedTabView = _tabView; _tabView.SelectedTabChanged += TabView_SelectedTabChanged; _tabView.Enter += (s, e) => _focusedTabView = _tabView; - top.Ready += (s, e) => New (); + top.Ready += (s, e) => + { + New (); + LenShortcut.Title = $"Len:{_focusedTabView.Text?.Length ?? 0}"; + }; Application.Run (top); top.Dispose (); @@ -283,7 +277,7 @@ public class Notepad : Scenario /// File that was read or null if a new blank document private void Open (FileInfo fileInfo, string tabName) { - var tab = new OpenedFile { DisplayText = tabName, File = fileInfo }; + var tab = new OpenedFile (this) { DisplayText = tabName, File = fileInfo }; tab.View = tab.CreateTextView (fileInfo); tab.SavedText = tab.View.Text; tab.RegisterTextViewEvents (_focusedTabView); @@ -327,9 +321,8 @@ public class Notepad : Scenario private void TabView_SelectedTabChanged (object sender, TabChangedEventArgs e) { -#if V2_STATUSBAR - _lenStatusItem.Title = $"Len:{e.NewTab?.View?.Text?.Length ?? 0}"; -#endif + LenShortcut.Title = $"Len:{e.NewTab?.View?.Text?.Length ?? 0}"; + e.NewTab?.View?.SetFocus (); } @@ -376,11 +369,13 @@ public class Notepad : Scenario e.MouseEvent.Handled = true; } - private class OpenedFile : Tab + private class OpenedFile (Notepad notepad) : Tab { + private Notepad _notepad = notepad; + public OpenedFile CloneTo (TabView other) { - var newTab = new OpenedFile { DisplayText = base.Text, File = File }; + var newTab = new OpenedFile (_notepad) { DisplayText = base.Text, File = File }; newTab.View = newTab.CreateTextView (newTab.File); newTab.SavedText = newTab.View.Text; newTab.RegisterTextViewEvents (other); @@ -416,28 +411,27 @@ public class Notepad : Scenario var textView = (TextView)View; // when user makes changes rename tab to indicate unsaved - textView.KeyUp += (s, k) => - { - // if current text doesn't match saved text - bool areDiff = UnsavedChanges; + textView.ContentsChanged += (s, k) => + { + // if current text doesn't match saved text + bool areDiff = UnsavedChanges; - if (areDiff) - { - if (!Text.EndsWith ('*')) - { - Text = Text + '*'; - parent.SetNeedsDisplay (); - } - } - else - { - if (Text.EndsWith ('*')) - { - Text = Text.TrimEnd ('*'); - parent.SetNeedsDisplay (); - } - } - }; + if (areDiff) + { + if (!DisplayText.EndsWith ('*')) + { + DisplayText = Text + '*'; + } + } + else + { + if (DisplayText.EndsWith ('*')) + { + DisplayText = Text.TrimEnd ('*'); + } + } + _notepad.LenShortcut.Title = $"Len:{textView.Text.Length}"; + }; } /// The text of the tab the last time it was saved @@ -458,7 +452,7 @@ public class Notepad : Scenario System.IO.File.WriteAllText (File.FullName, newText); SavedText = newText; - Text = Text.TrimEnd ('*'); + DisplayText = DisplayText.TrimEnd ('*'); } } } diff --git a/UICatalog/Scenarios/Shortcuts.cs b/UICatalog/Scenarios/Shortcuts.cs index 7e1906e2b..69720ac2c 100644 --- a/UICatalog/Scenarios/Shortcuts.cs +++ b/UICatalog/Scenarios/Shortcuts.cs @@ -182,7 +182,7 @@ public class Shortcuts : Scenario if (peer.CanFocus) { peer.CommandView.CanFocus = e.NewValue == true; - peer.SetColorScheme (); + //peer.SetColors (); } } }