From 8f7a04f9c3dbc70fa1f8854398fc1aaef1bf52c4 Mon Sep 17 00:00:00 2001 From: leethomas Date: Sun, 22 May 2016 15:43:47 -0700 Subject: [PATCH 1/6] =?UTF-8?q?=F0=9F=8D=8E=20=20let=20Cocoa=20handle=20ke?= =?UTF-8?q?eping=20the=20aspect=20ratio=20whenever=20the=20edges=20are=20d?= =?UTF-8?q?ragged?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- atom/browser/native_window_mac.mm | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) diff --git a/atom/browser/native_window_mac.mm b/atom/browser/native_window_mac.mm index bfffc7a08d64..434919dde117 100644 --- a/atom/browser/native_window_mac.mm +++ b/atom/browser/native_window_mac.mm @@ -141,22 +141,11 @@ bool ScopedDisableResize::disable_resize_ = false; newSize.width = roundf((frameSize.height - extraHeightPlusFrame) * aspectRatio + extraWidthPlusFrame); + newSize.height = + roundf((newSize.width - extraWidthPlusFrame) / aspectRatio + + extraHeightPlusFrame); - // 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); - } + [sender setAspectRatio:NSMakeSize(newSize.width, newSize.height)]; } return newSize; From 7aaf97436218fb93988e658d83dd6394bfad52ab Mon Sep 17 00:00:00 2001 From: leethomas Date: Sun, 22 May 2016 16:50:50 -0700 Subject: [PATCH 2/6] override SetAspectRatio for NativeWindowMac --- atom/browser/native_window.h | 2 +- atom/browser/native_window_mac.h | 2 ++ atom/browser/native_window_mac.mm | 23 +++++++++++++++++++++-- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/atom/browser/native_window.h b/atom/browser/native_window.h index 7317a7ba6f76..8776e3d00652 100644 --- a/atom/browser/native_window.h +++ b/atom/browser/native_window.h @@ -190,7 +190,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 GetWeakPtr() { return weak_factory_.GetWeakPtr(); diff --git a/atom/browser/native_window_mac.h b/atom/browser/native_window_mac.h index cfb3141ede66..c1694c3c784a 100644 --- a/atom/browser/native_window_mac.h +++ b/atom/browser/native_window_mac.h @@ -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; diff --git a/atom/browser/native_window_mac.mm b/atom/browser/native_window_mac.mm index 434919dde117..f7cd4d42281e 100644 --- a/atom/browser/native_window_mac.mm +++ b/atom/browser/native_window_mac.mm @@ -144,8 +144,6 @@ bool ScopedDisableResize::disable_resize_ = false; newSize.height = roundf((newSize.width - extraWidthPlusFrame) / aspectRatio + extraHeightPlusFrame); - - [sender setAspectRatio:NSMakeSize(newSize.width, newSize.height)]; } return newSize; @@ -708,6 +706,27 @@ bool NativeWindowMac::IsResizable() { return [window_ styleMask] & NSResizableWindowMask; } +void NativeWindowMac::SetAspectRatio(double aspect_ratio, + const gfx::Size& extra_size) { + + gfx::Size windowSize = this->GetSize(); + gfx::Size contentSize = this->GetContentSize(); + + double extraWidthPlusFrame = + windowSize.width() - contentSize.width() + extra_size.width(); + double extraHeightPlusFrame = + windowSize.height() - contentSize.height() + extra_size.height(); + + double width = + roundf(([window_ frame].size.height - extraHeightPlusFrame) * + aspect_ratio + extraWidthPlusFrame); + double height = + roundf((width - extraWidthPlusFrame) / + aspect_ratio + extraHeightPlusFrame); + + [window_ setAspectRatio:NSMakeSize(width, height)]; +} + void NativeWindowMac::SetMovable(bool movable) { [window_ setMovable:movable]; } From ac6e4aff5e4dbb2b91ed8c6e17c4a3c5bd9165a7 Mon Sep 17 00:00:00 2001 From: leethomas Date: Sun, 22 May 2016 17:00:14 -0700 Subject: [PATCH 3/6] comments --- atom/browser/native_window_mac.mm | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/atom/browser/native_window_mac.mm b/atom/browser/native_window_mac.mm index f7cd4d42281e..264cab5fe739 100644 --- a/atom/browser/native_window_mac.mm +++ b/atom/browser/native_window_mac.mm @@ -709,6 +709,11 @@ bool NativeWindowMac::IsResizable() { void NativeWindowMac::SetAspectRatio(double aspect_ratio, const gfx::Size& 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. + gfx::Size windowSize = this->GetSize(); gfx::Size contentSize = this->GetContentSize(); From 09de0c2766b3c8e4340f1f6f14bfb4e80faf866a Mon Sep 17 00:00:00 2001 From: leethomas Date: Sun, 22 May 2016 17:22:57 -0700 Subject: [PATCH 4/6] call base SetAspectRatio in NativeWindowMac implementation --- atom/browser/native_window_mac.mm | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/atom/browser/native_window_mac.mm b/atom/browser/native_window_mac.mm index 264cab5fe739..3bd3ed1b995a 100644 --- a/atom/browser/native_window_mac.mm +++ b/atom/browser/native_window_mac.mm @@ -708,12 +708,13 @@ bool NativeWindowMac::IsResizable() { 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. - + gfx::Size windowSize = this->GetSize(); gfx::Size contentSize = this->GetContentSize(); From 1d61f987cb694232ec4c12190da4530f7aec9baf Mon Sep 17 00:00:00 2001 From: leethomas Date: Sun, 22 May 2016 18:36:05 -0700 Subject: [PATCH 5/6] code styling --- atom/browser/native_window_mac.mm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/atom/browser/native_window_mac.mm b/atom/browser/native_window_mac.mm index 3bd3ed1b995a..0f52adf30b6d 100644 --- a/atom/browser/native_window_mac.mm +++ b/atom/browser/native_window_mac.mm @@ -707,7 +707,7 @@ bool NativeWindowMac::IsResizable() { } void NativeWindowMac::SetAspectRatio(double aspect_ratio, - const gfx::Size& extra_size) { + 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 From de27b34891818e4a01913e62bacafd9a853cbe1a Mon Sep 17 00:00:00 2001 From: leethomas Date: Sun, 22 May 2016 19:09:21 -0700 Subject: [PATCH 6/6] disregard extraSize when initially setting the aspect ratio --- atom/browser/native_window_mac.mm | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/atom/browser/native_window_mac.mm b/atom/browser/native_window_mac.mm index 0f52adf30b6d..d6571be826d1 100644 --- a/atom/browser/native_window_mac.mm +++ b/atom/browser/native_window_mac.mm @@ -714,21 +714,8 @@ void NativeWindowMac::SetAspectRatio(double aspect_ratio, // 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. - - gfx::Size windowSize = this->GetSize(); - gfx::Size contentSize = this->GetContentSize(); - - double extraWidthPlusFrame = - windowSize.width() - contentSize.width() + extra_size.width(); - double extraHeightPlusFrame = - windowSize.height() - contentSize.height() + extra_size.height(); - - double width = - roundf(([window_ frame].size.height - extraHeightPlusFrame) * - aspect_ratio + extraWidthPlusFrame); - double height = - roundf((width - extraWidthPlusFrame) / - aspect_ratio + extraHeightPlusFrame); + double width = roundf([window_ frame].size.height * aspect_ratio); + double height = roundf(width / aspect_ratio); [window_ setAspectRatio:NSMakeSize(width, height)]; }