From c5790e39dc4c83b03b10b3b3b8758e7f0428015b Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Wed, 26 Oct 2016 12:12:12 +0900 Subject: [PATCH] Add support for zooming to content size --- atom/browser/native_window_mac.h | 4 +++ atom/browser/native_window_mac.mm | 49 +++++++++++++++++++++++++++++++ atom/common/options_switches.cc | 3 ++ atom/common/options_switches.h | 1 + 4 files changed, 57 insertions(+) diff --git a/atom/browser/native_window_mac.h b/atom/browser/native_window_mac.h index 339e10e853c..6bd2c69ed4d 100644 --- a/atom/browser/native_window_mac.h +++ b/atom/browser/native_window_mac.h @@ -117,6 +117,8 @@ class NativeWindowMac : public NativeWindow, }; TitleBarStyle title_bar_style() const { return title_bar_style_; } + bool zoom_to_content_size() const { return zoom_to_content_size_; } + protected: // Return a vector of non-draggable regions that fill a window of size // |width| by |height|, but leave gaps where the window should be draggable. @@ -155,6 +157,8 @@ class NativeWindowMac : public NativeWindow, bool is_kiosk_; + bool zoom_to_content_size_; + NSInteger attention_request_id_; // identifier from requestUserAttention // The presentation options before entering kiosk mode. diff --git a/atom/browser/native_window_mac.mm b/atom/browser/native_window_mac.mm index 42157f93f9d..69dd29b4f0b 100644 --- a/atom/browser/native_window_mac.mm +++ b/atom/browser/native_window_mac.mm @@ -102,6 +102,52 @@ bool ScopedDisableResize::disable_resize_ = false; } } +// Called when the user clicks the zoom button or selects it from the Window +// menu) to determine the "standard size" of the window. +- (NSRect)windowWillUseStandardFrame:(NSWindow*)window + defaultFrame:(NSRect)frame { + if (!shell_->zoom_to_content_size()) + return frame; + + // If the shift key is down, maximize. + if ([[NSApp currentEvent] modifierFlags] & NSShiftKeyMask) + return frame; + + // To prevent strange results on portrait displays, the basic minimum zoomed + // width is the larger of: 60% of available width, 60% of available height + // (bounded by available width). + const CGFloat kProportion = 0.6; + CGFloat zoomedWidth = + std::max(kProportion * NSWidth(frame), + std::min(kProportion * NSHeight(frame), NSWidth(frame))); + + content::WebContents* web_contents = shell_->web_contents(); + if (web_contents) { + // If the intrinsic width is bigger, then make it the zoomed width. + const int kScrollbarWidth = 16; // TODO(viettrungluu): ugh. + CGFloat intrinsicWidth = static_cast( + web_contents->GetPreferredSize().width() + kScrollbarWidth); + zoomedWidth = std::max(zoomedWidth, + std::min(intrinsicWidth, NSWidth(frame))); + } + + // Never shrink from the current size on zoom (see above). + NSRect currentFrame = [shell_->GetNativeWindow() frame]; + zoomedWidth = std::max(zoomedWidth, NSWidth(currentFrame)); + + // |frame| determines our maximum extents. We need to set the origin of the + // frame -- and only move it left if necessary. + if (currentFrame.origin.x + zoomedWidth > NSMaxX(frame)) + frame.origin.x = NSMaxX(frame) - zoomedWidth; + else + frame.origin.x = currentFrame.origin.x; + + // Set the width. Don't touch y or height. + frame.size.width = zoomedWidth; + + return frame; +} + - (void)windowDidBecomeMain:(NSNotification*)notification { content::WebContents* web_contents = shell_->web_contents(); if (!web_contents) @@ -584,6 +630,7 @@ NativeWindowMac::NativeWindowMac( NativeWindow* parent) : NativeWindow(web_contents, options, parent), is_kiosk_(false), + zoom_to_content_size_(false), attention_request_id_(0), title_bar_style_(NORMAL) { int width = 800, height = 600; @@ -706,6 +753,8 @@ NativeWindowMac::NativeWindowMac( if (!has_frame() || !use_content_size) SetSize(gfx::Size(width, height)); + options.Get(options::kZoomToContentSize, &zoom_to_content_size_); + // Enable the NSView to accept first mouse event. bool acceptsFirstMouse = false; options.Get(options::kAcceptFirstMouse, &acceptsFirstMouse); diff --git a/atom/common/options_switches.cc b/atom/common/options_switches.cc index 00b2c8c0268..eab2cf8145e 100644 --- a/atom/common/options_switches.cc +++ b/atom/common/options_switches.cc @@ -45,6 +45,9 @@ const char kAcceptFirstMouse[] = "acceptFirstMouse"; // Whether window size should include window frame. const char kUseContentSize[] = "useContentSize"; +// Whether window zoom should be to content size. +const char kZoomToContentSize[] = "zoomToContentSize"; + // The requested title bar style for the window const char kTitleBarStyle[] = "titleBarStyle"; diff --git a/atom/common/options_switches.h b/atom/common/options_switches.h index 345329524b7..fcd98550b0a 100644 --- a/atom/common/options_switches.h +++ b/atom/common/options_switches.h @@ -34,6 +34,7 @@ extern const char kKiosk[]; extern const char kAlwaysOnTop[]; extern const char kAcceptFirstMouse[]; extern const char kUseContentSize[]; +extern const char kZoomToContentSize[]; extern const char kTitleBarStyle[]; extern const char kAutoHideMenuBar[]; extern const char kEnableLargerThanScreen[];