22
33import android .annotation .SuppressLint ;
44import android .content .Context ;
5- import android .graphics .Color ;
65import android .graphics .Rect ;
76import android .graphics .drawable .ColorDrawable ;
87import android .os .Build ;
1211import android .support .v7 .widget .ListPopupWindow ;
1312import android .text .Layout ;
1413import android .util .AttributeSet ;
14+ import android .view .KeyEvent ;
1515import android .view .View ;
1616import android .view .ViewGroup ;
1717import android .view .WindowManager ;
2222import com .duy .ide .code .api .SuggestItem ;
2323import com .duy .ide .editor .internal .suggestion .OnSuggestItemClickListener ;
2424import com .duy .ide .editor .internal .suggestion .SuggestionAdapter ;
25+ import com .duy .ide .editor .theme .model .EditorTheme ;
2526
2627import java .util .List ;
2728
28- public class SuggestionEditor extends EditActionSupportEditor
29- implements OnSuggestItemClickListener {
29+ public class SuggestionEditor extends EditActionSupportEditor {
3030
3131 private static final String TAG = "SuggestionEditor" ;
3232 private final Rect mTmpRect = new Rect ();
3333 @ Nullable
3434 private SuggestionAdapter mAdapter ;
3535 @ Nullable
3636 private OnSuggestItemClickListener mOnSuggestItemClickListener ;
37+ private ListPopupWindow mPopup ;
38+
3739 private AdapterView .OnItemClickListener mSuggestClickListener
3840 = new AdapterView .OnItemClickListener () {
3941 @ Override
4042 public void onItemClick (AdapterView <?> parent , View view , int position , long id ) {
4143 if (mAdapter != null ) {
42- SuggestItem description = mAdapter .getAllItems ().get (position );
43- onClickSuggest (SuggestionEditor .this , description );
44+ performCompletion (position );
4445 }
4546 }
4647 };
47- private ListPopupWindow mPopup ;
48+
4849 @ IdRes
4950 private int mDropDownAnchorId = View .NO_ID ;
5051
@@ -68,17 +69,20 @@ private void init(Context context, AttributeSet attrs, int defStyleAttr) {
6869 updateCharHeight ();
6970 mPopup = new ListPopupWindow (context , attrs , defStyleAttr , 0 );
7071 mPopup .setSoftInputMode (WindowManager .LayoutParams .SOFT_INPUT_ADJUST_RESIZE );
71- mPopup .setBackgroundDrawable (new ColorDrawable (Color .BLACK ));
72+ if (getEditorTheme () != null ) {
73+ mPopup .setBackgroundDrawable (new ColorDrawable (getEditorTheme ().getBgColor ()));
74+ }
7275 }
7376
7477 @ Override
75- public void onClickSuggest (@ NonNull IEditAreaView editAreaView ,
76- @ NonNull SuggestItem item ) {
77- if (mOnSuggestItemClickListener != null ) {
78- mOnSuggestItemClickListener . onClickSuggest ( this , item );
78+ public void setTheme (@ NonNull EditorTheme editorTheme ) {
79+ super . setTheme ( editorTheme );
80+ if (mPopup != null ) {
81+ mPopup . setBackgroundDrawable ( new ColorDrawable ( editorTheme . getBgColor ()) );
7982 }
8083 }
8184
85+
8286 @ Override
8387 public void onTextChanged (CharSequence s , int start , int before , int count ) {
8488 super .onTextChanged (s , start , before , count );
@@ -191,7 +195,7 @@ public void setSuggestData(@NonNull List<SuggestItem> data) {
191195 mAdapter .clearAllData ();
192196 } else {
193197 mAdapter = new SuggestionAdapter (getContext (), data );
194- mAdapter .setListener (this );
198+ mAdapter .setListener (mSuggestClickListener );
195199 mPopup .setAdapter (mAdapter );
196200 }
197201 mAdapter .setData (data );
@@ -389,4 +393,80 @@ protected boolean setFrame(final int l, int t, final int r, int b) {
389393 return result ;
390394 }
391395
396+ @ Override
397+ public boolean onKeyUp (int keyCode , KeyEvent event ) {
398+ boolean consumed = mPopup .onKeyUp (keyCode , event );
399+ if (consumed ) {
400+ switch (keyCode ) {
401+ // if the list accepts the key events and the key event
402+ // was a click, the text view gets the selected item
403+ // from the drop down as its content
404+ case KeyEvent .KEYCODE_ENTER :
405+ case KeyEvent .KEYCODE_DPAD_CENTER :
406+ case KeyEvent .KEYCODE_TAB :
407+ if (event .hasNoModifiers ()) {
408+ performCompletion ();
409+ }
410+ return true ;
411+ }
412+ }
413+
414+ if (isPopupShowing () && keyCode == KeyEvent .KEYCODE_TAB && event .hasNoModifiers ()) {
415+ performCompletion ();
416+ return true ;
417+ }
418+
419+ return super .onKeyUp (keyCode , event );
420+ }
421+
422+ @ SuppressLint ("RestrictedApi" )
423+ private void performCompletion () {
424+ performCompletion (-1 );
425+ }
426+
427+ @ SuppressLint ("RestrictedApi" )
428+ private void performCompletion (int position ) {
429+ if (isPopupShowing () && mAdapter != null ) {
430+ if (position < 0 ) {
431+ position = mPopup .getSelectedItemPosition ();
432+ }
433+ if (position < 0 ) {
434+ return ;
435+ }
436+ SuggestItem item = mAdapter .getItem (position );
437+ if (mOnSuggestItemClickListener != null ) {
438+ //noinspection ConstantConditions
439+ mOnSuggestItemClickListener .onClickSuggest (this , position , item );
440+ }
441+ }
442+
443+ if (!mPopup .isDropDownAlwaysVisible ()) {
444+ dismissDropDown ();
445+ }
446+ }
447+
448+ @ Override
449+ public boolean onKeyDown (int keyCode , KeyEvent event ) {
450+ if (mPopup .onKeyDown (keyCode , event )) {
451+ return true ;
452+ }
453+ if (isPopupShowing () && keyCode == KeyEvent .KEYCODE_TAB && event .hasNoModifiers ()) {
454+ return true ;
455+ }
456+ boolean handled = super .onKeyDown (keyCode , event );
457+
458+ if (handled && isPopupShowing ()) {
459+ clearListSelection ();
460+ }
461+
462+ return handled ;
463+ }
464+
465+ /**
466+ * <p>Clear the list selection. This may only be temporary, as user input will often bring
467+ * it back.
468+ */
469+ public void clearListSelection () {
470+ mPopup .clearListSelection ();
471+ }
392472}
0 commit comments