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