Helios-KeyBindings.st 9.5 KB

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