Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion docs/EDITOR.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 6 additions & 0 deletions src/SharpEquations.Avalonia/MathEditBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
81 changes: 76 additions & 5 deletions src/SharpEquations.Rendering/MathEditorView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/// <summary>The view of an editor.</summary>
Expand Down Expand Up @@ -305,26 +308,62 @@ public bool IsInSelection(double x, double y)
/// </summary>
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;
}

/// <summary>The pointer moved to a point: while the button is held, the selection extends to it.</summary>
/// <summary>
/// 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.
/// </summary>
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();
}

/// <summary>The button released; returns whether it was selecting, and the control releases the pointer.</summary>
/// <summary>
/// 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.
/// </summary>
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;
}

/// <summary>
/// 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.
/// </summary>
public void PointerExited(double x, double y)
{
if (!selecting && IsSelectionBarOpen && !NearSelectionBar(x, y, 0))
DismissSelectionBar();
}

/// <summary>The pointer left the floating bar: the bar of the structures of the selection closes, as the pointer goes away from it.</summary>
public void QuickBarPointerExited()
{
if (IsSelectionBarOpen)
DismissSelectionBar();
}

/// <summary>
/// Acts on a key: first the floating bar, while it is open (<see cref="MathQuickBarState.ProcessKey"/>: the
/// control then updates it), then the editor (<see cref="MathEditor.ProcessKey"/>). The control gives first the
Expand All @@ -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<MathRect>();
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);
Expand Down Expand Up @@ -549,6 +596,30 @@ private void OnEditorChanged(object? sender, EventArgs e)
Changed?.Invoke(this, new MathEditorViewChangedEventArgs(changes));
}

/// <summary>How far from the selection, in device-independent pixels, the pointer goes before the floating bar of its structures closes.</summary>
private double QuickBarReach => Math.Max(48, FontSize * 2);

private bool IsSelectionBarOpen => QuickBar.Mode == MathQuickBarMode.Selection && QuickBarAnchor != null;

/// <summary>
/// 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.
/// </summary>
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;
}
Expand Down
2 changes: 2 additions & 0 deletions src/SharpEquations.Rendering/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 6 additions & 0 deletions src/SharpEquations.Uno/MathEditBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
19 changes: 17 additions & 2 deletions src/SharpEquations.WinForms/MathEditBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
}

/// <summary>
Expand Down Expand Up @@ -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);
}
Expand Down
6 changes: 6 additions & 0 deletions src/SharpEquations.Wpf/MathEditBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
84 changes: 84 additions & 0 deletions tests/SharpEquations.Avalonia.Tests/Rendering/EditorViewTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,90 @@ public async Task FontReadWithoutAToolkit()
await Assert.That(Libertinus.Value.GetFallback('x')).IsNull();
}

/// <summary>Selects the whole equation with the pointer, from its start to its end, as a drag does.</summary>
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;
}

/// <summary>The bar of the structures of a selection opens for a selection made with the pointer, as the mini toolbar of Word.</summary>
[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();
}

/// <summary>A selection made with the keyboard (Shift+arrows, Ctrl+A) opens no bar: it would cover what is under the editor.</summary>
[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();
}

/// <summary>The bar of the structures closes when the pointer goes away from the selection, or leaves the bar.</summary>
[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()
{
Expand Down
9 changes: 8 additions & 1 deletion tests/SharpEquations.Uno.Tests/EditBoxTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 8 additions & 1 deletion tests/SharpEquations.WinForms.Tests/EditBoxTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 8 additions & 1 deletion tests/SharpEquations.Wpf.Tests/EditBoxTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading