1
0

Helios-KeyBindings.st 14 KB

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