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

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

Merged
merged 45 commits into from
Jun 21, 2021

Conversation

SymboLinker
Copy link
Contributor

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.

… 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.
@codecov-commenter
Copy link

codecov-commenter commented Sep 15, 2020

Codecov Report

Merging #164 into master will increase coverage by 0.38%.
The diff coverage is 85.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #164      +/-   ##
==========================================
+ Coverage   86.71%   87.09%   +0.38%     
==========================================
  Files         157      159       +2     
  Lines       11200    11261      +61     
==========================================
+ Hits         9712     9808      +96     
+ Misses       1488     1453      -35     
Impacted Files Coverage Δ
CSharpMath.Forms/Buttons.cs 64.70% <57.14%> (+64.70%) ⬆️
CSharpMath.Forms.Tests/ButtonTests.cs 100.00% <100.00%> (ø)
CSharpMath.Forms/LatexHelper.cs 100.00% <100.00%> (ø)
CSharpMath.Forms/MathInputButton.cs 100.00% <100.00%> (+100.00%) ⬆️
CSharpMath/Atom/MathList.cs 88.50% <0.00%> (+2.29%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 43b209b...bca8b4d. Read the comment docs.

@Happypig375
Copy link
Collaborator

Not a fan of use-once functions. @FoggyFinder?

@Happypig375
Copy link
Collaborator

Also would be nice if some new tests related to this change are added to CSharpMath.Xaml.Tests.

@FoggyFinder
Copy link
Collaborator

Not a fan of use-once functions. @FoggyFinder?

Yep, as well

@SymboLinker
Copy link
Contributor Author

SymboLinker commented Sep 16, 2020 via email

…tton

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).
@SymboLinker
Copy link
Contributor Author

SymboLinker commented Sep 16, 2020

Oops, I didn't read "CSharpMath.Xaml.Tests".

While trying to find out where to write the unit tests, I was in doubt about the "Xaml" meaning. I thought that test project was meant for tests more related to the xml. I have created a test project "CSharpMath.Forms.Tests" in the "Xamarin.Forms" folder.

  • Shall I move the tests and delete the newly created test project?
  • I will add at least one more test for the bindable TextColorProperty (as soon as I find the time, which may be between today and monday), to the Xaml test project.

Copy link
Collaborator

@Happypig375 Happypig375 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm, if you create a new test project then make sure to include it in CSharpMath.CrossPlatform.slnf as well.

…nclude 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".
SymboLinker and others added 4 commits September 18, 2020 02:18
…m 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.)
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).
@SymboLinker
Copy link
Contributor Author

SymboLinker commented Sep 18, 2020

I really appreciate the feedback you are providing. This is the first pull request I've ever done, and I am surprised about the quick responses and thorough reviews, providing kind and helpful suggestions to make the code compact and clean. You also care about the code quality even more than I'd expected - commenting about a white line for example and using do() => bla; instead of do() { bla;} - and I like that.

Copy link
Collaborator

@Happypig375 Happypig375 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's because I am a victim of dead air in pull requests...
kostub/iosMath#142
LayoutFarm/Typography#192
fsharp/fslang-design#442
MicrosoftDocs/visualfsharpdocs#326
And you will most definitely face similar scenarios as well in the future.

Regarding code style, thanks - less code means fewer chances of bugs.

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

Maybe I should only have committed "Add MathInputButton_Command unit test and do the suggested cleaning 99417ce".
I thought "let's use the Keyboard getter to increase test coverage", but the intention of the test is more clear without it. I will revert the last two commits.

@SymboLinker SymboLinker marked this pull request as ready for review October 31, 2020 14:33
@FoggyFinder
Copy link
Collaborator

Does it still occur for you?

Yep, still the same

- 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).
@FoggyFinder
Copy link
Collaborator

No luck

@Happypig375 @charlesroddie any idea what's going on? Does theme changing work correctly for you?

@SymboLinker
Copy link
Contributor Author

Core Tests results:

