Merge pull request #2250 from EyeSee360/master
Maintain an aspect ratio for content within a window
This commit is contained in:
commit
ea1b89c699
6 changed files with 66 additions and 0 deletions
|
@ -249,6 +249,10 @@ bool Window::IsFullscreen() {
|
|||
return window_->IsFullscreen();
|
||||
}
|
||||
|
||||
void Window::SetAspectRatio(double aspectRatio, gfx::Size extraSize) {
|
||||
window_->SetAspectRatio(aspectRatio, extraSize);
|
||||
}
|
||||
|
||||
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("setAspectRatio", &Window::SetAspectRatio)
|
||||
.SetMethod("getBounds", &Window::GetBounds)
|
||||
.SetMethod("setBounds", &Window::SetBounds)
|
||||
.SetMethod("getSize", &Window::GetSize)
|
||||
|
|
|
@ -95,6 +95,7 @@ class Window : public mate::TrackableObject<Window>,
|
|||
bool IsMinimized();
|
||||
void SetFullScreen(bool fullscreen);
|
||||
bool IsFullscreen();
|
||||
void SetAspectRatio(double aspectRatio, gfx::Size extraSize);
|
||||
void SetBounds(const gfx::Rect& bounds);
|
||||
gfx::Rect GetBounds();
|
||||
void SetSize(int width, int height);
|
||||
|
|
|
@ -195,6 +195,20 @@ void NativeWindow::InitFromOptions(const mate::Dictionary& options) {
|
|||
Show();
|
||||
}
|
||||
|
||||
double NativeWindow::GetAspectRatio() {
|
||||
return aspectRatio_;
|
||||
}
|
||||
|
||||
gfx::Size NativeWindow::GetAspectRatioExtraSize() {
|
||||
return aspectRatioExtraSize_;
|
||||
}
|
||||
|
||||
void NativeWindow::SetAspectRatio(double aspectRatio,
|
||||
const gfx::Size& extraSize) {
|
||||
aspectRatio_ = aspectRatio;
|
||||
aspectRatioExtraSize_ = extraSize;
|
||||
}
|
||||
|
||||
void NativeWindow::SetSize(const gfx::Size& size) {
|
||||
SetBounds(gfx::Rect(GetPosition(), size));
|
||||
}
|
||||
|
|
|
@ -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 GetAspectRatio();
|
||||
virtual gfx::Size GetAspectRatioExtraSize();
|
||||
virtual void SetAspectRatio(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,11 @@ 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 aspectRatio_ = 0.0;
|
||||
gfx::Size aspectRatioExtraSize_;
|
||||
|
||||
// The page this window is viewing.
|
||||
brightray::InspectableWebContents* inspectable_web_contents_;
|
||||
|
||||
|
|
|
@ -95,6 +95,33 @@ static const CGFloat kAtomWindowCornerRadius = 4.0;
|
|||
shell_->NotifyWindowBlur();
|
||||
}
|
||||
|
||||
- (NSSize)windowWillResize:(NSWindow *)sender toSize:(NSSize)frameSize {
|
||||
NSSize newSize = frameSize;
|
||||
double aspectRatio = shell_->GetAspectRatio();
|
||||
|
||||
if (aspectRatio > 0.0) {
|
||||
gfx::Size windowFrameSize = shell_->GetSize();
|
||||
gfx::Size contentViewSize = shell_->GetContentSize();
|
||||
gfx::Size aspectRatioExtraSize = shell_->GetAspectRatioExtraSize();
|
||||
double extraWidthPlusFrame = windowFrameSize.width() - contentViewSize.width() + aspectRatioExtraSize.width();
|
||||
double extraHeightPlusFrame = windowFrameSize.height() - contentViewSize.height() + aspectRatioExtraSize.height();
|
||||
|
||||
newSize.width = roundf(((frameSize.height - extraHeightPlusFrame) * aspectRatio) + 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) / aspectRatio) + extraHeightPlusFrame);
|
||||
}
|
||||
else {
|
||||
newSize.height = roundf(((frameSize.width - extraWidthPlusFrame) / aspectRatio) + extraHeightPlusFrame);
|
||||
newSize.width = roundf(((newSize.height - extraHeightPlusFrame) * aspectRatio) + extraWidthPlusFrame);
|
||||
}
|
||||
}
|
||||
|
||||
return newSize;
|
||||
}
|
||||
|
||||
- (void)windowDidResize:(NSNotification*)notification {
|
||||
if (!shell_->has_frame())
|
||||
shell_->ClipWebView();
|
||||
|
|
|
@ -361,6 +361,17 @@ Sets whether the window should be in fullscreen mode.
|
|||
|
||||
Returns whether the window is in fullscreen mode.
|
||||
|
||||
### BrowserWindow.setAspectRatio(aspectRatio, [extraSize])
|
||||
|
||||
* `aspectRatio` The aspect ratio we want to maintain for some portion of the content view.
|
||||
* `rect` Object - The extra size to not be included in the aspect ratio to be maintained.
|
||||
* `width` Integer
|
||||
* `height` Integer
|
||||
|
||||
This will have a window maintain an aspect ratio. The extra size allows a developer to be able to have space, specifified in pixels, not included within the aspect ratio calculations. This API already takes into account the difference between a window's size and it's content size.
|
||||
|
||||
Consider a normal window with an HD video player and associated controls. Perhaps there are 15 pixels of controls on the left edge, 25 pixels of controls on the right edge and 50 pixels of controls below the player. In order to maintain a 16:9 aspect ratio (standard aspect ratio for HD @1920x1080) within the player itself we would call this function with arguments of 16/9 and [ 40, 50 ]. The second argument doesn't care where the extra width and height are within the content view — only that they exist. Just sum any extra width and height areas you have within the overall content view.
|
||||
|
||||
### BrowserWindow.setBounds(options)
|
||||
|
||||
* `options` Object
|
||||
|
|
Loading…
Reference in a new issue