123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488 |
- Smalltalk current createPackage: 'Helios-KeyBindings'!
- Object subclass: #HLBinding
- instanceVariableNames: 'key label each'
- package: 'Helios-KeyBindings'!
- atKey: aKey
- ^ nil
- displayLabel
- ^ self label
- key
- ^ key
- key: anInteger
- key := anInteger
- label
- ^ label
- label: aString
- label := aString
- shortcut
- ^ String fromCharCode: self key
- applyOn: aKeyBinder
- applyOn: aKeyBinder then: aBlock
- renderActionFor: aBinder html: html
- html span class: 'command'; with: [
- html span
- class: 'label';
- with: self shortcut asLowercase.
- html a
- class: 'action';
- with: self displayLabel;
- onClick: [ aBinder applyBinding: self ] ]
- renderOn: aBindingHelper html: html
- isActive
- ^ self subclassResponsibility
- isFinal
- " Answer true if the receiver is the final binding of a sequence "
-
- ^ false
- on: anInteger labelled: aString
- ^ self new
- key: anInteger;
- label: aString;
- yourself
- HLBinding subclass: #HLBindingAction
- instanceVariableNames: 'command'
- package: 'Helios-KeyBindings'!
- command
- ^ command
- command: aCommand
- command := aCommand
- inputBinding
- ^ HLBindingInput new
- label: self command inputLabel;
- callback: [ :val |
- self command
- input: val;
- execute ];
- yourself
- applyOn: aKeyBinder
- self command isInputRequired
- ifTrue: [ aKeyBinder selectBinding: self inputBinding ]
- ifFalse: [ self command execute ]
- isActive
- ^ self command isActive
- isFinal
- ^ self command isInputRequired not
- HLBinding subclass: #HLBindingGroup
- instanceVariableNames: 'bindings'
- package: 'Helios-KeyBindings'!
- activeBindings
- ^ self bindings select: [ :each | each isActive ]
- add: aBinding
- ^ self bindings add: aBinding
- addActionKey: anInteger labelled: aString callback: aBlock
- self add: ((HLBindingAction on: anInteger labelled: aString)
- callback: aBlock;
- yourself)
- addGroupKey: anInteger labelled: aString
- self add: (HLBindingGroup on: anInteger labelled: aString)
- at: aString
- ^ self bindings
- detect: [ :each | each label = aString ]
- ifNone: [ nil ]
- at: aString add: aBinding
- | binding |
-
- binding := self at: aString.
- binding ifNil: [ ^ self ].
-
- binding add: aBinding
- atKey: anInteger
- ^ self bindings
- detect: [ :each | each key = anInteger ]
- ifNone: [ nil ]
- bindings
- ^ bindings ifNil: [ bindings := OrderedCollection new ]
- displayLabel
- ^ super displayLabel, '...'
- renderOn: aBindingHelper html: html
- self isActive ifTrue: [
- aBindingHelper renderBindingGroup: self on: html ]
- isActive
- ^ self activeBindings notEmpty
- HLBinding subclass: #HLBindingInput
- instanceVariableNames: 'input callback'
- package: 'Helios-KeyBindings'!
- callback
- ^ callback ifNil: [ callback := [ :value | ] ]
- callback: aBlock
- callback := aBlock
- input
- ^ input asJQuery val
- renderActionFor: aBinder html: html
- html span
- class: 'command';
- with: [
- input := html input
- class: 'search-query';
- placeholder: self displayLabel ].
-
- input onKeyPress: [:event |
- event keyCode = 13 ifTrue: [
- self applyOn: aBinder ] ].
-
- input asJQuery focus
- applyOn: aKeyBinder
- self evaluate: self input
- evaluate: aString
- self callback value: aString
- isActive
- ^ true
- Object subclass: #HLKeyBinder
- instanceVariableNames: 'modifierKey helper bindings selectedBinding'
- package: 'Helios-KeyBindings'!
- activationKey
- "SPACE"
- ^ 32
- activationKeyLabel
- ^ 'ctrl + space'
- bindings
- ^ bindings ifNil: [ bindings := self defaultBindings ]
- escapeKey
- "ESC"
- ^ 27
- helper
- ^ helper
- selectedBinding
- ^ selectedBinding ifNil: [ self bindings ]
- activate
- self helper show
- applyBinding: aBinding
- aBinding isActive ifFalse: [ ^ self ].
-
- self selectBinding: aBinding.
- aBinding applyOn: self.
-
- aBinding isFinal ifTrue: [ self deactivate ]
- deactivate
- selectedBinding := nil.
- self helper hide
- flushBindings
- bindings := nil
- selectBinding: aBinding
- selectedBinding := aBinding.
- self helper refresh
- defaultBindings
- | group |
-
- group := HLBindingGroup new
- addGroupKey: 86 labelled: 'View';
- add: HLCloseTabCommand new asBinding;
- yourself.
-
- HLOpenCommand registerConcreteClassesOn: group.
-
- ^ group
- handleActiveKeyDown: event
- "ESC or ctrl+g deactivate the keyBinder"
- (event which = self escapeKey or: [
- event which = 71 and: [ event ctrlKey ] ])
- ifTrue: [
- self deactivate.
- event preventDefault.
- ^ false ].
-
- "Handle the keybinding"
- ^ self handleBindingFor: event
- handleBindingFor: anEvent
- | binding |
- binding := self selectedBinding atKey: anEvent which.
-
- binding ifNotNil: [
- self applyBinding: binding.
- anEvent preventDefault.
- ^ false ]
- handleInactiveKeyDown: event
- event which = self activationKey ifTrue: [
- event ctrlKey ifTrue: [
- self activate.
- event preventDefault.
- ^ false ] ]
- handleKeyDown: event
- ^ self isActive
- ifTrue: [ self handleActiveKeyDown: event ]
- ifFalse: [ self handleInactiveKeyDown: event ]
- setupEvents
- (window jQuery: 'body') keydown: [ :event | self handleKeyDown: event ]
- initialize
- super initialize.
- helper := HLKeyBinderHelper on: self.
- helper
- renderStart;
- renderCog.
- active := false
- isActive
- ^ ('.', self helper cssClass) asJQuery is: ':visible'
- systemIsMac
- ^ navigator platform match: 'Mac'
- HLWidget subclass: #HLKeyBinderHelper
- instanceVariableNames: 'keyBinder'
- package: 'Helios-KeyBindings'!
- cssClass
- ^ 'key_helper'
- keyBinder
- ^ keyBinder
- keyBinder: aKeyBinder
- keyBinder := aKeyBinder
- selectedBinding
- ^ self keyBinder selectedBinding
- hide
- ('.', self cssClass) asJQuery remove.
- self showCog
- hideCog
- '#cog-helper' asJQuery hide
- show
- self hideCog.
- self appendToJQuery: 'body' asJQuery
- showCog
- '#cog-helper' asJQuery show
- registerBindings
- "Do nothing"
- renderBindingGroup: aBindingGroup on: html
- (aBindingGroup activeBindings
- sorted: [ :a :b | a key < b key ])
- do: [ :each | each renderActionFor: self keyBinder html: html ]
- renderBindingOn: html
- self selectedBinding renderOn: self html: html
- renderCloseOn: html
- html a
- class: 'close';
- with: [ (html tag: 'i') class: 'icon-remove' ];
- onClick: [ self keyBinder deactivate ]
- renderCog
- [ :html |
- html
- div id: 'cog-helper';
- with: [
- html a
- with: [ (html tag: 'i') class: 'icon-cog' ];
- onClick: [ self keyBinder activate ] ] ]
- appendToJQuery: 'body' asJQuery
- renderContentOn: html
- html div class: self cssClass; with: [
- self
- renderSelectionOn:html;
- renderBindingOn: html;
- renderCloseOn: html ]
- renderSelectionOn: html
- html span
- class: 'selected';
- with: (self selectedBinding label ifNil: [ 'Action' ])
- renderStart
- [ :html |
- html div
- id: 'keybinding-start-helper';
- with: 'Press ', self keyBinder activationKeyLabel, ' to start' ] appendToJQuery: 'body' asJQuery.
-
- [ (window jQuery: '#keybinding-start-helper') fadeOut: 1000 ]
- valueWithTimeout: 2000
- on: aKeyBinder
- ^ self new
- keyBinder: aKeyBinder;
- yourself
|