OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "apps/ui/views/native_app_window_views.h" | |
6 | |
7 #include "base/threading/sequenced_worker_pool.h" | |
8 #include "content/public/browser/render_view_host.h" | |
9 #include "content/public/browser/render_widget_host_view.h" | |
10 #include "content/public/browser/web_contents.h" | |
11 #include "extensions/browser/app_window/app_window.h" | |
12 #include "extensions/common/draggable_region.h" | |
13 #include "third_party/skia/include/core/SkRegion.h" | |
14 #include "ui/gfx/path.h" | |
15 #include "ui/views/controls/webview/webview.h" | |
16 #include "ui/views/widget/widget.h" | |
17 #include "ui/views/window/non_client_view.h" | |
18 | |
19 #if defined(USE_AURA) | |
20 #include "ui/aura/window.h" | |
21 #endif | |
22 | |
23 using extensions::AppWindow; | |
24 | |
25 namespace apps { | |
26 | |
27 NativeAppWindowViews::NativeAppWindowViews() | |
28 : app_window_(NULL), | |
29 web_view_(NULL), | |
30 widget_(NULL), | |
31 frameless_(false), | |
32 resizable_(false) {} | |
33 | |
34 void NativeAppWindowViews::Init(AppWindow* app_window, | |
35 const AppWindow::CreateParams& create_params) { | |
36 app_window_ = app_window; | |
37 frameless_ = create_params.frame == AppWindow::FRAME_NONE; | |
38 resizable_ = create_params.resizable; | |
39 size_constraints_.set_minimum_size( | |
40 create_params.GetContentMinimumSize(gfx::Insets())); | |
41 size_constraints_.set_maximum_size( | |
42 create_params.GetContentMaximumSize(gfx::Insets())); | |
43 Observe(app_window_->web_contents()); | |
44 | |
45 widget_ = new views::Widget; | |
46 InitializeWindow(app_window, create_params); | |
47 | |
48 OnViewWasResized(); | |
49 widget_->AddObserver(this); | |
50 } | |
51 | |
52 NativeAppWindowViews::~NativeAppWindowViews() { | |
53 web_view_->SetWebContents(NULL); | |
54 } | |
55 | |
56 void NativeAppWindowViews::OnCanHaveAlphaEnabledChanged() { | |
57 app_window_->OnNativeWindowChanged(); | |
58 } | |
59 | |
60 void NativeAppWindowViews::InitializeWindow( | |
61 AppWindow* app_window, | |
62 const AppWindow::CreateParams& create_params) { | |
63 // Stub implementation. See also ChromeNativeAppWindowViews. | |
64 views::Widget::InitParams init_params(views::Widget::InitParams::TYPE_WINDOW); | |
65 init_params.delegate = this; | |
66 init_params.keep_on_top = create_params.always_on_top; | |
67 widget_->Init(init_params); | |
68 widget_->CenterWindow( | |
69 create_params.GetInitialWindowBounds(gfx::Insets()).size()); | |
70 } | |
71 | |
72 // ui::BaseWindow implementation. | |
73 | |
74 bool NativeAppWindowViews::IsActive() const { return widget_->IsActive(); } | |
75 | |
76 bool NativeAppWindowViews::IsMaximized() const { | |
77 return widget_->IsMaximized(); | |
78 } | |
79 | |
80 bool NativeAppWindowViews::IsMinimized() const { | |
81 return widget_->IsMinimized(); | |
82 } | |
83 | |
84 bool NativeAppWindowViews::IsFullscreen() const { | |
85 return widget_->IsFullscreen(); | |
86 } | |
87 | |
88 gfx::NativeWindow NativeAppWindowViews::GetNativeWindow() { | |
89 return widget_->GetNativeWindow(); | |
90 } | |
91 | |
92 gfx::Rect NativeAppWindowViews::GetRestoredBounds() const { | |
93 return widget_->GetRestoredBounds(); | |
94 } | |
95 | |
96 ui::WindowShowState NativeAppWindowViews::GetRestoredState() const { | |
97 // Stub implementation. See also ChromeNativeAppWindowViews. | |
98 if (IsMaximized()) | |
99 return ui::SHOW_STATE_MAXIMIZED; | |
100 if (IsFullscreen()) | |
101 return ui::SHOW_STATE_FULLSCREEN; | |
102 return ui::SHOW_STATE_NORMAL; | |
103 } | |
104 | |
105 gfx::Rect NativeAppWindowViews::GetBounds() const { | |
106 return widget_->GetWindowBoundsInScreen(); | |
107 } | |
108 | |
109 void NativeAppWindowViews::Show() { | |
110 if (widget_->IsVisible()) { | |
111 widget_->Activate(); | |
112 return; | |
113 } | |
114 widget_->Show(); | |
115 } | |
116 | |
117 void NativeAppWindowViews::ShowInactive() { | |
118 if (widget_->IsVisible()) | |
119 return; | |
120 | |
121 widget_->ShowInactive(); | |
122 } | |
123 | |
124 void NativeAppWindowViews::Hide() { widget_->Hide(); } | |
125 | |
126 void NativeAppWindowViews::Close() { widget_->Close(); } | |
127 | |
128 void NativeAppWindowViews::Activate() { widget_->Activate(); } | |
129 | |
130 void NativeAppWindowViews::Deactivate() { widget_->Deactivate(); } | |
131 | |
132 void NativeAppWindowViews::Maximize() { widget_->Maximize(); } | |
133 | |
134 void NativeAppWindowViews::Minimize() { widget_->Minimize(); } | |
135 | |
136 void NativeAppWindowViews::Restore() { widget_->Restore(); } | |
137 | |
138 void NativeAppWindowViews::SetBounds(const gfx::Rect& bounds) { | |
139 widget_->SetBounds(bounds); | |
140 } | |
141 | |
142 void NativeAppWindowViews::FlashFrame(bool flash) { | |
143 widget_->FlashFrame(flash); | |
144 } | |
145 | |
146 bool NativeAppWindowViews::IsAlwaysOnTop() const { | |
147 // Stub implementation. See also ChromeNativeAppWindowViews. | |
148 return widget_->IsAlwaysOnTop(); | |
149 } | |
150 | |
151 void NativeAppWindowViews::SetAlwaysOnTop(bool always_on_top) { | |
152 widget_->SetAlwaysOnTop(always_on_top); | |
153 } | |
154 | |
155 gfx::NativeView NativeAppWindowViews::GetHostView() const { | |
156 return widget_->GetNativeView(); | |
157 } | |
158 | |
159 gfx::Point NativeAppWindowViews::GetDialogPosition(const gfx::Size& size) { | |
160 gfx::Size app_window_size = widget_->GetWindowBoundsInScreen().size(); | |
161 return gfx::Point(app_window_size.width() / 2 - size.width() / 2, | |
162 app_window_size.height() / 2 - size.height() / 2); | |
163 } | |
164 | |
165 gfx::Size NativeAppWindowViews::GetMaximumDialogSize() { | |
166 return widget_->GetWindowBoundsInScreen().size(); | |
167 } | |
168 | |
169 void NativeAppWindowViews::AddObserver( | |
170 web_modal::ModalDialogHostObserver* observer) { | |
171 observer_list_.AddObserver(observer); | |
172 } | |
173 void NativeAppWindowViews::RemoveObserver( | |
174 web_modal::ModalDialogHostObserver* observer) { | |
175 observer_list_.RemoveObserver(observer); | |
176 } | |
177 | |
178 void NativeAppWindowViews::OnViewWasResized() { | |
179 FOR_EACH_OBSERVER(web_modal::ModalDialogHostObserver, | |
180 observer_list_, | |
181 OnPositionRequiresUpdate()); | |
182 } | |
183 | |
184 // WidgetDelegate implementation. | |
185 | |
186 void NativeAppWindowViews::OnWidgetMove() { | |
187 app_window_->OnNativeWindowChanged(); | |
188 } | |
189 | |
190 views::View* NativeAppWindowViews::GetInitiallyFocusedView() { | |
191 return web_view_; | |
192 } | |
193 | |
194 bool NativeAppWindowViews::CanResize() const { | |
195 return resizable_ && !size_constraints_.HasFixedSize() && | |
196 !WidgetHasHitTestMask(); | |
197 } | |
198 | |
199 bool NativeAppWindowViews::CanMaximize() const { | |
200 return resizable_ && !size_constraints_.HasMaximumSize() && | |
201 !app_window_->window_type_is_panel() && !WidgetHasHitTestMask(); | |
202 } | |
203 | |
204 base::string16 NativeAppWindowViews::GetWindowTitle() const { | |
205 return app_window_->GetTitle(); | |
206 } | |
207 | |
208 bool NativeAppWindowViews::ShouldShowWindowTitle() const { | |
209 return app_window_->window_type() == AppWindow::WINDOW_TYPE_V1_PANEL; | |
210 } | |
211 | |
212 bool NativeAppWindowViews::ShouldShowWindowIcon() const { | |
213 return app_window_->window_type() == AppWindow::WINDOW_TYPE_V1_PANEL; | |
214 } | |
215 | |
216 void NativeAppWindowViews::SaveWindowPlacement(const gfx::Rect& bounds, | |
217 ui::WindowShowState show_state) { | |
218 views::WidgetDelegate::SaveWindowPlacement(bounds, show_state); | |
219 app_window_->OnNativeWindowChanged(); | |
220 } | |
221 | |
222 void NativeAppWindowViews::DeleteDelegate() { | |
223 widget_->RemoveObserver(this); | |
224 app_window_->OnNativeClose(); | |
225 } | |
226 | |
227 views::Widget* NativeAppWindowViews::GetWidget() { return widget_; } | |
228 | |
229 const views::Widget* NativeAppWindowViews::GetWidget() const { return widget_; } | |
230 | |
231 views::View* NativeAppWindowViews::GetContentsView() { | |
232 return this; | |
233 } | |
234 | |
235 bool NativeAppWindowViews::ShouldDescendIntoChildForEventHandling( | |
236 gfx::NativeView child, | |
237 const gfx::Point& location) { | |
238 #if defined(USE_AURA) | |
239 if (child->Contains(web_view_->web_contents()->GetNativeView())) { | |
240 // App window should claim mouse events that fall within the draggable | |
241 // region. | |
242 return !draggable_region_.get() || | |
243 !draggable_region_->contains(location.x(), location.y()); | |
244 } | |
245 #endif | |
246 | |
247 return true; | |
248 } | |
249 | |
250 // WidgetObserver implementation. | |
251 | |
252 void NativeAppWindowViews::OnWidgetVisibilityChanged(views::Widget* widget, | |
253 bool visible) { | |
254 app_window_->OnNativeWindowChanged(); | |
255 } | |
256 | |
257 void NativeAppWindowViews::OnWidgetActivationChanged(views::Widget* widget, | |
258 bool active) { | |
259 app_window_->OnNativeWindowChanged(); | |
260 if (active) | |
261 app_window_->OnNativeWindowActivated(); | |
262 } | |
263 | |
264 // WebContentsObserver implementation. | |
265 | |
266 void NativeAppWindowViews::RenderViewCreated( | |
267 content::RenderViewHost* render_view_host) { | |
268 if (app_window_->requested_alpha_enabled() && CanHaveAlphaEnabled()) { | |
269 content::RenderWidgetHostView* view = render_view_host->GetView(); | |
270 DCHECK(view); | |
271 view->SetBackgroundOpaque(false); | |
272 } | |
273 } | |
274 | |
275 void NativeAppWindowViews::RenderViewHostChanged( | |
276 content::RenderViewHost* old_host, | |
277 content::RenderViewHost* new_host) { | |
278 OnViewWasResized(); | |
279 } | |
280 | |
281 // views::View implementation. | |
282 | |
283 void NativeAppWindowViews::Layout() { | |
284 DCHECK(web_view_); | |
285 web_view_->SetBounds(0, 0, width(), height()); | |
286 OnViewWasResized(); | |
287 } | |
288 | |
289 void NativeAppWindowViews::ViewHierarchyChanged( | |
290 const ViewHierarchyChangedDetails& details) { | |
291 if (details.is_add && details.child == this) { | |
292 web_view_ = new views::WebView(NULL); | |
293 AddChildView(web_view_); | |
294 web_view_->SetWebContents(app_window_->web_contents()); | |
295 } | |
296 } | |
297 | |
298 gfx::Size NativeAppWindowViews::GetMinimumSize() const { | |
299 return size_constraints_.GetMinimumSize(); | |
300 } | |
301 | |
302 gfx::Size NativeAppWindowViews::GetMaximumSize() const { | |
303 return size_constraints_.GetMaximumSize(); | |
304 } | |
305 | |
306 void NativeAppWindowViews::OnFocus() { | |
307 web_view_->RequestFocus(); | |
308 } | |
309 | |
310 // NativeAppWindow implementation. | |
311 | |
312 void NativeAppWindowViews::SetFullscreen(int fullscreen_types) { | |
313 // Stub implementation. See also ChromeNativeAppWindowViews. | |
314 widget_->SetFullscreen(fullscreen_types != AppWindow::FULLSCREEN_TYPE_NONE); | |
315 } | |
316 | |
317 bool NativeAppWindowViews::IsFullscreenOrPending() const { | |
318 // Stub implementation. See also ChromeNativeAppWindowViews. | |
319 return widget_->IsFullscreen(); | |
320 } | |
321 | |
322 void NativeAppWindowViews::UpdateWindowIcon() { widget_->UpdateWindowIcon(); } | |
323 | |
324 void NativeAppWindowViews::UpdateWindowTitle() { widget_->UpdateWindowTitle(); } | |
325 | |
326 void NativeAppWindowViews::UpdateBadgeIcon() { | |
327 // Stub implementation. See also ChromeNativeAppWindowViews. | |
328 } | |
329 | |
330 void NativeAppWindowViews::UpdateDraggableRegions( | |
331 const std::vector<extensions::DraggableRegion>& regions) { | |
332 // Draggable region is not supported for non-frameless window. | |
333 if (!frameless_) | |
334 return; | |
335 | |
336 draggable_region_.reset(AppWindow::RawDraggableRegionsToSkRegion(regions)); | |
337 OnViewWasResized(); | |
338 } | |
339 | |
340 SkRegion* NativeAppWindowViews::GetDraggableRegion() { | |
341 return draggable_region_.get(); | |
342 } | |
343 | |
344 void NativeAppWindowViews::UpdateShape(scoped_ptr<SkRegion> region) { | |
345 // Stub implementation. See also ChromeNativeAppWindowViews. | |
346 } | |
347 | |
348 void NativeAppWindowViews::HandleKeyboardEvent( | |
349 const content::NativeWebKeyboardEvent& event) { | |
350 unhandled_keyboard_event_handler_.HandleKeyboardEvent(event, | |
351 GetFocusManager()); | |
352 } | |
353 | |
354 bool NativeAppWindowViews::IsFrameless() const { return frameless_; } | |
355 | |
356 bool NativeAppWindowViews::HasFrameColor() const { return false; } | |
357 | |
358 SkColor NativeAppWindowViews::ActiveFrameColor() const { | |
359 return SK_ColorBLACK; | |
360 } | |
361 | |
362 SkColor NativeAppWindowViews::InactiveFrameColor() const { | |
363 return SK_ColorBLACK; | |
364 } | |
365 | |
366 gfx::Insets NativeAppWindowViews::GetFrameInsets() const { | |
367 if (frameless_) | |
368 return gfx::Insets(); | |
369 | |
370 // The pretend client_bounds passed in need to be large enough to ensure that | |
371 // GetWindowBoundsForClientBounds() doesn't decide that it needs more than | |
372 // the specified amount of space to fit the window controls in, and return a | |
373 // number larger than the real frame insets. Most window controls are smaller | |
374 // than 1000x1000px, so this should be big enough. | |
375 gfx::Rect client_bounds = gfx::Rect(1000, 1000); | |
376 gfx::Rect window_bounds = | |
377 widget_->non_client_view()->GetWindowBoundsForClientBounds(client_bounds); | |
378 return window_bounds.InsetsFrom(client_bounds); | |
379 } | |
380 | |
381 void NativeAppWindowViews::HideWithApp() {} | |
382 | |
383 void NativeAppWindowViews::ShowWithApp() {} | |
384 | |
385 void NativeAppWindowViews::UpdateShelfMenu() {} | |
386 | |
387 gfx::Size NativeAppWindowViews::GetContentMinimumSize() const { | |
388 return size_constraints_.GetMinimumSize(); | |
389 } | |
390 | |
391 gfx::Size NativeAppWindowViews::GetContentMaximumSize() const { | |
392 return size_constraints_.GetMaximumSize(); | |
393 } | |
394 | |
395 void NativeAppWindowViews::SetContentSizeConstraints( | |
396 const gfx::Size& min_size, const gfx::Size& max_size) { | |
397 size_constraints_.set_minimum_size(min_size); | |
398 size_constraints_.set_maximum_size(max_size); | |
399 widget_->OnSizeConstraintsChanged(); | |
400 } | |
401 | |
402 bool NativeAppWindowViews::CanHaveAlphaEnabled() const { | |
403 return widget_->IsTranslucentWindowOpacitySupported(); | |
404 } | |
405 | |
406 void NativeAppWindowViews::SetVisibleOnAllWorkspaces(bool always_visible) { | |
407 widget_->SetVisibleOnAllWorkspaces(always_visible); | |
408 } | |
409 | |
410 } // namespace apps | |
OLD | NEW |