From 824d0df67b52c71ffbea970f165ad60b35ba16d3 Mon Sep 17 00:00:00 2001 From: Hadrian Tang Date: Tue, 7 Apr 2020 21:24:36 +0800 Subject: [PATCH] Finalized MathButton API --- CSharpMath.Avalonia/MathButton.cs | 28 ---------------- .../Controls/MathKeyboard.xaml | 28 ++++++++++++---- CSharpMath.Forms/Buttons.cs | 32 +++++++++++++++++++ CSharpMath.Forms/MathButton.cs | 32 ------------------- CSharpMath.Forms/MathInputButton.cs | 6 +++- CSharpMath.Rendering.Tests/Test.cs | 1 - CSharpMath.Rendering/FrontEnd/MathKeyboard.cs | 2 +- CSharpMath.Rendering/FrontEnd/Painter.cs | 2 +- CSharpMath.Xaml/CSharpMath.Xaml.projitems | 1 - CSharpMath.Xaml/ICSharpMathView.cs | 19 ----------- CSharpMath.Xaml/Views.cs | 2 +- 11 files changed, 61 insertions(+), 92 deletions(-) delete mode 100644 CSharpMath.Avalonia/MathButton.cs create mode 100644 CSharpMath.Forms/Buttons.cs delete mode 100644 CSharpMath.Forms/MathButton.cs delete mode 100644 CSharpMath.Xaml/ICSharpMathView.cs diff --git a/CSharpMath.Avalonia/MathButton.cs b/CSharpMath.Avalonia/MathButton.cs deleted file mode 100644 index 5839bf82..00000000 --- a/CSharpMath.Avalonia/MathButton.cs +++ /dev/null @@ -1,28 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; -using Avalonia; -using Avalonia.Controls; -using Avalonia.Media; - -namespace CSharpMath.Avalonia { - public class MathButton : Button { - static MathButton() { - LaTeXProperty.Changed.AddClassHandler((b, e) => b.Painter.LaTeX = (string)e.NewValue); - AffectsMeasure(LaTeXProperty); - AffectsRender(LaTeXProperty); - } - public MathPainter Painter { get; } = new MathPainter(); - protected override global::Avalonia.Size MeasureOverride(global::Avalonia.Size availableSize) => - Painter.Measure((float)availableSize.Width) is { } rect - ? new global::Avalonia.Size(rect.Width, rect.Height) - : base.MeasureOverride(availableSize); - public override void Render(DrawingContext context) { - //Painter.Draw(new AvaloniaCanvas(context, Bounds.Size)); - base.Render(context); - } - public string? LaTeX { get => (string?)GetValue(LaTeXProperty); set => SetValue(LaTeXProperty, value); } - public static readonly AvaloniaProperty LaTeXProperty = - AvaloniaProperty.Register(nameof(LaTeX)); - } -} diff --git a/CSharpMath.Forms.Example/CSharpMath.Forms.Example/Controls/MathKeyboard.xaml b/CSharpMath.Forms.Example/CSharpMath.Forms.Example/Controls/MathKeyboard.xaml index 3cfff970..aa624bde 100644 --- a/CSharpMath.Forms.Example/CSharpMath.Forms.Example/Controls/MathKeyboard.xaml +++ b/CSharpMath.Forms.Example/CSharpMath.Forms.Example/Controls/MathKeyboard.xaml @@ -156,7 +156,9 @@ - + + + @@ -183,7 +185,9 @@ - + + + @@ -210,7 +214,9 @@ - + + + @@ -237,7 +243,9 @@ - + + + @@ -264,7 +272,9 @@ - + + + @@ -297,7 +307,9 @@ - + + + @@ -334,7 +346,9 @@ - + + + diff --git a/CSharpMath.Forms/Buttons.cs b/CSharpMath.Forms/Buttons.cs new file mode 100644 index 00000000..43af33ad --- /dev/null +++ b/CSharpMath.Forms/Buttons.cs @@ -0,0 +1,32 @@ +using SkiaSharp; +using Xamarin.Forms; +namespace CSharpMath.Forms { + using SkiaSharp; + [ContentProperty(nameof(Content))] + public abstract class BaseButton : ImageButton + where TView : BaseView + where TPainter : Rendering.FrontEnd.Painter, new() + where TContent : class { + public BaseButton() { + Aspect = Aspect.AspectFit; + // Color.Default will be ugly: https://github.com/verybadcat/CSharpMath/issues/111 + BackgroundColor = Color.Transparent; + Source = ImageSource.FromStream(() => { + if (Content is { } c) { + var latex = c.Painter.LaTeX; + // Appropriate positioning for non-full characters, e.g. prime, degree + // Also acts as spacing between MathButtons next to each other + c.Painter.LaTeX = @"{\color{#0000}|}" + latex + @"{\color{#0000}|}"; + var stream = c.Painter.DrawAsStream(); + c.Painter.LaTeX = latex; + return stream; + } + return null; + }); + } + public TView? Content { get => (TView?)GetValue(ContentProperty); set => SetValue(ContentProperty, value); } + public static readonly BindableProperty ContentProperty = BindableProperty.Create(nameof(Content), typeof(TView), typeof(BaseButton)); + } + public class MathButton : BaseButton { } + public class TextButton : BaseButton { } +} \ No newline at end of file diff --git a/CSharpMath.Forms/MathButton.cs b/CSharpMath.Forms/MathButton.cs deleted file mode 100644 index 2cc9116e..00000000 --- a/CSharpMath.Forms/MathButton.cs +++ /dev/null @@ -1,32 +0,0 @@ -using Xamarin.Forms; -using SkiaSharp.Views.Forms; -namespace CSharpMath.Forms { - using SkiaSharp; - public class MathButton : ImageButton { - public MathButton() { - Aspect = Aspect.AspectFit; - // Color.Default will be ugly: https://github.com/verybadcat/CSharpMath/issues/111 - BackgroundColor = Color.Transparent; - } - private static void Paint(BindableObject o, object _, object __) { - var b = (MathButton)o; - b.Source = ImageSource.FromStream(() => new MathPainter { - FontSize = b.Clarity, - // Appropriate positioning for non-full characters, e.g. prime, degree - // Also acts as spacing between MathButtons next to each other - LaTeX = @"{\color{#0000}|}" + b.LaTeX + @"{\color{#0000}|}", - TextColor = b.TextColor.ToSKColor() - }.DrawAsStream()); - } - static readonly MathPainter staticPainter = new MathPainter(); - public static readonly BindableProperty LaTeXProperty = - BindableProperty.Create(nameof(LaTeX), typeof(string), typeof(MathButton), staticPainter.LaTeX, propertyChanged: Paint); - public string LaTeX { get => (string)GetValue(LaTeXProperty); set => SetValue(LaTeXProperty, value); } - public static readonly BindableProperty TextColorProperty = - BindableProperty.Create(nameof(TextColor), typeof(Color), typeof(MathButton), staticPainter.TextColor.ToFormsColor(), propertyChanged: Paint); - public Color TextColor { get => (Color)GetValue(TextColorProperty); set => SetValue(TextColorProperty, value); } - public static readonly BindableProperty ClarityProperty = - BindableProperty.Create(nameof(Clarity), typeof(float), typeof(MathButton), 50f, propertyChanged: Paint); - public float Clarity { get => (float)GetValue(ClarityProperty); set => SetValue(ClarityProperty, value); } - } -} \ No newline at end of file diff --git a/CSharpMath.Forms/MathInputButton.cs b/CSharpMath.Forms/MathInputButton.cs index 713eb8e7..ac69d525 100644 --- a/CSharpMath.Forms/MathInputButton.cs +++ b/CSharpMath.Forms/MathInputButton.cs @@ -25,7 +25,11 @@ static string InputToLaTeX(MathKeyboardInput input) { } } public static readonly BindableProperty InputProperty = - BindableProperty.Create(nameof(Input), typeof(MathKeyboardInput), typeof(MathInputButton), propertyChanged:(b, o, n) => ((MathInputButton)b).LaTeX = InputToLaTeX((MathKeyboardInput)n)); + BindableProperty.Create(nameof(Input), typeof(MathKeyboardInput), typeof(MathInputButton), propertyChanged:(b, o, n) => { + var button = (MathInputButton)b; + button.Content ??= new MathView(); + button.Content.LaTeX = InputToLaTeX((MathKeyboardInput)n); + }); public MathKeyboardInput Input { get => (MathKeyboardInput)GetValue(InputProperty); set => SetValue(InputProperty, value); } } } diff --git a/CSharpMath.Rendering.Tests/Test.cs b/CSharpMath.Rendering.Tests/Test.cs index 48a00f3a..77d6e128 100644 --- a/CSharpMath.Rendering.Tests/Test.cs +++ b/CSharpMath.Rendering.Tests/Test.cs @@ -95,7 +95,6 @@ protected void Run( // Prevent black background behind black rendered output in File Explorer preview painter.HighlightColor = painter.UnwrapColor(new Structures.Color(0xF0, 0xF0, 0xF0)); - painter.FontSize = 50f; painter.LaTeX = latex; var actualFile = new FileInfo(System.IO.Path.Combine(folder, inFile + "." + frontEnd + ".png")); diff --git a/CSharpMath.Rendering/FrontEnd/MathKeyboard.cs b/CSharpMath.Rendering/FrontEnd/MathKeyboard.cs index 2f33f688..3c225c8d 100644 --- a/CSharpMath.Rendering/FrontEnd/MathKeyboard.cs +++ b/CSharpMath.Rendering/FrontEnd/MathKeyboard.cs @@ -7,7 +7,7 @@ namespace CSharpMath.Rendering.FrontEnd { public enum CaretShape { IBeam, UpArrow } public class MathKeyboard : MathKeyboard { - public MathKeyboard(float fontSize = PainterConstants.DefaultFontSize * 3 / 2, double blinkMilliseconds = DefaultBlinkMilliseconds) + public MathKeyboard(float fontSize = PainterConstants.DefaultFontSize, double blinkMilliseconds = DefaultBlinkMilliseconds) : base(TypesettingContext.Instance, new Fonts(Array.Empty(), fontSize), blinkMilliseconds) { } diff --git a/CSharpMath.Rendering/FrontEnd/Painter.cs b/CSharpMath.Rendering/FrontEnd/Painter.cs index 8b18d076..4b9d0d9c 100644 --- a/CSharpMath.Rendering/FrontEnd/Painter.cs +++ b/CSharpMath.Rendering/FrontEnd/Painter.cs @@ -14,7 +14,7 @@ namespace CSharpMath.Rendering.FrontEnd { using BackEnd; public static class PainterConstants { - public const float DefaultFontSize = 20f; + public const float DefaultFontSize = 50f; } public abstract class Painter : ICSharpMathAPI where TContent : class { public const float DefaultFontSize = PainterConstants.DefaultFontSize; diff --git a/CSharpMath.Xaml/CSharpMath.Xaml.projitems b/CSharpMath.Xaml/CSharpMath.Xaml.projitems index 37288b1a..4d496ab8 100644 --- a/CSharpMath.Xaml/CSharpMath.Xaml.projitems +++ b/CSharpMath.Xaml/CSharpMath.Xaml.projitems @@ -9,7 +9,6 @@ CSharpMath.Xaml - \ No newline at end of file diff --git a/CSharpMath.Xaml/ICSharpMathView.cs b/CSharpMath.Xaml/ICSharpMathView.cs deleted file mode 100644 index 6c6254ba..00000000 --- a/CSharpMath.Xaml/ICSharpMathView.cs +++ /dev/null @@ -1,19 +0,0 @@ -using CSharpMath.Rendering.FrontEnd; -using CSharpMath.Structures; - -#if Avalonia -namespace CSharpMath.Avalonia { -#elif Forms -namespace CSharpMath.Forms { -#endif - public interface ICSharpMathView - : ICSharpMathAPI - where TPainter : Painter - where TContent : class { - TPainter Painter { get; } - TextAlignment TextAlignment { get; set; } - Thickness Padding { get; set; } - float DisplacementX { get; set; } - float DisplacementY { get; set; } - } -} diff --git a/CSharpMath.Xaml/Views.cs b/CSharpMath.Xaml/Views.cs index 3f9cc66e..1dac78e3 100644 --- a/CSharpMath.Xaml/Views.cs +++ b/CSharpMath.Xaml/Views.cs @@ -26,7 +26,7 @@ namespace CSharpMath.Avalonia { namespace CSharpMath.Forms { [Xamarin.Forms.ContentProperty(nameof(LaTeX))] #endif - public class BaseView : XInheritControl, ICSharpMathView + public class BaseView : XInheritControl, ICSharpMathAPI where TPainter : Painter, new() where TContent : class { public TPainter Painter { get; } = new TPainter();