Merge pull request #5644 from leethomas/fix/osx-aspect-ratio

🍎  let Cocoa handle keeping aspect ratio on window resize
This commit is contained in:
Cheng Zhao 2016-05-23 08:15:44 +00:00
commit 1b9bced8c0
3 changed files with 20 additions and 17 deletions

View file

@ -191,7 +191,7 @@ class NativeWindow : public base::SupportsUserData,
// Set the aspect ratio when resizing window.
double GetAspectRatio();
gfx::Size GetAspectRatioExtraSize();
void SetAspectRatio(double aspect_ratio, const gfx::Size& extra_size);
virtual void SetAspectRatio(double aspect_ratio, const gfx::Size& extra_size);
base::WeakPtr<NativeWindow> GetWeakPtr() {
return weak_factory_.GetWeakPtr();

View file

@ -49,6 +49,8 @@ class NativeWindowMac : public NativeWindow {
void SetResizable(bool resizable) override;
bool IsResizable() override;
void SetMovable(bool movable) override;
void SetAspectRatio(double aspect_ratio, const gfx::Size& extra_size)
override;
bool IsMovable() override;
void SetMinimizable(bool minimizable) override;
bool IsMinimizable() override;

View file

@ -141,22 +141,9 @@ bool ScopedDisableResize::disable_resize_ = false;
newSize.width =
roundf((frameSize.height - extraHeightPlusFrame) * aspectRatio +
extraWidthPlusFrame);
// If the new width is less than the frame size use it as the primary
// constraint. This ensures that the value returned by this method will
// never be larger than the users requested window size.
if (newSize.width <= frameSize.width) {
newSize.height =
roundf((newSize.width - extraWidthPlusFrame) / aspectRatio +
extraHeightPlusFrame);
} else {
newSize.height =
roundf((frameSize.width - extraWidthPlusFrame) / aspectRatio +
extraHeightPlusFrame);
newSize.width =
roundf((newSize.height - extraHeightPlusFrame) * aspectRatio +
extraWidthPlusFrame);
}
newSize.height =
roundf((newSize.width - extraWidthPlusFrame) / aspectRatio +
extraHeightPlusFrame);
}
return newSize;
@ -721,6 +708,20 @@ bool NativeWindowMac::IsResizable() {
return [window_ styleMask] & NSResizableWindowMask;
}
void NativeWindowMac::SetAspectRatio(double aspect_ratio,
const gfx::Size& extra_size) {
NativeWindow::SetAspectRatio(aspect_ratio, extra_size);
// We can't just pass the aspect ratio to Cocoa, since our API receives
// it as a float, and Cocoa expects an NSRect with explicit width & height
// arguments. Instead we derive those args ourselves from the given aspect
// ratio.
double width = roundf([window_ frame].size.height * aspect_ratio);
double height = roundf(width / aspect_ratio);
[window_ setAspectRatio:NSMakeSize(width, height)];
}
void NativeWindowMac::SetMovable(bool movable) {
[window_ setMovable:movable];
}