Total tests: 937
Passed: 933
Skipped: 4

The test fail errors are not related to this PR.

Test Core fail reason:

Error: Codecov failed with the following error: The process '/bin/bash' failed with exit code 2

Test iOS fail reason:

2020-11-01 08:20:56.474 CSharpMath.Ios.Tests[2654:25586] [FAIL] CSharpMath.Ios.Tests.Tests.MathDisplay(file: "ItalicAlignment", latex: "\colorbox{yellow}P\\\begin{array}{r}\colorbox"...) : Assert.InRange() Failure\r\nRange: (-1655.8 - 6525.8)\r\nActual: 6688
2020-11-01 08:20:56.474 CSharpMath.Ios.Tests[2654:25586] at CSharpMath.Ios.Tests.Tests.Test (System.String directory, System.Action`1[T] init, System.String file, System.String latex) [0x0020a] in /Users/runner/work/CSharpMath/CSharpMath/CSharpMath.Ios.Tests/Tests.cs:54

2020-11-01 08:20:55.897 CSharpMath.Ios.Tests[2654:25586] [FAIL] CSharpMath.Ios.Tests.Tests.MathInline(file: "ItalicAlignment", latex: "\colorbox{yellow}P\\\begin{array}{r}\colorbox"...) : Assert.InRange() Failure\r\nRange: (-1655.8 - 6525.8)\r\nActual: 6688
2020-11-01 08:20:55.897 CSharpMath.Ios.Tests[2654:25586] at CSharpMath.Ios.Tests.Tests.Test (System.String directory, System.Action`1[T] init, System.String file, System.String latex) [0x0020a] in /Users/runner/work/CSharpMath/CSharpMath/CSharpMath.Ios.Tests/Tests.cs:54

@SymboLinker
Copy link
Contributor Author

@FoggyFinder I suspect that it is solved now via commit 486ac0f and I hope that calling ButtonDraw during each theme change in the example project is acceptable. Officially it is only needed to go from theme 1 to theme 2.

@Happypig375
Copy link
Collaborator

@FoggyFinder @SymboLinker I'll test this PR tomorrow

@SymboLinker
Copy link
Contributor Author

@FoggyFinder @SymboLinker I'll test this PR tomorrow

@Happypig375 It's more than half a year later now. Could you please share why this PR has not been tested / has not gotten any response? I would like to know that, so that I can do better next time (in other public repositories).

@Happypig375
Copy link
Collaborator

It's just me focusing on other projects than this. I'll just merge this.

@Happypig375 Happypig375 requested a review from FoggyFinder June 13, 2021 10:25
@Happypig375
Copy link
Collaborator

@FoggyFinder Just missing your approval

Copy link
Collaborator

@FoggyFinder FoggyFinder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Color doesn't return back to Black in theme switching. I'll take a look soon and write my thoughts / questions

@FoggyFinder FoggyFinder self-assigned this Jun 14, 2021
@FoggyFinder
Copy link
Collaborator

It's more than half a year later now. Could you please share why this PR has not been tested / has not gotten any response? I would like to know that, so that I can do better next time (in other public repositories).

I'm doing some testing. I totally newbie with Regex so getting help with those who know at least something (pretty much to my opinion). Expect feedback / commit with fix proposal or. at least. tests that are failing within 1-2 days.

@SymboLinker
Copy link
Contributor Author

SymboLinker commented Jun 15, 2021

