This source file includes following definitions.
- is_visible_
- Init
- Show
- SetFocusAndSelection
- IsAnimating
- Hide
- StopAnimation
- IsVisible
- OnWillChangeFocus
- OnDidChangeFocus
- AnimationProgressed
- AnimationEnded
- ResetFocusTracker
- OnVisibilityChanged
- GetWidgetBounds
- UpdateWindowEdges
- RegisterAccelerators
- UnregisterAccelerators
#include "chrome/browser/ui/views/dropdown_bar_host.h"
#include <algorithm>
#include "chrome/browser/ui/view_ids.h"
#include "chrome/browser/ui/views/dropdown_bar_host_delegate.h"
#include "chrome/browser/ui/views/dropdown_bar_view.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "ui/events/keycodes/keyboard_codes.h"
#include "ui/gfx/animation/slide_animation.h"
#include "ui/gfx/path.h"
#include "ui/gfx/scrollbar_size.h"
#include "ui/views/focus/external_focus_tracker.h"
#include "ui/views/focus/view_storage.h"
#include "ui/views/widget/widget.h"
#if defined(USE_AURA)
#include "ui/gfx/scoped_sk_region.h"
#elif defined(OS_WIN)
#include "base/win/scoped_gdi_object.h"
#endif
namespace {
#if defined(USE_AURA)
typedef gfx::ScopedSkRegion ScopedPlatformRegion;
#elif defined(OS_WIN)
typedef base::win::ScopedRegion ScopedPlatformRegion;
#endif
}
using gfx::Path;
bool DropdownBarHost::disable_animations_during_testing_ = false;
DropdownBarHost::DropdownBarHost(BrowserView* browser_view)
: browser_view_(browser_view),
view_(NULL),
delegate_(NULL),
animation_offset_(0),
focus_manager_(NULL),
esc_accel_target_registered_(false),
is_visible_(false) {
}
void DropdownBarHost::Init(views::View* host_view,
views::View* view,
DropdownBarHostDelegate* delegate) {
DCHECK(view);
DCHECK(delegate);
view_ = view;
delegate_ = delegate;
host_.reset(new views::Widget);
views::Widget::InitParams params(views::Widget::InitParams::TYPE_CONTROL);
params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
params.parent = browser_view_->GetWidget()->GetNativeView();
#if defined(USE_AURA)
params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW;
#endif
host_->Init(params);
host_->SetContentsView(view_);
SetHostViewNative(host_view);
focus_manager_ = host_->GetFocusManager();
if (focus_manager_) {
focus_manager_->AddFocusChangeListener(this);
} else {
NOTREACHED();
}
animation_.reset(new gfx::SlideAnimation(this));
}
DropdownBarHost::~DropdownBarHost() {
focus_manager_->RemoveFocusChangeListener(this);
focus_tracker_.reset(NULL);
}
void DropdownBarHost::Show(bool animate) {
focus_tracker_.reset(new views::ExternalFocusTracker(view_, focus_manager_));
bool was_visible = is_visible_;
is_visible_ = true;
if (!animate || disable_animations_during_testing_) {
animation_->Reset(1);
AnimationProgressed(animation_.get());
} else if (!was_visible) {
animation_->Reset();
animation_->Show();
}
if (!was_visible)
OnVisibilityChanged();
}
void DropdownBarHost::SetFocusAndSelection() {
delegate_->SetFocusAndSelection(true);
}
bool DropdownBarHost::IsAnimating() const {
return animation_->is_animating();
}
void DropdownBarHost::Hide(bool animate) {
if (!IsVisible())
return;
if (animate && !disable_animations_during_testing_ &&
!animation_->IsClosing()) {
animation_->Hide();
} else {
if (animation_->IsClosing()) {
StopAnimation();
} else {
animation_->Reset();
AnimationEnded(animation_.get());
}
}
}
void DropdownBarHost::StopAnimation() {
animation_->End();
}
bool DropdownBarHost::IsVisible() const {
return is_visible_;
}
void DropdownBarHost::OnWillChangeFocus(views::View* focused_before,
views::View* focused_now) {
bool our_view_before = focused_before && view_->Contains(focused_before);
bool our_view_now = focused_now && view_->Contains(focused_now);
if (!our_view_before && our_view_now) {
RegisterAccelerators();
} else if (our_view_before && !our_view_now) {
UnregisterAccelerators();
}
}
void DropdownBarHost::OnDidChangeFocus(views::View* focused_before,
views::View* focused_now) {
}
void DropdownBarHost::AnimationProgressed(const gfx::Animation* animation) {
gfx::Size pref_size = view_->GetPreferredSize();
animation_offset_ = static_cast<int>((1.0 - animation_->GetCurrentValue()) *
pref_size.height());
gfx::Rect dlg_rect = GetDialogPosition(gfx::Rect());
SetDialogPosition(dlg_rect, false);
delegate_->SetAnimationOffset(animation_offset_);
view_->SchedulePaint();
}
void DropdownBarHost::AnimationEnded(const gfx::Animation* animation) {
animation_offset_ = 0;
if (!animation_->IsShowing()) {
host_->Hide();
is_visible_ = false;
OnVisibilityChanged();
} else {
}
}
void DropdownBarHost::ResetFocusTracker() {
focus_tracker_.reset(NULL);
}
void DropdownBarHost::OnVisibilityChanged() {
}
void DropdownBarHost::GetWidgetBounds(gfx::Rect* bounds) {
DCHECK(bounds);
*bounds = browser_view_->bounds();
}
void DropdownBarHost::UpdateWindowEdges(const gfx::Rect& new_pos) {
int w = new_pos.width() - 6;
int h = new_pos.height();
const Path::Point polygon[] = {
{2, 0}, {3, 1}, {3, h - 2}, {4, h - 1},
{4, h}, {w+0, h},
{w+2, h - 1}, {w+3, h - 2}, {w+3, 1}, {w+4, 0}
};
int max_x = 0, max_y = 0;
for (size_t i = 0; i < arraysize(polygon); i++) {
max_x = std::max(max_x, static_cast<int>(polygon[i].x));
max_y = std::max(max_y, static_cast<int>(polygon[i].y));
}
Path path(polygon, arraysize(polygon));
ScopedPlatformRegion region(path.CreateNativeRegion());
if (animation_offset() > 0) {
int y = animation_offset();
Path animation_path;
SkRect animation_rect = { SkIntToScalar(0), SkIntToScalar(y),
SkIntToScalar(max_x), SkIntToScalar(max_y) };
animation_path.addRect(animation_rect);
ScopedPlatformRegion animation_region(
animation_path.CreateNativeRegion());
region.Set(Path::IntersectRegions(animation_region.Get(), region.Get()));
Path::Point left_curve[] = {
{2, y+0}, {3, y+1}, {3, y+0}, {2, y+0}
};
Path::Point right_curve[] = {
{w+3, y+1}, {w+4, y+0}, {w+3, y+0}, {w+3, y+1}
};
Path left_path(left_curve, arraysize(left_curve));
ScopedPlatformRegion r(left_path.CreateNativeRegion());
region.Set(Path::CombineRegions(r.Get(), region.Get()));
Path right_path(right_curve, arraysize(right_curve));
region.Set(Path::CombineRegions(r.Get(), region.Get()));
}
gfx::Rect widget_bounds;
GetWidgetBounds(&widget_bounds);
static const int kAddedWidth = 7;
int difference = new_pos.right() - kAddedWidth - widget_bounds.right() -
gfx::scrollbar_size() + 1;
if (difference > 0) {
Path::Point exclude[4];
exclude[0].x = max_x - difference;
exclude[0].y = 0;
exclude[1].x = max_x;
exclude[1].y = 0;
exclude[2].x = max_x;
exclude[2].y = max_y;
exclude[3].x = max_x - difference;
exclude[3].y = max_y;
gfx::Path exclude_path(exclude, arraysize(exclude));
ScopedPlatformRegion exclude_region(exclude_path.CreateNativeRegion());
region.Set(Path::SubtractRegion(region.Get(), exclude_region.Get()));
}
host()->SetShape(region.release());
}
void DropdownBarHost::RegisterAccelerators() {
DCHECK(!esc_accel_target_registered_);
ui::Accelerator escape(ui::VKEY_ESCAPE, ui::EF_NONE);
focus_manager_->RegisterAccelerator(
escape, ui::AcceleratorManager::kNormalPriority, this);
esc_accel_target_registered_ = true;
}
void DropdownBarHost::UnregisterAccelerators() {
DCHECK(esc_accel_target_registered_);
ui::Accelerator escape(ui::VKEY_ESCAPE, ui::EF_NONE);
focus_manager_->UnregisterAccelerator(escape, this);
esc_accel_target_registered_ = false;
}