remove constant repaint for jupyter notebook

so reduced cpu for PY-18625
This commit is contained in:
Ekaterina Tuzova
2017-06-09 14:04:39 +03:00
parent 9428a69d84
commit b0214d4830
10 changed files with 61 additions and 95 deletions
@@ -5,6 +5,7 @@ import com.github.rjeschke.txtmark.Processor;
import com.intellij.ide.BrowserUtil;
import com.intellij.ide.ui.LafManager;
import com.intellij.ide.ui.laf.darcula.DarculaLookAndFeelInfo;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.colors.EditorColorsManager;
import com.intellij.openapi.util.text.StringUtil;
@@ -66,7 +67,7 @@ public class IpnbJfxUtils {
private static final String ourPostfix = "</div></body></html>";
private static URL ourStyleUrl;
public static JComponent createHtmlPanel(@NotNull final String source, int width) {
public static JComponent createHtmlPanel(@NotNull final String source, int width, boolean repaint) {
final JFXPanel javafxPanel = new JFXPanel() {
@Override
@@ -89,14 +90,14 @@ public class IpnbJfxUtils {
engine.setOnStatusChanged(event -> {
final String data = event.getData();
if (data != null && data.isEmpty()) {
adjustHeight(webView, javafxPanel, source);
adjustHeight(webView, javafxPanel, source, repaint);
}
});
}
else {
engine.getLoadWorker().stateProperty().addListener((observable, oldValue, newValue) -> {
if (newValue == Worker.State.SUCCEEDED) {
adjustHeight(webView, javafxPanel, source);
adjustHeight(webView, javafxPanel, source, repaint);
}
});
}
@@ -232,7 +233,7 @@ public class IpnbJfxUtils {
}
}
private static void adjustHeight(final WebView webView, final JFXPanel javafxPanel, String source) {
private static void adjustHeight(final WebView webView, final JFXPanel javafxPanel, String source, boolean repaintCallback) {
final WebEngine engine = webView.getEngine();
final Document document = engine.getDocument();
if (document != null) {
@@ -273,8 +274,11 @@ public class IpnbJfxUtils {
final Dimension size = new Dimension(
width, height + count * EditorColorsManager.getInstance().getGlobalScheme().getEditorFontSize());
javafxPanel.setPreferredSize(size);
javafxPanel.setMinimumSize(size);
ApplicationManager.getApplication().invokeLater(()-> javafxPanel.setPreferredSize(size));
if (repaintCallback) {
ApplicationManager.getApplication().invokeLater(()->javafxPanel.repaint());
}
}
}
}
@@ -10,7 +10,6 @@ import javafx.application.Platform;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.plugins.ipnb.editor.IpnbEditorUtil;
import org.jetbrains.plugins.ipnb.editor.panels.IpnbFilePanel;
import javax.swing.*;
import java.awt.*;
@@ -24,8 +23,8 @@ public class IpnbUtils {
private static final Logger LOG = Logger.getInstance(IpnbUtils.class);
private static int hasFx = 0;
public static JComponent createLatexPane(@NotNull final String source, int width, IpnbFilePanel parent) {
final JComponent panel = createHtmlPanel(source, width, parent);
public static JComponent createLatexPane(@NotNull final String source, int width, boolean repaint) {
final JComponent panel = createHtmlPanel(source, width, repaint);
panel.addMouseListener(new MouseAdapter() {
@Override
@@ -53,9 +52,9 @@ public class IpnbUtils {
return hasFx == 1;
}
public static JComponent createHtmlPanel(@NotNull final String source, int width, IpnbFilePanel parent) {
public static JComponent createHtmlPanel(@NotNull final String source, int width, boolean repaint) {
if (hasFx()) {
return IpnbJfxUtils.createHtmlPanel(source, width);
return IpnbJfxUtils.createHtmlPanel(source, width, repaint);
}
return createNonJfxPanel(source);
}
@@ -62,11 +62,8 @@ public class IpnbRunAllCellsAction extends IpnbRunCellBaseAction {
private static void runCells(List<IpnbEditablePanel> cells, IpnbFilePanel ipnbFilePanel) {
for (IpnbEditablePanel cell : cells) {
cell.runCell(true);
ipnbFilePanel.revalidate();
ipnbFilePanel.repaint();
IdeFocusManager.getGlobalInstance().doWhenFocusSettlesDown(() -> {
IdeFocusManager.getGlobalInstance().requestFocus(ipnbFilePanel, true);
});
IdeFocusManager.getGlobalInstance().doWhenFocusSettlesDown(() ->
IdeFocusManager.getGlobalInstance().requestFocus(ipnbFilePanel, true));
}
}
@@ -9,7 +9,6 @@ import com.intellij.ui.JBColor;
import com.intellij.ui.OnePixelSplitter;
import com.intellij.ui.awt.RelativePoint;
import com.intellij.util.ui.JBUI;
import com.intellij.util.ui.UIUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.plugins.ipnb.editor.IpnbEditorUtil;
@@ -34,8 +33,8 @@ public abstract class IpnbEditablePanel<T extends JComponent, K extends IpnbEdit
protected JTextArea myEditableTextArea;
public final static String EDITABLE_PANEL = "Editable panel";
public final static String VIEW_PANEL = "View panel";
private OnePixelSplitter mySplitter;
private JPanel myViewPrompt;
protected OnePixelSplitter mySplitter;
protected JPanel myViewPrompt;
private JPanel myEditablePrompt;
protected JLabel myPromptLabel;
@@ -55,6 +54,7 @@ public abstract class IpnbEditablePanel<T extends JComponent, K extends IpnbEdit
mySplitter.setSecondComponent(null);
setBackground(IpnbEditorUtil.getBackground());
add(mySplitter);
setBorder(BorderFactory.createLineBorder(IpnbEditorUtil.getBackground()));
addRightClickMenu();
}
@@ -118,7 +118,7 @@ public abstract class IpnbEditablePanel<T extends JComponent, K extends IpnbEdit
setEditing(true);
mySplitter.setFirstComponent(myEditablePrompt);
UIUtil.requestFocus(myEditableTextArea);
IdeFocusManager.getGlobalInstance().requestFocus(myEditableTextArea, true);
mySplitter.setSecondComponent(null);
}
@@ -142,11 +142,10 @@ public abstract class IpnbEditablePanel<T extends JComponent, K extends IpnbEdit
setEditing(false);
final Container parent = getParent();
if (parent instanceof IpnbFilePanel) {
UIUtil.requestFocus((IpnbFilePanel)parent);
IdeFocusManager.getGlobalInstance().requestFocus(parent, true);
if (selectNext) {
((IpnbFilePanel)parent).selectNext(this, true);
}
((IpnbFilePanel)parent).revalidateAndRepaint();
}
}
}
@@ -163,7 +162,6 @@ public abstract class IpnbEditablePanel<T extends JComponent, K extends IpnbEdit
if (e.getClickCount() == 1) {
setEditing(true);
final Container parent = getParent();
parent.repaint();
if (parent instanceof IpnbFilePanel) {
((IpnbFilePanel)parent).setSelectedCellPanel(IpnbEditablePanel.this);
IdeFocusManager.getGlobalInstance().doWhenFocusSettlesDown(() -> {
@@ -180,8 +178,7 @@ public abstract class IpnbEditablePanel<T extends JComponent, K extends IpnbEdit
setEditing(false);
final Container parent = getParent();
if (parent instanceof IpnbFilePanel) {
parent.repaint();
UIUtil.requestFocus((IpnbFilePanel)parent);
IdeFocusManager.getGlobalInstance().requestFocus(parent, true);
}
}
}
@@ -207,6 +204,7 @@ public abstract class IpnbEditablePanel<T extends JComponent, K extends IpnbEdit
public void setEditing(boolean editing) {
myEditing = editing;
setBorder(BorderFactory.createLineBorder(editing ? JBColor.GREEN : JBColor.GRAY));
}
public void updateCellView() {
@@ -31,8 +31,6 @@ import com.intellij.ui.KeyStrokeAdapter;
import com.intellij.util.Alarm;
import com.intellij.util.PlatformUtils;
import com.intellij.util.messages.MessageBusConnection;
import com.intellij.util.ui.update.MergingUpdateQueue;
import com.intellij.util.ui.update.Update;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.plugins.ipnb.IpnbUtils;
@@ -59,8 +57,6 @@ import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import static com.intellij.util.ui.update.Update.HIGH_PRIORITY;
public class IpnbFilePanel extends JPanel implements Scrollable, DataProvider, Disposable {
private static final Logger LOG = Logger.getInstance(IpnbFilePanel.class);
private final DocumentListener myDocumentListener;
@@ -80,13 +76,10 @@ public class IpnbFilePanel extends JPanel implements Scrollable, DataProvider, D
private int myInitialSelection = 0;
private boolean mySynchronize;
private static final String ourHelpID = "IPython_Notebook_Support";
private MergingUpdateQueue myQueue;
public IpnbFilePanel(@NotNull final Project project, @NotNull final IpnbFileEditor parent, @NotNull final VirtualFile vFile,
@NotNull final IpnbFileEditor.CellSelectionListener listener) {
super(new VerticalFlowLayout(VerticalFlowLayout.TOP, 100, 5, true, false));
myQueue = new MergingUpdateQueue("Jupyter", 100, true, this, this,
null, true);
myProject = project;
myParent = parent;
myVirtualFile = vFile;
@@ -122,11 +115,7 @@ public class IpnbFilePanel extends JPanel implements Scrollable, DataProvider, D
});
setFocusable(true);
}, 10, ModalityState.stateForComponent(this));
alarm.addRequest(() -> {
revalidate();
repaint();
myParent.loaded();
}, 100);
myParent.loaded();
IdeFocusManager.getGlobalInstance().doWhenFocusSettlesDown(() -> {
IdeFocusManager.getGlobalInstance().requestFocus(this, true);
});
@@ -610,7 +599,6 @@ public class IpnbFilePanel extends JPanel implements Scrollable, DataProvider, D
if (mySelectedCellPanel != null && e.getID() == KeyEvent.KEY_PRESSED) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
mySelectedCellPanel.switchToEditing();
repaint();
}
if (e.getKeyCode() == KeyEvent.VK_UP) {
selectPrev(mySelectedCellPanel);
@@ -692,17 +680,6 @@ public class IpnbFilePanel extends JPanel implements Scrollable, DataProvider, D
else {
mySelectedCellPanel = null;
mySelectedIndex = -1;
repaint();
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (mySelectedCellPanel != null) {
g.setColor(mySelectedCellPanel.isEditing() ? JBColor.GREEN : JBColor.GRAY);
g.drawRoundRect(mySelectedCellPanel.getX() - 50, mySelectedCellPanel.getTop() - 1,
mySelectedCellPanel.getWidth() + 145 - IpnbEditorUtil.PROMPT_SIZE.width, mySelectedCellPanel.getHeight() + 2, 5, 5);
}
}
@@ -714,7 +691,6 @@ public class IpnbFilePanel extends JPanel implements Scrollable, DataProvider, D
IdeFocusManager.getGlobalInstance().doWhenFocusSettlesDown(() -> {
IdeFocusManager.getGlobalInstance().requestFocus(this, true);
});
repaint();
setSelectedCell(ipnbPanel, true);
}
}
@@ -732,29 +708,15 @@ public class IpnbFilePanel extends JPanel implements Scrollable, DataProvider, D
if (ipnbPanel.equals(mySelectedCellPanel)) return;
if (mySelectedCellPanel != null) {
mySelectedCellPanel.setEditing(false);
mySelectedCellPanel.setBorder(BorderFactory.createLineBorder(IpnbEditorUtil.getBackground()));
}
ipnbPanel.setBorder(BorderFactory.createLineBorder(ipnbPanel.isEditing() ? JBColor.GREEN : JBColor.GRAY));
mySelectedCellPanel = ipnbPanel;
mySelectedIndex = myIpnbPanels.indexOf(ipnbPanel);
myQueue.queue(new Update("Jupyter.Repaint", HIGH_PRIORITY) {
@Override
public void run() {
revalidate();
repaint();
if (ipnbPanel.getBounds().getHeight() != 0) {
myListener.selectionChanged(ipnbPanel, mouse);
}
}
});
}
public void revalidateAndRepaint() {
myQueue.queue(new Update("Jupyter.Repaint", HIGH_PRIORITY) {
@Override
public void run() {
revalidate();
repaint();
}
});
if (ipnbPanel.getBounds().getHeight() != 0) {
myListener.selectionChanged(ipnbPanel, mouse);
}
}
@Nullable
@@ -3,10 +3,14 @@ package org.jetbrains.plugins.ipnb.editor.panels;
import com.intellij.ui.KeyStrokeAdapter;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.plugins.ipnb.IpnbUtils;
import org.jetbrains.plugins.ipnb.editor.IpnbEditorUtil;
import org.jetbrains.plugins.ipnb.format.cells.IpnbMarkdownCell;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class IpnbMarkdownPanel extends IpnbEditablePanel<JComponent, IpnbMarkdownCell> {
@@ -32,13 +36,32 @@ public class IpnbMarkdownPanel extends IpnbEditablePanel<JComponent, IpnbMarkdow
@Override
protected JComponent createViewPanel() {
int width = myParent.getWidth();
return IpnbUtils.createLatexPane(myCell.getSourceAsString(), width, myParent);
return IpnbUtils.createLatexPane(myCell.getSourceAsString(), width, false);
}
@Override
public void updateCellView() {
removeAll();
initPanel();
int width = myParent.getWidth();
myViewPanel = IpnbUtils.createLatexPane(myCell.getSourceAsString(), width,true);
myViewPanel.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
final Container parent = getParent();
final MouseEvent parentEvent = SwingUtilities.convertMouseEvent(myViewPanel, e, parent);
parent.dispatchEvent(parentEvent);
if (e.getClickCount() == 2) {
switchToEditing();
}
}
});
myViewPanel.setName(VIEW_PANEL);
myViewPrompt = new JPanel(new GridBagLayout());
addPromptPanel(myViewPrompt, null, IpnbEditorUtil.PromptType.None, myViewPanel);
myViewPrompt.setBackground(IpnbEditorUtil.getBackground());
mySplitter.setFirstComponent(myViewPrompt);
mySplitter.setSecondComponent(null);
}
@SuppressWarnings("CloneDoesntCallSuperClone")
@@ -60,6 +60,7 @@ public class IpnbCodePanel extends IpnbEditablePanel<JComponent, IpnbCodeCell> {
myParent.getIpnbFilePanel().processKeyPressed(event);
}
});
setBorder(BorderFactory.createLineBorder(IpnbEditorUtil.getBackground()));
}
@NotNull
@@ -261,10 +262,6 @@ public class IpnbCodePanel extends IpnbEditablePanel<JComponent, IpnbCodeCell> {
@Override
public void switchToEditing() {
setEditing(true);
final Container parent = getParent();
if (parent != null) {
parent.repaint();
}
IdeFocusManager.findInstance().requestFocus(myCodeSourcePanel.getEditor().getContentComponent(), true);
}
@@ -306,7 +303,6 @@ public class IpnbCodePanel extends IpnbEditablePanel<JComponent, IpnbCodeCell> {
myPromptLabel.setText(promptText);
final IpnbFilePanel filePanel = myParent.getIpnbFilePanel();
setEditing(false);
filePanel.revalidateAndRepaint();
IdeFocusManager.findInstance().requestFocus(filePanel, true);
if (mySelectNext) {
filePanel.selectNext(this, true);
@@ -342,8 +338,6 @@ public class IpnbCodePanel extends IpnbEditablePanel<JComponent, IpnbCodeCell> {
addOutputPanel(component, outputContent, outputContent instanceof IpnbOutOutputCell);
}
}
final IpnbFilePanel filePanel = myParent.getIpnbFilePanel();
filePanel.revalidateAndRepaint();
}, ModalityState.stateForComponent(this));
}
@@ -352,10 +346,6 @@ public class IpnbCodePanel extends IpnbEditablePanel<JComponent, IpnbCodeCell> {
myViewPanel.removeAll();
final JComponent panel = createViewPanel();
myViewPanel.add(panel);
final IpnbFilePanel filePanel = myParent.getIpnbFilePanel();
filePanel.revalidate();
filePanel.repaint();
}
@Override
@@ -27,6 +27,7 @@ import com.intellij.openapi.editor.event.EditorMouseListener;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.popup.JBPopupFactory;
import com.intellij.openapi.ui.popup.ListPopup;
import com.intellij.openapi.wm.IdeFocusManager;
import com.intellij.reference.SoftReference;
import com.intellij.ui.Gray;
import com.intellij.ui.awt.RelativePoint;
@@ -103,8 +104,7 @@ public class IpnbCodeSourcePanel extends IpnbPanel<JComponent, IpnbCodeCell> imp
final Container parent = codePanel.getParent();
if (keyCode == KeyEvent.VK_ESCAPE && parent instanceof IpnbFilePanel) {
getIpnbCodePanel().setEditing(false);
parent.repaint();
UIUtil.requestFocus(getIpnbCodePanel().getFileEditor().getIpnbFilePanel());
IdeFocusManager.getGlobalInstance().requestFocus(getIpnbCodePanel().getFileEditor().getIpnbFilePanel(), true);
}
}
@@ -134,15 +134,9 @@ public class IpnbCodeSourcePanel extends IpnbPanel<JComponent, IpnbCodeCell> imp
final int height = myEditor.getLineHeight() * Math.max(myEditor.getDocument().getLineCount(), 1) + 10;
contentComponent.setPreferredSize(new Dimension(parent.getWidth() - 300, height));
panel.setPreferredSize(new Dimension(parent.getWidth() - 300, height));
codePanel.revalidate();
codePanel.repaint();
panel.revalidate();
panel.repaint();
if (parent instanceof IpnbFilePanel) {
IpnbFilePanel ipnbFilePanel = (IpnbFilePanel)parent;
ipnbFilePanel.revalidate();
ipnbFilePanel.repaint();
if (keyCode == KeyEvent.VK_ENTER && InputEvent.CTRL_MASK == e.getModifiers()) {
IpnbRunCellBaseAction.runCell(ipnbFilePanel, false);
}
@@ -193,7 +187,6 @@ public class IpnbCodeSourcePanel extends IpnbPanel<JComponent, IpnbCodeCell> imp
panel.setPreferredSize(new Dimension(parent.getWidth() - 300, height));
panel.revalidate();
panel.repaint();
parent.repaint();
}
}
});
@@ -19,6 +19,6 @@ public class IpnbHtmlPanel extends IpnbCodeOutputPanel<IpnbHtmlOutputCell> {
@Override
protected JComponent createViewPanel() {
final int width = myParent.getWidth();
return IpnbUtils.createLatexPane(StringUtil.join(myCell.getHtmls(), ""), width, myParent);
return IpnbUtils.createLatexPane(StringUtil.join(myCell.getHtmls(), ""), width, false);
}
}
@@ -23,6 +23,6 @@ public class IpnbLatexPanel extends IpnbCodeOutputPanel<IpnbLatexOutputCell> {
@Override
protected JComponent createViewPanel() {
final int width = myParent.getWidth();
return IpnbUtils.createLatexPane(StringUtil.join(myCell.getLatex(), ""), width, myParent);
return IpnbUtils.createLatexPane(StringUtil.join(myCell.getLatex(), ""), width, false);
}
}