mirror of
https://github.com/mpv-player/mpv.git
synced 2025-12-23 19:30:20 +00:00
mac/dialog: add file extension filtering for open dialogs
use the configured file extensions from options to filter.
This commit is contained in:
@@ -16,12 +16,34 @@
|
||||
*/
|
||||
|
||||
import Cocoa
|
||||
import UniformTypeIdentifiers
|
||||
|
||||
class Dialog {
|
||||
func open(directories: Bool, multiple: Bool) -> [String]? {
|
||||
var option: OptionHelper?
|
||||
|
||||
init(_ option: OptionHelper? = nil) {
|
||||
self.option = option
|
||||
}
|
||||
|
||||
func openFiles() -> [String]? {
|
||||
let types: [UTType] = (TypeHelper.toStringArray(option?.root.video_exts) +
|
||||
TypeHelper.toStringArray(option?.root.audio_exts) +
|
||||
TypeHelper.toStringArray(option?.root.image_exts) +
|
||||
TypeHelper.toStringArray(option?.root.archive_exts) +
|
||||
TypeHelper.toStringArray(option?.root.playlist_exts)).compactMap { UTType(filenameExtension: $0) }
|
||||
return open(types: types)
|
||||
}
|
||||
|
||||
func openPlaylist() -> String? {
|
||||
let types: [UTType] = TypeHelper.toStringArray(option?.root.playlist_exts).compactMap { UTType(filenameExtension: $0) }
|
||||
return open(directories: false, multiple: false, types: types)?.first
|
||||
}
|
||||
|
||||
func open(directories: Bool = true, multiple: Bool = true, types: [UTType] = []) -> [String]? {
|
||||
let panel = NSOpenPanel()
|
||||
panel.canChooseDirectories = directories
|
||||
panel.allowsMultipleSelection = multiple
|
||||
panel.allowedContentTypes = types
|
||||
|
||||
if panel.runModal() == .OK {
|
||||
return panel.urls.map { $0.path }
|
||||
|
||||
@@ -76,7 +76,7 @@ class MenuBar: NSObject {
|
||||
let servicesMenu = NSMenu(title: "Services")
|
||||
var menuConfigs: [Config] = []
|
||||
var dynamicMenuItems: [Type: [MenuItem]] = [:]
|
||||
let dialog = Dialog()
|
||||
let dialog: Dialog
|
||||
let appIcon: NSImage
|
||||
|
||||
@objc init(_ appHub: AppHub) {
|
||||
@@ -86,6 +86,7 @@ class MenuBar: NSObject {
|
||||
UserDefaults.standard.set(true, forKey: "NSDisabledCharacterPaletteMenuItem")
|
||||
NSWindow.allowsAutomaticWindowTabbing = false
|
||||
appIcon = appHub.getIcon()
|
||||
dialog = Dialog(appHub.option)
|
||||
|
||||
super.init()
|
||||
|
||||
@@ -324,13 +325,12 @@ class MenuBar: NSObject {
|
||||
}
|
||||
|
||||
@objc func openFiles() {
|
||||
guard let files = dialog.open(directories: true, multiple: true) else { return }
|
||||
guard let files = dialog.openFiles() else { return }
|
||||
appHub.input.open(files: files)
|
||||
}
|
||||
|
||||
@objc func openPlaylist() {
|
||||
guard let files = dialog.open(directories: false, multiple: false),
|
||||
let file = files.first else { return }
|
||||
guard let file = dialog.openPlaylist() else { return }
|
||||
appHub.input.command("loadlist \"\(file)\"")
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user