Helios-KeyBindings.st 9.2 KB

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