Helios-KeyBindings.st 7.5 KB

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