如何使RichTextFx的外观和感觉类似于TextArea?

时间:2016-06-14 09:55:20

标签: javafx javafx-8 richtextfx

RichTextFx(顶级控件)默认情况下看起来不像TextArea(底部控件):

RichTextFX without border, corners and focus border

我找到了一种如何添加圆角的方法(只需从modena.css复制并配置some styles):

.virtualized-scroll-pane {
    -fx-padding: 0;
    -fx-cursor: default;
    -fx-background-color: linear-gradient(to bottom, derive(-fx-text-box-border, -10%), -fx-text-box-border),
        derive(-fx-base,-1%);
}
.virtualized-scroll-pane {
    -fx-background-color: null;
}
.virtualized-scroll-pane > .scroll-bar:horizontal {
    -fx-background-radius: 0 0 2 2;
}
.virtualized-scroll-pane > .scroll-bar:vertical {
    -fx-background-radius: 0 2 2 0;
}
.virtualized-scroll-pane > .corner {
    -fx-background-radius: 0 0 2 0;
}
.virtualized-scroll-pane .code-area {
    /*the is 1px less top and bottom than TextInput because of scrollpane border */
    -fx-padding: 0.25em 0.583em 0.25em 0.583em; /* 3 7 3 7 */
    -fx-cursor: text;
    -fx-background-color:
        linear-gradient(from 0px 0px to 0px 4px, derive(-fx-control-inner-background, -8%), -fx-control-inner-background);
    -fx-background-radius: 2;
}
.virtualized-scroll-pane:focused .code-area {
    -fx-background-color:
        -fx-control-inner-background,
        -fx-faint-focus-color,
        linear-gradient(from 0px 0px to 0px 5px, derive(-fx-control-inner-background, -9%), -fx-control-inner-background);
    -fx-background-insets: 0, 0, 2;
    -fx-background-radius: 2, 1, 0;
}

但是不明白如何在聚焦控制周围添加灰色边框和围绕聚焦控制的蓝色边框(即文本控件和滚动条周围)?

似乎配置了here聚焦控件的蓝色边框,但以下CSS不起作用:

.virtualized-scroll-pane {
    -fx-text-fill: -fx-text-inner-color;
    -fx-highlight-fill: derive(-fx-control-inner-background,-20%);
    -fx-highlight-text-fill: -fx-text-inner-color;
    -fx-prompt-text-fill: derive(-fx-control-inner-background,-30%);
    -fx-background-color: linear-gradient(to bottom, derive(-fx-text-box-border, -10%), -fx-text-box-border),
        linear-gradient(from 0px 0px to 0px 5px, derive(-fx-control-inner-background, -9%), -fx-control-inner-background);
    -fx-background-insets: 0, 1;
    -fx-background-radius: 3, 2;
    -fx-cursor: text;
    -fx-padding: 0.333333em 0.583em 0.333333em 0.583em; /* 4 7 4 7 */
}
.virtualized-scroll-pane:focused {
    -fx-highlight-fill: -fx-accent;
    -fx-highlight-text-fill: white;
    -fx-background-color:
        -fx-focus-color,
        -fx-control-inner-background,
        -fx-faint-focus-color,
        linear-gradient(from 0px 0px to 0px 5px, derive(-fx-control-inner-background, -9%), -fx-control-inner-background);
    -fx-background-insets: -0.2, 1, -1.4, 3;
    -fx-background-radius: 3, 2, 4, 0;
    -fx-prompt-text-fill: transparent;
}

据我所知,这是行不通的,因为.virtualized-scroll-pane并不重点关注。

Here是用于重现问题的Maven项目。

那么,如何修复呢?

仅供参考:RichTextFX 0.7-M1,JDK 1.8.0_91-b14

3 个答案:

答案 0 :(得分:2)

您可以手动切换focused的{​​{1}}伪类,然后您可以在CSS中使用VirtualizedScrollPane选择器:

.virtualized-scroll-pane:focused

答案 1 :(得分:2)

我找到了以下方法来自定义RichTextFx:

对于0.6.10版本:

.code-area {
    -fx-background-color: linear-gradient(to bottom, derive(-fx-text-box-border, -10%), -fx-text-box-border),
        linear-gradient(from 0px 0px to 0px 5px, derive(-fx-control-inner-background, -9%), -fx-control-inner-background);
    -fx-background-insets: 0, 1;
    -fx-background-radius: 3, 2;
    -fx-padding: 0.041666625em;
}

.code-area:focused {
    -fx-background-color:
        -fx-focus-color,
        -fx-control-inner-background,
        -fx-faint-focus-color,
        linear-gradient(from 0px 0px to 0px 5px, derive(-fx-control-inner-background, -9%), -fx-control-inner-background);
    -fx-background-insets: -0.2, 1, -1.4, 3;
    -fx-background-radius: 3, 2, 4, 0;
}

.code-area .scroll-bar:horizontal {
    -fx-background-radius: 0 0 2 2;
}

.code-area .scroll-bar:vertical {
    -fx-background-radius: 0 2 2 0;
}

richtextfx-0.6.10-without-selection richtextfx-0.6.10-with-selection

对于0.7-M1(修复了#39;焦点'伪类):

.virtualized-scroll-pane {
    -fx-background-color: linear-gradient(to bottom, derive(-fx-text-box-border, -10%), -fx-text-box-border),
        linear-gradient(from 0px 0px to 0px 5px, derive(-fx-control-inner-background, -9%), -fx-control-inner-background);
    -fx-background-insets: 0, 1;
    -fx-background-radius: 3, 2;
}

.virtualized-scroll-pane:focused {
    -fx-background-color:
        -fx-focus-color,
        -fx-control-inner-background,
        -fx-faint-focus-color,
        linear-gradient(from 0px 0px to 0px 5px, derive(-fx-control-inner-background, -9%), -fx-control-inner-background);
    -fx-background-insets: -0.2, 1, -1.4, 3;
    -fx-background-radius: 3, 2, 4, 0;
}

/* 
I don't understand why this rule fix problem with border around CodeArea.
May be somebody can explain it.
*/
.virtualized-scroll-pane .styled-text-area {
    -fx-background-insets: 0px;
}

.virtualized-scroll-pane .scroll-bar:horizontal {
    -fx-background-radius: 0 0 2 2;

    -fx-padding: 0 0.08333325em 0.08333325em 0.08333325em;
    -fx-border-insets: 0 0.08333325em 0.08333325em 0.08333325em;
    -fx-background-insets: 0 0.08333325em 0.08333325em 0.08333325em;
}

.virtualized-scroll-pane .scroll-bar:vertical {
    -fx-background-radius: 0 2 2 0;

    -fx-padding: 0.08333325em 0.08333325em 0.08333325em 0;
    -fx-border-insets: 0.08333325em 0.08333325em 0.08333325em 0;
    -fx-background-insets: 0.08333325em 0.08333325em 0.08333325em 0;
}

richtextfx-0.7-M1-without-selection richtextfx-0.7-M1-with-selection

repository分隔分支中的完整版本。

未解决的问题:

  • 滚动条尺寸
  • 右下角的方形颜色
  • 在文本顶部和顶部缩进

可能有人可以提供更好的解决方案吗?

答案 2 :(得分:0)

对于右下角广场的颜色,请使用:

.virtualized-scroll-pane  .scroll-pane .filler {
    -fx-background-color: white;
}

.virtualized-scroll-pane  .scroll-pane .corner {
    -fx-background-color: white;
}