15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved. 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file. 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/views/dropdown_bar_host.h" 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <algorithm> 85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/view_ids.h" 105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/views/dropdown_bar_host_delegate.h" 115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/views/dropdown_bar_view.h" 125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/views/frame/browser_view.h" 135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/base/animation/slide_animation.h" 145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/base/keycodes/keyboard_codes.h" 155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/gfx/path.h" 165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/gfx/scrollbar_size.h" 175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/views/focus/external_focus_tracker.h" 185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/views/focus/view_storage.h" 195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/views/widget/widget.h" 205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(USE_AURA) 225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/gfx/scoped_sk_region.h" 235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#elif defined(OS_WIN) 245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/win/scoped_gdi_object.h" 255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif 265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace { 285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(USE_AURA) 305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef gfx::ScopedSkRegion ScopedPlatformRegion; 315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#elif defined(OS_WIN) 325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef base::win::ScopedRegion ScopedPlatformRegion; 335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif 345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} // namespace 365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using gfx::Path; 385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static 405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool DropdownBarHost::disable_animations_during_testing_ = false; 415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//////////////////////////////////////////////////////////////////////////////// 435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// DropdownBarHost, public: 445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)DropdownBarHost::DropdownBarHost(BrowserView* browser_view) 465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) : browser_view_(browser_view), 475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) view_(NULL), 485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) delegate_(NULL), 495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) animation_offset_(0), 505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) focus_manager_(NULL), 515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) esc_accel_target_registered_(false), 525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) is_visible_(false) { 535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 55868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)void DropdownBarHost::Init(views::View* host_view, 56868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) views::View* view, 575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DropdownBarHostDelegate* delegate) { 585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DCHECK(view); 595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DCHECK(delegate); 605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) view_ = view; 625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) delegate_ = delegate; 635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Initialize the host. 655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) host_.reset(new views::Widget); 665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) views::Widget::InitParams params(views::Widget::InitParams::TYPE_CONTROL); 675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; 682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) params.parent = browser_view_->GetWidget()->GetNativeView(); 695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(USE_AURA) 70eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW; 715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif 725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) host_->Init(params); 735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) host_->SetContentsView(view_); 745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 75868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) SetHostViewNative(host_view); 76868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Start listening to focus changes, so we can register and unregister our 785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // own handler for Escape. 795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) focus_manager_ = host_->GetFocusManager(); 805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (focus_manager_) { 815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) focus_manager_->AddFocusChangeListener(this); 825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } else { 835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // In some cases (see bug http://crbug.com/17056) it seems we may not have 845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // a focus manager. Please reopen the bug if you hit this. 855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) NOTREACHED(); 865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Start the process of animating the opening of the widget. 895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) animation_.reset(new ui::SlideAnimation(this)); 905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)DropdownBarHost::~DropdownBarHost() { 935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) focus_manager_->RemoveFocusChangeListener(this); 945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) focus_tracker_.reset(NULL); 955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void DropdownBarHost::Show(bool animate) { 985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Stores the currently focused view, and tracks focus changes so that we can 995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // restore focus when the dropdown widget is closed. 1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) focus_tracker_.reset(new views::ExternalFocusTracker(view_, focus_manager_)); 1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1027d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) bool was_visible = is_visible_; 1037d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) is_visible_ = true; 1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (!animate || disable_animations_during_testing_) { 1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) animation_->Reset(1); 1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) AnimationProgressed(animation_.get()); 1077d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) } else if (!was_visible) { 1087d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) // Don't re-start the animation. 1097d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) animation_->Reset(); 1107d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) animation_->Show(); 1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1127d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 1137d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) if (!was_visible) 1147d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) OnVisibilityChanged(); 1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void DropdownBarHost::SetFocusAndSelection() { 1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) delegate_->SetFocusAndSelection(true); 1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool DropdownBarHost::IsAnimating() const { 1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return animation_->is_animating(); 1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void DropdownBarHost::Hide(bool animate) { 1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (!IsVisible()) 1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return; 1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (animate && !disable_animations_during_testing_ && 1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) !animation_->IsClosing()) { 1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) animation_->Hide(); 1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } else { 1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (animation_->IsClosing()) { 1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // If we're in the middle of a close animation, skip immediately to the 1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // end of the animation. 1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) StopAnimation(); 1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } else { 1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Otherwise we need to set both the animation state to ended and the 1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // DropdownBarHost state to ended/hidden, otherwise the next time we try 1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // to show the bar, it might refuse to do so. Note that we call 1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // AnimationEnded ourselves as Reset does not call it if we are not 1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // animating here. 1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) animation_->Reset(); 1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) AnimationEnded(animation_.get()); 1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void DropdownBarHost::StopAnimation() { 1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) animation_->End(); 1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool DropdownBarHost::IsVisible() const { 1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return is_visible_; 1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//////////////////////////////////////////////////////////////////////////////// 1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// DropdownBarHost, views::FocusChangeListener implementation: 1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void DropdownBarHost::OnWillChangeFocus(views::View* focused_before, 1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) views::View* focused_now) { 1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // First we need to determine if one or both of the views passed in are child 1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // views of our view. 1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool our_view_before = focused_before && view_->Contains(focused_before); 1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool our_view_now = focused_now && view_->Contains(focused_now); 1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // When both our_view_before and our_view_now are false, it means focus is 1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // changing hands elsewhere in the application (and we shouldn't do anything). 1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Similarly, when both are true, focus is changing hands within the dropdown 1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // widget (and again, we should not do anything). We therefore only need to 1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // look at when we gain initial focus and when we loose it. 1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (!our_view_before && our_view_now) { 1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // We are gaining focus from outside the dropdown widget so we must register 1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // a handler for Escape. 1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) RegisterAccelerators(); 1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } else if (our_view_before && !our_view_now) { 1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // We are losing focus to something outside our widget so we restore the 1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // original handler for Escape. 1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) UnregisterAccelerators(); 1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void DropdownBarHost::OnDidChangeFocus(views::View* focused_before, 1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) views::View* focused_now) { 1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//////////////////////////////////////////////////////////////////////////////// 1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// DropdownBarHost, ui::AnimationDelegate implementation: 1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void DropdownBarHost::AnimationProgressed(const ui::Animation* animation) { 1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // First, we calculate how many pixels to slide the widget. 1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) gfx::Size pref_size = view_->GetPreferredSize(); 1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) animation_offset_ = static_cast<int>((1.0 - animation_->GetCurrentValue()) * 1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) pref_size.height()); 1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // This call makes sure it appears in the right location, the size and shape 1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // is correct and that it slides in the right direction. 1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) gfx::Rect dlg_rect = GetDialogPosition(gfx::Rect()); 1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) SetDialogPosition(dlg_rect, false); 1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Let the view know if we are animating, and at which offset to draw the 2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // edges. 2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) delegate_->SetAnimationOffset(animation_offset_); 2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) view_->SchedulePaint(); 2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void DropdownBarHost::AnimationEnded(const ui::Animation* animation) { 2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Place the dropdown widget in its fully opened state. 2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) animation_offset_ = 0; 2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (!animation_->IsShowing()) { 2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Animation has finished closing. 2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) host_->Hide(); 2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) is_visible_ = false; 2137d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) OnVisibilityChanged(); 2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } else { 2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Animation has finished opening. 2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//////////////////////////////////////////////////////////////////////////////// 2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// DropdownBarHost protected: 2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void DropdownBarHost::ResetFocusTracker() { 2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) focus_tracker_.reset(NULL); 2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2267d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)void DropdownBarHost::OnVisibilityChanged() { 2277d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)} 2287d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void DropdownBarHost::GetWidgetBounds(gfx::Rect* bounds) { 2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DCHECK(bounds); 2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *bounds = browser_view_->bounds(); 2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void DropdownBarHost::UpdateWindowEdges(const gfx::Rect& new_pos) { 2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // |w| is used to make it easier to create the part of the polygon that curves 2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // the right side of the Find window. It essentially keeps track of the 2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // x-pixel position of the right-most background image inside the view. 2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // TODO(finnur): Let the view tell us how to draw the curves or convert 2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // this to a CustomFrameWindow. 2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int w = new_pos.width() - 6; // -6 positions us at the left edge of the 2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // rightmost background image of the view. 2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int h = new_pos.height(); 2435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // This polygon array represents the outline of the background image for the 2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // window. Basically, it encompasses only the visible pixels of the 2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // concatenated find_dlg_LMR_bg images (where LMR = [left | middle | right]). 2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const Path::Point polygon[] = { 2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) {2, 0}, {3, 1}, {3, h - 2}, {4, h - 1}, 2495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) {4, h}, {w+0, h}, 2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) {w+2, h - 1}, {w+3, h - 2}, {w+3, 1}, {w+4, 0} 2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) }; 2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Find the largest x and y value in the polygon. 2545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int max_x = 0, max_y = 0; 2555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) for (size_t i = 0; i < arraysize(polygon); i++) { 2565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) max_x = std::max(max_x, static_cast<int>(polygon[i].x)); 2575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) max_y = std::max(max_y, static_cast<int>(polygon[i].y)); 2585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 2595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // We then create the polygon and use SetWindowRgn to force the window to draw 2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // only within that area. This region may get reduced in size below. 2625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Path path(polygon, arraysize(polygon)); 2635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ScopedPlatformRegion region(path.CreateNativeRegion()); 2645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Are we animating? 2655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (animation_offset() > 0) { 2665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // The animation happens in two steps: First, we clip the window and then in 2675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // GetWidgetPosition we offset the window position so that it still looks 2685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // attached to the toolbar as it grows. We clip the window by creating a 2695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // rectangle region (that gradually increases as the animation progresses) 2705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // and find the intersection between the two regions using CombineRgn. 2715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // |y| shrinks as the animation progresses from the height of the view down 2735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // to 0 (and reverses when closing). 2745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int y = animation_offset(); 2755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // |y| shrinking means the animation (visible) region gets larger. In other 2765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // words: the rectangle grows upward (when the widget is opening). 2775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Path animation_path; 2785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) SkRect animation_rect = { SkIntToScalar(0), SkIntToScalar(y), 2795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) SkIntToScalar(max_x), SkIntToScalar(max_y) }; 2805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) animation_path.addRect(animation_rect); 2815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ScopedPlatformRegion animation_region( 2825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) animation_path.CreateNativeRegion()); 2835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) region.Set(Path::IntersectRegions(animation_region.Get(), region.Get())); 2845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Next, we need to increase the region a little bit to account for the 2865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // curved edges that the view will draw to make it look like grows out of 2875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // the toolbar. 2885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Path::Point left_curve[] = { 2895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) {2, y+0}, {3, y+1}, {3, y+0}, {2, y+0} 2905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) }; 2915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Path::Point right_curve[] = { 2925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) {w+3, y+1}, {w+4, y+0}, {w+3, y+0}, {w+3, y+1} 2935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) }; 2945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Combine the region for the curve on the left with our main region. 2965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Path left_path(left_curve, arraysize(left_curve)); 2975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ScopedPlatformRegion r(left_path.CreateNativeRegion()); 2985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) region.Set(Path::CombineRegions(r.Get(), region.Get())); 2995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Combine the region for the curve on the right with our main region. 3015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Path right_path(right_curve, arraysize(right_curve)); 3025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) region.Set(Path::CombineRegions(r.Get(), region.Get())); 3035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 3045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Now see if we need to truncate the region because parts of it obscures 3065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // the main window border. 3075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) gfx::Rect widget_bounds; 3085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) GetWidgetBounds(&widget_bounds); 3095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Calculate how much our current position overlaps our boundaries. If we 3115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // overlap, it means we have too little space to draw the whole widget and 3125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // we allow overwriting the scrollbar before we start truncating our widget. 3135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // 3145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // TODO(brettw) this constant is evil. This is the amount of room we've added 3155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // to the window size, when we set the region, it can change the size. 3165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static const int kAddedWidth = 7; 3175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int difference = new_pos.right() - kAddedWidth - widget_bounds.right() - 3185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) gfx::scrollbar_size() + 1; 3195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (difference > 0) { 3205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Path::Point exclude[4]; 3215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) exclude[0].x = max_x - difference; // Top left corner. 3225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) exclude[0].y = 0; 3235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) exclude[1].x = max_x; // Top right corner. 3255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) exclude[1].y = 0; 3265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) exclude[2].x = max_x; // Bottom right corner. 3285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) exclude[2].y = max_y; 3295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) exclude[3].x = max_x - difference; // Bottom left corner. 3315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) exclude[3].y = max_y; 3325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Subtract this region from the original region. 3345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) gfx::Path exclude_path(exclude, arraysize(exclude)); 3355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ScopedPlatformRegion exclude_region(exclude_path.CreateNativeRegion()); 3365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) region.Set(Path::SubtractRegion(region.Get(), exclude_region.Get())); 3375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 3385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Window takes ownership of the region. 3405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) host()->SetShape(region.release()); 3415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 3425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void DropdownBarHost::RegisterAccelerators() { 3445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DCHECK(!esc_accel_target_registered_); 3455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ui::Accelerator escape(ui::VKEY_ESCAPE, ui::EF_NONE); 3465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) focus_manager_->RegisterAccelerator( 3475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) escape, ui::AcceleratorManager::kNormalPriority, this); 3485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) esc_accel_target_registered_ = true; 3495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 3505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void DropdownBarHost::UnregisterAccelerators() { 3525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DCHECK(esc_accel_target_registered_); 3535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ui::Accelerator escape(ui::VKEY_ESCAPE, ui::EF_NONE); 3545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) focus_manager_->UnregisterAccelerator(escape, this); 3555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) esc_accel_target_registered_ = false; 3565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 357