Merge pull request #164 from gnustep/NSDrawer_position_bug54715

Please let me know if any additional suggestions or issues.  GC
This commit is contained in:
Gregory Casamento 2023-01-14 03:56:49 -05:00 committed by GitHub
commit 4b13050e70
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 209 additions and 149 deletions

View file

@ -1,3 +1,8 @@
2023-01-03 Gregory John Casamento <greg.casamento@gmail.com>
* Source/NSDrawer.m: Open drawer on the opposite edge if needed when
close to the boundary of the screen.
2022-12-29 Richard Frith-Macdonald <rfm@gnu.org>
* ChangeLog: Update for new release

View file

@ -42,27 +42,33 @@
#import "AppKit/NSView.h"
#import "AppKit/NSDrawer.h"
#import "AppKit/NSGraphics.h"
#import "AppKit/NSScreen.h"
static NSNotificationCenter *nc = nil;
@interface GSDrawerWindow : NSWindow
{
NSWindow *_parentWindow;
NSWindow *_pendingParentWindow;
NSDrawer *_drawer;
id _container;
NSBox *_borderBox;
NSSize _borderSize;
NSTimer *_timer;
NSRect _latestParentFrame;
BOOL _wasOpen;
NSWindow *_parentWindow;
NSWindow *_pendingParentWindow;
NSDrawer *_drawer;
id _container;
NSBox *_borderBox;
NSSize _borderSize;
NSTimer *_timer;
NSRect _latestParentFrame;
BOOL _wasOpen;
NSRectEdge _currentEdge;
}
- (NSRect) frameFromParentWindowFrameInState:(NSInteger)state;
- (void) configureContainer: (NSRect)contentRect;
// open/close
- (void) openOnEdge;
- (void) openOnEdge: (NSRectEdge)edge;
- (void) closeOnEdge: (NSRectEdge)edge;
- (void) closeOnEdge;
- (void) slideOpen:(BOOL)opening;
- (void) slideOpen: (BOOL)opening onEdge: (NSRectEdge)edge;
- (void) slideOpen: (BOOL)opening;
- (void) startTimer;
- (void) stopTimer;
@ -80,9 +86,11 @@ static NSNotificationCenter *nc = nil;
- (void) handleWindowMiniaturize: (NSNotification *)notification;
- (void) handleWindowDeminiaturize: (NSNotification *)notification;
- (void) handleWindowMove: (NSNotification *)notification;
@end
@implementation GSDrawerWindow
+ (void) initialize
{
if (self == [GSDrawerWindow class])
@ -103,47 +111,46 @@ static NSNotificationCenter *nc = nil;
}
self = [super initWithContentRect: contentRect
styleMask: aStyle
backing: bufferingType
defer: flag];
styleMask: aStyle
backing: bufferingType
defer: flag];
if (self != nil)
{
NSRect rect = contentRect;
NSRect border = contentRect;
NSSize containerContentSize;
[self configureContainer: contentRect];
}
return self;
}
rect.origin.x += 6;
rect.origin.y += 6;
rect.size.width -= 16;
rect.size.height -= 16;
- (void) configureContainer: (NSRect)contentRect
{
NSRect rect = contentRect;
NSSize containerContentSize;
border.origin.x += 1;
border.origin.y += 1;
border.size.width -= 2;
border.size.height -= 2;
_borderBox = [[NSBox alloc] initWithFrame: border];
[_borderBox setTitle: @""];
[_borderBox setTitlePosition: NSNoTitle];
[_borderBox setBorderType: NSLineBorder];
[_borderBox setAutoresizingMask: NSViewWidthSizable | NSViewHeightSizable];
[_borderBox setContentViewMargins: NSMakeSize(0,0)];
[[super contentView] addSubview: _borderBox];
rect.origin.x += 1.0;
rect.origin.y -= 5.0;
if (_container == nil)
{
_container = [[NSBox alloc] initWithFrame: rect];
[[super contentView] addSubview: _container];
RELEASE(_container);
[_container setTitle: @""];
[_container setTitlePosition: NSNoTitle];
[_container setBorderType: NSBezelBorder];
[_container setAutoresizingMask: NSViewWidthSizable | NSViewHeightSizable];
[_container setContentViewMargins: NSMakeSize(2,2)];
[_borderBox addSubview: _container];
// determine the difference between the container's content size and the window content size
containerContentSize = [[_container contentView] frame].size;
_borderSize = NSMakeSize(contentRect.size.width - containerContentSize.width,
contentRect.size.height - containerContentSize.height);
[_container setContentViewMargins: NSMakeSize(2,2)];
}
return self;
else
{
[_container setFrame: rect];
}
// determine the difference between the container's content size and the window content size
containerContentSize = [[_container contentView] frame].size;
_borderSize = NSMakeSize(contentRect.size.width - containerContentSize.width,
contentRect.size.height - containerContentSize.height);
}
- (id) container
@ -151,20 +158,21 @@ static NSNotificationCenter *nc = nil;
return _container;
}
- (NSRect) frameFromParentWindowFrameInState:(NSInteger)state
- (NSRect) frameFromParentWindowFrameInState: (NSInteger)state
{
NSRect newFrame = [_parentWindow frame];
CGFloat totalOffset = [_drawer leadingOffset] + [_drawer trailingOffset];
NSRectEdge edge = [_drawer preferredEdge];
BOOL opened = (state == NSDrawerOpenState || state == NSDrawerOpeningState);
NSSize size = [_parentWindow frame].size; // [_drawer maxContentSize];
NSSize size = [_parentWindow frame].size;
NSRect windowContentRect = [[_parentWindow contentView] frame];
CGFloat windowHeightWithoutTitleBar = windowContentRect.origin.y + windowContentRect.size.height; // FIXME: This should probably add the toolbar height too, if the window has a toolbar
if (edge == NSMinXEdge) // left
CGFloat windowHeightWithoutTitleBar = windowContentRect.origin.y + windowContentRect.size.height;
// FIXME: This should probably add the toolbar height too, if the window has a toolbar
[self configureContainer: windowContentRect];
if (_currentEdge == NSMinXEdge) // left
{
if (opened)
newFrame.size.width = [_drawer minContentSize].width + _borderSize.width;
newFrame.size.width = [_drawer maxContentSize].width + _borderSize.width;
else
newFrame.size.width = 16;
@ -173,10 +181,10 @@ static NSNotificationCenter *nc = nil;
if (opened)
newFrame.origin.x -= newFrame.size.width;
}
else if (edge == NSMinYEdge) // bottom
else if (_currentEdge == NSMinYEdge) // bottom
{
if (opened)
newFrame.size.height = [_drawer minContentSize].height + _borderSize.height;
newFrame.size.height = [_drawer maxContentSize].height + _borderSize.height;
else
newFrame.size.height = 16;
@ -185,10 +193,10 @@ static NSNotificationCenter *nc = nil;
if (opened)
newFrame.origin.y -= newFrame.size.height;
}
else if (edge == NSMaxXEdge) // right
else if (_currentEdge == NSMaxXEdge) // right
{
if (opened)
newFrame.size.width = [_drawer minContentSize].width + _borderSize.width;
newFrame.size.width = [_drawer maxContentSize].width + _borderSize.width;
else
newFrame.size.width = 16;
@ -198,10 +206,10 @@ static NSNotificationCenter *nc = nil;
if (!opened)
newFrame.origin.x -= newFrame.size.width;
}
else if (edge == NSMaxYEdge) // top
else if (_currentEdge == NSMaxYEdge) // top
{
if (opened)
newFrame.size.height = [_drawer minContentSize].height + _borderSize.height;
newFrame.size.height = [_drawer maxContentSize].height + _borderSize.height;
else
newFrame.size.height = 16;
@ -215,8 +223,6 @@ static NSNotificationCenter *nc = nil;
return newFrame;
}
- (BOOL) canBecomeKeyWindow
{
return YES;
@ -229,42 +235,18 @@ static NSNotificationCenter *nc = nil;
- (void) becomeKeyWindow
{
[_parentWindow orderFrontRegardless]; // so clicking on the drawer will bring the parent to the front
[super becomeKeyWindow];
[_parentWindow orderFrontRegardless]; // so clicking on the drawer will bring the parent to the front
[super becomeKeyWindow];
}
/*
- (void) orderFront: (id)sender
{
NSPoint holdOrigin = [self frame].origin;
NSPoint newOrigin = NSMakePoint(-10000,-10000);
NSRect tempFrame = [self frame];
// order the window under the parent...
tempFrame.origin = newOrigin;
[self setFrame: tempFrame display: NO];
[super orderFront: sender];
// [_parentWindow orderWindow: NSWindowAbove relativeTo: [self windowNumber]];
tempFrame.origin = holdOrigin;
// [self setFrame: tempFrame display: YES];
}
*/
/*
- (void) orderFront: (id)sender
{
[super orderWindow:NSWindowBelow relativeTo:[_parentWindow windowNumber]];
}
*/
- (void) startTimer
{
NSTimeInterval time = 0.1;
_timer = [NSTimer scheduledTimerWithTimeInterval: time
target: self
selector: @selector(_timedWindowReset)
userInfo: nil
repeats: YES];
target: self
selector: @selector(_timedWindowReset)
userInfo: nil
repeats: YES];
}
- (void) stopTimer
@ -283,36 +265,31 @@ static NSNotificationCenter *nc = nil;
[super orderOut: sender];
}
/*
- (void) orderWindow: (NSWindowOrderingMode)place relativeTo: (int)windowNum
{
NSLog(@"Ordering window....");
[super orderWindow: place relativeTo: windowNum];
}
*/
// set the _borderBox to not resize during the slide,
// and attach it to the appropriate edge instead
- (void) lockBorderBoxForSliding
{
// set the _borderBox to not resize during the slide, and attach it to the appropriate edge instead
NSRectEdge edge = [_drawer preferredEdge];
NSUInteger resizeMask = 0;
if (edge == NSMinXEdge) // left
if (_currentEdge == NSMinXEdge) // left
{
resizeMask = NSViewMaxXMargin;
}
else if (edge == NSMinYEdge) // bottom
else if (_currentEdge == NSMinYEdge) // bottom
{
resizeMask = NSViewMaxYMargin;
}
else if (edge == NSMaxXEdge) // right
else if (_currentEdge == NSMaxXEdge) // right
{
resizeMask = NSViewMinXMargin;
}
else if (edge == NSMaxYEdge) // top
else if (_currentEdge == NSMaxYEdge) // top
{
resizeMask = NSViewMinYMargin;
}
[_borderBox setAutoresizingMask: resizeMask]; // don't resize -- just give appearance of sliding
// don't resize -- just give appearance of sliding
[_borderBox setAutoresizingMask: resizeMask];
}
- (void) unlockBorderBoxAfterSliding
@ -321,28 +298,35 @@ static NSNotificationCenter *nc = nil;
[_borderBox setAutoresizingMask: NSViewWidthSizable | NSViewHeightSizable];
}
- (void) openOnEdge
// prepare drawer contents before sliding...
- (void) openOnEdge: (NSRectEdge)edge
{
// prepare drawer contents before sliding...
NSRect frame = [self frameFromParentWindowFrameInState:NSDrawerOpenState];
_currentEdge = edge;
[self setFrame:frame display: YES]; // make sure it's the full (open) size before locking the borderBox
if ([_parentWindow isVisible]) // don't order front until parent window is visible
{
[self lockBorderBoxForSliding];
[self slideOpen: YES onEdge: edge];
[self orderFront: self];
[self slideOpen:YES];
[self performSelector:@selector(unlockBorderBoxAfterSliding) withObject:nil afterDelay:0.01];
}
[self startTimer];
}
- (void) closeOnEdge
- (void) openOnEdge
{
[self openOnEdge: _currentEdge];
}
- (void) closeOnEdge: (NSRectEdge)edge
{
NSRect frame;
[self stopTimer];
[self lockBorderBoxForSliding];
[self slideOpen:NO];
[self slideOpen: NO onEdge: edge];
[self orderOut: self];
frame = [self frameFromParentWindowFrameInState:NSDrawerOpenState];
@ -353,21 +337,30 @@ static NSNotificationCenter *nc = nil;
&& _pendingParentWindow != _parentWindow)
{
[self setParentWindow: _pendingParentWindow];
ASSIGN(_pendingParentWindow, nil);
}
_pendingParentWindow = nil;
}
}
- (void) slideOpen:(BOOL)opening
- (void) closeOnEdge
{
NSRect frame = [self frameFromParentWindowFrameInState:(opening?NSDrawerClosedState:NSDrawerOpenState)];
NSRect newFrame = [self frameFromParentWindowFrameInState:(opening?NSDrawerOpenState:NSDrawerClosedState)];
[self closeOnEdge: _currentEdge];
}
- (void) slideOpen: (BOOL)opening onEdge: (NSRectEdge)edge
{
NSUInteger count = 10;
NSRect frame = [self frameFromParentWindowFrameInState:
(opening ? NSDrawerClosedState : NSDrawerOpenState)];
NSRect newFrame = [self frameFromParentWindowFrameInState:
(opening ? NSDrawerOpenState : NSDrawerClosedState)];
NSTimeInterval slideDelay = 0.03;
NSDate *nextStop = [NSDate dateWithTimeIntervalSinceNow:slideDelay];
int count = 10;
CGFloat deltaX = (newFrame.origin.x - frame.origin.x) / count;
CGFloat deltaY = (newFrame.origin.y - frame.origin.y) / count;
CGFloat deltaW = (newFrame.size.width - frame.size.width) / count;
CGFloat deltaH = (newFrame.size.height - frame.size.height) / count;
_currentEdge = edge;
while (count--)
{
frame.origin.x += deltaX;
@ -376,11 +369,17 @@ static NSNotificationCenter *nc = nil;
frame.size.height += deltaH;
[self setFrame: frame display: YES];
[NSThread sleepUntilDate:nextStop];
nextStop = [nextStop addTimeInterval:slideDelay];
nextStop = [nextStop addTimeInterval: slideDelay];
}
[self setFrame:newFrame display: YES];
}
- (void) slideOpen: (BOOL)opening
{
[self slideOpen: opening onEdge: _currentEdge];
}
- (void) _resetWindowPosition
{
if ([_parentWindow isVisible]) // don't set our frame until parent window is visible
@ -550,31 +549,42 @@ static NSNotificationCenter *nc = nil;
- (id) initWithContentSize: (NSSize)contentSize
preferredEdge: (NSRectEdge)edge
{
{
self = [super init];
// initialize the drawer window...
_drawerWindow = [[GSDrawerWindow alloc]
initWithContentRect: NSMakeRect(0, 0, contentSize.width,
contentSize.height)
styleMask: 0
backing: NSBackingStoreBuffered
defer: NO];
[_drawerWindow setDrawer: self];
[_drawerWindow setReleasedWhenClosed: NO];
_preferredEdge = edge;
_currentEdge = edge;
_maxContentSize = NSMakeSize(200,200);
_minContentSize = contentSize; //NSMakeSize(50,50);
if (edge == NSMinXEdge || edge == NSMaxXEdge) {
_leadingOffset = 0.0; // for side drawers, top of drawer is immediately below the title bar
_trailingOffset = 10.0;
} else {
_leadingOffset = 10.0;
_trailingOffset = 10.0;
}
_state = NSDrawerClosedState;
if (self != nil)
{
NSRect rect = NSMakeRect(0, 0, contentSize.width, contentSize.height);
// initialize the drawer window...
_drawerWindow = [[GSDrawerWindow alloc]
initWithContentRect: rect
styleMask: 0
backing: NSBackingStoreBuffered
defer: NO];
[_drawerWindow setDrawer: self];
[_drawerWindow setReleasedWhenClosed: NO];
_preferredEdge = edge;
_currentEdge = edge;
_maxContentSize = contentSize;
_minContentSize = NSMakeSize(200,200);
// for side drawers, top of drawer is immediately below the title bar
if (edge == NSMinXEdge || edge == NSMaxXEdge)
{
_leadingOffset = 0.0;
_trailingOffset = 10.0;
}
else
{
_leadingOffset = 10.0;
_trailingOffset = 10.0;
}
_state = NSDrawerClosedState;
}
return self;
}
@ -590,6 +600,59 @@ static NSNotificationCenter *nc = nil;
}
// Opening and Closing
- (NSRectEdge) _computeEdge
{
NSRectEdge result = _preferredEdge;
NSRect windowFrame = [[self parentWindow] frame];
NSRect screenRect = [[NSScreen mainScreen] frame];
NSSize contentSize = [self contentSize];
// Diffs,,,
CGFloat top = screenRect.size.height - (windowFrame.origin.y + windowFrame.size.height);
CGFloat bottom = windowFrame.origin.y;
CGFloat right = screenRect.size.width - (windowFrame.origin.x + windowFrame.size.width);
CGFloat left = windowFrame.origin.x;
// Space left...
CGFloat topSpace = top - contentSize.height;
CGFloat bottomSpace = bottom - contentSize.height;
CGFloat rightSpace = right - contentSize.width;
CGFloat leftSpace = left - contentSize.width;
switch (_preferredEdge)
{
case NSMinXEdge:
if (leftSpace < 0.0 && rightSpace > leftSpace)
{
result = NSMaxXEdge;
}
break;
case NSMinYEdge:
if (bottomSpace < 0.0 && topSpace > bottomSpace)
{
result = NSMaxYEdge;
}
break;
case NSMaxXEdge:
if (rightSpace < 0.0 && leftSpace > rightSpace)
{
result = NSMinXEdge;
}
break;
case NSMaxYEdge:
if (topSpace < 0.0 && bottomSpace > topSpace)
{
result = NSMinYEdge;
}
break;
}
return result;
}
- (void) close
{
if (_state != NSDrawerOpenState)
@ -603,7 +666,7 @@ static NSNotificationCenter *nc = nil;
_state = NSDrawerClosingState;
[nc postNotificationName: NSDrawerWillCloseNotification object: self];
[_drawerWindow closeOnEdge];
[_drawerWindow closeOnEdge];
_state = NSDrawerClosedState;
[nc postNotificationName: NSDrawerDidCloseNotification object: self];
@ -616,7 +679,8 @@ static NSNotificationCenter *nc = nil;
- (void) open
{
[self openOnEdge: _preferredEdge];
NSRectEdge edge = [self _computeEdge];
[self openOnEdge: edge];
}
- (void) open: (id)sender
@ -639,7 +703,7 @@ static NSNotificationCenter *nc = nil;
[nc postNotificationName: NSDrawerWillOpenNotification object: self];
_currentEdge = edge;
[_drawerWindow openOnEdge];
[_drawerWindow openOnEdge: edge];
_state = NSDrawerOpenState;
[nc postNotificationName: NSDrawerDidOpenNotification object: self];
@ -677,16 +741,6 @@ static NSNotificationCenter *nc = nil;
- (void) setContentSize: (NSSize)size
{
// Check with min and max size
if (size.width < _minContentSize.width)
size.width = _minContentSize.width;
if (size.height < _minContentSize.height)
size.height = _minContentSize.height;
if (size.width > _maxContentSize.width)
size.width = _maxContentSize.width;
if (size.height > _maxContentSize.height)
size.height = _maxContentSize.height;
// Check with delegate
if ((_delegate != nil)
&& ([_delegate respondsToSelector:
@ -754,6 +808,7 @@ static NSNotificationCenter *nc = nil;
- (void) setContentView: (NSView *)aView
{
[[_drawerWindow container] setContentView: aView];
[self setContentSize: [aView frame].size];
}
- (void) setParentWindow: (NSWindow *)parent