1
0

Helios-KeyBindings.st 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  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. inputText := nil
  217. ! !
  218. !HLBindingInput methodsFor: 'rendering'!
  219. refresh
  220. wrapper ifNil: [ ^ self ].
  221. wrapper class: self status.
  222. messageTag contents: self message
  223. !
  224. renderOn: aBinder html: html
  225. binder := aBinder.
  226. wrapper ifNil: [ wrapper := html span ].
  227. wrapper
  228. class: self status;
  229. with: [
  230. input := html input
  231. placeholder: self ghostText;
  232. yourself.
  233. input asJQuery
  234. typeahead: #{ 'source' -> self inputCompletion }.
  235. messageTag := (html span
  236. class: 'help-inline';
  237. with: self message;
  238. yourself) ].
  239. "Evaluate with a timeout to ensure focus.
  240. Commands can be executed from a menu, clicking on the menu to
  241. evaluate the command would give it the focus otherwise"
  242. [ input asJQuery focus ] valueWithTimeout: 10
  243. ! !
  244. !HLBindingInput methodsFor: 'testing'!
  245. isActive
  246. ^ true
  247. !
  248. isFinal
  249. ^ isFinal ifNil: [ isFinal := super isFinal ]
  250. !
  251. isFinal: aBoolean
  252. isFinal := aBoolean
  253. ! !
  254. Object subclass: #HLKeyBinder
  255. instanceVariableNames: 'modifierKey helper bindings selectedBinding'
  256. package: 'Helios-KeyBindings'!
  257. !HLKeyBinder methodsFor: 'accessing'!
  258. activationKey
  259. "SPACE"
  260. ^ 32
  261. !
  262. activationKeyLabel
  263. ^ 'ctrl + space'
  264. !
  265. bindings
  266. ^ bindings ifNil: [ bindings := self defaultBindings ]
  267. !
  268. escapeKey
  269. "ESC"
  270. ^ 27
  271. !
  272. helper
  273. ^ helper
  274. !
  275. selectedBinding
  276. ^ selectedBinding ifNil: [ self bindings ]
  277. ! !
  278. !HLKeyBinder methodsFor: 'actions'!
  279. activate
  280. self helper show
  281. !
  282. applyBinding: aBinding
  283. aBinding isActive ifFalse: [ ^ self ].
  284. self selectBinding: aBinding.
  285. aBinding applyOn: self.
  286. aBinding isFinal ifTrue: [ self deactivate ]
  287. !
  288. deactivate
  289. selectedBinding ifNotNil: [ selectedBinding release ].
  290. selectedBinding := nil.
  291. self helper hide
  292. !
  293. flushBindings
  294. bindings := nil
  295. !
  296. selectBinding: aBinding
  297. aBinding = selectedBinding ifTrue: [ ^ self ].
  298. selectedBinding := aBinding.
  299. self helper refresh
  300. ! !
  301. !HLKeyBinder methodsFor: 'defaults'!
  302. defaultBindings
  303. | group |
  304. group := HLBindingGroup new
  305. addGroupKey: 86 labelled: 'View';
  306. add: HLCloseTabCommand new asBinding;
  307. yourself.
  308. HLOpenCommand registerConcreteClassesOn: group.
  309. ^ group
  310. ! !
  311. !HLKeyBinder methodsFor: 'events'!
  312. handleActiveKeyDown: event
  313. "ESC or ctrl+g deactivate the keyBinder"
  314. (event which = self escapeKey or: [
  315. event which = 71 and: [ event ctrlKey ] ])
  316. ifTrue: [
  317. self deactivate.
  318. event preventDefault.
  319. ^ false ].
  320. "Handle the keybinding"
  321. ^ self handleBindingFor: event
  322. !
  323. handleBindingFor: anEvent
  324. | binding |
  325. binding := self selectedBinding atKey: anEvent which.
  326. binding ifNotNil: [
  327. self applyBinding: binding.
  328. anEvent preventDefault.
  329. ^ false ]
  330. !
  331. handleInactiveKeyDown: event
  332. event which = self activationKey ifTrue: [
  333. event ctrlKey ifTrue: [
  334. self activate.
  335. event preventDefault.
  336. ^ false ] ]
  337. !
  338. handleKeyDown: event
  339. ^ self isActive
  340. ifTrue: [ self handleActiveKeyDown: event ]
  341. ifFalse: [ self handleInactiveKeyDown: event ]
  342. !
  343. setupEvents
  344. (window jQuery: 'body') keydown: [ :event | self handleKeyDown: event ]
  345. ! !
  346. !HLKeyBinder methodsFor: 'initialization'!
  347. initialize
  348. super initialize.
  349. helper := HLKeyBinderHelper on: self.
  350. helper
  351. renderStart;
  352. renderCog
  353. ! !
  354. !HLKeyBinder methodsFor: 'testing'!
  355. isActive
  356. ^ ('.', self helper cssClass) asJQuery is: ':visible'
  357. !
  358. systemIsMac
  359. ^ navigator platform match: 'Mac'
  360. ! !
  361. HLWidget subclass: #HLKeyBinderHelper
  362. instanceVariableNames: 'keyBinder'
  363. package: 'Helios-KeyBindings'!
  364. !HLKeyBinderHelper methodsFor: 'accessing'!
  365. cssClass
  366. ^ 'key_helper'
  367. !
  368. keyBinder
  369. ^ keyBinder
  370. !
  371. keyBinder: aKeyBinder
  372. keyBinder := aKeyBinder
  373. !
  374. selectedBinding
  375. ^ self keyBinder selectedBinding
  376. ! !
  377. !HLKeyBinderHelper methodsFor: 'actions'!
  378. hide
  379. ('.', self cssClass) asJQuery remove.
  380. self showCog
  381. !
  382. hideCog
  383. '#cog-helper' asJQuery hide
  384. !
  385. show
  386. self hideCog.
  387. self appendToJQuery: 'body' asJQuery
  388. !
  389. showCog
  390. '#cog-helper' asJQuery show
  391. ! !
  392. !HLKeyBinderHelper methodsFor: 'keyBindings'!
  393. registerBindings
  394. "Do nothing"
  395. ! !
  396. !HLKeyBinderHelper methodsFor: 'rendering'!
  397. renderBindingGroup: aBindingGroup on: html
  398. (aBindingGroup activeBindings
  399. sorted: [ :a :b | a key < b key ])
  400. do: [ :each | each renderActionFor: self keyBinder html: html ]
  401. !
  402. renderBindingOn: html
  403. self selectedBinding renderOn: self html: html
  404. !
  405. renderCloseOn: html
  406. html a
  407. class: 'close';
  408. with: [ (html tag: 'i') class: 'icon-remove' ];
  409. onClick: [ self keyBinder deactivate ]
  410. !
  411. renderCog
  412. [ :html |
  413. html
  414. div id: 'cog-helper';
  415. with: [
  416. html a
  417. with: [ (html tag: 'i') class: 'icon-cog' ];
  418. onClick: [ self keyBinder activate ] ] ]
  419. appendToJQuery: 'body' asJQuery
  420. !
  421. renderContentOn: html
  422. html div class: self cssClass; with: [
  423. self
  424. renderSelectionOn:html;
  425. renderBindingOn: html;
  426. renderCloseOn: html ]
  427. !
  428. renderSelectionOn: html
  429. html span
  430. class: 'selected';
  431. with: (self selectedBinding label ifNil: [ 'Action' ])
  432. !
  433. renderStart
  434. [ :html |
  435. html div
  436. id: 'helper';
  437. with: 'Press ', self keyBinder activationKeyLabel, ' to start' ] appendToJQuery: 'body' asJQuery.
  438. [ (window jQuery: '#helper') fadeOut: 1000 ]
  439. valueWithTimeout: 2000
  440. ! !
  441. !HLKeyBinderHelper class methodsFor: 'instance creation'!
  442. on: aKeyBinder
  443. ^ self new
  444. keyBinder: aKeyBinder;
  445. yourself
  446. ! !