Skip to content

Commit cea8230

Browse files
committed
Laziely initialize svgCanvas to optimize startup time and memory usage
1 parent 6d7092f commit cea8230

File tree

1 file changed

+19
-9
lines changed
  • src/main/java/de/bwravencl/controllerbuddy/gui

1 file changed

+19
-9
lines changed

src/main/java/de/bwravencl/controllerbuddy/gui/Main.java

+19-9
Original file line numberDiff line numberDiff line change
@@ -564,8 +564,6 @@ public final class Main {
564564

565565
private final JMenuItem stopMenuItem;
566566

567-
private final JSVGCanvas svgCanvas;
568-
569567
private final JTabbedPane tabbedPane = new JTabbedPane(SwingConstants.TOP);
570568

571569
private final SVGDocument templateSvgDocument;
@@ -638,6 +636,8 @@ public final class Main {
638636

639637
private long stopTrayEntry;
640638

639+
private JSVGCanvas svgCanvas;
640+
641641
private volatile Rectangle totalDisplayBounds;
642642

643643
private long tray;
@@ -863,9 +863,19 @@ public void windowOpened(final WindowEvent e) {
863863
visualizationPanel = new JPanel(new BorderLayout());
864864
tabbedPane.addTab(STRINGS.getString("VISUALIZATION_TAB"), visualizationPanel);
865865

866-
svgCanvas = new JSVGCanvas(null, false, false);
867-
svgCanvas.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));
868-
visualizationPanel.add(svgCanvas, BorderLayout.CENTER);
866+
tabbedPane.addChangeListener(e -> {
867+
if (e.getSource() != tabbedPane || tabbedPane.getSelectedComponent() != visualizationPanel) {
868+
return;
869+
}
870+
871+
if (svgCanvas == null) {
872+
svgCanvas = new JSVGCanvas(null, false, false);
873+
svgCanvas.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));
874+
visualizationPanel.add(svgCanvas, BorderLayout.CENTER);
875+
876+
updateVisualizationPanel();
877+
}
878+
});
869879

870880
final var exportPanel = new JPanel(LOWER_BUTTONS_FLOW_LAYOUT);
871881
final var exportButton = new JButton(new ExportAction());
@@ -3901,7 +3911,7 @@ public void updateTrayIconToolTip(final int batteryPercent) {
39013911
}
39023912

39033913
void updateVisualizationPanel() {
3904-
if (visualizationPanel == null || input == null) {
3914+
if (visualizationPanel == null || svgCanvas == null || input == null) {
39053915
return;
39063916
}
39073917

@@ -3924,11 +3934,11 @@ public void actionPerformed(final ActionEvent e) {
39243934
svgCanvas.setSVGDocument(workingCopySvgDocument);
39253935
}
39263936
});
3927-
} else {
3928-
modeComboBox.setModel(model);
3929-
modeComboBox.setSelectedIndex(model.getSize() > 0 ? 0 : -1);
39303937
}
39313938

3939+
modeComboBox.setModel(model);
3940+
modeComboBox.setSelectedIndex(model.getSize() > 0 ? 0 : -1);
3941+
39323942
svgCanvas.setBackground(UIManager.getColor("Panel.background"));
39333943
}
39343944

0 commit comments

Comments
 (0)