Browse Source

feat: implement win.setAspectRatio() on Linux (#19516)

Erick Zhao 5 years ago
parent
commit
dcf6f046d9

+ 2 - 2
docs/api/browser-window.md

@@ -946,11 +946,11 @@ Returns `Boolean` - Whether the window is in simple (pre-Lion) fullscreen mode.
 
 Returns `Boolean` - Whether the window is in normal state (not maximized, not minimized, not in fullscreen mode).
 
-#### `win.setAspectRatio(aspectRatio[, extraSize])` _macOS_
+#### `win.setAspectRatio(aspectRatio[, extraSize])` _macOS_ _Linux_
 
 * `aspectRatio` Float - The aspect ratio to maintain for some portion of the
 content view.
-* `extraSize` [Size](structures/size.md) (optional) - The extra size not to be included while
+ * `extraSize` [Size](structures/size.md) (optional) _macOS_ - The extra size not to be included while
 maintaining the aspect ratio.
 
 This will make a window maintain an aspect ratio. The extra size allows a

+ 1 - 0
patches/chromium/.patches

@@ -84,3 +84,4 @@ build_win_fix_ambiguous_reference_with_msstl.patch
 build_win_iwyu_for_smil_time.patch
 remove_usage_of_incognito_apis_in_the_spellchecker.patch
 chore_use_electron_resources_not_chrome_for_spellchecker.patch
+feat_unset_window_aspect_ratio_on_linux.patch

+ 28 - 0
patches/chromium/feat_unset_window_aspect_ratio_on_linux.patch

@@ -0,0 +1,28 @@
+From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
+From: Erick Zhao <[email protected]>
+Date: Thu, 1 Aug 2019 13:40:17 -0700
+Subject: feat: unset window aspect ratio on linux
+
+
+diff --git a/ui/base/x/x11_window.cc b/ui/base/x/x11_window.cc
+index 7df0a3a8ed268e9b2ca1bad8ac5fd35115b98f77..a895defd2b695e2d5b2b4324551adcaac487f588 100644
+--- a/ui/base/x/x11_window.cc
++++ b/ui/base/x/x11_window.cc
+@@ -676,9 +676,14 @@ void XWindow::SetAspectRatio(const gfx::SizeF& aspect_ratio) {
+   long supplied_return;
+ 
+   XGetWMNormalHints(xdisplay_, xwindow_, &size_hints, &supplied_return);
+-  size_hints.flags |= PAspect;
+-  size_hints.min_aspect.x = size_hints.max_aspect.x = aspect_ratio.width();
+-  size_hints.min_aspect.y = size_hints.max_aspect.y = aspect_ratio.height();
++  // if ratio parameter has length 0, unforce the aspect ratio
++  if (aspect_ratio.IsEmpty()) {
++    size_hints.flags &= ~PAspect;
++  } else {
++    size_hints.flags |= PAspect;
++    size_hints.min_aspect.x = size_hints.max_aspect.x = aspect_ratio.width();
++    size_hints.min_aspect.y = size_hints.max_aspect.y = aspect_ratio.height();
++  }
+   XSetWMNormalHints(xdisplay_, xwindow_, &size_hints);
+ }
+ 

+ 12 - 0
shell/browser/native_window_views.cc

@@ -709,6 +709,18 @@ bool NativeWindowViews::IsResizable() {
   return CanResize();
 }
 
+void NativeWindowViews::SetAspectRatio(double aspect_ratio,
+                                       const gfx::Size& extra_size) {
+  NativeWindow::SetAspectRatio(aspect_ratio, extra_size);
+#if defined(OS_LINUX)
+  gfx::SizeF aspect(aspect_ratio, 1.0);
+  // Scale up because SetAspectRatio() truncates aspect value to int
+  aspect.Scale(100);
+
+  widget()->SetAspectRatio(aspect);
+#endif
+}
+
 void NativeWindowViews::SetMovable(bool movable) {
   movable_ = movable;
 }

+ 2 - 0
shell/browser/native_window_views.h

@@ -72,6 +72,8 @@ class NativeWindowViews : public NativeWindow,
   bool MoveAbove(const std::string& sourceId) override;
   void MoveTop() override;
   bool IsResizable() override;
+  void SetAspectRatio(double aspect_ratio,
+                      const gfx::Size& extra_size) override;
   void SetMovable(bool movable) override;
   bool IsMovable() override;
   void SetMinimizable(bool minimizable) override;