Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
* dev:
  update: README
  bump version
  feat: drag n drop to import lyrics
  drag with string content
  drag with string content
  refactor: duplicated code
  feat: drag & drop to export search result
  style: no code change
  group files
  • Loading branch information
ddddxxx committed Apr 10, 2017
2 parents 61526ff + 4bca01f commit 03da6be
Show file tree
Hide file tree
Showing 29 changed files with 186 additions and 63 deletions.
74 changes: 43 additions & 31 deletions LyricsX.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions LyricsX/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -1211,6 +1211,10 @@
<rect key="frame" x="0.0" y="0.0" width="320" height="280"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<customView fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="00o-Z8-m5E" customClass="DragNDropView" customModule="LyricsX" customModuleProvider="target">
<rect key="frame" x="0.0" y="0.0" width="320" height="280"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
</customView>
<scrollView wantsLayer="YES" borderType="none" horizontalLineScroll="10" horizontalPageScroll="10" verticalLineScroll="10" verticalPageScroll="10" hasHorizontalScroller="NO" usesPredominantAxisScrolling="NO" translatesAutoresizingMaskIntoConstraints="NO" id="IdM-WB-V2o" customClass="ScrollLyricsView" customModule="LyricsX" customModuleProvider="target">
<rect key="frame" x="8" y="8" width="304" height="264"/>
<clipView key="contentView" drawsBackground="NO" id="lTs-ol-c97">
Expand Down Expand Up @@ -1281,6 +1285,7 @@ IA
</constraints>
</view>
<connections>
<outlet property="dragNDropView" destination="00o-Z8-m5E" id="kV0-Yy-bTs"/>
<outlet property="lyricsScrollView" destination="IdM-WB-V2o" id="c7A-VH-22w"/>
</connections>
</viewController>
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@
import Cocoa
import EasyPreference

