From 49a52f9eff7cf914b9b377cf83c9058741bda69c Mon Sep 17 00:00:00 2001 From: Bjorn Winckler Date: Sat, 18 Oct 2008 17:34:54 +0200 Subject: [PATCH] Keep whole window visible on ":set lines=X columns=Y" --- src/MacVim/MMBackend.m | 11 +++++-- src/MacVim/MMVimController.m | 9 ++++-- src/MacVim/MMWindowController.h | 4 ++- src/MacVim/MMWindowController.m | 53 +++++++++++++++++++++++---------- src/MacVim/MacVim.h | 3 +- src/MacVim/MacVim.m | 3 +- 6 files changed, 58 insertions(+), 25 deletions(-) diff --git a/src/MacVim/MMBackend.m b/src/MacVim/MMBackend.m index 63e79f472f..0daa4d5132 100644 --- a/src/MacVim/MMBackend.m +++ b/src/MacVim/MMBackend.m @@ -1698,13 +1698,18 @@ static NSString *MMSymlinkWarningString = if (SetTextRowsMsgID == msgid || SetTextColumnsMsgID == msgid) { int dim[2] = { rows, cols }; d = [NSData dataWithBytes:dim length:2*sizeof(int)]; - msgid = SetTextDimensionsMsgID; + msgid = SetTextDimensionsReplyMsgID; } + if (SetTextDimensionsMsgID == msgid) + msgid = SetTextDimensionsReplyMsgID; + // NOTE! Vim doesn't call gui_mch_set_shellsize() after // gui_resize_shell(), so we have to manually set the rows and columns - // here. (MacVim doesn't change the rows and columns to avoid - // inconsistent states between Vim and MacVim.) + // here since MacVim doesn't change the rows and columns to avoid + // inconsistent states between Vim and MacVim. The message sent back + // indicates that it is a reply to a message that originated in MacVim + // since we need to be able to determine where a message originated. [self queueMessage:msgid data:d]; //NSLog(@"[VimTask] Resizing shell to %dx%d.", cols, rows); diff --git a/src/MacVim/MMVimController.m b/src/MacVim/MMVimController.m index b03a138da7..37d4c26de1 100644 --- a/src/MacVim/MMVimController.m +++ b/src/MacVim/MMVimController.m @@ -742,13 +742,16 @@ static BOOL isUnsafeMessage(int msgid); [windowController showTabBar:YES]; } else if (HideTabBarMsgID == msgid) { [windowController showTabBar:NO]; - } else if (SetTextDimensionsMsgID == msgid || LiveResizeMsgID == msgid) { + } else if (SetTextDimensionsMsgID == msgid || LiveResizeMsgID == msgid || + SetTextDimensionsReplyMsgID == msgid) { const void *bytes = [data bytes]; int rows = *((int*)bytes); bytes += sizeof(int); int cols = *((int*)bytes); bytes += sizeof(int); - [windowController setTextDimensionsWithRows:rows columns:cols - live:(LiveResizeMsgID==msgid)]; + [windowController setTextDimensionsWithRows:rows + columns:cols + isLive:(LiveResizeMsgID==msgid) + isReply:(SetTextDimensionsReplyMsgID==msgid)]; } else if (SetWindowTitleMsgID == msgid) { const void *bytes = [data bytes]; int len = *((int*)bytes); bytes += sizeof(int); diff --git a/src/MacVim/MMWindowController.h b/src/MacVim/MMWindowController.h index 246190e83e..fd26453a0a 100644 --- a/src/MacVim/MMWindowController.h +++ b/src/MacVim/MMWindowController.h @@ -22,6 +22,7 @@ MMVimView *vimView; BOOL setupDone; BOOL shouldResizeVimView; + BOOL keepOnScreen; BOOL fullscreenEnabled; NSString *windowAutosaveKey; MMFullscreenWindow *fullscreenWindow; @@ -39,7 +40,8 @@ - (void)showWindow; - (void)updateTabsWithData:(NSData *)data; - (void)selectTabWithIndex:(int)idx; -- (void)setTextDimensionsWithRows:(int)rows columns:(int)cols live:(BOOL)live; +- (void)setTextDimensionsWithRows:(int)rows columns:(int)cols isLive:(BOOL)live + isReply:(BOOL)reply; - (void)setTitle:(NSString *)title; - (void)setDocumentFilename:(NSString *)filename; - (void)setToolbar:(NSToolbar *)toolbar; diff --git a/src/MacVim/MMWindowController.m b/src/MacVim/MMWindowController.m index 3622828490..403c85c90e 100644 --- a/src/MacVim/MMWindowController.m +++ b/src/MacVim/MMWindowController.m @@ -72,7 +72,8 @@ @interface MMWindowController (Private) - (NSSize)contentSize; -- (void)resizeWindowToFitContentSize:(NSSize)contentSize; +- (void)resizeWindowToFitContentSize:(NSSize)contentSize + keepOnScreen:(BOOL)onScreen; - (NSSize)constrainContentSizeToScreenSize:(NSSize)contentSize; - (NSRect)constrainFrame:(NSRect)frame; - (void)updateResizeConstraints; @@ -272,7 +273,8 @@ setupDone = YES; [self updateResizeConstraints]; - [self resizeWindowToFitContentSize:[vimView desiredSize]]; + [self resizeWindowToFitContentSize:[vimView desiredSize] + keepOnScreen:YES]; } - (void)showWindow @@ -296,10 +298,11 @@ [vimView selectTabWithIndex:idx]; } -- (void)setTextDimensionsWithRows:(int)rows columns:(int)cols live:(BOOL)live +- (void)setTextDimensionsWithRows:(int)rows columns:(int)cols isLive:(BOOL)live + isReply:(BOOL)reply { - //NSLog(@"setTextDimensionsWithRows:%d columns:%d live:%s", rows, cols, - // live ? "YES" : "NO"); + //NSLog(@"setTextDimensionsWithRows:%d columns:%d isLive:%d isReply:%d", + // rows, cols, live, reply); // NOTE: The only place where the (rows,columns) of the vim view are // modified is here and when entering/leaving full-screen. Setting these @@ -311,11 +314,17 @@ // resize when this message is received. We refrain from changing the view // size when this flag is set, otherwise the window might jitter when the // user drags to resize the window. + // + // The 'reply' flag indicates that this resize originated in MacVim and + // that Vim is now replying to that resize to make sure that it comes into + // effect. [vimView setDesiredRows:rows columns:cols]; - if (setupDone && !live) + if (setupDone && !live) { shouldResizeVimView = YES; + keepOnScreen = !reply; + } } - (void)setTitle:(NSString *)title @@ -433,8 +442,11 @@ [[fullscreenWindow contentView] setNeedsDisplay:YES]; [fullscreenWindow centerView]; } else { - [self resizeWindowToFitContentSize:contentSize]; + [self resizeWindowToFitContentSize:contentSize + keepOnScreen:keepOnScreen]; } + + keepOnScreen = NO; } } @@ -534,7 +546,8 @@ // Sending of synchronous message failed. Force the window size to // match the last dimensions received from Vim, otherwise we end up // with inconsistent states. - [self resizeWindowToFitContentSize:[vimView desiredSize]]; + [self resizeWindowToFitContentSize:[vimView desiredSize] + keepOnScreen:NO]; } // If we saved the original title while resizing, restore it. @@ -836,6 +849,7 @@ } - (void)resizeWindowToFitContentSize:(NSSize)contentSize + keepOnScreen:(BOOL)onScreen { NSRect frame = [decoratedWindow frame]; NSRect contentRect = [decoratedWindow contentRectForFrameRect:frame]; @@ -844,22 +858,29 @@ contentRect.origin.y -= contentSize.height - contentRect.size.height; contentRect.size = contentSize; - frame = [decoratedWindow frameRectForContentRect:contentRect]; + NSRect newFrame = [decoratedWindow frameRectForContentRect:contentRect]; // Ensure that the window fits inside the visible part of the screen. NSRect maxFrame = [[decoratedWindow screen] visibleFrame]; maxFrame = [self constrainFrame:maxFrame]; - if (frame.size.width > maxFrame.size.width) { - frame.size.width = maxFrame.size.width; - frame.origin.x = maxFrame.origin.x; + if (newFrame.size.width > maxFrame.size.width) { + newFrame.size.width = maxFrame.size.width; + newFrame.origin.x = maxFrame.origin.x; } - if (frame.size.height > maxFrame.size.height) { - frame.size.height = maxFrame.size.height; - frame.origin.y = maxFrame.origin.y; + if (newFrame.size.height > maxFrame.size.height) { + newFrame.size.height = maxFrame.size.height; + newFrame.origin.y = maxFrame.origin.y; } - [decoratedWindow setFrame:frame display:YES]; + if (onScreen) { + if (newFrame.origin.y < maxFrame.origin.y) + newFrame.origin.y = maxFrame.origin.y; + if (NSMaxX(newFrame) > NSMaxX(maxFrame)) + newFrame.origin.x = NSMaxX(maxFrame) - newFrame.size.width; + } + + [decoratedWindow setFrame:newFrame display:YES]; } - (NSSize)constrainContentSizeToScreenSize:(NSSize)contentSize diff --git a/src/MacVim/MacVim.h b/src/MacVim/MacVim.h index d5192faf7f..19c2ea636b 100644 --- a/src/MacVim/MacVim.h +++ b/src/MacVim/MacVim.h @@ -124,6 +124,8 @@ enum { SetTextRowsMsgID, SetTextColumnsMsgID, SetTextDimensionsMsgID, + LiveResizeMsgID, + SetTextDimensionsReplyMsgID, SetWindowTitleMsgID, ScrollWheelMsgID, MouseDownMsgID, @@ -166,7 +168,6 @@ enum { SetPreEditPositionMsgID, TerminateNowMsgID, XcodeModMsgID, - LiveResizeMsgID, EnableAntialiasMsgID, DisableAntialiasMsgID, SetVimStateMsgID, diff --git a/src/MacVim/MacVim.m b/src/MacVim/MacVim.m index 35b7407d65..51ac78b473 100644 --- a/src/MacVim/MacVim.m +++ b/src/MacVim/MacVim.m @@ -31,6 +31,8 @@ char *MessageStrings[] = "SetTextRowsMsgID", "SetTextColumsMsgID", "SetTextDimensionsMsgID", + "LiveResizeMsgID", + "SetTextDimensionsReplyMsgID", "SetWindowTitleMsgID", "ScrollWheelMsgID", "MouseDownMsgID", @@ -73,7 +75,6 @@ char *MessageStrings[] = "SetPreEditPositionMsgID", "TerminateNowMsgID", "XcodeModMsgID", - "LiveResizeMsgID", "EnableAntialiasMsgID", "DisableAntialiasMsgID", "SetVimStateMsgID",