.mkv/.mka/.mks now open directly into a tags-only document instead of forcing the convert-import sheet: - Metadata, cover art, and the four per-track fields Matroska stores (name, language, enabled, subtitle forced) save back into the same file via MP42MkvTagWriter; MP4-only properties stay read-only. - Real tracks can be deleted and Save-As is enabled; both go through MP42MkvRemuxer's stream-copy rebuild (Document keeps an importer-order track snapshot and pairs TrackEntry elements by object identity). Destinations must stay .mkv/.mka/.mks. - Info.plist: the Matroska document type becomes Editor so NSDocument offers .mkv Save-As instead of defaulting to the MP4 type. - Delete on MKV docs skips organizeAlternateGroups/ inferMediaCharacteristics because they reorder the model, which the TrackEntry pairing depends on; chapter rows stay undeletable. Bumps MP42Foundation for the Matroska tagging/remux support. Co-Authored-By: Claude Code <noreply@anthropic.com>
355 lines
14 KiB
Swift
355 lines
14 KiB
Swift
//
|
|
// TracksViewController.swift
|
|
// Subler
|
|
//
|
|
// Created by Damiano Galassi on 03/02/2018.
|
|
//
|
|
|
|
import Cocoa
|
|
import MP42Foundation
|
|
|
|
protocol TracksViewControllerDelegate: AnyObject {
|
|
@MainActor func didSelect(tracks: [MP42Track])
|
|
@MainActor func delete(tracks: [MP42Track])
|
|
}
|
|
|
|
final class TracksViewController: NSViewController, NSTableViewDataSource, NSTableViewDelegate, ExpandedTableViewDelegate {
|
|
|
|
weak var delegate: TracksViewControllerDelegate?
|
|
|
|
private let document: Document
|
|
|
|
var mp4: MP42File {
|
|
didSet {
|
|
let selectedIndexes = tracksTable.selectedRowIndexes
|
|
reloadData()
|
|
if let last = selectedIndexes.last, last <= mp4.tracks.count {
|
|
tracksTable.selectRowIndexes(selectedIndexes, byExtendingSelection: false)
|
|
}
|
|
}
|
|
}
|
|
|
|
@IBOutlet var tracksTable: ExpandedTableView!
|
|
|
|
// In Matroska tags-only mode the track list cannot be structurally
|
|
// changed: no deletion, reordering or import.
|
|
private var isReadOnly: Bool {
|
|
return document.isMatroska
|
|
}
|
|
|
|
// The per-track fields whose values MP42MkvTagWriter writes back into the
|
|
// file's Tracks element (name, language, enabled, subtitle forced) stay
|
|
// editable even in tags-only mode; the chapter pseudo-track has no
|
|
// Matroska counterpart.
|
|
private func isFieldEditable(_ track: MP42Track?) -> Bool {
|
|
guard let track = track else { return false }
|
|
return document.isMatroska == false || track.isMember(of: MP42ChapterTrack.self) == false
|
|
}
|
|
|
|
private static let languagesMenu: NSMenu = {
|
|
let menu = NSMenu()
|
|
menu.autoenablesItems = false
|
|
|
|
for title in MP42Languages.defaultManager.localizedExtendedLanguages {
|
|
menu.addItem(withTitle: title, action: nil, keyEquivalent: "")
|
|
}
|
|
|
|
return menu
|
|
}()
|
|
|
|
override var nibName: NSNib.Name? {
|
|
return "TracksViewController"
|
|
}
|
|
|
|
override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
|
|
tracksTable.registerForDraggedTypes([.tableViewIndex])
|
|
tracksTable.doubleAction = #selector(doubleClickAction)
|
|
tracksTable.scrollRowToVisible(0)
|
|
}
|
|
|
|
init(document: Document, delegate: TracksViewControllerDelegate) {
|
|
self.document = document;
|
|
self.mp4 = document.mp4
|
|
self.delegate = delegate
|
|
super.init(nibName: nil, bundle: nil)
|
|
}
|
|
|
|
required init?(coder: NSCoder) {
|
|
fatalError("init(coder:) has not been implemented")
|
|
}
|
|
|
|
func reloadData() {
|
|
let indexes = tracksTable.selectedRowIndexes
|
|
tracksTable.reloadData()
|
|
if let last = indexes.last, last <= mp4.tracks.count {
|
|
tracksTable.selectRowIndexes(indexes, byExtendingSelection: false)
|
|
}
|
|
delegate?.didSelect(tracks: selectedTracks)
|
|
}
|
|
|
|
func selectTracks(_ tracks: [MP42Track]) {
|
|
var indexes = IndexSet()
|
|
for track in tracks {
|
|
if let trackIndex = mp4.tracks.firstIndex(of: track) {
|
|
indexes.insert(trackIndex + 1)
|
|
}
|
|
}
|
|
|
|
if indexes.isEmpty && tracksTable.selectedRow == 0 {
|
|
indexes.insert(0)
|
|
}
|
|
tracksTable.selectRowIndexes(indexes, byExtendingSelection: false)
|
|
}
|
|
|
|
var selectedTracks: [MP42Track] {
|
|
return tracksTable != nil ? tracksTable.selectedRowIndexes.compactMap { track(at: $0) } : []
|
|
}
|
|
|
|
// MARK: Delegate
|
|
|
|
func tableViewSelectionDidChange(_ notification: Notification) {
|
|
delegate?.didSelect(tracks: selectedTracks)
|
|
}
|
|
|
|
@IBAction func doubleClickAction(_ sender: NSTableView) {
|
|
// make sure they clicked a real cell and not a header or empty row
|
|
if sender.clickedRow >= 1, let track = track(at: sender.clickedRow) {
|
|
let column = sender.tableColumns[sender.clickedColumn]
|
|
if column.identifier == trackNameColumn, isFieldEditable(track) {
|
|
sender.editColumn(sender.clickedColumn, row: sender.clickedRow, with: nil, select: true)
|
|
}
|
|
}
|
|
}
|
|
|
|
func deleteSelection(in tableview: NSTableView) {
|
|
// On MKV documents real tracks are deletable too (the remuxer drops
|
|
// their blocks on save); the chapter pseudo-track still is not.
|
|
let deletable = document.isMatroska ? selectedTracks.filter { isFieldEditable($0) } : selectedTracks
|
|
guard deletable.isEmpty == false else { return }
|
|
delegate?.delete(tracks: deletable)
|
|
}
|
|
|
|
func tableView(_ tableView: NSTableView, shouldEdit tableColumn: NSTableColumn?, row: Int) -> Bool {
|
|
if isReadOnly == false {
|
|
return true
|
|
}
|
|
// tags-only mode still allows inline renaming of real tracks
|
|
return tableColumn?.identifier == trackNameColumn && isFieldEditable(track(at: row))
|
|
}
|
|
|
|
// MARK: Actions
|
|
|
|
@IBAction func setTrackEnabled(_ sender: NSButton) {
|
|
let row = tracksTable.row(for: sender)
|
|
if let track = track(at: row) {
|
|
track.isEnabled = sender.state == NSControl.StateValue.on ? true : false
|
|
}
|
|
}
|
|
|
|
@IBAction func setTrackName(_ sender: NSTextField) {
|
|
let row = tracksTable.row(for: sender)
|
|
if let track = track(at: row), track.name != sender.stringValue {
|
|
track.name = sender.stringValue
|
|
let column = tracksTable.column(for: sender)
|
|
tracksTable.reloadData(forRowIndexes: IndexSet(integer: row), columnIndexes: IndexSet(integer: column))
|
|
}
|
|
}
|
|
|
|
@IBAction func setTrackLanguage(_ sender: NSPopUpButton) {
|
|
let manager = MP42Languages.defaultManager
|
|
let row = tracksTable.row(for: sender)
|
|
let index = sender.indexOfSelectedItem
|
|
var language = sender.selectedItem?.title
|
|
|
|
if index >= 0 && index < manager.localizedExtendedLanguages.count {
|
|
let localizedLanguage = MP42Languages.defaultManager.localizedExtendedLanguages[sender.indexOfSelectedItem]
|
|
language = MP42Languages.defaultManager.extendedTag(forLocalizedLang: localizedLanguage)
|
|
}
|
|
|
|
if let track = track(at: row), track.language != language {
|
|
track.language = language ?? "und"
|
|
}
|
|
}
|
|
|
|
// MARK: Data source
|
|
|
|
private let trackIdColumn = NSUserInterfaceItemIdentifier(rawValue: "trackId")
|
|
private let trackEnabledColumn = NSUserInterfaceItemIdentifier(rawValue: "trackEnabled")
|
|
private let trackNameColumn = NSUserInterfaceItemIdentifier(rawValue: "trackName")
|
|
private let trackDurationColumn = NSUserInterfaceItemIdentifier(rawValue: "trackDuration")
|
|
private let trackLanguageColumn = NSUserInterfaceItemIdentifier(rawValue: "trackLanguage")
|
|
private let trackInfoColumn = NSUserInterfaceItemIdentifier(rawValue: "trackInfo")
|
|
|
|
private let disabledIdCell = NSUserInterfaceItemIdentifier(rawValue: "disabledIdCell")
|
|
private let disabledFormatCell = NSUserInterfaceItemIdentifier(rawValue: "DisabledFormatCell")
|
|
|
|
private func track(at row: Int) -> MP42Track? {
|
|
return row <= 0 ? nil : mp4.track(at: UInt(row - 1))
|
|
}
|
|
|
|
func numberOfRows(in tableView: NSTableView) -> Int {
|
|
return mp4.tracks.count + 1
|
|
}
|
|
|
|
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
|
|
|
|
if let track = track(at: row) {
|
|
|
|
switch tableColumn?.identifier {
|
|
|
|
case trackIdColumn?:
|
|
let cell = tableView.makeView(withIdentifier: trackIdColumn, owner:self) as? NSTableCellView
|
|
cell?.textField?.stringValue = track.trackId == 0 ? NSLocalizedString("na", comment: "") : String(track.trackId)
|
|
return cell
|
|
|
|
case trackEnabledColumn?:
|
|
let cell = tableView.makeView(withIdentifier: trackEnabledColumn, owner:self) as? CheckBoxCellView
|
|
cell?.checkboxButton?.state = track.isEnabled ? NSControl.StateValue.on : NSControl.StateValue.off
|
|
cell?.checkboxButton?.isEnabled = isFieldEditable(track)
|
|
return cell
|
|
|
|
case trackNameColumn?:
|
|
let cell = tableView.makeView(withIdentifier: trackNameColumn, owner:self) as? NSTableCellView
|
|
cell?.textField?.stringValue = track.name
|
|
cell?.textField?.isEditable = isFieldEditable(track)
|
|
return cell
|
|
|
|
case trackDurationColumn?:
|
|
let cell = tableView.makeView(withIdentifier: trackDurationColumn, owner:self) as? NSTableCellView
|
|
cell?.textField?.attributedStringValue = StringFromTime(Int64(track.duration), 1000).monospacedAttributedString()
|
|
return cell
|
|
|
|
case trackLanguageColumn?:
|
|
let cell = tableView.makeView(withIdentifier: trackLanguageColumn, owner:self) as? PopUpCellView
|
|
|
|
if (cell?.popUpButton.numberOfItems == 0) {
|
|
cell?.popUpButton.menu = TracksViewController.languagesMenu.copy() as? NSMenu
|
|
}
|
|
|
|
cell?.popUpButton.selectItem(withTitle: MP42Languages.defaultManager.localizedLang(forExtendedTag: track.language))
|
|
|
|
if (cell?.popUpButton.indexOfSelectedItem == -1) {
|
|
cell?.popUpButton.addItem(withTitle: track.language)
|
|
cell?.popUpButton.selectItem(withTitle: track.language)
|
|
}
|
|
|
|
cell?.textField?.stringValue = MP42Languages.defaultManager.localizedLang(forExtendedTag: track.language)
|
|
cell?.popUpButton?.isEnabled = isFieldEditable(track)
|
|
return cell
|
|
|
|
case trackInfoColumn?:
|
|
let cell = tableView.makeView(withIdentifier: trackInfoColumn, owner:self) as? NSTableCellView
|
|
if isReadOnly {
|
|
// Tags-only documents never mux, and the default conversion
|
|
// settings attached at import (see Document.readMatroska)
|
|
// would make formatSummary advertise a conversion target
|
|
// instead of the actual source codec.
|
|
var summary = localizedDisplayName(track.mediaType, track.format) ?? "Unknown"
|
|
if let audioTrack = track as? MP42AudioTrack, audioTrack.channels > 0,
|
|
track.format != kMP42AudioCodecType_DTS {
|
|
summary = "\(summary), \(audioTrack.channels) ch"
|
|
}
|
|
cell?.textField?.stringValue = summary
|
|
} else {
|
|
cell?.textField?.stringValue = track.formatSummary
|
|
}
|
|
return cell
|
|
|
|
default:
|
|
return nil
|
|
}
|
|
|
|
}
|
|
else {
|
|
switch tableColumn?.identifier {
|
|
|
|
case trackIdColumn?:
|
|
let cell = tableView.makeView(withIdentifier: disabledIdCell, owner:self) as? NSTableCellView
|
|
cell?.textField?.stringValue = "-"
|
|
return cell
|
|
|
|
case trackNameColumn?:
|
|
let cell = tableView.makeView(withIdentifier: trackNameColumn, owner:self) as? NSTableCellView
|
|
cell?.textField?.stringValue = NSLocalizedString("Metadata", comment: "")
|
|
cell?.textField?.isEditable = false
|
|
return cell
|
|
|
|
case trackDurationColumn?:
|
|
let cell = tableView.makeView(withIdentifier: trackDurationColumn, owner:self) as? NSTableCellView
|
|
cell?.textField?.attributedStringValue = StringFromTime(Int64(mp4.duration), 1000).monospacedAttributedString()
|
|
return cell
|
|
|
|
case trackLanguageColumn?:
|
|
let cell = tableView.makeView(withIdentifier: disabledFormatCell, owner:self) as? NSTableCellView
|
|
cell?.textField?.stringValue = NSLocalizedString("-NA-", comment: "")
|
|
return cell
|
|
|
|
case trackInfoColumn?:
|
|
let cell = tableView.makeView(withIdentifier: disabledFormatCell, owner:self) as? NSTableCellView
|
|
cell?.textField?.stringValue = NSLocalizedString("-NA-", comment: "")
|
|
return cell
|
|
|
|
default:
|
|
return nil
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
// MARK: Drag & drop
|
|
|
|
func tableView(_ tableView: NSTableView,
|
|
pasteboardWriterForRow row: Int) -> (any NSPasteboardWriting)? {
|
|
if isReadOnly {
|
|
return nil
|
|
}
|
|
if let track = track(at: row), track.isMuxed == false {
|
|
return PasteboardItem(index: row, type: .tableViewIndex)
|
|
} else {
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func tableView(_ tableView: NSTableView,
|
|
validateDrop info: NSDraggingInfo,
|
|
proposedRow row: Int,
|
|
proposedDropOperation dropOperation: NSTableView.DropOperation) -> NSDragOperation {
|
|
if isReadOnly { return [] }
|
|
guard dropOperation == .above else { return [] }
|
|
|
|
guard let source = info.draggingSource as? NSTableView,
|
|
tableView == source else { return [] }
|
|
|
|
let count = mp4.tracks.count + 1
|
|
if row < count && row != 0, let track = track(at: row), track.isMuxed == false {
|
|
return .move
|
|
} else if row == count {
|
|
return .move
|
|
}
|
|
|
|
return []
|
|
}
|
|
|
|
func tableView(_ tableView: NSTableView,
|
|
acceptDrop info: NSDraggingInfo,
|
|
row: Int,
|
|
dropOperation: NSTableView.DropOperation) -> Bool {
|
|
guard let pasteboardItems = info.draggingPasteboard.pasteboardItems
|
|
else { return false }
|
|
|
|
if let source = info.draggingSource as? NSTableView,
|
|
source == tableView
|
|
{
|
|
let rowIndexes = IndexSet(pasteboardItems.compactMap { $0.integer(forType: .tableViewIndex) })
|
|
let tracks = mp4.tracks.enumerated().filter { rowIndexes.contains($0.offset + 1)} .map { $0.element }
|
|
mp4.moveTracks(tracks, to: UInt(row - 1))
|
|
tableView.reloadData()
|
|
return true
|
|
}
|
|
|
|
return false
|
|
}
|
|
}
|