mkpaz / atlantafx

Modern JavaFX CSS theme collection with additional controls.
https://mkpaz.github.io/atlantafx
MIT License
791 stars 64 forks source link

Editable text field table cells style doesn't reflect the editing state after the first time the value is committed #79

Closed giraciopide closed 1 month ago

giraciopide commented 11 months ago

Can be easily reproduced using the latest release of the sampler.

This is a table cell while being edited the first time. image

If the value is committed by pressing enter (does't really matter if the value is changed or not... in the screenshot it does since it's the most common scenario), starting with the next time the cell enters edit mode, the style won't reflect the editing state. image

By looking at the second image it's not obvious at all that the cell is being edited, but it is, and can be committed again.

I'm not familiar with java-fx skinning/theming. If someone has pointers to where one could look at, it would be appreciated.

giraciopide commented 11 months ago

I investigated a bit using scenic view and it seems that after the first commit, the TableCellView doesn't receive the :focus-within pseudo state (which instead is there when it styled properly during the first edit).

Looking at data.scss I see that this is a bit of a can of worms... maybe TextFieldTableCell is just bugged in java-fx.

I would be happy to work-around it with some custom styling on my side, but using TextFieldTableCell I see no ways to style the different editing states (looking at scenic view i see no classes changing depending on the cell state... ).

I'll investigate into extending TextFieldTableCell or coming up with an equivalent implementation that at least exposes something about the editing state in a class that can be targeted for some styling.

giraciopide commented 11 months ago

I crudely worked around it, leaving it here as someone may find this useful.

The style that is not getting applied (because of the missing :focus-within) is this: https://github.com/mkpaz/atlantafx/blob/07f36be7add7f93b8e8457fe248fdbe568b26ccb/styles/src/components/_data.scss#L166-L175

As I'm building the styles myself from source (since I need to do some tweaking) I just added a variant with a arbitrary css class of mine: being-edited:

.table-view>.virtual-flow>.clipped-container>.sheet>.table-row-cell .text-field-table-cell.being-edited,
.table-view>.virtual-flow>.clipped-container>.sheet>.table-row-cell .text-field-table-cell:focus-within {
  -fx-background-insets: 0, 1, 2;
  -fx-background-color: -color-bg-default, -color-accent-emphasis, -color-bg-default;
}

Then, this is how I create the cells (to be used in a cell factory):

    public static <R, V> TextFieldTableCell<R, V> newTextFieldTableCell(StringConverter<V> converter) {
        final TextFieldTableCell<R, V> cell = new TextFieldTableCell<>(converter);
        final ObservableList<String> styleClasses = cell.getStyleClass();
        cell.editingProperty().addListener((obs, wasEditing, isEditing) -> {
            if (isEditing) {
                styleClasses.add("being-edited");
            } else {
                styleClasses.remove("being-edited");
            }
        });
        return cell;
    }

If there are better ways, feel free to share.

As for the issue... I think it may be a java-fx bug. I honestly don't know what atlantafx could do better: i just think that :focus-within might not be reliable at all for table cells.

mkpaz commented 1 month ago

I think it was fixed by JDK-8313956.