Skip to content

Commit

Permalink
[#538] Add dark mode CSS to scene instead of root.
Browse files Browse the repository at this point in the history
  • Loading branch information
jkcoding7 authored and prmr committed Jul 2, 2024
1 parent 9720097 commit a9169cb
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 23 deletions.
4 changes: 3 additions & 1 deletion docs/functional/DarkMode.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ In JetUML, the CSS file is `DarkMode.css`, and it is applied to the scene of the
}
```

CSS style classes can be implemented in two ways, and both are used in `DarkMode.css`. One way is to use predefined JavaFX names mapped to CSS names. For instance, the `root` style class and its properties with the prefix `-fx-` are predefined JavaFX names derived from CSS names, and together, they provide the overall default colors for a node. Simply defining the style class and adding the stylesheet is enough. The other way is to define a custom style class. This requires an extra step, and the style class needs to be added by calling `Node#getStyleClass().add(String).
CSS style classes can be implemented in two ways, and both are used in `DarkMode.css`. One way is to use predefined JavaFX names mapped to CSS names. For instance, the `root` style class and its properties with the prefix `-fx-` are predefined JavaFX names derived from CSS names, and together, they provide the overall default colors for a node. Simply defining the style class and adding the stylesheet is enough. The other way is to define a custom style class. This requires an extra step, and the style class needs to be added by calling `Node#getStyleClass().add(String)` on the `Node` which requires the specific styling.

### Canvas and Diagram Elements

Expand All @@ -52,8 +52,10 @@ public enum ColorScheme
```

The following sequence diagram shows how `ColorScheme` and `GraphicsContext` is used to render text in diagrams:

![JetUML Sequence Diagram](DarkMode.sequence.png)


1. A StringRenderer object calls RenderingUtils#drawText.
2. The attributes of the GraphicsContext is saved.
3. The font is set.
Expand Down
1 change: 1 addition & 0 deletions src/org/jetuml/JetUML.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ public void start(Stage pStage) throws Exception
NotificationService.instance().setMainStage(pStage);

pStage.getScene().getStylesheets().add(getClass().getResource("JetUML.css").toExternalForm());
editor.booleanPreferenceChanged(UserPreferences.BooleanPreference.darkMode);

pStage.setOnCloseRequest(pWindowEvent ->
{
Expand Down
24 changes: 2 additions & 22 deletions src/org/jetuml/gui/EditorFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,6 @@ public EditorFrame(Stage pMainStage, Stage pDialogStage)
showWelcomeTabIfNecessary();

UserPreferences.instance().addBooleanPreferenceChangeHandler(this);
if( UserPreferences.instance().getBoolean(BooleanPreference.darkMode) )
{
getStylesheets().add(aDarkModeCSSPath);
aDialogStage.getScene().getStylesheets().add(aDarkModeCSSPath);
}

setOnKeyPressed(e ->
{
Expand Down Expand Up @@ -316,7 +311,6 @@ private void open(File pFile)
{
Alert alert = new DeserializationErrorAlert(exception);
alert.initOwner(aMainStage);
applyDarkTheme(alert);
alert.showAndWait();
}
}
Expand Down Expand Up @@ -438,7 +432,6 @@ private void close()
alert.initOwner(aMainStage);
alert.setTitle(RESOURCES.getString("dialog.close.title"));
alert.setHeaderText(RESOURCES.getString("dialog.close.title"));
applyDarkTheme(alert);
alert.showAndWait();

if(alert.getResult() == ButtonType.YES)
Expand Down Expand Up @@ -467,7 +460,6 @@ public void close(DiagramTab pDiagramTab)
alert.initOwner(aMainStage);
alert.setTitle(RESOURCES.getString("dialog.close.title"));
alert.setHeaderText(RESOURCES.getString("dialog.close.title"));
applyDarkTheme(alert);
alert.showAndWait();

if(alert.getResult() == ButtonType.YES)
Expand Down Expand Up @@ -506,7 +498,6 @@ private void save()
{
Alert alert = new Alert(AlertType.ERROR, RESOURCES.getString("error.save_file"), ButtonType.OK);
alert.initOwner(aMainStage);
applyDarkTheme(alert);
alert.showAndWait();
}
}
Expand Down Expand Up @@ -552,7 +543,6 @@ private void saveAs()
{
Alert alert = new Alert(AlertType.ERROR, RESOURCES.getString("error.save_file"), ButtonType.OK);
alert.initOwner(aMainStage);
applyDarkTheme(alert);
alert.showAndWait();
}
}
Expand Down Expand Up @@ -624,7 +614,6 @@ else if("bmp".equals(format)) // to correct the BufferedImage type
{
Alert alert = new Alert(AlertType.ERROR, RESOURCES.getString("error.save_file"), ButtonType.OK);
alert.initOwner(aMainStage);
applyDarkTheme(alert);
alert.showAndWait();
}
}
Expand Down Expand Up @@ -686,7 +675,6 @@ public void exit()
alert.initOwner(aMainStage);
alert.setTitle(RESOURCES.getString("dialog.exit.title"));
alert.setHeaderText(RESOURCES.getString("dialog.exit.title"));
applyDarkTheme(alert);
alert.showAndWait();

if(alert.getResult() == ButtonType.YES)
Expand Down Expand Up @@ -762,27 +750,19 @@ private void removeGraphFrameFromTabbedPane(DiagramTab pTab)
showWelcomeTabIfNecessary();
}

private void applyDarkTheme(Alert pAlert)
{
if( UserPreferences.instance().getBoolean(BooleanPreference.darkMode) )
{
pAlert.getDialogPane().getStylesheets().add(aDarkModeCSSPath);
}
}

@Override
public void booleanPreferenceChanged(BooleanPreference pPreference)
{
if( pPreference == BooleanPreference.darkMode )
{
if( UserPreferences.instance().getBoolean(pPreference) )
{
getStylesheets().add(aDarkModeCSSPath);
getScene().getStylesheets().add(aDarkModeCSSPath);
aDialogStage.getScene().getStylesheets().add(aDarkModeCSSPath);
}
else
{
getStylesheets().remove(aDarkModeCSSPath);
getScene().getStylesheets().remove(aDarkModeCSSPath);
aDialogStage.getScene().getStylesheets().remove(aDarkModeCSSPath);
}
}
Expand Down

0 comments on commit a9169cb

Please sign in to comment.