Skip to content

Commit 4cb9c98

Browse files
committed
properly wait for animations to end before quit
1 parent 42caaf0 commit 4cb9c98

5 files changed

Lines changed: 40 additions & 27 deletions

File tree

osdep/macosx_events.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ void cocoa_uninit_media_keys(void);
3737
void cocoa_set_input_context(struct input_ctx *input_context);
3838
void cocoa_set_mpv_handle(struct mpv_handle *ctx);
3939

40-
void cocoa_cocoa_cb_dealloc(void);
40+
void cocoa_cb_dealloc(void);
41+
void cocoa_cb_animation_wait(void);
4142

4243
#endif

osdep/macosx_events.m

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@
3939

4040
#include "config.h"
4141

42+
#if HAVE_MACOS_COCOA_CB
43+
#include "osdep/macOS_swift.h"
44+
#endif
45+
4246
@interface EventsResponder ()
4347
{
4448
struct input_ctx *_inputContext;
@@ -227,13 +231,19 @@ void cocoa_set_mpv_handle(struct mpv_handle *ctx)
227231
printf("cocoa_set_mpv_handle end\n");
228232
}
229233

230-
void cocoa_cocoa_cb_dealloc() {
234+
void cocoa_cb_dealloc() {
231235
if ([(Application *)NSApp cocoaCB]) {
232236
[[(Application *)NSApp cocoaCB] dealloc];
233237
[(Application *)NSApp setCocoaCB:nil];
234238
}
235239
}
236240

241+
void cocoa_cb_animation_wait() {
242+
if ([(Application *)NSApp cocoaCB]) {
243+
[[(Application *)NSApp cocoaCB] waitForAnimation];
244+
}
245+
}
246+
237247
@implementation EventsResponder
238248

239249
+ (EventsResponder *)sharedInstance

player/main.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,10 @@ void mp_destroy(struct MPContext *mpctx)
173173
mp_uninit_ipc(mpctx->ipc_ctx);
174174
mpctx->ipc_ctx = NULL;
175175

176+
#if HAVE_COCOA
177+
cocoa_cb_animation_wait();
178+
#endif
179+
176180
uninit_audio_out(mpctx);
177181
uninit_video_out(mpctx);
178182

@@ -468,7 +472,7 @@ int mp_initialize(struct MPContext *mpctx, char **options)
468472
if (opts->vo->video_driver_list == NULL ||
469473
strcmp(opts->vo->video_driver_list->name, "opengl-cb") != 0)
470474
{
471-
cocoa_cocoa_cb_dealloc();
475+
cocoa_cb_dealloc();
472476
}
473477
mpv_handle *ctx = mp_new_client(mpctx->clients, "osx");
474478
cocoa_set_mpv_handle(ctx);

video/out/cocoa-cb/window.swift

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class VideoWindow: NSWindow, NSWindowDelegate {
1616
var isInFullscreen: Bool = false
1717
var isAnimating: Bool = false
1818
var isMoving: Bool = false
19-
//let animationLock = NSCondition()
19+
let animationLock = NSCondition()
2020

2121
override var canBecomeKey: Bool { return true }
2222
override var canBecomeMain: Bool { return true }
@@ -143,8 +143,7 @@ class VideoWindow: NSWindow, NSWindowDelegate {
143143
self.cocoaCB.layer.isAsynchronous = false
144144
}*/
145145

146-
isAnimating = false
147-
//unlockAnimation()
146+
unlockAnimation()
148147
}
149148

150149
func windowDidExitFullScreen(_ notification: Notification) {
@@ -156,49 +155,42 @@ class VideoWindow: NSWindow, NSWindowDelegate {
156155
}*/
157156

158157
contentAspectRatio = unfsContentFrame!.size
159-
isAnimating = false
160-
//unlockAnimation()
158+
unlockAnimation()
161159
}
162160

163161
func windowDidFailToEnterFullScreen(_ window: NSWindow) {
164162
let newFrame = calculateWindowPosition(for: targetScreen!, withoutBounds: targetScreen == screen)
165163
setFrame(newFrame, display: true)
166164
contentAspectRatio = unfsContentFrame!.size
167-
isAnimating = false
168-
//unlockAnimation()
165+
unlockAnimation()
169166
}
170167

171168
func windowDidFailToExitFullScreen(_ window: NSWindow) {
172169
let newFrame = targetScreen!.frame
173170
setFrame(newFrame, display: true)
174-
isAnimating = false
175-
//unlockAnimation()
171+
unlockAnimation()
176172
}
177173

178-
/*func unlockAnimation() {
179-
/*Swift.print("unlockAnimation start")
180-
animationLock.lock()*/
174+
func unlockAnimation() {
175+
animationLock.lock()
181176
isAnimating = false
182-
/*animationLock.signal()
177+
animationLock.signal()
183178
animationLock.unlock()
184-
Swift.print("unlockAnimation end")*/
185-
}*/
179+
}
186180

187-
/*func waitForAnimation() {
188-
Swift.print("waitForAnimation start")
181+
func waitForAnimation() {
189182
animationLock.lock()
190183
while isAnimating {
191-
Swift.print("waitForAnimation wait")
192184
animationLock.wait()
193185
}
186+
close()
194187
animationLock.unlock()
195-
Swift.print("waitForAnimation end")
196-
}*/
188+
}
197189

198190
func setToFullScreen() {
199191
NSApp.presentationOptions = [.autoHideMenuBar, .autoHideDock]
200192
setFrame(targetScreen!.frame, display: true)
201-
isAnimating = false
193+
unlockAnimation()
202194
isInFullscreen = true
203195
cocoaCB.flagEvents(VO_EVENT_FULLSCREEN_STATE)
204196
}
@@ -208,7 +200,7 @@ class VideoWindow: NSWindow, NSWindowDelegate {
208200
NSApp.presentationOptions = []
209201
setFrame(newFrame, display: true)
210202
contentAspectRatio = unfsContentFrame!.size
211-
isAnimating = false
203+
unlockAnimation()
212204
isInFullscreen = false
213205
cocoaCB.flagEvents(VO_EVENT_FULLSCREEN_STATE)
214206
}

video/out/cocoa-cb_common.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ class CocoaCB: NSObject {
3737
view.wantsLayer = true
3838
}
3939

40+
func waitForAnimation() {
41+
window.waitForAnimation()
42+
}
43+
4044
func setMpvHandle(_ ctx: OpaquePointer) {
4145
mpv = MPVHelper(ctx)
4246
layer.setUpGLCB()
@@ -401,8 +405,10 @@ https://github.com/mpv-player/mpv/commit/c028d782c1eec46e2416da483881f1d0b27c2be
401405
func processEvent(_ event: UnsafePointer<mpv_event>) {
402406
switch event.pointee.event_id {
403407
case MPV_EVENT_SHUTDOWN:
404-
// TODO: need to stop shutdown for animations, not on main thread
405-
//window.waitForAnimation()
408+
if window.isInFullscreen {
409+
window.toggleFullScreen(nil)
410+
}
411+
406412
setCursorVisiblility(true)
407413
stopDisplaylink()
408414
uninitLightSensor()

0 commit comments

Comments
 (0)