Skip to content

Commit

Permalink
Add TextColor property to BaseButton and make MathInputButton's color…
Browse files Browse the repository at this point in the history
… customizable (#164)

* Add TextColor property to BaseButton and make MathInputButton's color customizable

The main goal of this commit is to make keyboard colors customizable (per keyboard key) to fit an app's color theme(s). For example, a dark keyboard background with light button texts.

CSharpMath.Forms:
- Refactor BaseButton's constructor.
- Add TextColor property to BaseButton and a bindable TextColorProperty.
- Remove the predefined Red color from MathInputButton.InputToLaTeX(input) for "Backspace" and "Clear".

CSharpMath.Forms.Example (MathKeyboard.xaml):
- Use the TextColorProperty in the Style of "Backspace" and "Clear", setting it to Red.

* Add a few ButtonTests and refactor out use-once functions from BaseButton

Notes:
- I guessed that CSharpMath.Forms needs a Test project of its own.
- It seems to deviate from the current coding style to have such a class, but I needed to make sure that the unit test MathInputButtonsHaveBackTextColorByDefault could detect the "phantom" and thus I added a static class LatexHelper that contains the temporary implementation of a "fake" vphantom. I also added the method SetColor(latex,Xamarin.Forms.Color).

* Fix typo in unit test name MathInputButtonsHaveBlackTextColorByDefault

* ButtonTests: use [ClassData] attribute, use NotNull<T>() extension, include in slnf

- Instead of IEnumerable<MathInputButton> TheMathInputButtons, use the [ClassData] attribute, introducing TestHelper.ComplexClassData<T> that can pass classes instead of ValueType data.
- Instead of using the ! (null-forgiving) operator, introduce and use NotNull<T>() extension that throws a Xunit.Sdk.NotNullException if the object is null and otherwise returns the not-null object. (Note: the namespace deviates from the folder structure, so that it is available everywhere in the test project without the need to add a using statement.)
- Improve logic in Assert of AllMathInputButtonsHaveLatexContent.
- Add CSharpMath.Forms.Tests to CSharpMath.CrossPlatform.slnf.

Maybe the newly created classes can be used in other parts of the solution as well, but I don't feel comfortable deciding that. Most of the code is very compact and adding classes feels like ignoring that coding style. Also, I am not very confident that those classes I have created meet the expected standards. I therefore put them "close to where they are used".

* Update CSharpMath.CrossPlatform.slnf (suggested commit))

Co-authored-by: Hadrian Tang <[email protected]>

* ButtonTests: use the [MemberData] attribute and MathKeyboardInput enum for individual testcases + do some cleaning

