1 // Copyright (c) 2011 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 #ifndef CHROME_BROWSER_UI_VIEWS_DROPDOWN_BAR_HOST_H_
6 #define CHROME_BROWSER_UI_VIEWS_DROPDOWN_BAR_HOST_H_
8 #include "base/compiler_specific.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "content/public/browser/native_web_keyboard_event.h"
11 #include "ui/gfx/animation/animation_delegate.h"
12 #include "ui/gfx/geometry/rect.h"
13 #include "ui/gfx/native_widget_types.h"
14 #include "ui/views/focus/focus_manager.h"
17 class DropdownBarHostDelegate
;
18 class DropdownBarView
;
29 class ExternalFocusTracker
;
34 ////////////////////////////////////////////////////////////////////////////////
36 // The DropdownBarHost implements the container widget for the UI that
37 // is shown at the top of browser contents. It uses the implementation from
38 // dropdown_bar_host_aura.cc to draw its content and is responsible for showing,
39 // hiding, animating, closing, and moving the bar if needed, for example if the
40 // widget is obscuring the selection results in FindBar.
42 ////////////////////////////////////////////////////////////////////////////////
43 class DropdownBarHost
: public ui::AcceleratorTarget
,
44 public views::FocusChangeListener
,
45 public gfx::AnimationDelegate
{
47 explicit DropdownBarHost(BrowserView
* browser_view
);
48 ~DropdownBarHost() override
;
50 // Initializes the DropdownBarHost. This creates the widget that |view| paints
52 // |host_view| is the view whose position in the |browser_view_| view
53 // hierarchy determines the z-order of the widget relative to views with
54 // layers and views with associated NativeViews.
55 void Init(views::View
* host_view
,
57 DropdownBarHostDelegate
* delegate
);
59 // Whether we are animating the position of the dropdown widget.
60 bool IsAnimating() const;
61 // Returns true if the dropdown bar view is visible, or false otherwise.
62 bool IsVisible() const;
63 // Selects text in the entry field and set focus.
64 void SetFocusAndSelection();
65 // Stops the animation.
68 // Shows the dropdown bar.
69 virtual void Show(bool animate
);
70 // Hides the dropdown bar.
71 virtual void Hide(bool animate
);
73 // Returns the rectangle representing where to position the dropdown widget.
74 virtual gfx::Rect
GetDialogPosition(gfx::Rect avoid_overlapping_rect
) = 0;
76 // Moves the widget to the provided location, moves it to top
77 // in the z-order (HWND_TOP, not HWND_TOPMOST for windows) and shows
78 // the widget (if hidden).
79 virtual void SetDialogPosition(const gfx::Rect
& new_pos
) = 0;
81 // Overridden from views::FocusChangeListener:
82 void OnWillChangeFocus(views::View
* focused_before
,
83 views::View
* focused_now
) override
;
84 void OnDidChangeFocus(views::View
* focused_before
,
85 views::View
* focused_now
) override
;
87 // Overridden from ui::AcceleratorTarget:
88 bool AcceleratorPressed(const ui::Accelerator
& accelerator
) override
= 0;
89 bool CanHandleAccelerators() const override
= 0;
91 // gfx::AnimationDelegate implementation:
92 void AnimationProgressed(const gfx::Animation
* animation
) override
;
93 void AnimationEnded(const gfx::Animation
* animation
) override
;
95 // During testing we can disable animations by setting this flag to true,
96 // so that opening and closing the dropdown bar is shown instantly, instead of
97 // having to poll it while it animates to open/closed status.
98 static bool disable_animations_during_testing_
;
100 // Returns the browser view that the dropdown belongs to.
101 BrowserView
* browser_view() const { return browser_view_
; }
103 // Registers this class as the handler for when Escape is pressed. Once we
104 // loose focus we will unregister Escape and (any accelerators the derived
105 // classes registers by using overrides of RegisterAccelerators). See also:
106 // SetFocusChangeListener().
107 virtual void RegisterAccelerators();
109 // When we loose focus, we unregister all accelerator handlers. See also:
110 // SetFocusChangeListener().
111 virtual void UnregisterAccelerators();
114 // Called when the drop down bar visibility, aka the value of IsVisible(),
116 virtual void OnVisibilityChanged();
118 // Returns the dropdown bar view.
119 views::View
* view() const { return view_
; }
121 // Returns the focus tracker.
122 views::ExternalFocusTracker
* focus_tracker() const {
123 return focus_tracker_
.get();
126 // Resets the focus tracker.
127 void ResetFocusTracker();
129 // The focus manager we register with to keep track of focus changes.
130 views::FocusManager
* focus_manager() const { return focus_manager_
; }
132 // Returns the host widget.
133 views::Widget
* host() const { return host_
.get(); }
135 // Returns the animation offset.
136 int animation_offset() const { return animation_offset_
; }
138 // Retrieves the boundary that the dropdown widget has to work with
139 // within the Chrome frame window. The boundary differs depending on
140 // the dropdown bar implementation. The default implementation
141 // returns the boundary of browser_view and the drop down
142 // can be shown in any client area.
143 virtual void GetWidgetBounds(gfx::Rect
* bounds
);
145 // Returns the animation for the dropdown.
146 gfx::SlideAnimation
* animation() {
147 return animation_
.get();
151 // Set the view whose position in the |browser_view_| view hierarchy
152 // determines the z-order of |host_| relative to views with layers and
153 // views with associated NativeViews.
154 void SetHostViewNative(views::View
* host_view
);
156 // The BrowserView that created us.
157 BrowserView
* browser_view_
;
159 // Our view, which is responsible for drawing the UI.
161 DropdownBarHostDelegate
* delegate_
;
163 // The y position pixel offset of the widget while animating the
165 int animation_offset_
;
167 // The animation class to use when opening the Dropdown widget.
168 scoped_ptr
<gfx::SlideAnimation
> animation_
;
170 // The focus manager we register with to keep track of focus changes.
171 views::FocusManager
* focus_manager_
;
173 // True if the accelerator target for Esc key is registered.
174 bool esc_accel_target_registered_
;
176 // Tracks and stores the last focused view which is not the DropdownBarView
177 // or any of its children. Used to restore focus once the DropdownBarView is
179 scoped_ptr
<views::ExternalFocusTracker
> focus_tracker_
;
181 // Host is the Widget implementation that is created and maintained by the
182 // dropdown bar. It contains the DropdownBarView.
183 scoped_ptr
<views::Widget
> host_
;
185 // A flag to manually manage visibility. GTK/X11 is asynchronous and
186 // the state of the widget can be out of sync.
189 DISALLOW_COPY_AND_ASSIGN(DropdownBarHost
);
192 #endif // CHROME_BROWSER_UI_VIEWS_DROPDOWN_BAR_HOST_H_