1 /* 2 * KeyboardShortcut.java 3 * 4 * Copyright (C) 2021 by RStudio, PBC 5 * 6 * Unless you have received this program directly from RStudio pursuant 7 * to the terms of a commercial license agreement with RStudio, then 8 * this program is licensed to you under the terms of version 3 of the 9 * GNU Affero General Public License. This program is distributed WITHOUT 10 * ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT, 11 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the 12 * AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details. 13 * 14 */ 15 package org.rstudio.core.client.command; 16 17 import com.google.gwt.dom.client.NativeEvent; 18 19 public class KeyboardShortcut 20 { KeyboardShortcut(KeySequence keySequence, String groupName, String title, String disableModes)21 public KeyboardShortcut(KeySequence keySequence, 22 String groupName, 23 String title, 24 String disableModes) 25 { 26 keySequence_ = keySequence; 27 groupName_ = groupName; 28 order_ = ORDER++; 29 title_ = title; 30 disableModes_ = ShortcutManager.parseDisableModes(disableModes); 31 } 32 KeyboardShortcut(KeySequence keySequence)33 public KeyboardShortcut(KeySequence keySequence) 34 { 35 this(keySequence, "", "", ""); 36 } 37 KeyboardShortcut(String key, int keyCode, int modifiers)38 public KeyboardShortcut(String key, int keyCode, int modifiers) 39 { 40 this(keySequence(key, keyCode, modifiers)); 41 } 42 43 getDisableModes()44 public int getDisableModes() 45 { 46 return disableModes_; 47 } 48 getKeySequence()49 public KeySequence getKeySequence() 50 { 51 return keySequence_; 52 } 53 startsWith(KeyboardShortcut other, boolean strict)54 public boolean startsWith(KeyboardShortcut other, boolean strict) 55 { 56 return getKeySequence().startsWith(other.getKeySequence(), strict); 57 } 58 startsWith(KeySequence sequence, boolean strict)59 public boolean startsWith(KeySequence sequence, boolean strict) 60 { 61 return getKeySequence().startsWith(sequence, strict); 62 } 63 64 @Override equals(Object object)65 public boolean equals(Object object) 66 { 67 if (object == null || !(object instanceof KeyboardShortcut)) 68 return false; 69 70 KeyboardShortcut other = (KeyboardShortcut) object; 71 return keySequence_ == other.keySequence_; 72 } 73 74 @Override hashCode()75 public int hashCode() 76 { 77 return keySequence_.hashCode(); 78 } 79 80 @Override toString()81 public String toString() 82 { 83 return keySequence_.toString(false); 84 } 85 toString(boolean pretty)86 public String toString(boolean pretty) 87 { 88 return keySequence_.toString(pretty); 89 } 90 getGroupName()91 public String getGroupName() 92 { 93 return groupName_; 94 } 95 getOrder()96 public int getOrder() 97 { 98 return order_; 99 } 100 getTitle()101 public String getTitle() 102 { 103 return title_; 104 } 105 isModeDisabled(int mode)106 public boolean isModeDisabled(int mode) 107 { 108 return (mode & disableModes_) > 0; 109 } 110 isModalShortcut()111 public boolean isModalShortcut() 112 { 113 return disableModes_ != MODE_NONE; 114 } 115 getModifierValue(NativeEvent e)116 public static int getModifierValue(NativeEvent e) 117 { 118 int modifiers = 0; 119 if (e.getAltKey()) 120 modifiers += ALT; 121 if (e.getCtrlKey()) 122 modifiers += CTRL; 123 if (e.getMetaKey()) 124 modifiers += META; 125 if (e.getShiftKey()) 126 modifiers += SHIFT; 127 return modifiers; 128 } 129 keySequence(String key, int keyCode, int modifiers)130 private static KeySequence keySequence(String key, 131 int keyCode, 132 int modifiers) 133 { 134 KeySequence sequence = new KeySequence(); 135 sequence.add(new KeyCombination(key, keyCode, modifiers)); 136 return sequence; 137 } 138 139 private final KeySequence keySequence_; 140 141 private String groupName_; 142 private int order_ = 0; 143 private String title_ = ""; 144 private int disableModes_ = MODE_NONE; 145 146 private static int ORDER = 0; 147 148 public static final int NONE = 0; 149 public static final int ALT = 1; 150 public static final int CTRL = 2; 151 public static final int META = 4; 152 public static final int SHIFT = 8; 153 154 public static final int MODE_NONE = 0; 155 public static final int MODE_DEFAULT = 1; 156 public static final int MODE_VIM = 2; 157 public static final int MODE_EMACS = 4; 158 public static final int MODE_SUBLIME = 8; 159 } 160