macOS: Simplify Windows.m animations
authorMarvin Scholz <epirat07@gmail.com>
Thu, 16 Mar 2017 09:55:10 +0000 (10:55 +0100)
committerMarvin Scholz <epirat07@gmail.com>
Thu, 16 Mar 2017 10:13:59 +0000 (11:13 +0100)
modules/gui/macosx/Windows.h
modules/gui/macosx/Windows.m
modules/gui/macosx/misc.h
modules/gui/macosx/misc.m

index 30a8f7f..e44052f 100644 (file)
 @property (nonatomic, readwrite) BOOL hasActiveVideo;
 @property (nonatomic, readwrite) BOOL fullscreen;
 
-/* animate mode is only supported in >=10.4 */
-- (void)orderFront: (id)sender animate: (BOOL)animate;
-
-/* animate mode is only supported in >=10.4 */
-- (void)orderOut: (id)sender animate: (BOOL)animate;
-
-/* animate mode is only supported in >=10.4 */
-- (void)orderOut: (id)sender animate: (BOOL)animate callback:(NSInvocation *)callback;
-
-/* animate mode is only supported in >=10.4 */
-- (void)closeAndAnimate: (BOOL)animate;
+- (void)closeAndAnimate:(BOOL)animate;
+- (void)orderFront:(id)sender animate:(BOOL)animate;
+- (void)orderOut:(id)sender animate:(BOOL)animate;
 
 - (VLCVoutView *)videoView;
 
index 98f9558..6b9f005 100644 (file)
     BOOL b_isset_canBecomeKeyWindow;
     BOOL b_canBecomeMainWindow;
     BOOL b_isset_canBecomeMainWindow;
-    NSViewAnimation *o_current_animation;
-
-    /*
-     * YES when all animations are over
-     * for fullscreen window: always YES
-     */
 }
 @end
 
     return [super canBecomeMainWindow];
 }
 
-- (void)closeAndAnimate: (BOOL)animate
+- (void)closeAndAnimate:(BOOL)animate
 {
-    NSInvocation *invoc;
-
+    // No animation, just close
     if (!animate) {
         [super close];
         return;
     }
 
-    // TODO this callback stuff does not work and is not needed
-    invoc = [[NSInvocation alloc] init];
-    [invoc setSelector:@selector(close)];
-    [invoc setTarget: self];
-
-    if (![self isVisible] || [self alphaValue] == 0.0) {
-        [super close];
-        return;
-    }
-
-    [self orderOut: self animate: YES callback: invoc];
+    // Animate window alpha value
+    [self setAlphaValue:1.0];
+    __unsafe_unretained typeof(self) this = self;
+    [NSAnimationContext runAnimationGroup:^(NSAnimationContext *context){
+        [[NSAnimationContext currentContext] setDuration:0.9];
+        [[this animator] setAlphaValue:0.0];
+    } completionHandler:^{
+        [this close];
+    }];
 }
 
-- (void)orderOut: (id)sender animate: (BOOL)animate
+- (void)orderOut:(id)sender animate:(BOOL)animate
 {
-    NSInvocation *invoc = [[NSInvocation alloc] init];
-    [invoc setSelector:@selector(orderOut:)];
-    [invoc setTarget: self];
-    [invoc setArgument:(__bridge void * __nonnull)sender atIndex:2];
-    [self orderOut: sender animate: animate callback: invoc];
-}
-
-- (void)orderOut: (id)sender animate: (BOOL)animate callback:(NSInvocation *)callback
-{
-    NSViewAnimation *anim;
-    NSViewAnimation *current_anim;
-    NSMutableDictionary *dict;
-
     if (!animate) {
-        [self orderOut: sender];
+        [super orderOut:sender];
         return;
     }
 
-    dict = [[NSMutableDictionary alloc] initWithCapacity:2];
-
-    [dict setObject:self forKey:NSViewAnimationTargetKey];
-
-    [dict setObject:NSViewAnimationFadeOutEffect forKey:NSViewAnimationEffectKey];
-    anim = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObject:dict]];
-
-    [anim setAnimationBlockingMode:NSAnimationNonblocking];
-    [anim setDuration:0.9];
-    [anim setFrameRate:30];
-    [anim setUserInfo:(__bridge void *)callback];
-    [anim setDelegate:self];
-
-    @synchronized(self) {
-        current_anim = self->o_current_animation;
-
-        if (!([[[current_anim viewAnimations] firstObject] objectForKey: NSViewAnimationEffectKey] == NSViewAnimationFadeOutEffect && [current_anim isAnimating])) {
-            if (current_anim) {
-                [current_anim stopAnimation];
-                [anim setCurrentProgress:1.0 - [current_anim currentProgress]];
-            } else
-                [anim setCurrentProgress:1.0 - [self alphaValue]];
-            self->o_current_animation = anim;
-            [anim startAnimation];
-        }
+    if ([self alphaValue] == 0.0) {
+        [super orderOut:self];
+        return;
     }
+    __unsafe_unretained typeof(self) this = self;
+    [NSAnimationContext runAnimationGroup:^(NSAnimationContext *context){
+        [[NSAnimationContext currentContext] setDuration:0.5];
+        [[this animator] setAlphaValue:0.0];
+    } completionHandler:^{
+        [this orderOut:self];
+    }];
 }
 
