Helios-KeyBindings.st 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  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. self isFinal ifTrue: [ aKeyBinder deactivate ]
  30. !
  31. release
  32. ! !
  33. !HLBinding methodsFor: 'rendering'!
  34. renderActionFor: aBinder html: html
  35. html span class: 'command'; with: [
  36. html span
  37. class: 'label';
  38. with: self shortcut asLowercase.
  39. html a
  40. class: 'action';
  41. with: self displayLabel;
  42. onClick: [ aBinder applyBinding: self ] ]
  43. !
  44. renderOn: aBindingHelper html: html
  45. ! !
  46. !HLBinding methodsFor: 'testing'!
  47. isActive
  48. ^ self subclassResponsibility
  49. !
  50. isFinal
  51. " Answer true if the receiver is the final binding of a sequence "
  52. ^ false
  53. ! !
  54. !HLBinding class methodsFor: 'instance creation'!
  55. on: anInteger labelled: aString
  56. ^ self new
  57. key: anInteger;
  58. label: aString;
  59. yourself
  60. ! !
  61. HLBinding subclass: #HLBindingAction
  62. instanceVariableNames: 'command'
  63. package: 'Helios-KeyBindings'!
  64. !HLBindingAction methodsFor: 'accessing'!
  65. command
  66. ^ command
  67. !
  68. command: aCommand
  69. command := aCommand
  70. !
  71. inputBinding
  72. ^ HLBindingInput new
  73. label: self command inputLabel;
  74. ghostText: self command displayLabel;
  75. defaultValue: self command defaultInput;
  76. inputCompletion: self command inputCompletion;
  77. callback: [ :val |
  78. self command
  79. input: val;
  80. execute ];
  81. yourself
  82. ! !
  83. !HLBindingAction methodsFor: 'actions'!
  84. applyOn: aKeyBinder
  85. self command isInputRequired
  86. ifTrue: [ aKeyBinder selectBinding: self inputBinding ]
  87. ifFalse: [ self command execute ].
  88. super applyOn: aKeyBinder
  89. ! !
  90. !HLBindingAction methodsFor: 'testing'!
  91. isActive
  92. ^ self command isActive
  93. !
  94. isFinal
  95. ^ self command isInputRequired not
  96. ! !
  97. HLBinding subclass: #HLBindingGroup
  98. instanceVariableNames: 'bindings'
  99. package: 'Helios-KeyBindings'!
  100. !HLBindingGroup methodsFor: 'accessing'!
  101. activeBindings
  102. ^ self bindings select: [ :each | each isActive ]
  103. !
  104. add: aBinding
  105. ^ self bindings add: aBinding
  106. !
  107. addActionKey: anInteger labelled: aString callback: aBlock
  108. self add: ((HLBindingAction on: anInteger labelled: aString)
  109. callback: aBlock;
  110. yourself)
  111. !
  112. addGroupKey: anInteger labelled: aString
  113. self add: (HLBindingGroup on: anInteger labelled: aString)
  114. !
  115. at: aString
  116. ^ self bindings
  117. detect: [ :each | each label = aString ]
  118. ifNone: [ nil ]
  119. !
  120. at: aString add: aBinding
  121. | binding |
  122. binding := self at: aString.
  123. binding ifNil: [ ^ self ].
  124. binding add: aBinding
  125. !
  126. atKey: anInteger
  127. ^ self bindings
  128. detect: [ :each | each key = anInteger ]
  129. ifNone: [ nil ]
  130. !
  131. bindings
  132. ^ bindings ifNil: [ bindings := OrderedCollection new ]
  133. !
  134. displayLabel
  135. ^ super displayLabel, '...'
  136. ! !
  137. !HLBindingGroup methodsFor: 'actions'!
  138. release
  139. self bindings do: [ :each | each release ]
  140. ! !
  141. !HLBindingGroup methodsFor: 'rendering'!
  142. renderOn: aBindingHelper html: html
  143. self isActive ifTrue: [
  144. aBindingHelper renderBindingGroup: self on: html ]
  145. ! !
  146. !HLBindingGroup methodsFor: 'testing'!
  147. isActive
  148. ^ self activeBindings notEmpty
  149. ! !
  150. HLBinding subclass: #HLBindingInput
  151. instanceVariableNames: 'input callback status wrapper binder ghostText isFinal message messageTag inputCompletion defaultValue'
  152. package: 'Helios-KeyBindings'!
  153. !HLBindingInput commentStamp!
  154. This class should be refactored the following way:
  155. - It shouldn't be a binding but a widget
  156. - the binder helper should display the widget if the BindingAction requires an input before evaluating it.!
  157. !HLBindingInput methodsFor: 'accessing'!
  158. atKey: aKey
  159. aKey = 13 ifFalse: [ ^ nil ]
  160. !
  161. callback
  162. ^ callback ifNil: [ callback := [ :value | ] ]
  163. !
  164. callback: aBlock
  165. callback := aBlock
  166. !
  167. defaultValue
  168. ^ defaultValue ifNil: [ '' ]
  169. !
  170. defaultValue: aString
  171. defaultValue := aString
  172. !
  173. ghostText
  174. ^ ghostText
  175. !
  176. ghostText: aText
  177. ghostText := aText
  178. !
  179. input
  180. ^ input
  181. !
  182. inputCompletion
  183. ^ inputCompletion ifNil: [ #() ]
  184. !
  185. inputCompletion: aCollection
  186. inputCompletion := aCollection
  187. !
  188. message
  189. ^ message ifNil: [ message := '' ]
  190. !
  191. message: aString
  192. message := aString
  193. !
  194. status
  195. ^ status ifNil: [ status := 'info' ]
  196. !
  197. status: aStatus
  198. status := aStatus
  199. ! !
  200. !HLBindingInput methodsFor: 'actions'!
  201. applyOn: aKeyBinder
  202. self isFinal: true.
  203. self evaluate: self input asJQuery val
  204. !
  205. clearStatus
  206. self status: 'info'.
  207. self message: ''.
  208. self refresh
  209. !
  210. errorStatus
  211. self status: 'error'.
  212. self refresh
  213. !
  214. evaluate: aString
  215. [ self callback value: aString ]
  216. on: Error
  217. do: [:ex |
  218. self input asJQuery
  219. one: 'keydown'
  220. do: [ self clearStatus ].
  221. self message: ex messageText.
  222. self errorStatus.
  223. self isFinal: false ].
  224. !
  225. release
  226. status := nil.
  227. wrapper := nil.
  228. binder := nil
  229. ! !
  230. !HLBindingInput methodsFor: 'rendering'!
  231. refresh
  232. wrapper ifNil: [ ^ self ].
  233. wrapper class: self status.
  234. messageTag contents: self message
  235. !
  236. renderOn: aBinder html: html
  237. binder := aBinder.
  238. wrapper ifNil: [ wrapper := html span ].
  239. wrapper
  240. class: self status;
  241. with: [
  242. input := html input
  243. placeholder: self ghostText;
  244. value: self defaultValue;
  245. yourself.
  246. input asJQuery
  247. typeahead: #{ 'source' -> self inputCompletion }.
  248. messageTag := (html span
  249. class: 'help-inline';
  250. with: self message;
  251. yourself) ].
  252. "Evaluate with a timeout to ensure focus.
  253. Commands can be executed from a menu, clicking on the menu to
  254. evaluate the command would give it the focus otherwise"
  255. [ input asJQuery focus ] valueWithTimeout: 10
  256. ! !
  257. !HLBindingInput methodsFor: 'testing'!
  258. isActive
  259. ^ true
  260. !
  261. isFinal
  262. ^ isFinal ifNil: [ isFinal := super isFinal ]
  263. !
  264. isFinal: aBoolean
  265. isFinal := aBoolean
  266. ! !
  267. Object subclass: #HLKeyBinder
  268. instanceVariableNames: 'modifierKey helper bindings selectedBinding'
  269. package: 'Helios-KeyBindings'!
  270. !HLKeyBinder commentStamp!
  271. My `current` instance holds keybindings for Helios actions and evaluate them.
  272. Bindings can be nested by groups. The `bindings` instance variable holds the root of the key bindings tree.
  273. Bindings are instances of a concrete subclass of `HLBinding`.
  274. I am always either in 'active' or 'inactive' state. In active state I capture key down events and my `helper` widget is displayed at the bottom of the window. My `selectedBinding`, if any, is displayed by the helper.
  275. Bindings are evaluated through `applyBinding:`. If a binding is final (not a group of other bindings), evaluating it will result in deactivating the binder, and hiding the `helper` widget.!
  276. !HLKeyBinder methodsFor: 'accessing'!
  277. activationKey
  278. "SPACE"
  279. ^ 32
  280. !
  281. activationKeyLabel
  282. ^ 'ctrl + space'
  283. !
  284. bindings
  285. ^ bindings ifNil: [ bindings := self defaultBindings ]
  286. !
  287. escapeKey
  288. "ESC"
  289. ^ 27
  290. !
  291. helper
  292. ^ helper
  293. !
  294. selectedBinding
  295. ^ selectedBinding ifNil: [ self bindings ]
  296. ! !
  297. !HLKeyBinder methodsFor: 'actions'!
  298. activate
  299. self helper show
  300. !
  301. applyBinding: aBinding
  302. aBinding isActive ifFalse: [ ^ self ].
  303. self selectBinding: aBinding.
  304. aBinding applyOn: self
  305. !
  306. deactivate
  307. selectedBinding ifNotNil: [ selectedBinding release ].
  308. selectedBinding := nil.
  309. self helper hide
  310. !
  311. flushBindings
  312. bindings := nil
  313. !
  314. selectBinding: aBinding
  315. aBinding = selectedBinding ifTrue: [ ^ self ].
  316. selectedBinding := aBinding.
  317. self helper refresh
  318. ! !
  319. !HLKeyBinder methodsFor: 'defaults'!
  320. defaultBindings
  321. | group |
  322. group := HLBindingGroup new
  323. add: HLCloseTabCommand new asBinding;
  324. add: HLSwitchTabCommand new asBinding;
  325. yourself.
  326. HLOpenCommand registerConcreteClassesOn: group.
  327. ^ group
  328. ! !
  329. !HLKeyBinder methodsFor: 'events'!
  330. handleActiveKeyDown: event
  331. "ESC or ctrl+g deactivate the keyBinder"
  332. (event which = self escapeKey or: [
  333. event which = 71 and: [ event ctrlKey ] ])
  334. ifTrue: [
  335. self deactivate.
  336. event preventDefault.
  337. ^ false ].
  338. "Handle the keybinding"
  339. ^ self handleBindingFor: event
  340. !
  341. handleBindingFor: anEvent
  342. | binding |
  343. binding := self selectedBinding atKey: anEvent which.
  344. binding ifNotNil: [
  345. self applyBinding: binding.
  346. anEvent preventDefault.
  347. ^ false ]
  348. !
  349. handleInactiveKeyDown: event
  350. event which = self activationKey ifTrue: [
  351. event ctrlKey ifTrue: [
  352. self activate.
  353. event preventDefault.
  354. ^ false ] ]
  355. !
  356. handleKeyDown: event
  357. ^ self isActive
  358. ifTrue: [ self handleActiveKeyDown: event ]
  359. ifFalse: [ self handleInactiveKeyDown: event ]
  360. !
  361. setupEvents
  362. 'body' asJQuery keydown: [ :event | self handleKeyDown: event ]
  363. ! !
  364. !HLKeyBinder methodsFor: 'initialization'!
  365. initialize
  366. super initialize.
  367. helper := HLKeyBinderHelper on: self.
  368. helper
  369. renderStart;
  370. renderCog
  371. ! !
  372. !HLKeyBinder methodsFor: 'testing'!
  373. isActive
  374. ^ ('.', self helper cssClass) asJQuery is: ':visible'
  375. !
  376. systemIsMac
  377. ^ navigator platform match: 'Mac'
  378. ! !
  379. HLKeyBinder class instanceVariableNames: 'current'!
  380. !HLKeyBinder class methodsFor: 'instance creation'!
  381. current
  382. ^ current ifNil: [ current := super new ]
  383. !
  384. new
  385. self shouldNotImplement
  386. ! !
  387. HLWidget subclass: #HLKeyBinderHelper
  388. instanceVariableNames: 'keyBinder'
  389. package: 'Helios-KeyBindings'!
  390. !HLKeyBinderHelper commentStamp!
  391. I am the widget responsible for displaying active keybindings in a bar at the bottom of the window. Each keybinding is an instance of `HLBinding`.
  392. Rendering is done through a double dispatch, see `#renderSelectedBindingOn:`.!
  393. !HLKeyBinderHelper methodsFor: 'accessing'!
  394. cssClass
  395. ^ 'key_helper'
  396. !
  397. keyBinder
  398. ^ keyBinder
  399. !
  400. keyBinder: aKeyBinder
  401. keyBinder := aKeyBinder
  402. !
  403. selectedBinding
  404. ^ self keyBinder selectedBinding
  405. ! !
  406. !HLKeyBinderHelper methodsFor: 'actions'!
  407. hide
  408. ('.', self cssClass) asJQuery remove.
  409. self showCog
  410. !
  411. hideCog
  412. '#cog-helper' asJQuery hide
  413. !
  414. show
  415. self hideCog.
  416. self appendToJQuery: 'body' asJQuery
  417. !
  418. showCog
  419. '#cog-helper' asJQuery show
  420. ! !
  421. !HLKeyBinderHelper methodsFor: 'rendering'!
  422. renderBindingActionFor: aBinding on: html
  423. html span class: 'command'; with: [
  424. html span
  425. class: 'label';
  426. with: aBinding shortcut asLowercase.
  427. html a
  428. class: 'action';
  429. with: aBinding displayLabel;
  430. onClick: [ self keyBinder applyBinding: aBinding ] ]
  431. !
  432. renderBindingGroup: aBindingGroup on: html
  433. (aBindingGroup activeBindings
  434. sorted: [ :a :b | a key < b key ])
  435. do: [ :each | self renderBindingActionFor: each on: html ]
  436. !
  437. renderCloseOn: html
  438. html a
  439. class: 'close';
  440. with: [ (html tag: 'i') class: 'icon-remove' ];
  441. onClick: [ self keyBinder deactivate ]
  442. !
  443. renderCog
  444. [ :html |
  445. html
  446. div id: 'cog-helper';
  447. with: [
  448. html a
  449. with: [ (html tag: 'i') class: 'icon-cog' ];
  450. onClick: [ self keyBinder activate ] ] ]
  451. appendToJQuery: 'body' asJQuery
  452. !
  453. renderContentOn: html
  454. html div class: self cssClass; with: [
  455. self
  456. renderLabelOn:html;
  457. renderSelectedBindingOn: html;
  458. renderCloseOn: html ]
  459. !
  460. renderLabelOn: html
  461. html span
  462. class: 'selected';
  463. with: (self selectedBinding label ifNil: [ 'Action' ])
  464. !
  465. renderSelectedBindingOn: html
  466. self selectedBinding renderOn: self html: html
  467. !
  468. renderStart
  469. '#helper' asJQuery remove.
  470. [ :html |
  471. html div
  472. id: 'helper';
  473. with: 'Press ', self keyBinder activationKeyLabel, ' to start' ] appendToJQuery: 'body' asJQuery.
  474. [ '#helper' asJQuery fadeOut: 1000 ]
  475. valueWithTimeout: 2000
  476. ! !
  477. !HLKeyBinderHelper class methodsFor: 'instance creation'!
  478. on: aKeyBinder
  479. ^ self new
  480. keyBinder: aKeyBinder;
  481. yourself
  482. ! !
  483. Object subclass: #HLRepeatedKeyDownHandler
  484. instanceVariableNames: 'repeatInterval delay interval keyBindings widget keyDown'
  485. package: 'Helios-KeyBindings'!
  486. !HLRepeatedKeyDownHandler commentStamp!
  487. I am responsible for handling repeated key down actions for widgets.
  488. ##Usage
  489. (self on: aWidget)
  490. whileKeyDown: 38 do: aBlock;
  491. whileKeyDown: 40 do: anotherBlock;
  492. bindKeys
  493. I perform an action block on a key press, wait for 300 ms and then preform the same action block every `repeatInterval` milliseconds until the key is released.!
  494. !HLRepeatedKeyDownHandler methodsFor: 'accessing'!
  495. keyBindings
  496. ^ keyBindings ifNil: [ keyBindings := Dictionary new ]
  497. !
  498. repeatInterval
  499. ^ repeatInterval ifNil: [ self defaultRepeatInterval ]
  500. !
  501. repeatInterval: anInteger
  502. repeatInterval := anInteger
  503. !
  504. widget
  505. ^ widget
  506. !
  507. widget: aWidget
  508. widget := aWidget
  509. ! !
  510. !HLRepeatedKeyDownHandler methodsFor: 'actions'!
  511. startRepeatingAction: aBlock
  512. ^ [ (self widget hasFocus)
  513. ifTrue: [ aBlock value ]
  514. ifFalse: [ self handleKeyUp ] ] valueWithInterval: self repeatInterval
  515. !
  516. whileKeyDown: aKey do: aBlock
  517. self keyBindings at: aKey put: aBlock
  518. ! !
  519. !HLRepeatedKeyDownHandler methodsFor: 'binding'!
  520. bindKeys
  521. self widget
  522. bindKeyDown: [ :e | self handleKeyDown: e ]
  523. keyUp: [ :e | self handleKeyUp ]
  524. !
  525. rebindKeys
  526. self
  527. unbindKeys;
  528. bindKeys
  529. !
  530. unbindKeys
  531. self widget unbindKeyDownKeyUp
  532. ! !
  533. !HLRepeatedKeyDownHandler methodsFor: 'defaults'!
  534. defaultRepeatInterval
  535. ^ 70
  536. ! !
  537. !HLRepeatedKeyDownHandler methodsFor: 'events handling'!
  538. handleEvent: anEvent forKey: anInteger action: aBlock
  539. (anEvent which = anInteger and: [ self isKeyDown not ])
  540. ifTrue: [ self whileKeyDownDo: aBlock ]
  541. !
  542. handleKeyDown: anEvent
  543. self keyBindings keysAndValuesDo: [ :key :action |
  544. self handleEvent: anEvent forKey: key action: action ]
  545. !
  546. handleKeyUp
  547. self isKeyDown ifTrue: [
  548. keyDown := false.
  549. interval ifNotNil: [ interval clearInterval ].
  550. delay ifNotNil: [ delay clearTimeout ] ]
  551. !
  552. whileKeyDownDo: aBlock
  553. keyDown := true.
  554. aBlock value.
  555. delay := [ interval := self startRepeatingAction: aBlock ]
  556. valueWithTimeout: 300
  557. ! !
  558. !HLRepeatedKeyDownHandler methodsFor: 'testing'!
  559. isKeyDown
  560. ^ keyDown ifNil: [ false ]
  561. ! !
  562. !HLRepeatedKeyDownHandler class methodsFor: 'instance creation'!
  563. on: aWidget
  564. ^ self new
  565. widget: aWidget;
  566. yourself
  567. ! !