summaryrefslogtreecommitdiffstats
path: root/views/bubble/bubble_delegate.cc
blob: f5efff16f1cf5af9c5e52230a64e31cd92efeb6c (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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// 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 = 200;

namespace views {

namespace {

// Create a widget to host the bubble.
Widget* CreateBubbleWidget(BubbleDelegateView* bubble, Widget* parent) {
  Widget* bubble_widget = new Widget();
  Widget::InitParams bubble_params(Widget::InitParams::TYPE_BUBBLE);
  bubble_params.delegate = bubble;
  bubble_params.transparent = true;
  bubble_params.parent_widget = parent;
  if (!bubble_params.parent_widget)
    bubble_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
#if defined(OS_WIN) && !defined(USE_AURA)
  bubble_params.type = Widget::InitParams::TYPE_WINDOW_FRAMELESS;
  bubble_params.transparent = false;
#endif
  bubble_widget->Init(bubble_params);
  return bubble_widget;
}

#if defined(OS_WIN) && !defined(USE_AURA)
// The border widget's delegate, needed for transparent Windows native controls.
// TODO(msw): Remove this when Windows native controls are no longer needed.
class VIEWS_EXPORT BubbleBorderDelegateView : public WidgetDelegateView {
 public:
  explicit BubbleBorderDelegateView(BubbleDelegateView* bubble)
      : bubble_(bubble) {}
  virtual ~BubbleBorderDelegateView() {}

  // WidgetDelegateView overrides:
  virtual bool CanActivate() const OVERRIDE;
  virtual NonClientFrameView* CreateNonClientFrameView() OVERRIDE;

 private:
  BubbleDelegateView* bubble_;

  DISALLOW_COPY_AND_ASSIGN(BubbleBorderDelegateView);
};

bool BubbleBorderDelegateView::CanActivate() const { return false; }

NonClientFrameView* BubbleBorderDelegateView::CreateNonClientFrameView() {
  return bubble_->CreateNonClientFrameView();
}

// Create a widget to host the bubble's border.
Widget* CreateBorderWidget(BubbleDelegateView* bubble, Widget* parent) {
  Widget* border_widget = new Widget();
  Widget::InitParams border_params(Widget::InitParams::TYPE_BUBBLE);
  border_params.delegate = new BubbleBorderDelegateView(bubble);
  border_params.transparent = true;
  border_params.parent_widget = parent;
  if (!border_params.parent_widget)
    border_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
  border_widget->Init(border_params);
  return border_widget;
}
#endif

}  // namespace

BubbleDelegateView::BubbleDelegateView()
    : close_on_esc_(true),
      close_on_deactivate_(true),
      allow_bubble_offscreen_(false),
      anchor_view_(NULL),
      arrow_location_(BubbleBorder::TOP_LEFT),
      color_(SK_ColorWHITE),
      border_widget_(NULL),
      use_focusless_(false) {
  set_background(views::Background::CreateSolidBackground(color_));
  AddAccelerator(ui::Accelerator(ui::VKEY_ESCAPE, 0));
}

BubbleDelegateView::BubbleDelegateView(
    View* anchor_view,
    BubbleBorder::ArrowLocation arrow_location,
    const SkColor& color)
    : close_on_esc_(true),
      close_on_deactivate_(true),
      allow_bubble_offscreen_(false),
      anchor_view_(anchor_view),
      arrow_location_(arrow_location),
      color_(color),
      original_opacity_(255),
      border_widget_(NULL),
      use_focusless_(false) {
  set_background(views::Background::CreateSolidBackground(color_));
  AddAccelerator(ui::Accelerator(ui::VKEY_ESCAPE, 0));
}

BubbleDelegateView::~BubbleDelegateView() {
  if (border_widget_)
    border_widget_->Close();
}

// static
Widget* BubbleDelegateView::CreateBubble(BubbleDelegateView* bubble_delegate) {
  bubble_delegate->Init();
  Widget* parent_widget = bubble_delegate->anchor_view() ?
      bubble_delegate->anchor_view()->GetWidget() : NULL;
  Widget* bubble_widget = CreateBubbleWidget(bubble_delegate, parent_widget);

#if defined(OS_WIN) && !defined(USE_AURA)
  // First set the contents view to initialize view bounds for widget sizing.
  bubble_widget->SetContentsView(bubble_delegate->GetContentsView());
  bubble_delegate->InitializeBorderWidget(parent_widget);
  bubble_widget->SetBounds(bubble_delegate->GetBubbleClientBounds());
#else
  bubble_widget->SetBounds(bubble_delegate->GetBubbleBounds());
#endif

  bubble_widget->AddObserver(bubble_delegate);
  if (parent_widget && parent_widget->GetTopLevelWidget())
    parent_widget->GetTopLevelWidget()->DisableInactiveRendering();
  return bubble_widget;
}

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

BubbleDelegateView* BubbleDelegateView::AsBubbleDelegate() {
  return this;
}

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

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

void BubbleDelegateView::OnWidgetActivationChanged(Widget* widget,
                                                   bool active) {
  if (close_on_deactivate() && widget == GetWidget() && !active) {
    GetWidget()->RemoveObserver(this);
    GetWidget()->Close();
  }
}

gfx::Point BubbleDelegateView::GetAnchorPoint() {
  if (!anchor_view())
    return gfx::Point();

  BubbleBorder::ArrowLocation location = GetArrowLocation();
  gfx::Point anchor(anchor_view()->bounds().CenterPoint());
  // By default, pick the middle of |anchor_view_|'s edge opposite the arrow.
  if (BubbleBorder::is_arrow_on_horizontal(location)) {
    anchor.SetPoint(anchor_view()->width() / 2,
        BubbleBorder::is_arrow_on_top(location) ? anchor_view()->height() : 0);
  } else if (BubbleBorder::has_arrow(location)) {
    anchor.SetPoint(
        BubbleBorder::is_arrow_on_left(location) ? anchor_view()->width() : 0,
        anchor_view_->height() / 2);
  }
  View::ConvertPointToScreen(anchor_view(), &anchor);
  return anchor;
}

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

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

void BubbleDelegateView::Show() {
  if (border_widget_)
    border_widget_->Show();
  GetWidget()->Show();
  GetFocusManager()->SetFocusedView(GetInitiallyFocusedView());
}

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) {
    original_opacity_ = 0;
    if (border_widget_)
      border_widget_->SetOpacity(original_opacity_);
    GetWidget()->SetOpacity(original_opacity_);
    Show();
    fade_animation_->Show();
  } else {
    original_opacity_ = 255;
    fade_animation_->Hide();
  }
}

