diff options
author | melevin@chromium.org <melevin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-12-14 01:11:04 +0000 |
---|---|---|
committer | melevin@chromium.org <melevin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-12-14 01:11:04 +0000 |
commit | ec4aad544c94b17ad2a01537e5bbbec5cd4d13d4 (patch) | |
tree | 95bc5a69c4627ec014856268fbc077f56a3472d2 | |
parent | e34dc5abef02c60b672ba0fae814233682bbbd01 (diff) | |
download | chromium_src-ec4aad544c94b17ad2a01537e5bbbec5cd4d13d4.zip chromium_src-ec4aad544c94b17ad2a01537e5bbbec5cd4d13d4.tar.gz chromium_src-ec4aad544c94b17ad2a01537e5bbbec5cd4d13d4.tar.bz2 |
Implement the Instant extended API startMargin, endMargin, and rtl properties and the onmarginchange event for Views only.
BUG=153403
Review URL: https://chromiumcodereview.appspot.com/11359198
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@173028 0039d316-1c4b-4281-b951-d872f2087c98
23 files changed, 232 insertions, 63 deletions
diff --git a/chrome/browser/instant/instant_client.cc b/chrome/browser/instant/instant_client.cc index 1582a97..aa46868 100644 --- a/chrome/browser/instant/instant_client.cc +++ b/chrome/browser/instant/instant_client.cc @@ -36,8 +36,12 @@ void InstantClient::Cancel(const string16& text) { Send(new ChromeViewMsg_SearchBoxCancel(routing_id(), text)); } -void InstantClient::SetOmniboxBounds(const gfx::Rect& bounds) { - Send(new ChromeViewMsg_SearchBoxResize(routing_id(), bounds)); +void InstantClient::SetPopupBounds(const gfx::Rect& bounds) { + Send(new ChromeViewMsg_SearchBoxPopupResize(routing_id(), bounds)); +} + +void InstantClient::SetMarginSize(const int start, const int end) { + Send(new ChromeViewMsg_SearchBoxMarginChange(routing_id(), start, end)); } void InstantClient::DetermineIfPageSupportsInstant() { diff --git a/chrome/browser/instant/instant_client.h b/chrome/browser/instant/instant_client.h index 8a4b54f..0f44868 100644 --- a/chrome/browser/instant/instant_client.h +++ b/chrome/browser/instant/instant_client.h @@ -99,7 +99,10 @@ class InstantClient : public content::WebContentsObserver { // Tells the page the bounds of the omnibox dropdown (in screen coordinates). // This is used by the page to offset the results to avoid them being covered // by the omnibox dropdown. - void SetOmniboxBounds(const gfx::Rect& bounds); + void SetPopupBounds(const gfx::Rect& bounds); + + // Tells the page what size start and end margins to use. + void SetMarginSize(const int start, const int end); // Tells the renderer to determine if the page supports the Instant API, which // results in a call to InstantSupportDetermined() when the reply is received. diff --git a/chrome/browser/instant/instant_controller.cc b/chrome/browser/instant/instant_controller.cc index 6dbc404..7ec753c 100644 --- a/chrome/browser/instant/instant_controller.cc +++ b/chrome/browser/instant/instant_controller.cc @@ -157,6 +157,8 @@ InstantController::InstantController(chrome::BrowserInstantController* browser, last_transition_type_(content::PAGE_TRANSITION_LINK), last_match_was_search_(false), omnibox_focus_state_(OMNIBOX_FOCUS_NONE), + start_margin_(0), + end_margin_(0), allow_preview_to_show_search_suggestions_(false) { } @@ -354,24 +356,36 @@ bool InstantController::Update(const AutocompleteMatch& match, // TODO(tonyg): This method only fires when the omnibox bounds change. It also // needs to fire when the preview bounds change (e.g.: open/close info bar). -void InstantController::SetOmniboxBounds(const gfx::Rect& bounds) { +void InstantController::SetPopupBounds(const gfx::Rect& bounds) { if (!extended_enabled_ && !instant_enabled_) return; - if (omnibox_bounds_ == bounds) + if (popup_bounds_ == bounds) return; - omnibox_bounds_ = bounds; - if (omnibox_bounds_.height() > last_omnibox_bounds_.height()) { + popup_bounds_ = bounds; + if (popup_bounds_.height() > last_popup_bounds_.height()) { update_bounds_timer_.Stop(); - SendBoundsToPage(); + SendPopupBoundsToPage(); } else if (!update_bounds_timer_.IsRunning()) { update_bounds_timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(kUpdateBoundsDelayMS), this, - &InstantController::SendBoundsToPage); + &InstantController::SendPopupBoundsToPage); } } +void InstantController::SetMarginSize(int start, int end) { + if (!extended_enabled_ || (start_margin_ == start && end_margin_ == end)) + return; + + start_margin_ = start; + end_margin_ = end; + if (loader_) + loader_->SetMarginSize(start_margin_, end_margin_); + if (instant_tab_) + instant_tab_->SetMarginSize(start_margin_, end_margin_); +} + void InstantController::HandleAutocompleteResults( const std::vector<AutocompleteProvider*>& providers) { if (!extended_enabled_) @@ -869,6 +883,7 @@ bool InstantController::ResetLoader(const TemplateURL* template_url, loader_->SetDisplayInstantResults(instant_enabled_); loader_->SearchModeChanged(search_mode_); loader_->KeyCaptureChanged(omnibox_focus_state_ == OMNIBOX_FOCUS_INVISIBLE); + loader_->SetMarginSize(start_margin_, end_margin_); } // Restart the stale loader timer. @@ -911,6 +926,7 @@ void InstantController::ResetInstantTab() { instant_tab_.reset(new InstantTab(this, active_tab)); instant_tab_->Init(); instant_tab_->SetDisplayInstantResults(instant_enabled_); + instant_tab_->SetMarginSize(start_margin_, end_margin_); } // Hide the |loader_| since we are now using |instant_tab_| instead. @@ -1027,14 +1043,14 @@ void InstantController::ShowLoader(InstantShownReason reason, CommitIfPossible(INSTANT_COMMIT_CLICKED_QUERY_SUGGESTION); } -void InstantController::SendBoundsToPage() { - if (last_omnibox_bounds_ == omnibox_bounds_ || !loader_ || +void InstantController::SendPopupBoundsToPage() { + if (last_popup_bounds_ == popup_bounds_ || !loader_ || loader_->is_pointer_down_from_activate()) return; - last_omnibox_bounds_ = omnibox_bounds_; + last_popup_bounds_ = popup_bounds_; gfx::Rect preview_bounds = browser_->GetInstantBounds(); - gfx::Rect intersection = gfx::IntersectRects(omnibox_bounds_, preview_bounds); + gfx::Rect intersection = gfx::IntersectRects(popup_bounds_, preview_bounds); // Translate into window coordinates. if (!intersection.IsEmpty()) { @@ -1051,7 +1067,7 @@ void InstantController::SendBoundsToPage() { DCHECK_LE(0, intersection.width()); DCHECK_LE(0, intersection.height()); - loader_->SetOmniboxBounds(intersection); + loader_->SetPopupBounds(intersection); } bool InstantController::GetInstantURL(const TemplateURL* template_url, diff --git a/chrome/browser/instant/instant_controller.h b/chrome/browser/instant/instant_controller.h index 0e93bd01..2e1940e 100644 --- a/chrome/browser/instant/instant_controller.h +++ b/chrome/browser/instant/instant_controller.h @@ -64,8 +64,11 @@ class InstantController { bool omnibox_popup_is_open, bool escape_pressed); - // Sets the bounds of the omnibox dropdown, in screen coordinates. - void SetOmniboxBounds(const gfx::Rect& bounds); + // Sets the bounds of the omnibox popup, in screen coordinates. + void SetPopupBounds(const gfx::Rect& bounds); + + // Sets the start and end margins of the omnibox text area. + void SetMarginSize(int start, int end); // Send autocomplete results from |providers| to the preview page. void HandleAutocompleteResults( @@ -205,8 +208,8 @@ class InstantController { int height, InstantSizeUnits units); - // Send the omnibox dropdown bounds to the page. - void SendBoundsToPage(); + // Send the omnibox popup bounds to the page. + void SendPopupBoundsToPage(); // If |template_url| is a valid TemplateURL for use with Instant, fills in // |instant_url| and returns true; returns false otherwise. @@ -262,13 +265,19 @@ class InstantController { // The search model mode for the active tab. chrome::search::Mode search_mode_; - // Current omnibox bounds. - gfx::Rect omnibox_bounds_; + // Current omnibox popup bounds. + gfx::Rect popup_bounds_; + + // Last popup bounds passed to the page. + gfx::Rect last_popup_bounds_; + + // Size of the start-edge omnibox text area margin. + int start_margin_; - // Last bounds passed to the page. - gfx::Rect last_omnibox_bounds_; + // Size of the end-edge omnibox text area margin. + int end_margin_; - // Timer used to update the bounds of the omnibox. + // Timer used to update the bounds of the omnibox popup. base::OneShotTimer<InstantController> update_bounds_timer_; // Timer used to ensure that the Instant page does not get too stale. diff --git a/chrome/browser/instant/instant_loader.cc b/chrome/browser/instant/instant_loader.cc index 906f750..9f9a85c 100644 --- a/chrome/browser/instant/instant_loader.cc +++ b/chrome/browser/instant/instant_loader.cc @@ -244,8 +244,12 @@ void InstantLoader::Cancel(const string16& text) { client_.Cancel(text); } -void InstantLoader::SetOmniboxBounds(const gfx::Rect& bounds) { - client_.SetOmniboxBounds(bounds); +void InstantLoader::SetPopupBounds(const gfx::Rect& bounds) { + client_.SetPopupBounds(bounds); +} + +void InstantLoader::SetMarginSize(int start, int end) { + client_.SetMarginSize(start, end); } void InstantLoader::SendAutocompleteResults( diff --git a/chrome/browser/instant/instant_loader.h b/chrome/browser/instant/instant_loader.h index 6694cd3..618a4fc 100644 --- a/chrome/browser/instant/instant_loader.h +++ b/chrome/browser/instant/instant_loader.h @@ -93,7 +93,8 @@ class InstantLoader : public InstantClient::Delegate, bool verbatim); void Submit(const string16& text); void Cancel(const string16& text); - void SetOmniboxBounds(const gfx::Rect& bounds); + void SetPopupBounds(const gfx::Rect& bounds); + void SetMarginSize(int start, int end); void SendAutocompleteResults( const std::vector<InstantAutocompleteResult>& results); void UpOrDownKeyPressed(int count); diff --git a/chrome/browser/instant/instant_tab.cc b/chrome/browser/instant/instant_tab.cc index f9166d3..33cfe62 100644 --- a/chrome/browser/instant/instant_tab.cc +++ b/chrome/browser/instant/instant_tab.cc @@ -46,6 +46,10 @@ void InstantTab::UpOrDownKeyPressed(int count) { client_.UpOrDownKeyPressed(count); } +void InstantTab::SetMarginSize(int start, int end) { + client_.SetMarginSize(start, end); +} + void InstantTab::SetSuggestions( const std::vector<InstantSuggestion>& suggestions) { InstantSupportDetermined(true); diff --git a/chrome/browser/instant/instant_tab.h b/chrome/browser/instant/instant_tab.h index 1e9dcaf..2e0321a 100644 --- a/chrome/browser/instant/instant_tab.h +++ b/chrome/browser/instant/instant_tab.h @@ -44,6 +44,7 @@ class InstantTab : public InstantClient::Delegate { const std::vector<InstantAutocompleteResult>& results); void SetDisplayInstantResults(bool display_instant_results); void UpOrDownKeyPressed(int count); + void SetMarginSize(int start, int end); private: // Overridden from InstantClient::Delegate: diff --git a/chrome/browser/ui/browser_instant_controller.cc b/chrome/browser/ui/browser_instant_controller.cc index f5aee48..ba89cdf 100644 --- a/chrome/browser/ui/browser_instant_controller.cc +++ b/chrome/browser/ui/browser_instant_controller.cc @@ -171,6 +171,10 @@ void BrowserInstantController::OpenURLInCurrentTab( false)); } +void BrowserInstantController::SetMarginSize(int start, int end) { + instant_.SetMarginSize(start, end); +} + void BrowserInstantController::ResetInstant() { instant_.SetInstantEnabled(IsInstantEnabled(browser_->profile())); } diff --git a/chrome/browser/ui/browser_instant_controller.h b/chrome/browser/ui/browser_instant_controller.h index e66ff5f..401cfbd 100644 --- a/chrome/browser/ui/browser_instant_controller.h +++ b/chrome/browser/ui/browser_instant_controller.h @@ -87,6 +87,9 @@ class BrowserInstantController : public content::NotificationObserver, // Invoked by the InstantController when it wants to open a URL. void OpenURLInCurrentTab(const GURL& url, content::PageTransition transition); + // Sets the start and end margins of the omnibox text area. + void SetMarginSize(int start, int end); + private: // Sets the value of |instant_| based on value from profile. Invoked // on pref change. diff --git a/chrome/browser/ui/cocoa/omnibox/omnibox_view_mac.mm b/chrome/browser/ui/cocoa/omnibox/omnibox_view_mac.mm index 40c571e..3f7e7b9 100644 --- a/chrome/browser/ui/cocoa/omnibox/omnibox_view_mac.mm +++ b/chrome/browser/ui/cocoa/omnibox/omnibox_view_mac.mm @@ -930,7 +930,7 @@ void OmniboxViewMac::OnFrameChanged() { // things even cheaper by refactoring between the popup-placement // code and the matrix-population code. popup_view_->UpdatePopupAppearance(); - model()->PopupBoundsChangedTo(popup_view_->GetTargetBounds()); + model()->OnPopupBoundsChanged(popup_view_->GetTargetBounds()); // Give controller a chance to rearrange decorations. model()->OnChanged(); diff --git a/chrome/browser/ui/omnibox/omnibox_edit_model.cc b/chrome/browser/ui/omnibox/omnibox_edit_model.cc index 507cf65..b1e489d 100644 --- a/chrome/browser/ui/omnibox/omnibox_edit_model.cc +++ b/chrome/browser/ui/omnibox/omnibox_edit_model.cc @@ -1027,10 +1027,10 @@ bool OmniboxEditModel::OnAfterPossibleChange(const string16& old_text, MaybeAcceptKeywordBySpace(user_text_)); } -void OmniboxEditModel::PopupBoundsChangedTo(const gfx::Rect& bounds) { +void OmniboxEditModel::OnPopupBoundsChanged(const gfx::Rect& bounds) { InstantController* instant = controller_->GetInstant(); if (instant) - instant->SetOmniboxBounds(bounds); + instant->SetPopupBounds(bounds); } void OmniboxEditModel::OnResultChanged(bool default_match_changed) { @@ -1067,14 +1067,14 @@ void OmniboxEditModel::OnResultChanged(bool default_match_changed) { } if (popup_->IsOpen()) { - PopupBoundsChangedTo(popup_->view()->GetTargetBounds()); + OnPopupBoundsChanged(popup_->view()->GetTargetBounds()); } else if (was_open) { // Accepts the temporary text as the user text, because it makes little // sense to have temporary text when the popup is closed. InternalSetUserText(UserTextFromDisplayText(view_->GetText())); has_temporary_text_ = false; is_temporary_text_set_by_instant_ = false; - PopupBoundsChangedTo(gfx::Rect()); + OnPopupBoundsChanged(gfx::Rect()); NotifySearchTabHelper(); } diff --git a/chrome/browser/ui/omnibox/omnibox_edit_model.h b/chrome/browser/ui/omnibox/omnibox_edit_model.h index 7952ef7..7524dc7 100644 --- a/chrome/browser/ui/omnibox/omnibox_edit_model.h +++ b/chrome/browser/ui/omnibox/omnibox_edit_model.h @@ -318,8 +318,9 @@ class OmniboxEditModel : public AutocompleteControllerDelegate { bool just_deleted_text, bool allow_keyword_ui_change); - // Invoked when the popup is going to change its bounds to |bounds|. - void PopupBoundsChangedTo(const gfx::Rect& bounds); + // Invoked when the popup has changed its bounds to |bounds|. |bounds| here + // is in screen coordinates. + void OnPopupBoundsChanged(const gfx::Rect& bounds); private: enum PasteState { diff --git a/chrome/browser/ui/views/location_bar/location_bar_view.cc b/chrome/browser/ui/views/location_bar/location_bar_view.cc index 860c75b..9714e3c 100644 --- a/chrome/browser/ui/views/location_bar/location_bar_view.cc +++ b/chrome/browser/ui/views/location_bar/location_bar_view.cc @@ -8,6 +8,7 @@ #include <map> #include "base/command_line.h" +#include "base/i18n/rtl.h" #include "base/stl_util.h" #include "base/utf_string_conversions.h" #include "chrome/app/chrome_command_ids.h" @@ -26,6 +27,7 @@ #include "chrome/browser/search_engines/template_url_service_factory.h" #include "chrome/browser/ui/browser.h" #include "chrome/browser/ui/browser_finder.h" +#include "chrome/browser/ui/browser_instant_controller.h" #include "chrome/browser/ui/browser_tabstrip.h" #include "chrome/browser/ui/omnibox/location_bar_util.h" #include "chrome/browser/ui/omnibox/omnibox_popup_model.h" @@ -1368,6 +1370,19 @@ bool LocationBarView::HasFocus() const { return location_entry_->model()->has_focus(); } +void LocationBarView::OnBoundsChanged(const gfx::Rect& previous_bounds) { + if (browser_ && browser_->instant_controller() && parent()) { + // Pass the side margins of the location bar to the Instant Controller. + const gfx::Rect bounds = GetBoundsInScreen(); + const gfx::Rect parent_bounds = parent()->GetBoundsInScreen(); + int start = bounds.x() - parent_bounds.x(); + int end = parent_bounds.right() - bounds.right(); + if (base::i18n::IsRTL()) + std::swap(start, end); + browser_->instant_controller()->SetMarginSize(start, end); + } +} + void LocationBarView::WriteDragDataForView(views::View* sender, const gfx::Point& press_pt, OSExchangeData* data) { diff --git a/chrome/browser/ui/views/location_bar/location_bar_view.h b/chrome/browser/ui/views/location_bar/location_bar_view.h index a42bbd0..abccf47 100644 --- a/chrome/browser/ui/views/location_bar/location_bar_view.h +++ b/chrome/browser/ui/views/location_bar/location_bar_view.h @@ -271,6 +271,7 @@ class LocationBarView : public LocationBar, const ui::KeyEvent& event) OVERRIDE; virtual void GetAccessibleState(ui::AccessibleViewState* state) OVERRIDE; virtual bool HasFocus() const OVERRIDE; + virtual void OnBoundsChanged(const gfx::Rect& previous_bounds) OVERRIDE; // Overridden from views::DragController: virtual void WriteDragDataForView(View* sender, diff --git a/chrome/common/render_messages.h b/chrome/common/render_messages.h index 08d08519..dbafae5 100644 --- a/chrome/common/render_messages.h +++ b/chrome/common/render_messages.h @@ -305,8 +305,12 @@ IPC_MESSAGE_ROUTED1(ChromeViewMsg_SearchBoxSubmit, IPC_MESSAGE_ROUTED1(ChromeViewMsg_SearchBoxCancel, string16 /* value */) -IPC_MESSAGE_ROUTED1(ChromeViewMsg_SearchBoxResize, - gfx::Rect /* search_box_bounds */) +IPC_MESSAGE_ROUTED1(ChromeViewMsg_SearchBoxPopupResize, + gfx::Rect /* bounds */) + +IPC_MESSAGE_ROUTED2(ChromeViewMsg_SearchBoxMarginChange, + int /* start */, + int /* end */) IPC_MESSAGE_ROUTED0(ChromeViewMsg_DetermineIfPageSupportsInstant) diff --git a/chrome/renderer/resources/extensions/searchbox_api.js b/chrome/renderer/resources/extensions/searchbox_api.js index 72925b8..b037d43 100644 --- a/chrome/renderer/resources/extensions/searchbox_api.js +++ b/chrome/renderer/resources/extensions/searchbox_api.js @@ -37,6 +37,9 @@ if (!chrome.searchBox) { native function GetY(); native function GetWidth(); native function GetHeight(); + native function GetStartMargin(); + native function GetEndMargin(); + native function GetRightToLeft(); native function GetAutocompleteResults(); native function GetContext(); native function GetDisplayInstantResults(); @@ -191,6 +194,9 @@ if (!chrome.searchBox) { this.__defineGetter__('y', GetY); this.__defineGetter__('width', GetWidth); this.__defineGetter__('height', GetHeight); + this.__defineGetter__('startMargin', GetStartMargin); + this.__defineGetter__('endMargin', GetEndMargin); + this.__defineGetter__('rtl', GetRightToLeft); this.__defineGetter__('nativeSuggestions', GetAutocompleteResultsWrapper); this.__defineGetter__('isKeyCaptureEnabled', IsKeyCaptureEnabled); this.__defineGetter__('context', GetContext); @@ -237,5 +243,6 @@ if (!chrome.searchBox) { this.onkeypress = null; this.onkeycapturechange = null; this.oncontextchange = null; + this.onmarginchange = null; }; } diff --git a/chrome/renderer/searchbox/searchbox.cc b/chrome/renderer/searchbox/searchbox.cc index bb6ab1d..d006e9f 100644 --- a/chrome/renderer/searchbox/searchbox.cc +++ b/chrome/renderer/searchbox/searchbox.cc @@ -16,6 +16,8 @@ SearchBox::SearchBox(content::RenderView* render_view) selection_start_(0), selection_end_(0), results_base_(0), + start_margin_(0), + end_margin_(0), last_results_base_(0), is_key_capture_enabled_(false), theme_area_height_(0), @@ -63,20 +65,20 @@ void SearchBox::NavigateToURL(const GURL& url, url, transition)); } -gfx::Rect SearchBox::GetRect() { - // Need to adjust for scale. - if (rect_.IsEmpty()) - return rect_; - WebKit::WebView* web_view = render_view()->GetWebView(); - if (!web_view) - return rect_; - double zoom = WebKit::WebView::zoomLevelToZoomFactor(web_view->zoomLevel()); - if (zoom == 0) - return rect_; - return gfx::Rect(static_cast<int>(static_cast<float>(rect_.x()) / zoom), - static_cast<int>(static_cast<float>(rect_.y()) / zoom), - static_cast<int>(static_cast<float>(rect_.width()) / zoom), - static_cast<int>(static_cast<float>(rect_.height()) / zoom)); +int SearchBox::GetStartMargin() const { + return static_cast<int>(start_margin_ / GetZoom()); +} + +int SearchBox::GetEndMargin() const { + return static_cast<int>(end_margin_ / GetZoom()); +} + +gfx::Rect SearchBox::GetPopupBounds() const { + double zoom = GetZoom(); + return gfx::Rect(static_cast<int>(popup_bounds_.x() / zoom), + static_cast<int>(popup_bounds_.y() / zoom), + static_cast<int>(popup_bounds_.width() / zoom), + static_cast<int>(popup_bounds_.height() / zoom)); } const std::vector<InstantAutocompleteResult>& @@ -111,7 +113,8 @@ bool SearchBox::OnMessageReceived(const IPC::Message& message) { IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxChange, OnChange) IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxSubmit, OnSubmit) IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxCancel, OnCancel) - IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxResize, OnResize) + IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxPopupResize, OnPopupResize) + IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxMarginChange, OnMarginChange) IPC_MESSAGE_HANDLER(ChromeViewMsg_DetermineIfPageSupportsInstant, OnDetermineIfPageSupportsInstant) IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxAutocompleteResults, @@ -176,15 +179,24 @@ void SearchBox::OnCancel(const string16& query) { Reset(); } -void SearchBox::OnResize(const gfx::Rect& bounds) { - rect_ = bounds; +void SearchBox::OnPopupResize(const gfx::Rect& bounds) { + popup_bounds_ = bounds; if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { - DVLOG(1) << render_view() << " OnResize"; + DVLOG(1) << render_view() << " OnPopupResize"; extensions_v8::SearchBoxExtension::DispatchResize( render_view()->GetWebView()->mainFrame()); } } +void SearchBox::OnMarginChange(int start, int end) { + start_margin_ = start; + end_margin_ = end; + if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { + extensions_v8::SearchBoxExtension::DispatchMarginChange( + render_view()->GetWebView()->mainFrame()); + } +} + void SearchBox::OnDetermineIfPageSupportsInstant() { if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { bool result = extensions_v8::SearchBoxExtension::PageSupportsInstant( @@ -253,13 +265,25 @@ void SearchBox::OnThemeAreaHeightChanged(int height) { } } +double SearchBox::GetZoom() const { + WebKit::WebView* web_view = render_view()->GetWebView(); + if (web_view) { + double zoom = WebKit::WebView::zoomLevelToZoomFactor(web_view->zoomLevel()); + if (zoom != 0) + return zoom; + } + return 1.0; +} + void SearchBox::Reset() { query_.clear(); verbatim_ = false; selection_start_ = 0; selection_end_ = 0; results_base_ = 0; - rect_ = gfx::Rect(); + popup_bounds_ = gfx::Rect(); + start_margin_ = 0; + end_margin_ = 0; autocomplete_results_.clear(); is_key_capture_enabled_ = false; mode_ = chrome::search::Mode(); diff --git a/chrome/renderer/searchbox/searchbox.h b/chrome/renderer/searchbox/searchbox.h index 36f1932..86cb953f 100644 --- a/chrome/renderer/searchbox/searchbox.h +++ b/chrome/renderer/searchbox/searchbox.h @@ -52,7 +52,14 @@ class SearchBox : public content::RenderViewObserver, bool is_key_capture_enabled() const { return is_key_capture_enabled_; } bool display_instant_results() const { return display_instant_results_; } - gfx::Rect GetRect(); + // These functions return the start/end margins of the page text area, + // adjusted for the page zoom. + int GetStartMargin() const; + int GetEndMargin() const; + + // Returns the bounds of the omnibox popup in screen coordinates. + gfx::Rect GetPopupBounds() const; + const std::vector<InstantAutocompleteResult>& GetAutocompleteResults(); // Searchbox retains ownership of this object. const InstantAutocompleteResult* @@ -71,7 +78,8 @@ class SearchBox : public content::RenderViewObserver, size_t selection_end); void OnSubmit(const string16& query); void OnCancel(const string16& query); - void OnResize(const gfx::Rect& bounds); + void OnPopupResize(const gfx::Rect& bounds); + void OnMarginChange(int start, int end); void OnDetermineIfPageSupportsInstant(); void OnAutocompleteResults( const std::vector<InstantAutocompleteResult>& results); @@ -82,6 +90,9 @@ class SearchBox : public content::RenderViewObserver, void OnThemeChanged(const ThemeBackgroundInfo& theme_info); void OnThemeAreaHeightChanged(int height); + // Returns the current zoom factor of the render view or 1 on failure. + double GetZoom() const; + // Sets the searchbox values to their initial value. void Reset(); @@ -90,7 +101,9 @@ class SearchBox : public content::RenderViewObserver, size_t selection_start_; size_t selection_end_; size_t results_base_; - gfx::Rect rect_; + int start_margin_; + int end_margin_; + gfx::Rect popup_bounds_; std::vector<InstantAutocompleteResult> autocomplete_results_; size_t last_results_base_; std::vector<InstantAutocompleteResult> last_autocomplete_results_; diff --git a/chrome/renderer/searchbox/searchbox_extension.cc b/chrome/renderer/searchbox/searchbox_extension.cc index 3fda904..0df4323 100644 --- a/chrome/renderer/searchbox/searchbox_extension.cc +++ b/chrome/renderer/searchbox/searchbox_extension.cc @@ -4,6 +4,7 @@ #include "chrome/renderer/searchbox/searchbox_extension.h" +#include "base/i18n/rtl.h" #include "base/string_number_conversions.h" #include "base/stringprintf.h" #include "chrome/common/extensions/extension.h" @@ -178,6 +179,15 @@ static const char kDispatchThemeAreaHeightChangeEventScript[] = " true;" "}"; +static const char kDispatchMarginChangeEventScript[] = + "if (window.chrome &&" + " window.chrome.searchBox &&" + " window.chrome.searchBox.onmarginchange &&" + " typeof window.chrome.searchBox.onmarginchange == 'function') {" + " window.chrome.searchBox.onmarginchange();" + " true;" + "}"; + // ---------------------------------------------------------------------------- class SearchBoxExtensionWrapper : public v8::Extension { @@ -220,6 +230,17 @@ class SearchBoxExtensionWrapper : public v8::Extension { // Gets the height of the region of the search box that overlaps the window. static v8::Handle<v8::Value> GetHeight(const v8::Arguments& args); + // Gets the width of the margin from the start-edge of the page to the start + // of the suggestions dropdown. + static v8::Handle<v8::Value> GetStartMargin(const v8::Arguments& args); + + // Gets the width of the margin from the end-edge of the page to the end of + // the suggestions dropdown. + static v8::Handle<v8::Value> GetEndMargin(const v8::Arguments& args); + + // Returns true if the Searchbox itself is oriented right-to-left. + static v8::Handle<v8::Value> GetRightToLeft(const v8::Arguments& args); + // Gets the autocomplete results from search box. static v8::Handle<v8::Value> GetAutocompleteResults( const v8::Arguments& args); @@ -311,6 +332,12 @@ v8::Handle<v8::FunctionTemplate> SearchBoxExtensionWrapper::GetNativeFunction( return v8::FunctionTemplate::New(GetWidth); if (name->Equals(v8::String::New("GetHeight"))) return v8::FunctionTemplate::New(GetHeight); + if (name->Equals(v8::String::New("GetStartMargin"))) + return v8::FunctionTemplate::New(GetStartMargin); + if (name->Equals(v8::String::New("GetEndMargin"))) + return v8::FunctionTemplate::New(GetEndMargin); + if (name->Equals(v8::String::New("GetRightToLeft"))) + return v8::FunctionTemplate::New(GetRightToLeft); if (name->Equals(v8::String::New("GetAutocompleteResults"))) return v8::FunctionTemplate::New(GetAutocompleteResults); if (name->Equals(v8::String::New("GetContext"))) @@ -405,7 +432,7 @@ v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetX( content::RenderView* render_view = GetRenderView(); if (!render_view) return v8::Undefined(); - return v8::Int32::New(SearchBox::Get(render_view)->GetRect().x()); + return v8::Int32::New(SearchBox::Get(render_view)->GetPopupBounds().x()); } // static @@ -414,7 +441,7 @@ v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetY( content::RenderView* render_view = GetRenderView(); if (!render_view) return v8::Undefined(); - return v8::Int32::New(SearchBox::Get(render_view)->GetRect().y()); + return v8::Int32::New(SearchBox::Get(render_view)->GetPopupBounds().y()); } // static @@ -423,7 +450,7 @@ v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetWidth( content::RenderView* render_view = GetRenderView(); if (!render_view) return v8::Undefined(); - return v8::Int32::New(SearchBox::Get(render_view)->GetRect().width()); + return v8::Int32::New(SearchBox::Get(render_view)->GetPopupBounds().width()); } // static @@ -432,7 +459,29 @@ v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetHeight( content::RenderView* render_view = GetRenderView(); if (!render_view) return v8::Undefined(); - return v8::Int32::New(SearchBox::Get(render_view)->GetRect().height()); + return v8::Int32::New(SearchBox::Get(render_view)->GetPopupBounds().height()); +} + +// static +v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetStartMargin( + const v8::Arguments& args) { + content::RenderView* render_view = GetRenderView(); + if (!render_view) return v8::Undefined(); + return v8::Int32::New(SearchBox::Get(render_view)->GetStartMargin()); +} + +// static +v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetEndMargin( + const v8::Arguments& args) { + content::RenderView* render_view = GetRenderView(); + if (!render_view) return v8::Undefined(); + return v8::Int32::New(SearchBox::Get(render_view)->GetEndMargin()); +} + +// static +v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetRightToLeft( + const v8::Arguments& args) { + return v8::Boolean::New(base::i18n::IsRTL()); } // static @@ -896,6 +945,11 @@ void SearchBoxExtension::DispatchThemeChange(WebKit::WebFrame* frame) { } // static +void SearchBoxExtension::DispatchMarginChange(WebKit::WebFrame* frame) { + Dispatch(frame, kDispatchMarginChangeEventScript); +} + +// static void SearchBoxExtension::DispatchThemeAreaHeightChange( WebKit::WebFrame* frame) { Dispatch(frame, kDispatchThemeAreaHeightChangeEventScript); diff --git a/chrome/renderer/searchbox/searchbox_extension.h b/chrome/renderer/searchbox/searchbox_extension.h index 51fb15b..964ba55 100644 --- a/chrome/renderer/searchbox/searchbox_extension.h +++ b/chrome/renderer/searchbox/searchbox_extension.h @@ -42,6 +42,7 @@ class SearchBoxExtension { static void DispatchContextChange(WebKit::WebFrame* frame); static void DispatchThemeChange(WebKit::WebFrame* frame); static void DispatchThemeAreaHeightChange(WebKit::WebFrame* frame); + static void DispatchMarginChange(WebKit::WebFrame* frame); private: DISALLOW_IMPLICIT_CONSTRUCTORS(SearchBoxExtension); diff --git a/content/public/renderer/render_view_observer.cc b/content/public/renderer/render_view_observer.cc index 371f7ff..c0271c1 100644 --- a/content/public/renderer/render_view_observer.cc +++ b/content/public/renderer/render_view_observer.cc @@ -44,7 +44,7 @@ bool RenderViewObserver::Send(IPC::Message* message) { return false; } -RenderView* RenderViewObserver::render_view() { +RenderView* RenderViewObserver::render_view() const { return render_view_; } diff --git a/content/public/renderer/render_view_observer.h b/content/public/renderer/render_view_observer.h index b99dc1d..b1addd9 100644 --- a/content/public/renderer/render_view_observer.h +++ b/content/public/renderer/render_view_observer.h @@ -104,7 +104,7 @@ class CONTENT_EXPORT RenderViewObserver : public IPC::Listener, // IPC::Sender implementation. virtual bool Send(IPC::Message* message) OVERRIDE; - RenderView* render_view(); + RenderView* render_view() const; int routing_id() { return routing_id_; } private: |