1 /*****************************************************************************
2 * Windows.m: MacOS X interface module
3 *****************************************************************************
4 * Copyright (C) 2012-2014 VLC authors and VideoLAN
7 * Authors: Felix Paul Kühne <fkuehne -at- videolan -dot- org>
8 * David Fuhrmann <david dot fuhrmann at googlemail dot com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
27 #import "CoreInteraction.h"
28 #import "ControlsBar.h"
30 #import "CompatibilityFixes.h"
32 /*****************************************************************************
35 * Missing extension to NSWindow
36 *****************************************************************************/
38 @implementation VLCWindow
40 @synthesize hasActiveVideo=b_has_active_video;
41 @synthesize fullscreen=b_fullscreen;
43 - (id)initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)styleMask
44 backing:(NSBackingStoreType)backingType defer:(BOOL)flag
46 self = [super initWithContentRect:contentRect styleMask:styleMask backing:backingType defer:flag];
48 /* we don't want this window to be restored on relaunch */
49 if (!OSX_SNOW_LEOPARD)
50 [self setRestorable:NO];
55 - (void)setCanBecomeKeyWindow: (BOOL)canBecomeKey
57 b_isset_canBecomeKeyWindow = YES;
58 b_canBecomeKeyWindow = canBecomeKey;
61 - (BOOL)canBecomeKeyWindow
63 if (b_isset_canBecomeKeyWindow)
64 return b_canBecomeKeyWindow;
66 return [super canBecomeKeyWindow];
69 - (void)setCanBecomeMainWindow: (BOOL)canBecomeMain
71 b_isset_canBecomeMainWindow = YES;
72 b_canBecomeMainWindow = canBecomeMain;
75 - (BOOL)canBecomeMainWindow
77 if (b_isset_canBecomeMainWindow)
78 return b_canBecomeMainWindow;
80 return [super canBecomeMainWindow];
83 - (void)closeAndAnimate: (BOOL)animate
92 // TODO this callback stuff does not work and is not needed
93 invoc = [[[NSInvocation alloc] init] autorelease];
94 [invoc setSelector:@selector(close)];
95 [invoc setTarget: self];
97 if (![self isVisible] || [self alphaValue] == 0.0) {
102 [self orderOut: self animate: YES callback: invoc];
105 - (void)orderOut: (id)sender animate: (BOOL)animate
107 NSInvocation *invoc = [[[NSInvocation alloc] init] autorelease];
108 [invoc setSelector:@selector(orderOut:)];
109 [invoc setTarget: self];
110 [invoc setArgument: sender atIndex: 2];
111 [self orderOut: sender animate: animate callback: invoc];
114 - (void)orderOut: (id)sender animate: (BOOL)animate callback:(NSInvocation *)callback
116 NSViewAnimation *anim;
117 NSViewAnimation *current_anim;
118 NSMutableDictionary *dict;
121 [self orderOut: sender];
125 dict = [[NSMutableDictionary alloc] initWithCapacity:2];
127 [dict setObject:self forKey:NSViewAnimationTargetKey];
129 [dict setObject:NSViewAnimationFadeOutEffect forKey:NSViewAnimationEffectKey];
130 anim = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObject:dict]];
133 [anim setAnimationBlockingMode:NSAnimationNonblocking];
134 [anim setDuration:0.9];
135 [anim setFrameRate:30];
136 [anim setUserInfo:callback];
137 [anim setDelegate:self];
139 @synchronized(self) {
140 current_anim = self->o_current_animation;
142 if ([[[current_anim viewAnimations] objectAtIndex:0] objectForKey: NSViewAnimationEffectKey] == NSViewAnimationFadeOutEffect && [current_anim isAnimating]) {
146 [current_anim stopAnimation];
147 [anim setCurrentProgress:1.0 - [current_anim currentProgress]];
148 [current_anim release];
151 [anim setCurrentProgress:1.0 - [self alphaValue]];
152 self->o_current_animation = anim;
153 [anim startAnimation];
158 - (void)orderFront: (id)sender animate: (BOOL)animate
160 NSViewAnimation *anim;
161 NSViewAnimation *current_anim;
162 NSMutableDictionary *dict;
165 [super orderFront: sender];
166 [self setAlphaValue: 1.0];
170 if (![self isVisible]) {
171 [self setAlphaValue: 0.0];
172 [super orderFront: sender];
174 else if ([self alphaValue] == 1.0) {
175 [super orderFront: self];
179 dict = [[NSMutableDictionary alloc] initWithCapacity:2];
181 [dict setObject:self forKey:NSViewAnimationTargetKey];
183 [dict setObject:NSViewAnimationFadeInEffect forKey:NSViewAnimationEffectKey];
184 anim = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObject:dict]];
187 [anim setAnimationBlockingMode:NSAnimationNonblocking];
188 [anim setDuration:0.5];
189 [anim setFrameRate:30];
190 [anim setDelegate:self];
192 @synchronized(self) {
193 current_anim = self->o_current_animation;
195 if ([[[current_anim viewAnimations] objectAtIndex:0] objectForKey: NSViewAnimationEffectKey] == NSViewAnimationFadeInEffect && [current_anim isAnimating]) {
199 [current_anim stopAnimation];
200 [anim setCurrentProgress:1.0 - [current_anim currentProgress]];
201 [current_anim release];
204 [anim setCurrentProgress:[self alphaValue]];
205 self->o_current_animation = anim;
206 [self orderFront: sender];
207 [anim startAnimation];
212 - (void)animationDidEnd:(NSAnimation*)anim
214 if ([self alphaValue] <= 0.0) {
215 NSInvocation * invoc;
216 [super orderOut: nil];
217 [self setAlphaValue: 1.0];
218 if ((invoc = [anim userInfo])) {
224 - (VLCVoutView *)videoView
226 if ([[self contentView] class] == [VLCVoutView class])
227 return (VLCVoutView *)[self contentView];
236 /*****************************************************************************
237 * VLCVideoWindowCommon
239 * Common code for main window, detached window and extra video window
240 *****************************************************************************/
242 @interface VLCVideoWindowCommon (Internal)
243 - (void)customZoom:(id)sender;
244 - (void)hasBecomeFullscreen;
245 - (void)leaveFullscreenAndFadeOut:(BOOL)fadeout;
246 - (void)hasEndedFullscreen;
249 @implementation VLCVideoWindowCommon
251 @synthesize videoView=o_video_view;
252 @synthesize controlsBar=o_controls_bar;
253 @synthesize inFullscreenTransition=b_in_fullscreen_transition;
258 - (id)initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)styleMask
259 backing:(NSBackingStoreType)backingType defer:(BOOL)flag
261 b_dark_interface = config_GetInt(VLCIntf, "macosx-interfacestyle");
263 if (b_dark_interface) {
264 styleMask = NSBorderlessWindowMask;
265 #ifdef MAC_OS_X_VERSION_10_7
266 if (!OSX_SNOW_LEOPARD)
267 styleMask |= NSResizableWindowMask;
271 self = [super initWithContentRect:contentRect styleMask:styleMask
272 backing:backingType defer:flag];
274 /* we want to be moveable regardless of our style */
275 [self setMovableByWindowBackground: YES];
276 [self setCanBecomeKeyWindow:YES];
278 o_temp_view = [[NSView alloc] init];
279 [o_temp_view setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable];
286 [o_temp_view release];
292 BOOL b_nativeFullscreenMode = NO;
293 #ifdef MAC_OS_X_VERSION_10_7
294 if (!OSX_SNOW_LEOPARD)
295 b_nativeFullscreenMode = var_InheritBool(VLCIntf, "macosx-nativefullscreenmode");
298 if (b_nativeFullscreenMode) {
299 [self setCollectionBehavior: NSWindowCollectionBehaviorFullScreenPrimary];
301 [o_titlebar_view setFullscreenButtonHidden: YES];
304 [super awakeFromNib];
307 - (void)setTitle:(NSString *)title
309 if (!title || [title length] < 1)
312 if (b_dark_interface && o_titlebar_view)
313 [o_titlebar_view setWindowTitle: title];
315 [super setTitle: title];
319 #pragma mark zoom / minimize / close
321 - (BOOL)validateMenuItem:(NSMenuItem *)menuItem
323 SEL s_menuAction = [menuItem action];
325 if ((s_menuAction == @selector(performClose:)) || (s_menuAction == @selector(performMiniaturize:)) || (s_menuAction == @selector(performZoom:)))
328 return [super validateMenuItem:menuItem];
331 - (BOOL)windowShouldClose:(id)sender
336 - (void)performClose:(id)sender
338 if (!([self styleMask] & NSTitledWindowMask)) {
339 [[NSNotificationCenter defaultCenter] postNotificationName:NSWindowWillCloseNotification object:self];
343 [super performClose: sender];
346 - (void)performMiniaturize:(id)sender
348 if (!([self styleMask] & NSTitledWindowMask))
349 [self miniaturize: sender];
351 [super performMiniaturize: sender];
354 - (void)performZoom:(id)sender
356 if (!([self styleMask] & NSTitledWindowMask))
357 [self customZoom: sender];
359 [super performZoom: sender];
362 - (void)zoom:(id)sender
364 if (!([self styleMask] & NSTitledWindowMask))
365 [self customZoom: sender];
367 [super zoom: sender];
371 * Given a proposed frame rectangle, return a modified version
372 * which will fit inside the screen.
374 * This method is based upon NSWindow.m, part of the GNUstep GUI Library, licensed under LGPLv2+.
375 * Authors: Scott Christley <scottc@net-community.com>, Venkat Ajjanagadde <venkat@ocbi.com>,
376 * Felipe A. Rodriguez <far@ix.netcom.com>, Richard Frith-Macdonald <richard@brainstorm.co.uk>
377 * Copyright (C) 1996 Free Software Foundation, Inc.
379 - (NSRect) customConstrainFrameRect: (NSRect)frameRect toScreen: (NSScreen*)screen
381 NSRect screenRect = [screen visibleFrame];
384 /* Move top edge of the window inside the screen */
385 difference = NSMaxY (frameRect) - NSMaxY (screenRect);
386 if (difference > 0) {
387 frameRect.origin.y -= difference;
390 /* If the window is resizable, resize it (if needed) so that the
391 bottom edge is on the screen or can be on the screen when the user moves
393 difference = NSMaxY (screenRect) - NSMaxY (frameRect);
394 if (_styleMask & NSResizableWindowMask) {
397 difference2 = screenRect.origin.y - frameRect.origin.y;
398 difference2 -= difference;
399 // Take in account the space between the top of window and the top of the
400 // screen which can be used to move the bottom of the window on the screen
401 if (difference2 > 0) {
402 frameRect.size.height -= difference2;
403 frameRect.origin.y += difference2;
406 /* Ensure that resizing doesn't makewindow smaller than minimum */
407 difference2 = [self minSize].height - frameRect.size.height;
408 if (difference2 > 0) {
409 frameRect.size.height += difference2;
410 frameRect.origin.y -= difference2;
420 Zooms the receiver. This method calls the delegate method
421 windowShouldZoom:toFrame: to determine if the window should
422 be allowed to zoom to full screen.
424 * This method is based upon NSWindow.m, part of the GNUstep GUI Library, licensed under LGPLv2+.
425 * Authors: Scott Christley <scottc@net-community.com>, Venkat Ajjanagadde <venkat@ocbi.com>,
426 * Felipe A. Rodriguez <far@ix.netcom.com>, Richard Frith-Macdonald <richard@brainstorm.co.uk>
427 * Copyright (C) 1996 Free Software Foundation, Inc.
429 - (void) customZoom: (id)sender
431 NSRect maxRect = [[self screen] visibleFrame];
432 NSRect currentFrame = [self frame];
434 if ([[self delegate] respondsToSelector: @selector(windowWillUseStandardFrame:defaultFrame:)]) {
435 maxRect = [[self delegate] windowWillUseStandardFrame: self defaultFrame: maxRect];
438 maxRect = [self customConstrainFrameRect: maxRect toScreen: [self screen]];
440 // Compare the new frame with the current one
441 if ((abs(NSMaxX(maxRect) - NSMaxX(currentFrame)) < DIST)
442 && (abs(NSMaxY(maxRect) - NSMaxY(currentFrame)) < DIST)
443 && (abs(NSMinX(maxRect) - NSMinX(currentFrame)) < DIST)
444 && (abs(NSMinY(maxRect) - NSMinY(currentFrame)) < DIST)) {
445 // Already in zoomed mode, reset user frame, if stored
446 if ([self frameAutosaveName] != nil) {
447 [self setFrame: previousSavedFrame display: YES animate: YES];
448 [self saveFrameUsingName: [self frameAutosaveName]];
453 if ([self frameAutosaveName] != nil) {
454 [self saveFrameUsingName: [self frameAutosaveName]];
455 previousSavedFrame = [self frame];
458 [self setFrame: maxRect display: YES animate: YES];
462 #pragma mark Video window resizing logic
464 - (void)setWindowLevel:(NSInteger)i_state
466 if (var_InheritBool(VLCIntf, "video-wallpaper") || [self level] < NSNormalWindowLevel)
469 if (!b_fullscreen && !b_in_fullscreen_transition)
470 [self setLevel: i_state];
472 // save it for restore if window is currently minimized or in fullscreen
473 i_originalLevel = i_state;
476 - (NSRect)getWindowRectForProposedVideoViewSize:(NSSize)size
478 NSSize windowMinSize = [self minSize];
479 NSRect screenFrame = [[self screen] visibleFrame];
481 NSPoint topleftbase = NSMakePoint(0, [self frame].size.height);
482 NSPoint topleftscreen = [self convertBaseToScreen: topleftbase];
484 unsigned int i_width = size.width;
485 unsigned int i_height = size.height;
486 if (i_width < windowMinSize.width)
487 i_width = windowMinSize.width;
488 if (i_height < f_min_video_height)
489 i_height = f_min_video_height;
491 /* Calculate the window's new size */
493 new_frame.size.width = [self frame].size.width - [o_video_view frame].size.width + i_width;
494 new_frame.size.height = [self frame].size.height - [o_video_view frame].size.height + i_height;
495 new_frame.origin.x = topleftscreen.x;
496 new_frame.origin.y = topleftscreen.y - new_frame.size.height;
498 /* make sure the window doesn't exceed the screen size the window is on */
499 if (new_frame.size.width > screenFrame.size.width) {
500 new_frame.size.width = screenFrame.size.width;
501 new_frame.origin.x = screenFrame.origin.x;
503 if (new_frame.size.height > screenFrame.size.height) {
504 new_frame.size.height = screenFrame.size.height;
505 new_frame.origin.y = screenFrame.origin.y;
507 if (new_frame.origin.y < screenFrame.origin.y)
508 new_frame.origin.y = screenFrame.origin.y;
510 CGFloat right_screen_point = screenFrame.origin.x + screenFrame.size.width;
511 CGFloat right_window_point = new_frame.origin.x + new_frame.size.width;
512 if (right_window_point > right_screen_point)
513 new_frame.origin.x -= (right_window_point - right_screen_point);
520 // VOUT_WINDOW_SET_SIZE is triggered when exiting fullscreen. This event is ignored here
521 // to avoid interference with the animation.
522 if ([self fullscreen] || b_in_fullscreen_transition)
525 NSRect window_rect = [self getWindowRectForProposedVideoViewSize:nativeVideoSize];
526 [[self animator] setFrame:window_rect display:YES];
529 - (void)setNativeVideoSize:(NSSize)size
531 nativeVideoSize = size;
533 if (var_InheritBool(VLCIntf, "macosx-video-autoresize") && !var_InheritBool(VLCIntf, "video-wallpaper"))
537 - (NSSize)windowWillResize:(NSWindow *)window toSize:(NSSize)proposedFrameSize
539 if (![[VLCMain sharedInstance] activeVideoPlayback] || nativeVideoSize.width == 0. || nativeVideoSize.height == 0. || window != self)
540 return proposedFrameSize;
542 // needed when entering lion fullscreen mode
543 if (b_in_fullscreen_transition || [self fullscreen])
544 return proposedFrameSize;
546 if ([[VLCCoreInteraction sharedInstance] aspectRatioIsLocked]) {
547 NSRect videoWindowFrame = [self frame];
548 NSRect viewRect = [o_video_view convertRect:[o_video_view bounds] toView: nil];
549 NSRect contentRect = [self contentRectForFrameRect:videoWindowFrame];
550 float marginy = viewRect.origin.y + videoWindowFrame.size.height - contentRect.size.height;
551 float marginx = contentRect.size.width - viewRect.size.width;
552 if (o_titlebar_view && b_dark_interface)
553 marginy += [o_titlebar_view frame].size.height;
555 proposedFrameSize.height = (proposedFrameSize.width - marginx) * nativeVideoSize.height / nativeVideoSize.width + marginy;
558 return proposedFrameSize;
561 - (void)windowWillMiniaturize:(NSNotification *)notification
563 // Set level to normal as a workaround for Mavericks bug causing window
564 // to vanish from screen, see radar://15473716
565 i_originalLevel = [self level];
566 [self setLevel: NSNormalWindowLevel];
569 - (void)windowDidDeminiaturize:(NSNotification *)notification
571 [self setLevel: i_originalLevel];
575 #pragma mark Mouse cursor handling
577 // NSTimer selectors require this function signature as per Apple's docs
578 - (void)hideMouseCursor:(NSTimer *)timer
580 [NSCursor setHiddenUntilMouseMoves: YES];
583 - (void)recreateHideMouseTimer
585 if (t_hide_mouse_timer != nil) {
586 [t_hide_mouse_timer invalidate];
587 [t_hide_mouse_timer release];
590 t_hide_mouse_timer = [NSTimer scheduledTimerWithTimeInterval:2
592 selector:@selector(hideMouseCursor:)
595 [t_hide_mouse_timer retain];
598 // Called automatically if window's acceptsMouseMovedEvents property is true
599 - (void)mouseMoved:(NSEvent *)theEvent
602 [self recreateHideMouseTimer];
604 [super mouseMoved: theEvent];
608 #pragma mark Lion native fullscreen handling
610 - (void)becomeKeyWindow
612 [super becomeKeyWindow];
614 // change fspanel state for the case when multiple windows are in fullscreen
615 if ([self hasActiveVideo] && [self fullscreen])
616 [[[VLCMainWindow sharedInstance] fsPanel] setActive:nil];
618 [[[VLCMainWindow sharedInstance] fsPanel] setNonActive:nil];
621 - (void)resignKeyWindow
623 [super resignKeyWindow];
625 [[[VLCMainWindow sharedInstance] fsPanel] setNonActive:nil];
628 - (void)windowWillEnterFullScreen:(NSNotification *)notification
630 // workaround, see #6668
631 [NSApp setPresentationOptions:(NSApplicationPresentationFullScreen | NSApplicationPresentationAutoHideDock | NSApplicationPresentationAutoHideMenuBar)];
633 i_originalLevel = [self level];
634 // b_fullscreen and b_in_fullscreen_transition must not be true yet
635 [[[VLCMain sharedInstance] voutController] updateWindowLevelForHelperWindows: NSNormalWindowLevel];
636 [self setLevel:NSNormalWindowLevel];
638 b_in_fullscreen_transition = YES;
640 var_SetBool(pl_Get(VLCIntf), "fullscreen", true);
642 if ([self hasActiveVideo]) {
643 vout_thread_t *p_vout = getVoutForActiveWindow();
645 var_SetBool(p_vout, "fullscreen", true);
646 vlc_object_release(p_vout);
650 if ([self hasActiveVideo])
651 [[VLCMainWindow sharedInstance] recreateHideMouseTimer];
653 if (b_dark_interface) {
654 [o_titlebar_view removeFromSuperviewWithoutNeedingDisplay];
657 CGFloat f_titleBarHeight = [o_titlebar_view frame].size.height;
658 winrect = [self frame];
660 winrect.size.height = winrect.size.height - f_titleBarHeight;
661 [self setFrame: winrect display:NO animate:NO];
664 [o_video_view setFrame: [[self contentView] frame]];
665 if (![o_video_view isHidden]) {
666 [[o_controls_bar bottomBarView] setHidden: YES];
670 [self setMovableByWindowBackground: NO];
673 - (void)windowDidEnterFullScreen:(NSNotification *)notification
675 // Indeed, we somehow can have an "inactive" fullscreen (but a visible window!).
676 // But this creates some problems when leaving fs over remote intfs, so activate app here.
677 [NSApp activateIgnoringOtherApps:YES];
679 [self setFullscreen: YES];
680 b_in_fullscreen_transition = NO;
682 if ([self hasActiveVideo]) {
683 [[[VLCMainWindow sharedInstance] fsPanel] setVoutWasUpdated: self];
684 if (![o_video_view isHidden])
685 [[[VLCMainWindow sharedInstance] fsPanel] setActive: nil];
688 NSArray *subviews = [[self videoView] subviews];
689 NSUInteger count = [subviews count];
691 for (NSUInteger x = 0; x < count; x++) {
692 if ([[subviews objectAtIndex:x] respondsToSelector:@selector(reshape)])
693 [[subviews objectAtIndex:x] reshape];
698 - (void)windowWillExitFullScreen:(NSNotification *)notification
700 b_in_fullscreen_transition = YES;
701 [self setFullscreen: NO];
703 var_SetBool(pl_Get(VLCIntf), "fullscreen", false);
705 if ([self hasActiveVideo]) {
706 vout_thread_t *p_vout = getVoutForActiveWindow();
708 var_SetBool(p_vout, "fullscreen", false);
709 vlc_object_release(p_vout);
713 [NSCursor setHiddenUntilMouseMoves: NO];
714 [[[VLCMainWindow sharedInstance] fsPanel] setNonActive: nil];
717 if (b_dark_interface) {
719 CGFloat f_titleBarHeight = [o_titlebar_view frame].size.height;
721 winrect = [o_video_view frame];
722 winrect.size.height -= f_titleBarHeight;
723 [o_video_view setFrame: winrect];
725 winrect = [self frame];
726 [o_titlebar_view setFrame: NSMakeRect(0, winrect.size.height - f_titleBarHeight,
727 winrect.size.width, f_titleBarHeight)];
728 [[self contentView] addSubview: o_titlebar_view];
730 winrect.size.height = winrect.size.height + f_titleBarHeight;
731 [self setFrame: winrect display:NO animate:NO];
734 NSRect videoViewFrame = [o_video_view frame];
735 videoViewFrame.origin.y += [o_controls_bar height];
736 videoViewFrame.size.height -= [o_controls_bar height];
737 [o_video_view setFrame: videoViewFrame];
739 if (![o_video_view isHidden]) {
740 [[o_controls_bar bottomBarView] setHidden: NO];
743 [self setMovableByWindowBackground: YES];
746 - (void)windowDidExitFullScreen:(NSNotification *)notification
748 b_in_fullscreen_transition = NO;
750 [[[VLCMain sharedInstance] voutController] updateWindowLevelForHelperWindows: i_originalLevel];
751 [self setLevel:i_originalLevel];
755 #pragma mark Fullscreen Logic
757 - (void)enterFullscreen
759 NSMutableDictionary *dict1, *dict2;
763 BOOL blackout_other_displays = var_InheritBool(VLCIntf, "macosx-black");
765 screen = [NSScreen screenWithDisplayID:(CGDirectDisplayID)var_InheritInteger(VLCIntf, "macosx-vdev")];
768 msg_Dbg(VLCIntf, "chosen screen isn't present, using current screen for fullscreen mode");
769 screen = [self screen];
772 msg_Dbg(VLCIntf, "Using deepest screen");
773 screen = [NSScreen deepestScreen];
776 screen_rect = [screen frame];
779 [o_controls_bar setFullscreenState:YES];
780 [[[VLCMainWindow sharedInstance] controlsBar] setFullscreenState:YES];
782 [[VLCMainWindow sharedInstance] recreateHideMouseTimer];
784 if (blackout_other_displays)
785 [screen blackoutOtherScreens];
787 /* Make sure we don't see the window flashes in float-on-top mode */
788 i_originalLevel = [self level];
789 // b_fullscreen must not be true yet
790 [[[VLCMain sharedInstance] voutController] updateWindowLevelForHelperWindows: NSNormalWindowLevel];
791 [self setLevel:NSNormalWindowLevel];
793 /* Only create the o_fullscreen_window if we are not in the middle of the zooming animation */
794 if (!o_fullscreen_window) {
795 /* We can't change the styleMask of an already created NSWindow, so we create another window, and do eye catching stuff */
797 rect = [[o_video_view superview] convertRect: [o_video_view frame] toView: nil]; /* Convert to Window base coord */
798 rect.origin.x += [self frame].origin.x;
799 rect.origin.y += [self frame].origin.y;
800 o_fullscreen_window = [[VLCWindow alloc] initWithContentRect:rect styleMask: NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:YES];
801 [o_fullscreen_window setBackgroundColor: [NSColor blackColor]];
802 [o_fullscreen_window setCanBecomeKeyWindow: YES];
803 [o_fullscreen_window setCanBecomeMainWindow: YES];
804 [o_fullscreen_window setHasActiveVideo: YES];
805 [o_fullscreen_window setFullscreen: YES];
807 if (![self isVisible] || [self alphaValue] == 0.0) {
808 /* We don't animate if we are not visible, instead we
809 * simply fade the display */
810 CGDisplayFadeReservationToken token;
812 if (blackout_other_displays) {
813 CGAcquireDisplayFadeReservation(kCGMaxDisplayReservationInterval, &token);
814 CGDisplayFade(token, 0.5, kCGDisplayBlendNormal, kCGDisplayBlendSolidColor, 0, 0, 0, YES);
817 [screen setFullscreenPresentationOptions];
819 [o_video_view retain];
820 [[o_video_view superview] replaceSubview:o_video_view with:o_temp_view];
821 [o_temp_view setFrame:[o_video_view frame]];
822 [o_fullscreen_window setContentView:o_video_view];
823 [o_video_view release];
825 [o_fullscreen_window makeKeyAndOrderFront:self];
826 [o_fullscreen_window orderFront:self animate:YES];
828 [o_fullscreen_window setFrame:screen_rect display:YES animate:YES];
829 [o_fullscreen_window setLevel:NSNormalWindowLevel];
831 if (blackout_other_displays) {
832 CGDisplayFade(token, 0.3, kCGDisplayBlendSolidColor, kCGDisplayBlendNormal, 0, 0, 0, NO);
833 CGReleaseDisplayFadeReservation(token);
836 /* Will release the lock */
837 [self hasBecomeFullscreen];
842 /* Make sure video view gets visible in case the playlist was visible before */
843 b_video_view_was_hidden = [o_video_view isHidden];
844 [o_video_view setHidden: NO];
846 /* Make sure we don't see the o_video_view disappearing of the screen during this operation */
847 NSDisableScreenUpdates();
848 [o_video_view retain];
849 [[o_video_view superview] replaceSubview:o_video_view with:o_temp_view];
850 [o_temp_view setFrame:[o_video_view frame]];
851 [o_fullscreen_window setContentView:o_video_view];
852 [o_video_view release];
853 [o_fullscreen_window makeKeyAndOrderFront:self];
854 NSEnableScreenUpdates();
857 /* We are in fullscreen (and no animation is running) */
858 if ([self fullscreen]) {
859 /* Make sure we are hidden */
860 [self orderOut: self];
865 if (o_fullscreen_anim1) {
866 [o_fullscreen_anim1 stopAnimation];
867 [o_fullscreen_anim1 release];
869 if (o_fullscreen_anim2) {
870 [o_fullscreen_anim2 stopAnimation];
871 [o_fullscreen_anim2 release];
874 [screen setFullscreenPresentationOptions];
876 dict1 = [[NSMutableDictionary alloc] initWithCapacity:2];
877 dict2 = [[NSMutableDictionary alloc] initWithCapacity:3];
879 [dict1 setObject:self forKey:NSViewAnimationTargetKey];
880 [dict1 setObject:NSViewAnimationFadeOutEffect forKey:NSViewAnimationEffectKey];
882 [dict2 setObject:o_fullscreen_window forKey:NSViewAnimationTargetKey];
883 [dict2 setObject:[NSValue valueWithRect:[o_fullscreen_window frame]] forKey:NSViewAnimationStartFrameKey];
884 [dict2 setObject:[NSValue valueWithRect:screen_rect] forKey:NSViewAnimationEndFrameKey];
886 /* Strategy with NSAnimation allocation:
887 - Keep at most 2 animation at a time
888 - leaveFullscreen/enterFullscreen are the only responsible for releasing and alloc-ing
890 o_fullscreen_anim1 = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObject:dict1]];
891 o_fullscreen_anim2 = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObject:dict2]];
896 [o_fullscreen_anim1 setAnimationBlockingMode: NSAnimationNonblocking];
897 [o_fullscreen_anim1 setDuration: 0.3];
898 [o_fullscreen_anim1 setFrameRate: 30];
899 [o_fullscreen_anim2 setAnimationBlockingMode: NSAnimationNonblocking];
900 [o_fullscreen_anim2 setDuration: 0.2];
901 [o_fullscreen_anim2 setFrameRate: 30];
903 [o_fullscreen_anim2 setDelegate: self];
904 [o_fullscreen_anim2 startWhenAnimation: o_fullscreen_anim1 reachesProgress: 1.0];
906 [o_fullscreen_anim1 startAnimation];
907 /* fullscreenAnimation will be unlocked when animation ends */
909 b_in_fullscreen_transition = YES;
912 - (void)hasBecomeFullscreen
914 if ([[o_video_view subviews] count] > 0)
915 [o_fullscreen_window makeFirstResponder: [[o_video_view subviews] objectAtIndex:0]];
917 [o_fullscreen_window makeKeyWindow];
918 [o_fullscreen_window setAcceptsMouseMovedEvents: YES];
920 /* tell the fspanel to move itself to front next time it's triggered */
921 [[[VLCMainWindow sharedInstance] fsPanel] setVoutWasUpdated: o_fullscreen_window];
922 [[[VLCMainWindow sharedInstance] fsPanel] setActive: nil];
924 if ([self isVisible])
925 [self orderOut: self];
927 b_in_fullscreen_transition = NO;
928 [self setFullscreen:YES];
931 - (void)leaveFullscreen
933 [self leaveFullscreenAndFadeOut: NO];
936 - (void)leaveFullscreenAndFadeOut: (BOOL)fadeout
938 NSMutableDictionary *dict1, *dict2;
940 BOOL blackout_other_displays = var_InheritBool(VLCIntf, "macosx-black");
943 [o_controls_bar setFullscreenState:NO];
944 [[[VLCMainWindow sharedInstance] controlsBar] setFullscreenState:NO];
946 /* We always try to do so */
947 [NSScreen unblackoutScreens];
949 [[o_video_view window] makeKeyAndOrderFront: nil];
951 /* Don't do anything if o_fullscreen_window is already closed */
952 if (!o_fullscreen_window) {
957 /* We don't animate if we are not visible, instead we
958 * simply fade the display */
959 CGDisplayFadeReservationToken token;
961 if (blackout_other_displays) {
962 CGAcquireDisplayFadeReservation(kCGMaxDisplayReservationInterval, &token);
963 CGDisplayFade(token, 0.3, kCGDisplayBlendNormal, kCGDisplayBlendSolidColor, 0, 0, 0, YES);
966 [[[VLCMainWindow sharedInstance] fsPanel] setNonActive: nil];
967 [[o_fullscreen_window screen] setNonFullscreenPresentationOptions];
969 /* Will release the lock */
970 [self hasEndedFullscreen];
972 /* Our window is hidden, and might be faded. We need to workaround that, so note it
974 b_window_is_invisible = YES;
976 if (blackout_other_displays) {
977 CGDisplayFade(token, 0.5, kCGDisplayBlendSolidColor, kCGDisplayBlendNormal, 0, 0, 0, NO);
978 CGReleaseDisplayFadeReservation(token);
984 b_in_fullscreen_transition = YES;
986 [self setAlphaValue: 0.0];
987 [self orderFront: self];
988 [[o_video_view window] orderFront: self];
990 [[[VLCMainWindow sharedInstance] fsPanel] setNonActive: nil];
991 [[o_fullscreen_window screen] setNonFullscreenPresentationOptions];
993 if (o_fullscreen_anim1) {
994 [o_fullscreen_anim1 stopAnimation];
995 [o_fullscreen_anim1 release];
997 if (o_fullscreen_anim2) {
998 [o_fullscreen_anim2 stopAnimation];
999 [o_fullscreen_anim2 release];
1002 frame = [[o_temp_view superview] convertRect: [o_temp_view frame] toView: nil]; /* Convert to Window base coord */
1003 frame.origin.x += [self frame].origin.x;
1004 frame.origin.y += [self frame].origin.y;
1006 dict2 = [[NSMutableDictionary alloc] initWithCapacity:2];
1007 [dict2 setObject:self forKey:NSViewAnimationTargetKey];
1008 [dict2 setObject:NSViewAnimationFadeInEffect forKey:NSViewAnimationEffectKey];
1010 o_fullscreen_anim2 = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObject:dict2]];
1013 [o_fullscreen_anim2 setAnimationBlockingMode: NSAnimationNonblocking];
1014 [o_fullscreen_anim2 setDuration: 0.3];
1015 [o_fullscreen_anim2 setFrameRate: 30];
1017 [o_fullscreen_anim2 setDelegate: self];
1019 dict1 = [[NSMutableDictionary alloc] initWithCapacity:3];
1021 [dict1 setObject:o_fullscreen_window forKey:NSViewAnimationTargetKey];
1022 [dict1 setObject:[NSValue valueWithRect:[o_fullscreen_window frame]] forKey:NSViewAnimationStartFrameKey];
1023 [dict1 setObject:[NSValue valueWithRect:frame] forKey:NSViewAnimationEndFrameKey];
1025 o_fullscreen_anim1 = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObject:dict1]];
1028 [o_fullscreen_anim1 setAnimationBlockingMode: NSAnimationNonblocking];
1029 [o_fullscreen_anim1 setDuration: 0.2];
1030 [o_fullscreen_anim1 setFrameRate: 30];
1031 [o_fullscreen_anim2 startWhenAnimation: o_fullscreen_anim1 reachesProgress: 1.0];
1033 /* Make sure o_fullscreen_window is the frontmost window */
1034 [o_fullscreen_window orderFront: self];
1036 [o_fullscreen_anim1 startAnimation];
1037 /* fullscreenAnimation will be unlocked when animation ends */
1040 - (void)hasEndedFullscreen
1042 [self setFullscreen:NO];
1043 b_in_fullscreen_transition = NO;
1045 /* This function is private and should be only triggered at the end of the fullscreen change animation */
1046 /* Make sure we don't see the o_video_view disappearing of the screen during this operation */
1047 NSDisableScreenUpdates();
1048 [o_video_view retain];
1049 [o_video_view removeFromSuperviewWithoutNeedingDisplay];
1050 [[o_temp_view superview] replaceSubview:o_temp_view with:o_video_view];
1051 [o_video_view release];
1052 [o_video_view setFrame:[o_temp_view frame]];
1053 if ([[o_video_view subviews] count] > 0)
1054 [self makeFirstResponder: [[o_video_view subviews] objectAtIndex:0]];
1056 [o_video_view setHidden: b_video_view_was_hidden];
1058 [super makeKeyAndOrderFront:self]; /* our version (in main window) contains a workaround */
1060 [o_fullscreen_window orderOut: self];
1061 NSEnableScreenUpdates();
1063 [o_fullscreen_window release];
1064 o_fullscreen_window = nil;
1066 [[[VLCMain sharedInstance] voutController] updateWindowLevelForHelperWindows: i_originalLevel];
1067 [self setLevel:i_originalLevel];
1069 [self setAlphaValue: config_GetFloat(VLCIntf, "macosx-opaqueness")];
1071 // if we quit fullscreen because there is no video anymore, make sure non-embedded window is not visible
1072 if (![[VLCMain sharedInstance] activeVideoPlayback] && [self class] != [VLCMainWindow class])
1073 [self orderOut: self];
1076 - (void)animationDidEnd:(NSAnimation*)animation
1078 NSArray *viewAnimations;
1079 if (o_makekey_anim == animation) {
1080 [o_makekey_anim release];
1083 if ([animation currentValue] < 1.0)
1086 /* Fullscreen ended or started (we are a delegate only for leaveFullscreen's/enterFullscren's anim2) */
1087 viewAnimations = [o_fullscreen_anim2 viewAnimations];
1088 if ([viewAnimations count] >=1 &&
1089 [[[viewAnimations objectAtIndex:0] objectForKey: NSViewAnimationEffectKey] isEqualToString:NSViewAnimationFadeInEffect]) {
1090 /* Fullscreen ended */
1091 [self hasEndedFullscreen];
1093 /* Fullscreen started */
1094 [self hasBecomeFullscreen];
1097 - (void)orderOut:(id)sender
1099 [super orderOut:sender];
1102 * TODO reimplement leaveFullscreenAndFadeOut:YES, or remove code
1103 * and the hack below
1105 if (![NSStringFromClass([self class]) isEqualToString:@"VLCMainWindow"]) {
1106 [self leaveFullscreenAndFadeOut:YES];
1111 - (void)makeKeyAndOrderFront: (id)sender
1114 * when we exit fullscreen and fade out, we may endup in
1115 * having a window that is faded. We can't have it fade in unless we
1118 if (!b_window_is_invisible) {
1119 /* Make sure we don't do it too much */
1120 [super makeKeyAndOrderFront: sender];
1124 [super setAlphaValue:0.0f];
1125 [super makeKeyAndOrderFront: sender];
1127 NSMutableDictionary * dict = [[NSMutableDictionary alloc] initWithCapacity:2];
1128 [dict setObject:self forKey:NSViewAnimationTargetKey];
1129 [dict setObject:NSViewAnimationFadeInEffect forKey:NSViewAnimationEffectKey];
1131 o_makekey_anim = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObject:dict]];
1134 [o_makekey_anim setAnimationBlockingMode: NSAnimationNonblocking];
1135 [o_makekey_anim setDuration: 0.1];
1136 [o_makekey_anim setFrameRate: 30];
1137 [o_makekey_anim setDelegate: self];
1139 [o_makekey_anim startAnimation];
1140 b_window_is_invisible = NO;
1142 /* fullscreenAnimation will be unlocked when animation ends */
1147 #pragma mark Accessibility stuff
1149 - (NSArray *)accessibilityAttributeNames
1151 if (!b_dark_interface || !o_titlebar_view)
1152 return [super accessibilityAttributeNames];
1154 static NSMutableArray *attributes = nil;
1155 if (attributes == nil) {
1156 attributes = [[super accessibilityAttributeNames] mutableCopy];
1157 NSArray *appendAttributes = [NSArray arrayWithObjects:NSAccessibilitySubroleAttribute,
1158 NSAccessibilityCloseButtonAttribute,
1159 NSAccessibilityMinimizeButtonAttribute,
1160 NSAccessibilityZoomButtonAttribute, nil];
1162 for(NSString *attribute in appendAttributes) {
1163 if (![attributes containsObject:attribute])
1164 [attributes addObject:attribute];
1170 - (id)accessibilityAttributeValue: (NSString*)o_attribute_name
1172 if (b_dark_interface && o_titlebar_view) {
1173 VLCMainWindowTitleView *o_tbv = o_titlebar_view;
1175 if ([o_attribute_name isEqualTo: NSAccessibilitySubroleAttribute])
1176 return NSAccessibilityStandardWindowSubrole;
1178 if ([o_attribute_name isEqualTo: NSAccessibilityCloseButtonAttribute])
1179 return [[o_tbv closeButton] cell];
1181 if ([o_attribute_name isEqualTo: NSAccessibilityMinimizeButtonAttribute])
1182 return [[o_tbv minimizeButton] cell];
1184 if ([o_attribute_name isEqualTo: NSAccessibilityZoomButtonAttribute])
1185 return [[o_tbv zoomButton] cell];
1188 return [super accessibilityAttributeValue: o_attribute_name];