Merge pull request #5644 from leethomas/fix/osx-aspect-ratio
🍎 let Cocoa handle keeping aspect ratio on window resize
This commit is contained in:
commit
1b9bced8c0
3 changed files with 20 additions and 17 deletions
|
@ -191,7 +191,7 @@ class NativeWindow : public base::SupportsUserData,
|
||||||
// Set the aspect ratio when resizing window.
|
// Set the aspect ratio when resizing window.
|
||||||
double GetAspectRatio();
|
double GetAspectRatio();
|
||||||
gfx::Size GetAspectRatioExtraSize();
|
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() {
|
base::WeakPtr<NativeWindow> GetWeakPtr() {
|
||||||
return weak_factory_.GetWeakPtr();
|
return weak_factory_.GetWeakPtr();
|
||||||
|
|
|
@ -49,6 +49,8 @@ class NativeWindowMac : public NativeWindow {
|
||||||
void SetResizable(bool resizable) override;
|
void SetResizable(bool resizable) override;
|
||||||
bool IsResizable() override;
|
bool IsResizable() override;
|
||||||
void SetMovable(bool movable) override;
|
void SetMovable(bool movable) override;
|
||||||
|
void SetAspectRatio(double aspect_ratio, const gfx::Size& extra_size)
|
||||||
|
override;
|
||||||
bool IsMovable() override;
|
bool IsMovable() override;
|
||||||
void SetMinimizable(bool minimizable) override;
|
void SetMinimizable(bool minimizable) override;
|
||||||
bool IsMinimizable() override;
|
bool IsMinimizable() override;
|
||||||
|
|
|
@ -141,22 +141,9 @@ bool ScopedDisableResize::disable_resize_ = false;
|
||||||
newSize.width =
|
newSize.width =
|
||||||
roundf((frameSize.height - extraHeightPlusFrame) * aspectRatio +
|
roundf((frameSize.height - extraHeightPlusFrame) * aspectRatio +
|
||||||
extraWidthPlusFrame);
|
extraWidthPlusFrame);
|
||||||
|
newSize.height =
|
||||||
// If the new width is less than the frame size use it as the primary
|
roundf((newSize.width - extraWidthPlusFrame) / aspectRatio +
|
||||||
// constraint. This ensures that the value returned by this method will
|
extraHeightPlusFrame);
|
||||||
// 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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return newSize;
|
return newSize;
|
||||||
|
@ -721,6 +708,20 @@ bool NativeWindowMac::IsResizable() {
|
||||||
return [window_ styleMask] & NSResizableWindowMask;
|
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) {
|
void NativeWindowMac::SetMovable(bool movable) {
|
||||||
[window_ setMovable:movable];
|
[window_ setMovable:movable];
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue