Skip to content

Commit

Permalink
Add default parameters to modals (#88)
Browse files Browse the repository at this point in the history
* feat(framework): Add default parameters to modals
* docs(framework): Add missing entry
* feat(framework): Rename stage parameter to modalStage
* docs(framework): Add more modal documentation
* feat(framework): Add `@Title` support to modals
  • Loading branch information
LeStegii authored May 6, 2024
1 parent f10d46f commit cebf199
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 11 deletions.
39 changes: 32 additions & 7 deletions docs/features/4-modals.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,41 @@ Modals are a special type of window that can be used to display a controller on
can be used to display popup windows, dialogs, etc.

The framework provides a `Modals` class that can be used to display a modal.
When using `showModal()` a stage will be created and configured to be displayed above the current stage.
A BiConsumer provides access to the component instance and the stage for configuring other things.

When displaying the component, the parameters `modalStage` and `ownerStage` will be passed so that the modal can for
example be closed from inside the component class.

```java
Modals.showModal(app, confirmComponent, (stage, component) -> {
stage.setTitle("Modal");
component.setConfirmAction(() -> {
// Do something
stage.close();
});
});

@Component
public class ModalComponent extends VBox {

// ...

@Param("modalStage")
Stage modal;

@OnRender
void onRender() {
modal.setTitle("Modal");
}

@FXML
void onCloseClick() {
doStuff();
modal.close();
}

}
```

```java
Modals.showModal(app, modalComponent, (stage, component) -> {
stage.doSomething();
component.doSomethingElse();
});
```

---
Expand Down
3 changes: 2 additions & 1 deletion docs/features/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ This section will give you an overview of the features and how to use them.
2. [For-Loop](2-for.md)
3. [History and refresh](3-history.md)
4. [Modals](4-modals.md)
5. [Node Duplicator](5-node-duplicator.md)
5. [Node Duplicator](5-node-duplicator.md)
6. [Component List Cell](7-componentlistcell.md)
17 changes: 14 additions & 3 deletions framework/src/main/java/org/fulib/fx/controller/Modals.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.fulib.fx.FulibFxApp;
import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.Map;
import java.util.function.BiConsumer;

Expand Down Expand Up @@ -171,16 +172,26 @@ public static <Display extends Node> void showModal(FulibFxApp app, Stage curren
FulibFxApp.FX_SCHEDULER.scheduleDirect(() -> {
ModalStage modalStage = new ModalStage(app, destroyOnClose, component);

app.frameworkComponent().controllerManager().init(component, params);
Node rendered = app.frameworkComponent().controllerManager().render(component, params);
// Add additional default parameters
Map<String, Object> parameters = new HashMap<>(params);
parameters.putIfAbsent("modalStage", modalStage);
parameters.putIfAbsent("ownerStage", currentStage);

// Initialize and render the component
app.frameworkComponent().controllerManager().init(component, parameters);
Node rendered = app.frameworkComponent().controllerManager().render(component, parameters);

// As the displayed component will be the root of a stage, it has to be a parent
if (!(rendered instanceof Parent parent)) {
throw new IllegalArgumentException(error(1011).formatted(component.getClass().getName()));
}

// Set the title if present
app.getTitle(component).ifPresent(title -> modalStage.setTitle(app.formatTitle(title)));

// Configure scene to look like a popup (can be changed using the initializer)
Scene scene = new Scene(parent);
scene.setFill(Paint.valueOf("transparent"));

modalStage.setScene(scene);
modalStage.initStyle(StageStyle.TRANSPARENT);
modalStage.initOwner(currentStage);
Expand Down

0 comments on commit cebf199

Please sign in to comment.