Helios-KeyBindings.st 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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. applyOn: aKeyBinder then: aBlock
  31. !
  32. release
  33. ! !
  34. !HLBinding methodsFor: 'rendering'!
  35. renderActionFor: aBinder html: html
  36. html span class: 'command'; with: [
  37. html span
  38. class: 'label';
  39. with: self shortcut asLowercase.
  40. html a
  41. class: 'action';
  42. with: self displayLabel;
  43. onClick: [ aBinder applyBinding: self ] ]
  44. !
  45. renderOn: aBindingHelper html: html
  46. ! !
  47. !HLBinding methodsFor: 'testing'!
  48. isActive
  49. ^ self subclassResponsibility
  50. !
  51. isFinal
  52. " Answer true if the receiver is the final binding of a sequence "
  53. ^ false
  54. ! !
  55. !HLBinding class methodsFor: 'instance creation'!
  56. on: anInteger labelled: aString
  57. ^ self new
  58. key: anInteger;
  59. label: aString;
  60. yourself
  61. ! !
  62. HLBinding subclass: #HLBindingAction
  63. instanceVariableNames: 'command'
  64. package: 'Helios-KeyBindings'!
  65. !HLBindingAction methodsFor: 'accessing'!
  66. command
  67. ^ command
  68. !
  69. command: aCommand
  70. command := aCommand
  71. !
  72. inputBinding
  73. ^ HLBindingInput new
  74. label: self command inputLabel;
  75. callback: [ :val :errorBlock |
  76. self command
  77. input: val;
  78. errorBlock: errorBlock;
  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 inputText wrapper binder'
  150. package: 'Helios-KeyBindings'!
  151. !HLBindingInput methodsFor: 'accessing'!
  152. callback
  153. ^ callback ifNil: [ callback := [ :value | ] ]
  154. !
  155. callback: aBlock
  156. callback := aBlock
  157. !
  158. input
  159. ^ input
  160. !
  161. inputText
  162. ^ inputText ifNil: [ inputText := '' ].
  163. !
  164. inputText: aText
  165. inputText := aText
  166. !
  167. status
  168. ^ status ifNil: [ status := 'info' ]
  169. !
  170. status: aStatus
  171. status := aStatus
  172. ! !
  173. !HLBindingInput methodsFor: 'actions'!
  174. applyOn: aKeyBinder
  175. self
  176. evaluate: self input
  177. onError: [
  178. self errorStatus.
  179. self refresh.
  180. ^ false ]
  181. !
  182. errorStatus
  183. self status: 'error'
  184. !
  185. evaluate: aString onError: aBlock
  186. self callback value: aString value: aBlock
  187. !
  188. release
  189. status := nil.
  190. wrapper := nil.
  191. binder := nil.
  192. inputText := nil
  193. ! !
  194. !HLBindingInput methodsFor: 'rendering'!
  195. refresh
  196. wrapper ifNil: [ ^ self ].
  197. wrapper asJQuery empty.
  198. [ :html | self renderActionFor: binder html: html ] appendToJQuery: wrapper asJQuery
  199. !
  200. renderActionFor: aBinder html: html
  201. binder := aBinder.
  202. wrapper ifNil: [ wrapper := html span ].
  203. wrapper
  204. class: 'control-group ', self status;
  205. with: [
  206. input := html input
  207. class: 'input';
  208. placeholder: self displayLabel;
  209. with: self inputText ].
  210. input onKeyPress: [ :event |
  211. event keyCode = 13
  212. ifTrue: [ self applyOn: aBinder ] ].
  213. input asJQuery focus
  214. ! !
  215. !HLBindingInput methodsFor: 'testing'!
  216. isActive
  217. ^ true
  218. ! !
  219. Object subclass: #HLKeyBinder
  220. instanceVariableNames: 'modifierKey helper bindings selectedBinding'
  221. package: 'Helios-KeyBindings'!
  222. !HLKeyBinder methodsFor: 'accessing'!
  223. activationKey
  224. "SPACE"
  225. ^ 32
  226. !
  227. activationKeyLabel
  228. ^ 'ctrl + space'
  229. !
  230. bindings
  231. ^ bindings ifNil: [ bindings := self defaultBindings ]
  232. !
  233. escapeKey
  234. "ESC"
  235. ^ 27
  236. !
  237. helper
  238. ^ helper
  239. !
  240. selectedBinding
  241. ^ selectedBinding ifNil: [ self bindings ]
  242. ! !
  243. !HLKeyBinder methodsFor: 'actions'!
  244. activate
  245. self helper show
  246. !
  247. applyBinding: aBinding
  248. aBinding isActive ifFalse: [ ^ self ].
  249. self selectBinding: aBinding.
  250. aBinding applyOn: self.
  251. aBinding isFinal ifTrue: [ self deactivate ]
  252. !
  253. deactivate
  254. selectedBinding ifNotNil: [ selectedBinding release ].
  255. selectedBinding := nil.
  256. self helper hide
  257. !
  258. flushBindings
  259. bindings := nil
  260. !
  261. selectBinding: aBinding
  262. selectedBinding := aBinding.
  263. self helper refresh
  264. ! !
  265. !HLKeyBinder methodsFor: 'defaults'!
  266. defaultBindings
  267. | group |
  268. group := HLBindingGroup new
  269. addGroupKey: 86 labelled: 'View';
  270. add: HLCloseTabCommand new asBinding;
  271. yourself.
  272. HLOpenCommand registerConcreteClassesOn: group.
  273. ^ group
  274. ! !
  275. !HLKeyBinder methodsFor: 'events'!
  276. handleActiveKeyDown: event
  277. "ESC or ctrl+g deactivate the keyBinder"
  278. (event which = self escapeKey or: [
  279. event which = 71 and: [ event ctrlKey ] ])
  280. ifTrue: [
  281. self deactivate.
  282. event preventDefault.
  283. ^ false ].
  284. "Handle the keybinding"
  285. ^ self handleBindingFor: event
  286. !
  287. handleBindingFor: anEvent
  288. | binding |
  289. binding := self selectedBinding atKey: anEvent which.
  290. binding ifNotNil: [
  291. self applyBinding: binding.
  292. anEvent preventDefault.
  293. ^ false ]
  294. !
  295. handleInactiveKeyDown: event
  296. event which = self activationKey ifTrue: [
  297. event ctrlKey ifTrue: [
  298. self activate.
  299. event preventDefault.
  300. ^ false ] ]
  301. !
  302. handleKeyDown: event
  303. ^ self isActive
  304. ifTrue: [ self handleActiveKeyDown: event ]
  305. ifFalse: [ self handleInactiveKeyDown: event ]
  306. !
  307. setupEvents
  308. (window jQuery: 'body') keydown: [ :event | self handleKeyDown: event ]
  309. ! !
  310. !HLKeyBinder methodsFor: 'initialization'!
  311. initialize
  312. super initialize.
  313. helper := HLKeyBinderHelper on: self.
  314. helper
  315. renderStart;
  316. renderCog.
  317. active := false
  318. ! !
  319. !HLKeyBinder methodsFor: 'testing'!
  320. isActive
  321. ^ ('.', self helper cssClass) asJQuery is: ':visible'
  322. !
  323. systemIsMac
  324. ^ navigator platform match: 'Mac'
  325. ! !
  326. HLWidget subclass: #HLKeyBinderHelper
  327. instanceVariableNames: 'keyBinder'
  328. package: 'Helios-KeyBindings'!
  329. !HLKeyBinderHelper methodsFor: 'accessing'!
  330. cssClass
  331. ^ 'key_helper'
  332. !
  333. keyBinder
  334. ^ keyBinder
  335. !
  336. keyBinder: aKeyBinder
  337. keyBinder := aKeyBinder
  338. !
  339. selectedBinding
  340. ^ self keyBinder selectedBinding
  341. ! !
  342. !HLKeyBinderHelper methodsFor: 'actions'!
  343. hide
  344. ('.', self cssClass) asJQuery remove.
  345. self showCog
  346. !
  347. hideCog
  348. '#cog-helper' asJQuery hide
  349. !
  350. show
  351. self hideCog.
  352. self appendToJQuery: 'body' asJQuery
  353. !
  354. showCog
  355. '#cog-helper' asJQuery show
  356. ! !
  357. !HLKeyBinderHelper methodsFor: 'keyBindings'!
  358. registerBindings
  359. "Do nothing"
  360. ! !
  361. !HLKeyBinderHelper methodsFor: 'rendering'!
  362. renderBindingGroup: aBindingGroup on: html
  363. (aBindingGroup activeBindings
  364. sorted: [ :a :b | a key < b key ])
  365. do: [ :each | each renderActionFor: self keyBinder html: html ]
  366. !
  367. renderBindingOn: html
  368. self selectedBinding renderOn: self html: html
  369. !
  370. renderCloseOn: html
  371. html a
  372. class: 'close';
  373. with: [ (html tag: 'i') class: 'icon-remove' ];
  374. onClick: [ self keyBinder deactivate ]
  375. !
  376. renderCog
  377. [ :html |
  378. html
  379. div id: 'cog-helper';
  380. with: [
  381. html a
  382. with: [ (html tag: 'i') class: 'icon-cog' ];
  383. onClick: [ self keyBinder activate ] ] ]
  384. appendToJQuery: 'body' asJQuery
  385. !
  386. renderContentOn: html
  387. html div class: self cssClass; with: [
  388. self
  389. renderSelectionOn:html;
  390. renderBindingOn: html;
  391. renderCloseOn: html ]
  392. !
  393. renderSelectionOn: html
  394. html span
  395. class: 'selected';
  396. with: (self selectedBinding label ifNil: [ 'Action' ])
  397. !
  398. renderStart
  399. [ :html |
  400. html div
  401. id: 'keybinding-start-helper';
  402. with: 'Press ', self keyBinder activationKeyLabel, ' to start' ] appendToJQuery: 'body' asJQuery.
  403. [ (window jQuery: '#keybinding-start-helper') fadeOut: 1000 ]
  404. valueWithTimeout: 2000
  405. ! !
  406. !HLKeyBinderHelper class methodsFor: 'instance creation'!
  407. on: aKeyBinder
  408. ^ self new
  409. keyBinder: aKeyBinder;
  410. yourself
  411. ! !