Helios-KeyBindings.st 9.4 KB

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