| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/extensions/shell_window.h" | 5 #include "chrome/browser/ui/extensions/shell_window.h" |
| 6 | 6 |
| 7 #include "apps/shell_window_geometry_cache.h" | 7 #include "apps/shell_window_geometry_cache.h" |
| 8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "base/values.h" | 9 #include "base/values.h" |
| 10 #include "chrome/browser/extensions/app_window_contents.h" | 10 #include "chrome/browser/extensions/app_window_contents.h" |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 gfx::Rect bounds = params.bounds; | 145 gfx::Rect bounds = params.bounds; |
| 146 | 146 |
| 147 if (bounds.width() == 0) | 147 if (bounds.width() == 0) |
| 148 bounds.set_width(kDefaultWidth); | 148 bounds.set_width(kDefaultWidth); |
| 149 if (bounds.height() == 0) | 149 if (bounds.height() == 0) |
| 150 bounds.set_height(kDefaultHeight); | 150 bounds.set_height(kDefaultHeight); |
| 151 | 151 |
| 152 // If left and top are left undefined, the native shell window will center | 152 // If left and top are left undefined, the native shell window will center |
| 153 // the window on the main screen in a platform-defined manner. | 153 // the window on the main screen in a platform-defined manner. |
| 154 | 154 |
| 155 gfx::Rect cached_screen_bounds; |
| 155 ui::WindowShowState cached_state = ui::SHOW_STATE_DEFAULT; | 156 ui::WindowShowState cached_state = ui::SHOW_STATE_DEFAULT; |
| 156 if (!params.window_key.empty()) { | 157 if (!params.window_key.empty()) { |
| 157 window_key_ = params.window_key; | 158 window_key_ = params.window_key; |
| 158 | 159 |
| 159 apps::ShellWindowGeometryCache* cache = | 160 apps::ShellWindowGeometryCache* cache = |
| 160 apps::ShellWindowGeometryCache::Get(profile()); | 161 apps::ShellWindowGeometryCache::Get(profile()); |
| 161 | 162 |
| 162 gfx::Rect cached_bounds; | 163 gfx::Rect cached_bounds; |
| 163 if (cache->GetGeometry(extension()->id(), params.window_key, | 164 if (cache->GetGeometry(extension()->id(), params.window_key, &cached_bounds, |
| 164 &cached_bounds, &cached_state)) { | 165 &cached_screen_bounds, &cached_state)) { |
| 165 bounds = cached_bounds; | 166 bounds = cached_bounds; |
| 166 } | 167 } |
| 167 } | 168 } |
| 168 | 169 |
| 169 CreateParams new_params = params; | 170 CreateParams new_params = params; |
| 170 | 171 |
| 171 gfx::Size& minimum_size = new_params.minimum_size; | 172 gfx::Size& minimum_size = new_params.minimum_size; |
| 172 gfx::Size& maximum_size = new_params.maximum_size; | 173 gfx::Size& maximum_size = new_params.maximum_size; |
| 173 | 174 |
| 174 // In the case that minimum size > maximum size, we consider the minimum | 175 // In the case that minimum size > maximum size, we consider the minimum |
| (...skipping 13 matching lines...) Expand all Loading... |
| 188 if (bounds.height() != INT_MIN && bounds.height() < minimum_size.height()) | 189 if (bounds.height() != INT_MIN && bounds.height() < minimum_size.height()) |
| 189 bounds.set_height(minimum_size.height()); | 190 bounds.set_height(minimum_size.height()); |
| 190 | 191 |
| 191 new_params.bounds = bounds; | 192 new_params.bounds = bounds; |
| 192 | 193 |
| 193 if (cached_state != ui::SHOW_STATE_DEFAULT) | 194 if (cached_state != ui::SHOW_STATE_DEFAULT) |
| 194 new_params.state = cached_state; | 195 new_params.state = cached_state; |
| 195 | 196 |
| 196 native_app_window_.reset(NativeAppWindow::Create(this, new_params)); | 197 native_app_window_.reset(NativeAppWindow::Create(this, new_params)); |
| 197 | 198 |
| 199 // App window has cached screen bounds, make sure it fits on screen in case of |
| 200 // the screen resolution changed before show. We can't adjust bounds before |
| 201 // the creation of native app window because current screen bounds is needed. |
| 202 if (!cached_screen_bounds.IsEmpty()) { |
| 203 gfx::Rect new_bounds; |
| 204 gfx::Rect current_screen_bounds = native_app_window_->GetScreenBounds(); |
| 205 AdjustBoundsToBeVisibleOnScreen(bounds, |
| 206 cached_screen_bounds, |
| 207 current_screen_bounds, |
| 208 minimum_size, |
| 209 &new_bounds); |
| 210 native_app_window_->SetBounds(new_bounds); |
| 211 } |
| 198 if (!new_params.hidden) { | 212 if (!new_params.hidden) { |
| 199 if (window_type_is_panel()) | 213 if (window_type_is_panel()) |
| 200 GetBaseWindow()->ShowInactive(); // Panels are not activated by default. | 214 GetBaseWindow()->ShowInactive(); // Panels are not activated by default. |
| 201 else | 215 else |
| 202 GetBaseWindow()->Show(); | 216 GetBaseWindow()->Show(); |
| 203 } | 217 } |
| 204 | 218 |
| 205 if (new_params.state == ui::SHOW_STATE_FULLSCREEN) | 219 if (new_params.state == ui::SHOW_STATE_FULLSCREEN) |
| 206 Fullscreen(); | 220 Fullscreen(); |
| 207 else if (new_params.state == ui::SHOW_STATE_MAXIMIZED) | 221 else if (new_params.state == ui::SHOW_STATE_MAXIMIZED) |
| (...skipping 403 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 611 if (window_key_.empty()) | 625 if (window_key_.empty()) |
| 612 return; | 626 return; |
| 613 if (!native_app_window_) | 627 if (!native_app_window_) |
| 614 return; | 628 return; |
| 615 | 629 |
| 616 apps::ShellWindowGeometryCache* cache = | 630 apps::ShellWindowGeometryCache* cache = |
| 617 apps::ShellWindowGeometryCache::Get(profile()); | 631 apps::ShellWindowGeometryCache::Get(profile()); |
| 618 | 632 |
| 619 gfx::Rect bounds = native_app_window_->GetRestoredBounds(); | 633 gfx::Rect bounds = native_app_window_->GetRestoredBounds(); |
| 620 bounds.Inset(native_app_window_->GetFrameInsets()); | 634 bounds.Inset(native_app_window_->GetFrameInsets()); |
| 635 gfx::Rect screen_bounds = native_app_window_->GetScreenBounds(); |
| 621 ui::WindowShowState window_state = native_app_window_->GetRestoredState(); | 636 ui::WindowShowState window_state = native_app_window_->GetRestoredState(); |
| 622 cache->SaveGeometry(extension()->id(), window_key_, bounds, window_state); | 637 cache->SaveGeometry(extension()->id(), |
| 638 window_key_, |
| 639 bounds, |
| 640 screen_bounds, |
| 641 window_state); |
| 623 } | 642 } |
| 624 | 643 |
| 644 void ShellWindow::AdjustBoundsToBeVisibleOnScreen( |
| 645 const gfx::Rect& cached_bounds, |
| 646 const gfx::Rect& cached_screen_bounds, |
| 647 const gfx::Rect& current_screen_bounds, |
| 648 const gfx::Size& minimum_size, |
| 649 gfx::Rect* bounds) const { |
| 650 if (!native_app_window_) |
| 651 return; |
| 652 if (!bounds) |
| 653 return; |
| 654 |
| 655 *bounds = cached_bounds; |
| 656 |
| 657 // Reposition and resize the bounds if the cached_screen_bounds is different |
| 658 // from the current screen bounds and the current screen bounds doesn't |
| 659 // completely contain the bounds. |
| 660 if (!cached_screen_bounds.IsEmpty() && |
| 661 cached_screen_bounds != current_screen_bounds && |
| 662 !current_screen_bounds.Contains(cached_bounds)) { |
| 663 bounds->set_width( |
| 664 std::max(minimum_size.width(), |
| 665 std::min(bounds->width(), current_screen_bounds.width()))); |
| 666 bounds->set_height( |
| 667 std::max(minimum_size.height(), |
| 668 std::min(bounds->height(), current_screen_bounds.height()))); |
| 669 bounds->set_x( |
| 670 std::max(current_screen_bounds.x(), |
| 671 std::min(bounds->x(), |
| 672 current_screen_bounds.right() - bounds->width()))); |
| 673 bounds->set_y( |
| 674 std::max(current_screen_bounds.y(), |
| 675 std::min(bounds->y(), |
| 676 current_screen_bounds.bottom() - bounds->height()))); |
| 677 } |
| 678 } |
| 679 |
| 680 |
| 625 // static | 681 // static |
| 626 SkRegion* ShellWindow::RawDraggableRegionsToSkRegion( | 682 SkRegion* ShellWindow::RawDraggableRegionsToSkRegion( |
| 627 const std::vector<extensions::DraggableRegion>& regions) { | 683 const std::vector<extensions::DraggableRegion>& regions) { |
| 628 SkRegion* sk_region = new SkRegion; | 684 SkRegion* sk_region = new SkRegion; |
| 629 for (std::vector<extensions::DraggableRegion>::const_iterator iter = | 685 for (std::vector<extensions::DraggableRegion>::const_iterator iter = |
| 630 regions.begin(); | 686 regions.begin(); |
| 631 iter != regions.end(); ++iter) { | 687 iter != regions.end(); ++iter) { |
| 632 const extensions::DraggableRegion& region = *iter; | 688 const extensions::DraggableRegion& region = *iter; |
| 633 sk_region->op( | 689 sk_region->op( |
| 634 region.bounds.x(), | 690 region.bounds.x(), |
| 635 region.bounds.y(), | 691 region.bounds.y(), |
| 636 region.bounds.right(), | 692 region.bounds.right(), |
| 637 region.bounds.bottom(), | 693 region.bounds.bottom(), |
| 638 region.draggable ? SkRegion::kUnion_Op : SkRegion::kDifference_Op); | 694 region.draggable ? SkRegion::kUnion_Op : SkRegion::kDifference_Op); |
| 639 } | 695 } |
| 640 return sk_region; | 696 return sk_region; |
| 641 } | 697 } |
| 642 | 698 |
| 643 void ShellWindow::DisableExternalOpenForTesting() { | 699 void ShellWindow::DisableExternalOpenForTesting() { |
| 644 disable_external_open_for_testing_ = true; | 700 disable_external_open_for_testing_ = true; |
| 645 } | 701 } |
| 646 | 702 |
| OLD | NEW |