Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "apps/shell_window.h" | 5 #include "apps/shell_window.h" |
| 6 | 6 |
| 7 #include "apps/native_app_window.h" | 7 #include "apps/native_app_window.h" |
| 8 #include "apps/shell_window_geometry_cache.h" | 8 #include "apps/shell_window_geometry_cache.h" |
| 9 #include "apps/shell_window_registry.h" | 9 #include "apps/shell_window_registry.h" |
| 10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 49 using web_modal::WebContentsModalDialogManager; | 49 using web_modal::WebContentsModalDialogManager; |
| 50 | 50 |
| 51 namespace { | 51 namespace { |
| 52 const int kDefaultWidth = 512; | 52 const int kDefaultWidth = 512; |
| 53 const int kDefaultHeight = 384; | 53 const int kDefaultHeight = 384; |
| 54 | 54 |
| 55 } // namespace | 55 } // namespace |
| 56 | 56 |
| 57 namespace apps { | 57 namespace apps { |
| 58 | 58 |
| 59 ShellWindow::SizeConstraints::SizeConstraints() | |
| 60 : maximum_size_(kUnboundedSize, kUnboundedSize) { | |
| 61 } | |
| 62 | |
| 63 ShellWindow::SizeConstraints::SizeConstraints(const gfx::Size& min_size, | |
| 64 const gfx::Size& max_size) | |
| 65 : minimum_size_(min_size), | |
| 66 maximum_size_(max_size) { | |
| 67 } | |
| 68 | |
| 69 ShellWindow::SizeConstraints::~SizeConstraints() {} | |
| 70 | |
| 71 gfx::Rect ShellWindow::SizeConstraints::ClampBounds( | |
| 72 const gfx::Rect& bounds) const { | |
| 73 gfx::Rect result(bounds); | |
| 74 if (maximum_size().width() != kUnboundedSize) | |
| 75 result.set_width(std::min(result.width(), maximum_size().width())); | |
| 76 if (maximum_size().height() != kUnboundedSize) | |
| 77 result.set_height(std::min(result.height(), maximum_size().height())); | |
| 78 result.set_width(std::max(result.width(), minimum_size().width())); | |
| 79 result.set_height(std::max(result.height(), minimum_size().height())); | |
| 80 return result; | |
| 81 } | |
| 82 | |
| 83 bool ShellWindow::SizeConstraints::HasMinimumSize() const { | |
| 84 return minimum_size().width() != kUnboundedSize || | |
| 85 minimum_size().height() != kUnboundedSize; | |
| 86 } | |
| 87 | |
| 88 bool ShellWindow::SizeConstraints::HasMaximumSize() const { | |
| 89 return maximum_size().width() != kUnboundedSize || | |
| 90 maximum_size().height() != kUnboundedSize; | |
| 91 } | |
| 92 | |
| 93 bool ShellWindow::SizeConstraints::HasFixedSize() const { | |
| 94 return !minimum_size().IsEmpty() && minimum_size() == maximum_size(); | |
| 95 } | |
| 96 | |
| 97 gfx::Size ShellWindow::SizeConstraints::minimum_size() const { | |
| 98 return minimum_size_; | |
| 99 } | |
| 100 | |
| 101 gfx::Size ShellWindow::SizeConstraints::maximum_size() const { | |
|
tapted
2013/10/14 05:13:43
drat- I missed that this is not a simple accessor.
jackhou1
2013/10/14 23:45:32
Done.
| |
| 102 return gfx::Size( | |
| 103 maximum_size_.width() == kUnboundedSize ? | |
| 104 kUnboundedSize : | |
| 105 std::max(maximum_size_.width(), minimum_size_.width()), | |
| 106 maximum_size_.height() == kUnboundedSize ? | |
| 107 kUnboundedSize : | |
| 108 std::max(maximum_size_.height(), minimum_size_.height())); | |
| 109 } | |
| 110 | |
| 59 ShellWindow::CreateParams::CreateParams() | 111 ShellWindow::CreateParams::CreateParams() |
| 60 : window_type(ShellWindow::WINDOW_TYPE_DEFAULT), | 112 : window_type(ShellWindow::WINDOW_TYPE_DEFAULT), |
| 61 frame(ShellWindow::FRAME_CHROME), | 113 frame(ShellWindow::FRAME_CHROME), |
| 62 transparent_background(false), | 114 transparent_background(false), |
| 63 bounds(INT_MIN, INT_MIN, 0, 0), | 115 bounds(INT_MIN, INT_MIN, 0, 0), |
| 64 creator_process_id(0), | 116 creator_process_id(0), |
| 65 state(ui::SHOW_STATE_DEFAULT), | 117 state(ui::SHOW_STATE_DEFAULT), |
| 66 hidden(false), | 118 hidden(false), |
| 67 resizable(true), | 119 resizable(true), |
| 68 focused(true) {} | 120 focused(true) {} |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 94 WebContents* web_contents = shell_window_contents_->GetWebContents(); | 146 WebContents* web_contents = shell_window_contents_->GetWebContents(); |
| 95 delegate_->InitWebContents(web_contents); | 147 delegate_->InitWebContents(web_contents); |
| 96 WebContentsModalDialogManager::CreateForWebContents(web_contents); | 148 WebContentsModalDialogManager::CreateForWebContents(web_contents); |
| 97 | 149 |
| 98 web_contents->SetDelegate(this); | 150 web_contents->SetDelegate(this); |
| 99 WebContentsModalDialogManager::FromWebContents(web_contents)-> | 151 WebContentsModalDialogManager::FromWebContents(web_contents)-> |
| 100 SetDelegate(this); | 152 SetDelegate(this); |
| 101 extensions::SetViewType(web_contents, extensions::VIEW_TYPE_APP_SHELL); | 153 extensions::SetViewType(web_contents, extensions::VIEW_TYPE_APP_SHELL); |
| 102 | 154 |
| 103 // Initialize the window | 155 // Initialize the window |
| 104 window_type_ = params.window_type; | 156 CreateParams new_params = LoadDefaultsAndConstrain(params); |
| 105 | 157 window_type_ = new_params.window_type; |
| 106 gfx::Rect bounds = params.bounds; | 158 window_key_ = new_params.window_key; |
| 107 | 159 size_constraints_ = SizeConstraints(new_params.minimum_size, |
|
tapted
2013/10/14 05:13:43
move this to the next CL too?
jackhou1
2013/10/14 23:45:32
Done.
| |
| 108 if (bounds.width() == 0) | 160 new_params.maximum_size); |
| 109 bounds.set_width(kDefaultWidth); | |
| 110 if (bounds.height() == 0) | |
| 111 bounds.set_height(kDefaultHeight); | |
| 112 | |
| 113 // If left and top are left undefined, the native shell window will center | |
| 114 // the window on the main screen in a platform-defined manner. | |
| 115 | |
| 116 CreateParams new_params = params; | |
| 117 | |
| 118 // Load cached state if it exists. | |
| 119 if (!params.window_key.empty()) { | |
| 120 window_key_ = params.window_key; | |
| 121 | |
| 122 ShellWindowGeometryCache* cache = ShellWindowGeometryCache::Get(profile()); | |
| 123 | |
| 124 gfx::Rect cached_bounds; | |
| 125 gfx::Rect cached_screen_bounds; | |
| 126 ui::WindowShowState cached_state = ui::SHOW_STATE_DEFAULT; | |
| 127 if (cache->GetGeometry(extension()->id(), params.window_key, &cached_bounds, | |
| 128 &cached_screen_bounds, &cached_state)) { | |
| 129 // App window has cached screen bounds, make sure it fits on screen in | |
| 130 // case the screen resolution changed. | |
| 131 gfx::Screen* screen = gfx::Screen::GetNativeScreen(); | |
| 132 gfx::Display display = screen->GetDisplayMatching(cached_bounds); | |
| 133 gfx::Rect current_screen_bounds = display.work_area(); | |
| 134 AdjustBoundsToBeVisibleOnScreen(cached_bounds, | |
| 135 cached_screen_bounds, | |
| 136 current_screen_bounds, | |
| 137 params.minimum_size, | |
| 138 &bounds); | |
| 139 new_params.state = cached_state; | |
| 140 } | |
| 141 } | |
| 142 | |
| 143 gfx::Size& minimum_size = new_params.minimum_size; | |
| 144 gfx::Size& maximum_size = new_params.maximum_size; | |
| 145 | |
| 146 // In the case that minimum size > maximum size, we consider the minimum | |
| 147 // size to be more important. | |
| 148 if (maximum_size.width() && maximum_size.width() < minimum_size.width()) | |
| 149 maximum_size.set_width(minimum_size.width()); | |
| 150 if (maximum_size.height() && maximum_size.height() < minimum_size.height()) | |
| 151 maximum_size.set_height(minimum_size.height()); | |
| 152 | |
| 153 if (maximum_size.width() && bounds.width() > maximum_size.width()) | |
| 154 bounds.set_width(maximum_size.width()); | |
| 155 if (bounds.width() != INT_MIN && bounds.width() < minimum_size.width()) | |
| 156 bounds.set_width(minimum_size.width()); | |
| 157 | |
| 158 if (maximum_size.height() && bounds.height() > maximum_size.height()) | |
| 159 bounds.set_height(maximum_size.height()); | |
| 160 if (bounds.height() != INT_MIN && bounds.height() < minimum_size.height()) | |
| 161 bounds.set_height(minimum_size.height()); | |
| 162 | |
| 163 new_params.bounds = bounds; | |
| 164 | |
| 165 native_app_window_.reset(delegate_->CreateNativeAppWindow(this, new_params)); | 161 native_app_window_.reset(delegate_->CreateNativeAppWindow(this, new_params)); |
| 166 | 162 |
| 167 if (!new_params.hidden) { | 163 if (!new_params.hidden) { |
| 168 if (window_type_is_panel()) | 164 if (window_type_is_panel()) |
| 169 GetBaseWindow()->ShowInactive(); // Panels are not activated by default. | 165 GetBaseWindow()->ShowInactive(); // Panels are not activated by default. |
| 170 else | 166 else |
| 171 GetBaseWindow()->Show(); | 167 GetBaseWindow()->Show(); |
| 172 } | 168 } |
| 173 | 169 |
| 174 if (new_params.state == ui::SHOW_STATE_FULLSCREEN) | 170 if (new_params.state == ui::SHOW_STATE_FULLSCREEN) |
| 175 Fullscreen(); | 171 Fullscreen(); |
| 176 else if (new_params.state == ui::SHOW_STATE_MAXIMIZED) | 172 else if (new_params.state == ui::SHOW_STATE_MAXIMIZED) |
| 177 Maximize(); | 173 Maximize(); |
| 178 else if (new_params.state == ui::SHOW_STATE_MINIMIZED) | 174 else if (new_params.state == ui::SHOW_STATE_MINIMIZED) |
| 179 Minimize(); | 175 Minimize(); |
| 180 | 176 |
| 181 OnNativeWindowChanged(); | 177 OnNativeWindowChanged(); |
| 182 | 178 |
| 183 // When the render view host is changed, the native window needs to know | 179 // When the render view host is changed, the native window needs to know |
| 184 // about it in case it has any setup to do to make the renderer appear | 180 // about it in case it has any setup to do to make the renderer appear |
| 185 // properly. In particular, on Windows, the view's clickthrough region needs | 181 // properly. In particular, on Windows, the view's clickthrough region needs |
| 186 // to be set. | 182 // to be set. |
| 187 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, | 183 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, |
| 188 content::Source<Profile>(profile_)); | 184 content::Source<Profile>(profile_)); |
| 189 // Close when the browser process is exiting. | 185 // Close when the browser process is exiting. |
| 190 registrar_.Add(this, chrome::NOTIFICATION_APP_TERMINATING, | 186 registrar_.Add(this, chrome::NOTIFICATION_APP_TERMINATING, |
| 191 content::NotificationService::AllSources()); | 187 content::NotificationService::AllSources()); |
| 192 | 188 |
| 193 shell_window_contents_->LoadContents(params.creator_process_id); | 189 shell_window_contents_->LoadContents(new_params.creator_process_id); |
| 194 | 190 |
| 195 // Prevent the browser process from shutting down while this window is open. | 191 // Prevent the browser process from shutting down while this window is open. |
| 196 chrome::StartKeepAlive(); | 192 chrome::StartKeepAlive(); |
| 197 | 193 |
| 198 UpdateExtensionAppIcon(); | 194 UpdateExtensionAppIcon(); |
| 199 | 195 |
| 200 ShellWindowRegistry::Get(profile_)->AddShellWindow(this); | 196 ShellWindowRegistry::Get(profile_)->AddShellWindow(this); |
| 201 } | 197 } |
| 202 | 198 |
| 203 ShellWindow::~ShellWindow() { | 199 ShellWindow::~ShellWindow() { |
| (...skipping 450 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 654 sk_region->op( | 650 sk_region->op( |
| 655 region.bounds.x(), | 651 region.bounds.x(), |
| 656 region.bounds.y(), | 652 region.bounds.y(), |
| 657 region.bounds.right(), | 653 region.bounds.right(), |
| 658 region.bounds.bottom(), | 654 region.bounds.bottom(), |
| 659 region.draggable ? SkRegion::kUnion_Op : SkRegion::kDifference_Op); | 655 region.draggable ? SkRegion::kUnion_Op : SkRegion::kDifference_Op); |
| 660 } | 656 } |
| 661 return sk_region; | 657 return sk_region; |
| 662 } | 658 } |
| 663 | 659 |
| 660 ShellWindow::CreateParams ShellWindow::LoadDefaultsAndConstrain( | |
| 661 const ShellWindow::CreateParams& create_params) const { | |
|
tapted
2013/10/14 05:13:43
nit: the ShellWindow:: prefix on the create_params
jackhou1
2013/10/14 23:45:32
Done.
| |
| 662 CreateParams params(create_params); | |
|
tapted
2013/10/14 05:13:43
You could also pass-by-value, call the argument `p
jackhou1
2013/10/14 23:45:32
Done.
| |
| 663 gfx::Rect bounds = params.bounds; | |
| 664 | |
| 665 if (bounds.width() == 0) | |
| 666 bounds.set_width(kDefaultWidth); | |
| 667 if (bounds.height() == 0) | |
| 668 bounds.set_height(kDefaultHeight); | |
| 669 | |
| 670 // If left and top are left undefined, the native shell window will center | |
| 671 // the window on the main screen in a platform-defined manner. | |
| 672 | |
| 673 // Load cached state if it exists. | |
| 674 if (!params.window_key.empty()) { | |
| 675 ShellWindowGeometryCache* cache = ShellWindowGeometryCache::Get(profile()); | |
| 676 | |
| 677 gfx::Rect cached_bounds; | |
| 678 gfx::Rect cached_screen_bounds; | |
| 679 ui::WindowShowState cached_state = ui::SHOW_STATE_DEFAULT; | |
| 680 if (cache->GetGeometry(extension()->id(), params.window_key, | |
| 681 &cached_bounds, &cached_screen_bounds, | |
| 682 &cached_state)) { | |
| 683 // App window has cached screen bounds, make sure it fits on screen in | |
| 684 // case the screen resolution changed. | |
| 685 gfx::Screen* screen = gfx::Screen::GetNativeScreen(); | |
| 686 gfx::Display display = screen->GetDisplayMatching(cached_bounds); | |
| 687 gfx::Rect current_screen_bounds = display.work_area(); | |
| 688 AdjustBoundsToBeVisibleOnScreen(cached_bounds, | |
| 689 cached_screen_bounds, | |
| 690 current_screen_bounds, | |
| 691 params.minimum_size, | |
| 692 &bounds); | |
| 693 params.state = cached_state; | |
| 694 } | |
| 695 } | |
| 696 | |
| 697 SizeConstraints size_constraints(params.minimum_size, params.maximum_size); | |
| 698 params.bounds = size_constraints.ClampBounds(bounds); | |
|
tapted
2013/10/14 05:13:43
this could then become
new_params.bounds.set_siz
jackhou1
2013/10/14 23:45:32
Done.
| |
| 699 params.minimum_size = size_constraints.minimum_size(); | |
| 700 params.minimum_size = size_constraints.maximum_size(); | |
| 701 | |
| 702 return params; | |
| 703 } | |
| 704 | |
| 664 } // namespace apps | 705 } // namespace apps |
| OLD | NEW |