diff options
author | jackhou@chromium.org <jackhou@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-10-15 08:28:05 +0000 |
---|---|---|
committer | jackhou@chromium.org <jackhou@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-10-15 08:28:05 +0000 |
commit | 20d38a6329cc1bb7297a01d84ba9f495de600ad7 (patch) | |
tree | 7779a5988f5c3218d8fa8343f354b3f322c73d4f /apps | |
parent | 2603e73bf191c664e3841592d94090ba98014fbb (diff) | |
download | chromium_src-20d38a6329cc1bb7297a01d84ba9f495de600ad7.zip chromium_src-20d38a6329cc1bb7297a01d84ba9f495de600ad7.tar.gz chromium_src-20d38a6329cc1bb7297a01d84ba9f495de600ad7.tar.bz2 |
Factor out [min|max]_size into ShellWindow::SizeConstraints
This encapsulates the logic for managing window size constraints.
BUG=305477
Review URL: https://codereview.chromium.org/26751003
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@228654 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'apps')
-rw-r--r-- | apps/shell_window.cc | 161 | ||||
-rw-r--r-- | apps/shell_window.h | 36 |
2 files changed, 135 insertions, 62 deletions
diff --git a/apps/shell_window.cc b/apps/shell_window.cc index e722f4a..1c7aae1 100644 --- a/apps/shell_window.cc +++ b/apps/shell_window.cc @@ -56,6 +56,57 @@ const int kDefaultHeight = 384; namespace apps { +ShellWindow::SizeConstraints::SizeConstraints() + : maximum_size_(kUnboundedSize, kUnboundedSize) { +} + +ShellWindow::SizeConstraints::SizeConstraints(const gfx::Size& min_size, + const gfx::Size& max_size) + : minimum_size_(min_size), + maximum_size_(max_size) { +} + +ShellWindow::SizeConstraints::~SizeConstraints() {} + +gfx::Size ShellWindow::SizeConstraints::ClampSize(gfx::Size size) const { + const gfx::Size max_size = GetMaximumSize(); + if (max_size.width() != kUnboundedSize) + size.set_width(std::min(size.width(), GetMaximumSize().width())); + if (max_size.height() != kUnboundedSize) + size.set_height(std::min(size.height(), GetMaximumSize().height())); + size.SetToMax(GetMinimumSize()); + return size; +} + +bool ShellWindow::SizeConstraints::HasMinimumSize() const { + return GetMinimumSize().width() != kUnboundedSize || + GetMinimumSize().height() != kUnboundedSize; +} + +bool ShellWindow::SizeConstraints::HasMaximumSize() const { + const gfx::Size max_size = GetMaximumSize(); + return max_size.width() != kUnboundedSize || + max_size.height() != kUnboundedSize; +} + +bool ShellWindow::SizeConstraints::HasFixedSize() const { + return !GetMinimumSize().IsEmpty() && GetMinimumSize() == GetMaximumSize(); +} + +gfx::Size ShellWindow::SizeConstraints::GetMinimumSize() const { + return minimum_size_; +} + +gfx::Size ShellWindow::SizeConstraints::GetMaximumSize() const { + return gfx::Size( + maximum_size_.width() == kUnboundedSize ? + kUnboundedSize : + std::max(maximum_size_.width(), minimum_size_.width()), + maximum_size_.height() == kUnboundedSize ? + kUnboundedSize : + std::max(maximum_size_.height(), minimum_size_.height())); +} + ShellWindow::CreateParams::CreateParams() : window_type(ShellWindow::WINDOW_TYPE_DEFAULT), frame(ShellWindow::FRAME_CHROME), @@ -102,67 +153,9 @@ void ShellWindow::Init(const GURL& url, extensions::SetViewType(web_contents, extensions::VIEW_TYPE_APP_SHELL); // Initialize the window - window_type_ = params.window_type; - - gfx::Rect bounds = params.bounds; - - if (bounds.width() == 0) - bounds.set_width(kDefaultWidth); - if (bounds.height() == 0) - bounds.set_height(kDefaultHeight); - - // If left and top are left undefined, the native shell window will center - // the window on the main screen in a platform-defined manner. - - CreateParams new_params = params; - - // Load cached state if it exists. - if (!params.window_key.empty()) { - window_key_ = params.window_key; - - ShellWindowGeometryCache* cache = ShellWindowGeometryCache::Get(profile()); - - gfx::Rect cached_bounds; - gfx::Rect cached_screen_bounds; - ui::WindowShowState cached_state = ui::SHOW_STATE_DEFAULT; - if (cache->GetGeometry(extension()->id(), params.window_key, &cached_bounds, - &cached_screen_bounds, &cached_state)) { - // App window has cached screen bounds, make sure it fits on screen in - // case the screen resolution changed. - gfx::Screen* screen = gfx::Screen::GetNativeScreen(); - gfx::Display display = screen->GetDisplayMatching(cached_bounds); - gfx::Rect current_screen_bounds = display.work_area(); - AdjustBoundsToBeVisibleOnScreen(cached_bounds, - cached_screen_bounds, - current_screen_bounds, - params.minimum_size, - &bounds); - new_params.state = cached_state; - } - } - - gfx::Size& minimum_size = new_params.minimum_size; - gfx::Size& maximum_size = new_params.maximum_size; - - // In the case that minimum size > maximum size, we consider the minimum - // size to be more important. - if (maximum_size.width() && maximum_size.width() < minimum_size.width()) - maximum_size.set_width(minimum_size.width()); - if (maximum_size.height() && maximum_size.height() < minimum_size.height()) - maximum_size.set_height(minimum_size.height()); - - if (maximum_size.width() && bounds.width() > maximum_size.width()) - bounds.set_width(maximum_size.width()); - if (bounds.width() != INT_MIN && bounds.width() < minimum_size.width()) - bounds.set_width(minimum_size.width()); - - if (maximum_size.height() && bounds.height() > maximum_size.height()) - bounds.set_height(maximum_size.height()); - if (bounds.height() != INT_MIN && bounds.height() < minimum_size.height()) - bounds.set_height(minimum_size.height()); - - new_params.bounds = bounds; - + CreateParams new_params = LoadDefaultsAndConstrain(params); + window_type_ = new_params.window_type; + window_key_ = new_params.window_key; native_app_window_.reset(delegate_->CreateNativeAppWindow(this, new_params)); if (!new_params.hidden) { @@ -191,7 +184,7 @@ void ShellWindow::Init(const GURL& url, registrar_.Add(this, chrome::NOTIFICATION_APP_TERMINATING, content::NotificationService::AllSources()); - shell_window_contents_->LoadContents(params.creator_process_id); + shell_window_contents_->LoadContents(new_params.creator_process_id); // Prevent the browser process from shutting down while this window is open. chrome::StartKeepAlive(); @@ -644,6 +637,50 @@ void ShellWindow::AdjustBoundsToBeVisibleOnScreen( } } +ShellWindow::CreateParams ShellWindow::LoadDefaultsAndConstrain( + CreateParams params) const { + gfx::Rect bounds = params.bounds; + + if (bounds.width() == 0) + bounds.set_width(kDefaultWidth); + if (bounds.height() == 0) + bounds.set_height(kDefaultHeight); + + // If left and top are left undefined, the native shell window will center + // the window on the main screen in a platform-defined manner. + + // Load cached state if it exists. + if (!params.window_key.empty()) { + ShellWindowGeometryCache* cache = ShellWindowGeometryCache::Get(profile()); + + gfx::Rect cached_bounds; + gfx::Rect cached_screen_bounds; + ui::WindowShowState cached_state = ui::SHOW_STATE_DEFAULT; + if (cache->GetGeometry(extension()->id(), params.window_key, + &cached_bounds, &cached_screen_bounds, + &cached_state)) { + // App window has cached screen bounds, make sure it fits on screen in + // case the screen resolution changed. + gfx::Screen* screen = gfx::Screen::GetNativeScreen(); + gfx::Display display = screen->GetDisplayMatching(cached_bounds); + gfx::Rect current_screen_bounds = display.work_area(); + AdjustBoundsToBeVisibleOnScreen(cached_bounds, + cached_screen_bounds, + current_screen_bounds, + params.minimum_size, + &bounds); + params.state = cached_state; + } + } + + SizeConstraints size_constraints(params.minimum_size, params.maximum_size); + params.bounds.set_size(size_constraints.ClampSize(bounds.size())); + params.minimum_size = size_constraints.GetMinimumSize(); + params.maximum_size = size_constraints.GetMaximumSize(); + + return params; +} + // static SkRegion* ShellWindow::RawDraggableRegionsToSkRegion( const std::vector<extensions::DraggableRegion>& regions) { diff --git a/apps/shell_window.h b/apps/shell_window.h index aee0cc5..c873ecc 100644 --- a/apps/shell_window.h +++ b/apps/shell_window.h @@ -90,6 +90,37 @@ class ShellWindow : public content::NotificationObserver, FRAME_NONE, // Frameless window. }; + class SizeConstraints { + public: + // The value SizeConstraints uses to represent an unbounded width or height. + static const int kUnboundedSize = 0; + + SizeConstraints(); + SizeConstraints(const gfx::Size& min_size, const gfx::Size& max_size); + ~SizeConstraints(); + + // Returns the bounds with its size clamped to the min/max size. + gfx::Size ClampSize(gfx::Size size) const; + + // When gfx::Size is used as a min/max size, a zero represents an unbounded + // component. This method checks whether either component is specified. + // Note we can't use gfx::Size::IsEmpty as it returns true if either width + // or height is zero. + bool HasMinimumSize() const; + bool HasMaximumSize() const; + + // This returns true if all components are specified, and min and max are + // equal. + bool HasFixedSize() const; + + gfx::Size GetMaximumSize() const; + gfx::Size GetMinimumSize() const; + + private: + gfx::Size minimum_size_; + gfx::Size maximum_size_; + }; + struct CreateParams { CreateParams(); ~CreateParams(); @@ -327,6 +358,11 @@ class ShellWindow : public content::NotificationObserver, const gfx::Size& minimum_size, gfx::Rect* bounds) const; + // Loads the appropriate default or cached window bounds and constrains them + // based on screen size and minimum/maximum size. Returns a new CreateParams + // that should be used to create the window. + CreateParams LoadDefaultsAndConstrain(CreateParams params) const; + // Load the app's image, firing a load state change when loaded. void UpdateExtensionAppIcon(); |