Skip to content

Commit e4cc805

Browse files
committed
Prevent a temporary UI freeze when resolving local host name (#1793)
1 parent a96dd24 commit e4cc805

File tree

3 files changed

+36
-32
lines changed

3 files changed

+36
-32
lines changed

CHANGELOG.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ This document provides a high-level view of the changes introduced by release.
99

1010
=== 0.44.5
1111

12-
...
12+
- Prevent a temporary UI freeze when resolving local host name (#1793)
1313

1414
=== 0.44.4
1515

src/main/java/org/asciidoc/intellij/editor/AsciiDocPreviewEditor.java

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,6 @@
8787
public class AsciiDocPreviewEditor extends UserDataHolderBase implements FileEditor {
8888

8989
private static final Logger LOG = Logger.getInstance(AsciiDocPreviewEditor.class);
90-
private static final String EMPTY_HTML = "<html></html>";
91-
private static final String JCEF_HTML = "<html>If you can read this and not the content of your document, your preview does not show. One possible reason is that you are using a remote desktop and " +
92-
"GPU rendered content is not shown.<p>" +
93-
"To fix this, update the IDE's registry and set the key 'ide.browser.jcef.gpu.disable' to the value 'true'.<p>" +
94-
"To access the registry, open the menu 'Help | Find Action...' and then choose the action 'Registry...'. " +
95-
"Once the registry opens, type the key to find the entry, and enable the checkbox to set the value to 'true'. " +
96-
"Once you changed the value, restart the IDE for the setting to become effective.</html>";
9790
private final AsciiDocExtensionService extensionService = ApplicationManager.getApplication().getService(AsciiDocExtensionService.class);
9891
/**
9992
* single threaded with one task queue (one for each editor window).
@@ -219,6 +212,9 @@ private AsciiDocWrapper getAsciiDocInstance() {
219212
void renderIfVisible() {
220213
// visible = preview is enabled
221214
// displayable = editor window is visible as it is the active editor in a group
215+
if (myPanel == null && getComponent().isVisible() && getComponent().isDisplayable()) {
216+
setupPanel();
217+
}
222218
if (myPanel != null && getComponent().isVisible() && getComponent().isDisplayable()) {
223219
render();
224220
}
@@ -298,13 +294,13 @@ public AsciiDocPreviewEditor(final Document document, Project project) {
298294
myHtmlPanelWrapper = new JPanel();
299295
LayoutManager overlay = new OverlayLayout(myHtmlPanelWrapper);
300296
myHtmlPanelWrapper.setLayout(overlay);
301-
hint = new JLabel(EMPTY_HTML);
297+
hint = new JLabel();
302298
myHtmlPanelWrapper.add(hint);
303299

304300
myHtmlPanelWrapper.addComponentListener(new ComponentAdapter() {
305301
@Override
306302
public void componentShown(ComponentEvent e) {
307-
setupPanel();
303+
renderIfVisible();
308304
}
309305

310306
@Override
@@ -467,10 +463,15 @@ private static AsciiDocHtmlPanel detachOldPanelAndCreateAndAttachNewOne(Document
467463
}
468464
panelWrapper.add(newPanel.getComponent(), BorderLayout.CENTER, 0);
469465

470-
String hintText = newPanel instanceof AsciiDocJCEFHtmlPanel ? JCEF_HTML : EMPTY_HTML;
471-
if (!Objects.equals(hint.getText(), hintText)) {
472-
// defer UI update to avoid flicking of the shown text
473-
ApplicationManager.getApplication().invokeLater(() -> hint.setText(hintText));
466+
if (newPanel instanceof AsciiDocJCEFHtmlPanel) {
467+
hint.setText("<html>If you can read this and not the content of your document, your preview does not show. One possible reason is that you are using a remote desktop and " +
468+
"GPU rendered content is not shown.<p>" +
469+
"To fix this, update the IDE's registry and set the key 'ide.browser.jcef.gpu.disable' to the value 'true'.<p>" +
470+
"To access the registry, open the menu 'Help | Find Action...' and then choose the action 'Registry...'. " +
471+
"Once the registry opens, type the key to find the entry, and enable the checkbox to set the value to 'true'. " +
472+
"Once you changed the value, restart the IDE for the setting to become effective.");
473+
} else {
474+
hint.setText("<html></html>");
474475
}
475476

476477
return newPanel;
@@ -571,9 +572,7 @@ public boolean isValid() {
571572
*/
572573
@Override
573574
public void selectNotify() {
574-
if (myPanel == null) {
575-
setupPanel();
576-
}
575+
renderIfVisible();
577576
if (FileDocumentManager.getInstance().getUnsavedDocuments().length > 0) {
578577
ApplicationManager.getApplication().invokeLater(() -> {
579578
// don't try to run save-all in parallel, therefore synchronize

src/main/java/org/asciidoc/intellij/editor/jcef/AsciiDocJCEFHtmlPanelProvider.java

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.asciidoc.intellij.editor.jcef;
22

3+
import com.intellij.openapi.application.ApplicationManager;
34
import com.intellij.openapi.application.PathManager;
45
import com.intellij.openapi.diagnostic.Logger;
56
import com.intellij.openapi.editor.Document;
@@ -28,26 +29,30 @@ public AsciiDocHtmlPanel createHtmlPanel(Document document, Path imagesPath, Run
2829
return new AsciiDocJCEFHtmlPanel(document, imagesPath, forceRefresh);
2930
}
3031

31-
private static synchronized void clearStaleJCEFlock() {
32+
private static void clearStaleJCEFlock() {
3233
// Workaround for https://youtrack.jetbrains.com/issue/IJPL-148653,
3334
// when a lock file remains which the name of the old host name
3435
// Run this check everytime an AsciiDoc editor is opened to prevent problems, for example, after a computer comes back from suspend mode.
35-
Path jcefCache = PathManager.getSystemDir().resolve("jcef_cache");
36-
if (jcefCache.toFile().exists()) {
37-
Path singletonLock = jcefCache.resolve("SingletonLock");
38-
try {
39-
String hostName = InetAddress.getLocalHost().getHostName();
40-
String lockFile = Files.readSymbolicLink(singletonLock).toFile().getName();
41-
if (!lockFile.matches(Pattern.quote(hostName) + "-[0-9]*")) {
42-
// delete the stale lock to prevent the preview from appearing blank
43-
Files.delete(singletonLock);
36+
// This might be slow as the InetAddress.getLocalHost().getHostName() times out, therefore run it in a background thread.
37+
// In the worst of all cases, this would clear any stale lock only on a second attempt.
38+
ApplicationManager.getApplication().executeOnPooledThread(() -> {
39+
Path jcefCache = PathManager.getSystemDir().resolve("jcef_cache");
40+
if (jcefCache.toFile().exists()) {
41+
Path singletonLock = jcefCache.resolve("SingletonLock");
42+
try {
43+
String hostName = InetAddress.getLocalHost().getHostName();
44+
String lockFile = Files.readSymbolicLink(singletonLock).toFile().getName();
45+
if (!lockFile.matches(Pattern.quote(hostName) + "-[0-9]*")) {
46+
// delete the stale lock to prevent the preview from appearing blank
47+
Files.delete(singletonLock);
48+
}
49+
} catch (NoSuchFileException ignore) {
50+
// nop
51+
} catch (IOException e) {
52+
LOG.warn("Can't check lock", e);
4453
}
45-
} catch (NoSuchFileException ignore) {
46-
// nop
47-
} catch (IOException e) {
48-
LOG.warn("Can't check lock", e);
4954
}
50-
}
55+
});
5156
}
5257

5358
@NotNull

0 commit comments

Comments
 (0)