action-update-thread: support UpdateInBackground for providers

GitOrigin-RevId: 7e7dd52a7bcd1bfff01501401b8cf3b085ff5726
This commit is contained in:
Dmitry Avdeev
2022-06-10 15:54:35 +00:00
committed by intellij-monorepo-bot
parent 59cd325e0b
commit c22d91f17f
4 changed files with 73 additions and 72 deletions
@@ -9,6 +9,8 @@ import com.intellij.openapi.project.DumbAware;
import com.intellij.util.ui.EDT;
import org.jetbrains.annotations.NotNull;
import java.util.function.BiConsumer;
public class CopyAction extends AnAction implements DumbAware, LightEditCompatible {
public CopyAction() {
@@ -32,42 +34,31 @@ public class CopyAction extends AnAction implements DumbAware, LightEditCompatib
@Override
public void update(@NotNull AnActionEvent event) {
updateFromProvider(event, PlatformDataKeys.COPY_PROVIDER, (provider, presentation) -> {
presentation.setEnabled(provider.isCopyEnabled(event.getDataContext()));
boolean isEditorPopup = event.getPlace().equals(ActionPlaces.EDITOR_POPUP);
presentation.setVisible(!isEditorPopup || provider.isCopyVisible(event.getDataContext()));
});
}
static <T> void updateFromProvider(@NotNull AnActionEvent event,
DataKey<T> key,
BiConsumer<T, Presentation> presentationUpdater) {
Presentation presentation = event.getPresentation();
DataContext dataContext = event.getDataContext();
CopyProvider provider = PlatformDataKeys.COPY_PROVIDER.getData(dataContext);
T provider = key.getData(dataContext);
if (provider == null) {
presentation.setEnabled(false);
presentation.setVisible(true);
return;
}
boolean isEditorPopup = event.getPlace().equals(ActionPlaces.EDITOR_POPUP);
if (provider instanceof UpdateInBackground && ((UpdateInBackground)provider).isUpdateInBackground() ||
EDT.isCurrentThreadEdt()) {
ProviderState providerState = ProviderState.create(dataContext, isEditorPopup, provider);
presentation.setEnabled(providerState.isCopyEnabled);
presentation.setVisible(providerState.isVisible);
presentationUpdater.accept(provider, presentation);
}
else {
ProviderState providerState = Utils.getOrCreateUpdateSession(event).computeOnEdt(
"ProviderState#create", () -> ProviderState.create(dataContext, isEditorPopup, provider));
presentation.setEnabled(providerState.isCopyEnabled);
presentation.setVisible(providerState.isVisible);
}
}
private static class ProviderState {
final boolean isCopyEnabled;
final boolean isVisible;
ProviderState(boolean enabled, boolean visible) {
isCopyEnabled = enabled;
isVisible = visible;
}
static @NotNull ProviderState create(@NotNull DataContext dataContext, boolean isEditorPopup, @NotNull CopyProvider provider) {
boolean isCopyEnabled = provider.isCopyEnabled(dataContext);
boolean isVisible = !isEditorPopup || provider.isCopyVisible(dataContext);
return new ProviderState(isCopyEnabled, isVisible);
Utils.getOrCreateUpdateSession(event).computeOnEdt(
"ProviderState#create", () -> {presentationUpdater.accept(provider, presentation); return presentation;});
}
}
}
@@ -39,16 +39,17 @@ public class CutAction extends DumbAwareAction implements LightEditCompatible {
@Override
public void update(@NotNull AnActionEvent event) {
Presentation presentation = event.getPresentation();
DataContext dataContext = event.getDataContext();
CutProvider provider = getAvailableCutProvider(event);
Project project = CommonDataKeys.PROJECT.getData(dataContext);
presentation.setEnabled(project != null && project.isOpen() && provider != null && provider.isCutEnabled(dataContext));
if (event.getPlace().equals(ActionPlaces.EDITOR_POPUP) && provider != null) {
presentation.setVisible(provider.isCutVisible(dataContext));
}
else {
presentation.setVisible(true);
}
CopyAction.updateFromProvider(event, PlatformDataKeys.CUT_PROVIDER, (provider, presentation) -> {
DataContext dataContext = event.getDataContext();
Project project = CommonDataKeys.PROJECT.getData(dataContext);
boolean notDumbAware = project != null && DumbService.isDumb(project) && !DumbService.isDumbAware(provider);
presentation.setEnabled(!notDumbAware && project != null && project.isOpen() && provider != null && provider.isCutEnabled(dataContext));
if (event.getPlace().equals(ActionPlaces.EDITOR_POPUP) && provider != null) {
presentation.setVisible(provider.isCutVisible(dataContext));
}
else {
presentation.setVisible(true);
}
});
}
}
@@ -65,34 +65,40 @@ public class DeleteAction extends AnAction implements DumbAware, LightEditCompat
return;
}
DataContext dataContext = e.getDataContext();
DeleteProvider provider = getDeleteProvider(dataContext);
if (e.getInputEvent() instanceof KeyEvent) {
KeyEvent keyEvent = (KeyEvent)e.getInputEvent();
Object component = PlatformCoreDataKeys.CONTEXT_COMPONENT.getData(dataContext);
if (component instanceof JTextComponent) provider = null; // Do not override text deletion
if (keyEvent.getKeyCode() == KeyEvent.VK_BACK_SPACE) {
// Do not override text deletion in speed search
if (component instanceof JComponent) {
SpeedSearchSupply searchSupply = SpeedSearchSupply.getSupply((JComponent)component);
if (searchSupply != null) provider = null;
}
CopyAction.updateFromProvider(e, PlatformDataKeys.DELETE_ELEMENT_PROVIDER, (provider, p) -> {
DataContext dataContext = e.getDataContext();
if (e.getInputEvent() instanceof KeyEvent) {
KeyEvent keyEvent = (KeyEvent)e.getInputEvent();
Object component = PlatformCoreDataKeys.CONTEXT_COMPONENT.getData(dataContext);
if (component instanceof JTextComponent) provider = null; // Do not override text deletion
if (keyEvent.getKeyCode() == KeyEvent.VK_BACK_SPACE) {
// Do not override text deletion in speed search
if (component instanceof JComponent) {
SpeedSearchSupply searchSupply = SpeedSearchSupply.getSupply((JComponent)component);
if (searchSupply != null) provider = null;
}
String activeSpeedSearchFilter = SpeedSearchSupply.SPEED_SEARCH_CURRENT_QUERY.getData(dataContext);
if (!StringUtil.isEmpty(activeSpeedSearchFilter)) {
provider = null;
String activeSpeedSearchFilter = SpeedSearchSupply.SPEED_SEARCH_CURRENT_QUERY.getData(dataContext);
if (!StringUtil.isEmpty(activeSpeedSearchFilter)) {
provider = null;
}
}
}
}
if (provider instanceof TitledHandler) {
presentation.setText(((TitledHandler)provider).getActionTitle());
}
boolean canDelete = provider != null && provider.canDeleteElement(dataContext);
if (ActionPlaces.isPopupPlace(e.getPlace())) {
presentation.setVisible(canDelete);
}
else {
presentation.setEnabled(canDelete);
}
if (provider instanceof TitledHandler) {
presentation.setText(((TitledHandler)provider).getActionTitle());
}
boolean canDelete = provider != null && provider.canDeleteElement(dataContext);
if (ActionPlaces.isPopupPlace(e.getPlace())) {
presentation.setVisible(canDelete);
}
else {
presentation.setEnabled(canDelete);
}
});
}
@Override
public @NotNull ActionUpdateThread getActionUpdateThread() {
return ActionUpdateThread.BGT;
}
}
@@ -14,17 +14,20 @@ public class PasteAction extends AnAction implements DumbAware, LightEditCompati
@Override
public void update(@NotNull AnActionEvent event) {
Presentation presentation = event.getPresentation();
DataContext dataContext = event.getDataContext();
CopyAction.updateFromProvider(event, PlatformDataKeys.PASTE_PROVIDER, (provider, presentation) -> {
presentation.setEnabled(provider != null && provider.isPastePossible(event.getDataContext()));
if (event.getPlace().equals(ActionPlaces.EDITOR_POPUP) && provider != null) {
presentation.setVisible(presentation.isEnabled());
}
else {
presentation.setVisible(true);
}
});
}
PasteProvider provider = PlatformDataKeys.PASTE_PROVIDER.getData(dataContext);
presentation.setEnabled(provider != null && provider.isPastePossible(dataContext));
if (event.getPlace().equals(ActionPlaces.EDITOR_POPUP) && provider != null) {
presentation.setVisible(presentation.isEnabled());
}
else {
presentation.setVisible(true);
}
@Override
public @NotNull ActionUpdateThread getActionUpdateThread() {
return ActionUpdateThread.BGT;
}
@Override