]> khamidou — GitHub mirrors - electron.git/commitdiff
add function maintainAspectRatioOfInteriorContent to BrowserWindow in order to enforc...
authorMichael S. Barthelemy <msb@idiom.com>
Thu, 16 Jul 2015 17:54:51 +0000 (13:54 -0400)
committerMichael S. Barthelemy <msb@idiom.com>
Thu, 16 Jul 2015 17:54:51 +0000 (13:54 -0400)
atom/browser/api/atom_api_window.cc
atom/browser/api/atom_api_window.h
atom/browser/native_window.cc
atom/browser/native_window.h
atom/browser/native_window_mac.mm

index d5bce589113526aca1099e053ee0492569bfa3e2..ae907b0669d53b8cab8cd1de0c560e237a930449 100644 (file)
@@ -249,6 +249,10 @@ bool Window::IsFullscreen() {
   return window_->IsFullscreen();
 }
 
+void Window::MaintainAspectRatioOfInteriorContent(double aspectRatio, double extraWidth, double extraHeight) {
+  window_->MaintainAspectRatioOfInteriorContent(aspectRatio, gfx::Size(extraWidth, extraHeight));
+}
+
 void Window::SetBounds(const gfx::Rect& bounds) {
   window_->SetBounds(bounds);
 }
@@ -498,6 +502,7 @@ void Window::BuildPrototype(v8::Isolate* isolate,
       .SetMethod("isMinimized", &Window::IsMinimized)
       .SetMethod("setFullScreen", &Window::SetFullScreen)
       .SetMethod("isFullScreen", &Window::IsFullscreen)
+      .SetMethod("maintainAspectRatioOfInteriorContent", &Window::MaintainAspectRatioOfInteriorContent)
       .SetMethod("getBounds", &Window::GetBounds)
       .SetMethod("setBounds", &Window::SetBounds)
       .SetMethod("getSize", &Window::GetSize)
index 5cdb49e41bbf169f9d80f93d1ce418fdbee50a13..8c27f5b36a6f6a0e520f57c65227597adac32bd7 100644 (file)
@@ -95,6 +95,7 @@ class Window : public mate::TrackableObject<Window>,
   bool IsMinimized();
   void SetFullScreen(bool fullscreen);
   bool IsFullscreen();
+  void MaintainAspectRatioOfInteriorContent(double aspectRatio, double extraWidth, double extraHeight);
   void SetBounds(const gfx::Rect& bounds);
   gfx::Rect GetBounds();
   void SetSize(int width, int height);
index 2a093cc81200d083d67afa2172850381472b9b8f..8e21876fdfa3612180a4dd34d235b1489ecab111 100644 (file)
@@ -195,6 +195,19 @@ void NativeWindow::InitFromOptions(const mate::Dictionary& options) {
     Show();
 }
 
+double NativeWindow::GetInteriorContentAspectRatio() {
+  return interiorContentAspectRatio;
+}
+
+gfx::Size NativeWindow::GetInteriorContentExtraSize() {
+  return interiorContentExtraSize;
+}
+
+void NativeWindow::MaintainAspectRatioOfInteriorContent(double aspectRatio, const gfx::Size& extraSize) {
+  interiorContentAspectRatio = aspectRatio;
+  interiorContentExtraSize = extraSize;
+}
+
 void NativeWindow::SetSize(const gfx::Size& size) {
   SetBounds(gfx::Rect(GetPosition(), size));
 }
index 0ac7aa50c9150977bcf8c80c0aa982eaaf18154e..e9e159a7ea3b837bfc4962911d2123de95331319 100644 (file)
@@ -107,6 +107,9 @@ class NativeWindow : public content::WebContentsObserver,
   virtual bool IsMinimized() = 0;
   virtual void SetFullScreen(bool fullscreen) = 0;
   virtual bool IsFullscreen() const = 0;
+  double GetInteriorContentAspectRatio();
+  virtual gfx::Size GetInteriorContentExtraSize();
+  virtual void MaintainAspectRatioOfInteriorContent(double aspectRatio, const gfx::Size& extraSize);
   virtual void SetBounds(const gfx::Rect& bounds) = 0;
   virtual gfx::Rect GetBounds() = 0;
   virtual void SetSize(const gfx::Size& size);
@@ -285,6 +288,10 @@ class NativeWindow : public content::WebContentsObserver,
   // Page's default zoom factor.
   double zoom_factor_;
 
+  // Used to maintain the aspect ratio of a view which is inside of the content view.
+  double interiorContentAspectRatio = 0.0;
+  gfx::Size interiorContentExtraSize;
+
   // The page this window is viewing.
   brightray::InspectableWebContents* inspectable_web_contents_;
 
index c0d358bb8f72493e08761e5aa390aca489f6a3fb..de7e8ad779679992cafbb195797e51380f8a0ce6 100644 (file)
@@ -95,6 +95,33 @@ static const CGFloat kAtomWindowCornerRadius = 4.0;
   shell_->NotifyWindowBlur();
 }
 
+- (NSSize)windowWillResize:(NSWindow *)sender toSize:(NSSize)frameSize {
+  NSSize newSize = frameSize;
+  double interiorContentAspectRatio = shell_->GetInteriorContentAspectRatio();
+
+  if (interiorContentAspectRatio > 0.0) {
+    NSRect windowFrame = [sender frame];
+    NSRect contentViewBounds = [[sender contentView] bounds];
+    gfx::Size interiorContentExtraSize = shell_->GetInteriorContentExtraSize();
+    double extraWidthPlusFrame = windowFrame.size.width - contentViewBounds.size.width + interiorContentExtraSize.width();
+    double extraHeightPlusFrame = windowFrame.size.height - contentViewBounds.size.height + interiorContentExtraSize.height();
+
+    newSize.width = roundf(((frameSize.height - extraHeightPlusFrame) * interiorContentAspectRatio) + 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) / interiorContentAspectRatio) + extraHeightPlusFrame);
+    }
+    else {
+        newSize.width = roundf(((newSize.height - extraHeightPlusFrame) * interiorContentAspectRatio) + extraWidthPlusFrame);
+        newSize.height = roundf(((frameSize.width - extraWidthPlusFrame) / interiorContentAspectRatio) + extraHeightPlusFrame);
+    }
+  }
+
+  return newSize;
+}
+
 - (void)windowDidResize:(NSNotification*)notification {
   if (!shell_->has_frame())
     shell_->ClipWebView();