From ba5f7cb040b5f3072f749770089889cd6b6931ac Mon Sep 17 00:00:00 2001 From: xjbeta Date: Sun, 5 Aug 2018 12:00:12 +0800 Subject: [PATCH 1/7] Bookmarks tableview darg and drop support. --- iina+.xcodeproj/project.pbxproj | 4 +- iina+/AppDelegate.swift | 23 +++++ iina+/Core Data/BookmarkExtension.swift | 9 +- iina+/Core Data/DataManager.swift | 34 +++++-- .../DataModel.xcdatamodeld/.xccurrentversion | 8 ++ .../Bookmark v0.2.xcdatamodel/contents | 11 +++ .../Bookmark.xcdatamodel/contents | 2 +- iina+/Views/Base.lproj/Main.storyboard | 1 + .../MainWindow/LiveStatusTableRowView.swift | 19 ++++ .../Views/MainWindow/MainViewController.swift | 94 +++++++++++++++++-- 10 files changed, 186 insertions(+), 19 deletions(-) create mode 100644 iina+/Core Data/DataModel.xcdatamodeld/.xccurrentversion create mode 100644 iina+/Core Data/DataModel.xcdatamodeld/Bookmark v0.2.xcdatamodel/contents diff --git a/iina+.xcodeproj/project.pbxproj b/iina+.xcodeproj/project.pbxproj index 844f8349..6cc3184d 100644 --- a/iina+.xcodeproj/project.pbxproj +++ b/iina+.xcodeproj/project.pbxproj @@ -86,6 +86,7 @@ 01479CD6210B35480046AAAD /* MainMenu.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MainMenu.swift; sourceTree = ""; }; 014B447B210069FF00E7AA6A /* Bookmark.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Bookmark.swift; sourceTree = ""; }; 014B447C210069FF00E7AA6A /* BookmarkExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BookmarkExtension.swift; sourceTree = ""; }; + 014FD3A42115CB3000F05399 /* Bookmark v0.2.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = "Bookmark v0.2.xcdatamodel"; sourceTree = ""; }; 0161CAF9210E010200AB5D9A /* SubString.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SubString.swift; sourceTree = ""; }; 01AEC8A720EDFD01001406E8 /* iina+.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "iina+.app"; sourceTree = BUILT_PRODUCTS_DIR; }; 01AEC8AA20EDFD01001406E8 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; @@ -550,9 +551,10 @@ 010F0F1720FE4F0900F33553 /* DataModel.xcdatamodeld */ = { isa = XCVersionGroup; children = ( + 014FD3A42115CB3000F05399 /* Bookmark v0.2.xcdatamodel */, 010F0F1820FE4F0900F33553 /* Bookmark.xcdatamodel */, ); - currentVersion = 010F0F1820FE4F0900F33553 /* Bookmark.xcdatamodel */; + currentVersion = 014FD3A42115CB3000F05399 /* Bookmark v0.2.xcdatamodel */; path = DataModel.xcdatamodeld; sourceTree = ""; versionGroupType = wrapper.xcdatamodel; diff --git a/iina+/AppDelegate.swift b/iina+/AppDelegate.swift index 7832286d..fc207f87 100644 --- a/iina+/AppDelegate.swift +++ b/iina+/AppDelegate.swift @@ -40,6 +40,8 @@ class AppDelegate: NSObject, NSApplicationDelegate { lazy var persistentContainer: NSPersistentContainer = { let container = NSPersistentContainer(name: "DataModel") + let shouldInitOrder = !checkForMigration() + container.loadPersistentStores { _, error in if let error = error { fatalError("Unresolved error \(error)") @@ -47,6 +49,18 @@ class AppDelegate: NSObject, NSApplicationDelegate { container.viewContext.automaticallyMergesChangesFromParent = true container.viewContext.mergePolicy = NSMergePolicy.mergeByPropertyObjectTrump } + + if shouldInitOrder { + // set dafault value for orders + if container.managedObjectModel.entitiesByName["Bookmark"]?.versionHashModifier == "added order" { + try? container.viewContext.fetch(NSFetchRequest(entityName: "Bookmark")).enumerated().forEach { + if let bookmark = $0.element as? Bookmark { + bookmark.setValue($0.offset, forKey: "order") + } + } + try? container.viewContext.save() + } + } return container }() @@ -118,5 +132,14 @@ class AppDelegate: NSObject, NSApplicationDelegate { return .terminateNow } + + func checkForMigration() -> Bool { + let container = NSPersistentContainer(name: "DataModel") + if let storeUrl = container.persistentStoreDescriptions.first?.url, + let metadata = try? NSPersistentStoreCoordinator.metadataForPersistentStore(ofType: NSSQLiteStoreType, at: storeUrl) { + return container.managedObjectModel.isConfiguration(withName: nil, compatibleWithStoreMetadata: metadata) + } + return false + } } diff --git a/iina+/Core Data/BookmarkExtension.swift b/iina+/Core Data/BookmarkExtension.swift index 33ae805b..2836a939 100644 --- a/iina+/Core Data/BookmarkExtension.swift +++ b/iina+/Core Data/BookmarkExtension.swift @@ -12,12 +12,13 @@ import CoreData extension Bookmark { - + @nonobjc public class func fetchRequest() -> NSFetchRequest { return NSFetchRequest(entityName: "Bookmark") } - + + @NSManaged public var order: Double @NSManaged public var remark: String? - @NSManaged public var url: String? - + @NSManaged public var url: String + } diff --git a/iina+/Core Data/DataManager.swift b/iina+/Core Data/DataManager.swift index 2c265adb..1f89e698 100644 --- a/iina+/Core Data/DataManager.swift +++ b/iina+/Core Data/DataManager.swift @@ -10,14 +10,12 @@ import Cocoa class DataManager: NSObject { - var appDelegate: AppDelegate { - return NSApp.delegate as! AppDelegate - } + let context = (NSApp.delegate as! AppDelegate).persistentContainer.viewContext + let sortDescriptors = [NSSortDescriptor(key: #keyPath(Bookmark.order), ascending: true)] func requestData() -> [Bookmark] { - let context = appDelegate.persistentContainer.viewContext let request = NSFetchRequest(entityName: "Bookmark") - + request.sortDescriptors = sortDescriptors if let fetch = try? context.fetch(request), let re = fetch as? [Bookmark] { return re @@ -26,19 +24,41 @@ class DataManager: NSObject { } func addBookmark(_ str: String) { - let context = appDelegate.persistentContainer.viewContext let newBookmark = Bookmark(context: context) newBookmark.url = str + if let last = requestData().last { + newBookmark.order = last.order + 1 + } else { + newBookmark.order = 0 + } try? context.save() } func deleteBookmark(_ index: Int) { - let context = appDelegate.persistentContainer.viewContext let bookmark = requestData()[index] context.delete(bookmark) try? context.save() } + func moveBookmark(at oldIndex: Int, to newIndex: Int) { + let bookmarks = requestData() + let oldBookmark = bookmarks[oldIndex] + + switch newIndex { + case 0: + oldBookmark.order = bookmarks[0].order - 1 + case bookmarks.count - 1: + oldBookmark.order = bookmarks[newIndex].order + 1 + case _ where newIndex > oldIndex: + oldBookmark.order = (bookmarks[newIndex].order + bookmarks[newIndex + 1].order) / 2 + case _ where newIndex < oldIndex: + oldBookmark.order = (bookmarks[newIndex].order + bookmarks[newIndex - 1].order) / 2 + default: + break + } + try? context.save() + } + func checkURL(_ url: String) -> Bool { do { let detector = try NSDataDetector(types: NSTextCheckingResult.CheckingType.link.rawValue) diff --git a/iina+/Core Data/DataModel.xcdatamodeld/.xccurrentversion b/iina+/Core Data/DataModel.xcdatamodeld/.xccurrentversion new file mode 100644 index 00000000..d123ccbe --- /dev/null +++ b/iina+/Core Data/DataModel.xcdatamodeld/.xccurrentversion @@ -0,0 +1,8 @@ + + + + + _XCCurrentVersionName + Bookmark v0.2.xcdatamodel + + diff --git a/iina+/Core Data/DataModel.xcdatamodeld/Bookmark v0.2.xcdatamodel/contents b/iina+/Core Data/DataModel.xcdatamodeld/Bookmark v0.2.xcdatamodel/contents new file mode 100644 index 00000000..44d5607e --- /dev/null +++ b/iina+/Core Data/DataModel.xcdatamodeld/Bookmark v0.2.xcdatamodel/contents @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/iina+/Core Data/DataModel.xcdatamodeld/Bookmark.xcdatamodel/contents b/iina+/Core Data/DataModel.xcdatamodeld/Bookmark.xcdatamodel/contents index c57b3034..e0d2477d 100644 --- a/iina+/Core Data/DataModel.xcdatamodeld/Bookmark.xcdatamodel/contents +++ b/iina+/Core Data/DataModel.xcdatamodeld/Bookmark.xcdatamodel/contents @@ -1,5 +1,5 @@ - + diff --git a/iina+/Views/Base.lproj/Main.storyboard b/iina+/Views/Base.lproj/Main.storyboard index fe74b410..9e120e4c 100644 --- a/iina+/Views/Base.lproj/Main.storyboard +++ b/iina+/Views/Base.lproj/Main.storyboard @@ -557,6 +557,7 @@ + diff --git a/iina+/Views/MainWindow/LiveStatusTableRowView.swift b/iina+/Views/MainWindow/LiveStatusTableRowView.swift index 934bd3be..b285a639 100644 --- a/iina+/Views/MainWindow/LiveStatusTableRowView.swift +++ b/iina+/Views/MainWindow/LiveStatusTableRowView.swift @@ -35,6 +35,25 @@ class LiveStatusTableRowView: NSTableRowView { let defaultRowColor = NSColor(catalogName: "System", colorName: "controlAlternatingRowColor") + +} + + +extension NSView { + + public func screenshot( _ rect:CGRect?=nil ) -> NSImage { + + let image = NSImage() + let rect = rect ?? self.bounds + + if let bitmap = self.bitmapImageRepForCachingDisplay( in: rect ) { + self.cacheDisplay( in: rect, to: bitmap ) + image.addRepresentation( bitmap ) + } + + return image + } + } extension NSColor { diff --git a/iina+/Views/MainWindow/MainViewController.swift b/iina+/Views/MainWindow/MainViewController.swift index 29eebd42..dd85f62a 100644 --- a/iina+/Views/MainWindow/MainViewController.swift +++ b/iina+/Views/MainWindow/MainViewController.swift @@ -9,6 +9,10 @@ import Cocoa import CoreData +private extension NSPasteboard.PasteboardType { + static let bookmarkRow = NSPasteboard.PasteboardType("bookmark.Row") +} + class MainViewController: NSViewController { @IBOutlet weak var searchField: NSSearchField! @@ -25,10 +29,11 @@ class MainViewController: NSViewController { } @IBOutlet weak var bookmarkTableView: NSTableView! + @IBOutlet var bookmarkArrayController: NSArrayController! @IBAction func sendURL(_ sender: Any) { - if bookmarkTableView.selectedRow != -1, - let url = dataManager.requestData()[bookmarkTableView.selectedRow].url { + if bookmarkTableView.selectedRow != -1 { + let url = dataManager.requestData()[bookmarkTableView.selectedRow].url searchField.stringValue = url searchField.becomeFirstResponder() searchField(self) @@ -60,7 +65,10 @@ class MainViewController: NSViewController { override func viewDidLoad() { super.viewDidLoad() + bookmarkArrayController.sortDescriptors = dataManager.sortDescriptors bookmarkTableView.backgroundColor = .clear + bookmarkTableView.registerForDraggedTypes([.bookmarkRow]) + bookmarkTableView.draggingDestinationFeedbackStyle = .gap NotificationCenter.default.addObserver(self, selector: #selector(reloadBookmarks), name: .reloadLiveStatus, object: nil) } @@ -94,8 +102,8 @@ extension MainViewController: NSTableViewDelegate, NSTableViewDataSource { } func tableView(_ tableView: NSTableView, heightOfRow row: Int) -> CGFloat { - if let str = dataManager.requestData()[row].url, - let url = URL(string: str) { + let str = dataManager.requestData()[row].url + if let url = URL(string: str) { switch LiveSupportList(raw: url.host) { case .unsupported: return 17 @@ -107,8 +115,8 @@ extension MainViewController: NSTableViewDelegate, NSTableViewDataSource { } func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? { - if let str = dataManager.requestData()[row].url, - let url = URL(string: str) { + let str = dataManager.requestData()[row].url + if let url = URL(string: str) { switch LiveSupportList(raw: url.host) { case .unsupported: if let view = tableView.makeView(withIdentifier: .liveUrlTableCell, owner: nil) as? NSTableCellView { @@ -142,6 +150,80 @@ extension MainViewController: NSTableViewDelegate, NSTableViewDataSource { return tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier("LiveStatusTableRowView"), owner: self) as? LiveStatusTableRowView } + + + func tableView(_ tableView: NSTableView, draggingSession session: NSDraggingSession, willBeginAt screenPoint: NSPoint, forRowIndexes rowIndexes: IndexSet) { + guard let row: Int = rowIndexes.first, + let view = tableView.view(atColumn: 0, row: row, makeIfNecessary: false) else { + return + } + let image = view.screenshot() + + session.enumerateDraggingItems(options: .concurrent, for: tableView, classes: [NSPasteboardItem.self], searchOptions: [:]) { draggingItem, idx, stop in + + var rect = NSRect(origin: draggingItem.draggingFrame.origin, size: image.size) + rect.origin.y -= rect.size.height + rect.origin.y += draggingItem.draggingFrame.size.height + draggingItem.draggingFrame = rect + + let backgroundImageComponent = NSDraggingImageComponent(key: NSDraggingItem.ImageComponentKey(rawValue: "background")) + backgroundImageComponent.contents = image + backgroundImageComponent.frame = NSRect(origin: NSZeroPoint, size: image.size) + draggingItem.imageComponentsProvider = { + return [backgroundImageComponent] + } + + + } + } + + + func tableView(_ tableView: NSTableView, pasteboardWriterForRow row: Int) -> NSPasteboardWriting? { + let item = NSPasteboardItem() + item.setString(String(row), forType: .bookmarkRow) + return item + } + + func tableView(_ tableView: NSTableView, validateDrop info: NSDraggingInfo, proposedRow row: Int, proposedDropOperation dropOperation: NSTableView.DropOperation) -> NSDragOperation { + if dropOperation == .above { + return .move + } + return [] + } + + func tableView(_ tableView: NSTableView, acceptDrop info: NSDraggingInfo, row: Int, dropOperation: NSTableView.DropOperation) -> Bool { + var oldRows: [Int] = [] + info.enumerateDraggingItems(options: [], for: tableView, classes: [NSPasteboardItem.self], searchOptions: [:]) { + (draggingItem, idx, stop) in + guard let item = draggingItem.item as? NSPasteboardItem else { return } + guard let rowStr = item.string(forType: .bookmarkRow) else { return } + guard let row = Int(rowStr) else { return } + oldRows.append(row) + } + guard oldRows.count == 1, let oldRow = oldRows.first else { + return false + } + + + + tableView.beginUpdates() + + + if let objs = bookmarkArrayController.arrangedObjects as? [Bookmark] { + if oldRow < row { + dataManager.moveBookmark(at: oldRow, to: row - 1) + tableView.moveRow(at: oldRow, to: row - 1) + } else { + dataManager.moveBookmark(at: oldRow, to: row) + tableView.moveRow(at: oldRow, to: row) + } + } + tableView.endUpdates() + + return true + } + + } extension MainViewController: NSSearchFieldDelegate { From 55231ba148872158b8eacfcd719d8c9b5e050f28 Mon Sep 17 00:00:00 2001 From: xjbeta Date: Sun, 5 Aug 2018 12:33:34 +0800 Subject: [PATCH 2/7] Remove useless value. --- iina+/Views/MainWindow/MainViewController.swift | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/iina+/Views/MainWindow/MainViewController.swift b/iina+/Views/MainWindow/MainViewController.swift index dd85f62a..c82855d0 100644 --- a/iina+/Views/MainWindow/MainViewController.swift +++ b/iina+/Views/MainWindow/MainViewController.swift @@ -200,23 +200,18 @@ extension MainViewController: NSTableViewDelegate, NSTableViewDataSource { guard let row = Int(rowStr) else { return } oldRows.append(row) } + guard oldRows.count == 1, let oldRow = oldRows.first else { return false } - - tableView.beginUpdates() - - - if let objs = bookmarkArrayController.arrangedObjects as? [Bookmark] { if oldRow < row { dataManager.moveBookmark(at: oldRow, to: row - 1) tableView.moveRow(at: oldRow, to: row - 1) } else { dataManager.moveBookmark(at: oldRow, to: row) tableView.moveRow(at: oldRow, to: row) - } } tableView.endUpdates() From e40e5f4037e6bdca9e69d94a683d6eab962dc00f Mon Sep 17 00:00:00 2001 From: xjbeta Date: Sun, 5 Aug 2018 12:54:05 +0800 Subject: [PATCH 3/7] Move background color draw to LiveStatusCellView. --- iina+/Views/Base.lproj/Main.storyboard | 4 +- .../MainWindow/LiveStatusTableCellView.swift | 35 ++++++++++++++- .../MainWindow/LiveStatusTableRowView.swift | 45 +++---------------- .../Views/MainWindow/MainViewController.swift | 14 +++--- 4 files changed, 49 insertions(+), 49 deletions(-) diff --git a/iina+/Views/Base.lproj/Main.storyboard b/iina+/Views/Base.lproj/Main.storyboard index 9e120e4c..c7c06bf7 100644 --- a/iina+/Views/Base.lproj/Main.storyboard +++ b/iina+/Views/Base.lproj/Main.storyboard @@ -450,7 +450,7 @@ - + @@ -501,7 +501,7 @@ - + diff --git a/iina+/Views/MainWindow/LiveStatusTableCellView.swift b/iina+/Views/MainWindow/LiveStatusTableCellView.swift index 3cd8d7d2..50eec705 100644 --- a/iina+/Views/MainWindow/LiveStatusTableCellView.swift +++ b/iina+/Views/MainWindow/LiveStatusTableCellView.swift @@ -14,10 +14,24 @@ class LiveStatusTableCellView: NSTableCellView { @IBOutlet weak var liveStatusImageView: NSImageView! @IBOutlet weak var titleTextField: NSTextField! @IBOutlet weak var nameTextField: NSTextField! + + var isSelected: Bool = false { + didSet { + needsDisplay = true + } + } + override func draw(_ dirtyRect: NSRect) { super.draw(dirtyRect) - // Drawing code here. + let selectionRect = NSInsetRect(bounds, 0, 0) + let selectionPath = NSBezierPath(roundedRect: selectionRect, xRadius: 6, yRadius: 6) + if isSelected { + NSColor.selectedControlColor.setFill() + } else { + NSColor.white.setFill() + } + selectionPath.fill() } func resetInfo() { @@ -36,4 +50,23 @@ class LiveStatusTableCellView: NSTableCellView { } } + + func screenshot(_ rect: CGRect? = nil) -> NSImage { + let image = NSImage() + let rect = rect ?? self.bounds + + if let bitmap = self.bitmapImageRepForCachingDisplay( in: rect ) { + self.cacheDisplay( in: rect, to: bitmap ) + image.addRepresentation( bitmap ) + } + + return image + } + +} + +extension NSColor { + public class var customBackgroundColor: NSColor { + return NSColor(calibratedRed: 0.97, green: 0.95, blue: 0.94, alpha: 1) + } } diff --git a/iina+/Views/MainWindow/LiveStatusTableRowView.swift b/iina+/Views/MainWindow/LiveStatusTableRowView.swift index b285a639..eb1ae54a 100644 --- a/iina+/Views/MainWindow/LiveStatusTableRowView.swift +++ b/iina+/Views/MainWindow/LiveStatusTableRowView.swift @@ -12,52 +12,19 @@ class LiveStatusTableRowView: NSTableRowView { override func draw(_ dirtyRect: NSRect) { super.draw(dirtyRect) - drawSelection(in: bounds) } override var isSelected: Bool { didSet { - drawSelection(in: bounds) - needsDisplay = true + if let cell = subviews.first as? LiveStatusTableCellView { + cell.isSelected = isSelected + } } } - override func drawSelection(in dirtyRect: NSRect) { - let selectionRect = NSInsetRect(bounds, 4, 4) - let selectionPath = NSBezierPath(roundedRect: selectionRect, xRadius: 6, yRadius: 6) - if isSelected { - NSColor.selectedControlColor.setFill() - } else { - NSColor.white.setFill() - } - selectionPath.fill() - } - - let defaultRowColor = NSColor(catalogName: "System", colorName: "controlAlternatingRowColor") - -} - - -extension NSView { - - public func screenshot( _ rect:CGRect?=nil ) -> NSImage { - - let image = NSImage() - let rect = rect ?? self.bounds - - if let bitmap = self.bitmapImageRepForCachingDisplay( in: rect ) { - self.cacheDisplay( in: rect, to: bitmap ) - image.addRepresentation( bitmap ) - } - - return image - } - -} - -extension NSColor { - public class var customBackgroundColor: NSColor { - return NSColor(calibratedRed: 0.97, green: 0.95, blue: 0.94, alpha: 1) + override func drawFocusRingMask() { + let selectionRect = NSInsetRect(bounds, 0, 0) + NSBezierPath(roundedRect: selectionRect, xRadius: 6, yRadius: 6).fill() } } diff --git a/iina+/Views/MainWindow/MainViewController.swift b/iina+/Views/MainWindow/MainViewController.swift index c82855d0..df2cff0a 100644 --- a/iina+/Views/MainWindow/MainViewController.swift +++ b/iina+/Views/MainWindow/MainViewController.swift @@ -154,7 +154,7 @@ extension MainViewController: NSTableViewDelegate, NSTableViewDataSource { func tableView(_ tableView: NSTableView, draggingSession session: NSDraggingSession, willBeginAt screenPoint: NSPoint, forRowIndexes rowIndexes: IndexSet) { guard let row: Int = rowIndexes.first, - let view = tableView.view(atColumn: 0, row: row, makeIfNecessary: false) else { + let view = tableView.view(atColumn: 0, row: row, makeIfNecessary: false) as? LiveStatusTableCellView else { return } let image = view.screenshot() @@ -206,12 +206,12 @@ extension MainViewController: NSTableViewDelegate, NSTableViewDataSource { } tableView.beginUpdates() - if oldRow < row { - dataManager.moveBookmark(at: oldRow, to: row - 1) - tableView.moveRow(at: oldRow, to: row - 1) - } else { - dataManager.moveBookmark(at: oldRow, to: row) - tableView.moveRow(at: oldRow, to: row) + if oldRow < row { + dataManager.moveBookmark(at: oldRow, to: row - 1) + tableView.moveRow(at: oldRow, to: row - 1) + } else { + dataManager.moveBookmark(at: oldRow, to: row) + tableView.moveRow(at: oldRow, to: row) } tableView.endUpdates() From f2ed1ad976db07924a8fe6f19f2c2270b845151a Mon Sep 17 00:00:00 2001 From: xjbeta Date: Sun, 5 Aug 2018 18:31:43 +0800 Subject: [PATCH 4/7] Add undo redo support for bookmarkTableView. --- iina+/Views/Base.lproj/Main.storyboard | 4 +-- iina+/Views/MainMenu.swift | 32 +++++++++++++++++++ .../Views/MainWindow/MainViewController.swift | 3 +- 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/iina+/Views/Base.lproj/Main.storyboard b/iina+/Views/Base.lproj/Main.storyboard index c7c06bf7..57c00e9e 100644 --- a/iina+/Views/Base.lproj/Main.storyboard +++ b/iina+/Views/Base.lproj/Main.storyboard @@ -128,12 +128,12 @@ - + - + diff --git a/iina+/Views/MainMenu.swift b/iina+/Views/MainMenu.swift index 348f8f67..2fb1fc65 100644 --- a/iina+/Views/MainMenu.swift +++ b/iina+/Views/MainMenu.swift @@ -9,8 +9,40 @@ import Cocoa class MainMenu: NSObject { + override func validateMenuItem(_ menuItem: NSMenuItem) -> Bool { + + if let window = NSApp.keyWindow, + let appDelegate = NSApp.delegate as? AppDelegate, + let undoManager = appDelegate.persistentContainer.viewContext.undoManager { + + if menuItem.action == #selector(undo) { + return window.windowController is MainWindowController && undoManager.canUndo + } + if menuItem.action == #selector(redo) { + return window.windowController is MainWindowController && undoManager.canRedo + } + } + + if menuItem.action == #selector(reloadLiveStatus) { + return NSApp.keyWindow?.windowController is MainWindowController + } + return false + } + @IBAction func reloadLiveStatus(_ sender: Any) { NotificationCenter.default.post(name: .reloadLiveStatus, object: nil) } + @IBAction func undo(_ sender: Any) { + if let appDelegate = NSApp.delegate as? AppDelegate { + appDelegate.persistentContainer.viewContext.undo() + } + } + + @IBAction func redo(_ sender: Any) { + if let appDelegate = NSApp.delegate as? AppDelegate { + appDelegate.persistentContainer.viewContext.redo() + } + } + } diff --git a/iina+/Views/MainWindow/MainViewController.swift b/iina+/Views/MainWindow/MainViewController.swift index df2cff0a..e265c58a 100644 --- a/iina+/Views/MainWindow/MainViewController.swift +++ b/iina+/Views/MainWindow/MainViewController.swift @@ -59,7 +59,8 @@ class MainViewController: NSViewController { @objc var bookmarks: NSManagedObjectContext required init?(coder: NSCoder) { - self.bookmarks = (NSApp.delegate as! AppDelegate).persistentContainer.viewContext + bookmarks = (NSApp.delegate as! AppDelegate).persistentContainer.viewContext + bookmarks.undoManager = UndoManager() super.init(coder: coder) } From fbc1916bf5c10b789a8df73986e4506337649f6d Mon Sep 17 00:00:00 2001 From: xjbeta Date: Sun, 5 Aug 2018 18:53:11 +0800 Subject: [PATCH 5/7] Fix search bar constraints. --- iina+/Views/Base.lproj/Main.storyboard | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/iina+/Views/Base.lproj/Main.storyboard b/iina+/Views/Base.lproj/Main.storyboard index 57c00e9e..8ce988e9 100644 --- a/iina+/Views/Base.lproj/Main.storyboard +++ b/iina+/Views/Base.lproj/Main.storyboard @@ -386,7 +386,7 @@ - + @@ -547,9 +547,9 @@ - + - + From e9eb9cc011ce6cca62c72e8178106e08adff922a Mon Sep 17 00:00:00 2001 From: xjbeta Date: Sun, 5 Aug 2018 23:02:47 +0800 Subject: [PATCH 6/7] Fix IINA auto insert "--stdin". --- iina+/Processes.swift | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/iina+/Processes.swift b/iina+/Processes.swift index 0a92c9dc..de72a7e9 100644 --- a/iina+/Processes.swift +++ b/iina+/Processes.swift @@ -80,7 +80,8 @@ class Processes: NSObject { func openWithPlayer(_ url: String, title: String) { let task = Process() let pipe = Pipe() - task.standardOutput = pipe +// task.standardOutput = pipe + task.standardInput = pipe var mpvArgs = ["\(MPVOption.Miscellaneous.forceMediaTitle)=\(title)"] if url.contains("douyu") { From 3f9ef29e901c645e500260d8bf664b5d63cd1996 Mon Sep 17 00:00:00 2001 From: xjbeta Date: Sun, 5 Aug 2018 23:07:53 +0800 Subject: [PATCH 7/7] Bump version to v0.1-build4. --- iina+/Info.plist | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iina+/Info.plist b/iina+/Info.plist index 04e42730..2a5fc560 100644 --- a/iina+/Info.plist +++ b/iina+/Info.plist @@ -19,7 +19,7 @@ CFBundleShortVersionString 0.1 CFBundleVersion - 3 + 4 LSApplicationCategoryType public.app-category.video LSMinimumSystemVersion