Files
SublerMKV/Classes/PropertyView/ChapterViewController.swift
T
Scott GarrettandClaude Code f457b4e89c Make chapter tracks deletable, insertable, and language-editable
Delete: the phase-1 gate against deleting the chapter pseudo-track on
Matroska documents is obsolete now that chapter changes ride the
stream-copy remuxer — a removed pseudo-track simply makes the model
report "no chapters", which removes the Chapters element on save (the
same path emptying the Chapters tab already took). The toolbar Delete
button's validation is corrected too; it still rejected chapter-only
selections after deleteSelection was opened up.

Insert: the Actions toolbar gets a single "Insert a chapter" item, and
"Insert a chapter every" (previously MP4-only) is enabled on Matroska.
Both create the chapter track when the document has none, doing the
tags-only bookkeeping the model needs (dense display trackId,
updateChangeCount). A single insert lands 1s after the latest start,
clamped to the track duration, so it can never tie with a stored
chapter — ties get clamped +1ms on write and would silently shift the
user's own chapter times.

Language: the chapter row's language popup stays editable on MKV
documents (the value rides the remuxer's chapter rebuild, not the tag
writer, since the pseudo-track has no TrackEntry), and the Matroska
chapters snapshot carries it so a language-only edit triggers the
remux. Pairs with the submodule's ChapterLanguage round trip.

Co-Authored-By: Claude Code <noreply@anthropic.com>
2026-11-26 18:17:37 -05:00

161 lines
5.5 KiB
Swift

//
// ChapterViewController.swift
// Subler
//
// Created by Damiano Galassi on 11/11/2017.
//
import Cocoa
import MP42Foundation
final class ChapterViewController : PropertyView, NSTableViewDataSource, NSTableViewDelegate, ExpandedTableViewDelegate {
var track: MP42ChapterTrack {
didSet {
let indexes = tableView.selectedRowIndexes
tableView.reloadData()
if let last = indexes.last, last < track.chapters.count {
tableView.selectRowIndexes(indexes, byExtendingSelection: false)
}
}
}
@IBOutlet var tableView: ExpandedTableView!
@IBOutlet var removeChapter: NSButton!
@IBOutlet var actions: NSPopUpButton!
override var nibName: NSNib.Name? {
return "ChapterView"
}
init(track: MP42ChapterTrack) {
self.track = track
super.init(nibName: nil, bundle: nil)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
tableView.defaultEditingColumn = 1
tableView.doubleAction = #selector(doubleClickAction(_:))
if #available(macOS 26, *) {
actions.menu?.items.first?.image = NSImage.init(systemSymbolName: "ellipsis", accessibilityDescription: nil)
}
}
// MARK: Table View
func numberOfRows(in tableView: NSTableView) -> Int {
return Int(track.chapterCount())
}
let trackTimeColumn = NSUserInterfaceItemIdentifier(rawValue: "time")
let trackTitleColumn = NSUserInterfaceItemIdentifier(rawValue: "title")
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
let chapter = track.chapter(at: UInt(row))
switch tableColumn?.identifier {
case trackTimeColumn?:
let cell = tableView.makeView(withIdentifier: trackTimeColumn, owner:self) as? NSTableCellView
cell?.textField?.attributedStringValue = StringFromTime(Int64(chapter.timestamp), 1000).boldMonospacedAttributedString()
return cell
case trackTitleColumn?:
let cell = tableView.makeView(withIdentifier: trackTitleColumn, owner:self) as? NSTableCellView
cell?.textField?.stringValue = chapter.title
return cell
default:
return nil
}
}
func tableViewSelectionDidChange(_ notification: Notification) {
removeChapter.isEnabled = tableView.selectedRow != -1 && isReadOnly == false
}
func tableView(_ tableView: NSTableView, shouldEdit tableColumn: NSTableColumn?, row: Int) -> Bool {
return isReadOnly == false
}
@IBAction func setChapterTitle(_ sender: NSTextField) {
let row = tableView.row(for: sender)
if row > -1 {
let chapter = track.chapter(at: UInt(row))
track.setTitle(sender.stringValue, forChapter: chapter)
tableView.reloadData(forRowIndexes: IndexSet(integer: row), columnIndexes: IndexSet(integer: 1))
}
}
@IBAction func setChapterTimeStamp(_ sender: NSTextField) {
let row = tableView.row(for: sender)
if row > -1 {
let chapter = track.chapter(at: UInt(row))
let timestamp = TimeFromString(sender.stringValue, 1000)
track.setTimestamp(timestamp, forChapter: chapter)
tableView.reloadData()
}
}
@IBAction func doubleClickAction(_ sender: NSTableView) {
guard isReadOnly == false else { return }
if sender.clickedRow > -1 && sender.clickedColumn > -1 {
sender.editColumn(sender.clickedColumn, row: sender.clickedRow, with: nil, select: true)
}
}
func deleteSelection(in tableview: NSTableView) {
guard isReadOnly == false else { return }
let indexes = tableView.selectedRowIndexes
if indexes.isEmpty == false {
track.removeChapters(at: indexes)
tableView.removeRows(at: indexes, withAnimation: NSTableView.AnimationOptions.slideUp)
if let index = indexes.first, index < track.chapters.count {
tableView.selectRowIndexes(IndexSet(integer: index), byExtendingSelection: false)
}
}
}
// MARK: Actions
@IBAction func removeChapter(_ sender: Any) {
deleteSelection(in: tableView)
}
// Selects and scrolls to a row (used after the window controller appends
// a chapter from the Actions menu, when the tab may not have shown this
// track's table yet).
func selectChapter(at index: Int) {
guard isViewLoaded, index >= 0, index < track.chapters.count else { return }
tableView.reloadData()
tableView.scrollRowToVisible(index)
let indexSet = IndexSet(integer: index)
tableView.selectRowIndexes(indexSet, byExtendingSelection: false)
view.window?.makeFirstResponder(tableView)
}
@IBAction func addChapter(_ sender: Any) {
let index = track.addChapter("Chapter", timestamp: 0)
let indexSet = IndexSet(integer: Int(index))
tableView.insertRows(at: indexSet, withAnimation: .slideDown)
tableView.selectRowIndexes(indexSet, byExtendingSelection: false)
view.window?.makeFirstResponder(tableView)
}
@IBAction func renameChapters(_ sender: Any) {
for (index, chapter) in track.chapters.enumerated() {
let title = "Chapter \(index + 1)"
track.setTitle(title, forChapter: chapter)
}
tableView.reloadData()
}
}