From 8dd31867d086109252de50801360e0ed7c759a24 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Nov 2026 22:19:45 +0000 Subject: [PATCH] Floating bar: the structures of the selection as the mini toolbar of Word [no-new-version] The bar of the structures opened for every selection and stayed under the equation, over what is under the editor (the buttons of the demo). In MathEditorView, for every toolkit: it opens only for a selection made with the pointer, when the button is released (not for Shift+arrows nor Ctrl+A), and closes when the pointer goes away from the selection, leaves the control elsewhere than towards the bar, or leaves the bar (PointerExited, QuickBarPointerExited). The completions and the commands of tables are as they were. On Windows Forms the bar opens over the equation where there is no room under it on the screen, as the popups of WPF and Avalonia do. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01SyVioB8cEKzWWZ7KQGzLfe --- docs/EDITOR.md | 6 +- src/SharpEquations.Avalonia/MathEditBox.cs | 6 ++ .../MathEditorView.cs | 81 ++++++++++++++++-- .../PublicAPI.Unshipped.txt | 2 + src/SharpEquations.Uno/MathEditBox.cs | 6 ++ src/SharpEquations.WinForms/MathEditBox.cs | 19 ++++- src/SharpEquations.Wpf/MathEditBox.cs | 6 ++ .../Rendering/EditorViewTests.cs | 84 +++++++++++++++++++ .../SharpEquations.Uno.Tests/EditBoxTests.cs | 9 +- .../EditBoxTests.cs | 9 +- .../SharpEquations.Wpf.Tests/EditBoxTests.cs | 9 +- 11 files changed, 226 insertions(+), 11 deletions(-) diff --git a/docs/EDITOR.md b/docs/EDITOR.md index 0a47e6b..0808fd8 100644 --- a/docs/EDITOR.md +++ b/docs/EDITOR.md @@ -80,7 +80,11 @@ The behaviour is the one of the equations of Word and of `RichEditBox` in math m (fraction, root, scripts, brackets, bar, box, build down), the commands of the table of the caret, and quick symbols (`QuickSymbols`) in an empty slot. Its buttons do not take the focus. What it shows and the completion chosen are `MathQuickBarState` of Editing (`MathEditor.GetQuickBarMode`, `DismissCompletions`), so that a control of another - toolkit only places and draws it. + toolkit only places and draws it. The structures of the selection come as the mini toolbar of Word: only for a + selection made with the pointer (not Shift+arrows nor Ctrl+A), when the button is released, and they close when the + pointer goes away from the selection, leaves the control elsewhere than towards the bar, or leaves the bar + (`MathEditorView.PointerMoved`, `PointerExited`, `QuickBarPointerExited`), so that the bar does not stay over what + is under the editor. Where there is no room under the equation on the screen, the bar opens over it. - **Structures from code**: `InsertStructure("mfrac")`, `InsertFences`, `InsertMatrix(2, 2, "(", ")")`, `InsertTemplate(node)`, with the selection in the first slot and the caret in the first empty one, as the buttons of the equation tools of Word; Tab and Shift+Tab move through the cells of a table. diff --git a/src/SharpEquations.Avalonia/MathEditBox.cs b/src/SharpEquations.Avalonia/MathEditBox.cs index a94c8e6..c2946f4 100644 --- a/src/SharpEquations.Avalonia/MathEditBox.cs +++ b/src/SharpEquations.Avalonia/MathEditBox.cs @@ -143,6 +143,12 @@ public MathEditBox() IsLightDismissEnabled = false, Focusable = false, }; + QuickBar.PointerExited += (_, _) => View.QuickBarPointerExited(); + PointerExited += (_, e) => + { + var point = e.GetPosition(this); + View.PointerExited(point.X, point.Y); + }; LogicalChildren.Add(popup); VisualChildren.Add(popup); caretLayer = new CaretLayer(this); diff --git a/src/SharpEquations.Rendering/MathEditorView.cs b/src/SharpEquations.Rendering/MathEditorView.cs index 4448ce7..a0eed48 100644 --- a/src/SharpEquations.Rendering/MathEditorView.cs +++ b/src/SharpEquations.Rendering/MathEditorView.cs @@ -131,6 +131,9 @@ public sealed class MathEditorView private (MathNode Root, IMathFont Font, double FontSize, double? LineWidth)? laidOut; private MathSelection? drawnSelection; private bool selecting; + + // the selection made with the pointer that the floating bar shows its structures for, until the pointer goes away + private MathSelection? pointerSelection; private bool isFocused; /// The view of an editor. @@ -305,26 +308,62 @@ public bool IsInSelection(double x, double y) /// public void PointerPressed(double x, double y, bool extend) { + pointerSelection = null; + selecting = true; if (HitTest(x, y) is { } position) Editor.SetCaret(position, extend); - selecting = true; } - /// The pointer moved to a point: while the button is held, the selection extends to it. + /// + /// The pointer moved to a point: while the button is held, the selection extends to it; otherwise, far from the + /// selection, the floating bar of its structures closes, as the mini toolbar of Word fades away. + /// public void PointerMoved(double x, double y) { - if (selecting && HitTest(x, y) is { } position) - Editor.SetCaret(position, true); + if (selecting) + { + if (HitTest(x, y) is { } position) + Editor.SetCaret(position, true); + return; + } + + if (IsSelectionBarOpen && !NearSelectionBar(x, y, QuickBarReach)) + DismissSelectionBar(); } - /// The button released; returns whether it was selecting, and the control releases the pointer. + /// + /// The button released; returns whether it was selecting, and the control releases the pointer. A selection made + /// with the pointer opens the floating bar of its structures. + /// public bool PointerReleased() { var was = selecting; selecting = false; + if (was && Editor.Selection is { } selection) + { + pointerSelection = selection; + Changed?.Invoke(this, new MathEditorViewChangedEventArgs(MathEditorViewChanges.QuickBar)); + } return was; } + /// + /// The pointer left the control at a point (the last it moved to): the floating bar of the structures of the + /// selection closes, unless the pointer is going to it, at the edge of the control next to it. + /// + public void PointerExited(double x, double y) + { + if (!selecting && IsSelectionBarOpen && !NearSelectionBar(x, y, 0)) + DismissSelectionBar(); + } + + /// The pointer left the floating bar: the bar of the structures of the selection closes, as the pointer goes away from it. + public void QuickBarPointerExited() + { + if (IsSelectionBarOpen) + DismissSelectionBar(); + } + /// /// Acts on a key: first the floating bar, while it is open (: the /// control then updates it), then the editor (). The control gives first the @@ -350,6 +389,14 @@ public bool UpdateQuickBar() QuickBarAnchor = null; if (QuickBar.Mode == MathQuickBarMode.Hidden || Layout is not { } layout) return changed; + + // the structures of the selection only for a selection made with the pointer, as the mini toolbar of Word: + // not for Shift+arrows or Ctrl+A, and not once the pointer went away + if (QuickBar.Mode == MathQuickBarMode.Selection && !SameSelection(pointerSelection, Editor.Selection)) + { + pointerSelection = null; + return changed; + } var rects = Editor.Selection is { } selection ? layout.GetSelectionRects(selection) : Array.Empty(); var anchor = rects.Count > 0 && rects[0].Width > 0 ? rects[0] : layout.GetCaretRect(Editor.Caret) ?? default; QuickBarAnchor = new MathRect(anchor.X + ContentX, ContentY, Math.Max(1, anchor.Width), Math.Max(layout.Height, anchor.Y + anchor.Height) + Padding.Bottom); @@ -549,6 +596,30 @@ private void OnEditorChanged(object? sender, EventArgs e) Changed?.Invoke(this, new MathEditorViewChangedEventArgs(changes)); } + /// How far from the selection, in device-independent pixels, the pointer goes before the floating bar of its structures closes. + private double QuickBarReach => Math.Max(48, FontSize * 2); + + private bool IsSelectionBarOpen => QuickBar.Mode == MathQuickBarMode.Selection && QuickBarAnchor != null; + + /// + /// Whether a point is within a distance of the place of the floating bar: the selection, the bar under it (or + /// over it, where the control puts it there for want of room), which is wider than the selection. + /// + private bool NearSelectionBar(double x, double y, double distance) + { + if (QuickBarAnchor is not { } anchor) + return false; + var reach = QuickBarReach; + return x >= anchor.X - reach - distance && x <= anchor.Right + reach * 8 + distance + && y >= anchor.Y - reach - distance && y <= anchor.Bottom + reach + distance; + } + + private void DismissSelectionBar() + { + pointerSelection = null; + Changed?.Invoke(this, new MathEditorViewChangedEventArgs(MathEditorViewChanges.QuickBar)); + } + private static bool SameSelection(MathSelection? a, MathSelection? b) => a == null ? b == null : b != null && a.StartPosition == b.StartPosition && a.EndPosition == b.EndPosition; } diff --git a/src/SharpEquations.Rendering/PublicAPI.Unshipped.txt b/src/SharpEquations.Rendering/PublicAPI.Unshipped.txt index 203cac2..854c30d 100644 --- a/src/SharpEquations.Rendering/PublicAPI.Unshipped.txt +++ b/src/SharpEquations.Rendering/PublicAPI.Unshipped.txt @@ -302,3 +302,5 @@ static SharpEquations.Rendering.OpenType.MathTable.Parse(byte[]! data) -> SharpE static SharpEquations.Rendering.OpenType.ScriptStyles.Parse(byte[]? gsub) -> SharpEquations.Rendering.OpenType.ScriptStyles? virtual SharpEquations.Rendering.OpenTypeMathFont.GetFallback(int codePoint) -> SharpEquations.Rendering.IMathFont? SharpEquations.Rendering.OpenType.FontFile.GlyphCount.get -> int +SharpEquations.Rendering.MathEditorView.PointerExited(double x, double y) -> void +SharpEquations.Rendering.MathEditorView.QuickBarPointerExited() -> void diff --git a/src/SharpEquations.Uno/MathEditBox.cs b/src/SharpEquations.Uno/MathEditBox.cs index 85a51d2..1838761 100644 --- a/src/SharpEquations.Uno/MathEditBox.cs +++ b/src/SharpEquations.Uno/MathEditBox.cs @@ -189,6 +189,12 @@ public MathEditBox() PointerPressed += OnPointerPressed; PointerMoved += OnPointerMoved; PointerReleased += OnPointerReleased; + PointerExited += (_, e) => + { + var point = e.GetCurrentPoint(this).Position; + View.PointerExited(point.X, point.Y); + }; + quickBar.PointerExited += (_, _) => View.QuickBarPointerExited(); RightTapped += (_, e) => { var at = e.GetPosition(this); diff --git a/src/SharpEquations.WinForms/MathEditBox.cs b/src/SharpEquations.WinForms/MathEditBox.cs index 20842ae..07589ed 100644 --- a/src/SharpEquations.WinForms/MathEditBox.cs +++ b/src/SharpEquations.WinForms/MathEditBox.cs @@ -77,6 +77,11 @@ public MathEditBox() Padding = new Padding(6, 4, 6, 4); base.AutoSize = true; Size = new Size(200, 40); + MouseLeave += (_, _) => + { + var point = PointToClient(MousePosition); + View.PointerExited(point.X / DpiScale, point.Y / DpiScale); + }; } /// @@ -999,14 +1004,24 @@ private void UpdateQuickBar() quickBar?.Hide(); return; } - var bar = quickBar ??= new QuickBar(); + if (quickBar == null) + { + quickBar = new QuickBar(); + quickBar.MouseLeave += (_, _) => View.QuickBarPointerExited(); + } + var bar = quickBar; SetQuickBarOwner(form); var completions = View.QuickBar.Mode == MathQuickBarMode.Completions; if (refill || bar.Count == 0) bar.Fill(View.QuickBar.GetItems(QuickSymbols), completions, completions ? 130 : View.QuickBar.Mode == MathQuickBarMode.Symbols ? 26 : 36); bar.Highlight(completions ? View.QuickBar.SelectedIndex : -1); var scale = DpiScale; - bar.Location = PointToScreen(new Point((int)(anchor.X * scale), (int)Math.Ceiling(anchor.Bottom * scale))); + var below = PointToScreen(new Point((int)(anchor.X * scale), (int)Math.Ceiling(anchor.Bottom * scale))); + // over the equation where there is no room for it under it on the screen, as the popups of WPF and Avalonia + var screen = Screen.FromControl(this).WorkingArea; + bar.Location = below.Y + bar.Height > screen.Bottom + ? PointToScreen(new Point((int)(anchor.X * scale), (int)Math.Floor(anchor.Y * scale) - bar.Height)) + : below; if (!bar.Visible) bar.Show(form); } diff --git a/src/SharpEquations.Wpf/MathEditBox.cs b/src/SharpEquations.Wpf/MathEditBox.cs index 841dfe4..0722c32 100644 --- a/src/SharpEquations.Wpf/MathEditBox.cs +++ b/src/SharpEquations.Wpf/MathEditBox.cs @@ -134,6 +134,12 @@ public MathEditBox() // the bar is the user interface, not the equation: the font of the system, not the size of the editor's, which // the popup would inherit from it + quickBar.MouseLeave += (_, _) => View.QuickBarPointerExited(); + MouseLeave += (_, e) => + { + var point = e.GetPosition(this); + View.PointerExited(point.X, point.Y); + }; quickBar.SetValue(TextElement.FontSizeProperty, SystemFonts.MessageFontSize); quickBar.SetValue(TextElement.FontFamilyProperty, SystemFonts.MessageFontFamily); diff --git a/tests/SharpEquations.Avalonia.Tests/Rendering/EditorViewTests.cs b/tests/SharpEquations.Avalonia.Tests/Rendering/EditorViewTests.cs index 3f9f514..2ea8a46 100644 --- a/tests/SharpEquations.Avalonia.Tests/Rendering/EditorViewTests.cs +++ b/tests/SharpEquations.Avalonia.Tests/Rendering/EditorViewTests.cs @@ -39,6 +39,90 @@ public async Task FontReadWithoutAToolkit() await Assert.That(Libertinus.Value.GetFallback('x')).IsNull(); } + /// Selects the whole equation with the pointer, from its start to its end, as a drag does. + private static void DragOverAll(MathEditorView view) + { + view.Editor.MoveHome(); + var from = view.GetCaretRect()!.Value; + view.Editor.MoveEnd(); + var to = view.GetCaretRect()!.Value; + view.PointerPressed(from.X, from.Y + from.Height / 2, false); + view.PointerMoved(to.X, to.Y + to.Height / 2); + view.PointerReleased(); + } + + private static MathEditorView Focused(string unicodeMath) + { + var view = View(unicodeMath); + view.IsFocused = true; + return view; + } + + /// The bar of the structures of a selection opens for a selection made with the pointer, as the mini toolbar of Word. + [Test] + public async Task SelectionBarForAPointerSelection() + { + var view = Focused("a+b"); + DragOverAll(view); + view.UpdateQuickBar(); + await Assert.That(view.QuickBar.Mode).IsEqualTo(MathQuickBarMode.Selection); + await Assert.That(view.QuickBarAnchor).IsNotNull(); + } + + /// A selection made with the keyboard (Shift+arrows, Ctrl+A) opens no bar: it would cover what is under the editor. + [Test] + public async Task NoSelectionBarForAKeyboardSelection() + { + var view = Focused("a+b"); + view.Editor.SelectAll(); + view.UpdateQuickBar(); + var afterSelectAll = view.QuickBarAnchor; + DragOverAll(view); + view.UpdateQuickBar(); + view.Editor.ProcessKey(MathKey.Left, MathKeyModifiers.Shift, view.Layout, false); + view.UpdateQuickBar(); + await Assert.That(view.QuickBar.Mode).IsEqualTo(MathQuickBarMode.Selection); + await Assert.That(afterSelectAll).IsNull(); + await Assert.That(view.QuickBarAnchor).IsNull(); + } + + /// The bar of the structures closes when the pointer goes away from the selection, or leaves the bar. + [Test] + public async Task SelectionBarClosesWhenThePointerGoesAway() + { + var near = Focused("a+b"); + DragOverAll(near); + near.UpdateQuickBar(); + var anchor = near.QuickBarAnchor!.Value; + near.PointerMoved(anchor.X, anchor.Bottom + 10); + near.UpdateQuickBar(); + var stays = near.QuickBarAnchor != null; + near.PointerMoved(anchor.X, anchor.Bottom + 500); + near.UpdateQuickBar(); + var far = near.QuickBarAnchor; + + var exited = Focused("a+b"); + DragOverAll(exited); + exited.UpdateQuickBar(); + exited.PointerExited(exited.QuickBarAnchor!.Value.X, exited.QuickBarAnchor!.Value.Bottom); + exited.UpdateQuickBar(); + var towardsTheBar = exited.QuickBarAnchor != null; + exited.QuickBarPointerExited(); + exited.UpdateQuickBar(); + + var away = Focused("a+b"); + DragOverAll(away); + away.UpdateQuickBar(); + away.PointerExited(-500, 0); + away.UpdateQuickBar(); + + await Assert.That(stays).IsTrue(); + await Assert.That(far).IsNull(); + await Assert.That(towardsTheBar).IsTrue(); + await Assert.That(exited.QuickBarAnchor).IsNull(); + await Assert.That(away.QuickBarAnchor).IsNull(); + } + [Test] public async Task MeasureAndPlaceInTheMiddle() { diff --git a/tests/SharpEquations.Uno.Tests/EditBoxTests.cs b/tests/SharpEquations.Uno.Tests/EditBoxTests.cs index e58e36a..8b0aed6 100644 --- a/tests/SharpEquations.Uno.Tests/EditBoxTests.cs +++ b/tests/SharpEquations.Uno.Tests/EditBoxTests.cs @@ -179,7 +179,14 @@ public async Task StructuresForTheSelection() var (open, buttons, equation) = await Ui.Run(async () => { var editor = await Show("a+b"); - editor.Editor.SelectAll(); + // a selection made with the pointer, from the start to the end: the bar of its structures opens + editor.Editor.MoveHome(); + var from = editor.View.GetCaretRect()!.Value; + editor.Editor.MoveEnd(); + var to = editor.View.GetCaretRect()!.Value; + editor.PressAt(from.X, from.Y + from.Height / 2, false); + editor.View.PointerMoved(to.X, to.Y + to.Height / 2); + editor.View.PointerReleased(); await Ui.Settle(); var wasOpen = editor.IsQuickBarOpen; var count = editor.QuickBarPanel.Children.Count; diff --git a/tests/SharpEquations.WinForms.Tests/EditBoxTests.cs b/tests/SharpEquations.WinForms.Tests/EditBoxTests.cs index 46a68dd..166d27c 100644 --- a/tests/SharpEquations.WinForms.Tests/EditBoxTests.cs +++ b/tests/SharpEquations.WinForms.Tests/EditBoxTests.cs @@ -243,7 +243,14 @@ public async Task StructuresForTheSelection() var (open, items, equation) = await Ui.Run(async () => { var editor = await Show("a+b"); - editor.Editor.SelectAll(); + // a selection made with the pointer, from the start to the end: the bar of its structures opens + editor.Editor.MoveHome(); + var from = editor.View.GetCaretRect()!.Value; + editor.Editor.MoveEnd(); + var to = editor.View.GetCaretRect()!.Value; + editor.PressAt(from.X, from.Y + from.Height / 2, false); + editor.View.PointerMoved(to.X, to.Y + to.Height / 2); + editor.View.PointerReleased(); await Ui.Settle(); var wasOpen = editor.IsQuickBarOpen; var count = editor.QuickBarWindow?.Count ?? 0; diff --git a/tests/SharpEquations.Wpf.Tests/EditBoxTests.cs b/tests/SharpEquations.Wpf.Tests/EditBoxTests.cs index 9261fea..2f243d7 100644 --- a/tests/SharpEquations.Wpf.Tests/EditBoxTests.cs +++ b/tests/SharpEquations.Wpf.Tests/EditBoxTests.cs @@ -194,7 +194,14 @@ public async Task StructuresForTheSelection() var (open, buttons, equation) = await Ui.Run(async () => { var editor = await Show("a+b"); - editor.Editor.SelectAll(); + // a selection made with the pointer, from the start to the end: the bar of its structures opens + editor.Editor.MoveHome(); + var from = editor.View.GetCaretRect()!.Value; + editor.Editor.MoveEnd(); + var to = editor.View.GetCaretRect()!.Value; + editor.PressAt(from.X, from.Y + from.Height / 2, false); + editor.View.PointerMoved(to.X, to.Y + to.Height / 2); + editor.View.PointerReleased(); await Ui.Settle(); var wasOpen = editor.IsQuickBarOpen; var count = editor.QuickBarPanel.Children.Count;