Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make blinking Placeholder's Nucleus and ForeColor customizable in both CaretStates #167

Merged
merged 12 commits into from
Oct 4, 2020
Merged
Show file tree
Hide file tree
Changes from 8 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
3 changes: 3 additions & 0 deletions CSharpMath.CoreTests/MockTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Xunit;
using TGlyph = System.Char;
using CSharpMath.Display;
using System.Linq;
FoggyFinder marked this conversation as resolved.
Show resolved Hide resolved

namespace CSharpMath.CoreTests {
// purpose of this class is to make sure our mocks behave as expected.
Expand All @@ -12,6 +13,7 @@ public void TestGlyphBoundsWithoutM() {
var font = new TestFont(10);
var provider = TestGlyphBoundsProvider.Instance;
var glyphRun = new AttributedGlyphRun<TestFont, TGlyph>(hello, hello, font);
Assert.All(glyphRun.GlyphInfos, glyphInfo => Assert.Null(glyphInfo.Foreground));
var width = provider.GetTypographicWidth(font, glyphRun);
Approximately.Equal(width, 25, 0.01);
}
Expand All @@ -22,6 +24,7 @@ public void TestGlyphBoundsWithM() {
var font = new TestFont(10);
var provider = TestGlyphBoundsProvider.Instance;
var glyphRun = new AttributedGlyphRun<TestFont, TGlyph>(america, america, font);
Assert.All(glyphRun.GlyphInfos, glyphInfo => Assert.Null(glyphInfo.Foreground));
var width = provider.GetTypographicWidth(font, glyphRun);
Approximately.Equal(width, 40, 0.01);
}
Expand Down
108 changes: 103 additions & 5 deletions CSharpMath.Editor.Tests/CaretTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using CSharpMath.Atom;
using CSharpMath.CoreTests.FrontEnd;
using Xunit;

Expand Down Expand Up @@ -137,16 +138,14 @@ public async Task Test() {
Assert.Equal(MathKeyboardCaretState.Shown, keyboard.CaretState);
await Task.Delay((int)MathKeyboard<TestFont, char>.DefaultBlinkMilliseconds - CaretBlinks.MillisecondBuffer);

Assert.Equal(MathKeyboardCaretState.Shown, keyboard.CaretState);
keyboard.KeyPress(MathKeyboardInput.A);
await Task.Delay((int)MathKeyboard<TestFont, char>.DefaultBlinkMilliseconds - CaretBlinks.MillisecondBuffer);

Assert.Equal(MathKeyboardCaretState.Shown, keyboard.CaretState);
keyboard.KeyPress(MathKeyboardInput.Left);
await Task.Delay((int)MathKeyboard<TestFont, char>.DefaultBlinkMilliseconds - CaretBlinks.MillisecondBuffer);

keyboard.KeyPress(MathKeyboardInput.Left);
Assert.Equal(MathKeyboardCaretState.Shown, keyboard.CaretState);
await Task.Delay(4 * CaretBlinks.MillisecondBuffer);

await Task.Delay((int)MathKeyboard<TestFont, char>.DefaultBlinkMilliseconds + 4 * CaretBlinks.MillisecondBuffer);
Assert.Equal(MathKeyboardCaretState.TemporarilyHidden, keyboard.CaretState);
}
}
Expand Down Expand Up @@ -176,4 +175,103 @@ public async Task Test() {
Assert.Equal(MathKeyboardCaretState.Shown, keyboard.CaretState);
}
}
[Collection(nameof(NonParallelPlaceholderTests))]
public class CustomizablePlaceholder {
[Fact]
public async void CustomizedPlaceholderBlinks() {
LaTeXSettings.PlaceholderActiveNucleus = "😀";
LaTeXSettings.PlaceholderRestingNucleus = "😐";
LaTeXSettings.PlaceholderActiveColor = System.Drawing.Color.Green;
LaTeXSettings.PlaceholderRestingColor = System.Drawing.Color.Blue;

var keyboard = new MathKeyboard<TestFont, char>(TestTypesettingContexts.Instance, new TestFont()) {
CaretState = MathKeyboardCaretState.Shown
};
Assert.Equal(MathKeyboardCaretState.Shown, keyboard.CaretState);

keyboard.KeyPress(MathKeyboardInput.Subscript);
var outer = Assert.IsType<Atom.Atoms.Placeholder>(Assert.Single(keyboard.MathList));
var inner = Assert.IsType<Atom.Atoms.Placeholder>(Assert.Single(outer.Subscript));
Assert.Equal(MathKeyboardCaretState.ShownThroughPlaceholder, keyboard.CaretState);
Assert.Equal("😐", outer.Nucleus);
Assert.Equal(System.Drawing.Color.Blue, outer.Color);
Assert.Equal("😀", inner.Nucleus);
Assert.Equal(System.Drawing.Color.Green, inner.Color);

await Task.Delay((int)MathKeyboard<TestFont, char>.DefaultBlinkMilliseconds + CaretBlinks.MillisecondBuffer);
Assert.Equal(MathKeyboardCaretState.TemporarilyHidden, keyboard.CaretState);
Assert.Equal("😐", outer.Nucleus);
Assert.Equal(System.Drawing.Color.Blue, outer.Color);
Assert.Equal("😐", inner.Nucleus);
Assert.Equal(System.Drawing.Color.Blue, inner.Color);

await Task.Delay((int)MathKeyboard<TestFont, char>.DefaultBlinkMilliseconds + CaretBlinks.MillisecondBuffer);
Assert.Equal(MathKeyboardCaretState.ShownThroughPlaceholder, keyboard.CaretState);
Assert.Equal("😐", outer.Nucleus);
Assert.Equal(System.Drawing.Color.Blue, outer.Color);
Assert.Equal("😀", inner.Nucleus);
Assert.Equal(System.Drawing.Color.Green, inner.Color);

RestorePlaceholderDefaultSettings();
}
[Fact]
public void AllCustomizablePlaceholderPropertiesAreResetOnCaretVisible() {
LaTeXSettings.PlaceholderActiveNucleus = "😀";
LaTeXSettings.PlaceholderRestingNucleus = "😐";
LaTeXSettings.PlaceholderActiveColor = System.Drawing.Color.Green;
LaTeXSettings.PlaceholderRestingColor = System.Drawing.Color.Blue;
var keyboard = new MathKeyboard<TestFont, char>(TestTypesettingContexts.Instance, new TestFont()) {
CaretState = MathKeyboardCaretState.Shown
};
Assert.Equal(MathKeyboardCaretState.Shown, keyboard.CaretState);
keyboard.KeyPress(MathKeyboardInput.Subscript);
var outer = Assert.IsType<Atom.Atoms.Placeholder>(Assert.Single(keyboard.MathList));
var inner = Assert.IsType<Atom.Atoms.Placeholder>(Assert.Single(outer.Subscript));
Assert.Equal(MathKeyboardCaretState.ShownThroughPlaceholder, keyboard.CaretState);

keyboard.InsertionIndex = MathListIndex.Level0Index(keyboard.MathList.Count);
Assert.Equal(MathKeyboardCaretState.Shown, keyboard.CaretState);
Assert.Equal(LaTeXSettings.PlaceholderRestingNucleus, outer.Nucleus);
Assert.Equal(LaTeXSettings.PlaceholderRestingColor, outer.Color);
Assert.Equal(LaTeXSettings.PlaceholderRestingNucleus, inner.Nucleus);
Assert.Equal(LaTeXSettings.PlaceholderRestingColor, inner.Color);
RestorePlaceholderDefaultSettings();
}
[Fact]
public void LaTeXSettings_Placeholder_IsNewInstance() {
Assert.False(LaTeXSettings.Placeholder == LaTeXSettings.Placeholder);
// Double check, also verify that its contents are 'fresh':
FoggyFinder marked this conversation as resolved.
Show resolved Hide resolved
LaTeXSettings.Placeholder.Nucleus = "x";
Assert.NotEqual("x", LaTeXSettings.Placeholder.Nucleus);
LaTeXSettings.Placeholder.Color = System.Drawing.Color.Green;
Assert.NotEqual(System.Drawing.Color.Green, LaTeXSettings.Placeholder.Color);
}
[Fact]
public void DefaultPlaceholderAppearance() {
Assert.Null(LaTeXSettings.PlaceholderActiveColor);
Assert.Null(LaTeXSettings.PlaceholderRestingColor);
Assert.Equal("\u25A0", LaTeXSettings.PlaceholderActiveNucleus);
Assert.Equal("\u25A1", LaTeXSettings.PlaceholderRestingNucleus);
Assert.Equal(LaTeXSettings.PlaceholderRestingNucleus, LaTeXSettings.Placeholder.Nucleus);
Assert.Equal(LaTeXSettings.PlaceholderRestingColor, LaTeXSettings.Placeholder.Color);
}
SymboLinker marked this conversation as resolved.
Show resolved Hide resolved
[Fact]
public void CustomizedPlaceholderGetter() {
LaTeXSettings.PlaceholderActiveNucleus = "😀";
LaTeXSettings.PlaceholderRestingNucleus = "😐";
LaTeXSettings.PlaceholderActiveColor = System.Drawing.Color.Green;
LaTeXSettings.PlaceholderRestingColor = System.Drawing.Color.Blue;
Assert.Equal("😐", LaTeXSettings.Placeholder.Nucleus);
Assert.Equal(System.Drawing.Color.Blue, LaTeXSettings.Placeholder.Color);
RestorePlaceholderDefaultSettings();
}
private void RestorePlaceholderDefaultSettings() {
LaTeXSettings.PlaceholderActiveNucleus = "\u25A0";
LaTeXSettings.PlaceholderRestingNucleus = "\u25A1";
LaTeXSettings.PlaceholderActiveColor = null;
LaTeXSettings.PlaceholderRestingColor = null;
}
}
[CollectionDefinition(nameof(NonParallelPlaceholderTests), DisableParallelization = true)]
public class NonParallelPlaceholderTests { }
SymboLinker marked this conversation as resolved.
Show resolved Hide resolved
}
11 changes: 6 additions & 5 deletions CSharpMath.Editor/MathKeyboard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ static void ResetPlaceholders(MathList mathList) {
ResetPlaceholders(mathAtom.Superscript);
ResetPlaceholders(mathAtom.Subscript);
switch (mathAtom) {
case Atoms.Placeholder _:
mathAtom.Nucleus = "\u25A1";
case Atoms.Placeholder placeholder:
placeholder.Color = LaTeXSettings.PlaceholderRestingColor;
placeholder.Nucleus = LaTeXSettings.PlaceholderRestingNucleus;
break;
case IMathListContainer container:
foreach (var list in container.InnerLists)
Expand All @@ -62,10 +63,10 @@ public MathKeyboardCaretState CaretState {
blinkTimer.Start();
if (value != MathKeyboardCaretState.Hidden &&
MathList.AtomAt(_insertionIndex) is Atoms.Placeholder placeholder)
(placeholder.Nucleus, _caretState) =
(placeholder.Nucleus, placeholder.Color, _caretState) =
value == MathKeyboardCaretState.TemporarilyHidden
? ("\u25A1", MathKeyboardCaretState.TemporarilyHidden)
: ("\u25A0", MathKeyboardCaretState.ShownThroughPlaceholder);
? (LaTeXSettings.PlaceholderRestingNucleus, LaTeXSettings.PlaceholderRestingColor, MathKeyboardCaretState.TemporarilyHidden)
: (LaTeXSettings.PlaceholderActiveNucleus, LaTeXSettings.PlaceholderActiveColor, MathKeyboardCaretState.ShownThroughPlaceholder);
else _caretState = value;
RecreateDisplayFromMathList();
RedrawRequested?.Invoke(this, EventArgs.Empty);
Expand Down
6 changes: 4 additions & 2 deletions CSharpMath/Atom/Atoms/Placeholder.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System.Drawing;
namespace CSharpMath.Atom.Atoms {
/// <summary>A placeholder for future input</summary>
public sealed class Placeholder : MathAtom {
public Placeholder(string nucleus) : base(nucleus) { }
public Color? Color { get; set; }
public Placeholder(string nucleus, Color? color) : base(nucleus) => Color = color;
public override bool ScriptsAllowed => true;
public new Placeholder Clone(bool finalize) => (Placeholder)base.Clone(finalize);
protected override MathAtom CloneInside(bool finalize) => new Placeholder(Nucleus);
protected override MathAtom CloneInside(bool finalize) => new Placeholder(Nucleus, Color);
}
}
6 changes: 5 additions & 1 deletion CSharpMath/Atom/LaTeXSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,11 @@ public static class LaTeXSettings {
};
public static MathAtom Times => new BinaryOperator("×");
public static MathAtom Divide => new BinaryOperator("÷");
public static MathAtom Placeholder => new Placeholder("\u25A1");
public static Color? PlaceholderRestingColor { get; set; }
public static Color? PlaceholderActiveColor { get; set; }
public static string PlaceholderActiveNucleus { get; set; } = "\u25A0";
public static string PlaceholderRestingNucleus { get; set; } = "\u25A1";
public static Placeholder Placeholder => new Placeholder(PlaceholderRestingNucleus, PlaceholderRestingColor);
public static MathList PlaceholderList => new MathList { Placeholder };

public static AliasBiDictionary<string, FontStyle> FontStyles { get; } =
Expand Down
5 changes: 3 additions & 2 deletions CSharpMath/Display/AttributedGlyphRun.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;

Expand All @@ -7,9 +8,9 @@ namespace CSharpMath.Display {
/// over the whole string. We use KernedGlyph objects instead of Glyphs to
/// allow us to set kern on a per-glyph basis.</summary>
public class AttributedGlyphRun<TFont, TGlyph> where TFont : FrontEnd.IFont<TGlyph> {
public AttributedGlyphRun(string text, IEnumerable<TGlyph> glyphs, TFont font, bool isPlaceHolder = false) {
public AttributedGlyphRun(string text, IEnumerable<TGlyph> glyphs, TFont font, bool isPlaceHolder = false, Color? color = null) {
Text = new StringBuilder(text);
GlyphInfos = glyphs.Select(g => new GlyphInfo<TGlyph>(g)).ToList();
GlyphInfos = glyphs.Select(g => new GlyphInfo<TGlyph>(g) { Foreground = color}).ToList();
FoggyFinder marked this conversation as resolved.
Show resolved Hide resolved
Font = font;
Placeholder = isPlaceHolder;
}
Expand Down
2 changes: 1 addition & 1 deletion CSharpMath/Display/Typesetter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ private void CreateDisplayAtoms(List<MathAtom> preprocessedAtoms) {
var nucleusText = atom.Nucleus;
var glyphs = _context.GlyphFinder.FindGlyphs(_font, nucleusText);
var current = new AttributedGlyphRun<TFont, TGlyph>(
nucleusText, glyphs, _font, atom is Placeholder);
nucleusText, glyphs, _font, atom is Placeholder, (atom as Placeholder)?.Color);
_currentLine.AppendGlyphRun(current);
if (_currentLineIndexRange.Location == Range.UndefinedInt)
_currentLineIndexRange = atom.IndexRange;
Expand Down