Helios-KeyBindings.st 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. Smalltalk current createPackage: 'Helios-KeyBindings'!
  2. Object subclass: #HLBinding
  3. instanceVariableNames: 'key label'
  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. release
  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. ghostText: self command displayLabel;
  74. inputCompletion: self command inputCompletion;
  75. callback: [ :val |
  76. self command
  77. input: val;
  78. execute ];
  79. yourself
  80. ! !
  81. !HLBindingAction methodsFor: 'actions'!
  82. applyOn: aKeyBinder
  83. self command isInputRequired
  84. ifTrue: [ aKeyBinder selectBinding: self inputBinding ]
  85. ifFalse: [ self command execute ]
  86. ! !
  87. !HLBindingAction methodsFor: 'testing'!
  88. isActive
  89. ^ self command isActive
  90. !
  91. isFinal
  92. ^ self command isInputRequired not
  93. ! !
  94. HLBinding subclass: #HLBindingGroup
  95. instanceVariableNames: 'bindings'
  96. package: 'Helios-KeyBindings'!
  97. !HLBindingGroup methodsFor: 'accessing'!
  98. activeBindings
  99. ^ self bindings select: [ :each | each isActive ]
  100. !
  101. add: aBinding
  102. ^ self bindings add: aBinding
  103. !
  104. addActionKey: anInteger labelled: aString callback: aBlock
  105. self add: ((HLBindingAction on: anInteger labelled: aString)
  106. callback: aBlock;
  107. yourself)
  108. !
  109. addGroupKey: anInteger labelled: aString
  110. self add: (HLBindingGroup on: anInteger labelled: aString)
  111. !
  112. at: aString
  113. ^ self bindings
  114. detect: [ :each | each label = aString ]
  115. ifNone: [ nil ]
  116. !
  117. at: aString add: aBinding
  118. | binding |
  119. binding := self at: aString.
  120. binding ifNil: [ ^ self ].
  121. binding add: aBinding
  122. !
  123. atKey: anInteger
  124. ^ self bindings
  125. detect: [ :each | each key = anInteger ]
  126. ifNone: [ nil ]
  127. !
  128. bindings
  129. ^ bindings ifNil: [ bindings := OrderedCollection new ]
  130. !
  131. displayLabel
  132. ^ super displayLabel, '...'
  133. ! !
  134. !HLBindingGroup methodsFor: 'actions'!
  135. release
  136. self bindings do: [ :each | each release ]
  137. ! !
  138. !HLBindingGroup methodsFor: 'rendering'!
  139. renderOn: aBindingHelper html: html
  140. self isActive ifTrue: [
  141. aBindingHelper renderBindingGroup: self on: html ]
  142. ! !
  143. !HLBindingGroup methodsFor: 'testing'!
  144. isActive
  145. ^ self activeBindings notEmpty
  146. ! !
  147. HLBinding subclass: #HLBindingInput
  148. instanceVariableNames: 'input callback status wrapper binder ghostText isFinal message messageTag inputCompletion'
  149. package: 'Helios-KeyBindings'!
  150. !HLBindingInput methodsFor: 'accessing'!
  151. atKey: aKey
  152. aKey = 13 ifFalse: [ ^ nil ]
  153. !
  154. callback
  155. ^ callback ifNil: [ callback := [ :value | ] ]
  156. !
  157. callback: aBlock
  158. callback := aBlock
  159. !
  160. ghostText
  161. ^ ghostText
  162. !
  163. ghostText: aText
  164. ghostText := aText
  165. !
  166. input
  167. ^ input
  168. !
  169. inputCompletion
  170. ^ inputCompletion ifNil: [ #() ]
  171. !
  172. inputCompletion: aCollection
  173. inputCompletion := aCollection
  174. !
  175. message
  176. ^ message ifNil: [ message := '' ]
  177. !
  178. message: aString
  179. message := aString
  180. !
  181. status
  182. ^ status ifNil: [ status := 'info' ]
  183. !
  184. status: aStatus
  185. status := aStatus
  186. ! !
  187. !HLBindingInput methodsFor: 'actions'!
  188. applyOn: aKeyBinder
  189. self isFinal: true.
  190. self evaluate: self input asJQuery val
  191. !
  192. clearStatus
  193. self status: 'info'.
  194. self message: ''.
  195. self refresh
  196. !
  197. errorStatus
  198. self status: 'error'.
  199. self refresh
  200. !
  201. evaluate: aString
  202. [ self callback value: aString ]
  203. on: Error
  204. do: [:ex |
  205. self input asJQuery
  206. one: 'keydown'
  207. do: [ self clearStatus ].
  208. self message: ex messageText.
  209. self errorStatus.
  210. self isFinal: false ].
  211. !
  212. release
  213. status := nil.
  214. wrapper := nil.
  215. binder := nil
  216. ! !
  217. !HLBindingInput methodsFor: 'rendering'!
  218. refresh
  219. wrapper ifNil: [ ^ self ].
  220. wrapper class: self status.
  221. messageTag contents: self message
  222. !
  223. renderOn: aBinder html: html
  224. binder := aBinder.
  225. wrapper ifNil: [ wrapper := html span ].
  226. wrapper
  227. class: self status;
  228. with: [
  229. input := html input
  230. placeholder: self ghostText;
  231. yourself.
  232. input asJQuery
  233. typeahead: #{ 'source' -> self inputCompletion }.
  234. messageTag := (html span
  235. class: 'help-inline';
  236. with: self message;
  237. yourself) ].
  238. "Evaluate with a timeout to ensure focus.
  239. Commands can be executed from a menu, clicking on the menu to
  240. evaluate the command would give it the focus otherwise"
  241. [ input asJQuery focus ] valueWithTimeout: 10
  242. ! !
  243. !HLBindingInput methodsFor: 'testing'!
  244. isActive
  245. ^ true
  246. !
  247. isFinal
  248. ^ isFinal ifNil: [ isFinal := super isFinal ]
  249. !
  250. isFinal: aBoolean
  251. isFinal := aBoolean
  252. ! !
  253. Object subclass: #HLKeyBinder
  254. instanceVariableNames: 'modifierKey helper bindings selectedBinding'
  255. package: 'Helios-KeyBindings'!
  256. !HLKeyBinder methodsFor: 'accessing'!
  257. activationKey
  258. "SPACE"
  259. ^ 32
  260. !
  261. activationKeyLabel
  262. ^ 'ctrl + space'
  263. !
  264. bindings
  265. ^ bindings ifNil: [ bindings := self defaultBindings ]
  266. !
  267. escapeKey
  268. "ESC"
  269. ^ 27
  270. !
  271. helper
  272. ^ helper
  273. !
  274. selectedBinding
  275. ^ selectedBinding ifNil: [ self bindings ]
  276. ! !
  277. !HLKeyBinder methodsFor: 'actions'!
  278. activate
  279. self helper show
  280. !
  281. applyBinding: aBinding
  282. aBinding isActive ifFalse: [ ^ self ].
  283. self selectBinding: aBinding.
  284. aBinding applyOn: self.
  285. aBinding isFinal ifTrue: [ self deactivate ]
  286. !
  287. deactivate
  288. selectedBinding ifNotNil: [ selectedBinding release ].
  289. selectedBinding := nil.
  290. self helper hide
  291. !
  292. flushBindings
  293. bindings := nil
  294. !
  295. selectBinding: aBinding
  296. aBinding = selectedBinding ifTrue: [ ^ self ].
  297. selectedBinding := aBinding.
  298. self helper refresh
  299. ! !
  300. !HLKeyBinder methodsFor: 'defaults'!
  301. defaultBindings
  302. | group |
  303. group := HLBindingGroup new
  304. addGroupKey: 86 labelled: 'View';
  305. add: HLCloseTabCommand new asBinding;
  306. yourself.
  307. HLOpenCommand registerConcreteClassesOn: group.
  308. ^ group
  309. ! !
  310. !HLKeyBinder methodsFor: 'events'!
  311. handleActiveKeyDown: event
  312. "ESC or ctrl+g deactivate the keyBinder"
  313. (event which = self escapeKey or: [
  314. event which = 71 and: [ event ctrlKey ] ])
  315. ifTrue: [
  316. self deactivate.
  317. event preventDefault.
  318. ^ false ].
  319. "Handle the keybinding"
  320. ^ self handleBindingFor: event
  321. !
  322. handleBindingFor: anEvent
  323. | binding |
  324. binding := self selectedBinding atKey: anEvent which.
  325. binding ifNotNil: [
  326. self applyBinding: binding.
  327. anEvent preventDefault.
  328. ^ false ]
  329. !
  330. handleInactiveKeyDown: event
  331. event which = self activationKey ifTrue: [
  332. event ctrlKey ifTrue: [
  333. self activate.
  334. event preventDefault.
  335. ^ false ] ]
  336. !
  337. handleKeyDown: event
  338. ^ self isActive
  339. ifTrue: [ self handleActiveKeyDown: event ]
  340. ifFalse: [ self handleInactiveKeyDown: event ]
  341. !
  342. setupEvents
  343. (window jQuery: 'body') keydown: [ :event | self handleKeyDown: event ]
  344. ! !
  345. !HLKeyBinder methodsFor: 'initialization'!
  346. initialize
  347. super initialize.
  348. helper := HLKeyBinderHelper on: self.
  349. helper
  350. renderStart;
  351. renderCog
  352. ! !
  353. !HLKeyBinder methodsFor: 'testing'!
  354. isActive
  355. ^ ('.', self helper cssClass) asJQuery is: ':visible'
  356. !
  357. systemIsMac
  358. ^ navigator platform match: 'Mac'
  359. ! !
  360. HLWidget subclass: #HLKeyBinderHelper
  361. instanceVariableNames: 'keyBinder'
  362. package: 'Helios-KeyBindings'!
  363. !HLKeyBinderHelper methodsFor: 'accessing'!
  364. cssClass
  365. ^ 'key_helper'
  366. !
  367. keyBinder
  368. ^ keyBinder
  369. !
  370. keyBinder: aKeyBinder
  371. keyBinder := aKeyBinder
  372. !
  373. selectedBinding
  374. ^ self keyBinder selectedBinding
  375. ! !
  376. !HLKeyBinderHelper methodsFor: 'actions'!
  377. hide
  378. ('.', self cssClass) asJQuery remove.
  379. self showCog
  380. !
  381. hideCog
  382. '#cog-helper' asJQuery hide
  383. !
  384. show
  385. self hideCog.
  386. self appendToJQuery: 'body' asJQuery
  387. !
  388. showCog
  389. '#cog-helper' asJQuery show
  390. ! !
  391. !HLKeyBinderHelper methodsFor: 'keyBindings'!
  392. registerBindings
  393. "Do nothing"
  394. ! !
  395. !HLKeyBinderHelper methodsFor: 'rendering'!
  396. renderBindingGroup: aBindingGroup on: html
  397. (aBindingGroup activeBindings
  398. sorted: [ :a :b | a key < b key ])
  399. do: [ :each | each renderActionFor: self keyBinder html: html ]
  400. !
  401. renderBindingOn: html
  402. self selectedBinding renderOn: self html: html
  403. !
  404. renderCloseOn: html
  405. html a
  406. class: 'close';
  407. with: [ (html tag: 'i') class: 'icon-remove' ];
  408. onClick: [ self keyBinder deactivate ]
  409. !
  410. renderCog
  411. [ :html |
  412. html
  413. div id: 'cog-helper';
  414. with: [
  415. html a
  416. with: [ (html tag: 'i') class: 'icon-cog' ];
  417. onClick: [ self keyBinder activate ] ] ]
  418. appendToJQuery: 'body' asJQuery
  419. !
  420. renderContentOn: html
  421. html div class: self cssClass; with: [
  422. self
  423. renderSelectionOn:html;
  424. renderBindingOn: html;
  425. renderCloseOn: html ]
  426. !
  427. renderSelectionOn: html
  428. html span
  429. class: 'selected';
  430. with: (self selectedBinding label ifNil: [ 'Action' ])
  431. !
  432. renderStart
  433. [ :html |
  434. html div
  435. id: 'helper';
  436. with: 'Press ', self keyBinder activationKeyLabel, ' to start' ] appendToJQuery: 'body' asJQuery.
  437. [ (window jQuery: '#helper') fadeOut: 1000 ]
  438. valueWithTimeout: 2000
  439. ! !
  440. !HLKeyBinderHelper class methodsFor: 'instance creation'!
  441. on: aKeyBinder
  442. ^ self new
  443. keyBinder: aKeyBinder;
  444. yourself
  445. ! !