Skip to content

Commit

Permalink
Add placeholder blinks setting (#177)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
Co-authored-by: FoggyFinder <[email protected]>
  • Loading branch information
3 people authored Oct 27, 2020
1 parent 476beb0 commit 892eaec
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 139 deletions.
239 changes: 133 additions & 106 deletions CSharpMath.Editor.Tests/CaretTests.cs

Large diffs are not rendered by default.

49 changes: 19 additions & 30 deletions CSharpMath.Editor/MathKeyboard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@ namespace CSharpMath.Editor {
using Structures;
using Atoms = Atom.Atoms;

public enum MathKeyboardCaretState : byte {
Hidden,
TemporarilyHidden,
ShownThroughPlaceholder,
Shown
}
public class MathKeyboard<TFont, TGlyph> : IDisposable where TFont : IFont<TGlyph> {
protected Timer blinkTimer;
public const double DefaultBlinkMilliseconds = 800;
Expand All @@ -23,21 +17,14 @@ public MathKeyboard(TypesettingContext<TFont, TGlyph> context, TFont font, doubl
Font = font;
blinkTimer = new Timer(blinkMilliseconds);
blinkTimer.Elapsed += (sender, e) => {
switch (CaretState) {
case MathKeyboardCaretState.Shown:
case MathKeyboardCaretState.ShownThroughPlaceholder:
CaretState = MathKeyboardCaretState.TemporarilyHidden;
break;
case MathKeyboardCaretState.TemporarilyHidden:
CaretState = MathKeyboardCaretState.Shown;
break;
}
if (!(MathList.AtomAt(_insertionIndex) is Atoms.Placeholder) || LaTeXSettings.PlaceholderBlinks)
InsertionPositionHighlighted = !InsertionPositionHighlighted;
};
blinkTimer.Start();
}
public bool ShouldDrawCaret => InsertionPositionHighlighted && !(MathList.AtomAt(_insertionIndex) is Atoms.Placeholder);
public void StartBlinking() => blinkTimer.Start();
public void StopBlinking() => blinkTimer.Stop();
//private readonly List<MathListIndex> highlighted;
protected TypesettingContext<TFont, TGlyph> Context { get; }
static void ResetPlaceholders(MathList mathList) {
foreach (var mathAtom in mathList.Atoms) {
Expand All @@ -55,19 +42,19 @@ static void ResetPlaceholders(MathList mathList) {
}
}
}
MathKeyboardCaretState _caretState;
public MathKeyboardCaretState CaretState {
get => _caretState;
bool _insertionPositionHighlighted;
public bool InsertionPositionHighlighted {
get => _insertionPositionHighlighted;
set {
blinkTimer.Stop();
blinkTimer.Start();
if (value != MathKeyboardCaretState.Hidden &&
MathList.AtomAt(_insertionIndex) is Atoms.Placeholder placeholder)
(placeholder.Nucleus, placeholder.Color, _caretState) =
value == MathKeyboardCaretState.TemporarilyHidden
? (LaTeXSettings.PlaceholderRestingNucleus, LaTeXSettings.PlaceholderRestingColor, MathKeyboardCaretState.TemporarilyHidden)
: (LaTeXSettings.PlaceholderActiveNucleus, LaTeXSettings.PlaceholderActiveColor, MathKeyboardCaretState.ShownThroughPlaceholder);
else _caretState = value;
_insertionPositionHighlighted = value;
if (MathList.AtomAt(_insertionIndex) is Atoms.Placeholder placeholder) {
(placeholder.Nucleus, placeholder.Color) =
_insertionPositionHighlighted
? (LaTeXSettings.PlaceholderActiveNucleus, LaTeXSettings.PlaceholderActiveColor)
: (LaTeXSettings.PlaceholderRestingNucleus, LaTeXSettings.PlaceholderRestingColor);
}
RecreateDisplayFromMathList();
RedrawRequested?.Invoke(this, EventArgs.Empty);
}
Expand All @@ -81,7 +68,7 @@ public MathListIndex InsertionIndex {
set {
_insertionIndex = value;
ResetPlaceholders(MathList);
CaretState = MathKeyboardCaretState.Shown;
InsertionPositionHighlighted = true;
}
}
public TFont Font { get; set; }
Expand Down Expand Up @@ -451,11 +438,13 @@ void InsertSymbolName(string name, bool subscript = false, bool superscript = fa
break;
case MathKeyboardInput.Return:
ReturnPressed?.Invoke(this, EventArgs.Empty);
CaretState = MathKeyboardCaretState.Hidden;
InsertionPositionHighlighted = false;
StopBlinking();
return;
case MathKeyboardInput.Dismiss:
DismissPressed?.Invoke(this, EventArgs.Empty);
CaretState = MathKeyboardCaretState.Hidden;
InsertionPositionHighlighted = false;
StopBlinking();
return;
case MathKeyboardInput.Slash:
HandleSlashButton();
Expand Down Expand Up @@ -837,7 +826,7 @@ void InsertSymbolName(string name, bool subscript = false, bool superscript = fa
break;
}
ResetPlaceholders(MathList);
CaretState = MathKeyboardCaretState.Shown;
InsertionPositionHighlighted = true;
}

public void MoveCaretToPoint(PointF point) {
Expand Down
4 changes: 2 additions & 2 deletions CSharpMath.Forms/MathKeyboardExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public static void BindDisplay(this MathKeyboard keyboard,
var c = e.Surface.Canvas;
c.Clear();
MathPainter.DrawDisplay(settings, keyboard.Display, c);
keyboard.DrawCaret(
new SkiaCanvas(c, settings.AntiAlias), caretColor.FromNative(), caretShape);
if (keyboard.ShouldDrawCaret)
keyboard.DrawCaret(new SkiaCanvas(c, settings.AntiAlias), caretColor.FromNative(), caretShape);
};
}
}
Expand Down
2 changes: 1 addition & 1 deletion CSharpMath.Rendering/FrontEnd/MathKeyboard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public MathKeyboard(float fontSize = PainterConstants.DefaultFontSize, double bl
public override RectangleF Measure =>
Display != null ? new RectangleF(0, -Display.Ascent, Display.Width, Display.Ascent + Display.Descent) : RectangleF.Empty;
public void DrawCaret(ICanvas canvas, Color color, CaretShape shape) {
if (CaretState != MathKeyboardCaretState.Shown || Display is null)
if (Display == null)
return;
var cursorPosition = Display.PointForIndex(TypesettingContext.Instance, InsertionIndex) ?? Display.Position;
cursorPosition.Y *= -1; //inverted canvas, blah blah
Expand Down
1 change: 1 addition & 0 deletions CSharpMath/Atom/LaTeXSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ public static class LaTeXSettings {
};
public static MathAtom Times => new BinaryOperator("×");
public static MathAtom Divide => new BinaryOperator("÷");
public static bool PlaceholderBlinks { get; set; } = false;
public static Color? PlaceholderRestingColor { get; set; }
public static Color? PlaceholderActiveColor { get; set; }
public static string PlaceholderActiveNucleus { get; set; } = "\u25A0";
Expand Down
6 changes: 6 additions & 0 deletions NuGet.Config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
</packageSources>
</configuration>

0 comments on commit 892eaec

Please sign in to comment.