diff --git a/platform/platform-impl/src/com/intellij/ide/actions/RefCardAction.java b/platform/platform-impl/src/com/intellij/ide/actions/RefCardAction.java index df0a0b0c0c21..8b013c50fe94 100644 --- a/platform/platform-impl/src/com/intellij/ide/actions/RefCardAction.java +++ b/platform/platform-impl/src/com/intellij/ide/actions/RefCardAction.java @@ -1,4 +1,4 @@ -// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package com.intellij.ide.actions; import com.intellij.ide.BrowserUtil; @@ -7,54 +7,48 @@ import com.intellij.openapi.actionSystem.AnActionEvent; import com.intellij.openapi.application.PathManager; import com.intellij.openapi.application.ex.ApplicationInfoEx; import com.intellij.openapi.project.DumbAwareAction; +import com.intellij.openapi.util.NullableLazyValue; import com.intellij.openapi.util.SystemInfo; -import com.intellij.openapi.util.io.FileUtil; -import org.jetbrains.annotations.NonNls; +import com.intellij.openapi.vfs.DiskQueryRelay; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; -import java.io.File; +import java.nio.file.Files; +import java.nio.file.Path; -/** - * @author Vladimir Kondratyev - */ public class RefCardAction extends DumbAwareAction { - private static final @NonNls String REF_CARD_PATH = - PathManager.getHomePath() + "/help/" + (SystemInfo.isMac ? "ReferenceCardForMac.pdf" : "ReferenceCard.pdf"); + private final NullableLazyValue myRefCardPath = NullableLazyValue.lazyNullable(() -> { + var relay = new DiskQueryRelay(file -> Files.isRegularFile(file) ? file : null); + var file = Path.of(PathManager.getHomePath() + "/help/" + (SystemInfo.isMac ? "ReferenceCardForMac.pdf" : "ReferenceCard.pdf")); + return relay.accessDiskWithCheckCanceled(file); + }); @Override public @NotNull ActionUpdateThread getActionUpdateThread() { return ActionUpdateThread.BGT; } + @Override + public void update(@NotNull AnActionEvent e) { + e.getPresentation().setEnabledAndVisible(myRefCardPath.getValue() != null || getKeymapUrl() != null); + } + @Override public void actionPerformed(@NotNull AnActionEvent e) { - File file = getRefCardFile(); - if (file.isFile()) { + var file = myRefCardPath.getValue(); + if (file != null) { BrowserUtil.browse(file); } else { - String webUrl = getKeymapUrl(); + var webUrl = getKeymapUrl(); if (webUrl != null) { BrowserUtil.browse(webUrl); } } } - @Override - public void update(@NotNull AnActionEvent e) { - e.getPresentation().setEnabledAndVisible(isRefCardAvailable()); - } - - private static boolean isRefCardAvailable() { - return getRefCardFile().exists() || getKeymapUrl() != null; - } - - private static String getKeymapUrl() { - final ApplicationInfoEx appInfo = ApplicationInfoEx.getInstanceEx(); + private static @Nullable String getKeymapUrl() { + var appInfo = ApplicationInfoEx.getInstanceEx(); return SystemInfo.isMac ? appInfo.getMacKeymapUrl() : appInfo.getWinKeymapUrl(); } - - private static @NotNull File getRefCardFile() { - return new File(FileUtil.toSystemDependentName(REF_CARD_PATH)); - } }