Helios-KeyBindings.st 13 KB

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