1
0

Helios-KeyBindings.st 9.3 KB

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