1
0

Helios-KeyBindings.st 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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 active 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. active := true.
  165. self helper show
  166. !
  167. applyBinding: aBinding
  168. aBinding applyOn: self
  169. !
  170. applyBindingAction: aBinding
  171. aBinding callback value.
  172. self deactivate
  173. !
  174. applyBindingGroup: aBinding
  175. selectedBinding := aBinding.
  176. self helper refresh
  177. !
  178. deactivate
  179. active := false.
  180. selectedBinding := nil.
  181. self helper hide
  182. !
  183. flushBindings
  184. bindings := nil
  185. ! !
  186. !HLKeyBinder methodsFor: 'defaults'!
  187. defaultBindings
  188. | group |
  189. group := HLBindingGroup new
  190. addGroupKey: 79 labelled: 'Open';
  191. addGroupKey: 86 labelled: 'View';
  192. add: HLCloseTabCommand new asBinding;
  193. yourself.
  194. HLOpenCommand registerConcreteClassesOn: (group at: 'Open').
  195. ^ group
  196. ! !
  197. !HLKeyBinder methodsFor: 'events'!
  198. handleActiveKeyDown: event
  199. "ESC or ctrl+g deactivate the keyBinder"
  200. (event which = self escapeKey or: [
  201. event which = 71 and: [ event ctrlKey ] ])
  202. ifTrue: [
  203. self deactivate.
  204. event preventDefault.
  205. ^ false ].
  206. "Handle the keybinding"
  207. ^ self handleBindingFor: event
  208. !
  209. handleBindingFor: anEvent
  210. | binding |
  211. binding := self selectedBinding atKey: anEvent which.
  212. binding ifNotNil: [
  213. self applyBinding: binding.
  214. anEvent preventDefault.
  215. ^ false ]
  216. !
  217. handleInactiveKeyDown: event
  218. event which = self activationKey ifTrue: [
  219. event ctrlKey ifTrue: [
  220. self activate.
  221. event preventDefault.
  222. ^ false ] ]
  223. !
  224. handleKeyDown: event
  225. ^ self isActive
  226. ifTrue: [ self handleActiveKeyDown: event ]
  227. ifFalse: [ self handleInactiveKeyDown: event ]
  228. !
  229. setupEvents
  230. (window jQuery: 'body') keydown: [ :event | self handleKeyDown: event ]
  231. ! !
  232. !HLKeyBinder methodsFor: 'initialization'!
  233. initialize
  234. super initialize.
  235. helper := HLKeyBinderHelper on: self.
  236. helper renderStart.
  237. active := false
  238. ! !
  239. !HLKeyBinder methodsFor: 'testing'!
  240. isActive
  241. ^ active ifNil: [ false ]
  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. !
  266. show
  267. self appendToJQuery: 'body' asJQuery
  268. ! !
  269. !HLKeyBinderHelper methodsFor: 'keyBindings'!
  270. registerBindings
  271. "Do nothing"
  272. ! !
  273. !HLKeyBinderHelper methodsFor: 'rendering'!
  274. renderBindingGroup: aBindingGroup on: html
  275. (aBindingGroup activeBindings
  276. sorted: [ :a :b | a key < b key ])
  277. do: [ :each |
  278. html span class: 'command'; with: [
  279. html span class: 'label'; with: each shortcut asLowercase.
  280. html a
  281. class: 'action';
  282. with: each displayLabel;
  283. onClick: [ self keyBinder applyBinding: each ] ] ]
  284. !
  285. renderBindingOn: html
  286. self selectedBinding renderOn: self html: html
  287. !
  288. renderContentOn: html
  289. html div class: self cssClass; with: [
  290. self
  291. renderSelectionOn:html;
  292. renderBindingOn: html ]
  293. !
  294. renderSelectionOn: html
  295. html span
  296. class: 'selected';
  297. with: (self selectedBinding label ifNil: [ 'Action' ])
  298. !
  299. renderStart
  300. [ :html |
  301. html div
  302. id: 'keybinding-start-helper';
  303. with: 'Press ', self keyBinder activationKeyLabel, ' to start' ] appendToJQuery: 'body' asJQuery.
  304. [ (window jQuery: '#keybinding-start-helper') fadeOut: 1000 ]
  305. valueWithTimeout: 2000
  306. ! !
  307. !HLKeyBinderHelper class methodsFor: 'instance creation'!
  308. on: aKeyBinder
  309. ^ self new
  310. keyBinder: aKeyBinder;
  311. yourself
  312. ! !