Skip to content

Commit

Permalink
StackPane added | MarkdownFormatter initialized outside of contructor…
Browse files Browse the repository at this point in the history
… | Lazy initialization of WebView implemented
  • Loading branch information
mulla028 committed Dec 9, 2024
1 parent fe4b53f commit e62d4f2
Showing 1 changed file with 22 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import javafx.scene.control.CheckBox;
import javafx.scene.control.TextArea;
import javafx.scene.layout.Priority;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.scene.web.WebView;
Expand All @@ -22,17 +23,17 @@ public class SummaryShowingComponent extends VBox {
@FXML private TextArea summaryTextArea;
@FXML private Text summaryInfoText;
@FXML private CheckBox markdownCheckbox;
@FXML private StackPane contentStackPane;

private WebView markdownWebView;

private final Summary summary;
private final Runnable regenerateCallback;
private final MarkdownFormatter markdownFormatter;
private final MarkdownFormatter markdownFormatter = new MarkdownFormatter();

public SummaryShowingComponent(Summary summary, Runnable regenerateCallback) {
this.summary = summary;
this.regenerateCallback = regenerateCallback;
this.markdownFormatter = new MarkdownFormatter();

ViewLoader.view(this)
.root(this)
Expand All @@ -41,14 +42,9 @@ public SummaryShowingComponent(Summary summary, Runnable regenerateCallback) {

@FXML
private void initialize() {
markdownWebView = new WebView();
markdownWebView.setVisible(false);
markdownWebView.setManaged(false);

VBox.setVgrow(markdownWebView, Priority.ALWAYS);

int indexOfTextArea = getChildren().indexOf(summaryTextArea);
getChildren().add(indexOfTextArea + 1, markdownWebView);
if (!contentStackPane.getChildren().contains(summaryTextArea)) {
contentStackPane.getChildren().add(summaryTextArea);
}

summaryTextArea.setText(summary.content());

Expand All @@ -64,15 +60,28 @@ private static String formatTimestamp(LocalDateTime timestamp) {
return timestamp.format(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM).withLocale(Locale.getDefault()));
}

private void initializeMarkdownWebView() {
if (markdownWebView == null) {
markdownWebView = new WebView();
VBox.setVgrow(markdownWebView, Priority.ALWAYS);

markdownWebView.prefWidthProperty().bind(summaryTextArea.widthProperty());
markdownWebView.prefHeightProperty().bind(summaryTextArea.heightProperty());

markdownWebView.setVisible(false);
markdownWebView.setManaged(false);
contentStackPane.getChildren().add(markdownWebView);
}
}

@FXML
private void onMarkdownToggle() {
initializeMarkdownWebView();

if (markdownCheckbox.isSelected()) {
String htmlContent = markdownFormatter.format(summaryTextArea.getText());
markdownWebView.getEngine().loadContent(htmlContent);

markdownWebView.setPrefHeight(summaryTextArea.getPrefHeight());
markdownWebView.setPrefWidth(summaryTextArea.getPrefWidth());

markdownWebView.setVisible(true);
markdownWebView.setManaged(true);

Expand Down

0 comments on commit e62d4f2

Please sign in to comment.