OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.chrome.shell.omnibox; | 5 package org.chromium.chrome.shell.omnibox; |
6 | 6 |
7 import android.content.Context; | 7 import android.content.Context; |
8 import android.graphics.Rect; | |
8 import android.os.Handler; | 9 import android.os.Handler; |
9 import android.text.Editable; | 10 import android.text.Editable; |
10 import android.text.TextUtils; | 11 import android.text.TextUtils; |
11 import android.text.TextWatcher; | 12 import android.text.TextWatcher; |
12 import android.view.View; | 13 import android.view.View; |
13 import android.view.View.OnLayoutChangeListener; | 14 import android.view.View.OnLayoutChangeListener; |
14 import android.widget.AdapterView; | 15 import android.widget.AdapterView; |
15 import android.widget.AdapterView.OnItemClickListener; | 16 import android.widget.AdapterView.OnItemClickListener; |
16 import android.widget.ListPopupWindow; | 17 import android.widget.ListPopupWindow; |
17 import android.widget.PopupWindow.OnDismissListener; | 18 import android.widget.PopupWindow.OnDismissListener; |
18 import android.widget.TextView; | 19 import android.widget.TextView; |
19 | 20 |
20 import org.chromium.chrome.browser.omnibox.AutocompleteController; | 21 import org.chromium.chrome.browser.omnibox.AutocompleteController; |
21 import org.chromium.chrome.browser.omnibox.AutocompleteController.OnSuggestionsR eceivedListener; | 22 import org.chromium.chrome.browser.omnibox.AutocompleteController.OnSuggestionsR eceivedListener; |
22 import org.chromium.chrome.browser.omnibox.OmniboxSuggestion; | 23 import org.chromium.chrome.browser.omnibox.OmniboxSuggestion; |
24 import org.chromium.chrome.shell.ChromeShellActivity; | |
23 import org.chromium.chrome.shell.ChromeShellToolbar; | 25 import org.chromium.chrome.shell.ChromeShellToolbar; |
24 import org.chromium.chrome.shell.R; | 26 import org.chromium.chrome.shell.R; |
25 | 27 |
26 import java.util.List; | 28 import java.util.List; |
27 | 29 |
28 /** | 30 /** |
29 * Displays suggestions for the text that is entered to the ChromeShell URL fiel d. | 31 * Displays suggestions for the text that is entered to the ChromeShell URL fiel d. |
30 */ | 32 */ |
31 public class SuggestionPopup implements OnSuggestionsReceivedListener, TextWatch er { | 33 public class SuggestionPopup implements OnSuggestionsReceivedListener, TextWatch er { |
32 private static final long SUGGESTION_START_DELAY_MS = 30; | 34 private static final long SUGGESTION_START_DELAY_MS = 30; |
33 | 35 |
34 private final Context mContext; | 36 private final Context mContext; |
35 private final TextView mUrlField; | 37 private final TextView mUrlField; |
36 private final ChromeShellToolbar mToolbar; | 38 private final ChromeShellToolbar mToolbar; |
37 private final AutocompleteController mAutocomplete; | 39 private final AutocompleteController mAutocomplete; |
38 | 40 |
39 private boolean mHasStartedNewOmniboxEditSession; | 41 private boolean mHasStartedNewOmniboxEditSession; |
40 private Runnable mRequestSuggestions; | 42 private Runnable mRequestSuggestions; |
41 private ListPopupWindow mSuggestionsPopup; | 43 private ListPopupWindow mSuggestionsPopup; |
42 private SuggestionArrayAdapter mSuggestionArrayAdapter; | 44 private SuggestionArrayAdapter mSuggestionArrayAdapter; |
45 private int mSuggestionsPopupItemsCount; | |
43 | 46 |
44 /** | 47 /** |
45 * Initializes a suggestion popup that will track urlField value and display suggestions based | 48 * Initializes a suggestion popup that will track urlField value and display suggestions based |
46 * on that value. | 49 * on that value. |
47 */ | 50 */ |
48 public SuggestionPopup(Context context, TextView urlField, | 51 public SuggestionPopup(Context context, TextView urlField, |
49 ChromeShellToolbar toolbar) { | 52 ChromeShellToolbar toolbar) { |
50 mContext = context; | 53 mContext = context; |
51 mUrlField = urlField; | 54 mUrlField = urlField; |
52 mToolbar = toolbar; | 55 mToolbar = toolbar; |
53 mAutocomplete = new AutocompleteController(this); | 56 mAutocomplete = new AutocompleteController(this); |
54 OnLayoutChangeListener listener = new OnLayoutChangeListener() { | 57 OnLayoutChangeListener listener = new OnLayoutChangeListener() { |
55 @Override | 58 @Override |
56 public void onLayoutChange(View v, int left, int top, int right, int bottom, | 59 public void onLayoutChange(View v, int left, int top, int right, int bottom, |
57 int oldLeft, int oldTop, int oldRight, int oldBottom) { | 60 int oldLeft, int oldTop, int oldRight, int oldBottom) { |
58 if (mSuggestionsPopup == null || !mSuggestionsPopup.isShowing()) return; | 61 if (mSuggestionsPopup == null || !mSuggestionsPopup.isShowing()) return; |
59 mSuggestionsPopup.setWidth(mUrlField.getWidth()); | 62 mSuggestionsPopup.setWidth(mUrlField.getWidth()); |
63 mSuggestionsPopup.setHeight(getSuggestionPopupHeight()); | |
60 mSuggestionsPopup.show(); | 64 mSuggestionsPopup.show(); |
61 } | 65 } |
62 }; | 66 }; |
63 mUrlField.addOnLayoutChangeListener(listener); | 67 mUrlField.addOnLayoutChangeListener(listener); |
64 } | 68 } |
65 | 69 |
66 private void navigateToSuggestion(int position) { | 70 private void navigateToSuggestion(int position) { |
67 mToolbar.getCurrentTab().loadUrlWithSanitization( | 71 mToolbar.getCurrentTab().loadUrlWithSanitization( |
68 mSuggestionArrayAdapter.getItem(position).getUrl()); | 72 mSuggestionArrayAdapter.getItem(position).getUrl()); |
69 mUrlField.clearFocus(); | 73 mUrlField.clearFocus(); |
(...skipping 21 matching lines...) Expand all Loading... | |
91 * Signals the autocomplete controller to stop generating suggestions and | 95 * Signals the autocomplete controller to stop generating suggestions and |
92 * cancels the queued task to start the autocomplete controller, if any. | 96 * cancels the queued task to start the autocomplete controller, if any. |
93 * | 97 * |
94 * @param clear Whether to clear the most recent autocomplete results. | 98 * @param clear Whether to clear the most recent autocomplete results. |
95 */ | 99 */ |
96 private void stopAutocomplete(boolean clear) { | 100 private void stopAutocomplete(boolean clear) { |
97 if (mAutocomplete != null) mAutocomplete.stop(clear); | 101 if (mAutocomplete != null) mAutocomplete.stop(clear); |
98 if (mRequestSuggestions != null) mRequestSuggestions = null; | 102 if (mRequestSuggestions != null) mRequestSuggestions = null; |
99 } | 103 } |
100 | 104 |
105 private int getSuggestionPopupHeight() { | |
106 Rect appRect = new Rect(); | |
107 ((ChromeShellActivity)mContext).getWindow().getDecorView(). | |
108 getWindowVisibleDisplayFrame(appRect); | |
109 int dropDownItemHeight = mContext.getResources(). | |
110 getDimensionPixelSize(R.dimen.dropdown_item_height); | |
111 int popupHeight = appRect.height(); | |
112 if (mSuggestionsPopup != null) { | |
113 int height = dropDownItemHeight; | |
Bernhard Bauer
2014/09/02 08:54:15
This assignment is unnecessary.
ankit
2014/09/02 09:23:04
Done.
| |
114 height = mSuggestionsPopupItemsCount * dropDownItemHeight; | |
115 if (height < appRect.height()) | |
Bernhard Bauer
2014/09/02 08:54:15
You could use popupHeight instead of appRect.heigh
ankit
2014/09/02 09:23:04
Done.
| |
116 popupHeight = height; | |
117 } | |
118 // Maintaining margin height equal to |dropDownItemHeight|. | |
119 return (popupHeight - dropDownItemHeight > 0) ? | |
120 (popupHeight - dropDownItemHeight) : popupHeight; | |
Bernhard Bauer
2014/09/02 08:54:15
There's still a discontinuity here: As |popupHeigh
ankit
2014/09/02 09:23:04
As per current logic when number of suggestion ite
Bernhard Bauer
2014/09/02 09:31:01
Hm, now that I think about it, doesn't the margin
| |
121 } | |
122 | |
101 // OnSuggestionsReceivedListener implementation | 123 // OnSuggestionsReceivedListener implementation |
102 | |
103 @Override | 124 @Override |
104 public void onSuggestionsReceived(List<OmniboxSuggestion> suggestions, | 125 public void onSuggestionsReceived(List<OmniboxSuggestion> suggestions, |
105 String inlineAutocompleteText) { | 126 String inlineAutocompleteText) { |
106 if (!mUrlField.isFocused() || suggestions.isEmpty()) | 127 if (!mUrlField.isFocused() || suggestions.isEmpty()) |
107 return; | 128 return; |
129 mSuggestionsPopupItemsCount = suggestions.size(); | |
108 if (mSuggestionsPopup == null) { | 130 if (mSuggestionsPopup == null) { |
109 mSuggestionsPopup = new ListPopupWindow( | 131 mSuggestionsPopup = new ListPopupWindow( |
110 mContext, null, android.R.attr.autoCompleteTextViewStyle); | 132 mContext, null, android.R.attr.autoCompleteTextViewStyle); |
111 mSuggestionsPopup.setOnDismissListener(new OnDismissListener() { | 133 mSuggestionsPopup.setOnDismissListener(new OnDismissListener() { |
112 @Override | 134 @Override |
113 public void onDismiss() { | 135 public void onDismiss() { |
114 mHasStartedNewOmniboxEditSession = false; | 136 mHasStartedNewOmniboxEditSession = false; |
115 mSuggestionArrayAdapter = null; | 137 mSuggestionArrayAdapter = null; |
116 } | 138 } |
117 }); | 139 }); |
118 } | 140 } |
141 mSuggestionsPopup.setInputMethodMode(ListPopupWindow.INPUT_METHOD_NEEDED ); | |
119 mSuggestionsPopup.setWidth(mUrlField.getWidth()); | 142 mSuggestionsPopup.setWidth(mUrlField.getWidth()); |
120 mSuggestionArrayAdapter = | 143 mSuggestionArrayAdapter = |
121 new SuggestionArrayAdapter(mContext, R.layout.dropdown_item, sug gestions); | 144 new SuggestionArrayAdapter(mContext, R.layout.dropdown_item, sug gestions); |
145 mSuggestionsPopup.setHeight(getSuggestionPopupHeight()); | |
122 mSuggestionsPopup.setAdapter(mSuggestionArrayAdapter); | 146 mSuggestionsPopup.setAdapter(mSuggestionArrayAdapter); |
123 mSuggestionsPopup.setAnchorView(mUrlField); | 147 mSuggestionsPopup.setAnchorView(mUrlField); |
124 mSuggestionsPopup.setOnItemClickListener(new OnItemClickListener() { | 148 mSuggestionsPopup.setOnItemClickListener(new OnItemClickListener() { |
125 @Override | 149 @Override |
126 public void onItemClick(AdapterView<?> parent, View view, int positi on, long id) { | 150 public void onItemClick(AdapterView<?> parent, View view, int positi on, long id) { |
127 navigateToSuggestion(position); | 151 navigateToSuggestion(position); |
128 } | 152 } |
129 }); | 153 }); |
130 mSuggestionsPopup.show(); | 154 mSuggestionsPopup.show(); |
131 } | 155 } |
(...skipping 29 matching lines...) Expand all Loading... | |
161 | 185 |
162 @Override | 186 @Override |
163 public void beforeTextChanged(CharSequence s, int start, int count, int afte r) { | 187 public void beforeTextChanged(CharSequence s, int start, int count, int afte r) { |
164 mRequestSuggestions = null; | 188 mRequestSuggestions = null; |
165 } | 189 } |
166 | 190 |
167 @Override | 191 @Override |
168 public void onTextChanged(CharSequence s, int start, int before, int count) { | 192 public void onTextChanged(CharSequence s, int start, int before, int count) { |
169 } | 193 } |
170 } | 194 } |
OLD | NEW |