summaryrefslogtreecommitdiffstats
path: root/views/bubble/bubble_delegate.cc
blob: b46e3536bf73feba52fed2035c22b8abb319c331 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "views/bubble/bubble_delegate.h"

#include "ui/base/animation/slide_animation.h"
#include "views/bubble/bubble_frame_view.h"
#include "views/widget/widget.h"

// The duration of the fade animation in milliseconds.
static const int kHideFadeDurationMS = 1000;

namespace views {

BubbleDelegateView::BubbleDelegateView()
    : WidgetDelegateView(),
      close_on_esc_(true),
      arrow_location_(BubbleBorder::TOP_LEFT),
      color_(SK_ColorWHITE) {
  AddAccelerator(Accelerator(ui::VKEY_ESCAPE, 0));
}

BubbleDelegateView::BubbleDelegateView(
    const gfx::Point& anchor_point,
    BubbleBorder::ArrowLocation arrow_location,
    const SkColor& color)
    : WidgetDelegateView(),
      close_on_esc_(true),
      anchor_point_(anchor_point),
      arrow_location_(arrow_location),
      color_(color) {
  AddAccelerator(Accelerator(ui::VKEY_ESCAPE, 0));
}

BubbleDelegateView::~BubbleDelegateView() {}

// static
Widget* BubbleDelegateView::CreateBubble(BubbleDelegateView* bubble_delegate,
                                         Widget* parent_widget) {
  bubble_delegate->Init();
  views::Widget* bubble_widget = new views::Widget();
  views::Widget::InitParams params(views::Widget::InitParams::TYPE_BUBBLE);
  params.delegate = bubble_delegate;
  params.transparent = true;
  if (!parent_widget)
    params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
  params.parent_widget = parent_widget;
  bubble_widget->Init(params);
  bubble_widget->SetBounds(bubble_delegate->GetBubbleBounds());
  return bubble_widget;
}

View* BubbleDelegateView::GetInitiallyFocusedView() {
  return this;
}

View* BubbleDelegateView::GetContentsView() {
  return this;
}

ClientView* BubbleDelegateView::CreateClientView(Widget* widget) {
  return new ClientView(widget, GetContentsView());
}

NonClientFrameView* BubbleDelegateView::CreateNonClientFrameView() {
  return new BubbleFrameView(GetArrowLocation(),
                             GetPreferredSize(),
                             GetColor());
}

gfx::Point BubbleDelegateView::GetAnchorPoint() {
  return anchor_point_;
}

BubbleBorder::ArrowLocation BubbleDelegateView::GetArrowLocation() const {
  return arrow_location_;
}

SkColor BubbleDelegateView::GetColor() const {
  return color_;
}

void BubbleDelegateView::Init() {}

void BubbleDelegateView::StartFade(bool fade_in) {
  fade_animation_.reset(new ui::SlideAnimation(this));
  fade_animation_->SetSlideDuration(kHideFadeDurationMS);
  fade_animation_->Reset(fade_in ? 0.0 : 1.0);
  if (fade_in) {
    GetWidget()->SetOpacity(0);
    GetWidget()->Show();
    fade_animation_->Show();
  } else {
    fade_animation_->Hide();
  }
}

bool BubbleDelegateView::AcceleratorPressed(const Accelerator& accelerator) {
  if (!close_on_esc() || accelerator.key_code() != ui::VKEY_ESCAPE)
    return false;
  if (fade_animation_.get())
    fade_animation_->Reset();
  GetWidget()->Close();
  return true;
}

void BubbleDelegateView::AnimationEnded(const ui::Animation* animation) {
  DCHECK_EQ(animation, fade_animation_.get());
  bool closed = fade_animation_->GetCurrentValue() == 0;
  fade_animation_->Reset();
  if (closed)
    GetWidget()->Close();
}

void BubbleDelegateView::AnimationProgressed(const ui::Animation* animation) {
  DCHECK_EQ(animation, fade_animation_.get());
  DCHECK(fade_animation_->is_animating());
  GetWidget()->SetOpacity(fade_animation_->GetCurrentValue() * 255);
  SchedulePaint();
}

const BubbleView* BubbleDelegateView::GetBubbleView() const {
  return GetWidget()->client_view()->AsBubbleView();
}

const BubbleFrameView* BubbleDelegateView::GetBubbleFrameView() const {
  return static_cast<BubbleFrameView*>(
      GetWidget()->non_client_view()->frame_view());
}

gfx::Rect BubbleDelegateView::GetBubbleBounds() {
  // The argument rect has its origin at the bubble's arrow anchor point;
  // its size is the preferred size of the bubble's client view (this view).
  return GetBubbleFrameView()->GetWindowBoundsForClientBounds(
      gfx::Rect(GetAnchorPoint(), GetPreferredSize()));
}

}  // namespace views