Skip to content

Commit

Permalink
feat: browser window setaspectratio function activate in windows os (e…
Browse files Browse the repository at this point in the history
  • Loading branch information
MadfishDT committed Mar 6, 2019
1 parent 6cb7b8d commit 102f4eb
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
1 change: 1 addition & 0 deletions atom/browser/native_window_views.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ class NativeWindowViews : public NativeWindow,
LPARAM l_param,
LRESULT* result) override;
void HandleSizeEvent(WPARAM w_param, LPARAM l_param);
void HandleSizingEvent(WPARAM w_param, LPARAM l_param);
void SetForwardMouseMessages(bool forward);
static LRESULT CALLBACK SubclassProc(HWND hwnd,
UINT msg,
Expand Down
48 changes: 48 additions & 0 deletions atom/browser/native_window_views_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ bool NativeWindowViews::PreHandleMSG(UINT message,
return false;
case WM_SIZING: {
bool prevent_default = false;
HandleSizingEvent(w_param, l_param);
NotifyWindowWillResize(gfx::Rect(*reinterpret_cast<RECT*>(l_param)),
&prevent_default);
if (prevent_default) {
Expand Down Expand Up @@ -299,6 +300,53 @@ bool NativeWindowViews::PreHandleMSG(UINT message,
}
}

void NativeWindowViews::HandleSizingEvent(WPARAM w_param, LPARAM l_param) {
double aspect_ratio = GetAspectRatio();
// aspect_ratio width/height
if (aspect_ratio == 0) {
return;
}
RECT* window_rect;
window_rect = (RECT*)l_param;
int width = window_rect->right - window_rect->left;
int height = window_rect->bottom - window_rect->top;

double result_width = 0;
double result_height = 0;

switch ((UINT)w_param) {
case WMSZ_LEFT:
case WMSZ_RIGHT:
result_width = width;
result_height = (double)width / aspect_ratio;
break;
case WMSZ_TOP:
case WMSZ_BOTTOM:
result_width = (double)height * aspect_ratio;
result_height = height;
break;
case WMSZ_TOPLEFT:
case WMSZ_TOPRIGHT:
case WMSZ_BOTTOMLEFT:
case WMSZ_BOTTOMRIGHT:
result_width = (double)height * aspect_ratio;
result_height = height;
break;
default:
break;
}
gfx::Size ratio_fit_size(result_width, result_height);
gfx::Size min_fit_size = GetMinimumSize();
if (min_fit_size.width() >= result_width ||
min_fit_size.height() >= result_height) {
return;
} else {
// SetSize(ratio_fit_size, false);
window_rect->right = window_rect->left + result_width;
window_rect->bottom = window_rect->top + result_height;
}
}

void NativeWindowViews::HandleSizeEvent(WPARAM w_param, LPARAM l_param) {
// Here we handle the WM_SIZE event in order to figure out what is the current
// window state and notify the user accordingly.
Expand Down
6 changes: 3 additions & 3 deletions docs/api/browser-window.md
Original file line number Diff line number Diff line change
Expand Up @@ -854,12 +854,12 @@ 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_ _Windows_

* `aspectRatio` Float - The aspect ratio to maintain for some portion of the
content view.
* `extraSize` [Size](structures/size.md) - The extra size not to be included while
maintaining the aspect ratio.
* `extraSize` [Size](structures/size.md) - The extra size not to be included while,
maintaining the aspect ratio. this pamameter is not worked on _Windows_ OS

This will make a window maintain an aspect ratio. The extra size allows a
developer to have space, specified in pixels, not included within the aspect
Expand Down

0 comments on commit 102f4eb

Please sign in to comment.