void BubbleDelegateView::ResetFade() {
  fade_animation_.reset();
  if (border_widget_)
    border_widget_->SetOpacity(original_opacity_);
  GetWidget()->SetOpacity(original_opacity_);
}

bool BubbleDelegateView::AcceleratorPressed(
    const ui::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::Init() {}

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());
  unsigned char opacity = fade_animation_->GetCurrentValue() * 255;
#if defined(OS_WIN) && !defined(USE_AURA)
  // Explicitly set the content Widget's layered style and set transparency via
  // SetLayeredWindowAttributes. This is done because initializing the Widget as
  // transparent and setting opacity via UpdateLayeredWindow doesn't support
  // hosting child native Windows controls.
  const HWND hwnd = GetWidget()->GetNativeView();
  const DWORD style = GetWindowLong(hwnd, GWL_EXSTYLE);
  if ((opacity == 255) == !!(style & WS_EX_LAYERED))
    SetWindowLong(hwnd, GWL_EXSTYLE, style ^ WS_EX_LAYERED);
  SetLayeredWindowAttributes(hwnd, 0, opacity, LWA_ALPHA);
  // Update the border widget's opacity.
  border_widget_->SetOpacity(opacity);
  border_widget_->non_client_view()->SchedulePaint();
#endif
  GetWidget()->SetOpacity(opacity);
  SchedulePaint();
}

BubbleFrameView* BubbleDelegateView::GetBubbleFrameView() const {
  const Widget* widget = border_widget_ ? border_widget_ : GetWidget();
  return static_cast<BubbleFrameView*>(widget->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()));
}

#if defined(OS_WIN) && !defined(USE_AURA)
void BubbleDelegateView::InitializeBorderWidget(Widget* parent_widget) {
  border_widget_ = CreateBorderWidget(this, parent_widget);
  border_widget_->SetBounds(GetBubbleBounds());
}

gfx::Rect BubbleDelegateView::GetBubbleClientBounds() const {
  gfx::Rect client_bounds(GetBubbleFrameView()->GetBoundsForClientView());
  client_bounds.Offset(border_widget_->GetWindowScreenBounds().origin());
  return client_bounds;
}
#endif

}  // namespace views