-
Notifications
You must be signed in to change notification settings - Fork 236
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
Implemented background colors for text #166
Conversation
|
||
public class TextExt extends Text { | ||
|
||
private ObjectProperty<Paint[]> backgroundColor = null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If only a single color is supported, the type should not lie about it. Let's just change it to ObjectProperty<Paint>
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That the way the JavaFX converter works. I was using Color instead of Paint[] before, but the CSS converter injected a Paint array anyway which led to casting errors along the way.
It's the same way in the internal implementation in the Background class. All I can think of is making the property private and expose a Paint property which hides the implementation details.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
StyleConverter.getPaintConverter() should do the job.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried the ColorConverter before with the mentioned behavior. But I will give this one a try too, thanks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nope sorry, this one has the same behavior. Under the hood it still injects a Paint array which leads to casting errors later on:
Exception in thread "JavaFX Application Thread" java.lang.ClassCastException: [Ljavafx.scene.paint.Paint; cannot be cast to javafx.scene.paint.Paint
Maybe I'm just too stupid to make it work so don't hold back if you want to try your luck.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I reported a bug: https://bugs.openjdk.java.net/browse/JDK-8133685
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ranming it might cause confusion for the user who expects -fx-background-color.
So we can either just keep it like it is and tolerate the unnecessary Paint array. Or we rename it.
-fx-background-fill would be a good choice since it follows -fx-highlight-fill. It's your call.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is another problem and that is that com.sun.*
is not going to be visible from JDK 9 on. So unless you can implement it without com.sun.*
, I would say go with -fx-background-fill
. (Sure, for now you could probably get away with just giving null
as the converter, since it is overridden anyway, but that would stop working when/if they fix the bug).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just changed the code based on your last comment. I will test the latest version tonight a bit more.
My next pull request will be about line styles (think red background for lines with break point or or yellow background for current selected line). Any suggestions to make it more likely to get merged upstream?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A nifty customisation would be controlling line style and background offset
from/around text. This will enable highlighting text in different ways. Not
sure how hard it is to implement though.
In my mind, one of the things to consider is how the background will work
when making text selection. For example, if selection overlaps an already
coloured text background?
I look forward to plying with your addition :)
On Aug 17, 2015 3:58 AM, "Mewes Kochheim" [email protected] wrote:
In richtextfx/src/main/java/org/fxmisc/richtext/skin/TextExt.java
#166 (comment):+import javafx.beans.property.ObjectProperty;
+import javafx.css.CssMetaData;
+import javafx.css.Styleable;
+import javafx.css.StyleableObjectProperty;
+import javafx.css.StyleableProperty;
+import javafx.scene.paint.Color;
+import javafx.scene.paint.Paint;
+import javafx.scene.text.Text;
+
+import java.lang.reflect.Field;
+import java.util.ArrayList;
+import java.util.List;
+
+public class TextExt extends Text {
+
- private ObjectProperty<Paint[]> backgroundColor = null;
I just changed the code based on your last comment. I will test the latest
version tonight a bit more.My next pull request will be about line styles (think red background for
lines with break point or or yellow background for current selected line).
Any suggestions to make it more likely to get merged upstream?—
Reply to this email directly or view it on GitHub
https://github.com/TomasMikula/RichTextFX/pull/166/files#r37176819.
Great job! See my comments above. |
Implemented background colors for text
Looks good, thanks for the contribution! @melkhaldi so you would want the equivalent of the @MewesK Regarding styling the whole lines, let's move the discussion to #6. |
I have modified the RichText demo to support background color. |
Hi Maher, I haven't uploaded new jars. You would have to build from source at this point. It is just 3 commands, if you have Gradle installed:
|
I will make a new release later. |
oh alright, i thought it was uploaded already. Don't have gradle set up yet On Mon, Aug 17, 2015 at 1:12 PM, Tomas Mikula [email protected]
|
I followed Tomas' instructions to set it up from the source. It works good! Great job guys! Here's one idea for a slight enhancement (though it isn't a high priority at all). When the background-fill is blue, it's hard to distinguish it from a selected text (see the highlighted, selected example below). This could be improved further if the selection color (which defaults to blue) uses a different color when selecting a text that has a similarly-colored background-fill. I also noticed that a white vertical line appears between two texts with different background-fills, but that line is only visible if the text is rather large. Again, not really that big of an issue. (Edit: The black vertical line separated "text|text" is due to screenshotting the window when the caret was visible. This isn't some bug.) |
Hi Jordan, the white line is caused by the internal TextLayout class. We could of course just add 1px to the width of each BG shape to work around this "bug" but it may look "wrong" for small text. The second issue with two similar fill colors for BG and selection could be dealt with by using a slightly transparent color for the selection. (that's the way Mac OS is doing it at least) I'll play around with this idea a little when I#M at home. |
@MewesK I think the white line isn't a big enough issue to do that. However, if the situation iss at least known here, then a person who truly needs that to be gone could talk to you about how to adjust it or try doing it themselves... I'm not very familiar with Mac, so I'm not sure how that would work. I'm sure you'll come up with something :-) |
I believe this might be something that can be controlled via an inset
|
Regarding the white gap, we could also try to construct a minimal example, where |
Just as you suggested the implementation is based on the same principles as the selection fill.
On top of that I had to extend Text to support CSS based colors.
I should mention that I have only implemented -fx-background-color and not anything else from -fx-background. JavaFX under the hood is so damn weird that it would be rather ugly to support the actual Background property. Using just the color property makes the code simple and shouldn't have a huge impact like having an actual Image in every Text instance.
One thing I would like to improve is the background node instantiation. Right now every Text instance has a matching background Path. Maybe it is possible to create only Path instances when there actually is a background color set.
Well I'm actually quite happy with the result. It'S not perfect yet but it works great. The missing background color feature was a real show stopper for me. I'm glad that I was able to fix that. (taught me a lot about JavaFX)