@FoggyFinder

  • Why do you need RegEx? Is it because of the use of Regex.Matches(latex, LaTeXSettings.PlaceholderActiveNucleus).Count > 1 in the MathInputButton? That is actually a bug, I realize now! (The intention of that piece of code was to count the number of placeholders. I actually should not have used the Regex class there, because my intention was only to count the number of occurrences of a normal string. That's dangerous however, because LaTeX uses characters that may result in actual meaningful regular expressions. It was lazy to use that method instead of writing a simple "substring counting method" - I believe there are no such methods ready available.)
  • Whenever I "think" I need to use regular expressions, I remind myself of a joke about Regex: "Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems." (I try to avoid using Regex. Often, there are better/cleaner ways to solve problems without Regex.)
  • If I do use it, I mostly Google a "C# Regex cheat sheet" (for example: mikesdotnetting). There are multiple versions of Regex. Some languages outside of .NET may have a slightly different syntax.
  • Regex.Replace and Regex.Matches are examples of .NET methods that you can use to transform those Regex strings into doing something.

@FoggyFinder
Copy link
Collaborator

Exactly. I thought "Aha. Now I realize why it behaves like this. And then - I can write simple Regex to fix it quickly. Well it ended with debug why a regex doesn't work properly in some case and then another regex and so on until the point when I totally no idea what the regex pattern means itself".

Indeed I prefer not to use Regex and not only I'm very bad with them. But...you've been waiting for so long...my intention was to fix it ASAP, merge in and then, probably, rewrite without Regex.

@FoggyFinder
Copy link
Collaborator

The issue with display (like I saw brown color despite the theme is black) is the fact that DrawButton calls repeatedly in short period of time:

        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. */);

So when we store "original" latex in a variable

    var originalLatexString = painter.LaTeX;

it might be previously mutated LaTeX that actually contains color etc attributes.

@FoggyFinder
Copy link
Collaborator

I'm not a big fan of lock either but it was much easier than applying odd regex to prevent latex corruption.
Well, returning to black theme still shows light-red border in frac, ... but no visual glitch

@SymboLinker
Copy link
Contributor Author

SymboLinker commented Jun 20, 2021

EDIT: It surprises me that calling a synchrounous method multiple times synchrounously would require a lock to work. I guess that - for example - a part of a painting method is asynchrously without exposing that in its signature. I’m glad that you’ve found a fix. Thanks for investing the time :)

@codecov-commenter
Copy link

codecov-commenter commented Jun 20, 2021

Codecov Report

Merging #164 (db93ac9) into master (892eaec) will increase coverage by 0.33%.
The diff coverage is 83.69%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #164      +/-   ##
==========================================
+ Coverage   87.34%   87.68%   +0.33%     
==========================================
  Files         156      161       +5     
  Lines       11329    11576     +247     
==========================================
+ Hits         9895    10150     +255     
+ Misses       1434     1426       -8     
Impacted Files Coverage Δ
CSharpMath.Forms.Tests/ButtonTestsHelper.cs 0.00% <0.00%> (ø)
CSharpMath.Forms/Buttons.cs 61.11% <53.57%> (+61.11%) ⬆️
CSharpMath.Forms.Tests/ButtonTests.cs 88.88% <88.88%> (ø)
CSharpMath.Forms/MathInputButton.cs 96.07% <97.82%> (+96.07%) ⬆️
CSharpMath.Forms/LatexHelper.cs 100.00% <100.00%> (ø)
...pMath.Forms/NullableColorBindablePropertyHelper.cs 100.00% <100.00%> (ø)
CSharpMath.Forms/StringHelper.cs 100.00% <100.00%> (ø)
CSharpMath.Rendering/Text/TextLaTeXParser.cs 87.76% <0.00%> (-0.51%) ⬇️
CSharpMath.Editor/MathKeyboard.cs 88.81% <0.00%> (-0.22%) ⬇️
... and 8 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 892eaec...db93ac9. Read the comment docs.

Copy link
Collaborator

@FoggyFinder FoggyFinder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've left one comment can you take a look on it? Anyway, approved.

@FoggyFinder FoggyFinder merged commit 23e681b into verybadcat:master Jun 21, 2021
@FoggyFinder FoggyFinder added the Resolution/Implemented The described enhancement or housekeeping work has been implemented. label Jun 21, 2021
@SymboLinker
Copy link
Contributor Author

Thanks @Happypig375 and @FoggyFinder!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Resolution/Implemented The described enhancement or housekeeping work has been implemented. Type/Enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants