2
0

Helios-KeyBindings.st 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. Smalltalk current createPackage: 'Helios-KeyBindings'!
  2. Object subclass: #HLBinding
  3. instanceVariableNames: 'key label each'
  4. package: 'Helios-KeyBindings'!
  5. !HLBinding methodsFor: 'accessing'!
  6. atKey: aKey
  7. ^ nil
  8. !
  9. displayLabel
  10. ^ self label
  11. !
  12. key
  13. ^ key
  14. !
  15. key: anInteger
  16. key := anInteger
  17. !
  18. label
  19. ^ label
  20. !
  21. label: aString
  22. label := aString
  23. !
  24. shortcut
  25. ^ String fromCharCode: self key
  26. ! !
  27. !HLBinding methodsFor: 'actions'!
  28. applyOn: aKeyBinder
  29. !
  30. applyOn: aKeyBinder then: aBlock
  31. ! !
  32. !HLBinding methodsFor: 'rendering'!
  33. renderActionFor: aBinder html: html
  34. html span class: 'command'; with: [
  35. html span
  36. class: 'label';
  37. with: self shortcut asLowercase.
  38. html a
  39. class: 'action';
  40. with: self displayLabel;
  41. onClick: [ aBinder applyBinding: self ] ]
  42. !
  43. renderOn: aBindingHelper html: html
  44. ! !
  45. !HLBinding methodsFor: 'testing'!
  46. isActive
  47. ^ self subclassResponsibility
  48. !
  49. isFinal
  50. " Answer true if the receiver is the final binding of a sequence "
  51. ^ false
  52. ! !
  53. !HLBinding class methodsFor: 'instance creation'!
  54. on: anInteger labelled: aString
  55. ^ self new
  56. key: anInteger;
  57. label: aString;
  58. yourself
  59. ! !
  60. HLBinding subclass: #HLBindingAction
  61. instanceVariableNames: 'command'
  62. package: 'Helios-KeyBindings'!
  63. !HLBindingAction methodsFor: 'accessing'!
  64. command
  65. ^ command
  66. !
  67. command: aCommand
  68. command := aCommand
  69. !
  70. inputBinding
  71. ^ HLBindingInput new
  72. label: self command inputLabel;
  73. callback: [ :val |
  74. self command
  75. input: val;
  76. execute ];
  77. yourself
  78. ! !
  79. !HLBindingAction methodsFor: 'actions'!
  80. applyOn: aKeyBinder
  81. self command isInputRequired
  82. ifTrue: [ aKeyBinder selectBinding: self inputBinding ]
  83. ifFalse: [ self command execute ]
  84. ! !
  85. !HLBindingAction methodsFor: 'testing'!
  86. isActive
  87. ^ self command isActive
  88. !
  89. isFinal
  90. ^ self command isInputRequired not
  91. ! !
  92. HLBinding subclass: #HLBindingGroup
  93. instanceVariableNames: 'bindings'
  94. package: 'Helios-KeyBindings'!
  95. !HLBindingGroup methodsFor: 'accessing'!
  96. activeBindings
  97. ^ self bindings select: [ :each | each isActive ]
  98. !
  99. add: aBinding
  100. ^ self bindings add: aBinding
  101. !
  102. addActionKey: anInteger labelled: aString callback: aBlock
  103. self add: ((HLBindingAction on: anInteger labelled: aString)
  104. callback: aBlock;
  105. yourself)
  106. !
  107. addGroupKey: anInteger labelled: aString
  108. self add: (HLBindingGroup on: anInteger labelled: aString)
  109. !
  110. at: aString
  111. ^ self bindings
  112. detect: [ :each | each label = aString ]
  113. ifNone: [ nil ]
  114. !
  115. at: aString add: aBinding
  116. | binding |
  117. binding := self at: aString.
  118. binding ifNil: [ ^ self ].
  119. binding add: aBinding
  120. !
  121. atKey: anInteger
  122. ^ self bindings
  123. detect: [ :each | each key = anInteger ]
  124. ifNone: [ nil ]
  125. !
  126. bindings
  127. ^ bindings ifNil: [ bindings := OrderedCollection new ]
  128. !
  129. displayLabel
  130. ^ super displayLabel, '...'
  131. ! !
  132. !HLBindingGroup methodsFor: 'rendering'!
  133. renderOn: aBindingHelper html: html
  134. self isActive ifTrue: [
  135. aBindingHelper renderBindingGroup: self on: html ]
  136. ! !
  137. !HLBindingGroup methodsFor: 'testing'!
  138. isActive
  139. ^ self activeBindings notEmpty
  140. ! !
  141. HLBinding subclass: #HLBindingInput
  142. instanceVariableNames: 'input callback'
  143. package: 'Helios-KeyBindings'!
  144. !HLBindingInput methodsFor: 'accessing'!
  145. callback
  146. ^ callback ifNil: [ callback := [ :value | ] ]
  147. !
  148. callback: aBlock
  149. callback := aBlock
  150. !
  151. input
  152. ^ input asJQuery val
  153. !
  154. renderActionFor: aBinder html: html
  155. html span
  156. class: 'command';
  157. with: [
  158. input := html input
  159. class: 'search-query';
  160. placeholder: self displayLabel ].
  161. input onKeyPress: [:event |
  162. event keyCode = 13 ifTrue: [
  163. self applyOn: aBinder ] ].
  164. input asJQuery focus
  165. ! !
  166. !HLBindingInput methodsFor: 'actions'!
  167. applyOn: aKeyBinder
  168. self evaluate: self input
  169. !
  170. evaluate: aString
  171. self callback value: aString
  172. ! !
  173. !HLBindingInput methodsFor: 'testing'!
  174. isActive
  175. ^ true
  176. ! !
  177. Object subclass: #HLKeyBinder
  178. instanceVariableNames: 'modifierKey helper bindings selectedBinding'
  179. package: 'Helios-KeyBindings'!
  180. !HLKeyBinder methodsFor: 'accessing'!
  181. activationKey
  182. "SPACE"
  183. ^ 32
  184. !
  185. activationKeyLabel
  186. ^ 'ctrl + space'
  187. !
  188. bindings
  189. ^ bindings ifNil: [ bindings := self defaultBindings ]
  190. !
  191. escapeKey
  192. "ESC"
  193. ^ 27
  194. !
  195. helper
  196. ^ helper
  197. !
  198. selectedBinding
  199. ^ selectedBinding ifNil: [ self bindings ]
  200. ! !
  201. !HLKeyBinder methodsFor: 'actions'!
  202. activate
  203. self helper show
  204. !
  205. applyBinding: aBinding
  206. aBinding isActive ifFalse: [ ^ self ].
  207. self selectBinding: aBinding.
  208. aBinding applyOn: self.
  209. aBinding isFinal ifTrue: [ self deactivate ]
  210. !
  211. deactivate
  212. selectedBinding := nil.
  213. self helper hide
  214. !
  215. flushBindings
  216. bindings := nil
  217. !
  218. selectBinding: aBinding
  219. selectedBinding := aBinding.
  220. self helper refresh
  221. ! !
  222. !HLKeyBinder methodsFor: 'defaults'!
  223. defaultBindings
  224. | group |
  225. group := HLBindingGroup new
  226. addGroupKey: 86 labelled: 'View';
  227. add: HLCloseTabCommand new asBinding;
  228. yourself.
  229. HLOpenCommand registerConcreteClassesOn: group.
  230. ^ group
  231. ! !
  232. !HLKeyBinder methodsFor: 'events'!
  233. handleActiveKeyDown: event
  234. "ESC or ctrl+g deactivate the keyBinder"
  235. (event which = self escapeKey or: [
  236. event which = 71 and: [ event ctrlKey ] ])
  237. ifTrue: [
  238. self deactivate.
  239. event preventDefault.
  240. ^ false ].
  241. "Handle the keybinding"
  242. ^ self handleBindingFor: event
  243. !
  244. handleBindingFor: anEvent
  245. | binding |
  246. binding := self selectedBinding atKey: anEvent which.
  247. binding ifNotNil: [
  248. self applyBinding: binding.
  249. anEvent preventDefault.
  250. ^ false ]
  251. !
  252. handleInactiveKeyDown: event
  253. event which = self activationKey ifTrue: [
  254. event ctrlKey ifTrue: [
  255. self activate.
  256. event preventDefault.
  257. ^ false ] ]
  258. !
  259. handleKeyDown: event
  260. ^ self isActive
  261. ifTrue: [ self handleActiveKeyDown: event ]
  262. ifFalse: [ self handleInactiveKeyDown: event ]
  263. !
  264. setupEvents
  265. (window jQuery: 'body') keydown: [ :event | self handleKeyDown: event ]
  266. ! !
  267. !HLKeyBinder methodsFor: 'initialization'!
  268. initialize
  269. super initialize.
  270. helper := HLKeyBinderHelper on: self.
  271. helper
  272. renderStart;
  273. renderCog.
  274. active := false
  275. ! !
  276. !HLKeyBinder methodsFor: 'testing'!
  277. isActive
  278. ^ ('.', self helper cssClass) asJQuery is: ':visible'
  279. !
  280. systemIsMac
  281. ^ navigator platform match: 'Mac'
  282. ! !
  283. HLWidget subclass: #HLKeyBinderHelper
  284. instanceVariableNames: 'keyBinder'
  285. package: 'Helios-KeyBindings'!
  286. !HLKeyBinderHelper methodsFor: 'accessing'!
  287. cssClass
  288. ^ 'key_helper'
  289. !
  290. keyBinder
  291. ^ keyBinder
  292. !
  293. keyBinder: aKeyBinder
  294. keyBinder := aKeyBinder
  295. !
  296. selectedBinding
  297. ^ self keyBinder selectedBinding
  298. ! !
  299. !HLKeyBinderHelper methodsFor: 'actions'!
  300. hide
  301. ('.', self cssClass) asJQuery remove.
  302. self showCog
  303. !
  304. hideCog
  305. '#cog-helper' asJQuery hide
  306. !
  307. show
  308. self hideCog.
  309. self appendToJQuery: 'body' asJQuery
  310. !
  311. showCog
  312. '#cog-helper' asJQuery show
  313. ! !
  314. !HLKeyBinderHelper methodsFor: 'keyBindings'!
  315. registerBindings
  316. "Do nothing"
  317. ! !
  318. !HLKeyBinderHelper methodsFor: 'rendering'!
  319. renderBindingGroup: aBindingGroup on: html
  320. (aBindingGroup activeBindings
  321. sorted: [ :a :b | a key < b key ])
  322. do: [ :each | each renderActionFor: self keyBinder html: html ]
  323. !
  324. renderBindingOn: html
  325. self selectedBinding renderOn: self html: html
  326. !
  327. renderCloseOn: html
  328. html a
  329. class: 'close';
  330. with: [ (html tag: 'i') class: 'icon-remove' ];
  331. onClick: [ self keyBinder deactivate ]
  332. !
  333. renderCog
  334. [ :html |
  335. html
  336. div id: 'cog-helper';
  337. with: [
  338. html a
  339. with: [ (html tag: 'i') class: 'icon-cog' ];
  340. onClick: [ self keyBinder activate ] ] ]
  341. appendToJQuery: 'body' asJQuery
  342. !
  343. renderContentOn: html
  344. html div class: self cssClass; with: [
  345. self
  346. renderSelectionOn:html;
  347. renderBindingOn: html;
  348. renderCloseOn: html ]
  349. !
  350. renderSelectionOn: html
  351. html span
  352. class: 'selected';
  353. with: (self selectedBinding label ifNil: [ 'Action' ])
  354. !
  355. renderStart
  356. [ :html |
  357. html div
  358. id: 'keybinding-start-helper';
  359. with: 'Press ', self keyBinder activationKeyLabel, ' to start' ] appendToJQuery: 'body' asJQuery.
  360. [ (window jQuery: '#keybinding-start-helper') fadeOut: 1000 ]
  361. valueWithTimeout: 2000
  362. ! !
  363. !HLKeyBinderHelper class methodsFor: 'instance creation'!
  364. on: aKeyBinder
  365. ^ self new
  366. keyBinder: aKeyBinder;
  367. yourself
  368. ! !