0
0
mirror of https://github.com/mpv-player/mpv.git synced 2024-09-20 12:02:23 +02:00

cocoa_common: refactor centered resize

This refactor makes the code easier to understand. Also corrects a bug that
caused the window to move to the left when the new size was bigger than the
visible frame.
This commit is contained in:
Stefano Pigozzi 2013-04-16 22:46:50 +02:00
parent 0c1d95e81b
commit 33ffc28373

View File

@ -905,33 +905,30 @@ void create_menu()
- (void)setCenteredContentSize:(NSSize)ns
{
NSRect nf = [self frame];
NSRect vf = [[self screen] visibleFrame];
NSRect cb = [[self contentView] bounds];
int title_height = nf.size.height - cb.size.height;
double ratio = (double)ns.width / (double)ns.height;
#define get_center(x) NSMakePoint(CGRectGetMidX((x)), CGRectGetMidY((x)))
NSRect of = [self frame];
NSRect vf = [[self screen] visibleFrame];
NSRect cb = [[self contentView] bounds];
int title_h = of.size.height - cb.size.height;
// clip the new size to the visibleFrame's size if needed
if (ns.width > vf.size.width || ns.height + title_height > vf.size.height) {
ns = vf.size;
ns.height -= title_height; // make space for the title bar
NSPoint old_center = get_center(of);
if (ns.width > ns.height) {
ns.height = ((double)ns.width * 1/ratio + 0.5);
} else {
ns.width = ((double)ns.height * ratio + 0.5);
}
}
NSRect nf =
NSMakeRect(vf.origin.x, vf.origin.y, ns.width, ns.height + title_h);
int dw = nf.size.width - ns.width;
int dh = nf.size.height - ns.height - title_height;
// clip frame to screens visibile frame
nf = CGRectIntersection(nf, vf);
nf.origin.x += dw / 2;
nf.origin.y += dh / 2;
NSPoint new_center = get_center(nf);
NSRect new_frame =
NSMakeRect(nf.origin.x, nf.origin.y, ns.width, ns.height + title_height);
[self setFrame:new_frame display:YES animate:NO];
int dx0 = old_center.x - new_center.x;
int dy0 = old_center.y - new_center.y;
nf.origin.x += dx0;
nf.origin.y += dy0;
[self setFrame:nf display:YES animate:NO];
#undef get_center
}
- (void)setContentSize:(NSSize)ns keepCentered:(BOOL)keepCentered