Merge pull request #7776 from electron/zoom-to-web-contents-size

Add zoomToPageWidth window option
This commit is contained in:
Kevin Sawicki 2016-11-21 09:21:55 -08:00 committed by GitHub
commit 2e35b1c855
6 changed files with 68 additions and 0 deletions

View file

@ -117,6 +117,8 @@ class NativeWindowMac : public NativeWindow,
};
TitleBarStyle title_bar_style() const { return title_bar_style_; }
bool zoom_to_page_width() const { return zoom_to_page_width_; }
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_page_width_;
NSInteger attention_request_id_; // identifier from requestUserAttention
// The presentation options before entering kiosk mode.

View file

@ -102,6 +102,41 @@ 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_page_width())
return frame;
// If the shift key is down, maximize.
if ([[NSApp currentEvent] modifierFlags] & NSShiftKeyMask)
return frame;
content::WebContents* web_contents = shell_->web_contents();
if (!web_contents)
return frame;
CGFloat page_width = static_cast<CGFloat>(
web_contents->GetPreferredSize().width());
NSRect window_frame = [window frame];
// Never shrink from the current size on zoom.
CGFloat zoomed_width = std::max(page_width, NSWidth(window_frame));
// |frame| determines our maximum extents. We need to set the origin of the
// frame -- and only move it left if necessary.
if (window_frame.origin.x + zoomed_width > NSMaxX(frame))
frame.origin.x = NSMaxX(frame) - zoomed_width;
else
frame.origin.x = window_frame.origin.x;
// Set the width. Don't touch y or height.
frame.size.width = zoomed_width;
return frame;
}
- (void)windowDidBecomeMain:(NSNotification*)notification {
content::WebContents* web_contents = shell_->web_contents();
if (!web_contents)
@ -584,6 +619,7 @@ NativeWindowMac::NativeWindowMac(
NativeWindow* parent)
: NativeWindow(web_contents, options, parent),
is_kiosk_(false),
zoom_to_page_width_(false),
attention_request_id_(0),
title_bar_style_(NORMAL) {
int width = 800, height = 600;
@ -706,6 +742,8 @@ NativeWindowMac::NativeWindowMac(
if (!has_frame() || !use_content_size)
SetSize(gfx::Size(width, height));
options.Get(options::kZoomToPageWidth, &zoom_to_page_width_);
// Enable the NSView to accept first mouse event.
bool acceptsFirstMouse = false;
options.Get(options::kAcceptFirstMouse, &acceptsFirstMouse);

View file

@ -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 page width.
const char kZoomToPageWidth[] = "zoomToPageWidth";
// The requested title bar style for the window
const char kTitleBarStyle[] = "titleBarStyle";

View file

@ -34,6 +34,7 @@ extern const char kKiosk[];
extern const char kAlwaysOnTop[];
extern const char kAcceptFirstMouse[];
extern const char kUseContentSize[];
extern const char kZoomToPageWidth[];
extern const char kTitleBarStyle[];
extern const char kAutoHideMenuBar[];
extern const char kEnableLargerThanScreen[];

View file

@ -205,6 +205,12 @@ It creates a new `BrowserWindow` with native properties as set by the `options`.
* `vibrancy` String - Add a type of vibrancy effect to the window, only on
macOS. Can be `appearance-based`, `light`, `dark`, `titlebar`, `selection`,
`menu`, `popover`, `sidebar`, `medium-light` or `ultra-dark`.
* `zoomToPageWidth` Boolean - Controls the behavior on macOS when
option-clicking the green stoplight button on the toolbar or by clicking the
Window > Zoom menu item. If `true`, the window will grow to the preferred
width of the web page when zoomed, `false` will cause it to zoom to the
width of the screen. This will also affect the behavior when calling
`maximize()` directly. Default is `false`.
* `webPreferences` Object (optional) - Settings of web page's features.
* `devTools` Boolean (optional) - Whether to enable DevTools. If it is set to `false`, can not use `BrowserWindow.webContents.openDevTools()` to open DevTools. Default is `true`.
* `nodeIntegration` Boolean (optional) - Whether node integration is enabled. Default

View file

@ -626,6 +626,22 @@ describe('browser-window module', function () {
})
})
describe('"zoomToPageWidth" option', function () {
it('sets the window width to the page width when used', function () {
if (process.platform !== 'darwin') return this.skip()
w.destroy()
w = new BrowserWindow({
show: false,
width: 500,
height: 400,
zoomToPageWidth: true
})
w.maximize()
assert.equal(w.getSize()[0], 500)
})
})
describe('"web-preferences" option', function () {
afterEach(function () {
ipcMain.removeAllListeners('answer')