Adding instrumentation to locate the source of jankiness
[chromium-blink-merge.git] / chrome / browser / ui / views / apps / chrome_native_app_window_views.cc
blob30309392c6d2eb15d0bcd2142a1963edbcc884df
1 // Copyright 2014 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/apps/chrome_native_app_window_views.h"
7 #include "apps/ui/views/app_window_frame_view.h"
8 #include "base/command_line.h"
9 #include "chrome/app/chrome_command_ids.h"
10 #include "chrome/browser/app_mode/app_mode_utils.h"
11 #include "chrome/browser/chrome_page_zoom.h"
12 #include "chrome/browser/favicon/favicon_tab_helper.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/ui/host_desktop.h"
15 #include "chrome/browser/ui/views/apps/shaped_app_window_targeter.h"
16 #include "chrome/browser/ui/views/extensions/extension_keybinding_registry_views.h"
17 #include "chrome/browser/ui/views/frame/taskbar_decorator.h"
18 #include "chrome/browser/ui/zoom/zoom_controller.h"
19 #include "chrome/browser/web_applications/web_app.h"
20 #include "chrome/common/chrome_switches.h"
21 #include "extensions/common/extension.h"
22 #include "ui/aura/window.h"
23 #include "ui/base/hit_test.h"
24 #include "ui/base/models/simple_menu_model.h"
25 #include "ui/gfx/image/image_skia.h"
26 #include "ui/views/controls/menu/menu_runner.h"
27 #include "ui/views/controls/webview/webview.h"
28 #include "ui/views/widget/widget.h"
29 #include "ui/wm/core/easy_resize_window_targeter.h"
30 #include "ui/wm/core/shadow_types.h"
32 #if defined(OS_LINUX)
33 #include "chrome/browser/shell_integration_linux.h"
34 #endif
36 #if defined(USE_ASH)
37 #include "ash/ash_constants.h"
38 #include "ash/ash_switches.h"
39 #include "ash/frame/custom_frame_view_ash.h"
40 #include "ash/screen_util.h"
41 #include "ash/shell.h"
42 #include "ash/wm/immersive_fullscreen_controller.h"
43 #include "ash/wm/panels/panel_frame_view.h"
44 #include "ash/wm/window_properties.h"
45 #include "ash/wm/window_state.h"
46 #include "ash/wm/window_state_delegate.h"
47 #include "ash/wm/window_state_observer.h"
48 #include "chrome/browser/ui/ash/ash_util.h"
49 #include "chrome/browser/ui/ash/multi_user/multi_user_context_menu.h"
50 #include "ui/aura/client/aura_constants.h"
51 #include "ui/aura/client/window_tree_client.h"
52 #include "ui/aura/window_observer.h"
53 #endif
55 #if defined(OS_CHROMEOS)
56 #include "ash/shell_window_ids.h"
57 #endif
59 using extensions::AppWindow;
61 namespace {
63 const int kMinPanelWidth = 100;
64 const int kMinPanelHeight = 100;
65 const int kDefaultPanelWidth = 200;
66 const int kDefaultPanelHeight = 300;
68 struct AcceleratorMapping {
69 ui::KeyboardCode keycode;
70 int modifiers;
71 int command_id;
74 const AcceleratorMapping kAppWindowAcceleratorMap[] = {
75 { ui::VKEY_W, ui::EF_CONTROL_DOWN, IDC_CLOSE_WINDOW },
76 { ui::VKEY_W, ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN, IDC_CLOSE_WINDOW },
77 { ui::VKEY_F4, ui::EF_ALT_DOWN, IDC_CLOSE_WINDOW },
80 // These accelerators will only be available in kiosk mode. These allow the
81 // user to manually zoom app windows. This is only necessary in kiosk mode
82 // (in normal mode, the user can zoom via the screen magnifier).
83 // TODO(xiyuan): Write a test for kiosk accelerators.
84 const AcceleratorMapping kAppWindowKioskAppModeAcceleratorMap[] = {
85 { ui::VKEY_OEM_MINUS, ui::EF_CONTROL_DOWN, IDC_ZOOM_MINUS },
86 { ui::VKEY_OEM_MINUS, ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN,
87 IDC_ZOOM_MINUS },
88 { ui::VKEY_SUBTRACT, ui::EF_CONTROL_DOWN, IDC_ZOOM_MINUS },
89 { ui::VKEY_OEM_PLUS, ui::EF_CONTROL_DOWN, IDC_ZOOM_PLUS },
90 { ui::VKEY_OEM_PLUS, ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN, IDC_ZOOM_PLUS },
91 { ui::VKEY_ADD, ui::EF_CONTROL_DOWN, IDC_ZOOM_PLUS },
92 { ui::VKEY_0, ui::EF_CONTROL_DOWN, IDC_ZOOM_NORMAL },
93 { ui::VKEY_NUMPAD0, ui::EF_CONTROL_DOWN, IDC_ZOOM_NORMAL },
96 void AddAcceleratorsFromMapping(const AcceleratorMapping mapping[],
97 size_t mapping_length,
98 std::map<ui::Accelerator, int>* accelerators) {
99 for (size_t i = 0; i < mapping_length; ++i) {
100 ui::Accelerator accelerator(mapping[i].keycode, mapping[i].modifiers);
101 (*accelerators)[accelerator] = mapping[i].command_id;
105 const std::map<ui::Accelerator, int>& GetAcceleratorTable() {
106 typedef std::map<ui::Accelerator, int> AcceleratorMap;
107 CR_DEFINE_STATIC_LOCAL(AcceleratorMap, accelerators, ());
108 if (accelerators.empty()) {
109 AddAcceleratorsFromMapping(
110 kAppWindowAcceleratorMap,
111 arraysize(kAppWindowAcceleratorMap),
112 &accelerators);
114 // Add accelerators for kiosk mode.
115 if (chrome::IsRunningInForcedAppMode()) {
116 AddAcceleratorsFromMapping(
117 kAppWindowKioskAppModeAcceleratorMap,
118 arraysize(kAppWindowKioskAppModeAcceleratorMap),
119 &accelerators);
122 return accelerators;
125 #if defined(USE_ASH)
126 // This class handles a user's fullscreen request (Shift+F4/F4).
127 class NativeAppWindowStateDelegate : public ash::wm::WindowStateDelegate,
128 public ash::wm::WindowStateObserver,
129 public aura::WindowObserver {
130 public:
131 NativeAppWindowStateDelegate(AppWindow* app_window,
132 extensions::NativeAppWindow* native_app_window)
133 : app_window_(app_window),
134 window_state_(
135 ash::wm::GetWindowState(native_app_window->GetNativeWindow())) {
136 // Add a window state observer to exit fullscreen properly in case
137 // fullscreen is exited without going through AppWindow::Restore(). This
138 // is the case when exiting immersive fullscreen via the "Restore" window
139 // control.
140 // TODO(pkotwicz): This is a hack. Remove ASAP. http://crbug.com/319048
141 window_state_->AddObserver(this);
142 window_state_->window()->AddObserver(this);
144 virtual ~NativeAppWindowStateDelegate() {
145 if (window_state_) {
146 window_state_->RemoveObserver(this);
147 window_state_->window()->RemoveObserver(this);
151 private:
152 // Overridden from ash::wm::WindowStateDelegate.
153 virtual bool ToggleFullscreen(ash::wm::WindowState* window_state) override {
154 // Windows which cannot be maximized should not be fullscreened.
155 DCHECK(window_state->IsFullscreen() || window_state->CanMaximize());
156 if (window_state->IsFullscreen())
157 app_window_->Restore();
158 else if (window_state->CanMaximize())
159 app_window_->OSFullscreen();
160 return true;
163 // Overridden from ash::wm::WindowStateObserver:
164 virtual void OnPostWindowStateTypeChange(
165 ash::wm::WindowState* window_state,
166 ash::wm::WindowStateType old_type) override {
167 // Since the window state might get set by a window manager, it is possible
168 // to come here before the application set its |BaseWindow|.
169 if (!window_state->IsFullscreen() && !window_state->IsMinimized() &&
170 app_window_->GetBaseWindow() &&
171 app_window_->GetBaseWindow()->IsFullscreenOrPending()) {
172 app_window_->Restore();
173 // Usually OnNativeWindowChanged() is called when the window bounds are
174 // changed as a result of a state type change. Because the change in state
175 // type has already occurred, we need to call OnNativeWindowChanged()
176 // explicitly.
177 app_window_->OnNativeWindowChanged();
181 // Overridden from aura::WindowObserver:
182 virtual void OnWindowDestroying(aura::Window* window) override {
183 window_state_->RemoveObserver(this);
184 window_state_->window()->RemoveObserver(this);
185 window_state_ = NULL;
188 // Not owned.
189 AppWindow* app_window_;
190 ash::wm::WindowState* window_state_;
192 DISALLOW_COPY_AND_ASSIGN(NativeAppWindowStateDelegate);
194 #endif // USE_ASH
196 } // namespace
198 ChromeNativeAppWindowViews::ChromeNativeAppWindowViews()
199 : is_fullscreen_(false),
200 has_frame_color_(false),
201 active_frame_color_(SK_ColorBLACK),
202 inactive_frame_color_(SK_ColorBLACK) {
205 ChromeNativeAppWindowViews::~ChromeNativeAppWindowViews() {}
207 void ChromeNativeAppWindowViews::OnBeforeWidgetInit(
208 views::Widget::InitParams* init_params,
209 views::Widget* widget) {}
211 void ChromeNativeAppWindowViews::InitializeDefaultWindow(
212 const AppWindow::CreateParams& create_params) {
213 std::string app_name = web_app::GenerateApplicationNameFromExtensionId(
214 app_window()->extension_id());
216 views::Widget::InitParams init_params(views::Widget::InitParams::TYPE_WINDOW);
217 init_params.delegate = this;
218 init_params.remove_standard_frame = IsFrameless() || has_frame_color_;
219 init_params.use_system_default_icon = true;
220 if (create_params.alpha_enabled)
221 init_params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW;
222 init_params.keep_on_top = create_params.always_on_top;
223 init_params.visible_on_all_workspaces =
224 create_params.visible_on_all_workspaces;
226 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
227 // Set up a custom WM_CLASS for app windows. This allows task switchers in
228 // X11 environments to distinguish them from main browser windows.
229 init_params.wm_class_name = web_app::GetWMClassFromAppName(app_name);
230 init_params.wm_class_class = shell_integration_linux::GetProgramClassName();
231 const char kX11WindowRoleApp[] = "app";
232 init_params.wm_role_name = std::string(kX11WindowRoleApp);
233 #endif
235 OnBeforeWidgetInit(&init_params, widget());
236 #if defined(OS_CHROMEOS)
237 if (create_params.is_ime_window) {
238 // Puts ime windows into ime window container.
239 init_params.parent =
240 ash::Shell::GetContainer(ash::Shell::GetPrimaryRootWindow(),
241 ash::kShellWindowId_ImeWindowParentContainer);
243 #endif
244 widget()->Init(init_params);
246 // The frame insets are required to resolve the bounds specifications
247 // correctly. So we set the window bounds and constraints now.
248 gfx::Insets frame_insets = GetFrameInsets();
249 gfx::Rect window_bounds = create_params.GetInitialWindowBounds(frame_insets);
250 SetContentSizeConstraints(create_params.GetContentMinimumSize(frame_insets),
251 create_params.GetContentMaximumSize(frame_insets));
252 if (!window_bounds.IsEmpty()) {
253 typedef AppWindow::BoundsSpecification BoundsSpecification;
254 bool position_specified =
255 window_bounds.x() != BoundsSpecification::kUnspecifiedPosition &&
256 window_bounds.y() != BoundsSpecification::kUnspecifiedPosition;
257 if (!position_specified)
258 widget()->CenterWindow(window_bounds.size());
259 else
260 widget()->SetBounds(window_bounds);
263 if (IsFrameless() &&
264 init_params.opacity == views::Widget::InitParams::TRANSLUCENT_WINDOW) {
265 // The given window is most likely not rectangular since it uses
266 // transparency and has no standard frame, don't show a shadow for it.
267 // TODO(skuhne): If we run into an application which should have a shadow
268 // but does not have, a new attribute has to be added.
269 wm::SetShadowType(widget()->GetNativeWindow(), wm::SHADOW_TYPE_NONE);
272 #if defined(OS_CHROMEOS)
273 if (create_params.is_ime_window)
274 return;
275 #endif
277 // Register accelarators supported by app windows.
278 // TODO(jeremya/stevenjb): should these be registered for panels too?
279 views::FocusManager* focus_manager = GetFocusManager();
280 const std::map<ui::Accelerator, int>& accelerator_table =
281 GetAcceleratorTable();
282 const bool is_kiosk_app_mode = chrome::IsRunningInForcedAppMode();
284 // Ensures that kiosk mode accelerators are enabled when in kiosk mode (to be
285 // future proof). This is needed because GetAcceleratorTable() uses a static
286 // to store data and only checks kiosk mode once. If a platform app is
287 // launched before kiosk mode starts, the kiosk accelerators will not be
288 // registered. This DCHECK catches the case.
289 DCHECK(!is_kiosk_app_mode ||
290 accelerator_table.size() ==
291 arraysize(kAppWindowAcceleratorMap) +
292 arraysize(kAppWindowKioskAppModeAcceleratorMap));
294 // Ensure there is a ZoomController in kiosk mode, otherwise the processing
295 // of the accelerators will cause a crash.
296 DCHECK(!is_kiosk_app_mode ||
297 ZoomController::FromWebContents(web_view()->GetWebContents()));
299 for (std::map<ui::Accelerator, int>::const_iterator iter =
300 accelerator_table.begin();
301 iter != accelerator_table.end(); ++iter) {
302 if (is_kiosk_app_mode && !chrome::IsCommandAllowedInAppMode(iter->second))
303 continue;
305 focus_manager->RegisterAccelerator(
306 iter->first, ui::AcceleratorManager::kNormalPriority, this);
310 void ChromeNativeAppWindowViews::InitializePanelWindow(
311 const AppWindow::CreateParams& create_params) {
312 views::Widget::InitParams params(views::Widget::InitParams::TYPE_PANEL);
313 params.delegate = this;
315 gfx::Rect initial_window_bounds =
316 create_params.GetInitialWindowBounds(gfx::Insets());
317 preferred_size_ = gfx::Size(initial_window_bounds.width(),
318 initial_window_bounds.height());
319 if (preferred_size_.width() == 0)
320 preferred_size_.set_width(kDefaultPanelWidth);
321 else if (preferred_size_.width() < kMinPanelWidth)
322 preferred_size_.set_width(kMinPanelWidth);
324 if (preferred_size_.height() == 0)
325 preferred_size_.set_height(kDefaultPanelHeight);
326 else if (preferred_size_.height() < kMinPanelHeight)
327 preferred_size_.set_height(kMinPanelHeight);
328 #if defined(USE_ASH)
329 if (ash::Shell::HasInstance()) {
330 // Open a new panel on the target root.
331 aura::Window* target = ash::Shell::GetTargetRootWindow();
332 params.bounds = ash::ScreenUtil::ConvertRectToScreen(
333 target, gfx::Rect(preferred_size_));
334 } else {
335 params.bounds = gfx::Rect(preferred_size_);
337 #else
338 params.bounds = gfx::Rect(preferred_size_);
339 #endif
340 widget()->Init(params);
341 widget()->set_focus_on_creation(create_params.focused);
344 views::NonClientFrameView*
345 ChromeNativeAppWindowViews::CreateStandardDesktopAppFrame() {
346 return views::WidgetDelegateView::CreateNonClientFrameView(widget());
349 apps::AppWindowFrameView*
350 ChromeNativeAppWindowViews::CreateNonStandardAppFrame() {
351 apps::AppWindowFrameView* frame =
352 new apps::AppWindowFrameView(widget(),
353 this,
354 has_frame_color_,
355 active_frame_color_,
356 inactive_frame_color_);
357 frame->Init();
358 #if defined(USE_ASH)
359 // For Aura windows on the Ash desktop the sizes are different and the user
360 // can resize the window from slightly outside the bounds as well.
361 if (chrome::IsNativeWindowInAsh(widget()->GetNativeWindow())) {
362 frame->SetResizeSizes(ash::kResizeInsideBoundsSize,
363 ash::kResizeOutsideBoundsSize,
364 ash::kResizeAreaCornerSize);
366 #endif
368 #if !defined(OS_CHROMEOS)
369 // For non-Ash windows, install an easy resize window targeter, which ensures
370 // that the root window (not the app) receives mouse events on the edges.
371 if (chrome::GetHostDesktopTypeForNativeWindow(widget()->GetNativeWindow()) !=
372 chrome::HOST_DESKTOP_TYPE_ASH) {
373 aura::Window* window = widget()->GetNativeWindow();
374 int resize_inside = frame->resize_inside_bounds_size();
375 gfx::Insets inset(
376 resize_inside, resize_inside, resize_inside, resize_inside);
377 // Add the EasyResizeWindowTargeter on the window, not its root window. The
378 // root window does not have a delegate, which is needed to handle the event
379 // in Linux.
380 window->SetEventTargeter(scoped_ptr<ui::EventTargeter>(
381 new wm::EasyResizeWindowTargeter(window, inset, inset)));
383 #endif
385 return frame;
388 // ui::BaseWindow implementation.
390 gfx::Rect ChromeNativeAppWindowViews::GetRestoredBounds() const {
391 #if defined(USE_ASH)
392 gfx::Rect* bounds = widget()->GetNativeWindow()->GetProperty(
393 ash::kRestoreBoundsOverrideKey);
394 if (bounds && !bounds->IsEmpty())
395 return *bounds;
396 #endif
397 return widget()->GetRestoredBounds();
400 ui::WindowShowState ChromeNativeAppWindowViews::GetRestoredState() const {
401 #if !defined(USE_ASH)
402 if (IsMaximized())
403 return ui::SHOW_STATE_MAXIMIZED;
404 if (IsFullscreen())
405 return ui::SHOW_STATE_FULLSCREEN;
406 #else
407 // Use kRestoreShowStateKey in case a window is minimized/hidden.
408 ui::WindowShowState restore_state = widget()->GetNativeWindow()->GetProperty(
409 aura::client::kRestoreShowStateKey);
410 if (widget()->GetNativeWindow()->GetProperty(
411 ash::kRestoreBoundsOverrideKey)) {
412 // If an override is given, we use that restore state (after filtering).
413 restore_state = widget()->GetNativeWindow()->GetProperty(
414 ash::kRestoreShowStateOverrideKey);
415 } else {
416 // Otherwise first normal states are checked.
417 if (IsMaximized())
418 return ui::SHOW_STATE_MAXIMIZED;
419 if (IsFullscreen()) {
420 if (immersive_fullscreen_controller_.get() &&
421 immersive_fullscreen_controller_->IsEnabled()) {
422 // Restore windows which were previously in immersive fullscreen to
423 // maximized. Restoring the window to a different fullscreen type
424 // makes for a bad experience.
425 return ui::SHOW_STATE_MAXIMIZED;
427 return ui::SHOW_STATE_FULLSCREEN;
430 // Whitelist states to return so that invalid and transient states
431 // are not saved and used to restore windows when they are recreated.
432 switch (restore_state) {
433 case ui::SHOW_STATE_NORMAL:
434 case ui::SHOW_STATE_MAXIMIZED:
435 case ui::SHOW_STATE_FULLSCREEN:
436 return restore_state;
438 case ui::SHOW_STATE_DEFAULT:
439 case ui::SHOW_STATE_MINIMIZED:
440 case ui::SHOW_STATE_INACTIVE:
441 case ui::SHOW_STATE_END:
442 return ui::SHOW_STATE_NORMAL;
444 #endif // !defined(USE_ASH)
445 return ui::SHOW_STATE_NORMAL;
448 bool ChromeNativeAppWindowViews::IsAlwaysOnTop() const {
449 if (app_window()->window_type_is_panel()) {
450 #if defined(USE_ASH)
451 return ash::wm::GetWindowState(widget()->GetNativeWindow())
452 ->panel_attached();
453 #else
454 return true;
455 #endif
456 } else {
457 return widget()->IsAlwaysOnTop();
461 // views::ContextMenuController implementation.
463 void ChromeNativeAppWindowViews::ShowContextMenuForView(
464 views::View* source,
465 const gfx::Point& p,
466 ui::MenuSourceType source_type) {
467 #if defined(USE_ASH) && defined(OS_CHROMEOS)
468 scoped_ptr<ui::MenuModel> model =
469 CreateMultiUserContextMenu(app_window()->GetNativeWindow());
470 if (!model.get())
471 return;
473 // Only show context menu if point is in caption.
474 gfx::Point point_in_view_coords(p);
475 views::View::ConvertPointFromScreen(widget()->non_client_view(),
476 &point_in_view_coords);
477 int hit_test =
478 widget()->non_client_view()->NonClientHitTest(point_in_view_coords);
479 if (hit_test == HTCAPTION) {
480 menu_runner_.reset(new views::MenuRunner(
481 model.get(),
482 views::MenuRunner::HAS_MNEMONICS | views::MenuRunner::CONTEXT_MENU));
483 if (menu_runner_->RunMenuAt(source->GetWidget(),
484 NULL,
485 gfx::Rect(p, gfx::Size(0, 0)),
486 views::MENU_ANCHOR_TOPLEFT,
487 source_type) ==
488 views::MenuRunner::MENU_DELETED) {
489 return;
492 #endif
495 // views::WidgetDelegate implementation.
497 gfx::ImageSkia ChromeNativeAppWindowViews::GetWindowAppIcon() {
498 gfx::Image app_icon = app_window()->app_icon();
499 if (app_icon.IsEmpty())
500 return GetWindowIcon();
501 else
502 return *app_icon.ToImageSkia();
505 gfx::ImageSkia ChromeNativeAppWindowViews::GetWindowIcon() {
506 content::WebContents* web_contents = app_window()->web_contents();
507 if (web_contents) {
508 FaviconTabHelper* favicon_tab_helper =
509 FaviconTabHelper::FromWebContents(web_contents);
510 gfx::Image app_icon = favicon_tab_helper->GetFavicon();
511 if (!app_icon.IsEmpty())
512 return *app_icon.ToImageSkia();
514 return gfx::ImageSkia();
517 views::NonClientFrameView* ChromeNativeAppWindowViews::CreateNonClientFrameView(
518 views::Widget* widget) {
519 #if defined(USE_ASH)
520 if (chrome::IsNativeViewInAsh(widget->GetNativeView())) {
521 // Set the delegate now because CustomFrameViewAsh sets the
522 // WindowStateDelegate if one is not already set.
523 ash::wm::GetWindowState(GetNativeWindow())->SetDelegate(
524 scoped_ptr<ash::wm::WindowStateDelegate>(
525 new NativeAppWindowStateDelegate(app_window(), this)).Pass());
527 if (app_window()->window_type_is_panel()) {
528 ash::PanelFrameView::FrameType frame_type = IsFrameless() ?
529 ash::PanelFrameView::FRAME_NONE : ash::PanelFrameView::FRAME_ASH;
530 views::NonClientFrameView* frame_view =
531 new ash::PanelFrameView(widget, frame_type);
532 frame_view->set_context_menu_controller(this);
533 return frame_view;
536 if (IsFrameless())
537 return CreateNonStandardAppFrame();
539 ash::CustomFrameViewAsh* custom_frame_view =
540 new ash::CustomFrameViewAsh(widget);
541 // Non-frameless app windows can be put into immersive fullscreen.
542 immersive_fullscreen_controller_.reset(
543 new ash::ImmersiveFullscreenController());
544 custom_frame_view->InitImmersiveFullscreenControllerForView(
545 immersive_fullscreen_controller_.get());
546 custom_frame_view->GetHeaderView()->set_context_menu_controller(this);
548 if (has_frame_color_) {
549 custom_frame_view->SetFrameColors(active_frame_color_,
550 inactive_frame_color_);
553 return custom_frame_view;
555 #endif
556 return (IsFrameless() || has_frame_color_) ?
557 CreateNonStandardAppFrame() : CreateStandardDesktopAppFrame();
560 bool ChromeNativeAppWindowViews::WidgetHasHitTestMask() const {
561 return shape_ != NULL;
564 void ChromeNativeAppWindowViews::GetWidgetHitTestMask(gfx::Path* mask) const {
565 shape_->getBoundaryPath(mask);
568 // views::View implementation.
570 gfx::Size ChromeNativeAppWindowViews::GetPreferredSize() const {
571 if (!preferred_size_.IsEmpty())
572 return preferred_size_;
573 return NativeAppWindowViews::GetPreferredSize();
576 bool ChromeNativeAppWindowViews::AcceleratorPressed(
577 const ui::Accelerator& accelerator) {
578 const std::map<ui::Accelerator, int>& accelerator_table =
579 GetAcceleratorTable();
580 std::map<ui::Accelerator, int>::const_iterator iter =
581 accelerator_table.find(accelerator);
582 DCHECK(iter != accelerator_table.end());
583 int command_id = iter->second;
584 switch (command_id) {
585 case IDC_CLOSE_WINDOW:
586 Close();
587 return true;
588 case IDC_ZOOM_MINUS:
589 chrome_page_zoom::Zoom(web_view()->GetWebContents(),
590 content::PAGE_ZOOM_OUT);
591 return true;
592 case IDC_ZOOM_NORMAL:
593 chrome_page_zoom::Zoom(web_view()->GetWebContents(),
594 content::PAGE_ZOOM_RESET);
595 return true;
596 case IDC_ZOOM_PLUS:
597 chrome_page_zoom::Zoom(web_view()->GetWebContents(),
598 content::PAGE_ZOOM_IN);
599 return true;
600 default:
601 NOTREACHED() << "Unknown accelerator sent to app window.";
603 return NativeAppWindowViews::AcceleratorPressed(accelerator);
606 // NativeAppWindow implementation.
608 void ChromeNativeAppWindowViews::SetFullscreen(int fullscreen_types) {
609 // Fullscreen not supported by panels.
610 if (app_window()->window_type_is_panel())
611 return;
612 is_fullscreen_ = (fullscreen_types != AppWindow::FULLSCREEN_TYPE_NONE);
613 widget()->SetFullscreen(is_fullscreen_);
615 // TODO(oshima): Remove USE_ATHENA once athena has its own NativeAppWindow.
616 #if defined(USE_ASH) && !defined(USE_ATHENA)
617 if (immersive_fullscreen_controller_.get()) {
618 // |immersive_fullscreen_controller_| should only be set if immersive
619 // fullscreen is the fullscreen type used by the OS.
620 immersive_fullscreen_controller_->SetEnabled(
621 ash::ImmersiveFullscreenController::WINDOW_TYPE_PACKAGED_APP,
622 (fullscreen_types & AppWindow::FULLSCREEN_TYPE_OS) != 0);
623 // Autohide the shelf instead of hiding the shelf completely when only in
624 // OS fullscreen.
625 ash::wm::WindowState* window_state =
626 ash::wm::GetWindowState(widget()->GetNativeWindow());
627 window_state->set_hide_shelf_when_fullscreen(fullscreen_types !=
628 AppWindow::FULLSCREEN_TYPE_OS);
629 DCHECK(ash::Shell::HasInstance());
630 ash::Shell::GetInstance()->UpdateShelfVisibility();
632 #endif
634 // TODO(jeremya) we need to call RenderViewHost::ExitFullscreen() if we
635 // ever drop the window out of fullscreen in response to something that
636 // wasn't the app calling webkitCancelFullScreen().
639 bool ChromeNativeAppWindowViews::IsFullscreenOrPending() const {
640 return is_fullscreen_;
643 void ChromeNativeAppWindowViews::UpdateBadgeIcon() {
644 const gfx::Image* icon = NULL;
645 if (!app_window()->badge_icon().IsEmpty()) {
646 icon = &app_window()->badge_icon();
647 // chrome::DrawTaskbarDecoration can do interesting things with non-square
648 // bitmaps.
649 // TODO(benwells): Refactor chrome::DrawTaskbarDecoration to not be avatar
650 // specific, and lift this restriction.
651 if (icon->Width() != icon->Height()) {
652 LOG(ERROR) << "Attempt to set a non-square badge; request ignored.";
653 return;
656 chrome::DrawTaskbarDecoration(GetNativeWindow(), icon);
659 void ChromeNativeAppWindowViews::UpdateShape(scoped_ptr<SkRegion> region) {
660 bool had_shape = shape_;
661 shape_ = region.Pass();
663 aura::Window* native_window = widget()->GetNativeWindow();
664 if (shape_) {
665 widget()->SetShape(new SkRegion(*shape_));
666 if (!had_shape) {
667 native_window->SetEventTargeter(scoped_ptr<ui::EventTargeter>(
668 new ShapedAppWindowTargeter(native_window, this)));
670 } else {
671 widget()->SetShape(NULL);
672 if (had_shape)
673 native_window->SetEventTargeter(scoped_ptr<ui::EventTargeter>());
675 widget()->OnSizeConstraintsChanged();
678 bool ChromeNativeAppWindowViews::HasFrameColor() const {
679 return has_frame_color_;
682 SkColor ChromeNativeAppWindowViews::ActiveFrameColor() const {
683 return active_frame_color_;
686 SkColor ChromeNativeAppWindowViews::InactiveFrameColor() const {
687 return inactive_frame_color_;
690 // NativeAppWindowViews implementation.
692 void ChromeNativeAppWindowViews::InitializeWindow(
693 AppWindow* app_window,
694 const AppWindow::CreateParams& create_params) {
695 DCHECK(widget());
696 has_frame_color_ = create_params.has_frame_color;
697 active_frame_color_ = create_params.active_frame_color;
698 inactive_frame_color_ = create_params.inactive_frame_color;
699 if (create_params.window_type == AppWindow::WINDOW_TYPE_PANEL ||
700 create_params.window_type == AppWindow::WINDOW_TYPE_V1_PANEL) {
701 InitializePanelWindow(create_params);
702 } else {
703 InitializeDefaultWindow(create_params);
705 extension_keybinding_registry_.reset(new ExtensionKeybindingRegistryViews(
706 Profile::FromBrowserContext(app_window->browser_context()),
707 widget()->GetFocusManager(),
708 extensions::ExtensionKeybindingRegistry::PLATFORM_APPS_ONLY,
709 NULL));