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/frame/browser_frame.h"
7 #include "base/debug/leak_annotations.h"
8 #include "base/i18n/rtl.h"
9 #include "chrome/browser/app_mode/app_mode_utils.h"
10 #include "chrome/browser/themes/theme_service.h"
11 #include "chrome/browser/themes/theme_service_factory.h"
12 #include "chrome/browser/ui/browser.h"
13 #include "chrome/browser/ui/browser_list.h"
14 #include "chrome/browser/ui/browser_window_state.h"
15 #include "chrome/browser/ui/views/frame/browser_non_client_frame_view.h"
16 #include "chrome/browser/ui/views/frame/browser_root_view.h"
17 #include "chrome/browser/ui/views/frame/browser_view.h"
18 #include "chrome/browser/ui/views/frame/immersive_mode_controller.h"
19 #include "chrome/browser/ui/views/frame/native_browser_frame.h"
20 #include "chrome/browser/ui/views/frame/native_browser_frame_factory.h"
21 #include "chrome/browser/ui/views/frame/system_menu_model_builder.h"
22 #include "chrome/browser/ui/views/frame/top_container_view.h"
23 #include "ui/base/hit_test.h"
24 #include "ui/base/theme_provider.h"
25 #include "ui/events/event_handler.h"
26 #include "ui/gfx/font_list.h"
27 #include "ui/gfx/screen.h"
28 #include "ui/views/controls/menu/menu_runner.h"
29 #include "ui/views/widget/native_widget.h"
31 #if defined(OS_CHROMEOS)
32 #include "ash/session/session_state_delegate.h"
33 #include "ash/shell.h"
37 #include "chrome/browser/ui/views/frame/browser_command_handler_linux.h"
40 ////////////////////////////////////////////////////////////////////////////////
41 // BrowserFrame, public:
43 BrowserFrame::BrowserFrame(BrowserView
* browser_view
)
44 : native_browser_frame_(nullptr),
46 browser_frame_view_(nullptr),
47 browser_view_(browser_view
),
48 theme_provider_(ThemeServiceFactory::GetForProfile(
49 browser_view_
->browser()->profile())) {
50 browser_view_
->set_frame(this);
51 set_is_secondary_widget(false);
52 // Don't focus anything on creation, selecting a tab will set the focus.
53 set_focus_on_creation(false);
56 BrowserFrame::~BrowserFrame() {
60 const gfx::FontList
& BrowserFrame::GetTitleFontList() {
61 static const gfx::FontList
* title_font_list
= new gfx::FontList();
62 ANNOTATE_LEAKING_OBJECT_PTR(title_font_list
);
63 return *title_font_list
;
66 void BrowserFrame::InitBrowserFrame() {
67 native_browser_frame_
=
68 NativeBrowserFrameFactory::CreateNativeBrowserFrame(this, browser_view_
);
69 views::Widget::InitParams params
= native_browser_frame_
->GetWidgetParams();
70 params
.delegate
= browser_view_
;
71 if (browser_view_
->browser()->is_type_tabbed()) {
72 // Typed panel/popup can only return a size once the widget has been
74 chrome::GetSavedWindowBoundsAndShowState(browser_view_
->browser(),
80 if (!native_browser_frame_
->UsesNativeSystemMenu()) {
81 DCHECK(non_client_view());
82 non_client_view()->set_context_menu_controller(this);
86 browser_command_handler_
.reset(new BrowserCommandHandlerLinux(browser_view_
));
90 void BrowserFrame::SetThemeProvider(scoped_ptr
<ui::ThemeProvider
> provider
) {
91 owned_theme_provider_
= provider
.Pass();
92 theme_provider_
= owned_theme_provider_
.get();
95 int BrowserFrame::GetMinimizeButtonOffset() const {
96 return native_browser_frame_
->GetMinimizeButtonOffset();
99 gfx::Rect
BrowserFrame::GetBoundsForTabStrip(views::View
* tabstrip
) const {
100 // This can be invoked before |browser_frame_view_| has been set.
101 return browser_frame_view_
?
102 browser_frame_view_
->GetBoundsForTabStrip(tabstrip
) : gfx::Rect();
105 int BrowserFrame::GetTopInset() const {
106 return browser_frame_view_
->GetTopInset();
109 int BrowserFrame::GetThemeBackgroundXInset() const {
110 return browser_frame_view_
->GetThemeBackgroundXInset();
113 void BrowserFrame::UpdateThrobber(bool running
) {
114 browser_frame_view_
->UpdateThrobber(running
);
117 void BrowserFrame::UpdateToolbar() {
118 browser_frame_view_
->UpdateToolbar();
121 views::View
* BrowserFrame::GetLocationIconView() const {
122 return browser_frame_view_
->GetLocationIconView();
125 views::View
* BrowserFrame::GetFrameView() const {
126 return browser_frame_view_
;
129 bool BrowserFrame::UseCustomFrame() const {
130 return native_browser_frame_
->UseCustomFrame();
133 bool BrowserFrame::ShouldSaveWindowPlacement() const {
134 return native_browser_frame_
->ShouldSaveWindowPlacement();
137 void BrowserFrame::GetWindowPlacement(gfx::Rect
* bounds
,
138 ui::WindowShowState
* show_state
) const {
139 return native_browser_frame_
->GetWindowPlacement(bounds
, show_state
);
142 ///////////////////////////////////////////////////////////////////////////////
143 // BrowserFrame, views::Widget overrides:
145 views::internal::RootView
* BrowserFrame::CreateRootView() {
146 root_view_
= new BrowserRootView(browser_view_
, this);
150 views::NonClientFrameView
* BrowserFrame::CreateNonClientFrameView() {
151 browser_frame_view_
=
152 chrome::CreateBrowserNonClientFrameView(this, browser_view_
);
153 return browser_frame_view_
;
156 bool BrowserFrame::GetAccelerator(int command_id
,
157 ui::Accelerator
* accelerator
) const {
158 return browser_view_
->GetAccelerator(command_id
, accelerator
);
161 ui::ThemeProvider
* BrowserFrame::GetThemeProvider() const {
162 return theme_provider_
;
165 void BrowserFrame::SchedulePaintInRect(const gfx::Rect
& rect
) {
166 views::Widget::SchedulePaintInRect(rect
);
168 // Paint the frame caption area and window controls during immersive reveal.
170 browser_view_
->immersive_mode_controller()->IsRevealed()) {
171 // This function should not be reentrant because the TopContainerView
172 // paints to a layer for the duration of the immersive reveal.
173 views::View
* top_container
= browser_view_
->top_container();
174 CHECK(top_container
->layer());
175 top_container
->SchedulePaintInRect(rect
);
179 void BrowserFrame::OnNativeWidgetActivationChanged(bool active
) {
181 // When running under remote desktop, if the remote desktop client is not
182 // active on the users desktop, then none of the windows contained in the
183 // remote desktop will be activated. However, NativeWidget::Activate() will
184 // still bring this browser window to the foreground. We explicitly set
185 // ourselves as the last active browser window to ensure that we get treated
186 // as such by the rest of Chrome.
187 BrowserList::SetLastActive(browser_view_
->browser());
189 Widget::OnNativeWidgetActivationChanged(active
);
192 void BrowserFrame::ShowContextMenuForView(views::View
* source
,
194 ui::MenuSourceType source_type
) {
195 if (chrome::IsRunningInForcedAppMode())
198 // Only show context menu if point is in unobscured parts of browser, i.e.
199 // if NonClientHitTest returns :
200 // - HTCAPTION: in title bar or unobscured part of tabstrip
201 // - HTNOWHERE: as the name implies.
202 gfx::Point
point_in_view_coords(p
);
203 views::View::ConvertPointFromScreen(non_client_view(), &point_in_view_coords
);
204 int hit_test
= non_client_view()->NonClientHitTest(point_in_view_coords
);
205 if (hit_test
== HTCAPTION
|| hit_test
== HTNOWHERE
) {
206 menu_runner_
.reset(new views::MenuRunner(
207 GetSystemMenuModel(),
208 views::MenuRunner::HAS_MNEMONICS
| views::MenuRunner::CONTEXT_MENU
));
209 if (menu_runner_
->RunMenuAt(source
->GetWidget(),
211 gfx::Rect(p
, gfx::Size(0, 0)),
212 views::MENU_ANCHOR_TOPLEFT
,
214 views::MenuRunner::MENU_DELETED
) {
220 ui::MenuModel
* BrowserFrame::GetSystemMenuModel() {
221 #if defined(OS_CHROMEOS)
222 ash::SessionStateDelegate
* delegate
=
223 ash::Shell::GetInstance()->session_state_delegate();
224 if (delegate
&& delegate
->NumberOfLoggedInUsers() > 1) {
225 // In Multi user mode, the number of users as well as the order of users
226 // can change. Coming here we have more then one user and since the menu
227 // model contains the user information, it must get updated to show any
228 // changes happened since the last invocation.
229 menu_model_builder_
.reset();
232 if (!menu_model_builder_
.get()) {
233 menu_model_builder_
.reset(
234 new SystemMenuModelBuilder(browser_view_
, browser_view_
->browser()));
235 menu_model_builder_
->Init();
237 return menu_model_builder_
->menu_model();
240 AvatarMenuButton
* BrowserFrame::GetAvatarMenuButton() {
241 return browser_frame_view_
->avatar_button();
244 NewAvatarButton
* BrowserFrame::GetNewAvatarMenuButton() {
245 return browser_frame_view_
->new_avatar_button();
248 bool BrowserFrame::ShouldLeaveOffsetNearTopBorder() {
249 return !IsMaximized();