- NotNull.cs: remove pragma directives and unused namespace usings.
- Rename LatexHelper's vphantom to phantom, as the intention is to also add a tiny bit of horizontal spacing.
- ButtonTests: use the [MemberData] attribute and remove TestHelpers.ComplexClassData. Use the MathKeyboardInput enum as method parameter type, since only xunit-serializable types will result in individual test cases.
- Remove unused linked file from CSharpMath.Forms.Tests.csproj.
- Restore deleted space from commit 4a56307, and remove another white line. (I committed the suggestion to see its result: I didn't understand why the space character seemed to be removed at the start of the line and I thought it had something to do with my Visual Studio settings. Now I know that the suggested change was removing the white line and that the removed space character was not the intention.)

* Rename and move file from TestHelpers/NotNull.cs to /Extensions.cs

* Add ButtonBase.TextColorProperty Xaml Tests to CSharpMath.Forms.Tests

Notes:
- I changed the namespace of Extensions.NotNull and made the class partial, so that it can be linked from any test project.
- I am still in doubt about what is the right folder/project structure for the tests.

Long description of this last comment:
-- The name Xaml refers to the markup language and if code behind stuff should be included in that project, then maybe "UI" is a better name than "Xaml".
-- The class names are very broad: for example, "Test" seems to say that that is the main file that should include all tests. But that is set up only for tests that are shared between Avalonia and Xamarin.Froms. When thinking about creating a file that is for testing Xamarin.Forms only, I bump into the fact that one file is called TestXamarinForms.cs but that file has a completely different purpose than being the container for unit tests.
-- Postponing the decision to restructure - or actually awaiting to hear your preferences - I used external links. I wouldn't mind making the change or even thinking up a new the structure, but without that request I won't do that of course (as a newbie/guest to the project).

* Add MathInputButton_Command unit test and do the suggested cleaning

- Add MathInputButton_Command unit test.
- The global XML Namespace is only needed for the outermost node.
- Move a refactored NotNullExtension into ButtonTests.cs.

* MathInputButton_Command test: use the MathInputButton's Keyboard getter

* MathInputButton_Command test: no variable is needed anymore for the MathKeyboard instance

* Revert "MathInputButton_Command test: no variable is needed anymore for the MathKeyboard instance"

This reverts commit 132e19f.

* Revert "MathInputButton_Command test: use the MathInputButton's Keyboard getter"

This reverts commit fe0f460.

* Add MathButton unit tests that check that the image color is correctly set in the StreamImageSource

Probably needless to say: if ever the font changes, new images can created by adding the following method temporarily to the unit test and copying the result into the test project's "files/buttons" folder:
static void StreamToFile(Stream s, FileInfo f) { using (var fs = f.Create()) { s.CopyTo(fs); }}

* Attempt/test 1: also reference SkiaSharp from the CSharpMath.Forms.csproj

* Attempt/test 2: also reference SkiaSharp from CSharpMath.Forms.Test.csproj

* Attempt/test 3: Reference SkiaSharp & SkiaSharp.Views.Forms from test project (note: my previous commit message stated that I included SkiaSharp, but it was SkiaSharp.Views.Forms)

* Revert "Attempt/test 3: Reference SkiaSharp & SkiaSharp.Views.Forms from test project (note: my previous commit message stated that I included SkiaSharp, but it was SkiaSharp.Views.Forms)"

This reverts commit b43a974.

* Revert "Attempt/test 2: also reference SkiaSharp from CSharpMath.Forms.Test.csproj"

This reverts commit 350fc9b.

* Revert "Attempt/test 1: also reference SkiaSharp from the CSharpMath.Forms.csproj"

This reverts commit 8c486dd.

* Update SkiaSharp and SkiaSharp.View.Forms to 2.80.2 in all referencing projects

Introduces unwanted side effects:
- Warning NU1605 Detected package downgrade: Xamarin.Forms from 4.6.0.772 to 4.3.0.908675.
- A reference to a dll from CSharpMath.Forms.Tests, because somehow referencing the NuGet package wasn't enough. This seems to be a known bug: mono/SkiaSharp#1393.

* Add libSkiaSharp.dll to test project CSharpMath.Forms.Tests

Trying out a variation of workaround 3 from:
mono/SkiaSharp#1041 (comment)

* Revert "Add libSkiaSharp.dll to test project CSharpMath.Forms.Tests"

This reverts commit 593d677.

* Revert "Update SkiaSharp and SkiaSharp.View.Forms to 2.80.2 in all referencing projects"

This reverts commit 31cc12a.

* Customize buttons for MathKeyboardInput even more

- Make the button display Latex related to the enum values of MathKeyboardInput overridable. (Not the math that may be displayed after pressing the button, but only the appearance of the button itself.)
- Use a TextButton as a base class for MathInputButton. Then you have more options than when using a MathButton. (See the Example project, MyMathInputButton.cs where just the text "space" is used.)
- Add another button type related to MathKeyboardInput (but unrelated to LaTeX): ImageSourceMathInputButton. This may help people be creative. (See the Example project, the Clear button now appears as a flame: Controls/ImageSourceMathInputButtons/flame.png.)
- Add a unit tests:
--- ImageSourceMathInputButton_InputProperty_KeyboardProperty_and_Command
--- MathInputButton_KeyboardProperty

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

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

* Renaming of Placeholder-related variables + refactor (#167)

- Rename Placholder's "ForeColor" to "Color".
- Use property initializer GlyphInfo.Foreground.
- Restore "readonly field" instead of property getter for LaTeXSettings.Dummy.
- Use the name parts "Resting" and "Active" in the placeholder setting names (instead of "Hiding" and "FullShow" which are related to the caret but do not fit a blinking placeholder).
- Use variable name "placeholder" instead of "ph".

* Add unit tests for customizable placeholder (#167)

* Disable parallelization of customizable placeholder unit tests

Also: verify more in LaTeXSettings_Placeholder_IsNewInstance.

* Add unit test AllCustomizablePlaceholderPropertiesAreResetOnCaretVisible (#167)

Also: in the MockTests class, verify that AttributedGlyphRun sets the GlyphInfo.Foreground to null (default color).

* Unit test CustomizedPlaceholderBlinks: test complete cycle

* Fix failing unit test CaretTimerResetsOnKeyPress

* Use Assert.All instead of Assert.True(enumerable.All(pred))

* Revert "Fix failing unit test CaretTimerResetsOnKeyPress"

This reverts commit 9925952.

* Replace Assert.NotEqual + replace hardcoded strings by constants

* Refactoring and cleaning (of customizable placeholder tests and more)

* Placeholder tests: use Assert.NotSame and async Task

* MathInputButtons with customized placeholders: what you see is what you get in the output

In case MathInputButtons have the same TextColor as the output, the placeholders can be customized by only setting one or more of the LaTeXSetting properties PlaceholderActiveNucleus, PlaceholderRestingNucleus, PlaceholderActiveColor, PlaceholderRestingColor.
In case of different keyboard colors than output colors then the MathInputButton properties PlaceholderActiveColor and/or PlaceholderRestingColor can be used to override the LaTeXSettings. Those two properties use a weird hack: BindableProperties cannot have nullable value types it seems. Nobody will ever use a transparent placeholder, so that is save to use as a replacement for null. The color black should be available, in case the TextColor is not black.

This commit reverts the adding of MyMathInputButton of commit 94ebda2.

The Example project's Editor tab is updated to have a "Change appearance" button on the EditorPage that does a round trip through 3 themes that show different Clear buttons, different colors on the keyboard keys and output, different placeholders on the keyboard and in the output.

A large number of testcases has been added. It does not cover everything however.
The PlaceholderColorsProperties_MathInputButton expects a weird LaTeX string (you see "{}" at index 24):
@"\(\color{blue}{\square }{}^{\color{green}{■}}\)"

* Correct comment

* MathInputButton: only perform the placeholder color logic if defaults are overridden + refactor

- At ButtonDraw: only perform the placeholder color logic if the placeholder properties (of LaTeXSettings or of MathInputButton) are not the default values (null).
- Introduce NullableColorBindablePropertyHelper for nullable color workaround.

* Comment out 2 ButtonTests that have Linux failures

* Use FactAttribute.Skip

* Delete NullableColorBindablePropertyHelper (I am afraid I did not try the right things before - it just works)

* Revert "Delete NullableColorBindablePropertyHelper (I am afraid I did not try the right things before - it just works)"

This reverts commit da8b291.

* Use "foreach" instead of Xamarin.Forms.Internals.ForEach

* First call SetButtonsTextColor and then SetClearButtonImageSource

* Update CSharpMath.Forms.Example/CSharpMath.Forms.Example/Controls/MathKeyboard.xaml.cs

Co-authored-by: FoggyFinder <[email protected]>

* Add unit tests MathButtonTextColorCanChangeMultipleTimes and MathInputButtonTextColorCanChangeMultipleTimes

Also:
- Skip only on Linux via "FactSkipLinux(reason)" instead of a general Fact(Skip = reason).
- Move some helper methods into a separate file ButtonTestsHelper.cs.
- Rename png used in unit tests, using more specific names.

* Introduce interface IButtonDraw for bindablePropertyChanged

- Move ButtonDraw from TextColor setter to TextColorProperty's propertyChanged event.
- If calling ButtonDraw after casting to a base class, the subclass' override of ButtonDraw() is not executed, but via the IButtonDraw interface it is.
- Move NullableColorBindablePropertyHelper to its own file.
- ButtonTestsHelper refactor: extract method imageButton.ImageSourceAsStream().

Example project:
- use PlaceholderBlinks setting in the thrid theme.
- call ButtonDraw during each theme change (this is officially only needed for going from theme 1 to theme 2, but may also fix a not-understood bug).

* Shut CSharpMath.Ios.Tests

* add lock for now

* Create and use method SubStringCount() instead of Regex.Matches().Count

* Customized placeholder colors: add unit test

Co-authored-by: Hadrian Tang <[email protected]>
Co-authored-by: FoggyFinder <[email protected]>
  • Loading branch information
3 people authored Jun 21, 2021
1 parent 69fb790 commit 23e681b
Show file tree
Hide file tree
Showing 22 changed files with 610 additions and 62 deletions.
3 changes: 2 additions & 1 deletion CSharpMath.CrossPlatform.slnf
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"CSharpMath.Avalonia.Example/CSharpMath.Avalonia.Example.csproj",
"CSharpMath.SkiaSharp/CSharpMath.SkiaSharp.csproj",
"CSharpMath.Forms/CSharpMath.Forms.csproj",
"CSharpMath.Forms.Example/CSharpMath.Forms.Example/CSharpMath.Forms.Example.csproj"
"CSharpMath.Forms.Example/CSharpMath.Forms.Example/CSharpMath.Forms.Example.csproj",
"CSharpMath.Forms.Tests/CSharpMath.Forms.Tests.csproj"
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@
<Compile Include="..\..\CSharpMath.Rendering.Tests\TestRenderingSharedData.cs" Link="Examples\TestRenderingSharedData.cs" />
</ItemGroup>

<ItemGroup>
<Content Include="Controls\ImageSourceMathInputButtons\flame.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Controls\ImageSourceMathInputButtons\metaltrashcan.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Controls\ImageSourceMathInputButtons\recyclebin.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Xamarin.Forms" Version="4.3.0.908675" />
<ProjectReference Include="..\..\CSharpMath.Forms\CSharpMath.Forms.csproj" />
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:r="clr-namespace:CSharpMath.Rendering.FrontEnd;assembly=CSharpMath.Rendering"
xmlns:e="clr-namespace:CSharpMath.Forms.Example"
xmlns:f="clr-namespace:CSharpMath.Forms;assembly=CSharpMath.Forms"
x:Class="CSharpMath.Forms.Example.MathKeyboard"
Expand Down Expand Up @@ -113,12 +112,14 @@
<!--Right row of first 3 tabs-->
<Style TargetType="f:MathInputButton" Class="Back">
<Setter Property="Input" Value="Backspace"/>
<Setter Property="TextColor" Value="Red" />
<Setter Property="Grid.Column" Value="5"/>
<Setter Property="Grid.Row" Value="0"/>
<Setter Property="Keyboard" Value="{StaticResource Keyboard}"/>
</Style>
<Style TargetType="f:MathInputButton" Class="Clear">
<Style TargetType="f:ImageSourceMathInputButton" Class="Clear">
<Setter Property="Input" Value="Clear"/>
<Setter Property="Source" Value="Controls/ImageSourceMathInputButtons/recyclebin.png" />
<Setter Property="Grid.Column" Value="5"/>
<Setter Property="Grid.Row" Value="1"/>
<Setter Property="Keyboard" Value="{StaticResource Keyboard}"/>
Expand Down Expand Up @@ -155,7 +156,7 @@
<ColumnDefinition Width="*"/>
</ColumnDefinitionCollection>
</Grid.ColumnDefinitions>
<f:MathInputButton Input="Left" Grid.Column="0" Keyboard="{StaticResource Keyboard}"/>
<f:MathInputButton x:Name="LeftButton" Input="Left" Grid.Column="0" Keyboard="{StaticResource Keyboard}"/>
<f:MathButton x:Name="NumbersButton" Grid.Column="1" Command="{e:SwitchToTab Self={Reference this}, Target=Numbers}">
<f:MathView LaTeX="123"/>
</f:MathButton>
Expand All @@ -181,7 +182,7 @@
<f:MathInputButton Input="Equals" Grid.Row="3" Grid.Column="3" Keyboard="{StaticResource Keyboard}"/>
<f:MathInputButton Input="Plus" Grid.Row="3" Grid.Column="4" Keyboard="{StaticResource Keyboard}"/>
<f:MathInputButton StyleClass="Back"/>
<f:MathInputButton StyleClass="Clear"/>
<f:ImageSourceMathInputButton StyleClass="Clear"/>
<f:MathInputButton StyleClass="Dismiss"/>
<f:MathInputButton StyleClass="Enter"/>
</Grid>
Expand Down Expand Up @@ -210,7 +211,7 @@
<f:MathInputButton Input="Ratio" Grid.Row="3" Grid.Column="3" Keyboard="{StaticResource Keyboard}"/>
<f:MathInputButton Input="Comma" Grid.Row="3" Grid.Column="4" Keyboard="{StaticResource Keyboard}"/>
<f:MathInputButton StyleClass="Back"/>
<f:MathInputButton StyleClass="Clear"/>
<f:ImageSourceMathInputButton StyleClass="Clear"/>
<f:MathInputButton StyleClass="Dismiss"/>
<f:MathInputButton StyleClass="Enter"/>
</Grid>
Expand Down Expand Up @@ -239,7 +240,7 @@
<f:MathInputButton Input="NthRoot" Grid.Row="3" Grid.Column="3" Keyboard="{StaticResource Keyboard}"/>
<f:MathInputButton Input="Degree" Grid.Row="3" Grid.Column="4" Keyboard="{StaticResource Keyboard}"/>
<f:MathInputButton StyleClass="Back"/>
<f:MathInputButton StyleClass="Clear"/>
<f:ImageSourceMathInputButton StyleClass="Clear"/>
<f:MathInputButton StyleClass="Dismiss"/>
<f:MathInputButton StyleClass="Enter"/>
</Grid>
Expand Down Expand Up @@ -268,7 +269,7 @@
<f:MathInputButton Input="Up" Grid.Row="3" Grid.Column="3" Keyboard="{StaticResource Keyboard}"/>
<f:MathInputButton Input="Up" Grid.Row="3" Grid.Column="4" Keyboard="{StaticResource Keyboard}"/>
<f:MathInputButton StyleClass="Back"/>
<f:MathInputButton StyleClass="Clear"/>
<f:ImageSourceMathInputButton StyleClass="Clear"/>
<f:MathInputButton StyleClass="Dismiss"/>
<f:MathInputButton StyleClass="Enter"/>
</Grid>
Expand Down Expand Up @@ -349,10 +350,10 @@
<f:MathButton Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="3" Command="{e:SwitchToTab Self={Reference this}, Target=Letters}" x:Name="ShiftCapitalsButton">
<f:MathView LaTeX=""/>
</f:MathButton>
<f:MathInputButton StyleClass="Clear" Grid.Row="2" Grid.Column="17" Grid.ColumnSpan="3"/>
<f:ImageSourceMathInputButton StyleClass="Clear" Grid.Row="2" Grid.Column="17" Grid.ColumnSpan="3"/>
<f:MathInputButton StyleClass="Dismiss" Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="5"/>
<f:MathInputButton StyleClass="Enter" Grid.Row="3" Grid.Column="15" Grid.RowSpan="1" Grid.ColumnSpan="5"/>
</Grid>
<f:MathInputButton Input="Right" Grid.Column="6" Keyboard="{StaticResource Keyboard}"/>
<f:MathInputButton x:Name="RightButton" Input="Right" Grid.Column="6" Keyboard="{StaticResource Keyboard}"/>
</Grid>
</ContentView>
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System;
using System.Linq;
using CSharpMath.Editor;
using Xamarin.Forms;
using Xamarin.Forms.Internals;
using Xamarin.Forms.Xaml;

namespace CSharpMath.Forms.Example {
Expand All @@ -20,12 +23,8 @@ public MathKeyboard(float fontSize = Rendering.FrontEnd.PainterConstants.Default
public Tab CurrentTab {
get => _tab;
set {
foreach (var grid in new[] {
Numbers, Symbols, Functions, Operations, Letters, LettersCapitals
}) grid.IsVisible = false;
foreach (var gridButton in new[] {
NumbersButton, SymbolsButton, FunctionsButton, OperationsButton, LettersButton
}) gridButton.BackgroundColor = Color.Transparent;
foreach (var buttonGrid in ButtonGrids) buttonGrid.IsVisible = false;
foreach (var tabButton in TabButtons) tabButton.BackgroundColor = Color.Transparent;
var (selectedGrid, selectedGridButton) =
value switch {
Tab.Numbers => (Numbers, NumbersButton),
Expand All @@ -43,6 +42,27 @@ public Tab CurrentTab {
_tab = value;
}
}
public void SetButtonsTextColor(Color color, Color? placeholderRestingColor = null, Color? placeholderActiveColor = null) {
foreach (var button in new MathButton[] { ShiftButton, ShiftCapitalsButton }.Concat(TabButtons))
button.TextColor = color;
foreach (var button in new[] { LeftButton, RightButton}
.Concat(ButtonGrids.SelectMany(grid => grid.Children)
.Where(child => child is MathInputButton button && button.Input != MathKeyboardInput.Backspace)
.Cast<MathInputButton>())) {
button.TextColor = color;
button.PlaceholderRestingColor = placeholderRestingColor;
button.PlaceholderActiveColor = placeholderActiveColor;
button.ButtonDraw(/* If the LaTeXSettings change but the button's appearance properties don't, there's no event that causes the execution of this method. */);
};
}
public void SetClearButtonImageSource(ImageSource imageSource) {
foreach(var button in ButtonGrids.SelectMany(grid => grid.Children)
.OfType<ImageSourceMathInputButton>()
.Where(button => button.Input == MathKeyboardInput.Clear))
button.Source = imageSource;
}
MathButton[] TabButtons => new[] { NumbersButton, SymbolsButton, FunctionsButton, OperationsButton, LettersButton };
Grid[] ButtonGrids => new[] { Numbers, Symbols, Functions, Operations, Letters, LettersCapitals };
}
[AcceptEmptyServiceProvider]
public class SwitchToTabExtension : IMarkupExtension<Command> {
Expand All @@ -54,4 +74,4 @@ public Command ProvideValue(IServiceProvider _) =>
new Command(() => Self.CurrentTab = Target);
object IMarkupExtension.ProvideValue(IServiceProvider _) => ProvideValue(_);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using CSharpMath.SkiaSharp;
using SkiaSharp;
using SkiaSharp.Views.Forms;
using Xamarin.Forms;
Expand All @@ -13,14 +16,13 @@ public EditorPage() {
}
}
public class EditorView : ContentView {
public MathPainter OutputMathPainter = new MathPainter { TextColor = SKColors.Black };
MathKeyboard keyboard = new MathKeyboard(Rendering.FrontEnd.PainterConstants.LargerFontSize);
public EditorView() {
// Basic functionality
var view = new SKCanvasView { HeightRequest = 225 };
var keyboard = new MathKeyboard(Rendering.FrontEnd.PainterConstants.LargerFontSize);
var viewModel = keyboard.Keyboard;
viewModel.BindDisplay(view, new SkiaSharp.MathPainter {
TextColor = SKColors.Black
}, new SKColor(0, 0, 0, 153));
viewModel.BindDisplay(view, OutputMathPainter, new SKColor(0, 0, 0, 153));

// Input from physical keyboard
var entry = new Entry {
Expand Down Expand Up @@ -84,6 +86,7 @@ static View GridItem(int row, int col, View view) {
new StackLayout {
Orientation = StackOrientation.Horizontal,
Children = {
new Button { Text = "Change appearance", Command = new Command(ChangeAppearance) },
entry,
new Button {
Text = "Reset answer pan",
Expand All @@ -96,5 +99,44 @@ static View GridItem(int row, int col, View view) {
}
};
}
int CurrentThemeIndex = 0;
public void ChangeAppearance() {
CurrentThemeIndex = (CurrentThemeIndex + 1) % Themes.Count;
Themes[CurrentThemeIndex].Invoke();
keyboard.Keyboard.InsertionIndex = keyboard.Keyboard.InsertionIndex; // Hack to redraw placeholders in the output.
}
IList<Action> Themes => new Action[] {
() => { // This theme is the default. For a round-trip through the themes we need to set them again:
OutputMathPainter.TextColor = SKColors.Black;
Atom.LaTeXSettings.PlaceholderBlinks = false;
Atom.LaTeXSettings.PlaceholderActiveColor = null;
Atom.LaTeXSettings.PlaceholderRestingColor = null;
Atom.LaTeXSettings.PlaceholderActiveNucleus = "■";
Atom.LaTeXSettings.PlaceholderRestingNucleus = "□";
keyboard.SetButtonsTextColor(Color.Black);
keyboard.SetClearButtonImageSource("Controls/ImageSourceMathInputButtons/recyclebin.png");
},
() => {
UseMyCustomizedPlaceholderAppearance();
keyboard.SetButtonsTextColor(Color.Black); // Placeholder appearance on the keys is the same as in the output by default.
keyboard.SetClearButtonImageSource("Controls/ImageSourceMathInputButtons/metaltrashcan.png");
},
() => {
Atom.LaTeXSettings.PlaceholderBlinks = true;
OutputMathPainter.TextColor = SKColors.DarkGreen;
UseMyCustomizedPlaceholderAppearance();
// If you'd like to use different keyboard colors than output colors and you specified a placeholder color,
// probably you'll not want to use the same placeholder color on the keyboard:
keyboard.SetButtonsTextColor(Color.Brown, CalculateMyPlaceholderRestingColorFromSurroundingTextColor(Color.Brown));
keyboard.SetClearButtonImageSource("Controls/ImageSourceMathInputButtons/flame.png");
}
};

public void UseMyCustomizedPlaceholderAppearance() {
// You could also customize the "Active" placeholder nucleus and color, but for this example we don't.
Atom.LaTeXSettings.PlaceholderRestingNucleus = "■";
Atom.LaTeXSettings.PlaceholderRestingColor = CalculateMyPlaceholderRestingColorFromSurroundingTextColor(OutputMathPainter.TextColor.ToFormsColor());
}
public static Color CalculateMyPlaceholderRestingColorFromSurroundingTextColor(Color textColor) => textColor.WithLuminosity(textColor.Luminosity > 0.5 ? 0.2 : 0.8);
}
}
Loading

0 comments on commit 23e681b

Please sign in to comment.