IDEA-199434 Non-standard hotkey for uninstalling a plugin

This commit is contained in:
Alexander Lobas
2018-09-27 21:49:13 +03:00
parent 538cab8373
commit e4d00c6c1a
3 changed files with 42 additions and 26 deletions
@@ -1,10 +1,13 @@
// Copyright 2000-2018 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.
package com.intellij.ide.plugins.newui;
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.util.SystemInfo;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.util.Collections;
import java.util.List;
@@ -56,4 +59,23 @@ public class EventHandler {
public enum SelectionType {
SELECTION, HOVER, NONE
}
public static final int DELETE_CODE = SystemInfo.isMac ? KeyEvent.VK_BACK_SPACE : KeyEvent.VK_DELETE;
@Nullable
public static ShortcutSet getShortcuts(@NotNull String id) {
AnAction action = ActionManager.getInstance().getAction(id);
return action == null ? null : action.getShortcutSet();
}
public static boolean check(@NotNull KeyboardShortcut shortcut, @Nullable ShortcutSet set) {
if (set != null) {
for (Shortcut test : set.getShortcuts()) {
if (test.isKeyboard() && shortcut.startsWith(test)) {
return true;
}
}
}
return false;
}
}
@@ -336,7 +336,7 @@ public class ListPluginComponent extends CellPluginComponent {
}
Pair<Boolean, IdeaPluginDescriptor[]> result = getSelectionNewState(selection);
group.add(new MyAnAction(result.first ? "Enable" : "Disable", KeyEvent.VK_SPACE) {
group.add(new MyAnAction(result.first ? "Enable" : "Disable", null, KeyEvent.VK_SPACE) {
@Override
public void actionPerformed(@NotNull AnActionEvent e) {
myPluginModel.changeEnableDisable(result.second, result.first);
@@ -350,7 +350,7 @@ public class ListPluginComponent extends CellPluginComponent {
}
group.addSeparator();
group.add(new MyAnAction("Uninstall", KeyEvent.VK_BACK_SPACE) {
group.add(new MyAnAction("Uninstall", IdeActions.ACTION_EDITOR_DELETE, EventHandler.DELETE_CODE) {
@Override
public void actionPerformed(@NotNull AnActionEvent e) {
for (CellPluginComponent component : selection) {
@@ -404,7 +404,7 @@ public class ListPluginComponent extends CellPluginComponent {
myPluginModel.changeEnableDisable(result.second, result.first);
}
}
else if (keyCode == KeyEvent.VK_BACK_SPACE) {
else if (keyCode == EventHandler.DELETE_CODE) {
for (CellPluginComponent component : selection) {
if (((ListPluginComponent)component).myUninstalled || component.myPlugin.isBundled()) {
return;
@@ -465,9 +465,16 @@ public class ListPluginComponent extends CellPluginComponent {
}
private abstract static class MyAnAction extends DumbAwareAction {
MyAnAction(@Nullable String text, int keyCode) {
MyAnAction(@Nullable String text, @Nullable String actionId, int keyCode) {
super(text);
setShortcutSet(new CustomShortcutSet(KeyStroke.getKeyStroke(keyCode, 0)));
ShortcutSet shortcutSet = null;
if (actionId != null) {
shortcutSet = EventHandler.getShortcuts(actionId);
}
if (shortcutSet == null) {
shortcutSet = new CustomShortcutSet(KeyStroke.getKeyStroke(keyCode, 0));
}
setShortcutSet(shortcutSet);
}
}
}
@@ -4,6 +4,7 @@ package com.intellij.ide.plugins.newui;
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.ModalityState;
import com.intellij.util.ui.UIUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -13,8 +14,6 @@ import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;
import static com.intellij.util.ui.UIUtil.uiChildren;
/**
* @author Alexander Lobas
*/
@@ -33,6 +32,7 @@ public class MultiSelectionEventHandler extends EventHandler {
private final FocusListener myFocusListener;
private final ShortcutSet mySelectAllKeys;
private final ShortcutSet myDeleteKeys;
private boolean myAllSelected;
private boolean myMixSelection;
@@ -117,6 +117,7 @@ public class MultiSelectionEventHandler extends EventHandler {
};
mySelectAllKeys = getShortcuts(IdeActions.ACTION_SELECT_ALL);
myDeleteKeys = getShortcuts(IdeActions.ACTION_EDITOR_DELETE);
myKeyListener = new KeyAdapter() {
@Override
@@ -130,6 +131,9 @@ public class MultiSelectionEventHandler extends EventHandler {
selectAll();
return;
}
if (check(shortcut, myDeleteKeys)) {
code = DELETE_CODE;
}
if (code == KeyEvent.VK_HOME || code == KeyEvent.VK_END) {
if (myComponents.isEmpty()) {
@@ -163,7 +167,7 @@ public class MultiSelectionEventHandler extends EventHandler {
int pageCount = myContainer.getVisibleRect().height / myLayout.myLineHeight;
moveOrResizeSelection(code == KeyEvent.VK_PAGE_UP, !event.isShiftDown(), pageCount);
}
else if (code == KeyEvent.VK_SPACE || code == KeyEvent.VK_ENTER || code == KeyEvent.VK_BACK_SPACE) {
else if (code == KeyEvent.VK_SPACE || code == KeyEvent.VK_ENTER || code == DELETE_CODE) {
assert mySelectionLength != 0;
CellPluginComponent component = myComponents.get(mySelectionIndex);
if (component.getSelection() != SelectionType.SELECTION) {
@@ -189,23 +193,6 @@ public class MultiSelectionEventHandler extends EventHandler {
};
}
@Nullable
private static ShortcutSet getShortcuts(@NotNull String id) {
AnAction action = ActionManager.getInstance().getAction(id);
return action == null ? null : action.getShortcutSet();
}
private static boolean check(@NotNull KeyboardShortcut shortcut, @Nullable ShortcutSet set) {
if (set != null) {
for (Shortcut test : set.getShortcuts()) {
if (test.isKeyboard() && shortcut.startsWith(test)) {
return true;
}
}
}
return false;
}
@Override
public void connect(@NotNull PluginsGroupComponent container) {
myContainer = container;
@@ -462,7 +449,7 @@ public class MultiSelectionEventHandler extends EventHandler {
@Override
public void addAll(@NotNull Component component) {
add(component);
for (Component child : uiChildren(component)) {
for (Component child : UIUtil.uiChildren(component)) {
addAll(child);
}
}