Helios-KeyBindings.st 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. Smalltalk current createPackage: 'Helios-KeyBindings'!
  2. Object subclass: #HLBinding
  3. instanceVariableNames: 'key label each'
  4. package: 'Helios-KeyBindings'!
  5. !HLBinding methodsFor: 'accessing'!
  6. displayLabel
  7. ^ self label
  8. !
  9. key
  10. ^ key
  11. !
  12. key: anInteger
  13. key := anInteger
  14. !
  15. label
  16. ^ label
  17. !
  18. label: aString
  19. label := aString
  20. !
  21. shortcut
  22. ^ String fromCharCode: self key
  23. ! !
  24. !HLBinding methodsFor: 'actions'!
  25. applyOn: aKeyBinder
  26. self subclassResponsibility
  27. ! !
  28. !HLBinding methodsFor: 'rendering'!
  29. renderBindingOn: html actionOn: aBinder
  30. html span class: 'command'; with: [
  31. html span
  32. class: 'label';
  33. with: self shortcut asLowercase.
  34. html a
  35. class: 'action';
  36. with: self displayLabel;
  37. onClick: [ aBinder applyBinding: self ] ]
  38. !
  39. renderOn: aBindingHelper html: html
  40. ! !
  41. !HLBinding methodsFor: 'testing'!
  42. isActive
  43. ^ self subclassResponsibility
  44. !
  45. isBindingAction
  46. ^ false
  47. !
  48. isBindingGroup
  49. ^ false
  50. ! !
  51. !HLBinding class methodsFor: 'instance creation'!
  52. on: anInteger labelled: aString
  53. ^ self new
  54. key: anInteger;
  55. label: aString;
  56. yourself
  57. ! !
  58. HLBinding subclass: #HLBindingAction
  59. instanceVariableNames: 'callback activeBlock'
  60. package: 'Helios-KeyBindings'!
  61. !HLBindingAction methodsFor: 'accessing'!
  62. activeBlock
  63. ^ activeBlock ifNil: [ activeBlock := [ true ] ]
  64. !
  65. activeBlock: aBlock
  66. activeBlock := aBlock
  67. !
  68. callback
  69. ^ callback
  70. !
  71. callback: aBlock
  72. callback := aBlock
  73. ! !
  74. !HLBindingAction methodsFor: 'actions'!
  75. applyOn: aKeyBinder
  76. self isActive ifFalse: [ ^ self ].
  77. aKeyBinder applyBindingAction: self
  78. ! !
  79. !HLBindingAction methodsFor: 'testing'!
  80. isActive
  81. ^ self activeBlock value
  82. !
  83. isBindingAction
  84. ^ true
  85. ! !
  86. !HLBindingAction class methodsFor: 'instance creation'!
  87. on: anInteger labelled: aString activeBlock: aBlock
  88. | instance |
  89. instance := super on: anInteger labelled: aString.
  90. ^ instance
  91. activeBlock: aBlock;
  92. yourself
  93. ! !
  94. HLBindingAction subclass: #HLBindingInput
  95. instanceVariableNames: 'input'
  96. package: 'Helios-KeyBindings'!
  97. !HLBindingInput methodsFor: 'actions'!
  98. applyOn: aBinder
  99. self isActive ifFalse: [ ^ self ].
  100. aBinder applyBindingInput: self with: self input
  101. !
  102. input
  103. ^ input asJQuery val
  104. ! !
  105. !HLBindingInput methodsFor: 'rendering'!
  106. renderBindingOn: html actionOn: aBinder
  107. html span
  108. class: 'command';
  109. with: [
  110. "html form
  111. class: 'form-search';
  112. with: [
  113. html div
  114. class: 'input-append';
  115. with: [
  116. html input
  117. type: 'text';
  118. class: 'search-query';
  119. placeholder: self displayLabel.
  120. html button
  121. type: 'submit';
  122. class: 'btn';
  123. with: 'Ok' ] ] ]
  124. "
  125. input := html input
  126. class: 'search-query';
  127. placeholder: self displayLabel ].
  128. input asJQuery focus
  129. ! !
  130. HLBinding subclass: #HLBindingGroup
  131. instanceVariableNames: 'bindings'
  132. package: 'Helios-KeyBindings'!
  133. !HLBindingGroup methodsFor: 'accessing'!
  134. activeBindings
  135. ^ self bindings select: [ :each | each isActive ]
  136. !
  137. add: aBinding
  138. ^ self bindings add: aBinding
  139. !
  140. addActionKey: anInteger labelled: aString callback: aBlock
  141. self add: ((HLBindingAction on: anInteger labelled: aString)
  142. callback: aBlock;
  143. yourself)
  144. !
  145. addActionKey: anInteger labelled: aString command: aCommand
  146. self add: ((HLBindingAction on: anInteger labelled: aString)
  147. command: aCommand;
  148. yourself)
  149. !
  150. addGroupKey: anInteger labelled: aString
  151. self add: (HLBindingGroup on: anInteger labelled: aString)
  152. !
  153. at: aString
  154. ^ self bindings
  155. detect: [ :each | each label = aString ]
  156. ifNone: [ nil ]
  157. !
  158. at: aString add: aBinding
  159. | binding |
  160. binding := self at: aString.
  161. binding ifNil: [ ^ self ].
  162. binding add: aBinding
  163. !
  164. atKey: anInteger
  165. ^ self bindings
  166. detect: [ :each | each key = anInteger ]
  167. ifNone: [ nil ]
  168. !
  169. bindings
  170. ^ bindings ifNil: [ bindings := OrderedCollection new ]
  171. !
  172. displayLabel
  173. ^ super displayLabel, '...'
  174. ! !
  175. !HLBindingGroup methodsFor: 'actions'!
  176. applyOn: aKeyBinder
  177. self isActive ifFalse: [ ^ self ].
  178. aKeyBinder applyBindingGroup: self
  179. ! !
  180. !HLBindingGroup methodsFor: 'rendering'!
  181. renderOn: aBindingHelper html: html
  182. self isActive ifTrue: [
  183. aBindingHelper renderBindingGroup: self on: html ]
  184. ! !
  185. !HLBindingGroup methodsFor: 'testing'!
  186. isActive
  187. ^ self activeBindings notEmpty
  188. !
  189. isBindingGroup
  190. ^ true
  191. ! !
  192. Object subclass: #HLKeyBinder
  193. instanceVariableNames: 'modifierKey helper bindings selectedBinding'
  194. package: 'Helios-KeyBindings'!
  195. !HLKeyBinder methodsFor: 'accessing'!
  196. activationKey
  197. "SPACE"
  198. ^ 32
  199. !
  200. activationKeyLabel
  201. ^ 'ctrl + space'
  202. !
  203. bindings
  204. ^ bindings ifNil: [ bindings := self defaultBindings ]
  205. !
  206. escapeKey
  207. "ESC"
  208. ^ 27
  209. !
  210. helper
  211. ^ helper
  212. !
  213. selectedBinding
  214. ^ selectedBinding ifNil: [ self bindings ]
  215. ! !
  216. !HLKeyBinder methodsFor: 'actions'!
  217. activate
  218. self helper show
  219. !
  220. applyBinding: aBinding
  221. aBinding applyOn: self
  222. !
  223. applyBindingAction: aBinding
  224. aBinding callback value.
  225. self deactivate
  226. !
  227. applyBindingGroup: aBinding
  228. selectedBinding := aBinding.
  229. self helper refresh
  230. !
  231. applyBindingInput: aBinding with: value
  232. aBinding callback value: value.
  233. self deactivate
  234. !
  235. deactivate
  236. selectedBinding := nil.
  237. self helper hide
  238. !
  239. flushBindings
  240. bindings := nil
  241. ! !
  242. !HLKeyBinder methodsFor: 'defaults'!
  243. defaultBindings
  244. | group |
  245. group := HLBindingGroup new
  246. addGroupKey: 86 labelled: 'View';
  247. add: HLCloseTabCommand new asBinding;
  248. yourself.
  249. HLOpenCommand registerConcreteClassesOn: group.
  250. ^ group
  251. ! !
  252. !HLKeyBinder methodsFor: 'events'!
  253. handleActiveKeyDown: event
  254. "ESC or ctrl+g deactivate the keyBinder"
  255. (event which = self escapeKey or: [
  256. event which = 71 and: [ event ctrlKey ] ])
  257. ifTrue: [
  258. self deactivate.
  259. event preventDefault.
  260. ^ false ].
  261. "Handle the keybinding"
  262. ^ self handleBindingFor: event
  263. !
  264. handleBindingFor: anEvent
  265. | binding |
  266. binding := self selectedBinding atKey: anEvent which.
  267. binding ifNotNil: [
  268. self applyBinding: binding.
  269. anEvent preventDefault.
  270. ^ false ]
  271. !
  272. handleInactiveKeyDown: event
  273. event which = self activationKey ifTrue: [
  274. event ctrlKey ifTrue: [
  275. self activate.
  276. event preventDefault.
  277. ^ false ] ]
  278. !
  279. handleKeyDown: event
  280. ^ self isActive
  281. ifTrue: [ self handleActiveKeyDown: event ]
  282. ifFalse: [ self handleInactiveKeyDown: event ]
  283. !
  284. setupEvents
  285. (window jQuery: 'body') keydown: [ :event | self handleKeyDown: event ]
  286. ! !
  287. !HLKeyBinder methodsFor: 'initialization'!
  288. initialize
  289. super initialize.
  290. helper := HLKeyBinderHelper on: self.
  291. helper
  292. renderStart;
  293. renderCog.
  294. active := false
  295. ! !
  296. !HLKeyBinder methodsFor: 'testing'!
  297. isActive
  298. ^ ('.', self helper cssClass) asJQuery is: ':visible'
  299. !
  300. systemIsMac
  301. ^ navigator platform match: 'Mac'
  302. ! !
  303. HLWidget subclass: #HLKeyBinderHelper
  304. instanceVariableNames: 'keyBinder'
  305. package: 'Helios-KeyBindings'!
  306. !HLKeyBinderHelper methodsFor: 'accessing'!
  307. cssClass
  308. ^ 'key_helper'
  309. !
  310. keyBinder
  311. ^ keyBinder
  312. !
  313. keyBinder: aKeyBinder
  314. keyBinder := aKeyBinder
  315. !
  316. selectedBinding
  317. ^ self keyBinder selectedBinding
  318. ! !
  319. !HLKeyBinderHelper methodsFor: 'actions'!
  320. hide
  321. ('.', self cssClass) asJQuery remove.
  322. self showCog
  323. !
  324. hideCog
  325. '#cog-helper' asJQuery hide
  326. !
  327. show
  328. self hideCog.
  329. self appendToJQuery: 'body' asJQuery
  330. !
  331. showCog
  332. '#cog-helper' asJQuery show
  333. ! !
  334. !HLKeyBinderHelper methodsFor: 'keyBindings'!
  335. registerBindings
  336. "Do nothing"
  337. ! !
  338. !HLKeyBinderHelper methodsFor: 'rendering'!
  339. renderBindingGroup: aBindingGroup on: html
  340. (aBindingGroup activeBindings
  341. sorted: [ :a :b | a key < b key ])
  342. do: [ :each | each renderBindingOn: html actionOn: self keyBinder ]
  343. !
  344. renderBindingOn: html
  345. self selectedBinding renderOn: self html: html
  346. !
  347. renderCloseOn: html
  348. html a
  349. class: 'close';
  350. with: [ (html tag: 'i') class: 'icon-remove' ];
  351. onClick: [ self keyBinder deactivate ]
  352. !
  353. renderCog
  354. [ :html |
  355. html
  356. div id: 'cog-helper';
  357. with: [
  358. html a
  359. with: [ (html tag: 'i') class: 'icon-cog' ];
  360. onClick: [ self keyBinder activate ] ] ]
  361. appendToJQuery: 'body' asJQuery
  362. !
  363. renderContentOn: html
  364. html div class: self cssClass; with: [
  365. self
  366. renderSelectionOn:html;
  367. renderBindingOn: html;
  368. renderCloseOn: html ]
  369. !
  370. renderSelectionOn: html
  371. html span
  372. class: 'selected';
  373. with: (self selectedBinding label ifNil: [ 'Action' ])
  374. !
  375. renderStart
  376. [ :html |
  377. html div
  378. id: 'keybinding-start-helper';
  379. with: 'Press ', self keyBinder activationKeyLabel, ' to start' ] appendToJQuery: 'body' asJQuery.
  380. [ (window jQuery: '#keybinding-start-helper') fadeOut: 1000 ]
  381. valueWithTimeout: 2000
  382. ! !
  383. !HLKeyBinderHelper class methodsFor: 'instance creation'!
  384. on: aKeyBinder
  385. ^ self new
  386. keyBinder: aKeyBinder;
  387. yourself
  388. ! !