class LyricsHUDViewController: NSViewController, ScrollLyricsViewDelegate {
class LyricsHUDViewController: NSViewController, ScrollLyricsViewDelegate, DragNDropDelegate {

@IBOutlet weak var dragNDropView: DragNDropView!
@IBOutlet weak var lyricsScrollView: ScrollLyricsView!

dynamic var isTracking = true

override func viewWillAppear() {
override func awakeFromNib() {
view.window?.titlebarAppearsTransparent = true
view.window?.titleVisibility = .hidden
view.window?.styleMask.insert(.borderless)
Expand All @@ -24,6 +25,8 @@ class LyricsHUDViewController: NSViewController, ScrollLyricsViewDelegate {
accessory.layoutAttribute = .right
view.window?.addTitlebarAccessoryViewController(accessory)

dragNDropView.dragDelegate = self

lyricsScrollView.delegate = self
lyricsScrollView.setupTextContents(lyrics: appDelegate()?.mediaPlayerHelper.currentLyrics)

Expand Down Expand Up @@ -74,6 +77,12 @@ class LyricsHUDViewController: NSViewController, ScrollLyricsViewDelegate {
isTracking = false
}

// MARK: DragNDrop Delegate

func dragFinished(content: String) {
appDelegate()?.mediaPlayerHelper.importLyrics(content)
}

}

class LyricsHUDAccessoryViewController: NSTitlebarAccessoryViewController {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,14 @@ class SearchLyricsViewController: NSViewController, NSTableViewDelegate, NSTable

override func viewDidLoad() {
lyricsHelper.delegate = self
let helper = appDelegate()?.mediaPlayerHelper
tableView.setDraggingSourceOperationMask(.copy, forLocal: false)
normalConstraint.isActive = false

let helper = appDelegate()?.mediaPlayerHelper
searchArtist = helper?.player?.currentTrack?.artist ?? ""
searchTitle = helper?.player?.currentTrack?.name ?? ""
searchAction(nil)

super.viewDidLoad()
}

Expand Down Expand Up @@ -104,13 +107,39 @@ class SearchLyricsViewController: NSViewController, NSTableViewDelegate, NSTable
return
}
if self.hideLrcPreviewConstraint?.isActive == true {
self.expansionPreview()
self.expandPreview()
}
self.lyricsPreviewTextView.string = self.searchResult[index].contentString(withMetadata: false, ID3: true, timeTag: true, translation: true)
self.updateImage()
}

func expansionPreview() {
func tableView(_ tableView: NSTableView, writeRowsWith rowIndexes: IndexSet, to pboard: NSPasteboard) -> Bool {
let lrcContent = searchResult[rowIndexes.first!].contentString(withMetadata: false, ID3: true, timeTag: true, translation: true)
pboard.declareTypes([NSStringPboardType, NSFilesPromisePboardType], owner: self)
pboard.setString(lrcContent, forType: NSStringPboardType)
pboard.setPropertyList(["lrc"], forType: NSFilesPromisePboardType)
return true
}

func tableView(_ tableView: NSTableView, namesOfPromisedFilesDroppedAtDestination dropDestination: URL, forDraggedRowsWith indexSet: IndexSet) -> [String] {
return indexSet.flatMap { index -> String? in
let fileName = searchResult[index].fileName

let destURL = dropDestination.appendingPathComponent(fileName)
let lrcStr = searchResult[index].contentString(withMetadata: false, ID3: true, timeTag: true, translation: true)

do {
try lrcStr.write(to: destURL, atomically: true, encoding: .utf8)
} catch let error as NSError{
print(error)
return nil
}

return fileName
}
}

func expandPreview() {
let expandingHeight = -view.subviews.reduce(0) { min($0, $1.frame.minY) }
var windowFrame = self.view.window!.frame
windowFrame.size.height += expandingHeight
Expand Down
10 changes: 10 additions & 0 deletions LyricsX/Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,13 @@ extension EasyPreference {
}

}

extension Lyrics {

var fileName: String {
let title = (metadata[.searchTitle] as? String)?.replacingOccurrences(of: "/", with: "&") ?? ""
let artist = (metadata[.searchArtist] as? String)?.replacingOccurrences(of: "/", with: "&") ?? ""
return "\(title) - \(artist).lrc"
}

}
26 changes: 6 additions & 20 deletions LyricsX/Lyrics/Lyrics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -126,36 +126,22 @@ extension Lyrics {
return rawValue.hash
}

static let title: IDTagKey = .init("ti")

static let album: IDTagKey = .init("al")

static let artist: IDTagKey = .init("ar")

static let author: IDTagKey = .init("au")

static let lrcBy: IDTagKey = .init("by")

static let offset: IDTagKey = .init("offset")

static let title = IDTagKey("ti")
static let album = IDTagKey("al")
static let artist = IDTagKey("ar")
static let author = IDTagKey("au")
static let lrcBy = IDTagKey("by")
static let offset = IDTagKey("offset")
}

enum MetadataKey: String {

case source = "source"

case lyricsURL = "lyricsURL"

case searchTitle = "searchTitle"

case searchArtist = "searchArtist"

case searchIndex = "searchIndex"

case artworkURL = "artworkURL"

case includeTranslation = "includeTranslation"

}

}
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
22 changes: 17 additions & 5 deletions LyricsX/MediaPlayerHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,22 @@ class MediaPlayerHelper: NSObject, MediaPlayerDelegate, LyricsSourceDelegate {

}

extension MediaPlayerHelper {

func importLyrics(_ lyrics: String) {
if var lrc = Lyrics(lyrics),
let track = player?.currentTrack {
lrc.metadata = [
.searchTitle: track.name,
.searchArtist: track.artist,
.source: "Import"
]
setCurrentLyrics(lyrics: lrc)
}
}

}

extension LyricsSourceHelper {

static func readLocalLyrics(title: String, artist: String) -> Lyrics? {
Expand Down Expand Up @@ -183,11 +199,7 @@ extension Lyrics {
try fileManager.createDirectory(at: url, withIntermediateDirectories: true, attributes: nil)
}

guard let titleForSaving = (metadata[.searchTitle] as? String)?.replacingOccurrences(of: "/", with: "&"),
let artistForSaving = (metadata[.searchArtist] as? String)?.replacingOccurrences(of: "/", with: "&") else {
return
}
let lrcFileURL = url.appendingPathComponent("\(titleForSaving) - \(artistForSaving).lrc")
let lrcFileURL = url.appendingPathComponent(fileName)

if fileManager.fileExists(atPath: lrcFileURL.path) {
try fileManager.removeItem(at: lrcFileURL)
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions LyricsX/Info.plist → LyricsX/Support Files/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.0.0</string>
<string>1.1.0</string>
<key>CFBundleVersion</key>
<string>946</string>
<string>950</string>
<key>LSMinimumSystemVersion</key>
<string>$(MACOSX_DEPLOYMENT_TARGET)</string>
<key>LSUIElement</key>
Expand Down
File renamed without changes.
File renamed without changes.
54 changes: 54 additions & 0 deletions LyricsX/View/DragNDropView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//
// DragNDropView.swift
// LyricsX
//
// Created by 邓翔 on 2017/4/10.
// Copyright © 2017年 ddddxxx. All rights reserved.
//

import Cocoa

protocol DragNDropDelegate: class {
func dragFinished(content: String)
}

class DragNDropView: NSView {

weak var dragDelegate: DragNDropDelegate?

required init?(coder: NSCoder) {
super.init(coder: coder)
register(forDraggedTypes: [NSStringPboardType, NSFilenamesPboardType])
}

override func draggingEntered(_ sender: NSDraggingInfo) -> NSDragOperation {
return .copy
}

override func draggingUpdated(_ sender: NSDraggingInfo) -> NSDragOperation {
return .copy
}

override func performDragOperation(_ sender: NSDraggingInfo) -> Bool {
let pboard = sender.draggingPasteboard()

if pboard.types?.contains(NSStringPboardType) == true,
let str = pboard.string(forType: NSStringPboardType) {
dragDelegate?.dragFinished(content: str)
Swift.print(str)
return true
}

if pboard.types?.contains(NSFilenamesPboardType) == true,
let files = pboard.propertyList(forType: NSFilenamesPboardType) as? [Any],
let path = files.first as? String,
let str = try? String(contentsOf: URL(fileURLWithPath: path)) {
dragDelegate?.dragFinished(content: str)
Swift.print(str)
return true
}

return false
}

}
File renamed without changes.
File renamed without changes.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ You can download ZIP from [release](https://github.com/XQS6LB3A/LyricsX/releases
- Automatically search & download synchronized lyrics.
- Display lyrics on desktop and menubar. you can customize font, color and position for desktop lyrics.
- Adjust lyrics offset on status menu.
- Double click lyrics to set player position.
- Drag & Drop to import/export lyrics file.
- Auto launch & quit with music player.
- Auto convert Traditional Chinese or Simplified Chinese if you want.

Expand All @@ -37,3 +39,7 @@ You can download ZIP from [release](https://github.com/XQS6LB3A/LyricsX/releases
- [EasyPreference](https://github.com/XQS6LB3A/EasyPreference)
- [SwiftyJSON](https://github.com/SwiftyJSON/SwiftyJSON)
- [SnapKit](https://github.com/SnapKit/SnapKit)

## ⚠️ Disclaimer

All lyrics are property and copyright of their owners.

0 comments on commit 03da6be

Please sign in to comment.