Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes & improvements to Subtitle Chooser #4833

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion iina/Base.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@
"osd.find_online_sub" = "Finding online subtitles…";
"osd.find_online_sub.source" = "from";
"osd.sub_not_found" = "No subtitles found";
"osd.sub_found" = "%d subtitles found. Downloading…";
"osd.sub_found" = "%d subtitles found.";
"osd.sub_downloading" = "Downloading %d subtitles";
"osd.sub_downloaded" = "Subtitle downloaded";
"osd.sub_saved" = "Subtitle saved";
"osd.network_error" = "Network error";
Expand Down
102 changes: 56 additions & 46 deletions iina/Base.lproj/SubChooseViewController.xib

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions iina/Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,15 @@ extension BinaryInteger {
}
}

// Formats a number to max 2 digits after the decimal, rounded, but will omit trailing zeroes, and no commas or other formatting for large numbers
fileprivate let fmtDecimalMaxFractionDigits2: NumberFormatter = {
let fmt = NumberFormatter()
fmt.numberStyle = .decimal
fmt.usesGroupingSeparator = false
fmt.maximumFractionDigits = 2
return fmt
}()

extension FloatingPoint {
func clamped(to range: Range<Self>) -> Self {
if self < range.lowerBound {
Expand All @@ -290,6 +299,12 @@ extension FloatingPoint {
return self
}
}

/// Formats as String, rounding the number to 2 digits after the decimal
var stringMaxFrac2: String {
return fmtDecimalMaxFractionDigits2.string(for: self)!
}

}

extension NSColor {
Expand Down
7 changes: 6 additions & 1 deletion iina/OSDMessage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ enum OSDMessage {

case startFindingSub(String) // sub source
case foundSub(Int)
case downloadingSub(Int, String) // download count, ssub source
case downloadedSub(String) // filename
case savedSub
case cannotLogin
Expand Down Expand Up @@ -297,9 +298,13 @@ enum OSDMessage {
case .foundSub(let count):
let str = count == 0 ?
NSLocalizedString("osd.sub_not_found", comment: "No subtitles found.") :
String(format: NSLocalizedString("osd.sub_found", comment: "%d subtitle(s) found. Downloading..."), count)
String(format: NSLocalizedString("osd.sub_found", comment: "%d subtitle(s) found."), count)
return (str, .normal)

case .downloadingSub(let count, let source):
let str = String(format: NSLocalizedString("osd.sub_downloading", comment: "Downloading %d subtitles"), count)
return (str, .withText(NSLocalizedString("osd.find_online_sub.source", comment: "from") + " " + source))

case .downloadedSub(let filename):
return (
NSLocalizedString("osd.sub_downloaded", comment: "Subtitle downloaded"),
Expand Down
4 changes: 2 additions & 2 deletions iina/OnlineSubtitle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,11 @@ class OnlineSubtitle: NSObject {

func fetchSubtitles(url: URL, player: PlayerCore) -> Promise<[URL]> {
return getFetcher().fetch(from: url, withProviderID: providerID, playerCore: player)
.get { subtitles in
.get { [self] subtitles in
if subtitles.isEmpty {
throw OnlineSubtitle.CommonError.noResult
} else {
player.sendOSD(.foundSub(subtitles.count))
player.sendOSD(.downloadingSub(subtitles.count, name))
}
}.thenFlatMap { subtitle in
subtitle.download()
Expand Down
2 changes: 0 additions & 2 deletions iina/OpenSubClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -805,9 +805,7 @@ class OpenSubClient {
var comments: String?
#endif
var downloadCount: Int
#if DEBUG
var featureDetails: SubtitleFeatureDetails
#endif
var files: [SubtitleFile]
#if DEBUG
var foreignPartsOnly: Bool?
Expand Down
31 changes: 17 additions & 14 deletions iina/OpenSubSubtitle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class OpenSub {
private static let dateFormatter: DateFormatter = {
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .medium
dateFormatter.timeStyle = .medium
dateFormatter.timeStyle = .none
return dateFormatter
}()

Expand Down Expand Up @@ -72,22 +72,25 @@ class OpenSub {
/// view displayed to the user for choosing the subtitle files to download.
override func getDescription() -> (name: String, left: String, right: String) {
let attributes = subtitle.attributes
let downloadCount = String(attributes.downloadCount)
let filename = attributes.files[0].fileName
let framesPerSecond: String
var tokens: [String] = []

tokens.append(attributes.language)

if let releaseYear = attributes.featureDetails.year, releaseYear > 0 {
tokens.append("(\(releaseYear))")
}

if let fps = attributes.fps, fps != 0 {
framesPerSecond = " \(String(Int(fps.rounded(.up)))) fps"
} else {
framesPerSecond = ""
tokens.append("\(fps.stringMaxFrac2) fps")
}
let language = attributes.language
let rating = String(attributes.ratings)

let downloadCount = "\u{2b07}\(attributes.downloadCount)"
tokens.append(downloadCount)

let fileName = attributes.files[0].fileName
let description = tokens.joined(separator: " ")
let uploadDate = OpenSub.Subtitle.dateFormatter.string(from: attributes.uploadDate)
return (
filename,
"\(language)\(framesPerSecond) \u{2b07}\(downloadCount) \u{2605}\(rating)",
uploadDate
)
return (fileName, description, uploadDate)
}
}

Expand Down
5 changes: 5 additions & 0 deletions iina/SubChooseViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ class SubChooseViewController: NSViewController {
override func viewDidLoad() {
super.viewDidLoad()

if let scrollView = tableView.enclosingScrollView {
scrollView.wantsLayer = true
scrollView.layer?.cornerRadius = 6
}

tableView.delegate = self
tableView.dataSource = self

Expand Down