Add a minor text member to ui::MenuModel.
[chromium-blink-merge.git] / chrome / browser / ui / views / dropdown_bar_host.cc
blob1d150c81953aa4f63de6d7193dffa0ee03ad9c8f
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/views/dropdown_bar_host.h"
7 #include <algorithm>
9 #include "chrome/browser/ui/view_ids.h"
10 #include "chrome/browser/ui/views/dropdown_bar_host_delegate.h"
11 #include "chrome/browser/ui/views/dropdown_bar_view.h"
12 #include "chrome/browser/ui/views/frame/browser_view.h"
13 #include "ui/base/animation/slide_animation.h"
14 #include "ui/base/keycodes/keyboard_codes.h"
15 #include "ui/gfx/path.h"
16 #include "ui/gfx/scrollbar_size.h"
17 #include "ui/views/focus/external_focus_tracker.h"
18 #include "ui/views/focus/view_storage.h"
19 #include "ui/views/widget/widget.h"
21 #if defined(USE_AURA)
22 #include "ui/gfx/scoped_sk_region.h"
23 #elif defined(OS_WIN)
24 #include "base/win/scoped_gdi_object.h"
25 #endif
27 namespace {
29 #if defined(USE_AURA)
30 typedef gfx::ScopedSkRegion ScopedPlatformRegion;
31 #elif defined(OS_WIN)
32 typedef base::win::ScopedRegion ScopedPlatformRegion;
33 #endif
35 } // namespace
37 using gfx::Path;
39 // static
40 bool DropdownBarHost::disable_animations_during_testing_ = false;
42 ////////////////////////////////////////////////////////////////////////////////
43 // DropdownBarHost, public:
45 DropdownBarHost::DropdownBarHost(BrowserView* browser_view)
46 : browser_view_(browser_view),
47 view_(NULL),
48 delegate_(NULL),
49 animation_offset_(0),
50 focus_manager_(NULL),
51 esc_accel_target_registered_(false),
52 is_visible_(false) {
55 void DropdownBarHost::Init(views::View* host_view,
56 views::View* view,
57 DropdownBarHostDelegate* delegate) {
58 DCHECK(view);
59 DCHECK(delegate);
61 view_ = view;
62 delegate_ = delegate;
64 // Initialize the host.
65 host_.reset(new views::Widget);
66 views::Widget::InitParams params(views::Widget::InitParams::TYPE_CONTROL);
67 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
68 params.parent = browser_view_->GetWidget()->GetNativeView();
69 #if defined(USE_AURA)
70 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW;
71 #endif
72 host_->Init(params);
73 host_->SetContentsView(view_);
75 SetHostViewNative(host_view);
77 // Start listening to focus changes, so we can register and unregister our
78 // own handler for Escape.
79 focus_manager_ = host_->GetFocusManager();
80 if (focus_manager_) {
81 focus_manager_->AddFocusChangeListener(this);
82 } else {
83 // In some cases (see bug http://crbug.com/17056) it seems we may not have
84 // a focus manager. Please reopen the bug if you hit this.
85 NOTREACHED();
88 // Start the process of animating the opening of the widget.
89 animation_.reset(new ui::SlideAnimation(this));
92 DropdownBarHost::~DropdownBarHost() {
93 focus_manager_->RemoveFocusChangeListener(this);
94 focus_tracker_.reset(NULL);
97 void DropdownBarHost::Show(bool animate) {
98 // Stores the currently focused view, and tracks focus changes so that we can
99 // restore focus when the dropdown widget is closed.
100 focus_tracker_.reset(new views::ExternalFocusTracker(view_, focus_manager_));
102 bool was_visible = is_visible_;
103 is_visible_ = true;
104 if (!animate || disable_animations_during_testing_) {
105 animation_->Reset(1);
106 AnimationProgressed(animation_.get());
107 } else if (!was_visible) {
108 // Don't re-start the animation.
109 animation_->Reset();
110 animation_->Show();
113 if (!was_visible)
114 OnVisibilityChanged();
117 void DropdownBarHost::SetFocusAndSelection() {
118 delegate_->SetFocusAndSelection(true);
121 bool DropdownBarHost::IsAnimating() const {
122 return animation_->is_animating();
125 void DropdownBarHost::Hide(bool animate) {
126 if (!IsVisible())
127 return;
128 if (animate && !disable_animations_during_testing_ &&
129 !animation_->IsClosing()) {
130 animation_->Hide();
131 } else {
132 if (animation_->IsClosing()) {
133 // If we're in the middle of a close animation, skip immediately to the
134 // end of the animation.
135 StopAnimation();
136 } else {
137 // Otherwise we need to set both the animation state to ended and the
138 // DropdownBarHost state to ended/hidden, otherwise the next time we try
139 // to show the bar, it might refuse to do so. Note that we call
140 // AnimationEnded ourselves as Reset does not call it if we are not
141 // animating here.
142 animation_->Reset();
143 AnimationEnded(animation_.get());
148 void DropdownBarHost::StopAnimation() {
149 animation_->End();
152 bool DropdownBarHost::IsVisible() const {
153 return is_visible_;
156 ////////////////////////////////////////////////////////////////////////////////
157 // DropdownBarHost, views::FocusChangeListener implementation:
158 void DropdownBarHost::OnWillChangeFocus(views::View* focused_before,
159 views::View* focused_now) {
160 // First we need to determine if one or both of the views passed in are child
161 // views of our view.
162 bool our_view_before = focused_before && view_->Contains(focused_before);
163 bool our_view_now = focused_now && view_->Contains(focused_now);
165 // When both our_view_before and our_view_now are false, it means focus is
166 // changing hands elsewhere in the application (and we shouldn't do anything).
167 // Similarly, when both are true, focus is changing hands within the dropdown
168 // widget (and again, we should not do anything). We therefore only need to
169 // look at when we gain initial focus and when we loose it.
170 if (!our_view_before && our_view_now) {
171 // We are gaining focus from outside the dropdown widget so we must register
172 // a handler for Escape.
173 RegisterAccelerators();
174 } else if (our_view_before && !our_view_now) {
175 // We are losing focus to something outside our widget so we restore the
176 // original handler for Escape.
177 UnregisterAccelerators();
181 void DropdownBarHost::OnDidChangeFocus(views::View* focused_before,
182 views::View* focused_now) {
185 ////////////////////////////////////////////////////////////////////////////////
186 // DropdownBarHost, ui::AnimationDelegate implementation:
188 void DropdownBarHost::AnimationProgressed(const ui::Animation* animation) {
189 // First, we calculate how many pixels to slide the widget.
190 gfx::Size pref_size = view_->GetPreferredSize();
191 animation_offset_ = static_cast<int>((1.0 - animation_->GetCurrentValue()) *
192 pref_size.height());
194 // This call makes sure it appears in the right location, the size and shape
195 // is correct and that it slides in the right direction.
196 gfx::Rect dlg_rect = GetDialogPosition(gfx::Rect());
197 SetDialogPosition(dlg_rect, false);
199 // Let the view know if we are animating, and at which offset to draw the
200 // edges.
201 delegate_->SetAnimationOffset(animation_offset_);
202 view_->SchedulePaint();
205 void DropdownBarHost::AnimationEnded(const ui::Animation* animation) {
206 // Place the dropdown widget in its fully opened state.
207 animation_offset_ = 0;
209 if (!animation_->IsShowing()) {
210 // Animation has finished closing.
211 host_->Hide();
212 is_visible_ = false;
213 OnVisibilityChanged();
214 } else {
215 // Animation has finished opening.
219 ////////////////////////////////////////////////////////////////////////////////
220 // DropdownBarHost protected:
222 void DropdownBarHost::ResetFocusTracker() {
223 focus_tracker_.reset(NULL);
226 void DropdownBarHost::OnVisibilityChanged() {
229 void DropdownBarHost::GetWidgetBounds(gfx::Rect* bounds) {
230 DCHECK(bounds);
231 *bounds = browser_view_->bounds();
234 void DropdownBarHost::UpdateWindowEdges(const gfx::Rect& new_pos) {
235 // |w| is used to make it easier to create the part of the polygon that curves
236 // the right side of the Find window. It essentially keeps track of the
237 // x-pixel position of the right-most background image inside the view.
238 // TODO(finnur): Let the view tell us how to draw the curves or convert
239 // this to a CustomFrameWindow.
240 int w = new_pos.width() - 6; // -6 positions us at the left edge of the
241 // rightmost background image of the view.
242 int h = new_pos.height();
244 // This polygon array represents the outline of the background image for the
245 // window. Basically, it encompasses only the visible pixels of the
246 // concatenated find_dlg_LMR_bg images (where LMR = [left | middle | right]).
247 const Path::Point polygon[] = {
248 {2, 0}, {3, 1}, {3, h - 2}, {4, h - 1},
249 {4, h}, {w+0, h},
250 {w+2, h - 1}, {w+3, h - 2}, {w+3, 1}, {w+4, 0}
253 // Find the largest x and y value in the polygon.
254 int max_x = 0, max_y = 0;
255 for (size_t i = 0; i < arraysize(polygon); i++) {
256 max_x = std::max(max_x, static_cast<int>(polygon[i].x));
257 max_y = std::max(max_y, static_cast<int>(polygon[i].y));
260 // We then create the polygon and use SetWindowRgn to force the window to draw
261 // only within that area. This region may get reduced in size below.
262 Path path(polygon, arraysize(polygon));
263 ScopedPlatformRegion region(path.CreateNativeRegion());
264 // Are we animating?
265 if (animation_offset() > 0) {
266 // The animation happens in two steps: First, we clip the window and then in
267 // GetWidgetPosition we offset the window position so that it still looks
268 // attached to the toolbar as it grows. We clip the window by creating a
269 // rectangle region (that gradually increases as the animation progresses)
270 // and find the intersection between the two regions using CombineRgn.
272 // |y| shrinks as the animation progresses from the height of the view down
273 // to 0 (and reverses when closing).
274 int y = animation_offset();
275 // |y| shrinking means the animation (visible) region gets larger. In other
276 // words: the rectangle grows upward (when the widget is opening).
277 Path animation_path;
278 SkRect animation_rect = { SkIntToScalar(0), SkIntToScalar(y),
279 SkIntToScalar(max_x), SkIntToScalar(max_y) };
280 animation_path.addRect(animation_rect);
281 ScopedPlatformRegion animation_region(
282 animation_path.CreateNativeRegion());
283 region.Set(Path::IntersectRegions(animation_region.Get(), region.Get()));
285 // Next, we need to increase the region a little bit to account for the
286 // curved edges that the view will draw to make it look like grows out of
287 // the toolbar.
288 Path::Point left_curve[] = {
289 {2, y+0}, {3, y+1}, {3, y+0}, {2, y+0}
291 Path::Point right_curve[] = {
292 {w+3, y+1}, {w+4, y+0}, {w+3, y+0}, {w+3, y+1}
295 // Combine the region for the curve on the left with our main region.
296 Path left_path(left_curve, arraysize(left_curve));
297 ScopedPlatformRegion r(left_path.CreateNativeRegion());
298 region.Set(Path::CombineRegions(r.Get(), region.Get()));
300 // Combine the region for the curve on the right with our main region.
301 Path right_path(right_curve, arraysize(right_curve));
302 region.Set(Path::CombineRegions(r.Get(), region.Get()));
305 // Now see if we need to truncate the region because parts of it obscures
306 // the main window border.
307 gfx::Rect widget_bounds;
308 GetWidgetBounds(&widget_bounds);
310 // Calculate how much our current position overlaps our boundaries. If we
311 // overlap, it means we have too little space to draw the whole widget and
312 // we allow overwriting the scrollbar before we start truncating our widget.
314 // TODO(brettw) this constant is evil. This is the amount of room we've added
315 // to the window size, when we set the region, it can change the size.
316 static const int kAddedWidth = 7;
317 int difference = new_pos.right() - kAddedWidth - widget_bounds.right() -
318 gfx::scrollbar_size() + 1;
319 if (difference > 0) {
320 Path::Point exclude[4];
321 exclude[0].x = max_x - difference; // Top left corner.
322 exclude[0].y = 0;
324 exclude[1].x = max_x; // Top right corner.
325 exclude[1].y = 0;
327 exclude[2].x = max_x; // Bottom right corner.
328 exclude[2].y = max_y;
330 exclude[3].x = max_x - difference; // Bottom left corner.
331 exclude[3].y = max_y;
333 // Subtract this region from the original region.
334 gfx::Path exclude_path(exclude, arraysize(exclude));
335 ScopedPlatformRegion exclude_region(exclude_path.CreateNativeRegion());
336 region.Set(Path::SubtractRegion(region.Get(), exclude_region.Get()));
339 // Window takes ownership of the region.
340 host()->SetShape(region.release());
343 void DropdownBarHost::RegisterAccelerators() {
344 DCHECK(!esc_accel_target_registered_);
345 ui::Accelerator escape(ui::VKEY_ESCAPE, ui::EF_NONE);
346 focus_manager_->RegisterAccelerator(
347 escape, ui::AcceleratorManager::kNormalPriority, this);
348 esc_accel_target_registered_ = true;
351 void DropdownBarHost::UnregisterAccelerators() {
352 DCHECK(esc_accel_target_registered_);
353 ui::Accelerator escape(ui::VKEY_ESCAPE, ui::EF_NONE);
354 focus_manager_->UnregisterAccelerator(escape, this);
355 esc_accel_target_registered_ = false;