Skip to content

Commit

Permalink
UX improvements to the “Jump to…” command (iina#4084)
Browse files Browse the repository at this point in the history
* feat(ui): allow quick prompt panel to have initial value

* feat(ui): fill in current time code as initial value in jump to panel

* feat(ui): allow jumping to sub-second precision time codes

* fix(ui): return nil on invalid format, don’t assume 0

* chore: reformat

Co-authored-by: Yuze Jiang <[email protected]>

---------

Co-authored-by: Yuze Jiang <[email protected]>
  • Loading branch information
sabberworm and uiryuu authored Jan 8, 2024
1 parent 19beb74 commit 38b8501
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 19 deletions.
4 changes: 2 additions & 2 deletions iina/MainMenuActions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,9 @@ extension MainMenuActionHandler {
}

@objc func menuJumpTo(_ sender: NSMenuItem) {
Utility.quickPromptPanel("jump_to") { input in
Utility.quickPromptPanel("jump_to", inputValue: self.player.info.videoPosition?.stringRepresentationWithPrecision(3)) { input in
if let vt = VideoTime(input) {
self.player.seek(absoluteSecond: Double(vt.second))
self.player.seek(absoluteSecond: vt.second)
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion iina/Utility.swift
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ class Utility {
- Returns: Whether user dismissed the panel by clicking OK. Only works when using `.modal` mode.
*/
@discardableResult
static func quickPromptPanel(_ key: String, titleComment: String? = nil, messageComment: String? = nil, sheetWindow: NSWindow? = nil, callback: @escaping (String) -> Void) -> Bool {
static func quickPromptPanel(_ key: String, titleComment: String? = nil, messageComment: String? = nil, inputValue: String? = nil, sheetWindow: NSWindow? = nil, callback: @escaping (String) -> Void) -> Bool {
let panel = NSAlert()
let titleKey = "alert." + key + ".title"
let messageKey = "alert." + key + ".message"
Expand All @@ -236,6 +236,9 @@ class Utility {
input.lineBreakMode = .byClipping
input.usesSingleLineMode = true
input.cell?.isScrollable = true
if let inputValue = inputValue {
input.stringValue = inputValue
}
panel.accessoryView = input
panel.addButton(withTitle: NSLocalizedString("general.ok", comment: "OK"))
panel.addButton(withTitle: NSLocalizedString("general.cancel", comment: "Cancel"))
Expand Down
27 changes: 11 additions & 16 deletions iina/VideoTime.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import Foundation

class VideoTime {

static let infinite = VideoTime(999, 0, 0)
static let zero = VideoTime(0)

Expand Down Expand Up @@ -49,30 +48,26 @@ class VideoTime {
}

convenience init?(_ format: String) {
let split = format.split(separator: ":").map { (seq) -> Int? in
return Int(String(seq))
}
if !(split.contains {$0 == nil}) {
// if no nil in array
if split.count == 2 {
self.init(0, split[0]!, split[1]!)
} else if split.count == 3 {
self.init(split[0]!, split[1]!, split[2]!)
} else {
return nil
}
} else {
let split = Array(format.split(separator: ":").reversed())

let hour: Int? = split.count > 2 ? Int(split[2]) : nil
let minute: Int? = split.count > 1 ? Int(split[1]) : nil
let second: Double? = !split.isEmpty ? Double(split[0]) : nil

if hour == nil && minute == nil && second == nil {
return nil
}

self.init(hour ?? 0, minute ?? 0, second ?? 0.0)
}

init(_ second: Double) {
self.second = second

}

init(_ hour: Int, _ minute: Int, _ second: Int) {
self.second = Double(hour * 3600 + minute * 60 + second)
init(_ hour: Int, _ minute: Int, _ second: Double) {
self.second = Double(hour * 3600 + minute * 60) + second
}

/** whether self in [min, max) */
Expand Down

0 comments on commit 38b8501

Please sign in to comment.