diff --git a/Simplenote/NoteListViewController.swift b/Simplenote/NoteListViewController.swift index 5b261641f..cc1033342 100644 --- a/Simplenote/NoteListViewController.swift +++ b/Simplenote/NoteListViewController.swift @@ -875,9 +875,12 @@ extension NoteListViewController { guard let note = listController.note(at: tableView.selectedRow) else { return } - let windowController = NoteWindowController() - SimplenoteAppDelegate.shared().noteWindowsManager.windowControllers.append(windowController) + let window = NoteWindow() + let windowController = NSWindowController(window: window) - windowController.show(note) + window.show(note) + + tableView.deselectRow(tableView.selectedRow) + refreshPresentedNote() } } diff --git a/Simplenote/NoteWindow.swift b/Simplenote/NoteWindow.swift index 2271d0d41..785fa61cf 100644 --- a/Simplenote/NoteWindow.swift +++ b/Simplenote/NoteWindow.swift @@ -9,7 +9,11 @@ import Foundation class NoteWindow: NSWindow { - private let editor: NoteEditorViewController + let editor: NoteEditorViewController + + var selectedNoteID: String? { + editor.note?.simperiumKey + } init() { let storyboard = NSStoryboard(name: .main, bundle: nil) @@ -30,23 +34,13 @@ class NoteWindow: NSWindow { editor.metadataCache = editorCache } - // The note windows are stored in the windows manager and need to be removed when they close - // This override cleans up the windows in the manager - override func close() { - super.close() - let noteWindowsManager = SimplenoteAppDelegate.shared().noteWindowsManager - if let controller = windowController, - let index = noteWindowsManager.windowControllers.firstIndex(of: controller) { - noteWindowsManager.windowControllers.remove(at: index) - } - } - - - //MARK: Show Note + // MARK: Show Note // func show(_ note: Note) { editor.toolbarView.sidebarButton.isHidden = true editor.displayNote(note) title = note.titlePreview + + windowController?.showWindow(self) } } diff --git a/Simplenote/SimplenoteAppDelegate+Swift.swift b/Simplenote/SimplenoteAppDelegate+Swift.swift index 4d7f24937..108793268 100644 --- a/Simplenote/SimplenoteAppDelegate+Swift.swift +++ b/Simplenote/SimplenoteAppDelegate+Swift.swift @@ -89,11 +89,6 @@ extension SimplenoteAppDelegate { noteEditorViewController.editorDelegate = self } - @objc - func configureNoteWindowsManager() { - noteWindowsManager = NoteWindowsManager() - } - @objc func configureEditorMetadataCache() { let documentsDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first! @@ -471,8 +466,13 @@ extension SimplenoteAppDelegate: NotesControllerDelegate { } func notesController(_ controller: NoteListViewController, didSelect note: Note) { - breadcrumbsViewController.notesControllerDidSelectNote(note) - noteEditorViewController.displayNote(note) + if let noteWindow = noteWindow(for: note) { + notesControllerDidSelectZeroNotes(controller) + noteWindow.makeKeyAndOrderFront(nil) + } else { + breadcrumbsViewController.notesControllerDidSelectNote(note) + noteEditorViewController.displayNote(note) + } } func notesController(_ controller: NoteListViewController, didSelect notes: [Note]) { @@ -516,6 +516,22 @@ extension SimplenoteAppDelegate { } } +// MARK: - Window management +// +extension SimplenoteAppDelegate { + var windows: [NSWindow] { + NSApplication.shared.windows + } + + var noteWindows: [NoteWindow] { + windows.compactMap({ $0 as? NoteWindow }) + } + + func noteWindow(for note: Note) -> NoteWindow? { + noteWindows.first(where: { $0.selectedNoteID == note.simperiumKey }) + } +} + // MARK: - Constants // private struct Constants { diff --git a/Simplenote/SimplenoteAppDelegate.h b/Simplenote/SimplenoteAppDelegate.h index 828ab9b18..5953f6f00 100644 --- a/Simplenote/SimplenoteAppDelegate.h +++ b/Simplenote/SimplenoteAppDelegate.h @@ -20,7 +20,6 @@ @class VersionsController; @class NoteEditorMetadataCache; @class AccountDeletionController; -@class NoteWindowsManager; NS_ASSUME_NONNULL_BEGIN @@ -51,7 +50,6 @@ NS_ASSUME_NONNULL_BEGIN @property (strong, nonatomic) NoteEditorMetadataCache *noteEditorMetadataCache; @property (nullable, strong, nonatomic) NSWindowController *preferencesWindowController; -@property (strong, nonatomic) NoteWindowsManager *noteWindowsManager; + (SimplenoteAppDelegate *)sharedDelegate; diff --git a/Simplenote/SimplenoteAppDelegate.m b/Simplenote/SimplenoteAppDelegate.m index 5ab71270a..50653ec48 100644 --- a/Simplenote/SimplenoteAppDelegate.m +++ b/Simplenote/SimplenoteAppDelegate.m @@ -97,7 +97,6 @@ - (void)applicationWillFinishLaunching:(NSNotification *)notification [self configureTagsController]; [self configureNotesController]; [self configureEditorController]; - [self configureNoteWindowsManager]; [self configureVerificationCoordinator]; [self configureVersionsController]; [self configureAccountDeletionController];