From 77a2a8c7c236c0b20a5658d20bab38535bba20a9 Mon Sep 17 00:00:00 2001 From: Florian Enner Date: Tue, 26 Sep 2023 18:45:02 +0200 Subject: [PATCH] made BenchPlugin more extensible to support custom CSS files (#623) --- .../java/io/fair_acc/chartfx/XYChart.java | 30 +++++++-------- .../fair_acc/chartfx/plugins/BenchPlugin.java | 37 +++++++++++-------- 2 files changed, 36 insertions(+), 31 deletions(-) diff --git a/chartfx-chart/src/main/java/io/fair_acc/chartfx/XYChart.java b/chartfx-chart/src/main/java/io/fair_acc/chartfx/XYChart.java index 306b43562..8b1eddb94 100644 --- a/chartfx-chart/src/main/java/io/fair_acc/chartfx/XYChart.java +++ b/chartfx-chart/src/main/java/io/fair_acc/chartfx/XYChart.java @@ -292,18 +292,6 @@ protected void redrawCanvas() { public void setGlobalRecorder(MeasurementRecorder recorder) { setRecorder(recorder); int i = 0; - for (Axis axis : getAxes()) { - if (axis == getXAxis()) { - axis.setRecorder(recorder.addPrefix("x")); - } else if (axis == getYAxis()) { - axis.setRecorder(recorder.addPrefix("y")); - } else { - axis.setRecorder(recorder.addPrefix("axis" + i)); - } - i++; - } - i = 0; - gridRenderer.setRecorder(recorder); for (var renderer : getRenderers()) { var p = recorder.addPrefix("renderer" + i); renderer.setRecorder(p); @@ -315,6 +303,18 @@ public void setGlobalRecorder(MeasurementRecorder recorder) { } i++; } + gridRenderer.setRecorder(recorder); + i = 0; + for (Axis axis : getAxes()) { + if (axis == getXAxis()) { + axis.setRecorder(recorder.addPrefix("x")); + } else if (axis == getYAxis()) { + axis.setRecorder(recorder.addPrefix("y")); + } else { + axis.setRecorder(recorder.addPrefix("axis" + i)); + } + i++; + } i = 0; for (ChartPlugin plugin : getPlugins()) { plugin.setRecorder(recorder.addPrefix("plugin" + i++)); @@ -323,11 +323,11 @@ public void setGlobalRecorder(MeasurementRecorder recorder) { @Override public void setRecorder(MeasurementRecorder recorder) { - super.setRecorder(recorder); - benchDrawGrid = recorder.newDuration("xychart-drawGrid"); benchDrawData = recorder.newDuration("xychart-drawData"); + benchDrawGrid = recorder.newDuration("xychart-drawGrid"); + super.setRecorder(recorder); } - private DurationMeasure benchDrawGrid = DurationMeasure.DISABLED; private DurationMeasure benchDrawData = DurationMeasure.DISABLED; + private DurationMeasure benchDrawGrid = DurationMeasure.DISABLED; } diff --git a/chartfx-chart/src/main/java/io/fair_acc/chartfx/plugins/BenchPlugin.java b/chartfx-chart/src/main/java/io/fair_acc/chartfx/plugins/BenchPlugin.java index 47cbdf950..61c4e3464 100644 --- a/chartfx-chart/src/main/java/io/fair_acc/chartfx/plugins/BenchPlugin.java +++ b/chartfx-chart/src/main/java/io/fair_acc/chartfx/plugins/BenchPlugin.java @@ -5,6 +5,8 @@ import javafx.beans.property.BooleanProperty; import javafx.beans.property.SimpleBooleanProperty; +import javafx.collections.FXCollections; +import javafx.collections.ObservableList; import javafx.event.ActionEvent; import javafx.geometry.Insets; import javafx.scene.Scene; @@ -17,6 +19,7 @@ import io.fair_acc.bench.BenchLevel; import io.fair_acc.bench.MeasurementRecorder; +import io.fair_acc.chartfx.Chart; import io.fair_acc.chartfx.XYChart; import io.fair_acc.chartfx.bench.LiveDisplayRecorder; import io.fair_acc.chartfx.utils.FXUtils; @@ -32,12 +35,12 @@ public class BenchPlugin extends ChartPlugin { private static final int FONT_SIZE = 22; private static final String ICON_ENABLE_BENCH = "fa-hourglass-start:" + FONT_SIZE; private static final String ICON_DISABLE_BENCH = "fa-hourglass-end:" + FONT_SIZE; - private final BooleanProperty enabled = new SimpleBooleanProperty(false); + protected final BooleanProperty enabled = new SimpleBooleanProperty(false); private final HBox buttons = createButtonBar(); private UnaryOperator measurementFilter = rec -> rec.atLevel(BenchLevel.Info).contains("draw"); - private final Stage stage = new Stage(); + protected final Stage stage = new Stage(); - public HBox createButtonBar() { + protected HBox createButtonBar() { final Button enableBtn = new Button(null, new FontIcon(ICON_ENABLE_BENCH)); enableBtn.setPadding(new Insets(3, 3, 3, 3)); enableBtn.setTooltip(new Tooltip("starts displaying live benchmark stats")); @@ -47,7 +50,7 @@ public HBox createButtonBar() { disableBtn.setTooltip(new Tooltip("stops displaying live benchmark stats")); FXUtils.bindManagedToVisible(enableBtn).bind(enabled.not()); - enableBtn.setOnAction(this::enable); + enableBtn.setOnAction(evt -> enable()); FXUtils.bindManagedToVisible(disableBtn).bind(enabled); disableBtn.setOnAction(evt -> disable()); @@ -81,25 +84,27 @@ public BenchPlugin setFilter(UnaryOperator measurementFilte return this; } - private void enable(ActionEvent event) { + public void enable() { if (!enabled.get() && getChart() != null && getChart() instanceof XYChart) { XYChart chart = (XYChart) getChart(); - String title = Optional.ofNullable(chart.getTitle()) - .filter(string -> !string.isEmpty()) - .orElse("Benchmark"); - LiveDisplayRecorder recorder = LiveDisplayRecorder.createChart(title, pane -> { - Scene scene = new Scene(pane); - scene.getStylesheets().addAll(chart.getScene().getStylesheets()); - stage.setScene(scene); - resetRecorder = () -> chart.setGlobalRecorder(MeasurementRecorder.DISABLED); - stage.show(); - }); + MeasurementRecorder recorder = createRecorder(chart); chart.setGlobalRecorder(measurementFilter.apply(recorder)); + resetRecorder = () -> chart.setGlobalRecorder(MeasurementRecorder.DISABLED); enabled.set(true); } } - private void disable() { + protected MeasurementRecorder createRecorder(XYChart chart) { + String title = Optional.ofNullable(chart.getTitle()) + .filter(string -> !string.isEmpty()) + .orElse("Benchmark"); + return LiveDisplayRecorder.createChart(title, pane -> { + stage.setScene(new Scene(pane)); + stage.show(); + }); + } + + public void disable() { if (enabled.get()) { enabled.set(false); resetRecorder.run();