Helios-KeyBindings.st 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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. atKey: anInteger
  105. ^ self bindings
  106. detect: [ :each | each key = anInteger ]
  107. ifNone: [ nil ]
  108. !
  109. bindings
  110. ^ bindings ifNil: [ bindings := OrderedCollection new ]
  111. !
  112. displayLabel
  113. ^ super displayLabel, '...'
  114. ! !
  115. !HLBindingGroup methodsFor: 'actions'!
  116. applyOn: aKeyBinder
  117. self isActive ifFalse: [ ^ self ].
  118. aKeyBinder applyBindingGroup: self
  119. ! !
  120. !HLBindingGroup methodsFor: 'rendering'!
  121. renderOn: aBindingHelper html: html
  122. self isActive ifTrue: [
  123. aBindingHelper renderBindingGroup: self on: html ]
  124. ! !
  125. !HLBindingGroup methodsFor: 'testing'!
  126. isActive
  127. ^ self activeBindings notEmpty
  128. !
  129. isBindingGroup
  130. ^ true
  131. ! !
  132. Object subclass: #HLKeyBinder
  133. instanceVariableNames: 'modifierKey active helper bindings selectedBinding'
  134. package: 'Helios-KeyBindings'!
  135. !HLKeyBinder methodsFor: 'accessing'!
  136. activationKey
  137. "SPACE"
  138. ^ 32
  139. !
  140. activationKeyLabel
  141. ^ 'ctrl + space'
  142. !
  143. bindings
  144. ^ bindings ifNil: [ bindings := self defaulBindings ]
  145. !
  146. defaulBindings
  147. | group |
  148. group := HLBindingGroup new
  149. addGroupKey: 79 labelled: 'Open';
  150. addGroupKey: 86 labelled: 'View';
  151. add: HLCloseTabCommand new asBinding;
  152. yourself.
  153. HLOpenCommand allSubclasses do: [ :each |
  154. (group at: each bindingGroup)
  155. add: each new asBinding ].
  156. ^ group
  157. !
  158. escapeKey
  159. "ESC"
  160. ^ 27
  161. !
  162. helper
  163. ^ helper
  164. !
  165. selectedBinding
  166. ^ selectedBinding ifNil: [ self bindings ]
  167. ! !
  168. !HLKeyBinder methodsFor: 'actions'!
  169. activate
  170. active := true.
  171. self helper show
  172. !
  173. applyBinding: aBinding
  174. aBinding applyOn: self
  175. !
  176. applyBindingAction: aBinding
  177. aBinding callback value.
  178. self deactivate
  179. !
  180. applyBindingGroup: aBinding
  181. selectedBinding := aBinding.
  182. self helper refresh
  183. !
  184. deactivate
  185. active := false.
  186. selectedBinding := nil.
  187. self helper hide
  188. !
  189. flushBindings
  190. bindings := nil
  191. ! !
  192. !HLKeyBinder methodsFor: 'events'!
  193. handleActiveKeyDown: event
  194. "ESC or ctrl+g deactivate the keyBinder"
  195. (event which = self escapeKey or: [
  196. event which = 71 and: [ event ctrlKey ] ])
  197. ifTrue: [
  198. self deactivate.
  199. event preventDefault.
  200. ^ false ].
  201. "Handle the keybinding"
  202. ^ self handleBindingFor: event
  203. !
  204. handleBindingFor: anEvent
  205. | binding |
  206. binding := self selectedBinding atKey: anEvent which.
  207. binding ifNotNil: [
  208. self applyBinding: binding.
  209. anEvent preventDefault.
  210. ^ false ]
  211. !
  212. handleInactiveKeyDown: event
  213. event which = self activationKey ifTrue: [
  214. event ctrlKey ifTrue: [
  215. self activate.
  216. event preventDefault.
  217. ^ false ] ]
  218. !
  219. handleKeyDown: event
  220. ^ self isActive
  221. ifTrue: [ self handleActiveKeyDown: event ]
  222. ifFalse: [ self handleInactiveKeyDown: event ]
  223. !
  224. setupEvents
  225. (window jQuery: 'body') keydown: [ :event | self handleKeyDown: event ]
  226. ! !
  227. !HLKeyBinder methodsFor: 'initialization'!
  228. initialize
  229. super initialize.
  230. helper := HLKeyBinderHelper on: self.
  231. helper renderStart.
  232. active := false
  233. ! !
  234. !HLKeyBinder methodsFor: 'testing'!
  235. isActive
  236. ^ active ifNil: [ false ]
  237. !
  238. systemIsMac
  239. ^ navigator platform match: 'Mac'
  240. ! !
  241. HLWidget subclass: #HLKeyBinderHelper
  242. instanceVariableNames: 'keyBinder'
  243. package: 'Helios-KeyBindings'!
  244. !HLKeyBinderHelper methodsFor: 'accessing'!
  245. cssClass
  246. ^ 'key_helper'
  247. !
  248. keyBinder
  249. ^ keyBinder
  250. !
  251. keyBinder: aKeyBinder
  252. keyBinder := aKeyBinder
  253. !
  254. selectedBinding
  255. ^ self keyBinder selectedBinding
  256. ! !
  257. !HLKeyBinderHelper methodsFor: 'actions'!
  258. hide
  259. ('.', self cssClass) asJQuery remove
  260. !
  261. show
  262. self appendToJQuery: 'body' asJQuery
  263. ! !
  264. !HLKeyBinderHelper methodsFor: 'keyBindings'!
  265. registerBindings
  266. "Do nothing"
  267. ! !
  268. !HLKeyBinderHelper methodsFor: 'rendering'!
  269. renderBindingGroup: aBindingGroup on: html
  270. (aBindingGroup activeBindings
  271. sorted: [ :a :b | a key < b key ])
  272. do: [ :each |
  273. html span class: 'command'; with: [
  274. html span class: 'label'; with: each shortcut asLowercase.
  275. html a
  276. class: 'action';
  277. with: each displayLabel;
  278. onClick: [ self keyBinder applyBinding: each ] ] ]
  279. !
  280. renderBindingOn: html
  281. self selectedBinding renderOn: self html: html
  282. !
  283. renderContentOn: html
  284. html div class: self cssClass; with: [
  285. self
  286. renderSelectionOn:html;
  287. renderBindingOn: html ]
  288. !
  289. renderSelectionOn: html
  290. html span
  291. class: 'selected';
  292. with: (self selectedBinding label ifNil: [ 'Action' ])
  293. !
  294. renderStart
  295. [ :html |
  296. html div
  297. id: 'keybinding-start-helper';
  298. with: 'Press ', self keyBinder activationKeyLabel, ' to start' ] appendToJQuery: 'body' asJQuery.
  299. [ (window jQuery: '#keybinding-start-helper') fadeOut: 1000 ]
  300. valueWithTimeout: 2000
  301. ! !
  302. !HLKeyBinderHelper class methodsFor: 'instance creation'!
  303. on: aKeyBinder
  304. ^ self new
  305. keyBinder: aKeyBinder;
  306. yourself
  307. ! !