From 3716633aad4d512161ece936e2e929aa5f03841f Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 23 Nov 2026 19:58:49 +0000 Subject: [PATCH] Esc in the docked composer goes back to the tab it came from; add a close button MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Esc released the composer but left the focus on the composer's own pane, so the command line was the character's main window's even after reading Chat. It now activates the window that was in front before the composer took the keyboard (not when that window shares the composer's pane, which would hide it). Both hosts go through LeaveComposer. A composer alone in its pane draws no tab strip, so it could only be closed with Ctrl+W. Its heading now carries a close: a × link in the cells and a button in the window, both ending in CloseDockedComposer. Closing a composer window also re-pins the focus to the command line. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Rqqt7SwB1QPsj3aFAjhdDd --- CHANGELOG.md | 11 ++ CLAUDE.md | 12 +- src/SharpMUTerm.Avalonia/ComposerView.cs | 54 ++++++- .../Composer/ComposerSession.cs | 12 ++ .../Surfaces/SurfaceCatalog.cs | 6 +- src/SharpMUTerm.Tui/Panels/DockedComposer.cs | 28 +++- src/SharpMUTerm.Tui/Panels/HeadingBand.cs | 13 +- .../SharpMUTermApp.NativeComposer.cs | 3 +- src/SharpMUTerm.Tui/SharpMUTermApp.Panels.cs | 64 +++++++- src/SharpMUTerm.Tui/SharpMUTermApp.cs | 8 + .../NativeComposerTests.cs | 24 +++ .../SurfaceParityTests.cs | 30 ++++ .../DockedComposerTests.cs | 139 +++++++++++++++++- .../NativeComposerRoutingTests.cs | 51 +++++++ 14 files changed, 439 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b0f3351..8ca7e33a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,8 +7,19 @@ and a release's notes are its section. The procedure is in [`docs/RELEASING.md`] ## [Unreleased] +### Added + +- The docked composer's heading has a close button (`×`), in the terminal and the window. A composer in a pane of its + own, as the Roleplay layout puts it, had no tab to close it by. The draft is kept, and *Dock the composer* brings it + back. + ### Fixed +- Esc in the docked composer goes back to the tab you came from. It used to leave the keyboard on the composer's own + pane, so the command line was your character's main window's even when you had been reading Chat. +- Closing the docked composer (Ctrl+W or the new button) puts the keyboard focus straight back on the command line, so + a paste lands there. + - A character whose name has a space, connected as `"first last"` on a MUSH, gets a session log again. The quotes went into the log's file name, which Windows refuses, so no log was written. World and character names are now made safe for Windows, Linux and macOS before they name a file: quotes and control characters are dropped, `< > : / \ | ? *` diff --git a/CLAUDE.md b/CLAUDE.md index c1d327e0..230b7574 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -1653,13 +1653,19 @@ fallbacks) for inline images/maps. not been released (`_composerReleased`). While armed, `PinFocusToArmedBar` pins the framework's focus to the *editor* instead of the bar — so typing, paste and the caret all land there by the pin's existing rule — and both command lines wear the idle band. `HandleWindowKey` hands an armed composer's keys to - `HandleComposerKey` (macros, resize, ⌃C/⌃X, ⌃S, ⌥L, Esc, the ⌃arrows, else the editor). **Esc releases** (the - bar arms and ⏎ sends from it, to the same character); the ⌃↑/⌃↓ ladder releases too; **arriving** at the + `HandleComposerKey` (macros, resize, ⌃C/⌃X, ⌃S, ⌥L, Esc, the ⌃arrows, else the editor). **Esc releases and goes + back** (`LeaveComposer`, both hosts): the window in front before the composer took the keyboard (`_windowBeforeComposer`, + say Chat) is activated, so the bar arms there and ⏎ sends from *that* window's command line; leaving the focus on the + composer's pane gave the main window's bar, which read as "Esc goes to the first tab". The ⌃↑/⌃↓ ladder releases too; **arriving** at the composer's pane (⌃O, ⌃arrows, a tab, a click in the editor — `ArmClickedComposer` on `FocusChanged`, guarded by `_pinningFocus` so the pin's own focus moves never count) arms it again. `ArmBar` does **not** release: it is re-asserted on every layout change, and releasing there disarmed the composer the moment its pane was built. ⌥⏎ arrives as Esc then Enter, so an Enter within 50 ms of the releasing Esc re-arms and inserts a newline - (`TryComposerAltEnter`). `DockedComposerTests` covers each of these. + (`TryComposerAltEnter`, which activates the composer's window again first). `DockedComposerTests` covers each of these. + - **A lone composer pane draws no tab strip, so its heading carries a close** (`DockedComposer.CloseLink`, a `×` link only + its header answers, the last cell but one of the band; the window's `ComposerView` close button → + `ComposerSession.ClosePanel`). Both end in `CloseDockedComposer` → `CloseWindow`, which records the panel closed and keeps + the draft; closing any composer window re-pins the focus to the command line (it was left on the gone editor). - **The window can pop a native panel out into a window of its own** (map, vitals, who-here, quick bar; `SharpMUTermApp.Floating.cs`, Avalonia `FloatingPanels.cs`: `FloatingPanelPresenter`, `FloatingPanelWindow`, `PopOutButton`; Core `FloatingBounds`; Client `PanelFloatCommands`, `FloatingPanelModel`; ⌃P `panel:popout:` diff --git a/src/SharpMUTerm.Avalonia/ComposerView.cs b/src/SharpMUTerm.Avalonia/ComposerView.cs index 9aa4a83b..2d78e8d7 100644 --- a/src/SharpMUTerm.Avalonia/ComposerView.cs +++ b/src/SharpMUTerm.Avalonia/ComposerView.cs @@ -80,6 +80,9 @@ public IEnumerable EdgeRules(Size size) private readonly FittedText _summary = new() { VerticalAlignment = VerticalAlignment.Center }; private readonly Border _previewButton; private readonly Border _sendButton; + + // The docked heading's ×: closes the composer's pane (ComposerSession.ClosePanel), which draws no tab strip of its own. + private readonly Border _closeButton; private ComposerSession? _session; private ComposerModel? _model; private long _applied = -1; @@ -164,17 +167,21 @@ public ComposerView(TerminalView view, bool fullScreen) _target = new TextBlock { VerticalAlignment = VerticalAlignment.Center, TextTrimming = TextTrimming.CharacterEllipsis }; _chips = new StackPanel { Orientation = Orientation.Horizontal, Spacing = NativeTokens.Space6, VerticalAlignment = VerticalAlignment.Center }; _count = new TextBlock { VerticalAlignment = VerticalAlignment.Center, Margin = new Thickness(NativeTokens.Space10, 0, 0, 0) }; + _closeButton = CloseButton(() => _session?.ClosePanel()); + _closeButton.IsVisible = !fullScreen; var headerRow = new DockPanel { LastChildFill = true }; DockPanel.SetDock(_title, Dock.Left); + DockPanel.SetDock(_closeButton, Dock.Right); DockPanel.SetDock(_count, Dock.Right); DockPanel.SetDock(_chips, Dock.Right); headerRow.Children.Add(_title); + headerRow.Children.Add(_closeButton); headerRow.Children.Add(_count); headerRow.Children.Add(_chips); headerRow.Children.Add(_target); - // The docked panel's heading is every panel's (PanelText.Heading): 16 px in, a rule under it, the pop-out button - // clear at the right. - _header = new Border { Child = headerRow, Padding = fullScreen ? new Thickness(NativeTokens.Space24, NativeTokens.Space10) : new Thickness(PanelText.Pad, 0, NativeTokens.Space32, 0) }; + // The docked panel's heading is every panel's (PanelText.Heading): 16 px in, a rule under it, the close button at + // the right (the composer never pops out, so no pop-out button needs the corner). + _header = new Border { Child = headerRow, Padding = fullScreen ? new Thickness(NativeTokens.Space24, NativeTokens.Space10) : new Thickness(PanelText.Pad, 0, NativeTokens.Space8, 0) }; if (!fullScreen) { // Every panel's heading height, so the composer's rule meets the one beside it (who is here, the map). @@ -515,6 +522,11 @@ private void Paint() _title.Foreground = new SolidColorBrush(_text); _target.Foreground = new SolidColorBrush(Dim); _count.Foreground = new SolidColorBrush(Dim); + if (_closeButton.Child is TextBlock cross) + { + cross.Foreground = new SolidColorBrush(Dim); + } + _preview.Background = new SolidColorBrush(Band); _preview.BorderBrush = _header.BorderBrush; _preview.BorderThickness = new Thickness(0, NativeTokens.Hairline, 0, 0); @@ -1193,6 +1205,42 @@ private static Border ActionButton(string words, string tip, Action click) return button; } + /// The docked heading's ×: a quiet square that takes a plate under the pointer, as a tab's × does. + private Border CloseButton(Action click) + { + var button = new Border + { + Child = new TextBlock { Text = "×", HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center }, + Width = NativeTokens.ControlHeightSmall, + Height = NativeTokens.ControlHeightSmall, + Margin = new Thickness(NativeTokens.Space8, 0, 0, 0), + CornerRadius = new CornerRadius(NativeTokens.RadiusControl), + Background = Brushes.Transparent, + Cursor = new Cursor(StandardCursorType.Hand), + VerticalAlignment = VerticalAlignment.Center, + }; + button.Name = "Close composer"; + ToolTip.SetTip(button, "Close the composer (Ctrl+W). The draft is kept."); + AutomationProperties.SetName(button, "Close composer"); + button.PointerEntered += (_, _) => button.Background = new SolidColorBrush(Mix(_plane, _text, 0.1)); + button.PointerExited += (_, _) => button.Background = Brushes.Transparent; + button.PointerPressed += (_, e) => + { + if (e.GetCurrentPoint(button).Properties.IsLeftButtonPressed) + { + e.Handled = true; + click(); + } + }; + return button; + } + + /// Presses the docked heading's close button, as a click on it does. Internal for tests. + internal void PressClose() => _session?.ClosePanel(); + + /// Whether the docked heading shows its close button. Internal for tests. + internal bool ShowsClose => _closeButton.IsVisible; + private void PaintButton(Border button, bool accent) { button.Background = new SolidColorBrush(accent ? Accent : Mix(_plane, _text, 0.07)); diff --git a/src/SharpMUTerm.Client/Composer/ComposerSession.cs b/src/SharpMUTerm.Client/Composer/ComposerSession.cs index b9f2e637..3c8ca0e5 100644 --- a/src/SharpMUTerm.Client/Composer/ComposerSession.cs +++ b/src/SharpMUTerm.Client/Composer/ComposerSession.cs @@ -39,6 +39,9 @@ public sealed record ComposerCallbacks /// A click in a docked composer: it takes the keyboard. public Action? Arm { get; init; } + /// The docked composer's close button: its pane closes, the draft kept for the next one. + public Action? ClosePanel { get; init; } + /// Esc (or F1) in F1: the window closes, with the buffer to keep. public Action? Closed { get; init; } @@ -198,6 +201,15 @@ public void Arm() } } + /// The host's: the docked composer's close button. + public void ClosePanel() + { + if (!IsEnded) + { + _callbacks.ClosePanel?.Invoke(); + } + } + /// Whether this composer's character writes in a proportional face; false (monospace) with nobody to ask. public bool Proportional => _callbacks.Proportional?.Invoke() ?? false; diff --git a/src/SharpMUTerm.Client/Surfaces/SurfaceCatalog.cs b/src/SharpMUTerm.Client/Surfaces/SurfaceCatalog.cs index d829c2b1..53c027f5 100644 --- a/src/SharpMUTerm.Client/Surfaces/SurfaceCatalog.cs +++ b/src/SharpMUTerm.Client/Surfaces/SurfaceCatalog.cs @@ -485,7 +485,11 @@ [.. PanelViews("MapCanvas"), "SharpMUTerm.Avalonia.MapInspector"]), typeof(ComposerModel), [ new(SurfaceActionKind.Compose, "send", "SendDocked"), - new(SurfaceActionKind.Compose, "release", "ReleaseComposer"), + // Esc: the keyboard goes back to the window in front before the composer took it. + new(SurfaceActionKind.Compose, "release", "LeaveComposer"), + + // The heading's × (a link in the cells, a button in the window): the composer's window closes. + new(SurfaceActionKind.Compose, "close", "CloseDockedComposer"), // A click on the cell editor arms it through the framework's focus (ArmClickedComposer); the native // editor asks for the same thing by name. diff --git a/src/SharpMUTerm.Tui/Panels/DockedComposer.cs b/src/SharpMUTerm.Tui/Panels/DockedComposer.cs index 81fec366..d8cb1e60 100644 --- a/src/SharpMUTerm.Tui/Panels/DockedComposer.cs +++ b/src/SharpMUTerm.Tui/Panels/DockedComposer.cs @@ -112,6 +112,10 @@ internal DockedComposer(string windowId, string sessionKey, ScreenTheme scheme, }; _header = new MarkupControl(new List()) { HorizontalAlignment = HorizontalAlignment.Stretch }; + + // The heading's × is the panel's own link, answered here and never by the output panes' handler: a pane holding + // the composer alone draws no tab strip, so without it the pane could be closed only by ⌃W from inside it. + _header.LinkClicked += (_, e) => OnHeaderLink(e.Url); _footer = new MarkupControl(new List()) { HorizontalAlignment = HorizontalAlignment.Stretch }; var grid = Controls.Grid() @@ -255,6 +259,24 @@ internal void SetCommandFromHost(string command) /// Raised after the buffer or the mode changed by the writer's hand (never by ). internal event EventHandler? Changed; + /// Raised when the heading's × is clicked: the host closes the composer's window. + internal event EventHandler? CloseRequested; + + /// The heading's close link, a payload only this composer's header answers. + internal const string CloseLink = "mux:composer:close"; + + /// A link clicked in the heading: only the close link means anything. + private void OnHeaderLink(string url) + { + if (string.Equals(url, CloseLink, StringComparison.Ordinal)) + { + CloseRequested?.Invoke(this, EventArgs.Empty); + } + } + + /// A click on a link in the heading, as the header control raises it. Internal for tests. + internal void SimulateHeaderLink(string url) => OnHeaderLink(url); + /// The header row's markup, for the tests. internal string HeaderMarkup => _header.Text; @@ -336,7 +358,8 @@ internal void Render(ComposerModel model) var rest = (verb.Length > 0 ? $" {Escape(verb)}" : string.Empty) + $" {bandMode}"; var lead = model.Armed ? $"[{band.Accent}]▌[/]" : null; var chars = ComposerModel.CharactersLabel(model.Characters); - _header.SetContent(new List { HeadingBand.Row("COMPOSE", rest, chars, _width > 0 ? _width : VisibleLength(rest) + 9, band, lead) }); + var close = $"[link={CloseLink}]×[/]"; + _header.SetContent(new List { HeadingBand.Row("COMPOSE", rest, chars, _width > 0 ? _width : VisibleLength(rest) + 11, band, lead, close) }); RenderFooter(model); return; } @@ -347,11 +370,12 @@ internal void Render(ComposerModel model) // The length cell at the right edge, as F1 has it; the title gives way first, and a pane too narrow for both // keeps the title. - var length = $"[dim]{ComposerModel.CharactersLabel(model.Characters)}[/] "; + var length = $"[dim]{ComposerModel.CharactersLabel(model.Characters)}[/] [link={CloseLink}]×[/] "; _header.SetContent(new List { _width <= 0 ? $"{title} {length}" : _width >= VisibleLength(title) + VisibleLength(length) + 2 ? SpreadLR(title, length, _width) + : _width >= 6 ? ElideVisible(title, _width - 3) + $" [link={CloseLink}]×[/] " : ElideVisible(title, _width), }); diff --git a/src/SharpMUTerm.Tui/Panels/HeadingBand.cs b/src/SharpMUTerm.Tui/Panels/HeadingBand.cs index 95c02d7a..25855b99 100644 --- a/src/SharpMUTerm.Tui/Panels/HeadingBand.cs +++ b/src/SharpMUTerm.Tui/Panels/HeadingBand.cs @@ -33,10 +33,21 @@ internal static Ink For(Theme theme, string accent) /// the right part goes first, then the rest is elided. Never wider than . The first cell is /// the band's margin, or (one cell of markup: the docked composer's armed ▌) in it. /// - internal static string Row(string label, string rest, string right, int width, Ink ink, string? lead = null) + /// + /// One cell of markup kept at the far end whatever else gives way, before the last margin cell: the docked composer's + /// ×. Null for none. + /// + internal static string Row(string label, string rest, string right, int width, Ink ink, string? lead = null, string? close = null) { width = Math.Max(1, width); var left = $"[bold]{label}[/]{rest}"; + if (close is not null && width >= 5) + { + // The rest of the row is laid out two cells narrower, and the button and the margin take those two. + var row = Row(label, rest, right, width - 2, ink, lead); + return row[..^" [/]".Length] + $" {close} [/]"; + } + var inner = Math.Max(1, width - 2); var body = right.Length > 0 && VisibleLength(left) + VisibleLength(right) + 2 <= inner ? SpreadLR(left, right, inner) diff --git a/src/SharpMUTerm.Tui/SharpMUTermApp.NativeComposer.cs b/src/SharpMUTerm.Tui/SharpMUTermApp.NativeComposer.cs index b0a8fee0..3ccf815f 100644 --- a/src/SharpMUTerm.Tui/SharpMUTermApp.NativeComposer.cs +++ b/src/SharpMUTerm.Tui/SharpMUTermApp.NativeComposer.cs @@ -327,10 +327,11 @@ private ComposerSession DockedSessionFor(DockedComposer composer) { if (ReferenceEquals(ArmedComposer(), composer)) { - ReleaseComposer(); + LeaveComposer(); } }), Arm = () => OnUiThread(() => ArmComposer(composer)), + ClosePanel = () => OnUiThread(() => CloseDockedComposer(composer)), Proportional = ComposerFaceOf(composer.SessionKey), ChooseFace = ChooseComposerFace(composer.SessionKey), }); diff --git a/src/SharpMUTerm.Tui/SharpMUTermApp.Panels.cs b/src/SharpMUTerm.Tui/SharpMUTermApp.Panels.cs index b47ed238..d0384103 100644 --- a/src/SharpMUTerm.Tui/SharpMUTermApp.Panels.cs +++ b/src/SharpMUTerm.Tui/SharpMUTermApp.Panels.cs @@ -57,6 +57,16 @@ internal sealed partial class SharpMUTermApp /// When an Esc released a composer, so the Enter of a ⌥⏎ that follows it is not sent from the bar. private DateTimeOffset? _composerEscapeAt; + /// The composer window that Esc released, so the Enter of a ⌥⏎ can go back into it. + private string? _composerEscapeWindow; + + /// + /// The last window in front that was not a docked composer: where Esc hands the keyboard back to. Without it Esc + /// left the focus on the composer's own pane, whose command line is its character's main window's, not the tab the + /// writer was reading (Chat, say). + /// + private string? _windowBeforeComposer; + partial void RegisterTerminalPanels() { Panels.Register(PanelIds.WhoHere, NewWhoHerePanel); @@ -392,12 +402,27 @@ private IWindowControl NewDockedComposer(TerminalPanelContext context) } composer.Changed += (_, _) => KeepDockedDraft(composer); + composer.CloseRequested += (_, _) => CloseDockedComposer(composer); _dockedComposers[context.WindowId] = composer; var box = PanelBox(context.WindowId); composer.Fit(box.Width, box.Height); return composer.Control; } + /// + /// The composer's close button, in either host: its window closes as ⌃W or a tab's × closes it, recorded as a + /// closed panel, and the draft is kept for the next composer or F1. A pane holding the composer alone draws no tab + /// strip, so this is the way to close it that does not first mean moving a tab into its pane. + /// + private void CloseDockedComposer(DockedComposer composer) + { + if (_dockedComposers.TryGetValue(composer.WindowId, out var known) && ReferenceEquals(known, composer)) + { + KeepDockedDraft(composer); + CloseWindow(composer.WindowId); + } + } + /// /// Writes the docked buffer into the draft store F1 opens from, so the two are one draft. An empty buffer /// forgets the draft, as an F1 window closed empty does. @@ -436,6 +461,11 @@ private void SyncDockedComposers(string? sessionKey) var windowId = ActiveWindowId(); if (!string.Equals(windowId, _composerArmWindow, StringComparison.Ordinal)) { + if (_composerArmWindow is { } left && !_dockedComposers.ContainsKey(left)) + { + _windowBeforeComposer = left; + } + _composerArmWindow = windowId; _composerReleased = false; } @@ -490,7 +520,7 @@ private void SyncComposerArm() PinFocusToArmedBar(); } - /// Esc in the composer: the keyboard goes back to the command line; the pane stays where it is. + /// Hands the keyboard back to the command line; the pane stays where it is. private void ReleaseComposer() { _composerReleased = true; @@ -498,6 +528,26 @@ private void ReleaseComposer() PinFocusToArmedBar(); } + /// + /// Esc in the composer, in either host: the keyboard goes back to the window it came from — the tab that was in + /// front before the composer took the keyboard — so ⏎ sends from that window's command line. Where that window is + /// gone, shares the composer's pane, or the composer was in front from the start, the pane stays where it is. + /// + private void LeaveComposer() + { + var composerWindow = ActiveWindowId(); + ReleaseComposer(); + _composerEscapeWindow = composerWindow; + // Only from another pane: a window sharing the composer's pane would be brought in front of the composer and hide + // what was being written. + if (_windowBeforeComposer is { } back && !string.Equals(back, composerWindow, StringComparison.Ordinal) + && !_dockedComposers.ContainsKey(back) && _workspace.Layout.FindWindow(back) is { } pane + && !string.Equals(pane.Id, _workspace.Layout.FindWindow(composerWindow)?.Id, StringComparison.Ordinal)) + { + Activate(back); + } + } + /// /// A click put the framework's focus on a docked composer's editor: arm that composer, bringing its window to /// the front when it is not. Called on the framework's FocusChanged, ahead of the focus pin, which would @@ -603,7 +653,7 @@ private void ArmClickedComposer() { e.Handled = true; _composerEscapeAt = _time.GetUtcNow(); - ReleaseComposer(); + LeaveComposer(); return null; } @@ -643,15 +693,23 @@ private void ArmClickedComposer() private bool TryComposerAltEnter(KeyPressedEventArgs e) { var at = _composerEscapeAt; + var window = _composerEscapeWindow; _composerEscapeAt = null; + _composerEscapeWindow = null; if (e.KeyInfo.Key != ConsoleKey.Enter || e.KeyInfo.Modifiers != 0 || at is not { } released || _time.GetUtcNow() - released > AltEnterWindow - || !_dockedComposers.TryGetValue(ActiveWindowId(), out var composer)) + || window is null || !_dockedComposers.TryGetValue(window, out var composer)) { return false; } + // The Esc took the keyboard back to the window before the composer; the Enter puts it in the composer again. e.Handled = true; + if (!string.Equals(ActiveWindowId(), window, StringComparison.Ordinal)) + { + Activate(window); + } + _composerReleased = false; SyncComposerArm(); composer.Editor.ProcessKey(new ConsoleKeyInfo('\r', ConsoleKey.Enter, false, false, false)); diff --git a/src/SharpMUTerm.Tui/SharpMUTermApp.cs b/src/SharpMUTerm.Tui/SharpMUTermApp.cs index 6142ce77..a1fd3641 100644 --- a/src/SharpMUTerm.Tui/SharpMUTermApp.cs +++ b/src/SharpMUTerm.Tui/SharpMUTermApp.cs @@ -14145,6 +14145,14 @@ private void CloseWindow(string id) } RebuildPaneArea(); + + // A docked composer's editor held the framework's focus and has gone with its pane: the keyboard goes back to the + // command line, or nothing would hold it until the next focus move. + if (_dockedComposers.ContainsKey(id)) + { + SyncComposerArm(); + } + if (closedPanel) { PersistConfiguration(); // the workspace without it, too, so a crash before quitting does not bring it back diff --git a/tests/SharpMUTerm.Avalonia.Tests/NativeComposerTests.cs b/tests/SharpMUTerm.Avalonia.Tests/NativeComposerTests.cs index 86a86633..16d14eae 100644 --- a/tests/SharpMUTerm.Avalonia.Tests/NativeComposerTests.cs +++ b/tests/SharpMUTerm.Avalonia.Tests/NativeComposerTests.cs @@ -414,6 +414,30 @@ public async Task TheDockedEditorLiesOverItsPaneAndTakesTheKeyboardWhenArmed() await Assert.That(width).IsGreaterThan(0); } + /// The docked heading has a close button (the pane draws no tab strip), and pressing it closes the composer. + [Test] + public async Task TheDockedHeadingsCloseButtonClosesTheComposer() + { + var (shows, before, after, composers) = await Run(rig => + { + var composer = Dock(rig); + var windowId = rig.Layer.Composers.Keys.Single(); + var shows = composer.ShowsClose; + var before = rig.App.PaneIdOf(windowId) is not null; + composer.PressClose(); + rig.App.RenderNextFrame(); + Drain(); + rig.Window.UpdateLayout(); + Drain(); + return (shows, before, rig.App.PaneIdOf(windowId) is not null, rig.Layer.Composers.Count); + }); + + await Assert.That(shows).IsTrue(); + await Assert.That(before).IsTrue(); + await Assert.That(after).IsFalse(); + await Assert.That(composers).IsEqualTo(0).Because("a closed composer's editor leaves the window"); + } + [Test] public async Task TypingInTheDockedEditorIsTheDraftAndCtrlSSendsIt() { diff --git a/tests/SharpMUTerm.Avalonia.Tests/SurfaceParityTests.cs b/tests/SharpMUTerm.Avalonia.Tests/SurfaceParityTests.cs index c06f1258..c83d15bc 100644 --- a/tests/SharpMUTerm.Avalonia.Tests/SurfaceParityTests.cs +++ b/tests/SharpMUTerm.Avalonia.Tests/SurfaceParityTests.cs @@ -791,6 +791,14 @@ await Same("status: notice action", "layout-combat", await Assert.That(native).IsEqualTo("hello there|True|FullScreen"); } + // The docked composer's close: the window's heading button and the cells' heading × close its pane alike. + { + var native = await DockedClosed(native: true); + var cells = await DockedClosed(native: false); + await Assert.That(native).IsEqualTo(cells).Because("composer: close"); + await Assert.That(native).Contains("closed=True"); + } + // The quit prompt: Y quits and N stays, from the window's buttons and from the cell prompt's keys. await SameModal("quit: yes", "quit", (app, m) => ((QuitSession)m.Open(SurfaceCatalog.Quit)!).Answer(true), @@ -1363,6 +1371,28 @@ await Same("scroll-band: jump to latest", "scrollback-split", return (telnet.Lines.ToList(), $"{Outcome(app)} | armed={app.ComposerArmed} | docked={docked.Body} verb={docked.Verb}"); } + /// A connected client docks its composer and closes it from the heading: the window's button, or the cells' ×. + private static async Task DockedClosed(bool native) + { + using var driver = new AvaloniaConsoleDriver(140, 36); + var (app, _) = await Connected(driver, native ? new FakeComposer() : null); + await using var __ = app; + app.DispatchCommand("panel:composer"); + app.RenderNextFrame(); + var windowId = TerminalPanels.WindowId(PanelIds.Composer, "Quiet.Ann"); + if (native) + { + driver.Pointer.Panels.Single(p => p.PanelId == PanelIds.Composer).ComposerSession!.ClosePanel(); + } + else + { + app.DockedComposerIn(windowId)!.SimulateHeaderLink(SharpMUTerm.Tui.Panels.DockedComposer.CloseLink); + } + + app.RenderNextFrame(); + return $"{Outcome(app)} | closed={app.PaneIdOf(windowId) is null} | armed={app.ComposerArmed}"; + } + /// A connected client docks its composer, types a draft and asks for the preview; the F1 it opens, read back. private static async Task DockedPreview(bool native) { diff --git a/tests/SharpMUTerm.Tui.Tests/DockedComposerTests.cs b/tests/SharpMUTerm.Tui.Tests/DockedComposerTests.cs index 15274901..1ceefb17 100644 --- a/tests/SharpMUTerm.Tui.Tests/DockedComposerTests.cs +++ b/tests/SharpMUTerm.Tui.Tests/DockedComposerTests.cs @@ -3,6 +3,7 @@ using SharpMUTerm.Core.Configuration; using SharpMUTerm.Core.Layouts; using SharpMUTerm.Graphics; +using SharpMUTerm.Tui.Panels; namespace SharpMUTerm.Tui.Tests; @@ -129,6 +130,140 @@ public async Task AltEnterInTheComposerIsANewLineAndSendsNothing() await Assert.That(world.App.DockedComposerIn(windowId)!.Body).IsEqualTo("one\ntwo"); } + /// + /// Roleplay's side composer, reached from the output pane's Chat tab: Esc hands the keyboard back to Chat — the tab + /// that was in front — not to the composer's own pane, whose command line is the character's main window's. + /// + [Test] + public async Task EscGoesBackToTheTabTheComposerWasReachedFrom() + { + var app = Roleplay(); + var chat = ReachTheComposerFromChat(app); + + app.SimulateKey(Key(ConsoleKey.Escape)); + app.RenderNextFrame(); + + await Assert.That(app.ComposerArmed).IsFalse(); + await Assert.That(app.ActiveWindowId()).IsEqualTo(chat); + await Assert.That(app.FocusedPaneId).IsEqualTo(app.PaneIdOf(chat)); + await Assert.That(app.ArmedBarHasFocus).IsTrue(); + } + + /// + /// A narrow client docks the composer as a tab of the output's pane. Esc there leaves it in front: going back to the + /// window before it would put that window over the composer and hide what was being written. + /// + [Test] + public async Task EscLeavesAComposerThatSharesItsPaneInFront() + { + var world = await Connected(80, 24); + var windowId = world.OpenComposer(); + var pane = world.App.PaneIdOf(windowId); + await Assert.That(world.App.PaneIds.Count).IsEqualTo(1).Because("the fixture's composer shares the output's pane"); + + world.App.SimulateKey(Key(ConsoleKey.Escape)); + + await Assert.That(world.App.ComposerArmed).IsFalse(); + await Assert.That(world.App.PaneActiveTab(pane!)).IsEqualTo(windowId); + } + + /// ⌥⏎ is Esc then Enter: the Esc took the keyboard back to Chat, and the Enter puts it in the composer again. + [Test] + public async Task AltEnterStillInsertsALineAfterEscLeftForAnotherTab() + { + var app = Roleplay(); + ReachTheComposerFromChat(app); + var composer = app.ActiveWindowId(); + app.SimulateComposerTyping("one"); + + app.SimulateKey(Key(ConsoleKey.Escape)); + app.SimulateKey(Enter); + app.SimulateComposerTyping("two"); + + await Assert.That(app.ComposerArmed).IsTrue(); + await Assert.That(app.ActiveWindowId()).IsEqualTo(composer); + await Assert.That(app.DockedComposerIn(composer)!.Body).EndsWith("one\ntwo"); + } + + /// + /// A pane holding the composer alone draws no tab strip, so its heading carries a ×: a click closes the pane, + /// records it closed, and keeps the draft for the next composer. + /// + [Test] + public async Task TheHeadingsCloseLinkClosesTheComposerAndKeepsTheDraft() + { + var world = await Connected(); + var windowId = world.OpenComposer(); + world.App.SimulateComposerTyping("half a post"); + var composer = world.App.DockedComposerIn(windowId)!; + await Assert.That(composer.HeaderMarkup).Contains($"[link={DockedComposer.CloseLink}]×[/]"); + await Assert.That(world.App.RenderWholeFrame()).Contains("×"); + + composer.SimulateHeaderLink(DockedComposer.CloseLink); + world.App.RenderNextFrame(); + + await Assert.That(world.App.PaneIdOf(windowId)).IsNull(); + await Assert.That(world.App.ComposerArmed).IsFalse(); + await Assert.That(world.App.ArmedBarHasFocus).IsTrue(); + + world.OpenComposer(); + await Assert.That(world.App.DockedComposerIn(windowId)!.Body).IsEqualTo("half a post"); + } + + /// ⌃W from inside the composer closes it too, and the keyboard lands on the command line. + [Test] + public async Task CtrlWInTheComposerClosesIt() + { + var world = await Connected(); + var windowId = world.OpenComposer(); + await Assert.That(world.App.ComposerArmed).IsTrue(); + + world.App.SimulateKey(Key(ConsoleKey.W, ConsoleModifiers.Control)); + world.App.RenderNextFrame(); + + await Assert.That(world.App.PaneIdOf(windowId)).IsNull(); + await Assert.That(world.App.ArmedBarHasFocus).IsTrue(); + } + + /// Every width the heading is drawn at, the × is its last cell but one and the row is never wider. + [Test] + public async Task TheCloseLinkKeepsItsCellAtEveryWidth() + { + var ink = new HeadingBand.Ink("#222222", "#dddddd", "#00f5b7"); + foreach (var width in new[] { 5, 8, 20, 40, 80 }) + { + var row = HeadingBand.Row("COMPOSE", " as typed", "12 chars", width, ink, close: "×"); + var text = MarkupText.Plain(row); + await Assert.That(text.Length).IsEqualTo(width).Because($"{width} cells"); + await Assert.That(text[^2]).IsEqualTo('×').Because($"{width} cells"); + } + } + + /// The demo under the Roleplay layout: the output pane holds Corvid and Chat, the composer is beside it. + private static SharpMUTermApp Roleplay() + { + Console.SetIn(TextReader.Null); + var app = new SharpMUTermApp(DemoScene.Build(), Headless, new HeadlessConsoleDriver(Width, Height)); + app.RenderSnapshot("layout-roleplay"); + app.RenderNextFrame(); + return app; + } + + /// Brings Chat to the front of the output pane, then ⌃→ into the composer. Returns Chat's window id. + private static string ReachTheComposerFromChat(SharpMUTermApp app) + { + app.SimulateKey(Key(ConsoleKey.N, ConsoleModifiers.Control)); + var chat = app.ActiveWindowId(); + app.SimulateKey(Key(ConsoleKey.RightArrow, ConsoleModifiers.Control)); + app.RenderNextFrame(); + if (!app.ComposerArmed || chat.StartsWith("panel:", StringComparison.Ordinal)) + { + throw new InvalidOperationException($"the fixture did not reach the composer from a tab ({chat})"); + } + + return chat; + } + [Test] public async Task MovingToAnotherPaneDisarmsItAndComingBackArmsIt() { @@ -234,7 +369,7 @@ internal string OpenComposer() } } - private static async Task Connected() + private static async Task Connected(int width = Width, int height = Height) { Console.SetIn(TextReader.Null); var config = new AppConfiguration(); @@ -242,7 +377,7 @@ private static async Task Connected() world.Characters.Add(new CharacterDefinition { Name = "Ann", Logging = new LoggingSettings() }); config.Worlds.Add(world); - var app = new SharpMUTermApp(config, Headless, new HeadlessConsoleDriver(Width, Height)); + var app = new SharpMUTermApp(config, Headless, new HeadlessConsoleDriver(width, height)); var telnet = new RecordingTelnetSession(); app.TelnetFactory = _ => telnet; await app.BindWorldWithoutConnecting(world).ConnectAsync(); diff --git a/tests/SharpMUTerm.Tui.Tests/NativeComposerRoutingTests.cs b/tests/SharpMUTerm.Tui.Tests/NativeComposerRoutingTests.cs index 85f15c9b..16808617 100644 --- a/tests/SharpMUTerm.Tui.Tests/NativeComposerRoutingTests.cs +++ b/tests/SharpMUTerm.Tui.Tests/NativeComposerRoutingTests.cs @@ -418,6 +418,57 @@ public async Task EscFromTheHostsEditorReleasesAndAClickArmsAgain() await Assert.That(session.Model.Armed).IsTrue(); } + /// + /// The window's side composer (Roleplay), reached from the output pane's Chat tab: Esc in the host's editor hands the + /// keyboard back to Chat, as the cells' Esc does — not to the composer's pane, whose command line is the main window's. + /// + [Test] + public async Task EscFromTheHostsEditorGoesBackToTheTabItWasReachedFrom() + { + Console.SetIn(TextReader.Null); + var host = ClientHost.ForHarness(Headless, new HeadlessConsoleDriver(120, 34), null, null, null) with + { + NativeComposer = new FakeComposer(), + Pointer = new PanelPresenter(), + }; + await using var app = new SharpMUTermApp(DemoScene.Build(), host); + app.RenderSnapshot("layout-roleplay"); + app.RenderNextFrame(); + app.SimulateKey(Key(ConsoleKey.N, ConsoleModifiers.Control)); + var chat = app.ActiveWindowId(); + app.SimulateKey(Key(ConsoleKey.RightArrow, ConsoleModifiers.Control)); + app.RenderNextFrame(); + var composer = app.ActiveWindowId(); + await Assert.That(app.ComposerArmed).IsTrue(); + await Assert.That(chat).DoesNotStartWith("panel:"); + + app.DockedComposerSession(composer)!.Release(); + + await Assert.That(app.ComposerArmed).IsFalse(); + await Assert.That(app.ActiveWindowId()).IsEqualTo(chat); + await Assert.That(app.FocusedPaneId).IsEqualTo(app.PaneIdOf(chat)); + } + + /// The host's close button closes the composer's pane, as the cells' heading × does, and keeps the draft. + [Test] + public async Task TheHostsCloseButtonClosesTheDockedComposer() + { + var world = await Connected(); + await using var _ = world.App; + var windowId = OpenDocked(world); + var session = world.App.DockedComposerSession(windowId)!; + session.Edited("half a post"); + + session.ClosePanel(); + world.App.RenderNextFrame(); + + await Assert.That(world.App.PaneIdOf(windowId)).IsNull(); + await Assert.That(world.App.ArmedBarHasFocus).IsTrue(); + await Assert.That(DockedPanel(world, windowId)).IsNull().Because("a closed composer is no longer published"); + world.App.DispatchCommand("panel:composer"); + await Assert.That(world.App.DockedComposerIn(windowId)!.Body).IsEqualTo("half a post"); + } + [Test] public async Task TheDockedEditorIsNotPublishedWhileF1IsOpenNatively() {