From 1be37f9e155eecfc48c4b6fd7cf2df836079f0c2 Mon Sep 17 00:00:00 2001 From: Damiano Galassi Date: Tue, 1 Aug 2017 12:27:03 +0200 Subject: [PATCH] Make it on par with the old controller. --- Classes/ChapterSearchController.swift | 29 ++++++++++++++++++++--- Classes/MetadataImporters/ChapterDB.swift | 10 ++++++-- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/Classes/ChapterSearchController.swift b/Classes/ChapterSearchController.swift index 7b5f908..2f340cd 100644 --- a/Classes/ChapterSearchController.swift +++ b/Classes/ChapterSearchController.swift @@ -11,7 +11,7 @@ import Cocoa func chapterImportDone(chaptersToBeImported: [MP42TextSample]) } -@objc class ChapterSearchController: NSWindowController, NSTableViewDataSource, NSTableViewDelegate { +@objc class ChapterSearchController: NSWindowController, NSTableViewDataSource, NSTableViewDelegate, NSTextFieldDelegate { @IBOutlet var searchTitle: NSTextField! @@ -126,8 +126,8 @@ import Cocoa private func updateUI() { switch state { case .none: - searchButton.isEnabled = true searchButton.keyEquivalent = "\r" + updateSearchButtonVisibility() case .searching(_): progress.startAnimation(self) progress.isHidden = false @@ -158,6 +158,16 @@ import Cocoa } } + private func updateSearchButtonVisibility() { + searchButton.isEnabled = searchTitle.stringValue.count > 0 ? true : false + } + + override func controlTextDidChange(_ obj: Notification) { + updateSearchButtonVisibility() + searchButton.keyEquivalent = "\r" + addButton.keyEquivalent = "" + } + // MARK: - Table View func tableViewSelectionDidChange(_ notification: Notification) { @@ -213,7 +223,7 @@ import Cocoa let chapter = result.chapters[row] if tableColumn?.identifier.rawValue == "time" { - return StringFromTime(Int64(chapter.timestamp), 1000).monospacedAttributedString() + return StringFromTime(Int64(chapter.timestamp), 1000).boldMonospacedAttributedString() } else if tableColumn?.identifier.rawValue == "name" { return chapter.name @@ -222,4 +232,17 @@ import Cocoa return nil } + func tableView(_ tableView: NSTableView, willDisplayCell cell: Any, for tableColumn: NSTableColumn?, row: Int) { + if tableView == chapterTable && tableColumn?.identifier.rawValue == "time", let cell = cell as? NSTextFieldCell { + if tableView.selectedRowIndexes.contains(row) { + let highlightedString = NSMutableAttributedString(attributedString: cell.attributedStringValue) + highlightedString.addAttribute(NSAttributedStringKey.foregroundColor, value: NSColor.black, range: NSMakeRange(0, highlightedString.length)) + cell.attributedStringValue = highlightedString + } + else { + cell.textColor = NSColor.gray + } + } + } + } diff --git a/Classes/MetadataImporters/ChapterDB.swift b/Classes/MetadataImporters/ChapterDB.swift index 02a5463..8093636 100644 --- a/Classes/MetadataImporters/ChapterDB.swift +++ b/Classes/MetadataImporters/ChapterDB.swift @@ -15,8 +15,14 @@ public struct ChapterDB : ChapterService { let data = fetch(url: url) else { return [] } - let delta: Int64 = 10000 - return parse(data: data).filter { $0.duration < (Int64(duration) + delta) && $0.duration > (Int64(duration) - delta) } + let parsed = parse(data: data) + if (duration > 0) { + let delta: Int64 = 10000 + return parsed.filter { $0.duration < (Int64(duration) + delta) && $0.duration > (Int64(duration) - delta) } + } + else { + return parsed + } } private func fetch(url: URL) -> Data? {