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

How to update font size of an empty line? #149

Closed
JordanMartinez opened this issue Jun 8, 2015 · 6 comments
Closed

How to update font size of an empty line? #149

JordanMartinez opened this issue Jun 8, 2015 · 6 comments

Comments

@JordanMartinez
Copy link
Contributor

Hello Tomas,
This relates to #148. I am trying to change the font size of an area's text and line number, so that they are the same.
I'm using the style-remapping approach (#142) to update the text's style. I learned that, after remapping, the area only updates the text and line numbers if the mapper returns a new Style object. I can return a new Style object for every line that has content or text, but I can't do that if it's an empty line.

For example, the sixth line below (a blank line) does not update when I use this approach.
hello_017

@TomasMikula
Copy link
Member

Sorry for late response on this. Style information is only associated with text (that is, characters). No text means no way of specifying style. This will change when paragraph-level styles are supported (#6).

To change the default font size of the whole text area, setting the -fx-font-size property on the area should work. See the FontSizeSwitcher demo.

Binding line number font size to the value from your drop-down menu should work, independently of any style remapping. Do you mind sharing the code of your line number factory?

@JordanMartinez
Copy link
Contributor Author

No worries! The lateness was an exception, not the norm. Thanks for the response.

I was using an InlineStyleTextArea that was initialized with a style object that could restyle the font size of the text. There was a second object that styled any text a user typed into the area. That was why I was using the style remapping approach.

However, if the style object's toCss() method specifies a font size, the area.setStyle("-fx-font-size:" + size) approach won't work. Once I removed the font size option from my style object's ``toCss()` method, the above approach worked fine. Fortunately, since both of my style objects would style the text's font size to the same value anyway, this part of my style object is easily fixed.

@JordanMartinez
Copy link
Contributor Author

The LineNumberFactory (LNF) below was a quick test solution. The code below shows where I made minor modifications to yours and isn't the full class.
No doubt the code could be better optimized. If I use the styles remapping approach above, it only updates the label's font size in the LNF of the lines that haven't been rendered yet. Only when a user scrolls, so that the labels that were styled to the previous font size are hidden, and then scrolls again, so that these labels are shown again (and thus re-rendered), will it properly display the labels as the updated font size.

// DEFAULT_FONT was changed from a Font into a Closure (which is basically a method)
private static final Closure<Font> DEFAULT_FONT = { double size ->
        return Font.font("monospace", FontPosture.ITALIC, size)
    }

// the property used to determine the font size is held within a class of all static variables.
// when calling its apply method, I reference that property like so:
public Node apply(int idx) {
        Label lineNo = new Label()

        // 'UserSettings.fontSize.get()' was abbreviated to 'UserSettings.fontSize()'
        lineNo.setFont(DEFAULT_FONT(UserSettings.fontSize())) 

        lineNo.setBackground(DEFAULT_BACKGROUND)
        lineNo.setTextFill(DEFAULT_TEXT_FILL)
        lineNo.setPadding(DEFAULT_INSETS)

To create a Line Number Factory that also updates according to a font size change, could it...

  • include a Property that is used instead for determining the Default Font's font size, and thus
  • include another Constructor that can be passed such a Property as one of it's parameters?

@TomasMikula
Copy link
Member

However, if the style object's toCss() method specifies a font size, the area.setStyle("-fx-font-size:" + size) approach won't work.

Yes, the CSS returned from toCss() will override the CSS on the area, because the CSS from toCss() will be set on the specific Text node; standard CSS precedence rules apply, namely, CSS set on a child node takes precedence over CSS inherited from a parent.

If your style object has the capacity to override font size, but in a particular case does not (e.g. has null or empty Optional in place of font size), then the toCss() function does not need to return "-fx-font-size" in the result at all, letting the font size set on the area take effect.

include a Property that is used instead for determining the Default Font's font size, and thus

Yes, conceptually this:

lineNo.fontProperty().bind(UserSettings.fontSizeProperty);

but in order to make sure that the line numbers do not keep being updated (and thus be prevented from garbage collection) after they disappear from the scene, use this trick where the binding to fontSizeProperty depends on the label being part of the scene:

lineNo.fontProperty().bind(Val.flatMap(
        lineNo.sceneProperty(),
        scene -> scene != null
                ? UserSettings.fontSizeProperty
                : Val.constant(null)));

Same trick is already used in the original LineNumberFactory with observing area's total number of lines.

@JordanMartinez
Copy link
Contributor Author

(Sorry for the late reply. I've been sick the past few days.)

The above did the trick. Thank you again!

@TomasMikula
Copy link
Member

No need to be sorry. Get well soon!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants