Skip to content

Commit a190495

Browse files
committed
Use bounds for converting window/content sizes
1 parent 4751f97 commit a190495

6 files changed

+72
-64
lines changed

atom/browser/native_window.cc

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -219,34 +219,46 @@ gfx::Point NativeWindow::GetPosition() {
219219
}
220220

221221
void NativeWindow::SetContentSize(const gfx::Size& size, bool animate) {
222-
SetSize(ContentSizeToWindowSize(size), animate);
222+
SetSize(ContentBoundsToWindowBounds(gfx::Rect(size)).size(), animate);
223223
}
224224

225225
gfx::Size NativeWindow::GetContentSize() {
226-
return WindowSizeToContentSize(GetSize());
226+
return GetContentBounds().size();
227+
}
228+
229+
gfx::Rect NativeWindow::GetContentBounds() {
230+
return WindowBoundsToContentBounds(GetBounds());
227231
}
228232

229233
void NativeWindow::SetSizeConstraints(
230234
const extensions::SizeConstraints& window_constraints) {
231235
extensions::SizeConstraints content_constraints(GetContentSizeConstraints());
232-
if (window_constraints.HasMaximumSize())
233-
content_constraints.set_maximum_size(
234-
WindowSizeToContentSize(window_constraints.GetMaximumSize()));
235-
if (window_constraints.HasMinimumSize())
236-
content_constraints.set_minimum_size(
237-
WindowSizeToContentSize(window_constraints.GetMinimumSize()));
236+
if (window_constraints.HasMaximumSize()) {
237+
gfx::Rect max_bounds = WindowBoundsToContentBounds(
238+
gfx::Rect(window_constraints.GetMaximumSize()));
239+
content_constraints.set_maximum_size(max_bounds.size());
240+
}
241+
if (window_constraints.HasMinimumSize()) {
242+
gfx::Rect min_bounds = WindowBoundsToContentBounds(
243+
gfx::Rect(window_constraints.GetMinimumSize()));
244+
content_constraints.set_minimum_size(min_bounds.size());
245+
}
238246
SetContentSizeConstraints(content_constraints);
239247
}
240248

241249
extensions::SizeConstraints NativeWindow::GetSizeConstraints() {
242250
extensions::SizeConstraints content_constraints = GetContentSizeConstraints();
243251
extensions::SizeConstraints window_constraints;
244-
if (content_constraints.HasMaximumSize())
245-
window_constraints.set_maximum_size(
246-
ContentSizeToWindowSize(content_constraints.GetMaximumSize()));
247-
if (content_constraints.HasMinimumSize())
248-
window_constraints.set_minimum_size(
249-
ContentSizeToWindowSize(content_constraints.GetMinimumSize()));
252+
if (content_constraints.HasMaximumSize()) {
253+
gfx::Rect max_bounds = ContentBoundsToWindowBounds(
254+
gfx::Rect(content_constraints.GetMaximumSize()));
255+
content_constraints.set_maximum_size(max_bounds.size());
256+
}
257+
if (content_constraints.HasMinimumSize()) {
258+
gfx::Rect min_bounds = ContentBoundsToWindowBounds(
259+
gfx::Rect(content_constraints.GetMinimumSize()));
260+
window_constraints.set_minimum_size(min_bounds.size());
261+
}
250262
return window_constraints;
251263
}
252264

atom/browser/native_window.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ class NativeWindow : public base::SupportsUserData,
9191
virtual gfx::Point GetPosition();
9292
virtual void SetContentSize(const gfx::Size& size, bool animate = false);
9393
virtual gfx::Size GetContentSize();
94-
virtual gfx::Rect GetContentBounds() = 0;
94+
virtual gfx::Rect GetContentBounds();
9595
virtual void SetSizeConstraints(
9696
const extensions::SizeConstraints& size_constraints);
9797
virtual extensions::SizeConstraints GetSizeConstraints();
@@ -239,9 +239,9 @@ class NativeWindow : public base::SupportsUserData,
239239
std::unique_ptr<SkRegion> DraggableRegionsToSkRegion(
240240
const std::vector<DraggableRegion>& regions);
241241

242-
// Converts between content size to window size.
243-
virtual gfx::Size ContentSizeToWindowSize(const gfx::Size& size) = 0;
244-
virtual gfx::Size WindowSizeToContentSize(const gfx::Size& size) = 0;
242+
// Converts between content bounds and window bounds.
243+
virtual gfx::Rect ContentBoundsToWindowBounds(const gfx::Rect& bounds) = 0;
244+
virtual gfx::Rect WindowBoundsToContentBounds(const gfx::Rect& bounds) = 0;
245245

