Infobar material design refresh: layout
[chromium-blink-merge.git] / chrome / browser / ui / views / apps / chrome_native_app_window_views.cc
blob4f1635a7e237c41135e87f301c63875430975d04
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 "chrome/app/chrome_command_ids.h"
9 #include "chrome/browser/app_mode/app_mode_utils.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/ui/views/apps/desktop_keyboard_capture.h"
12 #include "chrome/browser/ui/views/extensions/extension_keybinding_registry_views.h"
13 #include "chrome/browser/ui/views/frame/taskbar_decorator.h"
14 #include "components/favicon/content/content_favicon_driver.h"
15 #include "components/ui/zoom/page_zoom.h"
16 #include "components/ui/zoom/zoom_controller.h"
17 #include "ui/views/controls/webview/webview.h"
18 #include "ui/views/widget/widget.h"
20 using extensions::AppWindow;
22 namespace {
24 const int kMinPanelWidth = 100;
25 const int kMinPanelHeight = 100;
26 const int kDefaultPanelWidth = 200;
27 const int kDefaultPanelHeight = 300;
29 struct AcceleratorMapping {
30 ui::KeyboardCode keycode;
31 int modifiers;
32 int command_id;
35 const AcceleratorMapping kAppWindowAcceleratorMap[] = {
36 { ui::VKEY_W, ui::EF_CONTROL_DOWN, IDC_CLOSE_WINDOW },
37 { ui::VKEY_W, ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN, IDC_CLOSE_WINDOW },
38 { ui::VKEY_F4, ui::EF_ALT_DOWN, IDC_CLOSE_WINDOW },
41 // These accelerators will only be available in kiosk mode. These allow the
42 // user to manually zoom app windows. This is only necessary in kiosk mode
43 // (in normal mode, the user can zoom via the screen magnifier).
44 // TODO(xiyuan): Write a test for kiosk accelerators.
45 const AcceleratorMapping kAppWindowKioskAppModeAcceleratorMap[] = {
46 { ui::VKEY_OEM_MINUS, ui::EF_CONTROL_DOWN, IDC_ZOOM_MINUS },
47 { ui::VKEY_OEM_MINUS, ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN,
48 IDC_ZOOM_MINUS },
49 { ui::VKEY_SUBTRACT, ui::EF_CONTROL_DOWN, IDC_ZOOM_MINUS },
50 { ui::VKEY_OEM_PLUS, ui::EF_CONTROL_DOWN, IDC_ZOOM_PLUS },
51 { ui::VKEY_OEM_PLUS, ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN, IDC_ZOOM_PLUS },
52 { ui::VKEY_ADD, ui::EF_CONTROL_DOWN, IDC_ZOOM_PLUS },
53 { ui::VKEY_0, ui::EF_CONTROL_DOWN, IDC_ZOOM_NORMAL },
54 { ui::VKEY_NUMPAD0, ui::EF_CONTROL_DOWN, IDC_ZOOM_NORMAL },
57 void AddAcceleratorsFromMapping(const AcceleratorMapping mapping[],
58 size_t mapping_length,
59 std::map<ui::Accelerator, int>* accelerators) {
60 for (size_t i = 0; i < mapping_length; ++i) {
61 ui::Accelerator accelerator(mapping[i].keycode, mapping[i].modifiers);
62 (*accelerators)[accelerator] = mapping[i].command_id;
66 const std::map<ui::Accelerator, int>& GetAcceleratorTable() {
67 typedef std::map<ui::Accelerator, int> AcceleratorMap;
68 CR_DEFINE_STATIC_LOCAL(AcceleratorMap, accelerators, ());
69 if (!chrome::IsRunningInForcedAppMode()) {
70 if (accelerators.empty()) {
71 AddAcceleratorsFromMapping(
72 kAppWindowAcceleratorMap,
73 arraysize(kAppWindowAcceleratorMap),
74 &accelerators);
76 return accelerators;
79 CR_DEFINE_STATIC_LOCAL(AcceleratorMap, app_mode_accelerators, ());
80 if (app_mode_accelerators.empty()) {
81 AddAcceleratorsFromMapping(
82 kAppWindowAcceleratorMap,
83 arraysize(kAppWindowAcceleratorMap),
84 &app_mode_accelerators);
85 AddAcceleratorsFromMapping(
86 kAppWindowKioskAppModeAcceleratorMap,
87 arraysize(kAppWindowKioskAppModeAcceleratorMap),
88 &app_mode_accelerators);
90 return app_mode_accelerators;
93 } // namespace
95 ChromeNativeAppWindowViews::ChromeNativeAppWindowViews()
96 : has_frame_color_(false),
97 active_frame_color_(SK_ColorBLACK),
98 inactive_frame_color_(SK_ColorBLACK) {
101 ChromeNativeAppWindowViews::~ChromeNativeAppWindowViews() {}
103 void ChromeNativeAppWindowViews::OnBeforeWidgetInit(
104 const AppWindow::CreateParams& create_params,
105 views::Widget::InitParams* init_params,
106 views::Widget* widget) {
109 void ChromeNativeAppWindowViews::OnBeforePanelWidgetInit(
110 bool use_default_bounds,
111 views::Widget::InitParams* init_params,
112 views::Widget* widget) {
115 void ChromeNativeAppWindowViews::InitializeDefaultWindow(
116 const AppWindow::CreateParams& create_params) {
117 views::Widget::InitParams init_params(views::Widget::InitParams::TYPE_WINDOW);
118 init_params.delegate = this;
119 init_params.remove_standard_frame = IsFrameless() || has_frame_color_;
120 init_params.use_system_default_icon = true;
121 if (create_params.alpha_enabled) {
122 init_params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW;
124 // The given window is most likely not rectangular since it uses
125 // transparency and has no standard frame, don't show a shadow for it.
126 // TODO(skuhne): If we run into an application which should have a shadow
127 // but does not have, a new attribute has to be added.
128 if (IsFrameless())
129 init_params.shadow_type = views::Widget::InitParams::SHADOW_TYPE_NONE;
131 init_params.keep_on_top = create_params.always_on_top;
132 init_params.visible_on_all_workspaces =
133 create_params.visible_on_all_workspaces;
135 OnBeforeWidgetInit(create_params, &init_params, widget());
136 widget()->Init(init_params);
138 // The frame insets are required to resolve the bounds specifications
139 // correctly. So we set the window bounds and constraints now.
140 gfx::Insets frame_insets = GetFrameInsets();
141 gfx::Rect window_bounds = create_params.GetInitialWindowBounds(frame_insets);
142 SetContentSizeConstraints(create_params.GetContentMinimumSize(frame_insets),
143 create_params.GetContentMaximumSize(frame_insets));
144 if (!window_bounds.IsEmpty()) {
145 using BoundsSpecification = AppWindow::BoundsSpecification;
146 bool position_specified =
147 window_bounds.x() != BoundsSpecification::kUnspecifiedPosition &&
148 window_bounds.y() != BoundsSpecification::kUnspecifiedPosition;
149 if (!position_specified)
150 widget()->CenterWindow(window_bounds.size());
151 else
152 widget()->SetBounds(window_bounds);
155 #if defined(OS_CHROMEOS)
156 if (create_params.is_ime_window)
157 return;
158 #endif
160 // Register accelarators supported by app windows.
161 // TODO(jeremya/stevenjb): should these be registered for panels too?
162 views::FocusManager* focus_manager = GetFocusManager();
163 const std::map<ui::Accelerator, int>& accelerator_table =
164 GetAcceleratorTable();
165 const bool is_kiosk_app_mode = chrome::IsRunningInForcedAppMode();
167 // Ensures that kiosk mode accelerators are enabled when in kiosk mode (to be
168 // future proof). This is needed because GetAcceleratorTable() uses a static
169 // to store data and only checks kiosk mode once. If a platform app is
170 // launched before kiosk mode starts, the kiosk accelerators will not be
171 // registered. This CHECK catches the case.
172 CHECK(!is_kiosk_app_mode ||
173 accelerator_table.size() ==
174 arraysize(kAppWindowAcceleratorMap) +
175 arraysize(kAppWindowKioskAppModeAcceleratorMap));
177 // Ensure there is a ZoomController in kiosk mode, otherwise the processing
178 // of the accelerators will cause a crash. Note CHECK here because DCHECK
179 // will not be noticed, as this could only be relevant on real hardware.
180 CHECK(!is_kiosk_app_mode ||
181 ui_zoom::ZoomController::FromWebContents(web_view()->GetWebContents()));
183 for (std::map<ui::Accelerator, int>::const_iterator iter =
184 accelerator_table.begin();
185 iter != accelerator_table.end(); ++iter) {
186 if (is_kiosk_app_mode && !chrome::IsCommandAllowedInAppMode(iter->second))
187 continue;
189 focus_manager->RegisterAccelerator(
190 iter->first, ui::AcceleratorManager::kNormalPriority, this);
194 void ChromeNativeAppWindowViews::InitializePanelWindow(
195 const AppWindow::CreateParams& create_params) {
196 views::Widget::InitParams params(views::Widget::InitParams::TYPE_PANEL);
197 params.delegate = this;
199 gfx::Rect initial_window_bounds =
200 create_params.GetInitialWindowBounds(gfx::Insets());
201 preferred_size_ = gfx::Size(initial_window_bounds.width(),
202 initial_window_bounds.height());
203 if (preferred_size_.width() == 0)
204 preferred_size_.set_width(kDefaultPanelWidth);
205 else if (preferred_size_.width() < kMinPanelWidth)
206 preferred_size_.set_width(kMinPanelWidth);
208 if (preferred_size_.height() == 0)
209 preferred_size_.set_height(kDefaultPanelHeight);
210 else if (preferred_size_.height() < kMinPanelHeight)
211 preferred_size_.set_height(kMinPanelHeight);
213 // When a panel is not docked it will be placed at a default origin in the
214 // currently active target root window.
215 bool use_default_bounds = create_params.state != ui::SHOW_STATE_DOCKED;
216 // Sanitize initial origin reseting it in case it was not specified.
217 using BoundsSpecification = AppWindow::BoundsSpecification;
218 bool position_specified =
219 initial_window_bounds.x() != BoundsSpecification::kUnspecifiedPosition &&
220 initial_window_bounds.y() != BoundsSpecification::kUnspecifiedPosition;
221 params.bounds = (use_default_bounds || !position_specified) ?
222 gfx::Rect(preferred_size_) :
223 gfx::Rect(initial_window_bounds.origin(), preferred_size_);
224 OnBeforePanelWidgetInit(use_default_bounds, &params, widget());
225 widget()->Init(params);
226 widget()->set_focus_on_creation(create_params.focused);
229 views::NonClientFrameView*
230 ChromeNativeAppWindowViews::CreateStandardDesktopAppFrame() {
231 return views::WidgetDelegateView::CreateNonClientFrameView(widget());
234 // ui::BaseWindow implementation.
236 gfx::Rect ChromeNativeAppWindowViews::GetRestoredBounds() const {
237 return widget()->GetRestoredBounds();
240 ui::WindowShowState ChromeNativeAppWindowViews::GetRestoredState() const {
241 if (IsMaximized())
242 return ui::SHOW_STATE_MAXIMIZED;
243 if (IsFullscreen())
244 return ui::SHOW_STATE_FULLSCREEN;
246 return ui::SHOW_STATE_NORMAL;
249 bool ChromeNativeAppWindowViews::IsAlwaysOnTop() const {
250 // TODO(jackhou): On Mac, only docked panels are always-on-top.
251 return app_window()->window_type_is_panel() || widget()->IsAlwaysOnTop();
254 // views::WidgetDelegate implementation.
256 gfx::ImageSkia ChromeNativeAppWindowViews::GetWindowAppIcon() {
257 gfx::Image app_icon = app_window()->app_icon();
258 if (app_icon.IsEmpty())
259 return GetWindowIcon();
260 else
261 return *app_icon.ToImageSkia();
264 gfx::ImageSkia ChromeNativeAppWindowViews::GetWindowIcon() {
265 content::WebContents* web_contents = app_window()->web_contents();
266 if (web_contents) {
267 favicon::FaviconDriver* favicon_driver =
268 favicon::ContentFaviconDriver::FromWebContents(web_contents);
269 gfx::Image app_icon = favicon_driver->GetFavicon();
270 if (!app_icon.IsEmpty())
271 return *app_icon.ToImageSkia();
273 return gfx::ImageSkia();
276 views::NonClientFrameView* ChromeNativeAppWindowViews::CreateNonClientFrameView(
277 views::Widget* widget) {
278 return (IsFrameless() || has_frame_color_) ?
279 CreateNonStandardAppFrame() : CreateStandardDesktopAppFrame();
282 bool ChromeNativeAppWindowViews::WidgetHasHitTestMask() const {
283 return shape_ != NULL;
286 void ChromeNativeAppWindowViews::GetWidgetHitTestMask(gfx::Path* mask) const {
287 shape_->getBoundaryPath(mask);
290 // views::View implementation.
292 gfx::Size ChromeNativeAppWindowViews::GetPreferredSize() const {
293 if (!preferred_size_.IsEmpty())
294 return preferred_size_;
295 return NativeAppWindowViews::GetPreferredSize();
298 bool ChromeNativeAppWindowViews::AcceleratorPressed(
299 const ui::Accelerator& accelerator) {
300 const std::map<ui::Accelerator, int>& accelerator_table =
301 GetAcceleratorTable();
302 std::map<ui::Accelerator, int>::const_iterator iter =
303 accelerator_table.find(accelerator);
304 DCHECK(iter != accelerator_table.end());
305 int command_id = iter->second;
306 switch (command_id) {
307 case IDC_CLOSE_WINDOW:
308 Close();
309 return true;
310 case IDC_ZOOM_MINUS:
311 ui_zoom::PageZoom::Zoom(web_view()->GetWebContents(),
312 content::PAGE_ZOOM_OUT);
313 return true;
314 case IDC_ZOOM_NORMAL:
315 ui_zoom::PageZoom::Zoom(web_view()->GetWebContents(),
316 content::PAGE_ZOOM_RESET);
317 return true;
318 case IDC_ZOOM_PLUS:
319 ui_zoom::PageZoom::Zoom(web_view()->GetWebContents(),
320 content::PAGE_ZOOM_IN);
321 return true;
322 default:
323 NOTREACHED() << "Unknown accelerator sent to app window.";
325 return NativeAppWindowViews::AcceleratorPressed(accelerator);
328 // NativeAppWindow implementation.
330 void ChromeNativeAppWindowViews::SetFullscreen(int fullscreen_types) {
331 // Fullscreen not supported by panels.
332 if (app_window()->window_type_is_panel())
333 return;
335 widget()->SetFullscreen(fullscreen_types != AppWindow::FULLSCREEN_TYPE_NONE);
338 bool ChromeNativeAppWindowViews::IsFullscreenOrPending() const {
339 return widget()->IsFullscreen();
342 void ChromeNativeAppWindowViews::UpdateShape(scoped_ptr<SkRegion> region) {
343 shape_ = region.Pass();
344 widget()->SetShape(shape() ? new SkRegion(*shape()) : nullptr);
345 widget()->OnSizeConstraintsChanged();
348 bool ChromeNativeAppWindowViews::HasFrameColor() const {
349 return has_frame_color_;
352 SkColor ChromeNativeAppWindowViews::ActiveFrameColor() const {
353 return active_frame_color_;
356 SkColor ChromeNativeAppWindowViews::InactiveFrameColor() const {
357 return inactive_frame_color_;
360 void ChromeNativeAppWindowViews::SetInterceptAllKeys(bool want_all_keys) {
361 if (want_all_keys && (desktop_keyboard_capture_.get() == NULL)) {
362 desktop_keyboard_capture_.reset(new DesktopKeyboardCapture(widget()));
363 } else if (!want_all_keys) {
364 desktop_keyboard_capture_.reset(NULL);
368 // NativeAppWindowViews implementation.
370 void ChromeNativeAppWindowViews::InitializeWindow(
371 AppWindow* app_window,
372 const AppWindow::CreateParams& create_params) {
373 DCHECK(widget());
374 has_frame_color_ = create_params.has_frame_color;
375 active_frame_color_ = create_params.active_frame_color;
376 inactive_frame_color_ = create_params.inactive_frame_color;
377 if (create_params.window_type == AppWindow::WINDOW_TYPE_PANEL ||
378 create_params.window_type == AppWindow::WINDOW_TYPE_V1_PANEL) {
379 InitializePanelWindow(create_params);
380 } else {
381 InitializeDefaultWindow(create_params);
383 extension_keybinding_registry_.reset(new ExtensionKeybindingRegistryViews(
384 Profile::FromBrowserContext(app_window->browser_context()),
385 widget()->GetFocusManager(),
386 extensions::ExtensionKeybindingRegistry::PLATFORM_APPS_ONLY,
387 NULL));