-- (void)orderFront: (id)sender animate: (BOOL)animate
+- (void)orderFront:(id)sender animate:(BOOL)animate
 {
-    NSViewAnimation *anim;
-    NSViewAnimation *current_anim;
-    NSMutableDictionary *dict;
-
     if (!animate) {
-        [super orderFront: sender];
-        [self setAlphaValue: 1.0];
+        [super orderFront:sender];
+        [self setAlphaValue:1.0];
         return;
     }
 
     if (![self isVisible]) {
-        [self setAlphaValue: 0.0];
-        [super orderFront: sender];
-    }
-    else if ([self alphaValue] == 1.0) {
-        [super orderFront: self];
+        [self setAlphaValue:0.0];
+        [super orderFront:sender];
+    } else if ([self alphaValue] == 1.0) {
+        [super orderFront:self];
         return;
     }
 
-    dict = [[NSMutableDictionary alloc] initWithCapacity:2];
-
-    [dict setObject:self forKey:NSViewAnimationTargetKey];
-
-    [dict setObject:NSViewAnimationFadeInEffect forKey:NSViewAnimationEffectKey];
-    anim = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObject:dict]];
-
-    [anim setAnimationBlockingMode:NSAnimationNonblocking];
-    [anim setDuration:0.5];
-    [anim setFrameRate:30];
-    [anim setDelegate:self];
-
-    @synchronized(self) {
-        current_anim = self->o_current_animation;
-
-        if (!([[[current_anim viewAnimations] firstObject] objectForKey: NSViewAnimationEffectKey] == NSViewAnimationFadeInEffect && [current_anim isAnimating])) {
-            if (current_anim) {
-                [current_anim stopAnimation];
-                [anim setCurrentProgress:1.0 - [current_anim currentProgress]];
-            }
-            else
-                [anim setCurrentProgress:[self alphaValue]];
-            self->o_current_animation = anim;
-            [self orderFront: sender];
-            [anim startAnimation];
-        }
-    }
-}
-
-- (void)animationDidEnd:(NSAnimation*)anim
-{
-    if ([self alphaValue] <= 0.0) {
-        NSInvocation * invoc;
-        [super orderOut: nil];
-        [self setAlphaValue: 1.0];
-        if ((invoc = [anim userInfo])) {
-            [invoc invoke];
-        }
-    }
+    [NSAnimationContext beginGrouping];
+    [[NSAnimationContext currentContext] setDuration:0.5];
+    [[self animator] setAlphaValue:1.0];
+    [NSAnimationContext endGrouping];
 }
 
 - (VLCVoutView *)videoView
index 848c8eb..b0561b6 100644 (file)
 
 #import <Cocoa/Cocoa.h>
 
-/*****************************************************************************
- * NSAnimation (VLCAddition)
- *****************************************************************************/
-
-@interface NSAnimation (VLCAdditions)
-@property (readwrite) void * userInfo;
-
-@end
-
 /*****************************************************************************
  * NSScreen (VLCAdditions)
  *
index 08d42f9..9e5c2a6 100644 (file)
 
 NSString *const VLCOpenTextFieldWasClicked = @"VLCOpenTextFieldWasClicked";
 
-/*****************************************************************************
- * NSAnimation (VLCAdditions)
- *
- *  Missing extension to NSAnimation
- *****************************************************************************/
-
-@implementation NSAnimation (VLCAdditions)
-/* fake class attributes  */
-static NSMapTable *VLCAdditions_userInfo = NULL;
-
-+ (void)load
-{
-    /* init our fake object attribute */
-    VLCAdditions_userInfo = NSCreateMapTable(NSNonRetainedObjectMapKeyCallBacks, NSObjectMapValueCallBacks, 16);
-}
-
-#warning FIXME: Dealloc is replaced, this seems like a very bad idea.
-- (void)dealloc
-{
-    NSMapRemove(VLCAdditions_userInfo, (__bridge const void * __nullable)(self));
-}
-
-- (void)setUserInfo: (void *)userInfo
-{
-    NSMapInsert(VLCAdditions_userInfo, (__bridge const void * __nullable)(self), (void*)userInfo);
-}
-
-- (void *)userInfo
-{
-    return NSMapGet(VLCAdditions_userInfo, (__bridge const void * __nullable)(self));
-}
-@end
-
 /*****************************************************************************
  * NSScreen (VLCAdditions)
  *