[Mac] Improve modal sheet positioning during window resizing.

Change sheet repositioning to be based on NSWindowDidResizeNotification,
because it tracks window resizing much better than
NSViewFrameDidChangeNotification.
The modal sheet should now track the parent window's resizing much more
smoothly.

BUG=358627

Review URL: https://codereview.chromium.org/299573002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@272421 0039d316-1c4b-4281-b951-d872f2087c98
parent 3845b974
......@@ -35,7 +35,7 @@ NSValue* GetKeyForParentWindow(NSWindow* parent_window) {
- (ConstrainedWindowSheetInfo*)
findSheetInfoForSheet:(id<ConstrainedWindowSheet>)sheet;
- (void)onParentWindowWillClose:(NSNotification*)note;
- (void)onParentViewFrameDidChange:(NSNotification*)note;
- (void)onParentWindowSizeDidChange:(NSNotification*)note;
- (void)updateSheetPosition:(NSView*)parentView;
- (NSRect)overlayWindowFrameForParentView:(NSView*)parentView;
- (NSPoint)originForSheetSize:(NSSize)sheetSize
......@@ -130,13 +130,12 @@ NSValue* GetKeyForParentWindow(NSWindow* parent_window) {
if (!activeView_.get())
activeView_.reset([parentView retain]);
// Observer the parent view's frame.
[parentView setPostsFrameChangedNotifications:YES];
// Observe the parent window's size.
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(onParentViewFrameDidChange:)
name:NSViewFrameDidChangeNotification
object:parentView];
selector:@selector(onParentWindowSizeDidChange:)
name:NSWindowDidResizeNotification
object:parentWindow_];
// Create an invisible overlay window.
NSRect rect = [self overlayWindowFrameForParentView:parentView];
......@@ -228,11 +227,8 @@ NSValue* GetKeyForParentWindow(NSWindow* parent_window) {
}
}
- (void)onParentViewFrameDidChange:(NSNotification*)note {
NSView* parentView = [note object];
if (![activeView_ isEqual:parentView])
return;
[self updateSheetPosition:parentView];
- (void)onParentWindowSizeDidChange:(NSNotification*)note {
[self updateSheetPosition:activeView_];
}
- (void)updateSheetPosition:(NSView*)parentView {
......@@ -293,8 +289,8 @@ NSValue* GetKeyForParentWindow(NSWindow* parent_window) {
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:NSViewFrameDidChangeNotification
object:[info parentView]];
name:NSWindowDidResizeNotification
object:parentWindow_];
[parentWindow_ removeChildWindow:[info overlayWindow]];
[[info sheet] closeSheetWithAnimation:withAnimation];
......
......@@ -136,6 +136,11 @@ class ConstrainedWindowSheetControllerTest : public CocoaTest {
EXPECT_EQ(expected_x, NSMinX(sheet_frame));
}
CGFloat GetSheetYOffset(NSRect sheet_frame, NSView* parent_view) {
return NSMaxY(sheet_frame) -
NSMaxY(GetViewFrameInScreenCoordinates(parent_view));
}
base::scoped_nsobject<NSWindow> sheet_window_;
base::scoped_nsobject<CustomConstrainedWindowSheet> sheet_;
base::scoped_nsobject<ConstrainedWindowSheetController> controller_;
......@@ -254,6 +259,31 @@ TEST_F(ConstrainedWindowSheetControllerTest, ResizeHiddenSheet) {
EXPECT_EQ(NSHeight(new_inactive_frame), NSHeight(new_active_frame));
}
// Test resizing parent window keeps the sheet anchored.
TEST_F(ConstrainedWindowSheetControllerTest, ResizeParentWindow) {
[controller_ showSheet:sheet_ forParentView:active_tab_view_];
CGFloat sheet_offset =
GetSheetYOffset([sheet_window_ frame], active_tab_view_);
// Test 3x3 different parent window sizes.
CGFloat insets[] = {-10, 0, 10};
NSRect old_frame = [test_window() frame];
for (size_t x = 0; x < sizeof(insets) / sizeof(CGFloat); x++) {
for (size_t y = 0; y < sizeof(insets) / sizeof(CGFloat); y++) {
NSRect resized_frame = NSInsetRect(old_frame, insets[x], insets[y]);
[test_window() setFrame:resized_frame display:YES];
NSRect sheet_frame = [sheet_window_ frame];
// Y pos should track parent view's position.
EXPECT_EQ(sheet_offset, GetSheetYOffset(sheet_frame, active_tab_view_));
// X pos should be centered on parent view.
VerifySheetXPosition(sheet_frame, active_tab_view_);
}
}
}
// Test system sheets.
TEST_F(ConstrainedWindowSheetControllerTest, SystemSheet) {
base::scoped_nsobject<ConstrainedWindowSystemSheetTest> system_sheet(
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment