#UI_ENHANCEMENT by cg
class: EditFieldWithCompletion
class definition
added:5 methods
changed:5 methods
added tab-completion.
--- a/EditFieldWithCompletion.st Wed May 04 13:03:08 2016 +0200
+++ b/EditFieldWithCompletion.st Wed May 04 13:04:30 2016 +0200
@@ -11,9 +11,11 @@
"
"{ Package: 'stx:libtool' }"
+"{ NameSpace: Smalltalk }"
+
EditField subclass:#EditFieldWithCompletion
instanceVariableNames:'showOptions optionsHolder optionsView optionsWindow
- focusEventsToIgnore hadFocus completionJob'
+ focusEventsToIgnore hadFocus completionJob tabCompletionJob'
classVariableNames:''
poolDictionaries:''
category:'Views-Text'
@@ -176,7 +178,7 @@
col:newContent size
].
"
- options size > 1 ifTrue:[
+ true "options size > 1" ifTrue:[
optionsHolder value:options.
self showOptionsWindow
]
@@ -243,17 +245,21 @@
<resource: #keyboard (#CursorDown #CursorUp #Accept #Return #Escape #BackSpace #Delete)>
(optionsView notNil
- and:[#(CursorDown CursorUp) includes:key]) ifTrue:[
+ and:[#(CursorDown CursorUp) includesIdentical:key]) ifTrue:[
optionsView keyPress:key x:x y:y.
^ self.
].
- key = #Accept ifTrue:[self hideOptionsWindow].
- key = #Return ifTrue:[self hideOptionsWindow].
- key = #Escape ifTrue:[self hideOptionsWindow].
- (key = #BackSpace or:[key = #Delete]) ifTrue:[
+ key == #Accept ifTrue:[self hideOptionsWindow].
+ key == #Return ifTrue:[self hideOptionsWindow].
+ key == #Escape ifTrue:[self hideOptionsWindow].
+ (key == #BackSpace or:[key == #Delete or:[key == #Tab]]) ifTrue:[
super keyPress:key x:x y:y.
- self startCompletion.
+ key == #Tab ifTrue:[
+ self startTabCompletion
+ ] ifFalse:[
+ self startCompletion.
+ ].
^ self.
].
@@ -269,6 +275,34 @@
self keyPress: key x:x y:y
"Created: / 09-12-2010 / 21:32:20 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+tabCompletion:best options:options
+ self updateContentsForLongest:best options:options.
+
+ true "options size > 1" ifTrue:[
+ optionsHolder value:options.
+ self showOptionsWindow
+ ].
+ "/ start another job, to update the list
+ self doCompletion.
+!
+
+updateContentsForLongest:best options:options
+ |newContent oldContent|
+
+ oldContent := ((self contents ? '') asString).
+ newContent := best ? oldContent.
+ newContent ~= oldContent ifTrue:[
+ self contents:newContent asString.
+ self cursorCol:oldContent size + 1.
+ oldContent size < newContent size ifTrue:[
+ self
+ selectFromLine:1 col:oldContent size + 1
+ toLine:1 col:newContent size.
+ typeOfSelection := #paste
+ ].
+ ].
! !
!EditFieldWithCompletion methodsFor:'initialization & release'!
@@ -288,9 +322,13 @@
showOptions := true.
optionsHolder := ValueHolder new.
focusEventsToIgnore := 0.
+
completionJob := BackgroundJob
named: 'Edit Field Completion Job'
on:[self doCompletion].
+ tabCompletionJob := BackgroundJob
+ named: 'Edit Field Completion Job'
+ on:[self doTabCompletion].
"Created: / 08-08-2009 / 20:24:05 / Jan Vrany <vranyj1@fel.cvut.cz>"
"Modified: / 09-08-2009 / 08:17:09 / Jan Vrany <vranyj1@fel.cvut.cz>"
@@ -300,27 +338,32 @@
!EditFieldWithCompletion methodsFor:'private'!
doCompletion
- |oldContents options best|
+ self doCompletionThenSend:#completion:options:
+!
+
+doCompletionThenSend:selector
+ |oldContents completionInfo options best|
oldContents := (self contents ? '') asString.
- options := self entryCompletionBlock
- valueWithOptionalArgument:oldContents
- and:self.
- options isNil ifTrue:[
+ completionInfo := self entryCompletionBlock
+ valueWithOptionalArgument:oldContents
+ and:self.
+ completionInfo isNil ifTrue:[
^ self
].
- best := options first.
- options := options second.
- (options includes:best) ifFalse:[
- best := options
- detect:[:e | e asString startsWith:best asString ]
- ifNone:[ best ]
- ].
- options isSortedCollection ifFalse:[
- options := options asSortedCollection:[:a :b | a displayString < b displayString ]
- ].
+
+ best := completionInfo first.
+ options := completionInfo second.
+"/ (options includes:best) ifFalse:[
+"/ best := options
+"/ detect:[:e | e asString startsWith:best asString ]
+"/ ifNone:[ best ]
+"/ ].
+"/ options isSortedCollection ifFalse:[
+"/ options := options asSortedCollection:[:a :b | a displayString < b displayString ]
+"/ ].
self sensor
- pushUserEvent:#completion:options:
+ pushUserEvent:selector
for:self
withArguments:(Array with:best with:options).
@@ -331,6 +374,10 @@
"Modified: / 20-04-2012 / 18:20:08 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!
+doTabCompletion
+ self doCompletionThenSend:#tabCompletion:options:
+!
+
hideOptionsWindow
optionsWindow notNil ifTrue:[
optionsWindow destroy.
@@ -347,7 +394,10 @@
| x y w |
showOptions ifFalse:[^ self].
- optionsWindow notNil ifTrue:[ ^ self ].
+ optionsWindow notNil ifTrue:[
+ optionsWindow raise.
+ ^ self
+ ].
optionsView := SelectionInListModelView new
textStartLeft: textStartLeft - 2;
@@ -402,15 +452,20 @@
"Created: / 26-07-2009 / 17:41:41 / Jan Vrany <vranyj1@fel.cvut.cz>"
"Modified (format): / 03-08-2011 / 17:50:53 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+startTabCompletion
+
+ tabCompletionJob restart.
! !
!EditFieldWithCompletion class methodsFor:'documentation'!
version_CVS
- ^ '$Header: /cvs/stx/stx/libtool/EditFieldWithCompletion.st,v 1.9 2014-12-03 23:37:48 cg Exp $'
+ ^ '$Header$'
!
version_SVN
- ^ '$Id: EditFieldWithCompletion.st,v 1.9 2014-12-03 23:37:48 cg Exp $'
+ ^ '$Id$'
! !