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

Toogle switch for boolean settings #90

Merged
merged 28 commits into from
Oct 30, 2024
Merged

Conversation

JonasSchaub
Copy link
Collaborator

Draft pull request for Zeynep's work on #66

@JonasSchaub JonasSchaub added the enhancement New feature or request label Apr 19, 2024
Copy link
Collaborator Author

@JonasSchaub JonasSchaub left a comment

Choose a reason for hiding this comment

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

The switch looks very nice and the code as well, all in all.
But it should be more configurable from outside the class, e.g. by setting sizes and colors. See code comments.

this.switchTransition = new ParallelTransition(this.switchAnimation, this.fillAnimation);
this.switchAnimation.setNode(this.switchButton);
this.fillAnimation.setShape(this.switchBackground);
getChildren().addAll(this.switchBackground, this.switchButton);
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Missing "this." statement

/**
* Boolean property to keep track of the state.
*/
private final SimpleBooleanProperty switchedOn;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Consider better variable naming, e.g. sth like "switchStateBooleanProperty"

this.switchTransition.play();
});
//Mouse listener.
setOnMouseClicked(event -> this.switchedOn.set(!this.switchedOn.get()));
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Missing "this." statement.

return this.switchedOn;
}
/**
* returns isSwitchedOn to change boolean state to true.
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Why is sth changed here? Or what does the comment mean?

import javafx.util.Duration;

/**
* A toggle switch to en- and disable features in settings.
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Can you describe your switch, what it does, etc. a little more here? Like, it's a circle on top of a rectangle, the transition is animated, the state on vs off is shown by coloring the rectangle differently, resizing is not implemented (yet), ...

* https://youtu.be/maX5ymmQixM?si=v2ULa57-pjCmoQlf, 05/17/2024, 10:33
*/
super();
this.switchedOn = new SimpleBooleanProperty(false);
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Would be nice to turn the initial state into a constructor parameter and implement a separate parameter-less constructor calling this one here with a defined initial state (off/false).

this.switchButton.setEffect(new DropShadow(5, Color.GRAY));
this.switchAnimation = new TranslateTransition(Duration.seconds(0.25));
this.switchAnimation.setNode(this.switchButton);
this.fillAnimation = new FillTransition(Duration.seconds(0.25));
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Size parameters like the rectangle size, the circle size, etc., should be configurable via setters (getters are then also needed, of course). Also the colors and transition times. The values you are using here should be defined in class constants and used as defaults. Shadows should be able to be turned off and on.

If we would do this more professionally, we would also define resizing and how the switch behaves for a given min/preferred/max height/width. But this goes beyond our current use case.
But at least, that these behaviours are not implemented should be noted in the class documentation because e.g. the setPrefHeight() method is defined by the extended Control class but your switch behaves weirdly if it is used.

//Listener
this.switchedOn.addListener((observable, oldValue, newValue) -> {
boolean tmpIsOn = newValue.booleanValue();
this.switchAnimation.setToX(tmpIsOn ? (44 - 18) : 0);
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

These values refer to the rectangle sizes, right? Then please query them from the rectangle instead of writing them explicitly again.

tmpBooleanComboBox.setPrefWidth(GuiDefinitions.GUI_TEXT_FIELD_PREF_WIDTH_VALUE);
tmpBooleanComboBox.setMaxWidth(GuiDefinitions.GUI_SETTINGS_TEXT_FIELD_MAX_WIDTH_VALUE);
tmpBooleanComboBox.getItems().addAll(Boolean.FALSE, Boolean.TRUE);
tmpBooleanComboBox.valueProperty().bindBidirectional(tmpSimpleBooleanProperty);
tmpBooleanComboBox.setTooltip(tmpTooltip);
//add to gridpane
aGridPane.add(tmpBooleanComboBox, 1, tmpRowIndex++);
GridPane.setMargin(tmpBooleanComboBox, new Insets(GuiDefinitions.GUI_INSETS_VALUE));
GridPane.setMargin(tmpBooleanComboBox, new Insets(GuiDefinitions.GUI_INSETS_VALUE));*/
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The to do comment "implement toggle switch here" and the commented-out code for the old combo box solution can be removed now.

/**
* Default value for the height of the Background.
*/
private static final int RECTANGLE_WIDTH_VALUE = 45;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The default values should be public.

/**
* Default value for the width of the Background.
*/
private static final int RECTANGLE_HEIGHT_VALUE = 18;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

You mixed up width and height in the comments here and above.

*/
private static final int RECTANGLE_POSITION_VALUE = -50;
/**
* Default value for the Layout which sets the position of the button on the x-axis.
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

... in "off" position?

/**
* Default color for the background when turned on.
*/
private static final Color RECTANGLE_COLOR_ON = Color.web("#0099cc");
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Could you add in the comment what color that hex value represents?

*
* @return switch value
*/
public boolean getSwitchStateBooleanProperty() {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'd rename this to getSwitchState().

*
* @return BooleanProperty
*/
public BooleanProperty valueProperty() {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'd rename this to getSwitchStateProperty().

Copy link

sonarcloud bot commented Jun 29, 2024

@JonasSchaub JonasSchaub marked this pull request as ready for review October 30, 2024 16:35
Copy link

sonarcloud bot commented Oct 30, 2024

@JonasSchaub JonasSchaub linked an issue Oct 30, 2024 that may be closed by this pull request
@JonasSchaub JonasSchaub merged commit f14cf91 into production Oct 30, 2024
2 checks passed
@JonasSchaub JonasSchaub deleted the issue-66-toggle-switch branch October 30, 2024 16:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement a toogle switch for the boolean settings?
2 participants