Skip to content

Commit 892eaec

Browse files
SymboLinkercharlesroddieFoggyFinder
authored
Add placeholder blinks setting (#177)
* Add setting LaTeXSettings.PlaceholderBlinks + unit tests * Fix PlaceholderBlinks setting + add unit test CaretStillBlinks * Non-blinking placeholder test "CaretStillBlinks" should also verify that the RestingNucleus is shown for all placeholders if the caret blinks * Refactor: if PlaceholderBlinks is false and ShownThroughPlaceholder true then don't change the CaretState instead of ignoring the changed state later on * Remove MathKeyboardCaretState.ShownThroughPlaceholder and avoid invoking RedrawRequested when state has not changed * Remove nuget.config * add xml * Set PlaceholderBlinks default to false * Don't show cursor if at placeholder * ternary conditional operator * fix ProcessCaretState * Refactor away ProcessCaretState() + fix indentation Also: - Set PlaceholderBlinks back to true because I don't like to do a commit that has failing unit tests. A commit that changes a default, should also change the tests. I will have a look at the changes needed. * change LaTeXSettings.PlaceholderBlinks again and remove tests checking for default blinking behaviour. Remove nuget.Config * Restore parts of some tests + mark CaretIsOverriddenByPlaceholder as "fix or delete" * Replace CaretState enum property by boolean properties "InsertionPositionHighlighted" and "ShouldDrawCaret" General notes: Having a CaretState that says "MathKeyboardCaretState.Shown" while actually no caret is shown because a placeholder is shown is wrong. Having a CaretState "MathKeyboardCaretState.Hidden" and "MathKeyboardCaretState.TemporarilyHidden" is not needed: you can use StopBlinking() just after setting the CaretState you want to keep until the next key press. These two observations resulted in the boolean properties "InsertionPositionHighlighted" (that makes sense for both the caret AND the placeholder appearance) and "ShouldDrawCaret". Because Drawing the caret is done in CSharpMath.Rendering.FrontEnd, the unit tests of CSharpMath.Editor can only test "ShouldDraw" and unit tests that do that can cover the same as before (when it was tested via a MathKeyboardCaretState enum). Notes about moved unit tests: - CaretIsOverriddenByPlaceholder has been replaced by PlaceholderDoesNotBlinkAndNoCaretVisible. - CaretMovesWithPlaceholder has been replaced by NonBlinkingActivePlaceholderMoves. * Make mergable without conflict (after #179 for IDisposable MathKeyboard) * Move method before first using * Fix inconsistent code style * Reverse assertion order * Reverse assertion order - part 2 Co-authored-by: Charles Roddie <charles.roddie@mathspire.com> Co-authored-by: FoggyFinder <FoggyFinder@yandex.ua>
1 parent 476beb0 commit 892eaec

File tree

6 files changed

+162
-139
lines changed

6 files changed

+162
-139
lines changed

CSharpMath.Editor.Tests/CaretTests.cs

Lines changed: 133 additions & 106 deletions
Large diffs are not rendered by default.

CSharpMath.Editor/MathKeyboard.cs

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,6 @@ namespace CSharpMath.Editor {
99
using Structures;
1010
using Atoms = Atom.Atoms;
1111

12-
public enum MathKeyboardCaretState : byte {
13-
Hidden,
14-
TemporarilyHidden,
15-
ShownThroughPlaceholder,
16-
Shown
17-
}
1812
public class MathKeyboard<TFont, TGlyph> : IDisposable where TFont : IFont<TGlyph> {
1913
protected Timer blinkTimer;
2014
public const double DefaultBlinkMilliseconds = 800;
@@ -23,21 +17,14 @@ public MathKeyboard(TypesettingContext<TFont, TGlyph> context, TFont font, doubl
2317
Font = font;
2418
blinkTimer = new Timer(blinkMilliseconds);
2519
blinkTimer.Elapsed += (sender, e) => {
26-
switch (CaretState) {
27-
case MathKeyboardCaretState.Shown:
28-
case MathKeyboardCaretState.ShownThroughPlaceholder:
29-
CaretState = MathKeyboardCaretState.TemporarilyHidden;
30-
break;
31-
case MathKeyboardCaretState.TemporarilyHidden:
32-
CaretState = MathKeyboardCaretState.Shown;
33-
break;
34-
}
20+
if (!(MathList.AtomAt(_insertionIndex) is Atoms.Placeholder) || LaTeXSettings.PlaceholderBlinks)
21+
InsertionPositionHighlighted = !InsertionPositionHighlighted;
3522
};
3623
blinkTimer.Start();
3724
}
25+
public bool ShouldDrawCaret => InsertionPositionHighlighted && !(MathList.AtomAt(_insertionIndex) is Atoms.Placeholder);
3826
public void StartBlinking() => blinkTimer.Start();
3927
public void StopBlinking() => blinkTimer.Stop();
40-
//private readonly List<MathListIndex> highlighted;
4128
protected TypesettingContext<TFont, TGlyph> Context { get; }
4229
static void ResetPlaceholders(MathList mathList) {
4330
foreach (var mathAtom in mathList.Atoms) {
@@ -55,19 +42,19 @@ static void ResetPlaceholders(MathList mathList) {
5542
}
5643
}
5744
}
58-
MathKeyboardCaretState _caretState;
59-
public MathKeyboardCaretState CaretState {
60-
get => _caretState;
45+
bool _insertionPositionHighlighted;
46+
public bool InsertionPositionHighlighted {
47+
get => _insertionPositionHighlighted;
6148
set {
6249
blinkTimer.Stop();
6350
blinkTimer.Start();
64-
if (value != MathKeyboardCaretState.Hidden &&
65-
MathList.AtomAt(_insertionIndex) is Atoms.Placeholder placeholder)
66-
(placeholder.Nucleus, placeholder.Color, _caretState) =
67-
value == MathKeyboardCaretState.TemporarilyHidden
68-
? (LaTeXSettings.PlaceholderRestingNucleus, LaTeXSettings.PlaceholderRestingColor, MathKeyboardCaretState.TemporarilyHidden)
69-
: (LaTeXSettings.PlaceholderActiveNucleus, LaTeXSettings.PlaceholderActiveColor, MathKeyboardCaretState.ShownThroughPlaceholder);
70-
else _caretState = value;
51+
_insertionPositionHighlighted = value;
52+
if (MathList.AtomAt(_insertionIndex) is Atoms.Placeholder placeholder) {
53+
(placeholder.Nucleus, placeholder.Color) =
54+
_insertionPositionHighlighted
55+
? (LaTeXSettings.PlaceholderActiveNucleus, LaTeXSettings.PlaceholderActiveColor)
56+
: (LaTeXSettings.PlaceholderRestingNucleus, LaTeXSettings.PlaceholderRestingColor);
57+
}
7158
RecreateDisplayFromMathList();
7259
RedrawRequested?.Invoke(this, EventArgs.Empty);
7360
}
@@ -81,7 +68,7 @@ public MathListIndex InsertionIndex {
8168
set {
8269
_insertionIndex = value;
8370
ResetPlaceholders(MathList);
84-
CaretState = MathKeyboardCaretState.Shown;
71+
InsertionPositionHighlighted = true;
8572
}
8673
}
8774
public TFont Font { get; set; }
@@ -451,11 +438,13 @@ void InsertSymbolName(string name, bool subscript = false, bool superscript = fa
451438
break;
452439
case MathKeyboardInput.Return:
453440
ReturnPressed?.Invoke(this, EventArgs.Empty);
454-
CaretState = MathKeyboardCaretState.Hidden;
441+
InsertionPositionHighlighted = false;
442+
StopBlinking();
455443
return;
456444
case MathKeyboardInput.Dismiss:
457445
DismissPressed?.Invoke(this, EventArgs.Empty);
458-
CaretState = MathKeyboardCaretState.Hidden;
446+
InsertionPositionHighlighted = false;
447+
StopBlinking();
459448
return;
460449
case MathKeyboardInput.Slash:
461450
HandleSlashButton();
@@ -837,7 +826,7 @@ void InsertSymbolName(string name, bool subscript = false, bool superscript = fa
837826
break;
838827
}
839828
ResetPlaceholders(MathList);
840-
CaretState = MathKeyboardCaretState.Shown;
829+
InsertionPositionHighlighted = true;
841830
}
842831

843832
public void MoveCaretToPoint(PointF point) {

CSharpMath.Forms/MathKeyboardExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ public static void BindDisplay(this MathKeyboard keyboard,
2020
var c = e.Surface.Canvas;
2121
c.Clear();
2222
MathPainter.DrawDisplay(settings, keyboard.Display, c);
23-
keyboard.DrawCaret(
24-
new SkiaCanvas(c, settings.AntiAlias), caretColor.FromNative(), caretShape);
23+
if (keyboard.ShouldDrawCaret)
24+
keyboard.DrawCaret(new SkiaCanvas(c, settings.AntiAlias), caretColor.FromNative(), caretShape);
2525
};
2626
}
2727
}

CSharpMath.Rendering/FrontEnd/MathKeyboard.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public MathKeyboard(float fontSize = PainterConstants.DefaultFontSize, double bl
1414
public override RectangleF Measure =>
1515
Display != null ? new RectangleF(0, -Display.Ascent, Display.Width, Display.Ascent + Display.Descent) : RectangleF.Empty;
1616
public void DrawCaret(ICanvas canvas, Color color, CaretShape shape) {
17-
if (CaretState != MathKeyboardCaretState.Shown || Display is null)
17+
if (Display == null)
1818
return;
1919
var cursorPosition = Display.PointForIndex(TypesettingContext.Instance, InsertionIndex) ?? Display.Position;
2020
cursorPosition.Y *= -1; //inverted canvas, blah blah

CSharpMath/Atom/LaTeXSettings.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ public static class LaTeXSettings {
320320
};
321321
public static MathAtom Times => new BinaryOperator("×");
322322
public static MathAtom Divide => new BinaryOperator("÷");
323+
public static bool PlaceholderBlinks { get; set; } = false;
323324
public static Color? PlaceholderRestingColor { get; set; }
324325
public static Color? PlaceholderActiveColor { get; set; }
325326
public static string PlaceholderActiveNucleus { get; set; } = "\u25A0";

NuGet.Config

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<configuration>
3+
<packageSources>
4+
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
5+
</packageSources>
6+
</configuration>

0 commit comments

Comments
 (0)