1
0
mirror of https://github.com/mpv-player/mpv synced 2025-01-05 14:40:43 +00:00
mpv/video/out/cocoa/window.m
Ryan Goulden 2878c2a914 cocoa: fix native fullscreen
This fixes a couple of issues with the Cocoa `--native-fs` mode, primarily:

 - A ghost titlebar at the top of the screen in full screen:

   This was caused by the window constraining code kicking in during
   fullscreen. Simply returning the unconstrained rect from the constraining
   method fixes the problem.

 - Incorrect behavior when using the titlebar buttons to enter/exit
   fullscreen, as opposed to the OSD button.

   This was caused by mpv's internal fullscreen state going out of sync with
   the NSWindow's one. This was the case because `toggleFullScreen:`
   completely bypassed the normal event flow that mpv expects.

Signed-off-by: Ryan Goulden <percontation@gmail.com>

Change style for mpv, simplify and refactor some of the constraining code.

Signed-off-by: Stefano Pigozzi <stefano.pigozzi@gmail.com>
2014-04-28 21:32:58 +02:00

192 lines
5.4 KiB
Objective-C

/*
* This file is part of mpv.
*
* mpv is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* mpv is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with mpv. If not, see <http://www.gnu.org/licenses/>.
*/
#include <libavutil/common.h>
#include "input/keycodes.h"
#include "osdep/macosx_application.h"
#include "osdep/macosx_events.h"
#include "osdep/macosx_compat.h"
#include "video/out/cocoa/additions.h"
#include "video/out/cocoa_common.h"
#include "window.h"
@implementation MpvVideoWindow {
NSSize _queued_video_size;
}
@synthesize adapter = _adapter;
- (id)initWithContentRect:(NSRect)content_rect
styleMask:(NSUInteger)style_mask
backing:(NSBackingStoreType)buffering_type
defer:(BOOL)flag
{
if (self = [super initWithContentRect:content_rect
styleMask:style_mask
backing:buffering_type
defer:flag]) {
[self setBackgroundColor:[NSColor blackColor]];
[self setMinSize:NSMakeSize(50,50)];
}
return self;
}
- (void)windowDidResize:(NSNotification *) notification
{
[self.adapter setNeedsResize];
}
- (void)windowDidChangeBackingProperties:(NSNotification *)notification
{
[self.adapter setNeedsResize];
}
- (void)windowDidChangeScreenProfile:(NSNotification *)notification
{
[self.adapter didChangeWindowedScreenProfile:[self screen]];
}
- (BOOL)isInFullScreenMode
{
return !!([self styleMask] & NSFullScreenWindowMask);
}
- (void)setFullScreen:(BOOL)willBeFullscreen
{
if (willBeFullscreen != [self isInFullScreenMode]) {
[super toggleFullScreen:nil];
}
}
- (void)toggleFullScreen:(id)sender {
if ([self isInFullScreenMode]) {
[self.adapter putCommand:"set fullscreen no"];
} else {
[self.adapter putCommand:"set fullscreen yes"];
}
}
- (BOOL)canBecomeMainWindow { return YES; }
- (BOOL)canBecomeKeyWindow { return YES; }
- (BOOL)windowShouldClose:(id)sender
{
cocoa_put_key(MP_KEY_CLOSE_WIN);
// We have to wait for MPlayer to handle this,
// otherwise we are in trouble if the
// MP_KEY_CLOSE_WIN handler is disabled
return NO;
}
- (void)normalSize { [self mulSize:1.0f]; }
- (void)halfSize { [self mulSize:0.5f];}
- (void)doubleSize { [self mulSize:2.0f];}
- (void)mulSize:(float)multiplier
{
char cmd[50];
snprintf(cmd, sizeof(cmd), "set window-scale %f", multiplier);
[self.adapter putCommand:cmd];
}
- (NSRect)frameRect:(NSRect)f forCenteredContentSize:(NSSize)ns
{
NSRect cr = [self contentRectForFrameRect:f];
CGFloat dx = (cr.size.width - ns.width) / 2;
CGFloat dy = (cr.size.height - ns.height) / 2;
return NSInsetRect(f, dx, dy);
}
- (void)setCenteredContentSize:(NSSize)ns
{
[self setFrame:[self frameRect:[self frame] forCenteredContentSize:ns]
display:NO
animate:NO];
}
- (NSRect)constrainFrameRect:(NSRect)nf toScreen:(NSScreen *)screen
{
if ([self isInFullScreenMode])
return [super constrainFrameRect:nf toScreen:screen];
NSRect of = [self frame];
NSRect vf = [[self screen] visibleFrame];
if (NSMaxY(nf) > NSMaxY(vf)) {
// If the new window is bigger than the visible frame, make sure it's
// titlebar is visible and at the top of the visible frame.
nf.origin.y = NSMaxY(vf) - NSHeight(nf);
} else if (NSHeight(of) > NSHeight(vf)) {
// If the window is smaller than the visible frame, but it was bigger
// previously (so we ran the previous conditional branch), recenter
// the smaller window vertically.
nf.origin.y = (NSHeight(vf) - NSHeight(nf)) / 2;
}
return nf;
}
- (void)windowDidEndLiveResize:(NSNotification *)notification
{
[self setFrame:[self constrainFrameRect:self.frame toScreen:self.screen]
display:NO];
}
- (void)tryDequeueSize {
if (_queued_video_size.width <= 0.0 || _queued_video_size.height <= 0.0)
return;
if (![self.adapter isInFullScreenMode]) {
[self setContentAspectRatio:_queued_video_size];
[self setCenteredContentSize:_queued_video_size];
_queued_video_size = NSZeroSize;
}
}
- (void)queueNewVideoSize:(NSSize)new_size
{
if (NSEqualSizes(_queued_video_size, new_size))
return;
_queued_video_size = new_size;
[self tryDequeueSize];
}
- (void)windowDidBecomeMain:(NSNotification *)notification {
[self tryDequeueSize];
}
- (NSSize)window:(NSWindow *)window willUseFullScreenContentSize:(NSSize)size {
return window.screen.frame.size;
}
- (NSApplicationPresentationOptions)window:(NSWindow *)window
willUseFullScreenPresentationOptions:(NSApplicationPresentationOptions)opts {
return NSApplicationPresentationFullScreen |
NSApplicationPresentationAutoHideDock |
NSApplicationPresentationAutoHideMenuBar |
NSApplicationPresentationAutoHideToolbar;
}
- (void)windowDidExitFullScreen:(NSNotification *)notification {
[self tryDequeueSize];
}
@end