From c661435648fe36aa4b516fac9f5faf745630ebc5 Mon Sep 17 00:00:00 2001 From: der richter Date: Sat, 9 Dec 2023 17:23:45 +0100 Subject: [PATCH] mac: add support for drag-and-drop option --- DOCS/man/options.rst | 1 - osdep/macOS_swift_bridge.h | 1 + osdep/macos/mpv_helper.swift | 14 ++++++++++++++ video/out/mac/view.swift | 4 ++-- 4 files changed, 17 insertions(+), 3 deletions(-) diff --git a/DOCS/man/options.rst b/DOCS/man/options.rst index c2536e2334..08c26c54ab 100644 --- a/DOCS/man/options.rst +++ b/DOCS/man/options.rst @@ -3172,7 +3172,6 @@ Window (Windows only) Snap the player window to screen edges. ``--drag-and-drop=`` - (X11, Wayland and Windows only) Controls the default behavior of drag and drop on platforms that support this. ``auto`` will obey what the underlying os/platform gives mpv. Typically, holding shift during the drag and drop will append the item to the playlist. Otherwise, diff --git a/osdep/macOS_swift_bridge.h b/osdep/macOS_swift_bridge.h index 9407b6fc9b..72be85274a 100644 --- a/osdep/macOS_swift_bridge.h +++ b/osdep/macOS_swift_bridge.h @@ -27,6 +27,7 @@ #include "options/m_config.h" #include "player/core.h" #include "input/input.h" +#include "input/event.h" #include "video/out/win_state.h" #include "osdep/macosx_application_objc.h" diff --git a/osdep/macos/mpv_helper.swift b/osdep/macos/mpv_helper.swift index 532a5f723c..53ef4cb521 100644 --- a/osdep/macos/mpv_helper.swift +++ b/osdep/macos/mpv_helper.swift @@ -75,6 +75,20 @@ class MPVHelper { return m_config_cache_get_next_changed(optsCachePtr, &property) } + func open(files: [String]) { + if opts.drag_and_drop == -2 { return } + + var action = NSEvent.modifierFlags.contains(.shift) ? DND_APPEND : DND_REPLACE + if opts.drag_and_drop >= 0 { + action = mp_dnd_action(UInt32(opts.drag_and_drop)) + } + + let filesClean = files.map{ $0.hasPrefix("file:///.file/id=") ? (URL(string: $0)?.path ?? $0) : $0 } + var filesPtr = filesClean.map { UnsafeMutablePointer(strdup($0)) } + mp_event_drop_files(input, Int32(files.count), &filesPtr, action) + for charPtr in filesPtr { free(UnsafeMutablePointer(mutating: charPtr)) } + } + func setOption(fullscreen: Bool) { optsPtr.pointee.fullscreen = fullscreen _ = withUnsafeMutableBytes(of: &optsPtr.pointee.fullscreen) { (ptr: UnsafeMutableRawBufferPointer) in diff --git a/video/out/mac/view.swift b/video/out/mac/view.swift index 0ac90f5a54..a05127f0c3 100644 --- a/video/out/mac/view.swift +++ b/video/out/mac/view.swift @@ -81,7 +81,7 @@ class View: NSView { if types.contains(.fileURL) || types.contains(.URL) { if let urls = pb.readObjects(forClasses: [NSURL.self]) as? [URL] { let files = urls.map { $0.absoluteString } - EventsResponder.sharedInstance().handleFilesArray(files) + mpv?.open(files: files) return true } } else if types.contains(.string) { @@ -97,7 +97,7 @@ class View: NSView { filesArray.append(path) } } - EventsResponder.sharedInstance().handleFilesArray(filesArray) + mpv?.open(files: filesArray) return true } return false