summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsky@chromium.org <sky@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-09-11 18:06:54 +0000
committersky@chromium.org <sky@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-09-11 18:06:54 +0000
commit031cf3177e536f6f708fbe6328252f63a3a16c4e (patch)
tree631d0efdd3d3ac744c25b8cbb8cd32e11a818934
parent3a13312bf873947f06d5f71b55aa92246a43f66f (diff)
downloadchromium_src-031cf3177e536f6f708fbe6328252f63a3a16c4e.zip
chromium_src-031cf3177e536f6f708fbe6328252f63a3a16c4e.tar.gz
chromium_src-031cf3177e536f6f708fbe6328252f63a3a16c4e.tar.bz2
Adds support for the location bar to show suggested text. There are
still a couple of rough edges, but this is a good start. BUG=54833 TEST=none Review URL: http://codereview.chromium.org/3333027 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@59192 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--chrome/browser/autocomplete/autocomplete_edit_view_gtk.cc12
-rw-r--r--chrome/browser/autocomplete/autocomplete_edit_view_gtk.h17
-rw-r--r--chrome/browser/autocomplete/autocomplete_edit_view_win.cc31
-rw-r--r--chrome/browser/autocomplete/autocomplete_edit_view_win.h18
-rw-r--r--chrome/browser/location_bar.h7
-rw-r--r--chrome/browser/views/location_bar/location_bar_view.cc68
-rw-r--r--chrome/browser/views/location_bar/location_bar_view.h6
-rw-r--r--chrome/test/test_location_bar.h1
8 files changed, 149 insertions, 11 deletions
diff --git a/chrome/browser/autocomplete/autocomplete_edit_view_gtk.cc b/chrome/browser/autocomplete/autocomplete_edit_view_gtk.cc
index b6e44cc..c23c5ab 100644
--- a/chrome/browser/autocomplete/autocomplete_edit_view_gtk.cc
+++ b/chrome/browser/autocomplete/autocomplete_edit_view_gtk.cc
@@ -341,6 +341,18 @@ int AutocompleteEditViewGtk::TextWidth() {
horizontal_border_size;
}
+int AutocompleteEditViewGtk::WidthOfTextAfterCursor() {
+ // TODO(sky): implement this.
+ NOTIMPLEMENTED();
+ return TextWidth();
+}
+
+gfx::Font AutocompleteEditViewGtk::GetFont() {
+ GtkRcStyle* rc_style = gtk_widget_get_modifier_style(text_view_);
+ return gfx::Font((rc_style && rc_style->font_desc) ?
+ rc_style->font_desc : text_view_->style->font_desc);
+}
+
void AutocompleteEditViewGtk::SaveStateToTab(TabContents* tab) {
DCHECK(tab);
// If any text has been selected, register it as the PRIMARY selection so it
diff --git a/chrome/browser/autocomplete/autocomplete_edit_view_gtk.h b/chrome/browser/autocomplete/autocomplete_edit_view_gtk.h
index 7e94ca2..ba014f1 100644
--- a/chrome/browser/autocomplete/autocomplete_edit_view_gtk.h
+++ b/chrome/browser/autocomplete/autocomplete_edit_view_gtk.h
@@ -29,6 +29,11 @@ class AutocompleteEditModel;
class AutocompletePopupView;
class Profile;
class TabContents;
+
+namespace gfx{
+class Font;
+}
+
namespace views {
class View;
}
@@ -70,10 +75,18 @@ class AutocompleteEditViewGtk : public AutocompleteEditView,
// Initialize, create the underlying widgets, etc.
void Init();
- // Returns the width, in pixels, needed to display the current text. The
- // returned value includes margins and borders.
+ // Returns the width in pixels needed to display the current text. The
+ // returned value includes margins.
int TextWidth();
+ // Returns the width in pixels needed to display the text from one character
+ // before the caret to the end of the string. See comments in
+ // LocationBarView::Layout as to why this uses -1.
+ int WidthOfTextAfterCursor();
+
+ // Returns the font.
+ gfx::Font GetFont();
+
// Implement the AutocompleteEditView interface.
virtual AutocompleteEditModel* model() { return model_.get(); }
virtual const AutocompleteEditModel* model() const { return model_.get(); }
diff --git a/chrome/browser/autocomplete/autocomplete_edit_view_win.cc b/chrome/browser/autocomplete/autocomplete_edit_view_win.cc
index a00526b..2238584 100644
--- a/chrome/browser/autocomplete/autocomplete_edit_view_win.cc
+++ b/chrome/browser/autocomplete/autocomplete_edit_view_win.cc
@@ -496,6 +496,21 @@ AutocompleteEditViewWin::~AutocompleteEditViewWin() {
g_paint_patcher.Pointer()->DerefPatch();
}
+int AutocompleteEditViewWin::TextWidth() {
+ return WidthNeededToDisplay(GetText());
+}
+
+int AutocompleteEditViewWin::WidthOfTextAfterCursor() {
+ CHARRANGE selection;
+ GetSelection(selection);
+ const int start = std::max(0, static_cast<int>(selection.cpMax - 1));
+ return WidthNeededToDisplay(GetText().substr(start));
+ }
+
+gfx::Font AutocompleteEditViewWin::GetFont() {
+ return font_;
+}
+
void AutocompleteEditViewWin::SaveStateToTab(TabContents* tab) {
DCHECK(tab);
@@ -2501,3 +2516,19 @@ void AutocompleteEditViewWin::TrackMousePosition(MouseButton button,
click_point_[button] = point;
}
}
+
+int AutocompleteEditViewWin::GetHorizontalMargin() {
+ RECT rect;
+ GetRect(&rect);
+ RECT client_rect;
+ GetClientRect(&client_rect);
+ return (rect.left - client_rect.left) + (client_rect.right - rect.right);
+}
+
+int AutocompleteEditViewWin::WidthNeededToDisplay(const std::wstring& text) {
+ // Use font_.GetStringWidth() instead of
+ // PosFromChar(location_entry_->GetTextLength()) because PosFromChar() is
+ // apparently buggy. In both LTR UI and RTL UI with left-to-right layout,
+ // PosFromChar(i) might return 0 when i is greater than 1.
+ return font_.GetStringWidth(text) + GetHorizontalMargin();
+}
diff --git a/chrome/browser/autocomplete/autocomplete_edit_view_win.h b/chrome/browser/autocomplete/autocomplete_edit_view_win.h
index 25fb45f..a993d80 100644
--- a/chrome/browser/autocomplete/autocomplete_edit_view_win.h
+++ b/chrome/browser/autocomplete/autocomplete_edit_view_win.h
@@ -74,6 +74,18 @@ class AutocompleteEditViewWin
views::View* parent_view() const { return parent_view_; }
+ // Returns the width in pixels needed to display the current text. The
+ // returned value includes margins.
+ int TextWidth();
+
+ // Returns the width in pixels needed to display the text from one character
+ // before the caret to the end of the string. See comments in
+ // LocationBarView::Layout as to why this uses -1.
+ int WidthOfTextAfterCursor();
+
+ // Returns the font.
+ gfx::Font GetFont();
+
// Implement the AutocompleteEditView interface.
virtual AutocompleteEditModel* model() { return model_.get(); }
virtual const AutocompleteEditModel* model() const { return model_.get(); }
@@ -386,6 +398,12 @@ class AutocompleteEditViewWin
void SelectAllIfNecessary(MouseButton button, const CPoint& point);
void TrackMousePosition(MouseButton button, const CPoint& point);
+ // Returns the sum of the left and right margins.
+ int GetHorizontalMargin();
+
+ // Returns the width in pixels needed to display |text|.
+ int WidthNeededToDisplay(const std::wstring& text);
+
scoped_ptr<AutocompleteEditModel> model_;
scoped_ptr<AutocompletePopupView> popup_view_;
diff --git a/chrome/browser/location_bar.h b/chrome/browser/location_bar.h
index 8ead356..d16bd8f 100644
--- a/chrome/browser/location_bar.h
+++ b/chrome/browser/location_bar.h
@@ -28,6 +28,13 @@ class LocationBar {
// Shows the first run information bubble anchored to the location bar.
virtual void ShowFirstRunBubble(FirstRun::BubbleType bubble_type) = 0;
+ // TODO: port
+#if defined(TOOLKIT_VIEWS)
+ // Sets the suggested text to show in the omnibox. This is shown in addition
+ // to the current text of the omnibox.
+ virtual void SetSuggestedText(const std::wstring& text) = 0;
+#endif
+
// Returns the string of text entered in the location bar.
virtual std::wstring GetInputString() const = 0;
diff --git a/chrome/browser/views/location_bar/location_bar_view.cc b/chrome/browser/views/location_bar/location_bar_view.cc
index b3d1e7f..79bb6e8 100644
--- a/chrome/browser/views/location_bar/location_bar_view.cc
+++ b/chrome/browser/views/location_bar/location_bar_view.cc
@@ -38,6 +38,7 @@
#include "gfx/skia_util.h"
#include "grit/generated_resources.h"
#include "grit/theme_resources.h"
+#include "views/controls/label.h"
#include "views/drag_utils.h"
#if defined(OS_WIN)
@@ -91,6 +92,7 @@ LocationBarView::LocationBarView(Profile* profile,
ev_bubble_view_(NULL),
location_entry_view_(NULL),
selected_keyword_view_(NULL),
+ suggested_text_view_(NULL),
keyword_hint_view_(NULL),
star_view_(NULL),
mode_(mode),
@@ -578,6 +580,35 @@ void LocationBarView::Layout() {
}
}
+ // Layout out the suggested text view right aligned to the location
+ // entry. Only show the suggested text if we can fit the text from one
+ // character before the end of the selection to the end of the text and the
+ // suggested text. If we can't it means either the suggested text is too big,
+ // or the user has scrolled.
+
+ // TODO(sky): We could potentially combine this with the previous step to
+ // force using minimum size if necessary, but currently the chance of showing
+ // keyword hints and suggested text is minimal and we're not confident this
+ // is the right approach for suggested text.
+ if (suggested_text_view_) {
+ // TODO(sky): need to layout when the user changes caret position.
+ int suggested_text_width = suggested_text_view_->GetPreferredSize().width();
+ int vis_text_width = location_entry_->WidthOfTextAfterCursor();
+ if (vis_text_width + suggested_text_width > entry_width) {
+ // Hide the suggested text if the user has scrolled or we can't fit all
+ // the suggested text.
+ suggested_text_view_->SetBounds(0, 0, 0, 0);
+ } else {
+ int location_needed_width = location_entry_->TextWidth();
+ location_bounds.set_width(std::min(location_needed_width,
+ entry_width - suggested_text_width));
+ suggested_text_view_->SetBounds(location_bounds.right(),
+ location_bounds.y(),
+ suggested_text_width,
+ location_bounds.height());
+ }
+ }
+
location_entry_view_->SetBounds(location_bounds);
}
@@ -772,17 +803,9 @@ std::wstring LocationBarView::GetTitle() const {
}
int LocationBarView::AvailableWidth(int location_bar_width) {
-#if defined(OS_WIN)
- // Use font_.GetStringWidth() instead of
- // PosFromChar(location_entry_->GetTextLength()) because PosFromChar() is
- // apparently buggy. In both LTR UI and RTL UI with left-to-right layout,
- // PosFromChar(i) might return 0 when i is greater than 1.
- return std::max(
- location_bar_width - font_.GetStringWidth(location_entry_->GetText()), 0);
-#else
return location_bar_width - location_entry_->TextWidth();
-#endif
}
+
void LocationBarView::LayoutView(views::View* view,
int padding,
int available_width,
@@ -1001,6 +1024,33 @@ void LocationBarView::ShowFirstRunBubble(FirstRun::BubbleType bubble_type) {
ShowFirstRunBubbleInternal(bubble_type);
}
+void LocationBarView::SetSuggestedText(const std::wstring& text) {
+ if (!text.empty()) {
+ if (!suggested_text_view_) {
+ suggested_text_view_ = new views::Label();
+ suggested_text_view_->SetHorizontalAlignment(views::Label::ALIGN_LEFT);
+ suggested_text_view_->SetColor(
+ GetColor(ToolbarModel::NONE,
+ LocationBarView::DEEMPHASIZED_TEXT));
+ suggested_text_view_->SetText(text);
+ suggested_text_view_->SetFont(location_entry_->GetFont());
+ AddChildView(suggested_text_view_);
+ } else if (suggested_text_view_->GetText() == text) {
+ return;
+ } else {
+ suggested_text_view_->SetText(text);
+ }
+ } else if (suggested_text_view_) {
+ delete suggested_text_view_;
+ suggested_text_view_ = NULL;
+ } else {
+ return;
+ }
+
+ Layout();
+ SchedulePaint();
+}
+
std::wstring LocationBarView::GetInputString() const {
return location_input_;
}
diff --git a/chrome/browser/views/location_bar/location_bar_view.h b/chrome/browser/views/location_bar/location_bar_view.h
index d67720b..120de8d 100644
--- a/chrome/browser/views/location_bar/location_bar_view.h
+++ b/chrome/browser/views/location_bar/location_bar_view.h
@@ -46,6 +46,7 @@ class TemplateURLModel;
namespace views {
class HorizontalPainter;
+class Label;
};
/////////////////////////////////////////////////////////////////////////////
@@ -200,6 +201,7 @@ class LocationBarView : public LocationBar,
// Overridden from LocationBar:
virtual void ShowFirstRunBubble(FirstRun::BubbleType bubble_type);
+ virtual void SetSuggestedText(const std::wstring& text);
virtual std::wstring GetInputString() const;
virtual WindowOpenDisposition GetWindowOpenDisposition() const;
virtual PageTransition::Type GetPageTransition() const;
@@ -344,6 +346,10 @@ class LocationBarView : public LocationBar,
// Shown if the user has selected a keyword.
SelectedKeywordView* selected_keyword_view_;
+ // View responsible for showing suggested text. This is NULL when there is no
+ // suggested text.
+ views::Label* suggested_text_view_;
+
// Shown if the selected url has a corresponding keyword.
KeywordHintView* keyword_hint_view_;
diff --git a/chrome/test/test_location_bar.h b/chrome/test/test_location_bar.h
index e968672..eab648f 100644
--- a/chrome/test/test_location_bar.h
+++ b/chrome/test/test_location_bar.h
@@ -30,6 +30,7 @@ class TestLocationBar : public LocationBar {
// Overridden from LocationBar:
virtual void ShowFirstRunBubble(FirstRun::BubbleType bubble_type) {}
+ virtual void SetSuggestedText(const std::wstring& text) {}
virtual std::wstring GetInputString() const { return input_string_; }
virtual WindowOpenDisposition GetWindowOpenDisposition() const {
return disposition_;