diff options
Diffstat (limited to 'chrome/browser/views/find_bar_host.cc')
-rw-r--r-- | chrome/browser/views/find_bar_host.cc | 231 |
1 files changed, 50 insertions, 181 deletions
diff --git a/chrome/browser/views/find_bar_host.cc b/chrome/browser/views/find_bar_host.cc index 1f53cd2..9b2cda5 100644 --- a/chrome/browser/views/find_bar_host.cc +++ b/chrome/browser/views/find_bar_host.cc @@ -21,9 +21,7 @@ #include "views/focus/view_storage.h" #include "views/controls/scrollbar/native_scroll_bar.h" #include "views/widget/root_view.h" - -// static -bool FindBarHost::disable_animations_during_testing_ = false; +#include "views/widget/widget.h" using gfx::Path; @@ -40,89 +38,46 @@ FindBar* CreateFindBar(BrowserView* browser_view) { // FindBarHost, public: FindBarHost::FindBarHost(BrowserView* browser_view) - : browser_view_(browser_view), - find_dialog_animation_offset_(0), - esc_accel_target_registered_(false), + : DropdownBarHost(browser_view), find_bar_controller_(NULL) { - view_ = new FindBarView(this); - - // Initialize the host. - host_.reset(CreateHost()); - host_->Init(GetNativeView(browser_view), gfx::Rect()); - host_->SetContentsView(view_); - - // Start listening to focus changes, so we can register and unregister our - // own handler for Escape. - focus_manager_ = - views::FocusManager::GetFocusManagerForNativeView(host_->GetNativeView()); - if (focus_manager_) { - focus_manager_->AddFocusChangeListener(this); - } else { - // In some cases (see bug http://crbug.com/17056) it seems we may not have - // a focus manager. Please reopen the bug if you hit this. - NOTREACHED(); - } - - // Start the process of animating the opening of the window. - animation_.reset(new SlideAnimation(this)); + Init(new FindBarView(this)); } FindBarHost::~FindBarHost() { - focus_manager_->RemoveFocusChangeListener(this); - focus_tracker_.reset(NULL); } void FindBarHost::Show() { - // Stores the currently focused view, and tracks focus changes so that we can - // restore focus when the find box is closed. - focus_tracker_.reset(new views::ExternalFocusTracker(view_, focus_manager_)); - - if (disable_animations_during_testing_) { - animation_->Reset(1); - MoveWindowIfNecessary(gfx::Rect(), true); - } else { - animation_->Reset(); - animation_->Show(); - } + DropdownBarHost::Show(); } void FindBarHost::SetFocusAndSelection() { - view_->SetFocusAndSelection(); -} - -bool FindBarHost::IsAnimating() { - return animation_->IsAnimating(); + DropdownBarHost::SetFocusAndSelection(); } void FindBarHost::Hide(bool animate) { - if (animate && !disable_animations_during_testing_) { - animation_->Reset(1.0); - animation_->Hide(); - } else { - host_->Hide(); - } + DropdownBarHost::Hide(animate); } void FindBarHost::ClearResults(const FindNotificationDetails& results) { - view_->UpdateForResult(results, string16()); + find_bar_view()->UpdateForResult(results, string16()); } void FindBarHost::StopAnimation() { - animation_->End(); + DropdownBarHost::StopAnimation(); } void FindBarHost::SetFindText(const string16& find_text) { - view_->SetFindText(find_text); + find_bar_view()->SetFindText(find_text); } bool FindBarHost::IsFindBarVisible() { - return host_->IsVisible(); + return DropdownBarHost::IsVisible(); } void FindBarHost::MoveWindowIfNecessary(const gfx::Rect& selection_rect, - bool no_redraw) { + bool no_redraw) { // We only move the window if one is active for the current TabContents. If we - // don't check this, then SetDialogPosition below will end up making the Find + // don't check this, then SetWidgetPosition below will end up making the Find // Bar visible. if (!find_bar_controller_->tab_contents() || !find_bar_controller_->tab_contents()->find_ui_active()) { @@ -133,37 +88,7 @@ void FindBarHost::MoveWindowIfNecessary(const gfx::Rect& selection_rect, SetDialogPosition(new_pos, no_redraw); // May need to redraw our frame to accommodate bookmark bar styles. - view_->SchedulePaint(); -} - -bool FindBarHost::IsVisible() { - return host_->IsVisible(); -} - -//////////////////////////////////////////////////////////////////////////////// -// FindBarHost, views::FocusChangeListener implementation: - -void FindBarHost::FocusWillChange(views::View* focused_before, - views::View* focused_now) { - // First we need to determine if one or both of the views passed in are child - // views of our view. - bool our_view_before = focused_before && view_->IsParentOf(focused_before); - bool our_view_now = focused_now && view_->IsParentOf(focused_now); - - // When both our_view_before and our_view_now are false, it means focus is - // changing hands elsewhere in the application (and we shouldn't do anything). - // Similarly, when both are true, focus is changing hands within the Find - // window (and again, we should not do anything). We therefore only need to - // look at when we gain initial focus and when we loose it. - if (!our_view_before && our_view_now) { - // We are gaining focus from outside the Find window so we must register - // a handler for Escape. - RegisterEscAccelerator(); - } else if (our_view_before && !our_view_now) { - // We are losing focus to something outside our window so we restore the - // original handler for Escape. - UnregisterEscAccelerator(); - } + view()->SchedulePaint(); } //////////////////////////////////////////////////////////////////////////////// @@ -182,53 +107,13 @@ bool FindBarHost::AcceleratorPressed(const views::Accelerator& accelerator) { } //////////////////////////////////////////////////////////////////////////////// -// FindBarHost, AnimationDelegate implementation: - -void FindBarHost::AnimationProgressed(const Animation* animation) { - // First, we calculate how many pixels to slide the window. - gfx::Size pref_size = view_->GetPreferredSize(); - find_dialog_animation_offset_ = - static_cast<int>((1.0 - animation_->GetCurrentValue()) * - pref_size.height()); - - // This call makes sure it appears in the right location, the size and shape - // is correct and that it slides in the right direction. - gfx::Rect find_dlg_rect = GetDialogPosition(gfx::Rect()); - SetDialogPosition(find_dlg_rect, false); - - // Let the view know if we are animating, and at which offset to draw the - // edges. - view_->animation_offset(find_dialog_animation_offset_); - view_->SchedulePaint(); -} - -void FindBarHost::AnimationEnded(const Animation* animation) { - // Place the find bar in its fully opened state. - find_dialog_animation_offset_ = 0; - - if (!animation_->IsShowing()) { - // Animation has finished closing. - host_->Hide(); - } else { - // Animation has finished opening. - } -} - -void FindBarHost::GetThemePosition(gfx::Rect* bounds) { - *bounds = GetDialogPosition(gfx::Rect()); - gfx::Rect toolbar_bounds = browser_view_->GetToolbarBounds(); - gfx::Rect tab_strip_bounds = browser_view_->GetTabStripBounds(); - bounds->Offset(-toolbar_bounds.x(), -tab_strip_bounds.y()); -} - -//////////////////////////////////////////////////////////////////////////////// // FindBarTesting implementation: bool FindBarHost::GetFindBarWindowInfo(gfx::Point* position, bool* fully_visible) { if (!find_bar_controller_ || #if defined(OS_WIN) - !::IsWindow(host_->GetNativeView())) { + !::IsWindow(host()->GetNativeView())) { #else false) { // TODO(sky): figure out linux side. @@ -241,21 +126,14 @@ bool FindBarHost::GetFindBarWindowInfo(gfx::Point* position, } gfx::Rect window_rect; - host_->GetBounds(&window_rect, true); + host()->GetBounds(&window_rect, true); if (position) *position = window_rect.origin(); if (fully_visible) - *fully_visible = host_->IsVisible() && !IsAnimating(); + *fully_visible = host()->IsVisible() && !IsAnimating(); return true; } -void FindBarHost::GetDialogBounds(gfx::Rect* bounds) { - DCHECK(bounds); - // The BrowserView does Layout for the components that we care about - // positioning relative to, so we ask it to tell us where we should go. - *bounds = browser_view_->GetFindBarBoundingBox(); -} - void FindBarHost::UpdateWindowEdges(const gfx::Rect& new_pos) { // |w| is used to make it easier to create the part of the polygon that curves // the right side of the Find window. It essentially keeps track of the @@ -266,7 +144,7 @@ void FindBarHost::UpdateWindowEdges(const gfx::Rect& new_pos) { // rightmost background image of the view. // This polygon array represents the outline of the background image for the - // dialog. Basically, it encompasses only the visible pixels of the + // window. Basically, it encompasses only the visible pixels of the // concatenated find_dlg_LMR_bg images (where LMR = [left | middle | right]). static const Path::Point polygon[] = { {0, 0}, {0, 1}, {2, 3}, {2, 29}, {4, 31}, @@ -287,18 +165,18 @@ void FindBarHost::UpdateWindowEdges(const gfx::Rect& new_pos) { ScopedRegion region(path.CreateNativeRegion()); // Are we animating? - if (find_dialog_animation_offset_ > 0) { + if (animation_offset() > 0) { // The animation happens in two steps: First, we clip the window and then in - // GetDialogPosition we offset the window position so that it still looks + // GetWidgetPosition we offset the window position so that it still looks // attached to the toolbar as it grows. We clip the window by creating a // rectangle region (that gradually increases as the animation progresses) // and find the intersection between the two regions using CombineRgn. // |y| shrinks as the animation progresses from the height of the view down // to 0 (and reverses when closing). - int y = find_dialog_animation_offset_; + int y = animation_offset(); // |y| shrinking means the animation (visible) region gets larger. In other - // words: the rectangle grows upward (when the dialog is opening). + // words: the rectangle grows upward (when the widget is opening). Path animation_path; SkRect animation_rect = { SkIntToScalar(0), SkIntToScalar(y), SkIntToScalar(max_x), SkIntToScalar(max_y) }; @@ -328,18 +206,18 @@ void FindBarHost::UpdateWindowEdges(const gfx::Rect& new_pos) { // Now see if we need to truncate the region because parts of it obscures // the main window border. - gfx::Rect dialog_bounds; - GetDialogBounds(&dialog_bounds); + gfx::Rect widget_bounds; + GetWidgetBounds(&widget_bounds); // Calculate how much our current position overlaps our boundaries. If we - // overlap, it means we have too little space to draw the whole dialog and - // we allow overwriting the scrollbar before we start truncating our dialog. + // overlap, it means we have too little space to draw the whole widget and + // we allow overwriting the scrollbar before we start truncating our widget. // // TODO(brettw) this constant is evil. This is the amount of room we've added // to the window size, when we set the region, it can change the size. static const int kAddedWidth = 7; int difference = (new_pos.right() - kAddedWidth) - - dialog_bounds.width() - + widget_bounds.width() - views::NativeScrollBar::GetVerticalScrollBarWidth() + 1; if (difference > 0) { @@ -363,25 +241,25 @@ void FindBarHost::UpdateWindowEdges(const gfx::Rect& new_pos) { } // Window takes ownership of the region. - host_->SetShape(region.release()); + host()->SetShape(region.release()); } gfx::Rect FindBarHost::GetDialogPosition(gfx::Rect avoid_overlapping_rect) { // Find the area we have to work with (after accounting for scrollbars, etc). - gfx::Rect dialog_bounds; - GetDialogBounds(&dialog_bounds); - if (dialog_bounds.IsEmpty()) + gfx::Rect widget_bounds; + GetWidgetBounds(&widget_bounds); + if (widget_bounds.IsEmpty()) return gfx::Rect(); // Ask the view how large an area it needs to draw on. - gfx::Size prefsize = view_->GetPreferredSize(); + gfx::Size prefsize = view()->GetPreferredSize(); - // Place the view in the top right corner of the dialog boundaries (top left + // Place the view in the top right corner of the widget boundaries (top left // for RTL languages). gfx::Rect view_location; - int x = view_->UILayoutIsRightToLeft() ? - dialog_bounds.x() : dialog_bounds.width() - prefsize.width(); - int y = dialog_bounds.y(); + int x = view()->UILayoutIsRightToLeft() ? + widget_bounds.x() : widget_bounds.width() - prefsize.width(); + int y = widget_bounds.y(); view_location.SetRect(x, y, prefsize.width(), prefsize.height()); // When we get Find results back, we specify a selection rect, which we @@ -389,18 +267,18 @@ gfx::Rect FindBarHost::GetDialogPosition(gfx::Rect avoid_overlapping_rect) { // selection rect (if one was provided). if (!avoid_overlapping_rect.IsEmpty()) { // For comparison (with the Intersects function below) we need to account - // for the fact that we draw the Find dialog relative to the window, + // for the fact that we draw the Find widget relative to the Chrome frame, // whereas the selection rect is relative to the page. - GetDialogPositionNative(&avoid_overlapping_rect); + GetWidgetPositionNative(&avoid_overlapping_rect); } gfx::Rect new_pos = FindBarController::GetLocationForFindbarView( - view_location, dialog_bounds, avoid_overlapping_rect); + view_location, widget_bounds, avoid_overlapping_rect); // While we are animating, the Find window will grow bottoms up so we need to - // re-position the dialog so that it appears to grow out of the toolbar. - if (find_dialog_animation_offset_ > 0) - new_pos.Offset(0, std::min(0, -find_dialog_animation_offset_)); + // re-position the widget so that it appears to grow out of the toolbar. + if (animation_offset() > 0) + new_pos.Offset(0, std::min(0, -animation_offset())); return new_pos; } @@ -414,15 +292,15 @@ void FindBarHost::SetDialogPosition(const gfx::Rect& new_pos, bool no_redraw) { // of it it doesn't look like the window crumbles into the toolbar. UpdateWindowEdges(new_pos); - SetDialogPositionNative(new_pos, no_redraw); + SetWidgetPositionNative(new_pos, no_redraw); } void FindBarHost::RestoreSavedFocus() { - if (focus_tracker_.get() == NULL) { + if (focus_tracker() == NULL) { // TODO(brettw) Focus() should be on TabContentsView. find_bar_controller_->tab_contents()->Focus(); } else { - focus_tracker_->FocusLastFocusedExternalView(); + focus_tracker()->FocusLastFocusedExternalView(); } } @@ -432,7 +310,7 @@ FindBarTesting* FindBarHost::GetFindBarTesting() { void FindBarHost::UpdateUIForFindResult(const FindNotificationDetails& result, const string16& find_text) { - view_->UpdateForResult(result, find_text); + find_bar_view()->UpdateForResult(result, find_text); // We now need to check if the window is obscuring the search results. if (!result.selection_rect().IsEmpty()) @@ -441,22 +319,9 @@ void FindBarHost::UpdateUIForFindResult(const FindNotificationDetails& result, // Once we find a match we no longer want to keep track of what had // focus. EndFindSession will then set the focus to the page content. if (result.number_of_matches() > 0) - focus_tracker_.reset(NULL); -} - -void FindBarHost::RegisterEscAccelerator() { - DCHECK(!esc_accel_target_registered_); - views::Accelerator escape(base::VKEY_ESCAPE, false, false, false); - focus_manager_->RegisterAccelerator(escape, this); - esc_accel_target_registered_ = true; + ResetFocusTracker(); } -void FindBarHost::UnregisterEscAccelerator() { - DCHECK(esc_accel_target_registered_); - views::Accelerator escape(base::VKEY_ESCAPE, false, false, false); - focus_manager_->UnregisterAccelerator(escape, this); - esc_accel_target_registered_ = false; -} bool FindBarHost::MaybeForwardKeystrokeToWebpage( const views::Textfield::Keystroke& key_stroke) { @@ -493,3 +358,7 @@ bool FindBarHost::MaybeForwardKeystrokeToWebpage( render_view_host->ForwardKeyboardEvent(event); return true; } + +FindBarView* FindBarHost::find_bar_view() { + return static_cast<FindBarView*>(view()); +} |