-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
81 changed files
with
2,426 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package jetbrains.datalore.base.event | ||
|
||
enum class Key private constructor(private val myValue: String) { | ||
A("A"), | ||
B("B"), | ||
C("C"), | ||
D("D"), | ||
E("E"), | ||
F("F"), | ||
G("G"), | ||
H("H"), | ||
I("I"), | ||
J("J"), | ||
K("K"), | ||
L("L"), | ||
M("M"), | ||
N("N"), | ||
O("O"), | ||
P("P"), | ||
Q("Q"), | ||
R("R"), | ||
S("S"), | ||
T("T"), | ||
U("U"), | ||
V("V"), | ||
W("W"), | ||
X("X"), | ||
Y("Y"), | ||
Z("Z"), | ||
DIGIT_0("0"), | ||
DIGIT_1("1"), | ||
DIGIT_2("2"), | ||
DIGIT_3("3"), | ||
DIGIT_4("4"), | ||
DIGIT_5("5"), | ||
DIGIT_6("6"), | ||
DIGIT_7("7"), | ||
DIGIT_8("8"), | ||
DIGIT_9("9"), | ||
LEFT_BRACE("["), | ||
RIGHT_BRACE("]"), | ||
UP("Up"), | ||
DOWN("Down"), | ||
LEFT("Left"), | ||
RIGHT("Right"), | ||
PAGE_UP("Page Up"), | ||
PAGE_DOWN("Page Down"), | ||
ESCAPE("Escape"), | ||
ENTER("Enter"), | ||
HOME("Home"), | ||
END("End"), | ||
TAB("Tab"), | ||
SPACE("Space"), | ||
INSERT("Insert"), | ||
DELETE("Delete"), | ||
BACKSPACE("Backspace"), | ||
EQUALS("Equals"), | ||
BACK_QUOTE("`"), | ||
PLUS("Plus"), | ||
MINUS("Minus"), | ||
SLASH("Slash"), | ||
CONTROL("Ctrl"), | ||
META("Meta"), | ||
ALT("Alt"), | ||
SHIFT("Shift"), | ||
UNKNOWN("?"), | ||
F1("F1"), | ||
F2("F2"), | ||
F3("F3"), | ||
F4("F4"), | ||
F5("F5"), | ||
F6("F6"), | ||
F7("F7"), | ||
F8("F8"), | ||
F9("F9"), | ||
F10("F10"), | ||
F11("F11"), | ||
F12("F12"), | ||
COMMA(","), | ||
PERIOD("."); | ||
|
||
override fun toString(): String { | ||
return myValue | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package jetbrains.datalore.base.event | ||
|
||
class KeyEvent : Event { | ||
|
||
val keyStroke: KeyStroke | ||
val keyChar: Char | ||
|
||
val key: Key | ||
get() = keyStroke.key | ||
|
||
val modifiers: Set<ModifierKey> | ||
get() = keyStroke.modifiers | ||
|
||
constructor(keyStroke: KeyStroke) { | ||
this.keyStroke = keyStroke | ||
keyChar = 0.toChar() | ||
} | ||
|
||
constructor(key: Key, ch: Char = 0.toChar()) { | ||
keyStroke = KeyStroke(key, emptyList()) | ||
keyChar = ch | ||
} | ||
|
||
constructor(key: Key, ch: Char, modifiers: Collection<ModifierKey>) { | ||
keyStroke = KeyStroke(key, modifiers) | ||
keyChar = ch | ||
} | ||
|
||
fun `is`(key: Key, vararg modifiers: ModifierKey): Boolean { | ||
return keyStroke.`is`(key, *modifiers) | ||
} | ||
|
||
fun `is`(vararg specs: KeyStrokeSpec): Boolean { | ||
for (s in specs) { | ||
if (s.matches(keyStroke)) return true | ||
} | ||
return false | ||
} | ||
|
||
fun `is`(vararg specs: KeyStroke): Boolean { | ||
for (s in specs) { | ||
if (s.matches(keyStroke)) return true | ||
} | ||
return false | ||
} | ||
|
||
fun has(key: ModifierKey): Boolean { | ||
return keyStroke.has(key) | ||
} | ||
|
||
fun copy(): KeyEvent { | ||
return KeyEvent(key, keyChar, modifiers) | ||
} | ||
|
||
override fun toString(): String { | ||
return keyStroke.toString() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package jetbrains.datalore.base.event | ||
|
||
class KeyStroke { | ||
val key: Key | ||
val modifiers: Set<ModifierKey> | ||
|
||
constructor(key: Key, vararg modifiers: ModifierKey) : this(key, modifiers.asList()) | ||
|
||
constructor(key: Key, modifiers: Collection<ModifierKey>) { | ||
this.key = key | ||
this.modifiers = HashSet(modifiers) | ||
} | ||
|
||
fun has(key: ModifierKey): Boolean { | ||
return modifiers.contains(key) | ||
} | ||
|
||
fun `is`(key: Key, vararg modifiers: ModifierKey): Boolean { | ||
return matches(KeyStroke(key, *modifiers)) | ||
} | ||
|
||
fun matches(keyStroke: KeyStroke): Boolean { | ||
return equals(keyStroke) | ||
} | ||
|
||
fun with(key: ModifierKey): KeyStroke { | ||
val keys = HashSet(modifiers) | ||
keys.add(key) | ||
return KeyStroke(this.key, keys) | ||
} | ||
|
||
override fun hashCode(): Int { | ||
return key.hashCode() * 31 + modifiers.hashCode() | ||
} | ||
|
||
override fun equals(other: Any?): Boolean { | ||
if (other !is KeyStroke) return false | ||
val otherKeyStroke = other as KeyStroke? | ||
|
||
return key === otherKeyStroke!!.key && modifiers == otherKeyStroke!!.modifiers | ||
} | ||
|
||
override fun toString(): String { | ||
return "$key $modifiers" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package jetbrains.datalore.base.event | ||
|
||
class KeyStrokeSpec { | ||
|
||
private val myKeyStrokes: Array<KeyStroke> | ||
|
||
val keyStrokes: Iterable<KeyStroke> | ||
get() = listOf(*myKeyStrokes) | ||
|
||
val isEmpty: Boolean | ||
get() = myKeyStrokes.isEmpty() | ||
|
||
constructor(key: Key, vararg modifiers: ModifierKey) { | ||
myKeyStrokes = arrayOf(KeyStroke(key, *modifiers)) | ||
} | ||
|
||
constructor(keyStrokes: Collection<KeyStroke>) { | ||
myKeyStrokes = keyStrokes.toTypedArray() | ||
} | ||
|
||
constructor(vararg keyStrokes: KeyStroke) { | ||
myKeyStrokes = arrayOf(*keyStrokes) | ||
} | ||
|
||
fun matches(keyStroke: KeyStroke): Boolean { | ||
for (spec in myKeyStrokes) { | ||
if (spec.matches(keyStroke)) { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
fun with(key: ModifierKey): KeyStrokeSpec { | ||
val modified = ArrayList<KeyStroke>() | ||
for (keyStroke in myKeyStrokes) { | ||
modified.add(keyStroke.with(key)) | ||
} | ||
return KeyStrokeSpec(modified) | ||
} | ||
|
||
override fun equals(other: Any?): Boolean { | ||
if (this === other) return true | ||
if (other == null || this::class != other::class) return false | ||
|
||
val that = other as KeyStrokeSpec? | ||
return keyStrokes == that!!.keyStrokes | ||
} | ||
|
||
override fun hashCode(): Int { | ||
return keyStrokes.hashCode() | ||
} | ||
|
||
override fun toString(): String { | ||
return keyStrokes.toString() | ||
} | ||
|
||
} |
110 changes: 110 additions & 0 deletions
110
base/src/commonMain/kotlin/base/event/KeyStrokeSpecs.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package jetbrains.datalore.base.event | ||
|
||
import jetbrains.datalore.base.event.ModifierKey.* | ||
|
||
object KeyStrokeSpecs { | ||
val COPY = composite(ctrlOrMeta(Key.C), KeyStrokeSpec(Key.INSERT, CONTROL)) | ||
val CUT = composite(ctrlOrMeta(Key.X), KeyStrokeSpec(Key.DELETE, SHIFT)) | ||
val PASTE = composite(ctrlOrMeta(Key.V), KeyStrokeSpec(Key.INSERT, SHIFT)) | ||
|
||
val UNDO = ctrlOrMeta(Key.Z) | ||
val REDO = UNDO.with(SHIFT) | ||
|
||
val COMPLETE = KeyStrokeSpec(Key.SPACE, CONTROL) | ||
|
||
val SHOW_DOC = composite(KeyStrokeSpec(Key.F1), ctrlOrMeta(Key.J)) | ||
|
||
val HELP = composite(ctrlOrMeta(Key.I), ctrlOrMeta(Key.F1)) | ||
|
||
val HOME = composite(KeyStroke(Key.HOME), KeyStroke(Key.LEFT, META)) | ||
val END = composite(KeyStroke(Key.END), KeyStroke(Key.RIGHT, META)) | ||
|
||
val FILE_HOME = ctrlOrMeta(Key.HOME) | ||
val FILE_END = ctrlOrMeta(Key.END) | ||
|
||
val PREV_WORD = ctrlOrAlt(Key.LEFT) | ||
val NEXT_WORD = ctrlOrAlt(Key.RIGHT) | ||
|
||
val NEXT_EDITABLE = ctrlOrMeta(Key.RIGHT, ALT) | ||
val PREV_EDITABLE = ctrlOrMeta(Key.LEFT, ALT) | ||
|
||
val SELECT_ALL = ctrlOrMeta(Key.A) | ||
|
||
val SELECT_FILE_HOME = FILE_HOME.with(SHIFT) | ||
val SELECT_FILE_END = FILE_END.with(SHIFT) | ||
|
||
val SELECT_HOME = HOME.with(SHIFT) | ||
val SELECT_END = END.with(SHIFT) | ||
|
||
val SELECT_WORD_FORWARD = NEXT_WORD.with(SHIFT) | ||
val SELECT_WORD_BACKWARD = PREV_WORD.with(SHIFT) | ||
|
||
val SELECT_LEFT = KeyStrokeSpec(Key.LEFT, SHIFT) | ||
val SELECT_RIGHT = KeyStrokeSpec(Key.RIGHT, SHIFT) | ||
|
||
val SELECT_UP = KeyStrokeSpec(Key.UP, SHIFT) | ||
val SELECT_DOWN = KeyStrokeSpec(Key.DOWN, SHIFT) | ||
|
||
val INCREASE_SELECTION = KeyStrokeSpec(Key.UP, ALT) | ||
val DECREASE_SELECTION = KeyStrokeSpec(Key.DOWN, ALT) | ||
|
||
val INSERT_BEFORE = composite( | ||
KeyStroke(Key.ENTER, add(META)), | ||
KeyStroke(Key.INSERT), | ||
KeyStroke(Key.ENTER, add(CONTROL)) | ||
) | ||
val INSERT_AFTER = KeyStrokeSpec(Key.ENTER) | ||
val INSERT = composite(INSERT_BEFORE, INSERT_AFTER) | ||
|
||
val DUPLICATE = ctrlOrMeta(Key.D) | ||
|
||
val DELETE_CURRENT = composite(ctrlOrMeta(Key.BACKSPACE), ctrlOrMeta(Key.DELETE)) | ||
|
||
val DELETE_TO_WORD_START = KeyStrokeSpec(Key.BACKSPACE, ALT) | ||
|
||
val MATCHING_CONSTRUCTS = composite(ctrlOrMeta(Key.LEFT_BRACE, ALT), ctrlOrMeta(Key.RIGHT_BRACE, ALT)) | ||
|
||
val NAVIGATE = ctrlOrMeta(Key.B) | ||
val NAVIGATE_BACK = ctrlOrMeta(Key.LEFT_BRACE) | ||
val NAVIGATE_FORWARD = ctrlOrMeta(Key.RIGHT_BRACE) | ||
|
||
fun ctrlOrMeta(key: Key, vararg modifiers: ModifierKey): KeyStrokeSpec { | ||
return composite(KeyStroke(key, add(CONTROL, *modifiers)), KeyStroke(key, add(META, *modifiers))) | ||
} | ||
|
||
fun ctrlOrAlt(key: Key, vararg modifiers: ModifierKey): KeyStrokeSpec { | ||
return composite(KeyStroke(key, add(CONTROL, *modifiers)), KeyStroke(key, add(ALT, *modifiers))) | ||
} | ||
|
||
private fun add(key: ModifierKey, vararg otherKeys: ModifierKey): Set<ModifierKey> { | ||
val result = HashSet(otherKeys.asList()) | ||
result.add(key) | ||
return result | ||
} | ||
|
||
fun composite(vararg specs: KeyStrokeSpec): KeyStrokeSpec { | ||
val keyStrokes = HashSet<KeyStroke>() | ||
for (spec in specs) { | ||
for (ks in spec.keyStrokes) { | ||
keyStrokes.add(ks) | ||
} | ||
} | ||
return KeyStrokeSpec(keyStrokes) | ||
} | ||
|
||
fun composite(vararg specs: KeyStroke): KeyStrokeSpec { | ||
return KeyStrokeSpec(*specs) | ||
} | ||
|
||
fun withoutShift(spec: KeyStrokeSpec): KeyEvent { | ||
val keyStroke = spec.keyStrokes.iterator().next() | ||
val modifiers = keyStroke.modifiers | ||
val withoutShift = HashSet<ModifierKey>() | ||
for (modifier in modifiers) { | ||
if (modifier !== SHIFT) { | ||
withoutShift.add(modifier) | ||
} | ||
} | ||
return KeyEvent(keyStroke.key, 0.toChar(), withoutShift) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package jetbrains.datalore.base.event | ||
|
||
enum class ModifierKey private constructor() { | ||
|
||
CONTROL, | ||
ALT, | ||
SHIFT, | ||
META | ||
} |
Oops, something went wrong.