Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions input/dnd.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,23 @@ static MP_THREAD_VOID mpv_event_loop_fn(void *arg)
MP_THREAD_RETURN();
}

void mp_dnd_load_file(mpv_handle *mpv, int num_files, char **files, enum mp_dnd_action action)
{
mpv_node *items = talloc_zero_array(NULL, mpv_node, num_files);
mpv_node_list list = {.values = items, .num = num_files};
mpv_node node = {.format = MPV_FORMAT_NODE_ARRAY, .u = {.list = &list}};
for (int n = 0; n < num_files; n++) {
items[n] = (mpv_node){.format = MPV_FORMAT_STRING,
.u = {.string = files[n]}};
}

char *actionstr = action == DND_REPLACE ? "replace" :
action == DND_APPEND ? "append" :
action == DND_INSERT_NEXT ? "insert-next" :
"none";
handle_dnd(mpv, &node, actionstr);
}

void mp_dnd_init(mpv_handle *mpv)
{
mp_thread mpv_event_loop;
Expand Down
3 changes: 3 additions & 0 deletions input/dnd.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,7 @@

#include "player/client.h"

#include "event.h"

void mp_dnd_load_file(mpv_handle *ctx, int num_files, char **files, enum mp_dnd_action action);
void mp_dnd_init(mpv_handle *mpv);
1 change: 1 addition & 0 deletions osdep/mac/app_bridge_objc.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "common/global.h"
#include "input/input.h"
#include "input/event.h"
#include "input/dnd.h"
#include "input/keycodes.h"
#include "video/out/win_state.h"

Expand Down
18 changes: 16 additions & 2 deletions osdep/mac/app_hub.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class AppHub: NSObject {
var isApplication: Bool { return NSApp is Application }
var isBundle: Bool { return ProcessInfo.processInfo.environment["MPVBUNDLE"] == "true" }
var openEvents: Int = 0
var openFiles: [String] = []

private override init() {
input = InputHelper()
Expand Down Expand Up @@ -113,9 +114,22 @@ class AppHub: NSObject {
return strL.localizedStandardCompare(strR) == .orderedAscending
}
log.verbose("\(openEvents > 0 ? "Appending" : "Opening") dropped files: \(files)")
input.open(files: files, append: openEvents > 0)
openEvents += 1
DispatchQueue.main.async {
self.open(files: files, append: self.openEvents > 0)
self.openEvents += 1
}
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { self.openEvents -= 1 }

}

func open(files: [String], append: Bool = false) {
openFiles += files
guard let mpv = mpv else { return }

input.open(files: files, append: append) { (filesPtr, action) in
mp_dnd_load_file(mpv, Int32(openFiles.count), &filesPtr, action)
}
openFiles = []
}

func getIcon() -> NSImage {
Expand Down
23 changes: 15 additions & 8 deletions osdep/mac/input_helper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -249,20 +249,27 @@ class InputHelper: NSObject {
return String(utf16CodeUnits: chars, count: length)
}

@objc func open(files: [String], append: Bool = false) {
@objc func handleDnd(files: [String]) {
lock.withLock {
guard let input = input else { return }

var action = DND_APPEND
if !append {
action = NSEvent.modifierFlags.contains(.shift) ? DND_APPEND : DND_REPLACE
open(files: files) { (filesPtr, action) in
mp_input_drop_files(input, Int32(files.count), &filesPtr, action)
}
}
}

let filesClean = files.map { $0.hasPrefix("file:///.file/id=") ? (URL(string: $0)?.path ?? $0) : $0 }
var filesPtr = filesClean.map { UnsafeMutablePointer<CChar>(strdup($0)) }
mp_input_drop_files(input, Int32(files.count), &filesPtr, action)
for charPtr in filesPtr { free(UnsafeMutablePointer(mutating: charPtr)) }
func open(files: [String], append: Bool = false,
completion: (inout [UnsafeMutablePointer<CChar>?], mp_dnd_action) -> Void) {
var action = DND_APPEND
if !append {
action = NSEvent.modifierFlags.contains(.shift) ? DND_APPEND : DND_REPLACE
}

let filesClean = files.map { $0.hasPrefix("file:///.file/id=") ? (URL(string: $0)?.path ?? $0) : $0 }
var filesPtr = filesClean.map { UnsafeMutablePointer<CChar>(strdup($0)) }
completion(&filesPtr, action)
for charPtr in filesPtr { free(UnsafeMutablePointer(mutating: charPtr)) }
}

private func useAltGr() -> Bool {
Expand Down
4 changes: 2 additions & 2 deletions osdep/mac/menu_bar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ class MenuBar: NSObject, EventSubscriber {

@objc func openFiles() {
guard let files = dialog.openFiles(path: currentDir) else { return }
appHub.input.open(files: files)
appHub.open(files: files)
}

@objc func openPlaylist() {
Expand All @@ -344,7 +344,7 @@ class MenuBar: NSObject, EventSubscriber {

@objc func openUrl() {
guard let file = dialog.openUrl() else { return }
appHub.input.open(files: [file])
appHub.open(files: [file])
}

@objc func command(_ menuItem: MenuItem) {
Expand Down
2 changes: 1 addition & 1 deletion video/out/mac/view.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class View: NSView, CALayerDelegate {
}
}
if files.isEmpty { return false }
input?.open(files: files)
input?.handleDnd(files: files)
return true
}

Expand Down
Loading