246246
// Called when the window needs to update its draggable region.
247247
virtual void UpdateDraggableRegions(

atom/browser/native_window_mac.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ class NativeWindowMac : public NativeWindow {
4646
bool IsFullscreen() const override;
4747
void SetBounds(const gfx::Rect& bounds, bool animate = false) override;
4848
gfx::Rect GetBounds() override;
49-
gfx::Rect GetContentBounds() override;
5049
void SetContentSizeConstraints(
5150
const extensions::SizeConstraints& size_constraints) override;
5251
void SetResizable(bool resizable) override;
@@ -115,8 +114,8 @@ class NativeWindowMac : public NativeWindow {
115114

116115
private:
117116
// NativeWindow:
118-
gfx::Size ContentSizeToWindowSize(const gfx::Size& size) override;
119-
gfx::Size WindowSizeToContentSize(const gfx::Size& size) override;
117+
gfx::Rect ContentBoundsToWindowBounds(const gfx::Rect& bounds);
118+
gfx::Rect WindowBoundsToContentBounds(const gfx::Rect& bounds);
120119
void UpdateDraggableRegions(
121120
const std::vector<DraggableRegion>& regions) override;
122121

atom/browser/native_window_mac.mm

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -753,14 +753,6 @@ static bool FromV8(v8::Isolate* isolate, v8::Handle<v8::Value> val,
753753
return bounds;
754754
}
755755

756-
gfx::Rect NativeWindowMac::GetContentBounds() {
757-
NSRect frame = [window_ convertRectToScreen:[[window_ contentView] frame]];
758-
gfx::Rect bounds(frame.origin.x, 0, NSWidth(frame), NSHeight(frame));
759-
NSScreen* screen = [[NSScreen screens] objectAtIndex:0];
760-
bounds.set_y(NSHeight([screen frame]) - NSMaxY(frame));
761-
return bounds;
762-
}
763-
764756
void NativeWindowMac::SetContentSizeConstraints(
765757
const extensions::SizeConstraints& size_constraints) {
766758
auto convertSize = [this](const gfx::Size& size) {
@@ -1051,22 +1043,25 @@ static bool FromV8(v8::Isolate* isolate, v8::Handle<v8::Value> val,
10511043
return result;
10521044
}
10531045

1054-
gfx::Size NativeWindowMac::ContentSizeToWindowSize(const gfx::Size& size) {
1055-
if (!has_frame())
1056-
return size;
1057-
1058-
NSRect content = NSMakeRect(0, 0, size.width(), size.height());
1059-
NSRect frame = [window_ frameRectForContentRect:content];
1060-
return gfx::Size(frame.size);
1046+
gfx::Rect NativeWindowMac::ContentBoundsToWindowBounds(
1047+
const gfx::Rect& bounds) {
1048+
if (has_frame())
1049+
return gfx::Rect([window_ frameRectForContentRect:bounds.ToCGRect()]);
1050+
else
1051+
return bounds;
10611052
}
10621053

1063-
gfx::Size NativeWindowMac::WindowSizeToContentSize(const gfx::Size& size) {
1064-
if (!has_frame())
1065-
return size;
1066-
1067-
NSRect frame = NSMakeRect(0, 0, size.width(), size.height());
1068-
NSRect content = [window_ contentRectForFrameRect:frame];
1069-
return gfx::Size(content.size);
1054+
gfx::Rect NativeWindowMac::WindowBoundsToContentBounds(
1055+
const gfx::Rect& bounds) {
1056+
if (has_frame()) {
1057+
gfx::Rect content_bounds(
1058+
[window_ contentRectForFrameRect:bounds.ToCGRect()]);
1059+
int frame_height = bounds.height() - content_bounds.height();
1060+
content_bounds.set_y(content_bounds.y() + frame_height);
1061+
return content_bounds;
1062+
} else {
1063+
return bounds;
1064+
}
10701065
}
10711066

10721067
void NativeWindowMac::UpdateDraggableRegions(

atom/browser/native_window_views.cc

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ NativeWindowViews::NativeWindowViews(
316316
if (has_frame() &&
317317
options.Get(options::kUseContentSize, &use_content_size_) &&
318318
use_content_size_)
319-
size = ContentSizeToWindowSize(size);
319+
size = ContentBoundsToWindowBounds(gfx::Rect(size)).size();
320320

321321
window_->CenterWindow(size);
322322
Layout();
@@ -1150,47 +1150,49 @@ void NativeWindowViews::OnWidgetMove() {
11501150
NotifyWindowMove();
11511151
}
11521152

1153-
gfx::Size NativeWindowViews::ContentSizeToWindowSize(const gfx::Size& size) {
1153+
gfx::Rect NativeWindowViews::ContentBoundsToWindowBounds(
1154+
const gfx::Rect& bounds) {
11541155
if (!has_frame())
1155-
return size;
1156+
return bounds;
11561157

1157-
gfx::Size window_size(size);
1158+
gfx::Rect window_bounds(bounds);
11581159
#if defined(OS_WIN)
11591160
HWND hwnd = GetAcceleratedWidget();
1160-
gfx::Rect dpi_bounds = gfx::Rect(
1161-
gfx::Point(), display::win::ScreenWin::DIPToScreenSize(hwnd, size));
1162-
gfx::Rect window_bounds = display::win::ScreenWin::ScreenToDIPRect(
1161+
gfx::Rect dpi_bounds = display::win::ScreenWin::DIPToScreenRect(hwnd, bounds);
1162+
window_bounds = display::win::ScreenWin::ScreenToDIPRect(
11631163
hwnd,
11641164
window_->non_client_view()->GetWindowBoundsForClientBounds(dpi_bounds));
1165-
window_size = window_bounds.size();
11661165
#endif
11671166

11681167
if (menu_bar_ && menu_bar_visible_)
1169-
window_size.set_height(window_size.height() + kMenuBarHeight);
1170-
return window_size;
1168+
window_bounds.set_height(window_bounds.height() + kMenuBarHeight);
1169+
return window_bounds;
11711170
}
11721171

1173-
gfx::Size NativeWindowViews::WindowSizeToContentSize(const gfx::Size& size) {
1172+
gfx::Rect NativeWindowViews::WindowBoundsToContentBounds(
1173+
const gfx::Rect& bounds) {
11741174
if (!has_frame())
1175-
return size;
1175+
return bounds;
11761176

1177-
gfx::Size content_size(size);
1177+
gfx::Rect content_bounds(bounds);
11781178
#if defined(OS_WIN)
11791179
HWND hwnd = GetAcceleratedWidget();
1180-
content_size = display::win::ScreenWin::DIPToScreenSize(hwnd, content_size);
1180+
content_bounds.set_size(
1181+
display::win::ScreenWin::DIPToScreenSize(hwnd, content_bounds.size()));
11811182
RECT rect;
11821183
SetRectEmpty(&rect);
11831184
DWORD style = ::GetWindowLong(hwnd, GWL_STYLE);
11841185
DWORD ex_style = ::GetWindowLong(hwnd, GWL_EXSTYLE);
11851186
AdjustWindowRectEx(&rect, style, FALSE, ex_style);
1186-
content_size.set_width(content_size.width() - (rect.right - rect.left));
1187-
content_size.set_height(content_size.height() - (rect.bottom - rect.top));
1188-
content_size = display::win::ScreenWin::ScreenToDIPSize(hwnd, content_size);
1187+
content_bounds.set_width(content_bounds.width() - (rect.right - rect.left));
1188+
content_bounds.set_height(content_bounds.height() - (rect.bottom - rect.top));
1189+
content_bounds.set_size(
1190+
display::win::ScreenWin::ScreenToDIPSize(hwnd, content_bounds.size()));
11891191
#endif
11901192

11911193
if (menu_bar_ && menu_bar_visible_)
1192-
content_size.set_height(content_size.height() - kMenuBarHeight);
1193-
return content_size;
1194+
content_bounds.set_height(content_bounds.height() - kMenuBarHeight);
1195+
return content_bounds;
11941196
}
11951197

11961198
void NativeWindowViews::HandleKeyboardEvent(

atom/browser/native_window_views.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,8 @@ class NativeWindowViews : public NativeWindow,
165165
#endif
166166

167167
// NativeWindow:
168-
gfx::Size ContentSizeToWindowSize(const gfx::Size& size) override;
169-
gfx::Size WindowSizeToContentSize(const gfx::Size& size) override;
168+
gfx::Rect ContentBoundsToWindowBounds(const gfx::Rect& bounds) override;
169+
gfx::Rect WindowBoundsToContentBounds(const gfx::Rect& bounds) override;
170170
void HandleKeyboardEvent(
171171
content::WebContents*,
172172
const content::NativeWebKeyboardEvent& event) override;

0 commit comments

Comments
 (0)